{"version":3,"sources":["../../src/use-eye-dropper/index.ts"],"sourcesContent":["import { useLatest } from '../use-latest'\nimport { useSafeState } from '../use-safe-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { useSupported } from '../use-supported'\n\nexport interface EyeDropperOpenOptions {\n  /**\n   * An AbortSignal to cancel the operation.\n   *\n   * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal\n   */\n  signal?: AbortSignal\n}\n\ninterface EyeDropperConstructor {\n  new (): EyeDropper\n}\n\ntype WindowWithEyedropper = Window &\n  typeof globalThis & {\n    EyeDropper: EyeDropper\n  }\n\nexport interface EyeDropper extends EyeDropperConstructor {\n  open: (options?: EyeDropperOpenOptions) => Promise<{ sRGBHex: string }>\n  [Symbol.toStringTag]: 'EyeDropper'\n}\n\nexport interface UseEyeDropperOptions {\n  /**\n   * Initial sRGBHex.\n   *\n   * @defaultValue ''\n   */\n  initialValue?: string\n  /**\n   * Whether to convert the sRGBHex to uppercase.\n   *\n   * @defaultValue true\n   */\n  upperCase?: boolean\n  /**\n   * Whether to keep the leading hash in the sRGBHex.\n   *\n   * @defaultValue true\n   */\n  keepLeadingHash?: boolean\n}\n\nexport interface UseEyeDropperReturns {\n  /**\n   * Whether the EyeDropper is supported.\n   */\n  isSupported: boolean\n  /**\n   * The sRGBHex color value.\n   */\n  sRGBHex: string\n  /**\n   * Open the EyeDropper.\n   *\n   * @param options - The open options.\n   */\n  open: (options?: EyeDropperOpenOptions) => Promise<string | null>\n}\n\n/**\n * A React Hook that allows you to use the eye dropper tool to get the color of a pixel on the screen.\n */\nexport function useEyeDropper(options: UseEyeDropperOptions = {}): UseEyeDropperReturns {\n  const { initialValue = '', upperCase = true, keepLeadingHash = true } = options\n\n  const isSupported = useSupported(() => 'EyeDropper' in window)\n  const [sRGBHex, setSRGBHex] = useSafeState(initialValue)\n\n  const latest = useLatest({ isSupported, upperCase, keepLeadingHash })\n\n  const open = useStableFn(async (openOptions?: EyeDropperOpenOptions) => {\n    const { isSupported, upperCase, keepLeadingHash } = latest.current\n\n    if (!isSupported) return null\n\n    const eyeDropper: EyeDropper = new (window as WindowWithEyedropper).EyeDropper()\n    const { sRGBHex = '' } = (await eyeDropper.open(openOptions)) || {}\n    const hash = keepLeadingHash ? '#' : ''\n    const hex = sRGBHex.slice(1)\n    const formattedHex = upperCase ? hex.toUpperCase() : hex\n    const color = hash + formattedHex\n\n    setSRGBHex(color)\n\n    return color\n  })\n\n  return {\n    isSupported,\n    sRGBHex,\n    open,\n  }\n}\n"],"names":["useEyeDropper","options","initialValue","upperCase","keepLeadingHash","isSupported","useSupported","window","useSafeState","sRGBHex","setSRGBHex","latest","useLatest","open","useStableFn","openOptions","eyeDropper","hash","hex","formattedHex","color","current","EyeDropper","slice","toUpperCase"],"mappings":";;;;+BAqEgBA;;;eAAAA;;;yBArEU;4BACG;2BACD;4BACC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEtB,SAASA;QAAcC,UAAAA,iEAAgC,CAAC;IAC7D,4BAAwEA,QAAhEC,cAAAA,kDAAe,iDAAiDD,QAA7CE,WAAAA,4CAAY,sDAAiCF,QAA3BG,iBAAAA,wDAAkB;IAE/D,IAAMC,cAAcC,IAAAA,0BAAY,EAAC;eAAM,gBAAgBC;;IACvD,IAA8BC,iCAAAA,IAAAA,0BAAY,EAACN,mBAApCO,UAAuBD,kBAAdE,aAAcF;IAE9B,IAAMG,SAASC,IAAAA,oBAAS,EAAC;QAAEP,aAAAA;QAAaF,WAAAA;QAAWC,iBAAAA;IAAgB;IAEnE,IAAMS,OAAOC,IAAAA,wBAAW,EAAC,SAAOC;;gBACsBJ,iBAA5CN,aAAaF,WAAWC,iBAI1BY,YACmB,oBAAjBP,SACFQ,M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