{"version":3,"sources":["../../src/use-breakpoints/index.ts"],"sourcesContent":["import { useCreation } from '../use-creation'\nimport { useDeepCompareLayoutEffect } from '../use-deep-compare-layout-effect'\nimport { useLatest } from '../use-latest'\nimport { useMediaQuery } from '../use-media-query'\nimport { useSafeState } from '../use-safe-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { increaseWithUnit, isNumber } from '../utils/basic'\n\n/**\n * A object that contains different breakpoints\n */\nexport type Breakpoints<K extends string = string> = Record<K, number | string>\n\nexport interface UseBreakpointsOptions {\n  /**\n   * The query strategy to use for the generated shortcut methods like `.lg`\n   *\n   * 'min-width' - .lg will be true when the viewport is greater than or equal to the lg breakpoint (mobile-first)\n   * 'max-width' - .lg will be true when the viewport is smaller than the xl breakpoint (desktop-first)\n   *\n   * @defaultValue \"min-width\"\n   */\n  strategy?: 'min-width' | 'max-width'\n}\n\nexport type UseBreakpointsReturns<K extends string> = Record<K, boolean> & {\n  /**\n   * The current breakpoints states\n   */\n  breakpoints: Record<K, boolean>\n  /**\n   * Check if the viewport is greater than the given breakpoint\n   *\n   * @param {K} k - `K`, the breakpoint key\n   * @returns {boolean} `boolean`, whether the viewport is greater than the given breakpoint\n   */\n  isGreater: (k: K) => boolean\n  /**\n   * Check if the viewport is greater or equal to the given breakpoint\n   *\n   * @param {K} k - `K`, the breakpoint key\n   * @returns {boolean} `boolean`, whether the viewport is greater or equal to the given breakpoint\n   */\n  isGreaterOrEqual: (k: K) => boolean\n  /**\n   * Check if the viewport is smaller than the given breakpoint\n   *\n   * @param {K} k - `K`, the breakpoint key\n   * @returns {boolean} `boolean`, whether the viewport is smaller than the given breakpoint\n   */\n  isSmaller: (k: K) => boolean\n  /**\n   * Check if the viewport is smaller or equal to the given breakpoint\n   *\n   * @param {K} k - `K`, the breakpoint key\n   * @returns {boolean} `boolean`, whether the viewport is smaller or equal to the given breakpoint\n   */\n  isSmallerOrEqual: (k: K) => boolean\n  /**\n   * Check if the viewport is between the given breakpoints\n   *\n   * @param {K} a - `K`, the breakpoint key\n   * @param {K} b - `K`, the breakpoint key\n   * @returns {boolean} `boolean`, whether the viewport is between the given breakpoints\n   */\n  isInBetween: (a: K, b: K) => boolean\n  /**\n   * The current matched breakpoints\n   */\n  currents: K[]\n}\n\n/**\n * five breakpoints by default, align with tailwindcss breakpoints\n *\n * @see {@link https://tailwindcss.com/docs/responsive-design | Tailwind CSS - Responsive Design}\n * */\nexport const defaultBreakpoints = { sm: 640, md: 768, lg: 1024, xl: 1280, xxl: 1536 }\n\n/**\n * A React Hook that provides a simple API to interact with the viewport breakpoints.\n *\n * @param {Breakpoints} [breakpoints=defaultBreakpoints] - `Breakpoints`, the breakpoints to use, see {@link Breakpoints} and {@link defaultBreakpoints}\n * @param {UseBreakpointsOptions} [options={}] - `UseBreakpointsOptions`, options to configure the hook, see {@link UseBreakpointsOptions}\n * @returns {UseBreakpointsReturns} `UseBreakpointsReturns`, see {@link UseBreakpointsReturns}\n */\nexport function useBreakpoints<K extends string>(\n  breakpoints: Breakpoints<K> = defaultBreakpoints as Breakpoints<K>,\n  options: UseBreakpointsOptions = {},\n): UseBreakpointsReturns<K> {\n  const { strategy = 'min-width' } = options\n  const latest = useLatest({ breakpoints, strategy, ...options })\n\n  const calcValue = useStableFn((k: K, delta?: number) => {\n    let v = latest.current.breakpoints[k]\n    if (delta) v = increaseWithUnit(v, delta)\n    if (isNumber(v)) v = `${v}px`\n    return v\n  })\n\n  const transformQueries = useStableFn(() => {\n    return Object.keys(latest.current.breakpoints).map((k) => {\n      const isMinWidth = strategy === 'min-width'\n      return [k, isMinWidth ? `(min-width: ${calcValue(k as K)})` : `(max-width: ${calcValue(k as K)})`] as [K, string]\n    })\n  })\n\n  const [queries, setQueries] = useSafeState<[Key: K, Query: string][]>(transformQueries)\n\n  const matches = useMediaQuery(queries.map((e) => e[1]))\n  const match = useStableFn((query: string) => Boolean(window?.matchMedia(query).matches))\n\n  // biome-ignore lint/correctness/useExhaustiveDependencies: effect need to re-run when breakpoints changes\n  useDeepCompareLayoutEffect(() => setQueries(transformQueries()), [breakpoints])\n\n  const isGreater = useStableFn((k: K) => match(`(min-width: ${calcValue(k)})`))\n  const isSmaller = useStableFn((k: K) => match(`(max-width: ${calcValue(k, -0.1)})`))\n  const isGreaterOrEqual = useStableFn((k: K) => match(`(min-width: ${calcValue(k, 0.1)})`))\n  const isSmallerOrEqual = useStableFn((k: K) => match(`(max-width: ${calcValue(k)})`))\n  const isInBetween = useStableFn((a: K, b: K) =>\n    match(`(min-width: ${calcValue(a)}) and (max-width: ${calcValue(b, -0.1)})`),\n  )\n\n  const { bps, currents } = useCreation(() => {\n    const bps = Object.keys(breakpoints).reduce(\n      (shortcuts, key) => {\n        shortcuts[key as K] = matches[queries.findIndex(([k]) => k === key)] ?? false\n        return shortcuts\n      },\n      {} as Record<K, boolean>,\n    )\n\n    const currents = Object.entries(bps)\n      .filter(([_, v]) => v)\n      .map(([k]) => k as K)\n\n    return { bps, currents }\n  }, [breakpoints, matches])\n\n  return {\n    ...bps,\n    breakpoints: bps,\n    isGreater,\n    isGreaterOrEqual,\n    isSmaller,\n    isSmallerOrEqual,\n    isInBetween,\n    currents,\n  }\n}\n"],"names":["defaultBreakpoints","useBreakpoints","sm","md","lg","xl","xxl","breakpoints","options","strategy","latest","useLatest","calcValue","useStableFn","k","delta","v","current","increaseWithUnit","isNumber","transformQueries","Object","keys","map","isMinWidth","useSafeState","queries","setQueries","matches","useMediaQuery","e","match","query","window","Boolean","matchMedia","useDeepCompareLayoutEffect","isGreater","isSmaller","isGreaterOrEqual","isSmallerOrEqual","isInBetween","a","b","useCreation","bps","reduce","shortcuts","key","findIndex","currents","entries","filter","_"],"mappings":";;;;;;;;;;;QA6EaA;eAAAA;;QASGC;eAAAA;;;2BAtFY;0CACe;yBACjB;6BACI;4BACD;2BACD;qBACe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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