UNPKG

@base-ui/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

27 lines (26 loc) 1.12 kB
'use client'; import * as React from 'react'; import { useSyncExternalStore } from 'use-sync-external-store/shim'; import { NOOP } from '@base-ui/utils/empty'; /** * Reads the store currently exposed by a popup handle and subscribes to store-pointer changes. * Detached triggers use this to follow a handle as a root attaches or detaches: while no root is * attached, the handle exposes its fallback store; once a root attaches, subscribers re-render and * read from the live root store. * * Returns `undefined` when no handle is provided so callers can fall back to their root context. * * @param handle The popup handle to read from, or `undefined` when the trigger is not handle-bound. */ export function usePopupHandleStore(handle) { const subscribe = React.useCallback(listener => { if (handle === undefined) { return NOOP; } return handle.subscribeStore(listener); }, [handle]); const getSnapshot = React.useCallback(() => { return handle === undefined ? undefined : handle.store; }, [handle]); return useSyncExternalStore(subscribe, getSnapshot, () => handle?.serverStore); }