{"version":3,"sources":["../../src/use-mouse-pressed/index.ts"],"sourcesContent":["import { useEventListener } from '../use-event-listener'\nimport { useSetState } from '../use-set-state'\nimport { useTargetElement } from '../use-target-element'\n\nimport type { UseMouseSourceType } from '../use-mouse'\nimport type { ElementTarget } from '../use-target-element'\n\nexport interface MousePressedOptions {\n  /**\n   * Whether to listen to touch events\n   *\n   * @defaultValue true\n   */\n  touch?: boolean\n  /**\n   * Whether to listen to drag events\n   *\n   * @defaultValue true\n   */\n  drag?: boolean\n  /**\n   * Whether to use event capturing\n   *\n   * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture MDN - addEventListener#capture}\n   *\n   * @defaultValue false\n   */\n  capture?: boolean\n  /**\n   * The initial value of the mouse press\n   *\n   * @defaultValue false\n   */\n  initialValue?: boolean\n}\n\nexport interface UseMousePressedReturns {\n  /**\n   * Whether the mouse is pressed\n   */\n  pressed: boolean\n  /**\n   * The source type of the mouse press\n   */\n  sourceType: UseMouseSourceType\n}\n\n/**\n * A React Hook that tracks mouse pressed state.\n */\nexport function useMousePressed(\n  target: ElementTarget | (() => Window) | (() => Document) = () => window,\n  options: MousePressedOptions = {},\n): UseMousePressedReturns {\n  const { touch = true, drag = true, capture = false, initialValue = false } = options\n\n  const el = useTargetElement(target)\n  const [state, setState] = useSetState({ pressed: initialValue, sourceType: null as UseMouseSourceType })\n\n  function createHandler(srcType: UseMouseSourceType) {\n    return () => {\n      setState({ pressed: true, sourceType: srcType })\n    }\n  }\n\n  function onReleased() {\n    setState({ pressed: false, sourceType: null })\n  }\n\n  const evtOptions = { passive: true, capture }\n\n  useEventListener(el, 'mousedown', createHandler('mouse'), evtOptions)\n  useEventListener(el, ['mouseleave', 'mouseup'], onReleased, evtOptions)\n\n  useEventListener(el, drag ? 'dragstart' : [], createHandler('mouse'), evtOptions)\n  useEventListener(el, drag ? ['drop', 'dragend'] : [], onReleased, evtOptions)\n\n  useEventListener(el, touch ? 'touchstart' : [], createHandler('touch'), evtOptions)\n  useEventListener(el, touch ? ['touchend', 'touchcancel'] : [], onReleased, evtOptions)\n\n  return state\n}\n"],"names":["useMousePressed","target","window","options","touch","drag","capture","initialValue","el","useTargetElement","useSetState","pressed","sourceType","state","setState","createHandler","srcType","onReleased","evtOptions","passive","useEventListener"],"mappings":";;;;+BAkDgBA;;;eAAAA;;;gCAlDiB;2BACL;gCACK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgD1B,SAASA;QACdC,SAAAA,iEAA4D;eAAMC;OAClEC,UAAAA,iEAA+B,CAAC;IAEhC,qBAA6EA,QAArEC,OAAAA,oCAAQ,uCAA6DD,QAAvDE,MAAAA,kCAAO,yCAAgDF,QAA1CG,SAAAA,wCAAU,kDAAgCH,QAAzBI,cAAAA,kDAAe;IAEnE,IAAMC,KAAKC,IAAAA,kCAAgB,EAACR;IAC5B,IAA0BS,gCAAAA,IAAAA,wBAAW,EAAC;QAAEC,SAASJ;QAAcK,YAAY;IAA2B,QAA/FC,QAAmBH,iBAAZI,WAAYJ;IAE1B,SAASK,cAAcC,OAA2B;QAChD,OAAO;YACLF,SAAS;gBAAEH,SAAS;gBAAMC,YAAYI;YAAQ;QAChD;IACF;IAEA,SAASC;QACPH,SAAS;YAAEH,SAAS;YAAOC,YAAY;QAAK;IAC9C;IAEA,IAAMM,aAAa;QAAEC,SAAS;QAAMb,SAAAA;IAAQ;IAE5Cc,IAAAA,kCAAgB,EAACZ,IAAI,aAAaO,cAAc,UAAUG;IAC1DE,IAAAA,kCAAgB,EAACZ,IAAI;QAAC;QAAc;KAAU,EAAES,YAAYC;IAE5DE,IAAAA,kCAAgB,EAACZ,IAAIH,OAAO,cAAc,EAAE,EAAEU,cAAc,UAAUG;IACtEE,IAAAA,kCAAgB,EAACZ,IAAIH,OAAO;QAAC;QAAQ;KAAU,GAAG,EAAE,EAAEY,YAAYC;IAElEE,IAAAA,kCAAgB,EAACZ,IAAIJ,QAAQ,eAAe,EAAE,EAAEW,cAAc,UAAUG;IACxEE,IAAAA,kCAAgB,EAACZ,IAAIJ,QAAQ;QAAC;QAAY;KAAc,GAAG,EAAE,EAAEa,YAAYC;IAE3E,OAAOL;AACT"}