{"version":3,"file":"context-screen-width-Cfe3atNC.cjs","names":[],"sources":["../src/contexts/context-screen-width/context.tsx","../src/contexts/context-screen-width/context.hooks.ts"],"sourcesContent":["import { CSS_VARS, IThemeBreakpoint } from '@local/styles/utils';\n\nimport { FC, useCallback, useEffect, useRef, useState } from 'react';\nimport { createContext } from 'use-context-selector';\n\nimport { IScreenWidthContext, IScreenWidthProvider } from './context.types';\n\nexport const ScreenWidthContext = createContext<IScreenWidthContext | null>(null);\n\nconst defaultOrientation =\n  typeof window !== 'undefined' && window.innerWidth > window.innerHeight ? 'landscape' : 'portrait';\n\nconst getDefaultBreakpoint = (): IScreenWidthContext['breakpoint'] => {\n  if (typeof window === 'undefined') return 'default';\n\n  const breakpoints = Object.entries(CSS_VARS.screen.breakpoint)\n    .map(([key, value]) => ({\n      key: key as keyof typeof CSS_VARS.screen.breakpoint,\n      bp: parseInt(value.replace(/\\D/g, ''), 10),\n    }))\n    .sort((a, b) => a.bp - b.bp);\n\n  const matched = breakpoints.find(({ bp }) => window.innerWidth <= bp);\n  return matched?.key ?? 'default';\n};\n\nconst defaultBreakpoint: IScreenWidthContext['breakpoint'] = getDefaultBreakpoint();\n\nexport const ProviderScreenWidth: FC<IScreenWidthProvider> = (props) => {\n  const [breakpoint, setBreakpoint] = useState<IScreenWidthContext['breakpoint']>(defaultBreakpoint);\n  const [orientation, setOrientation] = useState<IScreenWidthContext['orientation']>(defaultOrientation);\n\n  const queriesRef = useRef<Array<{\n    key: IThemeBreakpoint;\n    mq: MediaQueryList;\n  }> | null>(null);\n\n  if (queriesRef.current === null && typeof window !== 'undefined') {\n    queriesRef.current = Object.entries(CSS_VARS.screen.breakpoint)\n      .map(([key, value]) => {\n        const bp = parseInt(value.replace(/\\D/g, ''), 10);\n        return {\n          key: key as keyof typeof CSS_VARS.screen.breakpoint,\n          bp,\n        };\n      })\n      .sort((a, b) => a.bp - b.bp)\n      .map(({ key, bp }) => ({\n        key,\n        mq: window.matchMedia(`(max-width: ${bp}px)`),\n      }));\n  }\n\n  const updateBreakpoint = useCallback(() => {\n    if (!queriesRef.current) return;\n    const matched = queriesRef.current.find(({ mq }) => mq.matches);\n    setBreakpoint(matched?.key ?? 'default');\n  }, []);\n\n  useEffect(() => {\n    if (typeof window === 'undefined') return;\n\n    const handleResize = () => {\n      const newWidth = window.innerWidth;\n      const newOrientation = newWidth > window.innerHeight ? 'landscape' : 'portrait';\n\n      setOrientation(newOrientation);\n    };\n\n    window.addEventListener('resize', handleResize);\n    return () => window.removeEventListener('resize', handleResize);\n  }, []);\n\n  useEffect(() => {\n    if (!queriesRef.current) return;\n\n    queriesRef.current.forEach(({ mq }) => {\n      mq.addEventListener('change', updateBreakpoint);\n    });\n    updateBreakpoint();\n\n    return () => {\n      queriesRef.current?.forEach(({ mq }) => {\n        mq.removeEventListener('change', updateBreakpoint);\n      });\n    };\n  }, [updateBreakpoint]);\n\n  return (\n    <ScreenWidthContext.Provider\n      value={{\n        breakpoint,\n        orientation,\n        type: props.type ?? 'breakpoint',\n      }}\n    >\n      {props.children}\n    </ScreenWidthContext.Provider>\n  );\n};\n","import { useContextSelector } from 'use-context-selector';\n\nimport { ScreenWidthContext } from './context';\nimport { IScreenWidthContext, IUseScreenWidthDependencies } from './context.types';\n\nexport const useScreenWidth = (props: IUseScreenWidthDependencies): IScreenWidthContext => {\n  const context = useContextSelector(ScreenWidthContext, (v) => {\n    return v\n      ? props.reduce((acc, prop) => {\n          acc[prop] = v[prop];\n          return acc;\n        }, {} as any)\n      : null;\n  });\n  if (!context) {\n    throw new Error('useScreenWidth must be used within an ProviderScreenWidth');\n  }\n  return context;\n};\n"],"mappings":"iKAOA,IAAa,EAAqB,EAAA,cAA0C,IAAA,EAEtE,EACJ,OAAO,OAAW,KAAe,OAAO,WAAa,OAAO,YAAc,YAAc,WAEpF,EAAA,IACA,OAAO,OAAW,IAAoB,UAEtB,OAAO,QAAQ,EAAA,SAAS,OAAO,UAAA,EAChD,IAAA,CAAK,CAAC,EAAK,CAAA,KA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