( initialState: boolean | (() => boolean) = true )
| 362 | } |
| 363 | |
| 364 | export function useSlot( |
| 365 | initialState: boolean | (() => boolean) = true |
| 366 | ): [RefCallback<Element>, boolean] { |
| 367 | // Initial state is typically based on the parent having an aria-label or aria-labelledby. |
| 368 | // If it does, this value should be false so that we don't update the state and cause a rerender when we go through the layoutEffect |
| 369 | let [hasSlot, setHasSlot] = useState(initialState); |
| 370 | let hasRun = useRef(false); |
| 371 | |
| 372 | // A callback ref which will run when the slotted element mounts. |
| 373 | // This should happen before the useLayoutEffect below. |
| 374 | let ref = useCallback(el => { |
| 375 | hasRun.current = true; |
| 376 | setHasSlot(!!el); |
| 377 | }, []); |
| 378 | |
| 379 | // If the callback hasn't been called, then reset to false. |
| 380 | useLayoutEffect(() => { |
| 381 | if (!hasRun.current) { |
| 382 | setHasSlot(false); |
| 383 | } |
| 384 | }, []); |
| 385 | |
| 386 | return [ref, hasSlot]; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Filters out `data-*` attributes to keep them from being passed down and duplicated. |
no outgoing calls
no test coverage detected