{"version":3,"sources":["../../src/use-key-modifier/index.ts"],"sourcesContent":["import { useEventListener } from '../use-event-listener'\nimport { useSafeState } from '../use-safe-state'\nimport { isFunction } from '../utils/basic'\n\nimport type { WindowEventName } from '../utils/basic'\n\nexport type KeyModifier =\n  | 'Alt'\n  | 'AltGraph'\n  | 'CapsLock'\n  | 'Control'\n  | 'Fn'\n  | 'FnLock'\n  | 'Meta'\n  | 'NumLock'\n  | 'ScrollLock'\n  | 'Shift'\n  | 'Symbol'\n  | 'SymbolLock'\n\nconst defaultEvents: WindowEventName[] = ['mousedown', 'mouseup', 'keydown', 'keyup']\n\nexport interface UseModifierOptions<Initial> {\n  /**\n   * Event names that will prompt update to modifier states\n   *\n   * @defaultValue ['mousedown', 'mouseup', 'keydown', 'keyup']\n   */\n  events?: WindowEventName[]\n  /**\n   * Initial value of the returned ref\n   *\n   * @defaultValue null\n   */\n  initial?: Initial\n}\n\nexport type UseKeyModifierReturns<Initial> = Initial extends boolean ? boolean : boolean | null\n\n/**\n * A React Hook that allows you to detect if a [key modifier](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState) is pressed.\n */\nexport function useKeyModifier<Initial extends boolean | null>(\n  modifier: KeyModifier,\n  options: UseModifierOptions<Initial> = {},\n): UseKeyModifierReturns<Initial> {\n  const { events = defaultEvents, initial = null } = options\n  const [state, setState] = useSafeState(initial as boolean)\n\n  useEventListener(\n    () => document,\n    events,\n    (evt: KeyboardEvent | MouseEvent | Event) => {\n      if ('getModifierState' in evt && isFunction(evt.getModifierState)) {\n        setState(evt.getModifierState(modifier))\n      }\n    },\n  )\n\n  return state\n}\n"],"names":["useKeyModifier","defaultEvents","modifier","options","events","initial","useSafeState","state","setState","useEventListener","document","evt","isFunction","getModifierState"],"mappings":";;;;+BA0CgBA;;;eAAAA;;;gCA1CiB;4BACJ;qBACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkB3B,IAAMC,gBAAmC;IAAC;IAAa;IAAW;IAAW;CAAQ;AAsB9E,SAASD,eACdE,QAAqB;QACrBC,UAAAA,iEAAuC,CAAC;IAExC,sBAAmDA,QAA3CC,QAAAA,sCAASH,oDAAkCE,QAAnBE,SAAAA,wCAAU;IAC1C,IAA0BC,iCAAAA,IAAAA,0BAAY,EAACD,cAAhCE,QAAmBD,kBAAZE,WAAYF;IAE1BG,IAAAA,kCAAgB,EACd;eAAMC;OACNN,QACA,SAACO;QACC,IAAI,sBAAsBA,OAAOC,IAAAA,iBAAU,EAACD,IAAIE,gBAAgB,GAAG;YACjEL,SAASG,IAAIE,gBAAgB,CAACX;QAChC;IACF;IAGF,OAAOK;AACT"}