{"version":3,"file":"lenis-react.mjs","names":[],"sources":["../packages/react/src/store.ts","../packages/react/src/provider.tsx","../packages/react/src/use-lenis.ts"],"sourcesContent":["import { useEffect, useState } from 'react'\n\ntype Listener<S> = (state: S) => void\n\nexport class Store<S> {\n  private listeners: Listener<S>[] = []\n\n  constructor(private state: S) {}\n\n  set(state: S) {\n    this.state = state\n\n    for (const listener of this.listeners) {\n      listener(this.state)\n    }\n  }\n\n  subscribe(listener: Listener<S>) {\n    this.listeners = [...this.listeners, listener]\n    return () => {\n      this.listeners = this.listeners.filter((l) => l !== listener)\n    }\n  }\n\n  get() {\n    return this.state\n  }\n}\n\nexport function useStore<S>(store: Store<S>) {\n  const [state, setState] = useState(store.get())\n\n  useEffect(() => {\n    return store.subscribe((state) => setState(state))\n  }, [store])\n\n  return state\n}\n","import Lenis, { type ScrollCallback } from 'lenis'\nimport {\n  type ForwardRefExoticComponent,\n  type RefAttributes,\n  createContext,\n  forwardRef,\n  useCallback,\n  useEffect,\n  useImperativeHandle,\n  useRef,\n  useState,\n} from 'react'\nimport { Store } from './store'\nimport type { LenisContextValue, LenisProps, LenisRef } from './types'\n\nexport const LenisContext = createContext<LenisContextValue | null>(null)\n\n/**\n * The root store for the lenis context\n *\n * This store serves as a fallback for the context if it is not available\n * and allows us to use the global lenis instance above a provider\n */\nexport const rootLenisContextStore = new Store<LenisContextValue | null>(null)\n\n/**\n * React component to setup a Lenis instance\n */\nexport const ReactLenis: ForwardRefExoticComponent<\n  LenisProps & RefAttributes<LenisRef>\n> = forwardRef<LenisRef, LenisProps>(\n  (\n    {\n      children,\n      root = false,\n      options = {},\n      autoRaf = true,\n      className = '',\n      ...props\n    }: LenisProps,\n    ref\n  ) => {\n    const wrapperRef = useRef<HTMLDivElement>(null)\n    const contentRef = useRef<HTMLDivElement>(null)\n\n    const [lenis, setLenis] = useState<Lenis | undefined>(undefined)\n\n    // Setup ref\n    useImperativeHandle(\n      ref,\n      () => ({\n        wrapper: wrapperRef.current,\n        content: contentRef.current,\n        lenis,\n      }),\n      [lenis]\n    )\n\n    // Setup lenis instance\n    useEffect(() => {\n      const lenis = new Lenis({\n        ...options,\n        ...(wrapperRef.current &&\n          contentRef.current && {\n            wrapper: wrapperRef.current!,\n            content: contentRef.current!,\n          }),\n        autoRaf: options?.autoRaf ?? autoRaf, // this is to avoid breaking the autoRaf prop if it's still used (require breaking change)\n      })\n\n      setLenis(lenis)\n\n      return () => {\n        lenis.destroy()\n        setLenis(undefined)\n      }\n    }, [autoRaf, JSON.stringify({ ...options, wrapper: null, content: null })])\n\n    // Handle callbacks\n    const callbacksRefs = useRef<\n      {\n        callback: ScrollCallback\n        priority: number\n      }[]\n    >([])\n\n    const addCallback: LenisContextValue['addCallback'] = useCallback(\n      (callback, priority) => {\n        callbacksRefs.current.push({ callback, priority })\n        callbacksRefs.current.sort((a, b) => a.priority - b.priority)\n      },\n      []\n    )\n\n    const removeCallback: LenisContextValue['removeCallback'] = useCallback(\n      (callback) => {\n        callbacksRefs.current = callbacksRefs.current.filter(\n          (cb) => cb.callback !== callback\n        )\n      },\n      []\n    )\n\n    // This makes sure to set the global context if the root is true\n    useEffect(() => {\n      if (root && lenis) {\n        rootLenisContextStore.set({ lenis, addCallback, removeCallback })\n\n        return () => rootLenisContextStore.set(null)\n      }\n    }, [root, lenis, addCallback, removeCallback])\n\n    // Setup callback listeners\n    useEffect(() => {\n      if (!lenis) return\n\n      const onScroll: ScrollCallback = (data) => {\n        for (const { callback } of callbacksRefs.current) {\n          callback(data)\n        }\n      }\n\n      lenis.on('scroll', onScroll)\n\n      return () => {\n        lenis.off('scroll', onScroll)\n      }\n    }, [lenis])\n\n    if (!children) return null\n\n    return (\n      <LenisContext.Provider\n        value={{ lenis: lenis!, addCallback, removeCallback }}\n      >\n        {root && root !== 'asChild' ? (\n          children\n        ) : (\n          <div\n            ref={wrapperRef}\n            className={`${className} ${lenis?.className ?? ''}`.trim()}\n            {...props}\n          >\n            <div ref={contentRef}>{children}</div>\n          </div>\n        )}\n      </LenisContext.Provider>\n    )\n  }\n)\n","import type Lenis from 'lenis'\nimport type { ScrollCallback } from 'lenis'\nimport { useContext, useEffect } from 'react'\nimport { LenisContext, rootLenisContextStore } from './provider'\nimport { useStore } from './store'\nimport type { LenisContextValue } from './types'\n\n// Fall back to an empty object if both context and store are not available\nconst fallbackContext: Partial<LenisContextValue> = {}\n\n/**\n * Hook to access the Lenis instance and its methods\n *\n * @example <caption>Scroll callback</caption>\n *          useLenis((lenis) => {\n *            if (lenis.isScrolling) {\n *              console.log('Scrolling...')\n *            }\n *\n *            if (lenis.progress === 1) {\n *              console.log('At the end!')\n *            }\n *          })\n *\n * @example <caption>Scroll callback with dependencies</caption>\n *          useLenis((lenis) => {\n *            if (lenis.isScrolling) {\n *              console.log('Scrolling...', someDependency)\n *            }\n *          }, [someDependency])\n * @example <caption>Scroll callback with priority</caption>\n *          useLenis((lenis) => {\n *            if (lenis.isScrolling) {\n *              console.log('Scrolling...')\n *            }\n *          }, [], 1)\n * @example <caption>Instance access</caption>\n *          const lenis = useLenis()\n *\n *          handleClick() {\n *            lenis.scrollTo(100, {\n *              lerp: 0.1,\n *              duration: 1,\n *              easing: (t) => t,\n *              onComplete: () => {\n *                console.log('Complete!')\n *              }\n *            })\n *          }\n */\nexport function useLenis(\n  callback?: ScrollCallback,\n  deps: unknown[] = [],\n  priority = 0\n): Lenis | undefined {\n  // Try to get the lenis instance from the context first\n  const localContext = useContext(LenisContext)\n  // Fall back to the root store if the context is not available\n  const rootContext = useStore(rootLenisContextStore)\n  // Fall back to the fallback context if all else fails\n  const currentContext = localContext ?? rootContext ?? fallbackContext\n\n  const { lenis, addCallback, removeCallback } = currentContext\n\n  useEffect(() => {\n    if (!(callback && addCallback && removeCallback && lenis)) return\n\n    addCallback(callback, priority)\n    callback(lenis)\n\n    return () => {\n      removeCallback(callback)\n    }\n  }, [lenis, addCallback, removeCallback, priority, ...deps, callback])\n\n  return lenis\n}\n"],"mappings":";;;;;AAIA,IAAa,QAAb,M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