@mantine/hooks 9.0.1 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.cjs +8 -0
- package/cjs/use-click-outside/use-click-outside.cjs +11 -8
- package/cjs/use-click-outside/use-click-outside.cjs.map +1 -1
- package/cjs/use-clipboard/use-clipboard.cjs +13 -6
- package/cjs/use-clipboard/use-clipboard.cjs.map +1 -1
- package/cjs/use-collapse/use-collapse.cjs +1 -1
- package/cjs/use-collapse/use-collapse.cjs.map +1 -1
- package/cjs/use-collapse/use-horizontal-collapse.cjs +4 -3
- package/cjs/use-collapse/use-horizontal-collapse.cjs.map +1 -1
- package/cjs/use-counter/use-counter.cjs +12 -3
- package/cjs/use-counter/use-counter.cjs.map +1 -1
- package/cjs/use-debounced-callback/use-debounced-callback.cjs +28 -4
- package/cjs/use-debounced-callback/use-debounced-callback.cjs.map +1 -1
- package/cjs/use-debounced-state/use-debounced-state.cjs +1 -1
- package/cjs/use-debounced-state/use-debounced-state.cjs.map +1 -1
- package/cjs/use-debounced-value/use-debounced-value.cjs +22 -2
- package/cjs/use-debounced-value/use-debounced-value.cjs.map +1 -1
- package/cjs/use-event-listener/use-event-listener.cjs.map +1 -1
- package/cjs/use-favicon/use-favicon.cjs +3 -1
- package/cjs/use-favicon/use-favicon.cjs.map +1 -1
- package/cjs/use-fetch/use-fetch.cjs +7 -4
- package/cjs/use-fetch/use-fetch.cjs.map +1 -1
- package/cjs/use-file-dialog/use-file-dialog.cjs +1 -0
- package/cjs/use-file-dialog/use-file-dialog.cjs.map +1 -1
- package/cjs/use-floating-window/use-floating-window.cjs +33 -36
- package/cjs/use-floating-window/use-floating-window.cjs.map +1 -1
- package/cjs/use-focus-trap/use-focus-trap.cjs +12 -9
- package/cjs/use-focus-trap/use-focus-trap.cjs.map +1 -1
- package/cjs/use-fullscreen/use-fullscreen.cjs +3 -1
- package/cjs/use-fullscreen/use-fullscreen.cjs.map +1 -1
- package/cjs/use-headroom/use-headroom.cjs +4 -1
- package/cjs/use-headroom/use-headroom.cjs.map +1 -1
- package/cjs/use-hotkeys/parse-hotkey.cjs +1 -2
- package/cjs/use-hotkeys/parse-hotkey.cjs.map +1 -1
- package/cjs/use-hover/use-hover.cjs +1 -0
- package/cjs/use-hover/use-hover.cjs.map +1 -1
- package/cjs/use-in-viewport/use-in-viewport.cjs +11 -7
- package/cjs/use-in-viewport/use-in-viewport.cjs.map +1 -1
- package/cjs/use-interval/use-interval.cjs +15 -6
- package/cjs/use-interval/use-interval.cjs.map +1 -1
- package/cjs/use-local-storage/create-storage.cjs +1 -1
- package/cjs/use-local-storage/create-storage.cjs.map +1 -1
- package/cjs/use-long-press/use-long-press.cjs +6 -2
- package/cjs/use-long-press/use-long-press.cjs.map +1 -1
- package/cjs/use-mask/use-mask.cjs +457 -0
- package/cjs/use-mask/use-mask.cjs.map +1 -0
- package/cjs/use-mouse/use-mouse.cjs +2 -2
- package/cjs/use-mouse/use-mouse.cjs.map +1 -1
- package/cjs/use-move/use-move.cjs +8 -0
- package/cjs/use-move/use-move.cjs.map +1 -1
- package/cjs/use-orientation/use-orientation.cjs +8 -2
- package/cjs/use-orientation/use-orientation.cjs.map +1 -1
- package/cjs/use-os/use-os.cjs +1 -1
- package/cjs/use-os/use-os.cjs.map +1 -1
- package/cjs/use-radial-move/use-radial-move.cjs +4 -4
- package/cjs/use-radial-move/use-radial-move.cjs.map +1 -1
- package/cjs/use-roving-index/use-roving-index.cjs +200 -0
- package/cjs/use-roving-index/use-roving-index.cjs.map +1 -0
- package/cjs/use-scroll-direction/use-scroll-direction.cjs +7 -7
- package/cjs/use-scroll-direction/use-scroll-direction.cjs.map +1 -1
- package/cjs/use-scroll-into-view/use-scroll-into-view.cjs +14 -4
- package/cjs/use-scroll-into-view/use-scroll-into-view.cjs.map +1 -1
- package/cjs/use-scroll-spy/use-scroll-spy.cjs +7 -3
- package/cjs/use-scroll-spy/use-scroll-spy.cjs.map +1 -1
- package/cjs/use-scroller/use-scroller.cjs +3 -3
- package/cjs/use-scroller/use-scroller.cjs.map +1 -1
- package/cjs/use-set/use-set.cjs +6 -1
- package/cjs/use-set/use-set.cjs.map +1 -1
- package/cjs/use-throttled-callback/use-throttled-callback.cjs +3 -1
- package/cjs/use-throttled-callback/use-throttled-callback.cjs.map +1 -1
- package/cjs/use-timeout/use-timeout.cjs +3 -1
- package/cjs/use-timeout/use-timeout.cjs.map +1 -1
- package/cjs/use-viewport-size/use-viewport-size.cjs +3 -3
- package/cjs/use-viewport-size/use-viewport-size.cjs.map +1 -1
- package/cjs/use-window-scroll/use-window-scroll.cjs +2 -2
- package/cjs/use-window-scroll/use-window-scroll.cjs.map +1 -1
- package/esm/index.mjs +3 -1
- package/esm/use-click-outside/use-click-outside.mjs +11 -8
- package/esm/use-click-outside/use-click-outside.mjs.map +1 -1
- package/esm/use-clipboard/use-clipboard.mjs +14 -7
- package/esm/use-clipboard/use-clipboard.mjs.map +1 -1
- package/esm/use-collapse/use-collapse.mjs +1 -1
- package/esm/use-collapse/use-collapse.mjs.map +1 -1
- package/esm/use-collapse/use-horizontal-collapse.mjs +5 -4
- package/esm/use-collapse/use-horizontal-collapse.mjs.map +1 -1
- package/esm/use-counter/use-counter.mjs +12 -3
- package/esm/use-counter/use-counter.mjs.map +1 -1
- package/esm/use-debounced-callback/use-debounced-callback.mjs +28 -4
- package/esm/use-debounced-callback/use-debounced-callback.mjs.map +1 -1
- package/esm/use-debounced-state/use-debounced-state.mjs +1 -1
- package/esm/use-debounced-state/use-debounced-state.mjs.map +1 -1
- package/esm/use-debounced-value/use-debounced-value.mjs +22 -2
- package/esm/use-debounced-value/use-debounced-value.mjs.map +1 -1
- package/esm/use-event-listener/use-event-listener.mjs.map +1 -1
- package/esm/use-favicon/use-favicon.mjs +3 -1
- package/esm/use-favicon/use-favicon.mjs.map +1 -1
- package/esm/use-fetch/use-fetch.mjs +7 -4
- package/esm/use-fetch/use-fetch.mjs.map +1 -1
- package/esm/use-file-dialog/use-file-dialog.mjs +1 -0
- package/esm/use-file-dialog/use-file-dialog.mjs.map +1 -1
- package/esm/use-floating-window/use-floating-window.mjs +33 -36
- package/esm/use-floating-window/use-floating-window.mjs.map +1 -1
- package/esm/use-focus-trap/use-focus-trap.mjs +12 -9
- package/esm/use-focus-trap/use-focus-trap.mjs.map +1 -1
- package/esm/use-fullscreen/use-fullscreen.mjs +3 -1
- package/esm/use-fullscreen/use-fullscreen.mjs.map +1 -1
- package/esm/use-headroom/use-headroom.mjs +4 -1
- package/esm/use-headroom/use-headroom.mjs.map +1 -1
- package/esm/use-hotkeys/parse-hotkey.mjs +1 -2
- package/esm/use-hotkeys/parse-hotkey.mjs.map +1 -1
- package/esm/use-hover/use-hover.mjs +1 -0
- package/esm/use-hover/use-hover.mjs.map +1 -1
- package/esm/use-in-viewport/use-in-viewport.mjs +11 -7
- package/esm/use-in-viewport/use-in-viewport.mjs.map +1 -1
- package/esm/use-interval/use-interval.mjs +15 -6
- package/esm/use-interval/use-interval.mjs.map +1 -1
- package/esm/use-local-storage/create-storage.mjs +1 -1
- package/esm/use-local-storage/create-storage.mjs.map +1 -1
- package/esm/use-long-press/use-long-press.mjs +6 -2
- package/esm/use-long-press/use-long-press.mjs.map +1 -1
- package/esm/use-mask/use-mask.mjs +453 -0
- package/esm/use-mask/use-mask.mjs.map +1 -0
- package/esm/use-mouse/use-mouse.mjs +2 -2
- package/esm/use-mouse/use-mouse.mjs.map +1 -1
- package/esm/use-move/use-move.mjs +8 -0
- package/esm/use-move/use-move.mjs.map +1 -1
- package/esm/use-orientation/use-orientation.mjs +8 -2
- package/esm/use-orientation/use-orientation.mjs.map +1 -1
- package/esm/use-os/use-os.mjs +1 -1
- package/esm/use-os/use-os.mjs.map +1 -1
- package/esm/use-radial-move/use-radial-move.mjs +5 -5
- package/esm/use-radial-move/use-radial-move.mjs.map +1 -1
- package/esm/use-roving-index/use-roving-index.mjs +200 -0
- package/esm/use-roving-index/use-roving-index.mjs.map +1 -0
- package/esm/use-scroll-direction/use-scroll-direction.mjs +7 -7
- package/esm/use-scroll-direction/use-scroll-direction.mjs.map +1 -1
- package/esm/use-scroll-into-view/use-scroll-into-view.mjs +15 -5
- package/esm/use-scroll-into-view/use-scroll-into-view.mjs.map +1 -1
- package/esm/use-scroll-spy/use-scroll-spy.mjs +8 -4
- package/esm/use-scroll-spy/use-scroll-spy.mjs.map +1 -1
- package/esm/use-scroller/use-scroller.mjs +3 -3
- package/esm/use-scroller/use-scroller.mjs.map +1 -1
- package/esm/use-set/use-set.mjs +6 -1
- package/esm/use-set/use-set.mjs.map +1 -1
- package/esm/use-throttled-callback/use-throttled-callback.mjs +3 -1
- package/esm/use-throttled-callback/use-throttled-callback.mjs.map +1 -1
- package/esm/use-timeout/use-timeout.mjs +3 -1
- package/esm/use-timeout/use-timeout.mjs.map +1 -1
- package/esm/use-viewport-size/use-viewport-size.mjs +3 -3
- package/esm/use-viewport-size/use-viewport-size.mjs.map +1 -1
- package/esm/use-window-scroll/use-window-scroll.mjs +2 -2
- package/esm/use-window-scroll/use-window-scroll.mjs.map +1 -1
- package/lib/index.d.mts +5 -1
- package/lib/index.d.ts +5 -1
- package/lib/use-click-outside/use-click-outside.d.ts +1 -1
- package/lib/use-collapse/use-horizontal-collapse.d.ts +5 -0
- package/lib/use-counter/use-counter.d.ts +1 -0
- package/lib/use-debounced-callback/use-debounced-callback.d.ts +4 -0
- package/lib/use-debounced-value/use-debounced-value.d.ts +6 -1
- package/lib/use-event-listener/use-event-listener.d.ts +1 -1
- package/lib/use-hotkeys/parse-hotkey.d.ts +4 -5
- package/lib/use-long-press/use-long-press.d.ts +1 -0
- package/lib/use-mask/use-mask.d.ts +60 -0
- package/lib/use-roving-index/use-roving-index.d.ts +49 -0
- package/lib/use-scroll-into-view/use-scroll-into-view.d.ts +4 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-debounced-callback.mjs","names":[],"sources":["../../src/use-debounced-callback/use-debounced-callback.ts"],"sourcesContent":["import { useEffect, useMemo, useRef } from 'react';\nimport { useCallbackRef } from '../utils';\n\nexport interface UseDebouncedCallbackOptions {\n delay: number;\n flushOnUnmount?: boolean;\n leading?: boolean;\n}\n\nexport type UseDebouncedCallbackReturnValue<T extends (...args: any[]) => any> = ((\n ...args: Parameters<T>\n) => void) & { flush: () => void; cancel: () => void };\n\nexport function useDebouncedCallback<T extends (...args: any[]) => any>(\n callback: T,\n options: number | UseDebouncedCallbackOptions\n) {\n const { delay, flushOnUnmount, leading } =\n typeof options === 'number'\n ? {
|
|
1
|
+
{"version":3,"file":"use-debounced-callback.mjs","names":[],"sources":["../../src/use-debounced-callback/use-debounced-callback.ts"],"sourcesContent":["import { useEffect, useMemo, useRef } from 'react';\nimport { useCallbackRef } from '../utils';\n\nexport interface UseDebouncedCallbackOptions {\n delay: number;\n flushOnUnmount?: boolean;\n leading?: boolean;\n maxWait?: number;\n}\n\nexport type UseDebouncedCallbackReturnValue<T extends (...args: any[]) => any> = ((\n ...args: Parameters<T>\n) => void) & { flush: () => void; cancel: () => void; isPending: () => boolean };\n\nexport function useDebouncedCallback<T extends (...args: any[]) => any>(\n callback: T,\n options: number | UseDebouncedCallbackOptions\n) {\n const { delay, flushOnUnmount, leading, maxWait } =\n typeof options === 'number'\n ? {\n delay: options,\n flushOnUnmount: false,\n leading: false,\n maxWait: undefined as number | undefined,\n }\n : options;\n\n const handleCallback = useCallbackRef(callback);\n const debounceTimerRef = useRef(0);\n const maxWaitTimerRef = useRef(0);\n const latestArgsRef = useRef<Parameters<T> | null>(null);\n\n const lastCallback = useMemo(() => {\n const currentCallback = Object.assign(\n (...args: Parameters<T>) => {\n window.clearTimeout(debounceTimerRef.current);\n latestArgsRef.current = args;\n\n const isFirstCall = currentCallback._isFirstCall;\n currentCallback._isFirstCall = false;\n\n function clearTimeoutAndLeadingRef() {\n window.clearTimeout(debounceTimerRef.current);\n window.clearTimeout(maxWaitTimerRef.current);\n debounceTimerRef.current = 0;\n maxWaitTimerRef.current = 0;\n currentCallback._isFirstCall = true;\n currentCallback._hasPendingCallback = false;\n }\n\n function startMaxWaitTimer() {\n if (maxWait !== undefined && maxWaitTimerRef.current === 0) {\n maxWaitTimerRef.current = window.setTimeout(() => {\n if (debounceTimerRef.current !== 0) {\n const latestArgs = latestArgsRef.current!;\n clearTimeoutAndLeadingRef();\n handleCallback(...latestArgs);\n }\n }, maxWait);\n }\n }\n\n if (leading && isFirstCall) {\n handleCallback(...args);\n\n const resetLeadingState = () => {\n clearTimeoutAndLeadingRef();\n };\n\n const flush = () => {\n if (debounceTimerRef.current !== 0) {\n clearTimeoutAndLeadingRef();\n handleCallback(...args);\n }\n };\n\n const cancel = () => {\n clearTimeoutAndLeadingRef();\n };\n\n currentCallback.flush = flush;\n currentCallback.cancel = cancel;\n debounceTimerRef.current = window.setTimeout(resetLeadingState, delay);\n startMaxWaitTimer();\n return;\n }\n\n if (leading && !isFirstCall) {\n currentCallback._hasPendingCallback = true;\n const flush = () => {\n if (debounceTimerRef.current !== 0) {\n clearTimeoutAndLeadingRef();\n handleCallback(...args);\n }\n };\n\n const cancel = () => {\n clearTimeoutAndLeadingRef();\n };\n\n currentCallback.flush = flush;\n currentCallback.cancel = cancel;\n\n const resetLeadingState = () => {\n clearTimeoutAndLeadingRef();\n };\n debounceTimerRef.current = window.setTimeout(resetLeadingState, delay);\n startMaxWaitTimer();\n return;\n }\n\n currentCallback._hasPendingCallback = true;\n\n const flush = () => {\n if (debounceTimerRef.current !== 0) {\n clearTimeoutAndLeadingRef();\n handleCallback(...args);\n }\n };\n\n const cancel = () => {\n clearTimeoutAndLeadingRef();\n };\n\n currentCallback.flush = flush;\n currentCallback.cancel = cancel;\n debounceTimerRef.current = window.setTimeout(flush, delay);\n startMaxWaitTimer();\n },\n {\n flush: () => {},\n cancel: () => {},\n isPending: () => currentCallback._hasPendingCallback,\n _isFirstCall: true,\n _hasPendingCallback: false,\n }\n );\n return currentCallback;\n }, [handleCallback, delay, leading, maxWait]);\n\n useEffect(\n () => () => {\n if (flushOnUnmount) {\n lastCallback.flush();\n } else {\n lastCallback.cancel();\n }\n },\n [lastCallback, flushOnUnmount]\n );\n\n return lastCallback;\n}\n\nexport namespace useDebouncedCallback {\n export type Options = UseDebouncedCallbackOptions;\n export type ReturnValue<T extends (...args: any[]) => any> = UseDebouncedCallbackReturnValue<T>;\n}\n"],"mappings":";;;;AAcA,SAAgB,qBACd,UACA,SACA;CACA,MAAM,EAAE,OAAO,gBAAgB,SAAS,YACtC,OAAO,YAAY,WACf;EACE,OAAO;EACP,gBAAgB;EAChB,SAAS;EACT,SAAS,KAAA;EACV,GACD;CAEN,MAAM,iBAAiB,eAAe,SAAS;CAC/C,MAAM,mBAAmB,OAAO,EAAE;CAClC,MAAM,kBAAkB,OAAO,EAAE;CACjC,MAAM,gBAAgB,OAA6B,KAAK;CAExD,MAAM,eAAe,cAAc;EACjC,MAAM,kBAAkB,OAAO,QAC5B,GAAG,SAAwB;AAC1B,UAAO,aAAa,iBAAiB,QAAQ;AAC7C,iBAAc,UAAU;GAExB,MAAM,cAAc,gBAAgB;AACpC,mBAAgB,eAAe;GAE/B,SAAS,4BAA4B;AACnC,WAAO,aAAa,iBAAiB,QAAQ;AAC7C,WAAO,aAAa,gBAAgB,QAAQ;AAC5C,qBAAiB,UAAU;AAC3B,oBAAgB,UAAU;AAC1B,oBAAgB,eAAe;AAC/B,oBAAgB,sBAAsB;;GAGxC,SAAS,oBAAoB;AAC3B,QAAI,YAAY,KAAA,KAAa,gBAAgB,YAAY,EACvD,iBAAgB,UAAU,OAAO,iBAAiB;AAChD,SAAI,iBAAiB,YAAY,GAAG;MAClC,MAAM,aAAa,cAAc;AACjC,iCAA2B;AAC3B,qBAAe,GAAG,WAAW;;OAE9B,QAAQ;;AAIf,OAAI,WAAW,aAAa;AAC1B,mBAAe,GAAG,KAAK;IAEvB,MAAM,0BAA0B;AAC9B,gCAA2B;;IAG7B,MAAM,cAAc;AAClB,SAAI,iBAAiB,YAAY,GAAG;AAClC,iCAA2B;AAC3B,qBAAe,GAAG,KAAK;;;IAI3B,MAAM,eAAe;AACnB,gCAA2B;;AAG7B,oBAAgB,QAAQ;AACxB,oBAAgB,SAAS;AACzB,qBAAiB,UAAU,OAAO,WAAW,mBAAmB,MAAM;AACtE,uBAAmB;AACnB;;AAGF,OAAI,WAAW,CAAC,aAAa;AAC3B,oBAAgB,sBAAsB;IACtC,MAAM,cAAc;AAClB,SAAI,iBAAiB,YAAY,GAAG;AAClC,iCAA2B;AAC3B,qBAAe,GAAG,KAAK;;;IAI3B,MAAM,eAAe;AACnB,gCAA2B;;AAG7B,oBAAgB,QAAQ;AACxB,oBAAgB,SAAS;IAEzB,MAAM,0BAA0B;AAC9B,gCAA2B;;AAE7B,qBAAiB,UAAU,OAAO,WAAW,mBAAmB,MAAM;AACtE,uBAAmB;AACnB;;AAGF,mBAAgB,sBAAsB;GAEtC,MAAM,cAAc;AAClB,QAAI,iBAAiB,YAAY,GAAG;AAClC,gCAA2B;AAC3B,oBAAe,GAAG,KAAK;;;GAI3B,MAAM,eAAe;AACnB,+BAA2B;;AAG7B,mBAAgB,QAAQ;AACxB,mBAAgB,SAAS;AACzB,oBAAiB,UAAU,OAAO,WAAW,OAAO,MAAM;AAC1D,sBAAmB;KAErB;GACE,aAAa;GACb,cAAc;GACd,iBAAiB,gBAAgB;GACjC,cAAc;GACd,qBAAqB;GACtB,CACF;AACD,SAAO;IACN;EAAC;EAAgB;EAAO;EAAS;EAAQ,CAAC;AAE7C,uBACc;AACV,MAAI,eACF,cAAa,OAAO;MAEpB,cAAa,QAAQ;IAGzB,CAAC,cAAc,eAAe,CAC/B;AAED,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-debounced-state.mjs","names":[],"sources":["../../src/use-debounced-state/use-debounced-state.ts"],"sourcesContent":["import { SetStateAction, useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseDebouncedStateOptions {\n leading?: boolean;\n}\n\nexport type UseDebouncedStateReturnValue<T> = [T, (newValue: SetStateAction<T>) => void];\n\nexport function useDebouncedState<T = any>(\n defaultValue: T,\n wait: number,\n options: UseDebouncedStateOptions = { leading: false }\n): UseDebouncedStateReturnValue<T> {\n const [value, setValue] = useState(defaultValue);\n const timeoutRef = useRef<number | null>(null);\n const leadingRef = useRef(true);\n\n const clearTimeout = () => window.clearTimeout(timeoutRef.current!);\n useEffect(() => clearTimeout, []);\n\n const debouncedSetValue = useCallback(\n (newValue: SetStateAction<T>) => {\n clearTimeout();\n if (leadingRef.current && options.leading) {\n setValue(newValue);\n } else {\n timeoutRef.current = window.setTimeout(() => {\n leadingRef.current = true;\n setValue(newValue);\n }, wait);\n }\n leadingRef.current = false;\n },\n [options.leading]\n );\n\n return [value, debouncedSetValue] as const;\n}\n\nexport namespace useDebouncedState {\n export type Options = UseDebouncedStateOptions;\n export type ReturnValue<T> = UseDebouncedStateReturnValue<T>;\n}\n"],"mappings":";;;AAQA,SAAgB,kBACd,cACA,MACA,UAAoC,EAAE,SAAS,OAAO,EACrB;CACjC,MAAM,CAAC,OAAO,YAAY,SAAS,aAAa;CAChD,MAAM,aAAa,OAAsB,KAAK;CAC9C,MAAM,aAAa,OAAO,KAAK;CAE/B,MAAM,qBAAqB,OAAO,aAAa,WAAW,QAAS;AACnE,iBAAgB,cAAc,EAAE,CAAC;AAkBjC,QAAO,CAAC,OAhBkB,aACvB,aAAgC;AAC/B,gBAAc;AACd,MAAI,WAAW,WAAW,QAAQ,QAChC,UAAS,SAAS;MAElB,YAAW,UAAU,OAAO,iBAAiB;AAC3C,cAAW,UAAU;AACrB,YAAS,SAAS;KACjB,KAAK;AAEV,aAAW,UAAU;IAEvB,CAAC,QAAQ,
|
|
1
|
+
{"version":3,"file":"use-debounced-state.mjs","names":[],"sources":["../../src/use-debounced-state/use-debounced-state.ts"],"sourcesContent":["import { SetStateAction, useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseDebouncedStateOptions {\n leading?: boolean;\n}\n\nexport type UseDebouncedStateReturnValue<T> = [T, (newValue: SetStateAction<T>) => void];\n\nexport function useDebouncedState<T = any>(\n defaultValue: T,\n wait: number,\n options: UseDebouncedStateOptions = { leading: false }\n): UseDebouncedStateReturnValue<T> {\n const [value, setValue] = useState(defaultValue);\n const timeoutRef = useRef<number | null>(null);\n const leadingRef = useRef(true);\n\n const clearTimeout = () => window.clearTimeout(timeoutRef.current!);\n useEffect(() => clearTimeout, []);\n\n const debouncedSetValue = useCallback(\n (newValue: SetStateAction<T>) => {\n clearTimeout();\n if (leadingRef.current && options.leading) {\n setValue(newValue);\n } else {\n timeoutRef.current = window.setTimeout(() => {\n leadingRef.current = true;\n setValue(newValue);\n }, wait);\n }\n leadingRef.current = false;\n },\n [options.leading, wait]\n );\n\n return [value, debouncedSetValue] as const;\n}\n\nexport namespace useDebouncedState {\n export type Options = UseDebouncedStateOptions;\n export type ReturnValue<T> = UseDebouncedStateReturnValue<T>;\n}\n"],"mappings":";;;AAQA,SAAgB,kBACd,cACA,MACA,UAAoC,EAAE,SAAS,OAAO,EACrB;CACjC,MAAM,CAAC,OAAO,YAAY,SAAS,aAAa;CAChD,MAAM,aAAa,OAAsB,KAAK;CAC9C,MAAM,aAAa,OAAO,KAAK;CAE/B,MAAM,qBAAqB,OAAO,aAAa,WAAW,QAAS;AACnE,iBAAgB,cAAc,EAAE,CAAC;AAkBjC,QAAO,CAAC,OAhBkB,aACvB,aAAgC;AAC/B,gBAAc;AACd,MAAI,WAAW,WAAW,QAAQ,QAChC,UAAS,SAAS;MAElB,YAAW,UAAU,OAAO,iBAAiB;AAC3C,cAAW,UAAU;AACrB,YAAS,SAAS;KACjB,KAAK;AAEV,aAAW,UAAU;IAEvB,CAAC,QAAQ,SAAS,KAAK,CACxB,CAEgC"}
|
|
@@ -6,7 +6,20 @@ function useDebouncedValue(value, wait, options = { leading: false }) {
|
|
|
6
6
|
const mountedRef = useRef(false);
|
|
7
7
|
const timeoutRef = useRef(null);
|
|
8
8
|
const cooldownRef = useRef(false);
|
|
9
|
-
const
|
|
9
|
+
const latestValueRef = useRef(value);
|
|
10
|
+
latestValueRef.current = value;
|
|
11
|
+
const cancel = useCallback(() => {
|
|
12
|
+
window.clearTimeout(timeoutRef.current);
|
|
13
|
+
timeoutRef.current = null;
|
|
14
|
+
cooldownRef.current = false;
|
|
15
|
+
}, []);
|
|
16
|
+
const flush = useCallback(() => {
|
|
17
|
+
if (timeoutRef.current) {
|
|
18
|
+
cancel();
|
|
19
|
+
cooldownRef.current = false;
|
|
20
|
+
setValue(latestValueRef.current);
|
|
21
|
+
}
|
|
22
|
+
}, []);
|
|
10
23
|
useEffect(() => {
|
|
11
24
|
if (mountedRef.current) if (!cooldownRef.current && options.leading) {
|
|
12
25
|
cooldownRef.current = true;
|
|
@@ -27,7 +40,14 @@ function useDebouncedValue(value, wait, options = { leading: false }) {
|
|
|
27
40
|
mountedRef.current = true;
|
|
28
41
|
return cancel;
|
|
29
42
|
}, []);
|
|
30
|
-
return [
|
|
43
|
+
return [
|
|
44
|
+
_value,
|
|
45
|
+
cancel,
|
|
46
|
+
{
|
|
47
|
+
cancel,
|
|
48
|
+
flush
|
|
49
|
+
}
|
|
50
|
+
];
|
|
31
51
|
}
|
|
32
52
|
//#endregion
|
|
33
53
|
export { useDebouncedValue };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-debounced-value.mjs","names":[],"sources":["../../src/use-debounced-value/use-debounced-value.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseDebouncedValueOptions {\n leading?: boolean;\n}\n\nexport type UseDebouncedValueReturnValue<T> = [T, () => void];\n\nexport function useDebouncedValue<T = any>(\n value: T,\n wait: number,\n options: UseDebouncedValueOptions = { leading: false }\n): UseDebouncedValueReturnValue<T> {\n const [_value, setValue] = useState(value);\n const mountedRef = useRef(false);\n const timeoutRef = useRef<number | null>(null);\n const cooldownRef = useRef(false);\n\n const cancel = useCallback(() => window.clearTimeout(timeoutRef.current!), []);\n\n useEffect(() => {\n if (mountedRef.current) {\n if (!cooldownRef.current && options.leading) {\n cooldownRef.current = true;\n setValue(value);\n } else {\n cancel();\n timeoutRef.current = window.setTimeout(() => {\n cooldownRef.current = false;\n setValue(value);\n }, wait);\n }\n }\n }, [value, options.leading, wait]);\n\n useEffect(() => {\n mountedRef.current = true;\n return cancel;\n }, []);\n\n return [_value, cancel];\n}\n\nexport namespace useDebouncedValue {\n export type Options = UseDebouncedValueOptions;\n export type ReturnValue<T> = UseDebouncedValueReturnValue<T>;\n}\n"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"use-debounced-value.mjs","names":[],"sources":["../../src/use-debounced-value/use-debounced-value.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseDebouncedValueOptions {\n leading?: boolean;\n}\n\nexport interface UseDebouncedValueHandlers {\n cancel: () => void;\n flush: () => void;\n}\n\nexport type UseDebouncedValueReturnValue<T> = [T, () => void, UseDebouncedValueHandlers];\n\nexport function useDebouncedValue<T = any>(\n value: T,\n wait: number,\n options: UseDebouncedValueOptions = { leading: false }\n): UseDebouncedValueReturnValue<T> {\n const [_value, setValue] = useState(value);\n const mountedRef = useRef(false);\n const timeoutRef = useRef<number | null>(null);\n const cooldownRef = useRef(false);\n\n const latestValueRef = useRef(value);\n latestValueRef.current = value;\n\n const cancel = useCallback(() => {\n window.clearTimeout(timeoutRef.current!);\n timeoutRef.current = null;\n cooldownRef.current = false;\n }, []);\n\n const flush = useCallback(() => {\n if (timeoutRef.current) {\n cancel();\n cooldownRef.current = false;\n setValue(latestValueRef.current);\n }\n }, []);\n\n useEffect(() => {\n if (mountedRef.current) {\n if (!cooldownRef.current && options.leading) {\n cooldownRef.current = true;\n setValue(value);\n } else {\n cancel();\n timeoutRef.current = window.setTimeout(() => {\n cooldownRef.current = false;\n setValue(value);\n }, wait);\n }\n }\n }, [value, options.leading, wait]);\n\n useEffect(() => {\n mountedRef.current = true;\n return cancel;\n }, []);\n\n return [_value, cancel, { cancel, flush }];\n}\n\nexport namespace useDebouncedValue {\n export type Handlers = UseDebouncedValueHandlers;\n export type Options = UseDebouncedValueOptions;\n export type ReturnValue<T> = UseDebouncedValueReturnValue<T>;\n}\n"],"mappings":";;;AAaA,SAAgB,kBACd,OACA,MACA,UAAoC,EAAE,SAAS,OAAO,EACrB;CACjC,MAAM,CAAC,QAAQ,YAAY,SAAS,MAAM;CAC1C,MAAM,aAAa,OAAO,MAAM;CAChC,MAAM,aAAa,OAAsB,KAAK;CAC9C,MAAM,cAAc,OAAO,MAAM;CAEjC,MAAM,iBAAiB,OAAO,MAAM;AACpC,gBAAe,UAAU;CAEzB,MAAM,SAAS,kBAAkB;AAC/B,SAAO,aAAa,WAAW,QAAS;AACxC,aAAW,UAAU;AACrB,cAAY,UAAU;IACrB,EAAE,CAAC;CAEN,MAAM,QAAQ,kBAAkB;AAC9B,MAAI,WAAW,SAAS;AACtB,WAAQ;AACR,eAAY,UAAU;AACtB,YAAS,eAAe,QAAQ;;IAEjC,EAAE,CAAC;AAEN,iBAAgB;AACd,MAAI,WAAW,QACb,KAAI,CAAC,YAAY,WAAW,QAAQ,SAAS;AAC3C,eAAY,UAAU;AACtB,YAAS,MAAM;SACV;AACL,WAAQ;AACR,cAAW,UAAU,OAAO,iBAAiB;AAC3C,gBAAY,UAAU;AACtB,aAAS,MAAM;MACd,KAAK;;IAGX;EAAC;EAAO,QAAQ;EAAS;EAAK,CAAC;AAElC,iBAAgB;AACd,aAAW,UAAU;AACrB,SAAO;IACN,EAAE,CAAC;AAEN,QAAO;EAAC;EAAQ;EAAQ;GAAE;GAAQ;GAAO;EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-event-listener.mjs","names":[],"sources":["../../src/use-event-listener/use-event-listener.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport function useEventListener<K extends keyof HTMLElementEventMap, T extends HTMLElement = any>(\n type: K,\n listener: (this:
|
|
1
|
+
{"version":3,"file":"use-event-listener.mjs","names":[],"sources":["../../src/use-event-listener/use-event-listener.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport function useEventListener<K extends keyof HTMLElementEventMap, T extends HTMLElement = any>(\n type: K,\n listener: (this: T, ev: HTMLElementEventMap[K]) => any,\n options?: boolean | AddEventListenerOptions\n): React.RefCallback<T | null> {\n const previousListener = useRef<Function | null>(null);\n const previousNode = useRef<T | null>(null);\n\n const callbackRef: React.RefCallback<T | null> = useCallback(\n (node) => {\n if (!node) {\n return;\n }\n\n if (previousNode.current && previousListener.current) {\n previousNode.current.removeEventListener(type, previousListener.current as any, options);\n }\n\n node.addEventListener(type, listener as any, options);\n previousNode.current = node;\n previousListener.current = listener;\n },\n [type, listener, options]\n );\n\n useEffect(\n () => () => {\n if (previousNode.current && previousListener.current) {\n previousNode.current.removeEventListener(type, previousListener.current as any, options);\n }\n },\n [type, options]\n );\n\n return callbackRef;\n}\n"],"mappings":";;;AAEA,SAAgB,iBACd,MACA,UACA,SAC6B;CAC7B,MAAM,mBAAmB,OAAwB,KAAK;CACtD,MAAM,eAAe,OAAiB,KAAK;CAE3C,MAAM,cAA2C,aAC9C,SAAS;AACR,MAAI,CAAC,KACH;AAGF,MAAI,aAAa,WAAW,iBAAiB,QAC3C,cAAa,QAAQ,oBAAoB,MAAM,iBAAiB,SAAgB,QAAQ;AAG1F,OAAK,iBAAiB,MAAM,UAAiB,QAAQ;AACrD,eAAa,UAAU;AACvB,mBAAiB,UAAU;IAE7B;EAAC;EAAM;EAAU;EAAQ,CAC1B;AAED,uBACc;AACV,MAAI,aAAa,WAAW,iBAAiB,QAC3C,cAAa,QAAQ,oBAAoB,MAAM,iBAAiB,SAAgB,QAAQ;IAG5F,CAAC,MAAM,QAAQ,CAChB;AAED,QAAO"}
|
|
@@ -20,7 +20,9 @@ function useFavicon(url) {
|
|
|
20
20
|
document.querySelector("head").appendChild(element);
|
|
21
21
|
}
|
|
22
22
|
const splittedUrl = url.split(".");
|
|
23
|
-
|
|
23
|
+
const mimeType = MIME_TYPES[splittedUrl[splittedUrl.length - 1].toLowerCase()];
|
|
24
|
+
if (mimeType) link.current.setAttribute("type", mimeType);
|
|
25
|
+
else link.current.removeAttribute("type");
|
|
24
26
|
link.current.setAttribute("href", url);
|
|
25
27
|
}, [url]);
|
|
26
28
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-favicon.mjs","names":[],"sources":["../../src/use-favicon/use-favicon.ts"],"sourcesContent":["import { useRef } from 'react';\nimport { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';\n\nconst MIME_TYPES: Record<string, string> = {\n ico: 'image/x-icon',\n png: 'image/png',\n svg: 'image/svg+xml',\n gif: 'image/gif',\n};\n\nexport function useFavicon(url: string) {\n const link = useRef<HTMLLinkElement>(null);\n\n useIsomorphicEffect(() => {\n if (!url) {\n return;\n }\n\n if (!link.current) {\n const existingElements = document.querySelectorAll<HTMLLinkElement>('link[rel*=\"icon\"]');\n existingElements.forEach((element) => document.head.removeChild(element));\n\n const element = document.createElement('link');\n element.rel = 'shortcut icon';\n link.current = element;\n document.querySelector('head')!.appendChild(element);\n }\n\n const splittedUrl = url.split('.');\n
|
|
1
|
+
{"version":3,"file":"use-favicon.mjs","names":[],"sources":["../../src/use-favicon/use-favicon.ts"],"sourcesContent":["import { useRef } from 'react';\nimport { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';\n\nconst MIME_TYPES: Record<string, string> = {\n ico: 'image/x-icon',\n png: 'image/png',\n svg: 'image/svg+xml',\n gif: 'image/gif',\n};\n\nexport function useFavicon(url: string) {\n const link = useRef<HTMLLinkElement>(null);\n\n useIsomorphicEffect(() => {\n if (!url) {\n return;\n }\n\n if (!link.current) {\n const existingElements = document.querySelectorAll<HTMLLinkElement>('link[rel*=\"icon\"]');\n existingElements.forEach((element) => document.head.removeChild(element));\n\n const element = document.createElement('link');\n element.rel = 'shortcut icon';\n link.current = element;\n document.querySelector('head')!.appendChild(element);\n }\n\n const splittedUrl = url.split('.');\n const extension = splittedUrl[splittedUrl.length - 1].toLowerCase();\n const mimeType = MIME_TYPES[extension];\n\n if (mimeType) {\n link.current.setAttribute('type', mimeType);\n } else {\n link.current.removeAttribute('type');\n }\n\n link.current.setAttribute('href', url);\n }, [url]);\n}\n"],"mappings":";;;;AAGA,MAAM,aAAqC;CACzC,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACN;AAED,SAAgB,WAAW,KAAa;CACtC,MAAM,OAAO,OAAwB,KAAK;AAE1C,2BAA0B;AACxB,MAAI,CAAC,IACH;AAGF,MAAI,CAAC,KAAK,SAAS;AACQ,YAAS,iBAAkC,sBAAoB,CACvE,SAAS,YAAY,SAAS,KAAK,YAAY,QAAQ,CAAC;GAEzE,MAAM,UAAU,SAAS,cAAc,OAAO;AAC9C,WAAQ,MAAM;AACd,QAAK,UAAU;AACf,YAAS,cAAc,OAAO,CAAE,YAAY,QAAQ;;EAGtD,MAAM,cAAc,IAAI,MAAM,IAAI;EAElC,MAAM,WAAW,WADC,YAAY,YAAY,SAAS,GAAG,aAAa;AAGnE,MAAI,SACF,MAAK,QAAQ,aAAa,QAAQ,SAAS;MAE3C,MAAK,QAAQ,gBAAgB,OAAO;AAGtC,OAAK,QAAQ,aAAa,QAAQ,IAAI;IACrC,CAAC,IAAI,CAAC"}
|
|
@@ -11,9 +11,12 @@ function useFetch(url, { autoInvoke = true, ...options } = {}) {
|
|
|
11
11
|
controller.current = new AbortController();
|
|
12
12
|
setLoading(true);
|
|
13
13
|
return fetch(url, {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}).then((res) =>
|
|
14
|
+
...options,
|
|
15
|
+
signal: controller.current.signal
|
|
16
|
+
}).then((res) => {
|
|
17
|
+
if (!res.ok) throw new Error(`Request failed with status ${res.status}`);
|
|
18
|
+
return res.json();
|
|
19
|
+
}).then((res) => {
|
|
17
20
|
setData(res);
|
|
18
21
|
setLoading(false);
|
|
19
22
|
return res;
|
|
@@ -22,7 +25,7 @@ function useFetch(url, { autoInvoke = true, ...options } = {}) {
|
|
|
22
25
|
if (err.name !== "AbortError") setError(err);
|
|
23
26
|
return err;
|
|
24
27
|
});
|
|
25
|
-
}, [url]);
|
|
28
|
+
}, [url, JSON.stringify(options)]);
|
|
26
29
|
const abort = useCallback(() => {
|
|
27
30
|
if (controller.current) controller.current?.abort("");
|
|
28
31
|
}, []);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-fetch.mjs","names":[],"sources":["../../src/use-fetch/use-fetch.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseFetchOptions extends RequestInit {\n autoInvoke?: boolean;\n}\n\nexport interface UseFetchReturnValue<T> {\n data: T | null;\n loading: boolean;\n error: Error | null;\n refetch: () => Promise<any>;\n abort: () => void;\n}\n\nexport function useFetch<T>(\n url: string,\n { autoInvoke = true, ...options }: UseFetchOptions = {}\n): UseFetchReturnValue<T> {\n const [data, setData] = useState<T | null>(null);\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n const controller = useRef<AbortController | null>(null);\n\n const refetch = useCallback(() => {\n if (controller.current) {\n controller.current.abort();\n }\n\n controller.current = new AbortController();\n\n setLoading(true);\n\n return fetch(url, { signal: controller.current.signal
|
|
1
|
+
{"version":3,"file":"use-fetch.mjs","names":[],"sources":["../../src/use-fetch/use-fetch.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseFetchOptions extends RequestInit {\n autoInvoke?: boolean;\n}\n\nexport interface UseFetchReturnValue<T> {\n data: T | null;\n loading: boolean;\n error: Error | null;\n refetch: () => Promise<any>;\n abort: () => void;\n}\n\nexport function useFetch<T>(\n url: string,\n { autoInvoke = true, ...options }: UseFetchOptions = {}\n): UseFetchReturnValue<T> {\n const [data, setData] = useState<T | null>(null);\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n const controller = useRef<AbortController | null>(null);\n\n const refetch = useCallback(() => {\n if (controller.current) {\n controller.current.abort();\n }\n\n controller.current = new AbortController();\n\n setLoading(true);\n\n return fetch(url, { ...options, signal: controller.current.signal })\n .then((res) => {\n if (!res.ok) {\n throw new Error(`Request failed with status ${res.status}`);\n }\n return res.json();\n })\n .then((res) => {\n setData(res);\n setLoading(false);\n return res as T;\n })\n .catch((err) => {\n setLoading(false);\n\n if (err.name !== 'AbortError') {\n setError(err);\n }\n\n return err;\n });\n }, [url, JSON.stringify(options)]);\n\n const abort = useCallback(() => {\n if (controller.current) {\n controller.current?.abort('');\n }\n }, []);\n\n useEffect(() => {\n if (autoInvoke) {\n refetch();\n }\n\n return () => {\n if (controller.current) {\n controller.current.abort('');\n }\n };\n }, [refetch, autoInvoke]);\n\n return { data, loading, error, refetch, abort };\n}\n\nexport namespace useFetch {\n export type Options = UseFetchOptions;\n export type ReturnValue<T> = UseFetchReturnValue<T>;\n}\n"],"mappings":";;;AAcA,SAAgB,SACd,KACA,EAAE,aAAa,MAAM,GAAG,YAA6B,EAAE,EAC/B;CACxB,MAAM,CAAC,MAAM,WAAW,SAAmB,KAAK;CAChD,MAAM,CAAC,SAAS,cAAc,SAAS,MAAM;CAC7C,MAAM,CAAC,OAAO,YAAY,SAAuB,KAAK;CACtD,MAAM,aAAa,OAA+B,KAAK;CAEvD,MAAM,UAAU,kBAAkB;AAChC,MAAI,WAAW,QACb,YAAW,QAAQ,OAAO;AAG5B,aAAW,UAAU,IAAI,iBAAiB;AAE1C,aAAW,KAAK;AAEhB,SAAO,MAAM,KAAK;GAAE,GAAG;GAAS,QAAQ,WAAW,QAAQ;GAAQ,CAAC,CACjE,MAAM,QAAQ;AACb,OAAI,CAAC,IAAI,GACP,OAAM,IAAI,MAAM,8BAA8B,IAAI,SAAS;AAE7D,UAAO,IAAI,MAAM;IACjB,CACD,MAAM,QAAQ;AACb,WAAQ,IAAI;AACZ,cAAW,MAAM;AACjB,UAAO;IACP,CACD,OAAO,QAAQ;AACd,cAAW,MAAM;AAEjB,OAAI,IAAI,SAAS,aACf,UAAS,IAAI;AAGf,UAAO;IACP;IACH,CAAC,KAAK,KAAK,UAAU,QAAQ,CAAC,CAAC;CAElC,MAAM,QAAQ,kBAAkB;AAC9B,MAAI,WAAW,QACb,YAAW,SAAS,MAAM,GAAG;IAE9B,EAAE,CAAC;AAEN,iBAAgB;AACd,MAAI,WACF,UAAS;AAGX,eAAa;AACX,OAAI,WAAW,QACb,YAAW,QAAQ,MAAM,GAAG;;IAG/B,CAAC,SAAS,WAAW,CAAC;AAEzB,QAAO;EAAE;EAAM;EAAS;EAAO;EAAS;EAAO"}
|
|
@@ -43,6 +43,7 @@ function useFileDialog(input = {}) {
|
|
|
43
43
|
inputRef.current = createInput(options);
|
|
44
44
|
if (inputRef.current) {
|
|
45
45
|
inputRef.current.addEventListener("change", handleChange, { once: true });
|
|
46
|
+
if (options.onCancel) inputRef.current.addEventListener("cancel", options.onCancel, { once: true });
|
|
46
47
|
document.body.appendChild(inputRef.current);
|
|
47
48
|
}
|
|
48
49
|
}, [options, handleChange]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-file-dialog.mjs","names":[],"sources":["../../src/use-file-dialog/use-file-dialog.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\nimport { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';\n\nexport interface UseFileDialogOptions {\n /** Determines whether multiple files are allowed, `true` by default */\n multiple?: boolean;\n\n /** `accept` attribute of the file input, '*' by default */\n accept?: string;\n\n /** `capture` attribute of the file input */\n capture?: string;\n\n /** Determines whether the user can pick a directory instead of file, `false` by default */\n directory?: boolean;\n\n /** Determines whether the file input state should be reset when the file dialog is opened, `false` by default */\n resetOnOpen?: boolean;\n\n /** Initial selected files */\n initialFiles?: FileList | File[];\n\n /** Called when files are selected */\n onChange?: (files: FileList | null) => void;\n\n /** Called when file dialog is canceled */\n onCancel?: () => void;\n}\n\nconst defaultOptions: UseFileDialogOptions = {\n multiple: true,\n accept: '*',\n};\n\nfunction getInitialFilesList(files: UseFileDialogOptions['initialFiles']): FileList | null {\n if (!files) {\n return null;\n }\n\n if (files instanceof FileList) {\n return files;\n }\n\n const result = new DataTransfer();\n for (const file of files) {\n result.items.add(file);\n }\n\n return result.files;\n}\n\nfunction createInput(options: UseFileDialogOptions) {\n if (typeof document === 'undefined') {\n return null;\n }\n\n const input = document.createElement('input');\n input.type = 'file';\n\n if (options.accept) {\n input.accept = options.accept;\n }\n\n if (options.multiple) {\n input.multiple = options.multiple;\n }\n\n if (options.capture) {\n input.capture = options.capture;\n }\n\n if (options.directory) {\n input.webkitdirectory = options.directory;\n }\n\n input.style.display = 'none';\n return input;\n}\n\nexport interface UseFileDialogReturnValue {\n files: FileList | null;\n open: () => void;\n reset: () => void;\n}\n\nexport function useFileDialog(input: UseFileDialogOptions = {}): UseFileDialogReturnValue {\n const options: UseFileDialogOptions = { ...defaultOptions, ...input };\n const [files, setFiles] = useState<FileList | null>(getInitialFilesList(options.initialFiles));\n const inputRef = useRef<HTMLInputElement | null>(null);\n\n const handleChange = useCallback(\n (event: Event) => {\n const target = event.target as HTMLInputElement;\n if (target?.files) {\n setFiles(target.files);\n options.onChange?.(target.files);\n }\n },\n [options.onChange]\n );\n\n const createAndSetupInput = useCallback(() => {\n inputRef.current?.remove();\n inputRef.current = createInput(options);\n\n if (inputRef.current) {\n inputRef.current.addEventListener('change', handleChange, { once: true });\n document.body.appendChild(inputRef.current);\n }\n }, [options, handleChange]);\n\n useIsomorphicEffect(() => {\n createAndSetupInput();\n return () => inputRef.current?.remove();\n }, []);\n\n const reset = useCallback(() => {\n setFiles(null);\n options.onChange?.(null);\n }, [options.onChange]);\n\n const open = useCallback(() => {\n if (options.resetOnOpen) {\n reset();\n }\n\n createAndSetupInput();\n inputRef.current?.click();\n }, [options.resetOnOpen, reset, createAndSetupInput]);\n\n return { files, open, reset };\n}\n\nexport namespace useFileD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5C,CAAC,SAAS,aAAa,CAAC;AAE3B,2BAA0B;AACxB,uBAAqB;AACrB,eAAa,SAAS,SAAS,QAAQ;IACtC,EAAE,CAAC;CAEN,MAAM,QAAQ,kBAAkB;AAC9B,WAAS,KAAK;AACd,UAAQ,WAAW,KAAK;IACvB,CAAC,QAAQ,SAAS,CAAC;AAWtB,QAAO;EAAE;EAAO,MATH,kBAAkB;AAC7B,OAAI,QAAQ,YACV,QAAO;AAGT,wBAAqB;AACrB,YAAS,SAAS,OAAO;KACxB;GAAC,QAAQ;GAAa;GAAO;GAAoB,CAAC;EAE/B;EAAO"}
|
|
1
|
+
{"version":3,"file":"use-file-dialog.mjs","names":[],"sources":["../../src/use-file-dialog/use-file-dialog.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\nimport { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';\n\nexport interface UseFileDialogOptions {\n /** Determines whether multiple files are allowed, `true` by default */\n multiple?: boolean;\n\n /** `accept` attribute of the file input, '*' by default */\n accept?: string;\n\n /** `capture` attribute of the file input */\n capture?: string;\n\n /** Determines whether the user can pick a directory instead of file, `false` by default */\n directory?: boolean;\n\n /** Determines whether the file input state should be reset when the file dialog is opened, `false` by default */\n resetOnOpen?: boolean;\n\n /** Initial selected files */\n initialFiles?: FileList | File[];\n\n /** Called when files are selected */\n onChange?: (files: FileList | null) => void;\n\n /** Called when file dialog is canceled */\n onCancel?: () => void;\n}\n\nconst defaultOptions: UseFileDialogOptions = {\n multiple: true,\n accept: '*',\n};\n\nfunction getInitialFilesList(files: UseFileDialogOptions['initialFiles']): FileList | null {\n if (!files) {\n return null;\n }\n\n if (files instanceof FileList) {\n return files;\n }\n\n const result = new DataTransfer();\n for (const file of files) {\n result.items.add(file);\n }\n\n return result.files;\n}\n\nfunction createInput(options: UseFileDialogOptions) {\n if (typeof document === 'undefined') {\n return null;\n }\n\n const input = document.createElement('input');\n input.type = 'file';\n\n if (options.accept) {\n input.accept = options.accept;\n }\n\n if (options.multiple) {\n input.multiple = options.multiple;\n }\n\n if (options.capture) {\n input.capture = options.capture;\n }\n\n if (options.directory) {\n input.webkitdirectory = options.directory;\n }\n\n input.style.display = 'none';\n return input;\n}\n\nexport interface UseFileDialogReturnValue {\n files: FileList | null;\n open: () => void;\n reset: () => void;\n}\n\nexport function useFileDialog(input: UseFileDialogOptions = {}): UseFileDialogReturnValue {\n const options: UseFileDialogOptions = { ...defaultOptions, ...input };\n const [files, setFiles] = useState<FileList | null>(getInitialFilesList(options.initialFiles));\n const inputRef = useRef<HTMLInputElement | null>(null);\n\n const handleChange = useCallback(\n (event: Event) => {\n const target = event.target as HTMLInputElement;\n if (target?.files) {\n setFiles(target.files);\n options.onChange?.(target.files);\n }\n },\n [options.onChange]\n );\n\n const createAndSetupInput = useCallback(() => {\n inputRef.current?.remove();\n inputRef.current = createInput(options);\n\n if (inputRef.current) {\n inputRef.current.addEventListener('change', handleChange, { once: true });\n if (options.onCancel) {\n inputRef.current.addEventListener('cancel', options.onCancel, { once: true });\n }\n document.body.appendChild(inputRef.current);\n }\n }, [options, handleChange]);\n\n useIsomorphicEffect(() => {\n createAndSetupInput();\n return () => inputRef.current?.remove();\n }, []);\n\n const reset = useCallback(() => {\n setFiles(null);\n options.onChange?.(null);\n }, [options.onChange]);\n\n const open = useCallback(() => {\n if (options.resetOnOpen) {\n reset();\n }\n\n createAndSetupInput();\n inputRef.current?.click();\n }, [options.resetOnOpen, reset, createAndSetupInput]);\n\n return { files, open, reset };\n}\n\nexport namespace useFileDialog {\n export type Options = UseFileDialogOptions;\n export type ReturnValue = UseFileDialogReturnValue;\n}\n"],"mappings":";;;;AA6BA,MAAM,iBAAuC;CAC3C,UAAU;CACV,QAAQ;CACT;AAED,SAAS,oBAAoB,OAA8D;AACzF,KAAI,CAAC,MACH,QAAO;AAGT,KAAI,iBAAiB,SACnB,QAAO;CAGT,MAAM,SAAS,IAAI,cAAc;AACjC,MAAK,MAAM,QAAQ,MACjB,QAAO,MAAM,I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SAAS,CAAC;AAWtB,QAAO;EAAE;EAAO,MATH,kBAAkB;AAC7B,OAAI,QAAQ,YACV,QAAO;AAGT,wBAAqB;AACrB,YAAS,SAAS,OAAO;KACxB;GAAC,QAAQ;GAAa;GAAO;GAAoB,CAAC;EAE/B;EAAO"}
|
|
@@ -162,7 +162,39 @@ function useFloatingWindow(options = {}) {
|
|
|
162
162
|
}, [options.constrainToViewport, options.constrainOffset]);
|
|
163
163
|
return {
|
|
164
164
|
ref: assignRef,
|
|
165
|
-
setPosition:
|
|
165
|
+
setPosition: useCallback((position) => {
|
|
166
|
+
const el = ref.current;
|
|
167
|
+
if (!el) return;
|
|
168
|
+
const offset = options.constrainOffset ?? 0;
|
|
169
|
+
const rect = el.getBoundingClientRect();
|
|
170
|
+
let x;
|
|
171
|
+
let y;
|
|
172
|
+
if (position.left != null) x = position.left;
|
|
173
|
+
else if (position.right != null) x = window.innerWidth - rect.width - position.right;
|
|
174
|
+
if (position.top != null) y = position.top;
|
|
175
|
+
else if (position.bottom != null) y = window.innerHeight - rect.height - position.bottom;
|
|
176
|
+
x = x ?? pos.current.x;
|
|
177
|
+
y = y ?? pos.current.y;
|
|
178
|
+
if (options.constrainToViewport) {
|
|
179
|
+
const clamped = clampToViewport(x, y, el, offset);
|
|
180
|
+
x = clamped.x;
|
|
181
|
+
y = clamped.y;
|
|
182
|
+
}
|
|
183
|
+
pos.current = {
|
|
184
|
+
x,
|
|
185
|
+
y
|
|
186
|
+
};
|
|
187
|
+
el.style.left = `${x}px`;
|
|
188
|
+
el.style.top = `${y}px`;
|
|
189
|
+
options.onPositionChange?.({
|
|
190
|
+
x,
|
|
191
|
+
y
|
|
192
|
+
});
|
|
193
|
+
}, [
|
|
194
|
+
options.constrainToViewport,
|
|
195
|
+
options.constrainOffset,
|
|
196
|
+
options.onPositionChange
|
|
197
|
+
]),
|
|
166
198
|
isDragging
|
|
167
199
|
};
|
|
168
200
|
}
|
|
@@ -222,41 +254,6 @@ function clampToViewport(x, y, el, offset = 0) {
|
|
|
222
254
|
y: Math.min(Math.max(offset, y), maxY)
|
|
223
255
|
};
|
|
224
256
|
}
|
|
225
|
-
function createSetPosition(elRef, posRef, options) {
|
|
226
|
-
return useCallback((position) => {
|
|
227
|
-
const el = elRef.current;
|
|
228
|
-
if (!el) return;
|
|
229
|
-
const offset = options.constrainOffset ?? 0;
|
|
230
|
-
const rect = el.getBoundingClientRect();
|
|
231
|
-
let x;
|
|
232
|
-
let y;
|
|
233
|
-
if (position.left != null) x = position.left;
|
|
234
|
-
else if (position.right != null) x = window.innerWidth - rect.width - position.right;
|
|
235
|
-
if (position.top != null) y = position.top;
|
|
236
|
-
else if (position.bottom != null) y = window.innerHeight - rect.height - position.bottom;
|
|
237
|
-
x = x ?? posRef.current.x;
|
|
238
|
-
y = y ?? posRef.current.y;
|
|
239
|
-
if (options.constrainToViewport) {
|
|
240
|
-
const clamped = clampToViewport(x, y, el, offset);
|
|
241
|
-
x = clamped.x;
|
|
242
|
-
y = clamped.y;
|
|
243
|
-
}
|
|
244
|
-
posRef.current = {
|
|
245
|
-
x,
|
|
246
|
-
y
|
|
247
|
-
};
|
|
248
|
-
el.style.left = `${x}px`;
|
|
249
|
-
el.style.top = `${y}px`;
|
|
250
|
-
options.onPositionChange?.({
|
|
251
|
-
x,
|
|
252
|
-
y
|
|
253
|
-
});
|
|
254
|
-
}, [
|
|
255
|
-
options.constrainToViewport,
|
|
256
|
-
options.constrainOffset,
|
|
257
|
-
options.onPositionChange
|
|
258
|
-
]);
|
|
259
|
-
}
|
|
260
257
|
//#endregion
|
|
261
258
|
export { useFloatingWindow };
|
|
262
259
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-floating-window.mjs","names":[],"sources":["../../src/use-floating-window/use-floating-window.ts"],"sourcesContent":["// Required to disable for webkit-user-select, although deprecated, it is still required for Safari support\n/* oxlint-disable typescript/no-deprecated */\nimport { RefCallback, RefObject, useCallback, useEffect, useRef, useState } from 'react';\n\nfunction useRefValue<T>(value: T) {\n const ref = useRef(value);\n ref.current = value;\n return ref;\n}\n\ninterface FloatingWindowPositionConfig {\n top?: number;\n left?: number;\n right?: number;\n bottom?: number;\n}\n\ninterface FloatingWindowPosition {\n /** Element offset from the left side of the viewport */\n x: number;\n\n /** Element offset from the top side of the viewport */\n y: number;\n}\n\nexport interface UseFloatingWindowOptions {\n /** If `false`, the element can not be dragged. */\n enabled?: boolean;\n\n /** If `true`, the element can only move within the current viewport boundaries. */\n constrainToViewport?: boolean;\n\n /** The offset from the viewport edges when constraining the element. Requires `constrainToViewport: true`. */\n constrainOffset?: number;\n\n /** Selector of an element that should be used to drag floating window. If not specified, the entire root element is used as a drag target. */\n dragHandleSelector?: string;\n\n /** Selector of an element within `dragHandleSelector` that should be excluded from the drag event. */\n excludeDragHandleSelector?: string;\n\n /** If set, restricts movement to the specified axis */\n axis?: 'x' | 'y';\n\n /** Initial position. If not set, calculated from element styles. */\n initialPosition?: FloatingWindowPositionConfig;\n\n /** Called when the element position changes */\n onPositionChange?: (pos: FloatingWindowPosition) => void;\n\n /** Called when the drag starts */\n onDragStart?: () => void;\n\n /** Called when the drag stops */\n onDragEnd?: () => void;\n}\n\nexport type SetFloatingWindowPosition = (position: FloatingWindowPositionConfig) => void;\n\nexport interface UseFloatingWindowReturnValue<T extends HTMLElement> {\n /** Ref to the element that should be draggable */\n ref: RefCallback<T | null>;\n\n /** Function to set the position of the element */\n setPosition: SetFloatingWindowPosition;\n\n /** `true` if the element is currently being dragged */\n isDragging: boolean;\n}\n\nexport function useFloatingWindow<T extends HTMLElement>(\n options: UseFloatingWindowOptions = {}\n): UseFloatingWindowReturnValue<T> {\n const [element, setElement] = useState<T | null>(null);\n const ref = useRef<T>(null);\n const pos = useRef({ x: 0, y: 0 });\n const offset = useRef({ x: 0, y: 0 });\n const [isDragging, setIsDragging] = useState(false);\n const isDraggingRef = useRef(false);\n const initialized = useRef(false);\n const enabledRef = useRefValue(options.enabled);\n\n const setDragging = useCallback((value: boolean) => {\n setIsDragging(value);\n isDraggingRef.current = value;\n }, []);\n\n const assignRef = useCallback((node: T | null) => {\n if (node) {\n ref.current = node;\n setElement(node);\n } else {\n ref.current = null;\n setElement(null);\n }\n }, []);\n\n useEffect(() => {\n const el = ref.current;\n if (!initialized.current && el) {\n initialized.current = true;\n pos.current = calculateInitialPosition(el, options);\n el.style.left = `${pos.current.x}px`;\n el.style.top = `${pos.current.y}px`;\n el.style.right = 'unset';\n el.style.bottom = 'unset';\n }\n\n return () => {\n initialized.current = false;\n };\n }, [\n element,\n options.constrainOffset,\n options.initialPosition?.top,\n options.initialPosition?.left,\n options.initialPosition?.right,\n options.initialPosition?.bottom,\n options.constrainToViewport,\n ]);\n\n useEffect(() => {\n const el = ref.current;\n if (!el) {\n return;\n }\n\n const controller = new AbortController();\n const signal = controller.signal;\n\n const onStart = (e: MouseEvent | TouchEvent) => {\n if (enabledRef.current === false) {\n return;\n }\n\n const point = 'touches' in e ? e.touches[0] : e;\n\n if ('button' in e && e.button !== 0) {\n return;\n }\n\n if (!getHandle(el, e.target, options)) {\n return;\n }\n\n setDragging(true);\n document.body.style.userSelect = 'none';\n document.body.style.webkitUserSelect = 'none';\n\n const rect = el.getBoundingClientRect();\n\n offset.current = {\n x: point.clientX - rect.left,\n y: point.clientY - rect.top,\n };\n\n options.onDragStart?.();\n\n document.addEventListener('mousemove', onMove, { signal });\n document.addEventListener('mouseup', onEnd, { signal });\n document.addEventListener('touchmove', onMove, { signal, passive: false });\n document.addEventListener('touchend', onEnd, { signal });\n };\n\n const onMove = (e: MouseEvent | TouchEvent) => {\n if (!isDraggingRef.current) {\n return;\n }\n\n const point = 'touches' in e ? e.touches[0] : e;\n e.preventDefault();\n\n let x = point.clientX - offset.current.x;\n let y = point.clientY - offset.current.y;\n\n const constrained = getConstrainedPosition(el, { x, y }, options);\n if (options.axis === 'x') {\n x = constrained.x;\n y = pos.current.y;\n } else if (options.axis === 'y') {\n x = pos.current.x;\n y = constrained.y;\n } else {\n x = constrained.x;\n y = constrained.y;\n }\n\n pos.current = { x, y };\n\n if (ref.current) {\n ref.current.style.left = `${x}px`;\n ref.current.style.top = `${y}px`;\n }\n\n options.onPositionChange?.({ x, y });\n };\n\n const onEnd = () => {\n if (isDraggingRef.current) {\n setDragging(false);\n document.body.style.userSelect = '';\n document.body.style.webkitUserSelect = '';\n options.onDragEnd?.();\n }\n };\n\n el.addEventListener('mousedown', onStart, { signal });\n el.addEventListener('touchstart', onStart, { signal, passive: false });\n\n return () => {\n controller.abort();\n };\n }, [\n options.constrainToViewport,\n options.constrainOffset,\n options.dragHandleSelector,\n options.axis,\n options.onPositionChange,\n options.onDragStart,\n options.onDragEnd,\n options.initialPosition?.top,\n options.initialPosition?.left,\n options.initialPosition?.right,\n options.initialPosition?.bottom,\n element,\n ]);\n\n useEffect(() => {\n const el = ref.current;\n if (!el) {\n return;\n }\n\n const observer = new ResizeObserver(() => {\n // Re-clamp current position if element size changes\n const constrained = getConstrainedPosition(el, pos.current, options);\n pos.current = constrained;\n el.style.left = `${constrained.x}px`;\n el.style.top = `${constrained.y}px`;\n });\n\n observer.observe(el);\n\n return () => {\n observer.disconnect();\n };\n }, [options.constrainToViewport, options.constrainOffset]);\n\n return {\n ref: assignRef,\n setPosition: createSetPosition(ref, pos, options),\n isDragging,\n };\n}\n\n// -------------------------------------------------------\n// Helper functions\n// -------------------------------------------------------\n\nfunction px(v: string) {\n return v.endsWith('px') ? parseFloat(v) : 0;\n}\n\nfunction calculateInitialPosition(\n el: HTMLElement,\n options: UseFloatingWindowOptions\n): { x: number; y: number } {\n const rect = el.getBoundingClientRect();\n const offset = options.constrainOffset ?? 0;\n const winW = window.innerWidth;\n const winH = window.innerHeight;\n const style = window.getComputedStyle(el);\n const top = options.initialPosition?.top;\n const left = options.initialPosition?.left;\n const right = options.initialPosition?.right;\n const bottom = options.initialPosition?.bottom;\n\n let x = offset;\n let y = offset;\n\n if (left != null) {\n x = left;\n } else if (right != null) {\n x = winW - rect.width - right;\n } else {\n x = px(style.left) || winW - rect.width - px(style.right) || offset;\n }\n\n if (top != null) {\n y = top;\n } else if (bottom != null) {\n y = winH - rect.height - bottom;\n } else {\n y = px(style.top) || winH - rect.height - px(style.bottom) || offset;\n }\n\n return options.constrainToViewport\n ? clampToViewport(x, y, el, options.constrainOffset)\n : { x, y };\n}\n\nfunction getConstrainedPosition(\n el: HTMLElement,\n pos: FloatingWindowPosition,\n options: UseFloatingWindowOptions\n) {\n if (!options.constrainToViewport || !el) {\n return pos;\n }\n\n const rect = el.getBoundingClientRect();\n const offset = options.constrainOffset ?? 0;\n const maxX = window.innerWidth - rect.width - offset;\n const maxY = window.innerHeight - rect.height - offset;\n\n return {\n x: Math.min(Math.max(offset, pos.x), maxX),\n y: Math.min(Math.max(offset, pos.y), maxY),\n };\n}\n\nfunction matchesExcludeSelector(target: Node, excludeSelector?: string): boolean {\n if (!excludeSelector) {\n return false;\n }\n if (!(target instanceof Element)) {\n return false;\n }\n\n return Boolean(target.closest(excludeSelector));\n}\n\nfunction getHandle(\n el: HTMLElement,\n target: EventTarget | null,\n options: UseFloatingWindowOptions\n): boolean {\n if (!(target instanceof Node)) {\n return false;\n }\n\n // If no drag handle selector, allow dragging from entire element\n if (!options.dragHandleSelector) {\n return !matchesExcludeSelector(target, options.excludeDragHandleSelector);\n }\n\n const handles = Array.from(el.querySelectorAll(options.dragHandleSelector));\n return handles.some(\n (handle) =>\n handle.contains(target) && !matchesExcludeSelector(target, options.excludeDragHandleSelector)\n );\n}\n\nfunction clampToViewport(\n x: number,\n y: number,\n el: HTMLElement,\n offset: number = 0\n): { x: number; y: number } {\n const rect = el.getBoundingClientRect();\n const maxX = window.innerWidth - rect.width - offset;\n const maxY = window.innerHeight - rect.height - offset;\n\n return {\n x: Math.min(Math.max(offset, x), maxX),\n y: Math.min(Math.max(offset, y), maxY),\n };\n}\n\nfunction createSetPosition(\n elRef: RefObject<HTMLElement | null>,\n posRef: React.RefObject<{ x: number; y: number }>,\n options: UseFloatingWindowOptions\n) {\n return useCallback(\n (position: FloatingWindowPositionConfig) => {\n const el = elRef.current;\n if (!el) {\n return;\n }\n\n const offset = options.constrainOffset ?? 0;\n const rect = el.getBoundingClientRect();\n\n let x: number | undefined;\n let y: number | undefined;\n\n if (position.left != null) {\n x = position.left;\n } else if (position.right != null) {\n x = window.innerWidth - rect.width - position.right;\n }\n\n if (position.top != null) {\n y = position.top;\n } else if (position.bottom != null) {\n y = window.innerHeight - rect.height - position.bottom;\n }\n\n x = x ?? posRef.current.x;\n y = y ?? posRef.current.y;\n\n if (options.constrainToViewport) {\n const clamped = clampToViewport(x, y, el, offset);\n x = clamped.x;\n y = clamped.y;\n }\n\n posRef.current = { x, y };\n el.style.left = `${x}px`;\n el.style.top = `${y}px`;\n options.onPositionChange?.({ x, y });\n },\n [options.constrainToViewport, options.constrainOffset, options.onPositionChange]\n );\n}\n\nexport namespace useFloatingWindow {\n export type Options = UseFloatingWindowOptions;\n export type Position = FloatingWindowPosition;\n export type SetPosition = SetFloatingWindowPosition;\n export type ReturnValue<T extends HTMLElement> = UseFloatingWindowReturnValue<T>;\n}\n"],"mappings":";;;AAIA,SAAS,YAAe,OAAU;CAChC,MAAM,MAAM,OAAO,MAAM;AACzB,KAAI,UAAU;AACd,QAAO;;AA+DT,SAAgB,kBACd,UAAoC,EAAE,EACL;CACjC,MAAM,CAAC,SAAS,cAAc,SAAmB,KAAK;CACtD,MAAM,MAAM,OAAU,KAAK;CAC3B,MAAM,MAAM,OAAO;EAAE,GAAG;EAAG,GAAG;EAAG,CAAC;CAClC,MAAM,SAAS,OAAO;EAAE,GAAG;EAAG,GAAG;EAAG,CAAC;CACrC,MAAM,CAAC,YAAY,iBAAiB,SAAS,MAAM;CACnD,MAAM,gBAAgB,OAAO,MAAM;CACnC,MAAM,cAAc,OAAO,MAAM;CACjC,MAAM,aAAa,YAAY,QAAQ,QAAQ;CAE/C,MAAM,cAAc,aAAa,UAAmB;AAClD,gBAAc,MAAM;AACpB,gBAAc,UAAU;IACvB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AAGH,SAAS,kBACP,OACA,QACA,SACA;AACA,QAAO,aACJ,aAA2C;EAC1C,MAAM,KAAK,MAAM;AACjB,MAAI,CAAC,GACH;EAGF,MAAM,SAAS,QAAQ,mBAAmB;EAC1C,MAAM,OAAO,GAAG,uBAAuB;EAEvC,IAAI;EACJ,IAAI;AAEJ,MAAI,SAAS,QAAQ,KACnB,KAAI,SAAS;WACJ,SAAS,SAAS,KAC3B,KAAI,OAAO,aAAa,KAAK,QAAQ,SAAS;AAGhD,MAAI,SAAS,OAAO,KAClB,KAAI,SAAS;WACJ,SAAS,UAAU,KAC5B,KAAI,OAAO,cAAc,KAAK,SAAS,SAAS;AAGlD,MAAI,KAAK,OAAO,QAAQ;AACxB,MAAI,KAAK,OAAO,QAAQ;AAExB,MAAI,QAAQ,qBAAqB;GAC/B,MAAM,UAAU,gBAAgB,GAAG,GAAG,IAAI,OAAO;AACjD,OAAI,QAAQ;AACZ,OAAI,QAAQ;;AAGd,SAAO,UAAU;GAAE;GAAG;GAAG;AACzB,KAAG,MAAM,OAAO,GAAG,EAAE;AACrB,KAAG,MAAM,MAAM,GAAG,EAAE;AACpB,UAAQ,mBAAmB;GAAE;GAAG;GAAG,CAAC;IAEtC;EAAC,QAAQ;EAAqB,QAAQ;EAAiB,QAAQ;EAAiB,CACjF"}
|
|
1
|
+
{"version":3,"file":"use-floating-window.mjs","names":[],"sources":["../../src/use-floating-window/use-floating-window.ts"],"sourcesContent":["// Required to disable for webkit-user-select, although deprecated, it is still required for Safari support\nimport { RefCallback, useCallback, useEffect, useRef, useState } from 'react';\n\nfunction useRefValue<T>(value: T) {\n const ref = useRef(value);\n ref.current = value;\n return ref;\n}\n\ninterface FloatingWindowPositionConfig {\n top?: number;\n left?: number;\n right?: number;\n bottom?: number;\n}\n\ninterface FloatingWindowPosition {\n /** Element offset from the left side of the viewport */\n x: number;\n\n /** Element offset from the top side of the viewport */\n y: number;\n}\n\nexport interface UseFloatingWindowOptions {\n /** If `false`, the element can not be dragged. */\n enabled?: boolean;\n\n /** If `true`, the element can only move within the current viewport boundaries. */\n constrainToViewport?: boolean;\n\n /** The offset from the viewport edges when constraining the element. Requires `constrainToViewport: true`. */\n constrainOffset?: number;\n\n /** Selector of an element that should be used to drag floating window. If not specified, the entire root element is used as a drag target. */\n dragHandleSelector?: string;\n\n /** Selector of an element within `dragHandleSelector` that should be excluded from the drag event. */\n excludeDragHandleSelector?: string;\n\n /** If set, restricts movement to the specified axis */\n axis?: 'x' | 'y';\n\n /** Initial position. If not set, calculated from element styles. */\n initialPosition?: FloatingWindowPositionConfig;\n\n /** Called when the element position changes */\n onPositionChange?: (pos: FloatingWindowPosition) => void;\n\n /** Called when the drag starts */\n onDragStart?: () => void;\n\n /** Called when the drag stops */\n onDragEnd?: () => void;\n}\n\nexport type SetFloatingWindowPosition = (position: FloatingWindowPositionConfig) => void;\n\nexport interface UseFloatingWindowReturnValue<T extends HTMLElement> {\n /** Ref to the element that should be draggable */\n ref: RefCallback<T | null>;\n\n /** Function to set the position of the element */\n setPosition: SetFloatingWindowPosition;\n\n /** `true` if the element is currently being dragged */\n isDragging: boolean;\n}\n\nexport function useFloatingWindow<T extends HTMLElement>(\n options: UseFloatingWindowOptions = {}\n): UseFloatingWindowReturnValue<T> {\n const [element, setElement] = useState<T | null>(null);\n const ref = useRef<T>(null);\n const pos = useRef({ x: 0, y: 0 });\n const offset = useRef({ x: 0, y: 0 });\n const [isDragging, setIsDragging] = useState(false);\n const isDraggingRef = useRef(false);\n const initialized = useRef(false);\n const enabledRef = useRefValue(options.enabled);\n\n const setDragging = useCallback((value: boolean) => {\n setIsDragging(value);\n isDraggingRef.current = value;\n }, []);\n\n const assignRef = useCallback((node: T | null) => {\n if (node) {\n ref.current = node;\n setElement(node);\n } else {\n ref.current = null;\n setElement(null);\n }\n }, []);\n\n useEffect(() => {\n const el = ref.current;\n if (!initialized.current && el) {\n initialized.current = true;\n pos.current = calculateInitialPosition(el, options);\n el.style.left = `${pos.current.x}px`;\n el.style.top = `${pos.current.y}px`;\n el.style.right = 'unset';\n el.style.bottom = 'unset';\n }\n\n return () => {\n initialized.current = false;\n };\n }, [\n element,\n options.constrainOffset,\n options.initialPosition?.top,\n options.initialPosition?.left,\n options.initialPosition?.right,\n options.initialPosition?.bottom,\n options.constrainToViewport,\n ]);\n\n useEffect(() => {\n const el = ref.current;\n if (!el) {\n return;\n }\n\n const controller = new AbortController();\n const signal = controller.signal;\n\n const onStart = (e: MouseEvent | TouchEvent) => {\n if (enabledRef.current === false) {\n return;\n }\n\n const point = 'touches' in e ? e.touches[0] : e;\n\n if ('button' in e && e.button !== 0) {\n return;\n }\n\n if (!getHandle(el, e.target, options)) {\n return;\n }\n\n setDragging(true);\n document.body.style.userSelect = 'none';\n document.body.style.webkitUserSelect = 'none';\n\n const rect = el.getBoundingClientRect();\n\n offset.current = {\n x: point.clientX - rect.left,\n y: point.clientY - rect.top,\n };\n\n options.onDragStart?.();\n\n document.addEventListener('mousemove', onMove, { signal });\n document.addEventListener('mouseup', onEnd, { signal });\n document.addEventListener('touchmove', onMove, { signal, passive: false });\n document.addEventListener('touchend', onEnd, { signal });\n };\n\n const onMove = (e: MouseEvent | TouchEvent) => {\n if (!isDraggingRef.current) {\n return;\n }\n\n const point = 'touches' in e ? e.touches[0] : e;\n e.preventDefault();\n\n let x = point.clientX - offset.current.x;\n let y = point.clientY - offset.current.y;\n\n const constrained = getConstrainedPosition(el, { x, y }, options);\n if (options.axis === 'x') {\n x = constrained.x;\n y = pos.current.y;\n } else if (options.axis === 'y') {\n x = pos.current.x;\n y = constrained.y;\n } else {\n x = constrained.x;\n y = constrained.y;\n }\n\n pos.current = { x, y };\n\n if (ref.current) {\n ref.current.style.left = `${x}px`;\n ref.current.style.top = `${y}px`;\n }\n\n options.onPositionChange?.({ x, y });\n };\n\n const onEnd = () => {\n if (isDraggingRef.current) {\n setDragging(false);\n document.body.style.userSelect = '';\n document.body.style.webkitUserSelect = '';\n options.onDragEnd?.();\n }\n };\n\n el.addEventListener('mousedown', onStart, { signal });\n el.addEventListener('touchstart', onStart, { signal, passive: false });\n\n return () => {\n controller.abort();\n };\n }, [\n options.constrainToViewport,\n options.constrainOffset,\n options.dragHandleSelector,\n options.axis,\n options.onPositionChange,\n options.onDragStart,\n options.onDragEnd,\n options.initialPosition?.top,\n options.initialPosition?.left,\n options.initialPosition?.right,\n options.initialPosition?.bottom,\n element,\n ]);\n\n useEffect(() => {\n const el = ref.current;\n if (!el) {\n return;\n }\n\n const observer = new ResizeObserver(() => {\n // Re-clamp current position if element size changes\n const constrained = getConstrainedPosition(el, pos.current, options);\n pos.current = constrained;\n el.style.left = `${constrained.x}px`;\n el.style.top = `${constrained.y}px`;\n });\n\n observer.observe(el);\n\n return () => {\n observer.disconnect();\n };\n }, [options.constrainToViewport, options.constrainOffset]);\n\n const setPosition = useCallback(\n (position: FloatingWindowPositionConfig) => {\n const el = ref.current;\n if (!el) {\n return;\n }\n\n const offset = options.constrainOffset ?? 0;\n const rect = el.getBoundingClientRect();\n\n let x: number | undefined;\n let y: number | undefined;\n\n if (position.left != null) {\n x = position.left;\n } else if (position.right != null) {\n x = window.innerWidth - rect.width - position.right;\n }\n\n if (position.top != null) {\n y = position.top;\n } else if (position.bottom != null) {\n y = window.innerHeight - rect.height - position.bottom;\n }\n\n x = x ?? pos.current.x;\n y = y ?? pos.current.y;\n\n if (options.constrainToViewport) {\n const clamped = clampToViewport(x, y, el, offset);\n x = clamped.x;\n y = clamped.y;\n }\n\n pos.current = { x, y };\n el.style.left = `${x}px`;\n el.style.top = `${y}px`;\n options.onPositionChange?.({ x, y });\n },\n [options.constrainToViewport, options.constrainOffset, options.onPositionChange]\n );\n\n return {\n ref: assignRef,\n setPosition,\n isDragging,\n };\n}\n\n// -------------------------------------------------------\n// Helper functions\n// -------------------------------------------------------\n\nfunction px(v: string) {\n return v.endsWith('px') ? parseFloat(v) : 0;\n}\n\nfunction calculateInitialPosition(\n el: HTMLElement,\n options: UseFloatingWindowOptions\n): { x: number; y: number } {\n const rect = el.getBoundingClientRect();\n const offset = options.constrainOffset ?? 0;\n const winW = window.innerWidth;\n const winH = window.innerHeight;\n const style = window.getComputedStyle(el);\n const top = options.initialPosition?.top;\n const left = options.initialPosition?.left;\n const right = options.initialPosition?.right;\n const bottom = options.initialPosition?.bottom;\n\n let x = offset;\n let y = offset;\n\n if (left != null) {\n x = left;\n } else if (right != null) {\n x = winW - rect.width - right;\n } else {\n x = px(style.left) || winW - rect.width - px(style.right) || offset;\n }\n\n if (top != null) {\n y = top;\n } else if (bottom != null) {\n y = winH - rect.height - bottom;\n } else {\n y = px(style.top) || winH - rect.height - px(style.bottom) || offset;\n }\n\n return options.constrainToViewport\n ? clampToViewport(x, y, el, options.constrainOffset)\n : { x, y };\n}\n\nfunction getConstrainedPosition(\n el: HTMLElement,\n pos: FloatingWindowPosition,\n options: UseFloatingWindowOptions\n) {\n if (!options.constrainToViewport || !el) {\n return pos;\n }\n\n const rect = el.getBoundingClientRect();\n const offset = options.constrainOffset ?? 0;\n const maxX = window.innerWidth - rect.width - offset;\n const maxY = window.innerHeight - rect.height - offset;\n\n return {\n x: Math.min(Math.max(offset, pos.x), maxX),\n y: Math.min(Math.max(offset, pos.y), maxY),\n };\n}\n\nfunction matchesExcludeSelector(target: Node, excludeSelector?: string): boolean {\n if (!excludeSelector) {\n return false;\n }\n if (!(target instanceof Element)) {\n return false;\n }\n\n return Boolean(target.closest(excludeSelector));\n}\n\nfunction getHandle(\n el: HTMLElement,\n target: EventTarget | null,\n options: UseFloatingWindowOptions\n): boolean {\n if (!(target instanceof Node)) {\n return false;\n }\n\n // If no drag handle selector, allow dragging from entire element\n if (!options.dragHandleSelector) {\n return !matchesExcludeSelector(target, options.excludeDragHandleSelector);\n }\n\n const handles = Array.from(el.querySelectorAll(options.dragHandleSelector));\n return handles.some(\n (handle) =>\n handle.contains(target) && !matchesExcludeSelector(target, options.excludeDragHandleSelector)\n );\n}\n\nfunction clampToViewport(\n x: number,\n y: number,\n el: HTMLElement,\n offset: number = 0\n): { x: number; y: number } {\n const rect = el.getBoundingClientRect();\n const maxX = window.innerWidth - rect.width - offset;\n const maxY = window.innerHeight - rect.height - offset;\n\n return {\n x: Math.min(Math.max(offset, x), maxX),\n y: Math.min(Math.max(offset, y), maxY),\n };\n}\n\nexport namespace useFloatingWindow {\n export type Options = UseFloatingWindowOptions;\n export type Position = FloatingWindowPosition;\n export type SetPosition = SetFloatingWindowPosition;\n export type ReturnValue<T extends HTMLElement> = UseFloatingWindowReturnValue<T>;\n}\n"],"mappings":";;;AAGA,SAAS,YAAe,OAAU;CAChC,MAAM,MAAM,OAAO,MAAM;AACzB,KAAI,UAAU;AACd,QAAO;;AA+DT,SAAgB,kBACd,UAAoC,EAAE,EACL;CACjC,MAAM,CAAC,SAAS,cAAc,SAAmB,KAAK;CACtD,MAAM,MAAM,OAAU,KAAK;CAC3B,MAAM,MAAM,OAAO;EAAE,GAAG;EAAG,GAAG;EAAG,CAAC;CAClC,MAAM,SAAS,OAAO;EAAE,GAAG;EAAG,GAAG;EAAG,CAAC;CACrC,MAAM,CAAC,YAAY,iBAAiB,SAAS,MAAM;CACnD,MAAM,gBAAgB,OAAO,MAAM;CACnC,MAAM,cAAc,OAAO,MAAM;CACjC,MAAM,aAAa,YAAY,QAAQ,QAAQ;CAE/C,MAAM,cAAc,aAAa,UAAmB;AAClD,gBAAc,MAAM;AACpB,gBAAc,UAAU;IACvB,EAAE,CAAC;CAEN,MAAM,YAAY,aAAa,SAAmB;AAChD,MAAI,MAAM;AACR,OAAI,UAAU;AACd,cAAW,KAAK;SACX;AACL,OAAI,UAAU;AACd,cAAW,KAAK;;IAEjB,EAAE,CAAC;AAEN,iBAAgB;EACd,MAAM,KAAK,IAAI;AACf,MAAI,CAAC,YAAY,WAAW,IAAI;AAC9B,eAAY,UAAU;AACtB,OAAI,UAAU,yBAAyB,IAAI,QAAQ;AACnD,MAAG,MAAM,OAAO,GAAG,IAAI,QAAQ,EAAE;AACjC,MAAG,MAAM,MAAM,GAA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,OAAO,SAAS,OAAO,IAAI,CAAC,uBAAuB,QAAQ,QAAQ,0BAA0B,CAChG;;AAGH,SAAS,gBACP,GACA,GACA,IACA,SAAiB,GACS;CAC1B,MAAM,OAAO,GAAG,uBAAuB;CACvC,MAAM,OAAO,OAAO,aAAa,KAAK,QAAQ;CAC9C,MAAM,OAAO,OAAO,cAAc,KAAK,SAAS;AAEhD,QAAO;EACL,GAAG,KAAK,IAAI,KAAK,IAAI,QAAQ,EAAE,EAAE,KAAK;EACtC,GAAG,KAAK,IAAI,KAAK,IAAI,QAAQ,EAAE,EAAE,KAAK;EACvC"}
|
|
@@ -17,19 +17,22 @@ function useFocusTrap(active = true) {
|
|
|
17
17
|
};
|
|
18
18
|
const setRef = useCallback((node) => {
|
|
19
19
|
if (!active) return;
|
|
20
|
-
if (node === null)
|
|
20
|
+
if (node === null) {
|
|
21
|
+
ref.current = null;
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
21
24
|
if (ref.current === node) return;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
ref.current = node;
|
|
28
|
-
} else ref.current = null;
|
|
25
|
+
setTimeout(() => {
|
|
26
|
+
if (node.getRootNode()) focusNode(node);
|
|
27
|
+
else console.warn("[@mantine/hooks/use-focus-trap] Ref node is not part of the dom", node);
|
|
28
|
+
});
|
|
29
|
+
ref.current = node;
|
|
29
30
|
}, [active]);
|
|
30
31
|
useEffect(() => {
|
|
31
32
|
if (!active) return;
|
|
32
|
-
ref.current
|
|
33
|
+
if (ref.current) setTimeout(() => {
|
|
34
|
+
if (ref.current) focusNode(ref.current);
|
|
35
|
+
});
|
|
33
36
|
const handleKeyDown = (event) => {
|
|
34
37
|
if (event.key === "Tab" && ref.current) scopeTab(ref.current, event);
|
|
35
38
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-focus-trap.mjs","names":[],"sources":["../../src/use-focus-trap/use-focus-trap.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { scopeTab } from './scope-tab';\nimport { FOCUS_SELECTOR, focusable, tabbable } from './tabbable';\n\nexport function useFocusTrap(active = true): React.RefCallback<HTMLElement | null> {\n const ref = useRef<HTMLElement>(null);\n\n const focusNode = (node: HTMLElement) => {\n let focusElement: HTMLElement | null = node.querySelector('[data-autofocus]');\n\n if (!focusElement) {\n const children = Array.from<HTMLElement>(node.querySelectorAll(FOCUS_SELECTOR));\n focusElement = children.find(tabbable) || children.find(focusable) || null;\n if (!focusElement && focusable(node)) {\n focusElement = node;\n }\n }\n\n if (focusElement) {\n focusElement.focus({ preventScroll: true });\n } else if (process.env.NODE_ENV === 'development') {\n // oxlint-disable-next-line no-console\n console.warn(\n '[@mantine/hooks/use-focus-trap] Failed to find focusable element within provided node',\n node\n );\n }\n };\n\n const setRef = useCallback(\n (node: HTMLElement | null) => {\n if (!active) {\n return;\n }\n\n if (node === null) {\n return;\n }\n\n if (ref.current === node) {\n return;\n }\n\n
|
|
1
|
+
{"version":3,"file":"use-focus-trap.mjs","names":[],"sources":["../../src/use-focus-trap/use-focus-trap.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { scopeTab } from './scope-tab';\nimport { FOCUS_SELECTOR, focusable, tabbable } from './tabbable';\n\nexport function useFocusTrap(active = true): React.RefCallback<HTMLElement | null> {\n const ref = useRef<HTMLElement>(null);\n\n const focusNode = (node: HTMLElement) => {\n let focusElement: HTMLElement | null = node.querySelector('[data-autofocus]');\n\n if (!focusElement) {\n const children = Array.from<HTMLElement>(node.querySelectorAll(FOCUS_SELECTOR));\n focusElement = children.find(tabbable) || children.find(focusable) || null;\n if (!focusElement && focusable(node)) {\n focusElement = node;\n }\n }\n\n if (focusElement) {\n focusElement.focus({ preventScroll: true });\n } else if (process.env.NODE_ENV === 'development') {\n // oxlint-disable-next-line no-console\n console.warn(\n '[@mantine/hooks/use-focus-trap] Failed to find focusable element within provided node',\n node\n );\n }\n };\n\n const setRef = useCallback(\n (node: HTMLElement | null) => {\n if (!active) {\n return;\n }\n\n if (node === null) {\n ref.current = null;\n return;\n }\n\n if (ref.current === node) {\n return;\n }\n\n // Delay processing the HTML node by a frame. This ensures focus is assigned correctly.\n setTimeout(() => {\n if (node.getRootNode()) {\n focusNode(node);\n } else if (process.env.NODE_ENV === 'development') {\n // oxlint-disable-next-line no-console\n console.warn('[@mantine/hooks/use-focus-trap] Ref node is not part of the dom', node);\n }\n });\n\n ref.current = node;\n },\n [active]\n );\n\n useEffect(() => {\n if (!active) {\n return undefined;\n }\n\n if (ref.current) {\n setTimeout(() => {\n if (ref.current) {\n focusNode(ref.current);\n }\n });\n }\n\n const handleKeyDown = (event: KeyboardEvent) => {\n if (event.key === 'Tab' && ref.current) {\n scopeTab(ref.current, event);\n }\n };\n\n document.addEventListener('keydown', handleKeyDown);\n return () => document.removeEventListener('keydown', handleKeyDown);\n }, [active]);\n\n return setRef;\n}\n"],"mappings":";;;;;AAIA,SAAgB,aAAa,SAAS,MAA6C;CACjF,MAAM,MAAM,OAAoB,KAAK;CAErC,MAAM,aAAa,SAAsB;EACvC,IAAI,eAAmC,KAAK,cAAc,mBAAmB;AAE7E,MAAI,CAAC,cAAc;GACjB,MAAM,WAAW,MAAM,KAAkB,KAAK,iBAAiB,eAAe,CAAC;AAC/E,kBAAe,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,UAAU,IAAI;AACtE,OAAI,CAAC,gBAAgB,UAAU,KAAK,CAClC,gBAAe;;AAInB,MAAI,aACF,cAAa,MAAM,EAAE,eAAe,MAAM,CAAC;MAG3C,SAAQ,KACN,yFACA,KACD;;CAIL,MAAM,SAAS,aACZ,SAA6B;AAC5B,MAAI,CAAC,OACH;AAGF,MAAI,SAAS,MAAM;AACjB,OAAI,UAAU;AACd;;AAGF,MAAI,IAAI,YAAY,KAClB;AAIF,mBAAiB;AACf,OAAI,KAAK,aAAa,CACpB,WAAU,KAAK;OAGf,SAAQ,KAAK,mEAAmE,KAAK;IAEvF;AAEF,MAAI,UAAU;IAEhB,CAAC,OAAO,CACT;AAED,iBAAgB;AACd,MAAI,CAAC,OACH;AAGF,MAAI,IAAI,QACN,kBAAiB;AACf,OAAI,IAAI,QACN,WAAU,IAAI,QAAQ;IAExB;EAGJ,MAAM,iBAAiB,UAAyB;AAC9C,OAAI,MAAM,QAAQ,SAAS,IAAI,QAC7B,UAAS,IAAI,SAAS,MAAM;;AAIhC,WAAS,iBAAiB,WAAW,cAAc;AACnD,eAAa,SAAS,oBAAoB,WAAW,cAAc;IAClE,CAAC,OAAO,CAAC;AAEZ,QAAO"}
|
|
@@ -23,11 +23,13 @@ const prefixes = [
|
|
|
23
23
|
"moz",
|
|
24
24
|
"ms"
|
|
25
25
|
];
|
|
26
|
-
function addEvents(element,
|
|
26
|
+
function addEvents(element, events) {
|
|
27
|
+
const { onFullScreen, onError } = events;
|
|
27
28
|
prefixes.forEach((prefix) => {
|
|
28
29
|
element.addEventListener(`${prefix}fullscreenchange`, onFullScreen);
|
|
29
30
|
element.addEventListener(`${prefix}fullscreenerror`, onError);
|
|
30
31
|
});
|
|
32
|
+
return () => removeEvents(element, events);
|
|
31
33
|
}
|
|
32
34
|
function removeEvents(element, { onFullScreen, onError }) {
|
|
33
35
|
prefixes.forEach((prefix) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-fullscreen.mjs","names":[],"sources":["../../src/use-fullscreen/use-fullscreen.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nfunction getFullscreenElement(): HTMLElement | null {\n const _document = window.document as any;\n\n const fullscreenElement =\n _document.fullscreenElement ||\n _document.webkitFullscreenElement ||\n _document.mozFullScreenElement ||\n _document.msFullscreenElement;\n\n return fullscreenElement;\n}\n\nfunction exitFullscreen() {\n const _document = window.document as any;\n\n if (typeof _document.exitFullscreen === 'function') {\n return _document.exitFullscreen();\n }\n if (typeof _document.msExitFullscreen === 'function') {\n return _document.msExitFullscreen();\n }\n if (typeof _document.webkitExitFullscreen === 'function') {\n return _document.webkitExitFullscreen();\n }\n if (typeof _document.mozCancelFullScreen === 'function') {\n return _document.mozCancelFullScreen();\n }\n\n return null;\n}\n\nfunction enterFullScreen(element: HTMLElement) {\n const _element = element as any;\n\n return (\n _element.requestFullscreen?.() ||\n _element.msRequestFullscreen?.() ||\n _element.webkitEnterFullscreen?.() ||\n _element.webkitRequestFullscreen?.() ||\n _element.mozRequestFullscreen?.()\n );\n}\n\nconst prefixes = ['', 'webkit', 'moz', 'ms'];\n\ninterface FullscreenEvents {\n onFullScreen: (event: Event) => void;\n onError: (event: Event) => void;\n}\n\nfunction addEvents(element: HTMLElement, { onFullScreen, onError }
|
|
1
|
+
{"version":3,"file":"use-fullscreen.mjs","names":[],"sources":["../../src/use-fullscreen/use-fullscreen.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nfunction getFullscreenElement(): HTMLElement | null {\n const _document = window.document as any;\n\n const fullscreenElement =\n _document.fullscreenElement ||\n _document.webkitFullscreenElement ||\n _document.mozFullScreenElement ||\n _document.msFullscreenElement;\n\n return fullscreenElement;\n}\n\nfunction exitFullscreen() {\n const _document = window.document as any;\n\n if (typeof _document.exitFullscreen === 'function') {\n return _document.exitFullscreen();\n }\n if (typeof _document.msExitFullscreen === 'function') {\n return _document.msExitFullscreen();\n }\n if (typeof _document.webkitExitFullscreen === 'function') {\n return _document.webkitExitFullscreen();\n }\n if (typeof _document.mozCancelFullScreen === 'function') {\n return _document.mozCancelFullScreen();\n }\n\n return null;\n}\n\nfunction enterFullScreen(element: HTMLElement) {\n const _element = element as any;\n\n return (\n _element.requestFullscreen?.() ||\n _element.msRequestFullscreen?.() ||\n _element.webkitEnterFullscreen?.() ||\n _element.webkitRequestFullscreen?.() ||\n _element.mozRequestFullscreen?.()\n );\n}\n\nconst prefixes = ['', 'webkit', 'moz', 'ms'];\n\ninterface FullscreenEvents {\n onFullScreen: (event: Event) => void;\n onError: (event: Event) => void;\n}\n\nfunction addEvents(element: HTMLElement, events: FullscreenEvents) {\n const { onFullScreen, onError } = events;\n prefixes.forEach((prefix) => {\n element.addEventListener(`${prefix}fullscreenchange`, onFullScreen);\n element.addEventListener(`${prefix}fullscreenerror`, onError);\n });\n\n return () => removeEvents(element, events);\n}\n\nfunction removeEvents(element: HTMLElement, { onFullScreen, onError }: FullscreenEvents) {\n prefixes.forEach((prefix) => {\n element.removeEventListener(`${prefix}fullscreenchange`, onFullScreen);\n element.removeEventListener(`${prefix}fullscreenerror`, onError);\n });\n}\n\nexport interface UseFullscreenElementReturnValue<T extends HTMLElement = any> {\n ref: React.RefCallback<T | null>;\n toggle: () => Promise<void>;\n fullscreen: boolean;\n}\n\nexport function useFullscreenElement<\n T extends HTMLElement = any,\n>(): UseFullscreenElementReturnValue<T> {\n const [fullscreen, setFullscreen] = useState<boolean>(false);\n const refElement = useRef<T | null>(null);\n const prevNodeRef = useRef<T | null>(null);\n\n const handleFullscreenChange = useCallback(() => {\n setFullscreen(refElement.current === getFullscreenElement());\n }, []);\n\n const handleFullscreenError = useCallback(() => {\n setFullscreen(false);\n }, []);\n\n const toggle = useCallback(async () => {\n if (!getFullscreenElement() && refElement.current) {\n await enterFullScreen(refElement.current);\n } else {\n await exitFullscreen();\n }\n }, []);\n\n const refCallback: React.RefCallback<T | null> = useCallback((node) => {\n if (prevNodeRef.current && prevNodeRef.current !== node) {\n removeEvents(prevNodeRef.current, {\n onFullScreen: handleFullscreenChange,\n onError: handleFullscreenError,\n });\n }\n\n if (node) {\n addEvents(node, {\n onFullScreen: handleFullscreenChange,\n onError: handleFullscreenError,\n });\n }\n\n refElement.current = node;\n prevNodeRef.current = node;\n }, []);\n\n return { ref: refCallback, toggle, fullscreen };\n}\n\nexport interface UseFullscreenDocumentReturnValue {\n toggle: () => Promise<void>;\n fullscreen: boolean;\n}\n\nexport function useFullscreenDocument(): UseFullscreenDocumentReturnValue {\n const [fullscreen, setFullscreen] = useState<boolean>(false);\n\n const handleFullscreenChange = useCallback(() => {\n setFullscreen(getFullscreenElement() === window.document.documentElement);\n }, []);\n\n const handleFullscreenError = useCallback(() => {\n setFullscreen(false);\n }, []);\n\n const toggle = useCallback(async () => {\n if (!getFullscreenElement()) {\n await enterFullScreen(window.document.documentElement);\n } else {\n await exitFullscreen();\n }\n }, []);\n\n useEffect(() => {\n return addEvents(window.document.documentElement, {\n onFullScreen: handleFullscreenChange,\n onError: handleFullscreenError,\n });\n }, []);\n\n return { toggle, fullscreen };\n}\n"],"mappings":";;;AAEA,SAAS,uBAA2C;CAClD,MAAM,YAAY,OAAO;AAQzB,QALE,UAAU,qBACV,UAAU,2BACV,UAAU,wBACV,UAAU;;AAKd,SAAS,iBAAiB;CACxB,MAAM,YAAY,OAAO;AAEzB,KAAI,OAAO,UAAU,mBAAmB,WACtC,QAAO,UAAU,gBAAgB;AAEnC,KAAI,OAAO,UAAU,qBAAqB,WACxC,QAAO,UAAU,kBAAkB;AAErC,KAAI,OAAO,UAAU,yBAAyB,WAC5C,QAAO,UAAU,sBAAsB;AAEzC,KAAI,OAAO,UAAU,wBAAwB,WAC3C,QAAO,UAAU,qBAAqB;AAGxC,QAAO;;AAGT,SAAS,gBAAgB,SAAsB;CAC7C,MAAM,WAAW;AAEjB,QACE,SAAS,qBAAqB,IAC9B,SAAS,uBAAuB,IAChC,SAAS,yBAAyB,IAClC,SAAS,2BAA2B,IACpC,SAAS,wBAAwB;;AAIrC,MAAM,WAAW;CAAC;CAAI;CAAU;CAAO;CAAK;AAO5C,SAAS,UAAU,SAAsB,QAA0B;CACjE,MAAM,EAAE,cAAc,YAAY;AAClC,UAAS,SAAS,WAAW;AAC3B,UAAQ,iBAAiB,GAAG,OAAO,mBAAmB,aAAa;AACnE,UAAQ,iBAAiB,GAAG,OAAO,kBAAkB,QAAQ;GAC7D;AAEF,cAAa,aAAa,SAAS,OAAO;;AAG5C,SAAS,aAAa,SAAsB,EAAE,cAAc,WAA6B;AACvF,UAAS,SAAS,WAAW;AAC3B,UAAQ,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GACd,SAAS;GACV,CAAC;IACD,EAAE,CAAC;AAEN,QAAO;EAAE;EAAQ;EAAY"}
|
|
@@ -32,8 +32,11 @@ function useHeadroom({ fixedAt = 0, scrollDistance = 100, onPin, onFix, onReleas
|
|
|
32
32
|
fixedAt,
|
|
33
33
|
isScrollingUp
|
|
34
34
|
]);
|
|
35
|
+
const wasFixedRef = useRef(false);
|
|
35
36
|
useIsomorphicEffect(() => {
|
|
36
|
-
|
|
37
|
+
const currentlyInFixedZone = isFixed(scrollPosition, fixedAt);
|
|
38
|
+
if (currentlyInFixedZone && !wasFixedRef.current) onFixEvent();
|
|
39
|
+
wasFixedRef.current = currentlyInFixedZone;
|
|
37
40
|
}, [scrollPosition, fixedAt]);
|
|
38
41
|
const currentlyFixed = isFixed(scrollPosition, fixedAt);
|
|
39
42
|
const prevIsFixedRef = useRef(currentlyFixed);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-headroom.mjs","names":[],"sources":["../../src/use-headroom/use-headroom.ts"],"sourcesContent":["import { useEffectEvent, useRef } from 'react';\nimport { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';\nimport { useScrollDirection } from '../use-scroll-direction/use-scroll-direction';\nimport { useWindowScroll } from '../use-window-scroll/use-window-scroll';\n\nexport { useScrollDirection } from '../use-scroll-direction/use-scroll-direction';\n\nexport const isFixed = (current: number, fixedAt: number) => current <= fixedAt;\nexport const isPinned = (current: number, previous: number) => current <= previous;\nexport const isReleased = (current: number, previous: number, fixedAt: number) =>\n !isPinned(current, previous) && !isFixed(current, fixedAt);\n\nexport const isPinnedOrReleased = (\n current: number,\n fixedAt: number,\n isCurrentlyPinnedRef: React.RefObject<boolean>,\n isScrollingUp: boolean,\n onPin?: () => void,\n onRelease?: () => void\n) => {\n const isInFixedPosition = isFixed(current, fixedAt);\n if (isInFixedPosition && !isCurrentlyPinnedRef.current) {\n isCurrentlyPinnedRef.current = true;\n onPin?.();\n } else if (!isInFixedPosition && isScrollingUp && !isCurrentlyPinnedRef.current) {\n isCurrentlyPinnedRef.current = true;\n onPin?.();\n } else if (!isInFixedPosition && !isScrollingUp && isCurrentlyPinnedRef.current) {\n isCurrentlyPinnedRef.current = false;\n onRelease?.();\n }\n};\n\nexport interface UseHeadroomInput {\n /** Number in px at which element should be fixed */\n fixedAt?: number;\n\n /** Number of px to scroll to fully reveal or hide the element, 100 by default */\n scrollDistance?: number;\n\n /** Called when element is pinned */\n onPin?: () => void;\n\n /** Called when element is at fixed position */\n onFix?: () => void;\n\n /** Called when element is unpinned */\n onRelease?: () => void;\n}\n\nexport interface UseHeadroomReturnValue {\n /** True when the element is at least partially visible */\n pinned: boolean;\n\n /** Reveal progress: 0 = fully hidden, 1 = fully visible */\n scrollProgress: number;\n}\n\nexport function useHeadroom({\n fixedAt = 0,\n scrollDistance = 100,\n onPin,\n onFix,\n onRelease,\n}: UseHeadroomInput = {}): UseHeadroomReturnValue {\n const isCurrentlyPinnedRef = useRef(false);\n const scrollDirection = useScrollDirection();\n const isScrollingUp = scrollDirection === 'up';\n const [{ y: scrollPosition }] = useWindowScroll();\n\n const onPinEvent = useEffectEvent(() => onPin?.());\n const onReleaseEvent = useEffectEvent(() => onRelease?.());\n const onFixEvent = useEffectEvent(() => onFix?.());\n\n useIsomorphicEffect(() => {\n isPinnedOrReleased(\n scrollPosition,\n fixedAt,\n isCurrentlyPinnedRef,\n isScrollingUp,\n onPinEvent,\n onReleaseEvent\n );\n }, [scrollPosition, fixedAt, isScrollingUp]);\n\n useIsomorphicEffect(() => {\n
|
|
1
|
+
{"version":3,"file":"use-headroom.mjs","names":[],"sources":["../../src/use-headroom/use-headroom.ts"],"sourcesContent":["import { useEffectEvent, useRef } from 'react';\nimport { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';\nimport { useScrollDirection } from '../use-scroll-direction/use-scroll-direction';\nimport { useWindowScroll } from '../use-window-scroll/use-window-scroll';\n\nexport { useScrollDirection } from '../use-scroll-direction/use-scroll-direction';\n\nexport const isFixed = (current: number, fixedAt: number) => current <= fixedAt;\nexport const isPinned = (current: number, previous: number) => current <= previous;\nexport const isReleased = (current: number, previous: number, fixedAt: number) =>\n !isPinned(current, previous) && !isFixed(current, fixedAt);\n\nexport const isPinnedOrReleased = (\n current: number,\n fixedAt: number,\n isCurrentlyPinnedRef: React.RefObject<boolean>,\n isScrollingUp: boolean,\n onPin?: () => void,\n onRelease?: () => void\n) => {\n const isInFixedPosition = isFixed(current, fixedAt);\n if (isInFixedPosition && !isCurrentlyPinnedRef.current) {\n isCurrentlyPinnedRef.current = true;\n onPin?.();\n } else if (!isInFixedPosition && isScrollingUp && !isCurrentlyPinnedRef.current) {\n isCurrentlyPinnedRef.current = true;\n onPin?.();\n } else if (!isInFixedPosition && !isScrollingUp && isCurrentlyPinnedRef.current) {\n isCurrentlyPinnedRef.current = false;\n onRelease?.();\n }\n};\n\nexport interface UseHeadroomInput {\n /** Number in px at which element should be fixed */\n fixedAt?: number;\n\n /** Number of px to scroll to fully reveal or hide the element, 100 by default */\n scrollDistance?: number;\n\n /** Called when element is pinned */\n onPin?: () => void;\n\n /** Called when element is at fixed position */\n onFix?: () => void;\n\n /** Called when element is unpinned */\n onRelease?: () => void;\n}\n\nexport interface UseHeadroomReturnValue {\n /** True when the element is at least partially visible */\n pinned: boolean;\n\n /** Reveal progress: 0 = fully hidden, 1 = fully visible */\n scrollProgress: number;\n}\n\nexport function useHeadroom({\n fixedAt = 0,\n scrollDistance = 100,\n onPin,\n onFix,\n onRelease,\n}: UseHeadroomInput = {}): UseHeadroomReturnValue {\n const isCurrentlyPinnedRef = useRef(false);\n const scrollDirection = useScrollDirection();\n const isScrollingUp = scrollDirection === 'up';\n const [{ y: scrollPosition }] = useWindowScroll();\n\n const onPinEvent = useEffectEvent(() => onPin?.());\n const onReleaseEvent = useEffectEvent(() => onRelease?.());\n const onFixEvent = useEffectEvent(() => onFix?.());\n\n useIsomorphicEffect(() => {\n isPinnedOrReleased(\n scrollPosition,\n fixedAt,\n isCurrentlyPinnedRef,\n isScrollingUp,\n onPinEvent,\n onReleaseEvent\n );\n }, [scrollPosition, fixedAt, isScrollingUp]);\n\n const wasFixedRef = useRef(false);\n\n useIsomorphicEffect(() => {\n const currentlyInFixedZone = isFixed(scrollPosition, fixedAt);\n if (currentlyInFixedZone && !wasFixedRef.current) {\n onFixEvent();\n }\n wasFixedRef.current = currentlyInFixedZone;\n }, [scrollPosition, fixedAt]);\n\n // Refs for scroll-progress tracking. Mutated during render (safe for refs).\n const currentlyFixed = isFixed(scrollPosition, fixedAt);\n const prevIsFixedRef = useRef(currentlyFixed);\n const directionChangeScrollYRef = useRef(scrollPosition);\n const progressAtDirectionChangeRef = useRef(currentlyFixed ? 1 : 0);\n const prevIsScrollingUpRef = useRef(isScrollingUp);\n\n // Detect fixed-zone transitions first. When leaving the fixed zone the baseline\n // is anchored at fixedAt (not the current scroll position) so the delta is measured\n // from where the element was last fully visible, regardless of how scroll position\n // was initialised on the first render.\n if (prevIsFixedRef.current !== currentlyFixed) {\n prevIsFixedRef.current = currentlyFixed;\n\n if (!currentlyFixed) {\n directionChangeScrollYRef.current = fixedAt;\n progressAtDirectionChangeRef.current = 1;\n } else {\n directionChangeScrollYRef.current = scrollPosition;\n progressAtDirectionChangeRef.current = 1;\n }\n\n prevIsScrollingUpRef.current = isScrollingUp;\n }\n\n // When scroll direction changes outside the fixed zone, save the current progress\n // so the next direction accumulates from that point (handles partial reveals).\n if (!currentlyFixed && prevIsScrollingUpRef.current !== isScrollingUp) {\n const transitionDelta = Math.abs(scrollPosition - directionChangeScrollYRef.current);\n const transitionProgress = prevIsScrollingUpRef.current\n ? Math.min(progressAtDirectionChangeRef.current + transitionDelta / scrollDistance, 1)\n : Math.max(progressAtDirectionChangeRef.current - transitionDelta / scrollDistance, 0);\n\n prevIsScrollingUpRef.current = isScrollingUp;\n directionChangeScrollYRef.current = scrollPosition;\n progressAtDirectionChangeRef.current = transitionProgress;\n }\n\n let scrollProgress: number;\n\n if (currentlyFixed) {\n scrollProgress = 1;\n } else {\n const scrollDelta = Math.abs(scrollPosition - directionChangeScrollYRef.current);\n\n if (isScrollingUp) {\n scrollProgress = Math.min(\n progressAtDirectionChangeRef.current + scrollDelta / scrollDistance,\n 1\n );\n } else {\n scrollProgress = Math.max(\n progressAtDirectionChangeRef.current - scrollDelta / scrollDistance,\n 0\n );\n }\n }\n\n return { pinned: scrollProgress > 0, scrollProgress };\n}\n\nexport namespace useHeadroom {\n export type Input = UseHeadroomInput;\n export type ReturnValue = UseHeadroomReturnValue;\n}\n"],"mappings":";;;;;;AAOA,MAAa,WAAW,SAAiB,YAAoB,WAAW;AAKxE,MAAa,sBACX,SACA,SACA,sBACA,eACA,OACA,cACG;CACH,MAAM,oBAAoB,QAAQ,SAAS,QAAQ;AACnD,KAAI,qBAAqB,CAAC,qBAAqB,SAAS;AACtD,uBAAqB,UAAU;AAC/B,WAAS;YACA,CAAC,qBAAqB,iBAAiB,CAAC,qBAAqB,SAAS;AAC/E,uBAAqB,UAAU;AAC/B,WAAS;YACA,CAAC,qBAAqB,CAAC,iBAAiB,qBAAqB,SAAS;AAC/E,uBAAqB,UAAU;AAC/B,eAAa;;;AA6BjB,SAAgB,YAAY,EAC1B,UAAU,GACV,iBAAiB,KACjB,OACA,OACA,cACoB,EAAE,EAA0B;CAChD,MAAM,uBAAuB,OAAO,MAAM;CAE1C,MAAM,gBADkB,oBAAoB,KACF;CAC1C,MAAM,CAAC,EAAE,GAAG,oBAAoB,iBAAiB;CAEjD,MAAM,aAAa,qBAAqB,SAAS,CAAC;CAClD,MAAM,iBAAiB,qBAAqB,aAAa,CAAC;CAC1D,MAAM,aAAa,qBAAqB,SAAS,CAAC;AAElD,2BAA0B;AACxB,qBACE,gBACA,SACA,sBACA,eACA,YACA,eACD;IACA;EAAC;EAAgB;EAAS;EAAc,CAAC;CAE5C,MAAM,cAAc,OAAO,MAAM;AAEjC,2BAA0B;EACxB,MAAM,uBAAuB,QAAQ,gBAAgB,QAAQ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|
|
38
|
-
plus: keys.includes("[plus]")
|
|
37
|
+
shift: keys.includes("shift")
|
|
39
38
|
};
|
|
40
39
|
const reservedKeys = [
|
|
41
40
|
"alt",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-hotkey.mjs","names":[],"sources":["../../src/use-hotkeys/parse-hotkey.ts"],"sourcesContent":["export
|
|
1
|
+
{"version":3,"file":"parse-hotkey.mjs","names":[],"sources":["../../src/use-hotkeys/parse-hotkey.ts"],"sourcesContent":["export interface KeyboardModifiers {\n alt: boolean;\n ctrl: boolean;\n meta: boolean;\n mod: boolean;\n shift: boolean;\n}\n\nexport interface Hotkey extends KeyboardModifiers {\n key?: string;\n}\n\ntype CheckHotkeyMatch = (event: KeyboardEvent) => boolean;\n\nconst keyNameMap: Record<string, string> = {\n ' ': 'space',\n ArrowLeft: 'arrowleft',\n ArrowRight: 'arrowright',\n ArrowUp: 'arrowup',\n ArrowDown: 'arrowdown',\n Escape: 'escape',\n Esc: 'escape',\n esc: 'escape',\n Enter: 'enter',\n Tab: 'tab',\n Backspace: 'backspace',\n Delete: 'delete',\n Insert: 'insert',\n Home: 'home',\n End: 'end',\n PageUp: 'pageup',\n PageDown: 'pagedown',\n '+': 'plus',\n '-': 'minus',\n '*': 'asterisk',\n '/': 'slash',\n};\n\nfunction normalizeKey(key: string): string {\n const lowerKey = key.replace('Key', '').toLowerCase();\n return keyNameMap[key] || lowerKey;\n}\n\nexport function parseHotkey(hotkey: string): Hotkey {\n const keys = hotkey\n .toLowerCase()\n .split('+')\n .map((part) => part.trim());\n\n const modifiers: KeyboardModifiers = {\n alt: keys.includes('alt'),\n ctrl: keys.includes('ctrl'),\n meta: keys.includes('meta'),\n mod: keys.includes('mod'),\n shift: keys.includes('shift'),\n };\n\n const reservedKeys = ['alt', 'ctrl', 'meta', 'shift', 'mod'];\n\n const freeKey = keys.find((key) => !reservedKeys.includes(key));\n\n return {\n ...modifiers,\n key: freeKey === '[plus]' ? '+' : freeKey,\n };\n}\n\nfunction isExactHotkey(hotkey: Hotkey, event: KeyboardEvent, usePhysicalKeys?: boolean): boolean {\n const { alt, ctrl, meta, mod, shift, key } = hotkey;\n const { altKey, ctrlKey, metaKey, shiftKey, key: pressedKey, code: pressedCode } = event;\n\n if (alt !== altKey) {\n return false;\n }\n\n if (mod) {\n if (!ctrlKey && !metaKey) {\n return false;\n }\n } else {\n if (ctrl !== ctrlKey) {\n return false;\n }\n if (meta !== metaKey) {\n return false;\n }\n }\n if (shift !== shiftKey) {\n return false;\n }\n\n if (\n key &&\n (usePhysicalKeys\n ? normalizeKey(pressedCode) === normalizeKey(key)\n : normalizeKey(pressedKey ?? pressedCode) === normalizeKey(key))\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function getHotkeyMatcher(hotkey: string, usePhysicalKeys?: boolean): CheckHotkeyMatch {\n return (event) => isExactHotkey(parseHotkey(hotkey), event, usePhysicalKeys);\n}\n\nexport interface HotkeyItemOptions {\n preventDefault?: boolean;\n usePhysicalKeys?: boolean;\n}\n\ntype HotkeyItem = [string, (event: any) => void, HotkeyItemOptions?];\n\nexport function getHotkeyHandler(hotkeys: HotkeyItem[]) {\n return (event: React.KeyboardEvent<HTMLElement> | KeyboardEvent) => {\n const _event = 'nativeEvent' in event ? event.nativeEvent : event;\n hotkeys.forEach(\n ([hotkey, handler, options = { preventDefault: true, usePhysicalKeys: false }]) => {\n if (getHotkeyMatcher(hotkey, options.usePhysicalKeys)(_event)) {\n if (options.preventDefault) {\n event.preventDefault();\n }\n\n handler(_event);\n }\n }\n );\n };\n}\n"],"mappings":";;AAcA,MAAM,aAAqC;CACzC,KAAK;CACL,WAAW;CACX,YAAY;CACZ,SAAS;CACT,WAAW;CACX,QAAQ;CACR,KAAK;CACL,KAAK;CACL,OAAO;CACP,KAAK;CACL,WAAW;CACX,QAAQ;CACR,QAAQ;CACR,MAAM;CACN,KAAK;CACL,QAAQ;CACR,UAAU;CACV,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAA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