Files
furniture/node_modules/its-fine/dist/index.cjs.map
T
2026-10-17 12:09:36 -04:00

1 line
17 KiB
Plaintext

{"version":3,"file":"index.cjs","sources":["../src/index.tsx"],"sourcesContent":["import React from 'react'\nimport type ReactReconciler from 'react-reconciler'\n\n/**\n * An SSR-friendly useLayoutEffect.\n *\n * React currently throws a warning when using useLayoutEffect on the server.\n * To get around it, we can conditionally useEffect on the server (no-op) and\n * useLayoutEffect elsewhere.\n *\n * @see https://github.com/facebook/react/issues/14927\n */\nconst useIsomorphicLayoutEffect = /* @__PURE__ */ (() =>\n typeof window !== 'undefined' && (window.document?.createElement || window.navigator?.product === 'ReactNative'))()\n ? React.useLayoutEffect\n : React.useEffect\n\n/**\n * Represents a react-internal Fiber node.\n */\nexport type Fiber<T = any> = Omit<ReactReconciler.Fiber, 'stateNode'> & { stateNode: T }\n\n/**\n * Represents a {@link Fiber} node selector for traversal.\n */\nexport type FiberSelector<T = any> = (\n /** The current {@link Fiber} node. */\n node: Fiber<T | null>,\n) => boolean | void\n\n/**\n * Traverses up or down a {@link Fiber}, return `true` to stop and select a node.\n */\nexport function traverseFiber<T = any>(\n /** Input {@link Fiber} to traverse. */\n fiber: Fiber | undefined,\n /** Whether to ascend and walk up the tree. Will walk down if `false`. */\n ascending: boolean,\n /** A {@link Fiber} node selector, returns the first match when `true` is passed. */\n selector: FiberSelector<T>,\n): Fiber<T> | undefined {\n if (!fiber) return\n if (selector(fiber) === true) return fiber\n\n let child = ascending ? fiber.return : fiber.child\n while (child) {\n const match = traverseFiber(child, ascending, selector)\n if (match) return match\n\n child = ascending ? null : child.sibling\n }\n}\n\n// In development, React will warn about using contexts between renderers.\n// Hide the warning because its-fine fixes this issue\n// https://github.com/facebook/react/pull/12779\nfunction wrapContext<T>(context: React.Context<T>): React.Context<T> {\n try {\n return Object.defineProperties(context, {\n _currentRenderer: {\n get() {\n return null\n },\n set() {},\n },\n _currentRenderer2: {\n get() {\n return null\n },\n set() {},\n },\n })\n } catch (_) {\n return context\n }\n}\n\nconst FiberContext = /* @__PURE__ */ wrapContext(/* @__PURE__ */ React.createContext<Fiber>(null!))\n\n/**\n * A react-internal {@link Fiber} provider. This component binds React children to the React Fiber tree. Call its-fine hooks within this.\n */\nexport class FiberProvider extends React.Component<{ children?: React.ReactNode }> {\n private _reactInternals!: Fiber\n\n render() {\n return <FiberContext.Provider value={this._reactInternals}>{this.props.children}</FiberContext.Provider>\n }\n}\n\n/**\n * Returns the current react-internal {@link Fiber}. This is an implementation detail of [react-reconciler](https://github.com/facebook/react/tree/main/packages/react-reconciler).\n */\nexport function useFiber(): Fiber<null> | undefined {\n const root = React.useContext(FiberContext)\n if (root === null) throw new Error('its-fine: useFiber must be called within a <FiberProvider />!')\n\n const id = React.useId()\n const fiber = React.useMemo(() => {\n for (const maybeFiber of [root, root?.alternate]) {\n if (!maybeFiber) continue\n const fiber = traverseFiber<null>(maybeFiber, false, (node) => {\n let state = node.memoizedState\n while (state) {\n if (state.memoizedState === id) return true\n state = state.next\n }\n })\n if (fiber) return fiber\n }\n }, [root, id])\n\n return fiber\n}\n\n/**\n * Represents a react-reconciler container instance.\n */\nexport interface ContainerInstance<T = any> {\n containerInfo: T\n}\n\n/**\n * Returns the current react-reconciler container info passed to {@link ReactReconciler.Reconciler.createContainer}.\n *\n * In react-dom, a container will point to the root DOM element; in react-three-fiber, it will point to the root Zustand store.\n */\nexport function useContainer<T = any>(): T | undefined {\n const fiber = useFiber()\n const root = React.useMemo(\n () => traverseFiber<ContainerInstance<T>>(fiber, true, (node) => node.stateNode?.containerInfo != null),\n [fiber],\n )\n\n return root?.stateNode.containerInfo\n}\n\n/**\n * Returns the nearest react-reconciler child instance or the node created from {@link ReactReconciler.HostConfig.createInstance}.\n *\n * In react-dom, this would be a DOM element; in react-three-fiber this would be an instance descriptor.\n */\nexport function useNearestChild<T = any>(\n /** An optional element type to filter to. */\n type?: keyof React.JSX.IntrinsicElements,\n): React.RefObject<T | undefined> {\n const fiber = useFiber()\n const childRef = React.useRef<T>(undefined)\n\n useIsomorphicLayoutEffect(() => {\n childRef.current = traverseFiber<T>(\n fiber,\n false,\n (node) => typeof node.type === 'string' && (type === undefined || node.type === type),\n )?.stateNode\n }, [fiber])\n\n return childRef\n}\n\n/**\n * Returns the nearest react-reconciler parent instance or the node created from {@link ReactReconciler.HostConfig.createInstance}.\n *\n * In react-dom, this would be a DOM element; in react-three-fiber this would be an instance descriptor.\n */\nexport function useNearestParent<T = any>(\n /** An optional element type to filter to. */\n type?: keyof React.JSX.IntrinsicElements,\n): React.RefObject<T | undefined> {\n const fiber = useFiber()\n const parentRef = React.useRef<T>(undefined)\n\n useIsomorphicLayoutEffect(() => {\n parentRef.current = traverseFiber<T>(\n fiber,\n true,\n (node) => typeof node.type === 'string' && (type === undefined || node.type === type),\n )?.stateNode\n }, [fiber])\n\n return parentRef\n}\n\nexport type ContextMap = Map<React.Context<any>, any> & {\n get<T>(context: React.Context<T>): T | undefined\n}\n\nconst REACT_CONTEXT_TYPE = Symbol.for('react.context')\n\nconst isContext = <T,>(type: unknown): type is React.Context<T> =>\n type !== null && typeof type === 'object' && '$$typeof' in type && type.$$typeof === REACT_CONTEXT_TYPE\n\n/**\n * Returns a map of all contexts and their values.\n */\nexport function useContextMap(): ContextMap {\n const fiber = useFiber()\n const [contextMap] = React.useState(() => new Map<React.Context<any>, any>())\n\n // Collect live context\n contextMap.clear()\n let node = fiber\n while (node) {\n const context = node.type\n if (isContext(context) && context !== FiberContext && !contextMap.has(context)) {\n contextMap.set(context, React.use(wrapContext(context)))\n }\n\n node = node.return!\n }\n\n return contextMap\n}\n\n/**\n * Represents a react-context bridge provider component.\n */\nexport type ContextBridge = React.FC<React.PropsWithChildren<{}>>\n\n/**\n * React Context currently cannot be shared across [React renderers](https://reactjs.org/docs/codebase-overview.html#renderers) but explicitly forwarded between providers (see [react#17275](https://github.com/facebook/react/issues/17275)). This hook returns a {@link ContextBridge} of live context providers to pierce Context across renderers.\n *\n * Pass {@link ContextBridge} as a component to a secondary renderer to enable context-sharing within its children.\n */\nexport function useContextBridge(): ContextBridge {\n const contextMap = useContextMap()\n\n // Flatten context and their memoized values into a `ContextBridge` provider\n return React.useMemo(\n () =>\n Array.from(contextMap.keys()).reduce(\n (Prev, context) => (props) =>\n (\n <Prev>\n <context.Provider {...props} value={contextMap.get(context)} />\n </Prev>\n ),\n (props) => <FiberProvider {...props} />,\n ),\n [contextMap],\n )\n}\n\n/** A component that forwards ancestor Activity visibility into another React root. */\nexport type ActivityBridge = React.FC<React.PropsWithChildren<{}>>\n\nfunction requestFrame(callback: () => void): () => void {\n if (typeof requestAnimationFrame === 'function') {\n const frame = requestAnimationFrame(callback)\n return () => cancelAnimationFrame(frame)\n }\n const timeout = setTimeout(callback, 16)\n return () => clearTimeout(timeout)\n}\n\n/**\n * Forwards ancestor Activity visibility into another root. Requires React 19.2+,\n * an Activity capable destination renderer, and a {@link FiberProvider}.\n *\n * Suspense does not hide the destination. While Suspense hides the source,\n * visibility is sampled once per frame and changes between samples can be missed.\n * Source deletion hides the destination without unmounting its root.\n */\nexport function useActivityBridge(): ActivityBridge {\n if (!React.Activity) throw new Error('its-fine: useActivityBridge requires React 19.2 or later!')\n const fiber = useFiber()\n const [bridge] = React.useState(() => {\n const activities: { _visibility: number }[] = []\n let suspensible = false\n traverseFiber(fiber, true, (node) => {\n if (node.elementType === React.Activity) {\n // Fiber alternates share Activity's Offscreen instance.\n const instance = node.child?.tag === 22 ? node.child.stateNode : null\n if (typeof instance?._visibility !== 'number')\n throw new Error('its-fine: unsupported React Activity internals!')\n activities.push(instance)\n } else if (node.tag === 22 && node.return?.elementType !== React.Activity) {\n suspensible = true\n }\n })\n\n // Keep the destination hidden until the source commits.\n let mode: 'hidden' | 'visible' = 'hidden'\n const listeners = new Set<() => void>()\n let cancelPoll: (() => void) | undefined\n const store = {\n mounted: false,\n connected: false,\n subscribe(listener: () => void) {\n listeners.add(listener)\n return () => {\n listeners.delete(listener)\n }\n },\n sync(sample: boolean) {\n // Suspense can keep layout effects disconnected after Activity reveals.\n if (store.mounted && !store.connected && suspensible && activities.length > 0) {\n // Schedule before notifying listeners so an update error does not stop polling.\n if (!cancelPoll) {\n cancelPoll = requestFrame(() => {\n cancelPoll = undefined\n store.sync(true)\n })\n }\n } else {\n cancelPoll?.()\n cancelPoll = undefined\n }\n\n let next = mode\n if (!store.mounted) next = 'hidden'\n else if (store.connected) next = 'visible'\n else if (sample) next = activities.every((activity) => (activity._visibility & 1) !== 0) ? 'visible' : 'hidden'\n if (mode === next) return\n mode = next\n for (const listener of listeners) listener()\n },\n Bridge({ children }: React.PropsWithChildren<{}>) {\n // Subscribe outside Activity so hidden children can be revealed.\n return (\n <React.Activity\n mode={React.useSyncExternalStore(\n store.subscribe,\n () => mode,\n () => 'hidden' as const,\n )}\n >\n {children}\n </React.Activity>\n )\n },\n }\n return store\n })\n\n // Insertion effects stay mounted while hidden and clean up on deletion.\n React.useInsertionEffect(() => {\n bridge.mounted = true\n // Defer updates until insertion effects finish and ancestor visibility settles.\n queueMicrotask(() => bridge.sync(false))\n return () => {\n bridge.mounted = false\n queueMicrotask(() => bridge.sync(false))\n }\n }, [bridge])\n\n // Activity and Suspense disconnect layout effects when hiding the source.\n React.useLayoutEffect(() => {\n bridge.connected = true\n bridge.sync(false)\n return () => {\n bridge.connected = false\n bridge.sync(true)\n }\n }, [bridge])\n return bridge.Bridge\n}\n"],"names":["useIsomorphicLayoutEffect","_a","_b","React","traverseFiber","fiber","ascending","selector","child","match","wrapContext","context","_","FiberContext","FiberProvider","useFiber","root","id","maybeFiber","node","state","useContainer","useNearestChild","type","childRef","useNearestParent","parentRef","REACT_CONTEXT_TYPE","isContext","useContextMap","contextMap","useContextBridge","Prev","props","requestFrame","callback","frame","timeout","useActivityBridge","bridge","activities","suspensible","instance","mode","listeners","cancelPoll","store","listener","sample","next","activity","children"],"mappings":"yGAYMA,GAA6C,IAAA,SACjD,cAAO,QAAW,gBAAgBC,EAAA,OAAO,WAAP,YAAAA,EAAiB,kBAAiBC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