1
1
import * as React from 'react' ;
2
- import { getNativeElementProps , isResolvedShorthand , resolveShorthand , useId } from '@fluentui/react-utilities' ;
2
+ import {
3
+ getNativeElementProps ,
4
+ isResolvedShorthand ,
5
+ resolveShorthand ,
6
+ useControllableState ,
7
+ useId ,
8
+ } from '@fluentui/react-utilities' ;
3
9
import { ChevronRight12Regular } from '@fluentui/react-icons' ;
4
10
import { useFluent_unstable } from '@fluentui/react-shared-contexts' ;
5
11
import { useEventCallback } from '@fluentui/react-utilities' ;
@@ -47,6 +53,8 @@ export function useTreeItem_unstable<Value = string>(
47
53
const requestOpenChange = useTreeContext_unstable ( ctx => ctx . requestOpenChange ) ;
48
54
const requestNavigation = useTreeContext_unstable ( ctx => ctx . requestNavigation ) ;
49
55
56
+ const [ active , setActive ] = useControllableState ( { state : props . active , initialState : false } ) ;
57
+
50
58
const isBranch = ! isLeaf ;
51
59
52
60
const open = useTreeContext_unstable ( ctx => isBranch && ctx . openItems . has ( value ) ) ;
@@ -148,17 +156,16 @@ export function useTreeItem_unstable<Value = string>(
148
156
}
149
157
} ) ;
150
158
151
- const [ isActionsVisible , setActionsVisible ] = React . useState ( false ) ;
152
159
const showActions = useEventCallback ( ( event : React . SyntheticEvent ) => {
153
160
const isEventFromSubtree = subtreeRef . current && elementContains ( subtreeRef . current , event . target as Node ) ;
154
161
if ( ! isEventFromSubtree ) {
155
- setActionsVisible ( true ) ;
162
+ setActive ( true ) ;
156
163
}
157
164
} ) ;
158
165
const hideActions = useEventCallback ( ( event : React . SyntheticEvent ) => {
159
166
const isEventFromSubtree = subtreeRef . current && elementContains ( subtreeRef . current , event . target as Node ) ;
160
167
if ( ! isEventFromSubtree ) {
161
- setActionsVisible ( false ) ;
168
+ setActive ( false ) ;
162
169
}
163
170
} ) ;
164
171
@@ -167,21 +174,22 @@ export function useTreeItem_unstable<Value = string>(
167
174
React . useEffect ( ( ) => {
168
175
if ( actionsRef . current ) {
169
176
const handleFocusOut = ( event : FocusEvent ) => {
170
- setActionsVisible ( elementContains ( actionsRef . current , event . relatedTarget as Node ) ) ;
177
+ setActive ( elementContains ( actionsRef . current , event . relatedTarget as Node ) ) ;
171
178
} ;
172
179
targetDocument ?. addEventListener ( 'focusout' , handleFocusOut , { passive : true } ) ;
173
180
return ( ) => {
174
181
targetDocument ?. removeEventListener ( 'focusout' , handleFocusOut ) ;
175
182
} ;
176
183
}
184
+ // eslint-disable-next-line react-hooks/exhaustive-deps
177
185
} , [ targetDocument ] ) ;
178
186
179
187
return {
180
188
isLeaf,
181
189
open,
182
190
level,
183
191
buttonSize : 'small' ,
184
- isActionsVisible : actions ? isActionsVisible : false ,
192
+ active : actions ? active : false ,
185
193
components : {
186
194
content : 'div' ,
187
195
root : 'div' ,
0 commit comments