( initialState: boolean | (() => boolean) = true )
| 347 | } |
| 348 | |
| 349 | export function useSlot( |
| 350 | initialState: boolean | (() => boolean) = true |
| 351 | ): [RefCallback<Element>, boolean] { |
| 352 | // Initial state is typically based on the parent having an aria-label or aria-labelledby. |
| 353 | // 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 |
| 354 | let [hasSlot, setHasSlot] = useState(initialState); |
| 355 | let hasRun = useRef(false); |
| 356 | |
| 357 | // A callback ref which will run when the slotted element mounts. |
| 358 | // This should happen before the useLayoutEffect below. |
| 359 | let ref = useCallback(el => { |
| 360 | hasRun.current = true; |
| 361 | setHasSlot(!!el); |
| 362 | }, []); |
| 363 | |
| 364 | // If the callback hasn't been called, then reset to false. |
| 365 | useLayoutEffect(() => { |
| 366 | if (!hasRun.current) { |
| 367 | setHasSlot(false); |
| 368 | } |
| 369 | }, []); |
| 370 | |
| 371 | return [ref, hasSlot]; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Filters out `data-*` attributes to keep them from being passed down and duplicated. |
no outgoing calls
no test coverage detected