@siberiacancode/reactuse 0.1.1 → 0.2.1

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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/helpers/createContext/createContext.cjs +2 -0
  3. package/dist/cjs/helpers/createContext/createContext.cjs.map +1 -0
  4. package/dist/cjs/helpers/createReactiveContext/createReactiveContext.cjs +2 -0
  5. package/dist/cjs/helpers/createReactiveContext/createReactiveContext.cjs.map +1 -0
  6. package/dist/cjs/helpers/createStore/createStore.cjs +2 -0
  7. package/dist/cjs/helpers/createStore/createStore.cjs.map +1 -0
  8. package/dist/cjs/hooks/useClipboard/useClipboard.cjs.map +1 -1
  9. package/dist/cjs/hooks/useField/useField.cjs +1 -1
  10. package/dist/cjs/hooks/useField/useField.cjs.map +1 -1
  11. package/dist/cjs/hooks/useGamepad/useGamepad.cjs.map +1 -1
  12. package/dist/cjs/hooks/useIntersectionObserver/useIntersectionObserver.cjs.map +1 -1
  13. package/dist/cjs/hooks/useMemory/useMemory.cjs +1 -1
  14. package/dist/cjs/hooks/useMemory/useMemory.cjs.map +1 -1
  15. package/dist/cjs/hooks/useUrlSearchParams/useUrlSearchParams.cjs +2 -0
  16. package/dist/cjs/hooks/useUrlSearchParams/useUrlSearchParams.cjs.map +1 -0
  17. package/dist/cjs/index.cjs +1 -1
  18. package/dist/esm/helpers/createContext/createContext.mjs +34 -0
  19. package/dist/esm/helpers/createContext/createContext.mjs.map +1 -0
  20. package/dist/esm/helpers/createReactiveContext/createReactiveContext.mjs +54 -0
  21. package/dist/esm/helpers/createReactiveContext/createReactiveContext.mjs.map +1 -0
  22. package/dist/esm/helpers/createStore/createStore.mjs +25 -0
  23. package/dist/esm/helpers/createStore/createStore.mjs.map +1 -0
  24. package/dist/esm/hooks/useClipboard/useClipboard.mjs.map +1 -1
  25. package/dist/esm/hooks/useField/useField.mjs +1 -3
  26. package/dist/esm/hooks/useField/useField.mjs.map +1 -1
  27. package/dist/esm/hooks/useGamepad/useGamepad.mjs +7 -2
  28. package/dist/esm/hooks/useGamepad/useGamepad.mjs.map +1 -1
  29. package/dist/esm/hooks/useIntersectionObserver/useIntersectionObserver.mjs.map +1 -1
  30. package/dist/esm/hooks/useMemory/useMemory.mjs +14 -10
  31. package/dist/esm/hooks/useMemory/useMemory.mjs.map +1 -1
  32. package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs +63 -0
  33. package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs.map +1 -0
  34. package/dist/esm/index.mjs +330 -319
  35. package/dist/esm/index.mjs.map +1 -1
  36. package/dist/types/helpers/createContext/createContext.d.ts +48 -0
  37. package/dist/types/helpers/createReactiveContext/createReactiveContext.d.ts +46 -0
  38. package/dist/types/helpers/createStore/createStore.d.ts +31 -0
  39. package/dist/types/helpers/index.d.ts +3 -0
  40. package/dist/types/hooks/index.d.ts +1 -0
  41. package/dist/types/hooks/useClipboard/useClipboard.d.ts +1 -1
  42. package/dist/types/hooks/useIntersectionObserver/useIntersectionObserver.d.ts +1 -1
  43. package/dist/types/hooks/useUrlSearchParams/useUrlSearchParams.d.ts +39 -0
  44. package/dist/types/index.d.ts +1 -0
  45. package/package.json +19 -20
@@ -1 +1 @@
1
- {"version":3,"file":"useField.mjs","sources":["../../../../src/hooks/useField/useField.ts"],"sourcesContent":["import { useRef, useState } from 'react';\n\nimport { useRerender } from '../useRerender/useRerender';\n\n/** The use field params type */\nexport interface UseFieldParams<Value> {\n /** The auto focus */\n autoFocus?: boolean;\n /** The initial touched */\n initialTouched?: boolean;\n /** The initial value */\n initialValue?: Value;\n /** The validate on blur */\n validateOnBlur?: boolean;\n /** The validate on mount */\n validateOnChange?: boolean;\n /** The validate on mount */\n validateOnMount?: boolean;\n}\n\n/** The use field register params type */\nexport interface UseFieldRegisterParams {\n /** The required validation */\n required?: string;\n /** The custom validation */\n validate?: (value: string) => Promise<string | true>;\n /** The min value validation */\n max?: {\n value: number;\n message: string;\n };\n /** The max length validation */\n maxLength?: {\n value: number;\n message: string;\n };\n /** The max value validation */\n min?: {\n value: number;\n message: string;\n };\n /** The min length validation */\n minLength?: {\n value: number;\n message: string;\n };\n /** The pattern validation */\n pattern?: {\n value: RegExp;\n message: string;\n };\n}\n\n/** The use field return type */\nexport interface UseFieldReturn<Value> {\n /** The dirty state */\n dirty: boolean;\n /** The error state */\n error?: string;\n /** The set error function */\n touched: boolean;\n /** The set error function */\n clearError: () => void;\n /** The focus function */\n focus: () => void;\n /** The get value function */\n getValue: () => Value;\n /** The register function */\n register: (params?: UseFieldRegisterParams) => {\n onBlur: () => void;\n onChange: () => void;\n ref: (\n node: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | null | undefined\n ) => void;\n };\n /** The reset function */\n reset: () => void;\n /** The set error function */\n setError: (error: string) => void;\n /** The set value function */\n setValue: (value: Value) => void;\n /** The watch function */\n watch: () => Value;\n}\n\n/**\n * @name useField\n * @description - Hook to manage a form field\n * @category Utilities\n *\n * @template Value The input value\n * @template Type The input value type\n * @param {Value} [params.initialValue] Initial value\n * @param {boolean} [params.initialTouched=false] Initial touched state\n * @param {boolean} [params.autoFocus=false] Auto focus\n * @param {boolean} [params.validateOnChange=false] Validate on change\n * @param {boolean} [params.validateOnBlur=false] Validate on blur\n * @param {boolean} [params.validateOnMount=false] Validate on mount\n * @returns {UseFieldReturn<Value>} An object containing input information\n *\n * @example\n * const { register, getValue, setValue, reset, dirty, error, setError, clearError, touched, focus, watch } = useField();\n */\nexport const useField = <\n Value extends boolean | number | string = string,\n Type = Value extends string ? string : Value extends boolean ? boolean : number\n>(\n params?: UseFieldParams<Value>\n): UseFieldReturn<Type> => {\n const initialValue = (params?.initialValue ?? '') as Value;\n\n const inputRef = useRef<HTMLInputElement | null>(null);\n const watchingRef = useRef(false);\n const rerender = useRerender();\n\n const [dirty, setDirty] = useState(false);\n const [touched, setTouched] = useState(params?.initialTouched ?? false);\n const [error, setError] = useState<string | undefined>(undefined);\n\n const getValue = () => {\n if (inputRef.current?.type === 'radio' || inputRef.current?.type === 'checkbox')\n return inputRef.current.checked as Type;\n return (inputRef.current?.value ?? initialValue) as Type;\n };\n\n const setValue = (value: Type) => {\n if (inputRef.current?.type === 'radio' || inputRef.current?.type === 'checkbox') {\n inputRef.current.checked = value as boolean;\n if (watchingRef.current) return rerender();\n return;\n }\n\n inputRef.current!.value = value as string;\n if (watchingRef.current) return rerender();\n };\n\n const reset = () => {\n setValue(initialValue as unknown as Type);\n setDirty(false);\n setTouched(false);\n setError(undefined);\n };\n\n const focus = () => {\n inputRef.current!.focus();\n };\n\n const validate = (params: UseFieldRegisterParams) => {\n if (params.required && !inputRef.current!.value) {\n return setError(params.required);\n }\n\n if (params.minLength && inputRef.current!.value.length < params.minLength.value) {\n return setError(params.minLength.message);\n }\n\n if (params.maxLength && inputRef.current!.value.length > params.maxLength.value) {\n return setError(params.maxLength.message);\n }\n\n if (params.min && Number(inputRef.current!.value) < params.min.value) {\n return setError(params.min.message);\n }\n\n if (params.max && Number(inputRef.current!.value) > params.max.value) {\n return setError(params.max.message);\n }\n\n if (params.pattern && !params.pattern.value.test(inputRef.current!.value)) {\n return setError(params.pattern.message);\n }\n\n if (params.validate) {\n const error = params.validate(inputRef.current!.value);\n if (typeof error === 'string') return setError(error);\n }\n\n setError(undefined);\n };\n\n const register = (registerParams?: UseFieldRegisterParams) => ({\n ref: (node: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | null | undefined) => {\n if (!inputRef.current && node) {\n if (params?.autoFocus) node.focus();\n inputRef.current = node as HTMLInputElement;\n if (inputRef.current.type === 'radio') {\n inputRef.current.defaultChecked = params?.initialValue === node.value;\n return;\n }\n if (inputRef.current.type === 'checkbox') {\n inputRef.current.defaultChecked = !!params?.initialValue;\n return;\n }\n inputRef.current.defaultValue = String(initialValue);\n\n if (registerParams && params?.validateOnMount) validate(registerParams);\n }\n },\n onChange: async () => {\n if (watchingRef.current) return rerender();\n if (inputRef.current!.value !== initialValue) setDirty(true);\n if (dirty && inputRef.current!.value === initialValue) setDirty(false);\n if (registerParams && params?.validateOnChange) await validate(registerParams);\n if (registerParams && params?.validateOnBlur) setError(undefined);\n },\n onBlur: async () => {\n if (registerParams && params?.validateOnBlur) await validate(registerParams);\n setTouched(true);\n }\n });\n\n const watch = () => {\n watchingRef.current = true;\n return getValue();\n };\n\n const clearError = () => setError(undefined);\n\n return {\n register,\n dirty,\n touched,\n error,\n setError,\n clearError,\n getValue,\n setValue,\n reset,\n watch,\n focus\n };\n};\n"],"names":["useField","params","initialValue","inputRef","useRef","watchingRef","rerender","useRerender","dirty","setDirty","useState","touched","setTouched","error","setError","getValue","_a","_b","_c","setValue","value","reset","focus","validate","registerParams","node"],"mappings":";;AAuGa,MAAAA,IAAW,CAItBC,MACyB;AACnB,QAAAC,KAAgBD,KAAA,gBAAAA,EAAQ,iBAAgB,IAExCE,IAAWC,EAAgC,IAAI,GAC/CC,IAAcD,EAAO,EAAK,GAC1BE,IAAWC,EAAY,GAEvB,CAACC,GAAOC,CAAQ,IAAIC,EAAS,EAAK,GAClC,CAACC,GAASC,CAAU,IAAIF,GAAST,KAAA,gBAAAA,EAAQ,mBAAkB,EAAK,GAChE,CAACY,GAAOC,CAAQ,IAAIJ,EAA6B,MAAS,GAE1DK,IAAW,MAAM;;AACrB,aAAIC,IAAAb,EAAS,YAAT,gBAAAa,EAAkB,UAAS,aAAWC,IAAAd,EAAS,YAAT,gBAAAc,EAAkB,UAAS,aAC5Dd,EAAS,QAAQ,YAClBe,IAAAf,EAAS,YAAT,gBAAAe,EAAkB,UAAShB;AAAA,EACrC,GAEMiB,IAAW,CAACC,MAAgB;;AAChC,UAAIJ,IAAAb,EAAS,YAAT,gBAAAa,EAAkB,UAAS,aAAWC,IAAAd,EAAS,YAAT,gBAAAc,EAAkB,UAAS;AAE/D,aADJd,EAAS,QAAQ,UAAUiB,GACvBf,EAAY,UAAgBC,EAAS,IACzC;AAIE,QADJH,EAAS,QAAS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MAAM,GAClCtB,EAAS,UAAUsB,GACftB,EAAS,QAAQ,SAAS,SAAS;AACrC,YAAAA,EAAS,QAAQ,kBAAiBF,KAAA,gBAAAA,EAAQ,kBAAiBwB,EAAK;AAChE;AAAA,UAAA;AAEE,cAAAtB,EAAS,QAAQ,SAAS,YAAY;AACxC,YAAAA,EAAS,QAAQ,iBAAiB,CAAC,EAACF,KAAA,QAAAA,EAAQ;AAC5C;AAAA,UAAA;AAEO,UAAAE,EAAA,QAAQ,eAAe,OAAOD,CAAY,GAE/CsB,MAAkBvB,KAAA,QAAAA,EAAQ,oBAAiBsB,EAASC,CAAc;AAAA,QAAA;AAAA,MAE1E;AAAA,MACA,UAAU,YAAY;AAChB,YAAAnB,EAAY,QAAS,QAAOC,EAAS;AACzC,QAAIH,EAAS,QAAS,UAAUD,OAAuB,EAAI,GACvDM,KAASL,EAAS,QAAS,UAAUD,OAAuB,EAAK,GACjEsB,MAAkBvB,KAAA,QAAAA,EAAQ,qBAAkB,MAAMsB,EAASC,CAAc,GACzEA,MAAkBvB,KAAA,QAAAA,EAAQ,mBAAgBa,EAAS,MAAS;AAAA,MAClE;AAAA,MACA,QAAQ,YAAY;AAClB,QAAIU,MAAkBvB,KAAA,QAAAA,EAAQ,mBAAgB,MAAMsB,EAASC,CAAc,GAC3EZ,EAAW,EAAI;AAAA,MAAA;AAAA,IACjB;AAAA,IAYA,OAAAJ;AAAA,IACA,SAAAG;AAAA,IACA,OAAAE;AAAA,IACA,UAAAC;AAAA,IACA,YARiB,MAAMA,EAAS,MAAS;AAAA,IASzC,UAAAC;AAAA,IACA,UAAAI;AAAA,IACA,OAAAE;AAAA,IACA,OAjBY,OACZhB,EAAY,UAAU,IACfU,EAAS;AAAA,IAgBhB,OAAAO;AAAA,EACF;AACF;"}
1
+ {"version":3,"file":"useField.mjs","sources":["../../../../src/hooks/useField/useField.ts"],"sourcesContent":["import { useRef, useState } from 'react';\n\nimport { useRerender } from '../useRerender/useRerender';\n\n/** The use field params type */\nexport interface UseFieldParams<Value> {\n /** The auto focus */\n autoFocus?: boolean;\n /** The initial touched */\n initialTouched?: boolean;\n /** The initial value */\n initialValue?: Value;\n /** The validate on blur */\n validateOnBlur?: boolean;\n /** The validate on mount */\n validateOnChange?: boolean;\n /** The validate on mount */\n validateOnMount?: boolean;\n}\n\n/** The use field register params type */\nexport interface UseFieldRegisterParams {\n /** The required validation */\n required?: string;\n /** The custom validation */\n validate?: (value: string) => Promise<string | true>;\n /** The min value validation */\n max?: {\n value: number;\n message: string;\n };\n /** The max length validation */\n maxLength?: {\n value: number;\n message: string;\n };\n /** The max value validation */\n min?: {\n value: number;\n message: string;\n };\n /** The min length validation */\n minLength?: {\n value: number;\n message: string;\n };\n /** The pattern validation */\n pattern?: {\n value: RegExp;\n message: string;\n };\n}\n\n/** The use field return type */\nexport interface UseFieldReturn<Value> {\n /** The dirty state */\n dirty: boolean;\n /** The error state */\n error?: string;\n /** The set error function */\n touched: boolean;\n /** The set error function */\n clearError: () => void;\n /** The focus function */\n focus: () => void;\n /** The get value function */\n getValue: () => Value;\n /** The register function */\n register: (params?: UseFieldRegisterParams) => {\n onBlur: () => void;\n onChange: () => void;\n ref: (\n node: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | null | undefined\n ) => void;\n };\n /** The reset function */\n reset: () => void;\n /** The set error function */\n setError: (error: string) => void;\n /** The set value function */\n setValue: (value: Value) => void;\n /** The watch function */\n watch: () => Value;\n}\n\n/**\n * @name useField\n * @description - Hook to manage a form field\n * @category Utilities\n *\n * @template Value The input value\n * @template Type The input value type\n * @param {Value} [params.initialValue] Initial value\n * @param {boolean} [params.initialTouched=false] Initial touched state\n * @param {boolean} [params.autoFocus=false] Auto focus\n * @param {boolean} [params.validateOnChange=false] Validate on change\n * @param {boolean} [params.validateOnBlur=false] Validate on blur\n * @param {boolean} [params.validateOnMount=false] Validate on mount\n * @returns {UseFieldReturn<Value>} An object containing input information\n *\n * @example\n * const { register, getValue, setValue, reset, dirty, error, setError, clearError, touched, focus, watch } = useField();\n */\nexport const useField = <\n Value extends boolean | number | string = string,\n Type = Value extends string ? string : Value extends boolean ? boolean : number\n>(\n params?: UseFieldParams<Value>\n): UseFieldReturn<Type> => {\n const initialValue = (params?.initialValue ?? '') as Value;\n\n const inputRef = useRef<HTMLInputElement | null>(null);\n const watchingRef = useRef(false);\n const rerender = useRerender();\n\n const [dirty, setDirty] = useState(false);\n const [touched, setTouched] = useState(params?.initialTouched ?? false);\n const [error, setError] = useState<string | undefined>(undefined);\n\n const getValue = () => {\n if (inputRef.current?.type === 'radio' || inputRef.current?.type === 'checkbox')\n return inputRef.current.checked as Type;\n return (inputRef.current?.value ?? initialValue) as Type;\n };\n\n const setValue = (value: Type) => {\n if (inputRef.current?.type === 'radio' || inputRef.current?.type === 'checkbox') {\n inputRef.current.checked = value as boolean;\n if (watchingRef.current) return rerender();\n return;\n }\n\n inputRef.current!.value = value as string;\n if (watchingRef.current) return rerender();\n };\n\n const reset = () => {\n setValue(initialValue as unknown as Type);\n setDirty(false);\n setTouched(false);\n setError(undefined);\n };\n\n const focus = () => inputRef.current!.focus();\n\n const validate = (params: UseFieldRegisterParams) => {\n if (params.required && !inputRef.current!.value) {\n return setError(params.required);\n }\n\n if (params.minLength && inputRef.current!.value.length < params.minLength.value) {\n return setError(params.minLength.message);\n }\n\n if (params.maxLength && inputRef.current!.value.length > params.maxLength.value) {\n return setError(params.maxLength.message);\n }\n\n if (params.min && Number(inputRef.current!.value) < params.min.value) {\n return setError(params.min.message);\n }\n\n if (params.max && Number(inputRef.current!.value) > params.max.value) {\n return setError(params.max.message);\n }\n\n if (params.pattern && !params.pattern.value.test(inputRef.current!.value)) {\n return setError(params.pattern.message);\n }\n\n if (params.validate) {\n const error = params.validate(inputRef.current!.value);\n if (typeof error === 'string') return setError(error);\n }\n\n setError(undefined);\n };\n\n const register = (registerParams?: UseFieldRegisterParams) => ({\n ref: (node: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | null | undefined) => {\n if (!inputRef.current && node) {\n if (params?.autoFocus) node.focus();\n inputRef.current = node as HTMLInputElement;\n if (inputRef.current.type === 'radio') {\n inputRef.current.defaultChecked = params?.initialValue === node.value;\n return;\n }\n if (inputRef.current.type === 'checkbox') {\n inputRef.current.defaultChecked = !!params?.initialValue;\n return;\n }\n inputRef.current.defaultValue = String(initialValue);\n\n if (registerParams && params?.validateOnMount) validate(registerParams);\n }\n },\n onChange: async () => {\n if (watchingRef.current) return rerender();\n if (inputRef.current!.value !== initialValue) setDirty(true);\n if (dirty && inputRef.current!.value === initialValue) setDirty(false);\n if (registerParams && params?.validateOnChange) await validate(registerParams);\n if (registerParams && params?.validateOnBlur) setError(undefined);\n },\n onBlur: async () => {\n if (registerParams && params?.validateOnBlur) await validate(registerParams);\n setTouched(true);\n }\n });\n\n const watch = () => {\n watchingRef.current = true;\n return getValue();\n };\n\n const clearError = () => setError(undefined);\n\n return {\n register,\n dirty,\n touched,\n error,\n setError,\n clearError,\n getValue,\n setValue,\n reset,\n watch,\n focus\n };\n};\n"],"names":["useField","params","initialValue","inputRef","useRef","watchingRef","rerender","useRerender","dirty","setDirty","useState","touched","setTouched","error","setError","getValue","_a","_b","_c","setValue","value","reset","focus","validate","registerParams","node"],"mappings":";;AAuGa,MAAAA,IAAW,CAItBC,MACyB;AACnB,QAAAC,KAAgBD,KAAA,gBAAAA,EAAQ,iBAAgB,IAExCE,IAAWC,EAAgC,IAAI,GAC/CC,IAAcD,EAAO,EAAK,GAC1BE,IAAWC,EAAY,GAEvB,CAACC,GAAOC,CAAQ,IAAIC,EAAS,EAAK,GAClC,CAACC,GAASC,CAAU,IAAIF,GAAST,KAAA,gBAAAA,EAAQ,mBAAkB,EAAK,GAChE,CAACY,GAAOC,CAAQ,IAAIJ,EAA6B,MAAS,GAE1DK,IAAW,MAAM;;AACrB,aAAIC,IAAAb,EAAS,YAAT,gBAAAa,EAAkB,UAAS,aAAWC,IAAAd,EAAS,YAAT,gBAAAc,EAAkB,UAAS,aAC5Dd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;AAAA,IASzC,UAAAC;AAAA,IACA,UAAAI;AAAA,IACA,OAAAE;AAAA,IACA,OAjBY,OACZhB,EAAY,UAAU,IACfU,EAAS;AAAA,IAgBhB,OAAAO;AAAA,EACF;AACF;"}
@@ -10,13 +10,18 @@ const x = () => {
10
10
  }, b = () => {
11
11
  for (const n of navigator.getGamepads() ?? [])
12
12
  n && s[n.index] && (s[n.index] = u(n));
13
- }, { active: d } = v(b, { enabled: !!Object.keys(s).length });
13
+ }, { active: d } = v(b, {
14
+ enabled: !!Object.keys(s).length
15
+ });
14
16
  return a(() => {
15
17
  if (!t) return;
16
18
  const n = navigator.getGamepads();
17
19
  r(
18
20
  n.reduce(
19
- (o, e) => ({ ...o, ...e && { [e.index]: u(e) } }),
21
+ (o, e) => ({
22
+ ...o,
23
+ ...e && { [e.index]: u(e) }
24
+ }),
20
25
  {}
21
26
  )
22
27
  );
@@ -1 +1 @@
1
- {"version":3,"file":"useGamepad.mjs","sources":["../../../../src/hooks/useGamepad/useGamepad.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { useRaf } from '../useRaf/useRaf';\n\ndeclare global {\n interface Gamepad {\n hapticActuators?: GamepadHapticActuator[];\n }\n}\n\n/** The use gamepad return type */\nexport interface UseGamepadStateReturn {\n /** The gamepad active status */\n active: boolean;\n /** The gamepad state */\n gamepads: Gamepad[];\n /** The gamepad supported status */\n supported: boolean;\n}\n\n/**\n * @name useGamepad\n * @description - Hook for getting information about gamepad\n * @category Browser\n *\n * @browserapi navigator.getGamepads https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getGamepads\n *\n * @returns {UseGamepadStateReturn} An object containing the gamepad information\n *\n * @example\n * const { supported, gamepads, active } = useGamepad();\n */\nexport const useGamepad = () => {\n const supported = typeof navigator !== 'undefined' && 'getGamepads' in navigator;\n const [gamepads, setGamepads] = useState<Record<number, Gamepad>>({});\n\n const createGamepad = (gamepad: Gamepad) => {\n const hapticActuators = [];\n const vibrationActuator = 'vibrationActuator' in gamepad ? gamepad.vibrationActuator : null;\n\n if (vibrationActuator) hapticActuators.push(vibrationActuator);\n if (gamepad.hapticActuators) hapticActuators.push(...gamepad.hapticActuators);\n\n return {\n ...gamepad,\n hapticActuators\n } as Gamepad;\n };\n\n const updateGamepadState = () => {\n for (const gamepad of navigator.getGamepads() ?? []) {\n if (gamepad && gamepads[gamepad.index]) gamepads[gamepad.index] = createGamepad(gamepad);\n }\n };\n\n const { active } = useRaf(updateGamepadState, { enabled: !!Object.keys(gamepads).length });\n\n useEffect(() => {\n if (!supported) return;\n const gamepads = navigator.getGamepads();\n setGamepads(\n gamepads.reduce(\n (acc, gamepad) => ({ ...acc, ...(gamepad && { [gamepad.index]: createGamepad(gamepad) }) }),\n {}\n )\n );\n }, []);\n\n useEffect(() => {\n const onConnected = (event: Event) => {\n const { gamepad } = event as GamepadEvent;\n setGamepads({ ...gamepads, [gamepad.index]: createGamepad(gamepad) });\n };\n\n const onDisconnected = (event: Event) => {\n const { gamepad } = event as GamepadEvent;\n const updatedGamepads = { ...gamepads };\n delete updatedGamepads[gamepad.index];\n setGamepads(updatedGamepads);\n };\n\n document.addEventListener('gamepadconnected', onConnected);\n document.addEventListener('gamepaddisconnected', onDisconnected);\n\n return () => {\n document.removeEventListener('gamepadconnected', onConnected);\n document.removeEventListener('gamepaddisconnected', onDisconnected);\n };\n }, []);\n\n return {\n active,\n supported,\n gamepads: Object.values(gamepads)\n };\n};\n\nexport const mapGamepadToXbox360Controller = (gamepad: Gamepad) => ({\n buttons: {\n a: gamepad.buttons[0],\n b: gamepad.buttons[1],\n x: gamepad.buttons[2],\n y: gamepad.buttons[3]\n },\n bumper: {\n left: gamepad.buttons[4],\n right: gamepad.buttons[5]\n },\n triggers: {\n left: gamepad.buttons[6],\n right: gamepad.buttons[7]\n },\n stick: {\n left: {\n horizontal: gamepad.axes[0],\n vertical: gamepad.axes[1],\n button: gamepad.buttons[10]\n },\n right: {\n horizontal: gamepad.axes[2],\n vertical: gamepad.axes[3],\n button: gamepad.buttons[11]\n }\n },\n dpad: {\n up: gamepad.buttons[12],\n down: gamepad.buttons[13],\n left: gamepad.buttons[14],\n right: gamepad.buttons[15]\n },\n back: gamepad.buttons[8],\n start: gamepad.buttons[9]\n});\n"],"names":["useGamepad","supported","gamepads","setGamepads","useState","createGamepad","gamepad","hapticActuators","vibrationActuator","updateGamepadState","active","useRaf","useEffect","acc","onConnected","event","onDisconnected","updatedGamepads","mapGamepadToXbox360Controller"],"mappings":";;AAgCO,MAAMA,IAAa,MAAM;AAC9B,QAAMC,IAAY,OAAO,YAAc,OAAe,iBAAiB,WACjE,CAACC,GAAUC,CAAW,IAAIC,EAAkC,CAAA,CAAE,GAE9DC,IAAgB,CAACC,MAAqB;AAC1C,UAAMC,IAAkB,CAAC,GACnBC,IAAoB,uBAAuBF,IAAUA,EAAQ,oBAAoB;AAEnF,WAAAE,KAAmCD,EAAA,KAAKC,CAAiB,GACzDF,EAAQ,mBAAiBC,EAAgB,KAAK,GAAGD,EAAQ,eAAe,GAErE;AAAA,MACL,GAAGA;AAAA,MACH,iBAAAC;AAAA,IACF;AAAA,EACF,GAEME,IAAqB,MAAM;AAC/B,eAAWH,KAAW,UAAU,YAAY,KAAK,CAAA;AAC3C,MAAAA,KAAWJ,EAASI,EAAQ,KAAK,QAAYA,EAAQ,KAAK,IAAID,EAAcC,CAAO;AAAA,EAE3F,GAEM,EAAE,QAAAI,EAAA,IAAWC,EAAOF,GAAoB,EAAE,SAAS,CAAC,CAAC,OAAO,KAAKP,CAAQ,EAAE,QAAQ;AAEzF,SAAAU,EAAU,MAAM;AACd,QAAI,CAACX,EAAW;AACVC,UAAAA,IAAW,UAAU,YAAY;AACvC,IAAAC;AAAA,MACED,EAAS;AAAA,QACP,CAACW,GAAKP,OAAa,EAAE,GAAGO,GAAK,GAAIP,KAAW,EAAE,CAACA,EAAQ,KAAK,GAAGD,EAAcC,CAAO,EAAK,EAAA;AAAA,QACzF,CAAA;AAAA,MAAC;AAAA,IAEL;AAA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;AAAA,IAC5B;AAAA,IACA,OAAO;AAAA,MACL,YAAYA,EAAQ,KAAK,CAAC;AAAA,MAC1B,UAAUA,EAAQ,KAAK,CAAC;AAAA,MACxB,QAAQA,EAAQ,QAAQ,EAAE;AAAA,IAAA;AAAA,EAE9B;AAAA,EACA,MAAM;AAAA,IACJ,IAAIA,EAAQ,QAAQ,EAAE;AAAA,IACtB,MAAMA,EAAQ,QAAQ,EAAE;AAAA,IACxB,MAAMA,EAAQ,QAAQ,EAAE;AAAA,IACxB,OAAOA,EAAQ,QAAQ,EAAE;AAAA,EAC3B;AAAA,EACA,MAAMA,EAAQ,QAAQ,CAAC;AAAA,EACvB,OAAOA,EAAQ,QAAQ,CAAC;AAC1B;"}
1
+ {"version":3,"file":"useGamepad.mjs","sources":["../../../../src/hooks/useGamepad/useGamepad.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { useRaf } from '../useRaf/useRaf';\n\ndeclare global {\n interface Gamepad {\n hapticActuators?: GamepadHapticActuator[];\n }\n}\n\n/** The use gamepad return type */\nexport interface UseGamepadStateReturn {\n /** The gamepad active status */\n active: boolean;\n /** The gamepad state */\n gamepads: Gamepad[];\n /** The gamepad supported status */\n supported: boolean;\n}\n\n/**\n * @name useGamepad\n * @description - Hook for getting information about gamepad\n * @category Browser\n *\n * @browserapi navigator.getGamepads https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getGamepads\n *\n * @returns {UseGamepadStateReturn} An object containing the gamepad information\n *\n * @example\n * const { supported, gamepads, active } = useGamepad();\n */\nexport const useGamepad = () => {\n const supported = typeof navigator !== 'undefined' && 'getGamepads' in navigator;\n const [gamepads, setGamepads] = useState<Record<number, Gamepad>>({});\n\n const createGamepad = (gamepad: Gamepad) => {\n const hapticActuators = [];\n const vibrationActuator = 'vibrationActuator' in gamepad ? gamepad.vibrationActuator : null;\n\n if (vibrationActuator) hapticActuators.push(vibrationActuator);\n if (gamepad.hapticActuators) hapticActuators.push(...gamepad.hapticActuators);\n\n return {\n ...gamepad,\n hapticActuators\n } as Gamepad;\n };\n\n const updateGamepadState = () => {\n for (const gamepad of navigator.getGamepads() ?? []) {\n if (gamepad && gamepads[gamepad.index]) gamepads[gamepad.index] = createGamepad(gamepad);\n }\n };\n\n const { active } = useRaf(updateGamepadState, {\n enabled: !!Object.keys(gamepads).length\n });\n\n useEffect(() => {\n if (!supported) return;\n const gamepads = navigator.getGamepads();\n setGamepads(\n gamepads.reduce(\n (acc, gamepad) => ({\n ...acc,\n ...(gamepad && { [gamepad.index]: createGamepad(gamepad) })\n }),\n {}\n )\n );\n }, []);\n\n useEffect(() => {\n const onConnected = (event: Event) => {\n const { gamepad } = event as GamepadEvent;\n setGamepads({ ...gamepads, [gamepad.index]: createGamepad(gamepad) });\n };\n\n const onDisconnected = (event: Event) => {\n const { gamepad } = event as GamepadEvent;\n const updatedGamepads = { ...gamepads };\n delete updatedGamepads[gamepad.index];\n setGamepads(updatedGamepads);\n };\n\n document.addEventListener('gamepadconnected', onConnected);\n document.addEventListener('gamepaddisconnected', onDisconnected);\n\n return () => {\n document.removeEventListener('gamepadconnected', onConnected);\n document.removeEventListener('gamepaddisconnected', onDisconnected);\n };\n }, []);\n\n return {\n active,\n supported,\n gamepads: Object.values(gamepads)\n };\n};\n\nexport const mapGamepadToXbox360Controller = (gamepad: Gamepad) => ({\n buttons: {\n a: gamepad.buttons[0],\n b: gamepad.buttons[1],\n x: gamepad.buttons[2],\n y: gamepad.buttons[3]\n },\n bumper: {\n left: gamepad.buttons[4],\n right: gamepad.buttons[5]\n },\n triggers: {\n left: gamepad.buttons[6],\n right: gamepad.buttons[7]\n },\n stick: {\n left: {\n horizontal: gamepad.axes[0],\n vertical: gamepad.axes[1],\n button: gamepad.buttons[10]\n },\n right: {\n horizontal: gamepad.axes[2],\n vertical: gamepad.axes[3],\n button: gamepad.buttons[11]\n }\n },\n dpad: {\n up: gamepad.buttons[12],\n down: gamepad.buttons[13],\n left: gamepad.buttons[14],\n right: gamepad.buttons[15]\n },\n back: gamepad.buttons[8],\n start: gamepad.buttons[9]\n});\n"],"names":["useGamepad","supported","gamepads","setGamepads","useState","createGamepad","gamepad","hapticActuators","vibrationActuator","updateGamepadState","active","useRaf","useEffect","acc","onConnected","event","onDisconnected","updatedGamepads","mapGamepadToXbox360Controller"],"mappings":";;AAgCO,MAAMA,IAAa,MAAM;AAC9B,QAAMC,IAAY,OAAO,YAAc,OAAe,iBAAiB,WACjE,CAACC,GAAUC,CAAW,IAAIC,EAAkC,CAAA,CAAE,GAE9DC,IAAgB,CAACC,MAAqB;AAC1C,UAAMC,IAAkB,CAAC,GACnBC,IAAoB,uBAAuBF,IAAUA,EAAQ,oBAAoB;AAEnF,WAAAE,KAAmCD,EAAA,KAAKC,CAAiB,GACzDF,EAAQ,mBAAiBC,EAAgB,KAAK,GAAGD,EAAQ,eAAe,GAErE;AAAA,MACL,GAAGA;AAAA,MACH,iBAAAC;AAAA,IACF;AAAA,EACF,GAEME,IAAqB,MAAM;AAC/B,eAAWH,KAAW,UAAU,YAAY,KAAK,CAAA;AAC3C,MAAAA,KAAWJ,EAASI,EAAQ,KAAK,QAAYA,EAAQ,KAAK,IAAID,EAAcC,CAAO;AAAA,EAE3F,GAEM,EAAE,QAAAI,EAAA,IAAWC,EAAOF,GAAoB;AAAA,IAC5C,SAAS,CAAC,CAAC,OAAO,KAAKP,CAAQ,EAAE;AAAA,EAAA,CAClC;AAED,SAAAU,EAAU,MAAM;AACd,QAAI,CAACX,EAAW;AACVC,UAAAA,IAAW,UAAU,YAAY;AACvC,IAAAC;AAAA,MACED,EAAS;AAAA,QACP,CAACW,GAAKP,OAAa;AAAA,UACjB,GAAGO;AAAA,UACH,GAAIP,KAAW,EAAE,CAACA,EAAQ,KAAK,GAAGD,EAAcC,CAAO,EAAE;AAAA,QAAA;AAAA,QAE3D,CAAA;AAAA,MAAC;AAAA,IAEL;AAAA,EACF,GAAG,EAAE,GAELM,EAAU,MAAM;AACR,UAAAE,IAAc,CAACC,MAAiB;AA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AA,MAC1B,UAAUA,EAAQ,KAAK,CAAC;AAAA,MACxB,QAAQA,EAAQ,QAAQ,EAAE;AAAA,IAAA;AAAA,EAE9B;AAAA,EACA,MAAM;AAAA,IACJ,IAAIA,EAAQ,QAAQ,EAAE;AAAA,IACtB,MAAMA,EAAQ,QAAQ,EAAE;AAAA,IACxB,MAAMA,EAAQ,QAAQ,EAAE;AAAA,IACxB,OAAOA,EAAQ,QAAQ,EAAE;AAAA,EAC3B;AAAA,EACA,MAAMA,EAAQ,QAAQ,CAAC;AAAA,EACvB,OAAOA,EAAQ,QAAQ,CAAC;AAC1B;"}
@@ -1 +1 @@
1
- {"version":3,"file":"useIntersectionObserver.mjs","sources":["../../../../src/hooks/useIntersectionObserver/useIntersectionObserver.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { getElement, isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useRefState } from '../useRefState/useRefState';\n\n/** The intersection observer options type */\nexport interface UseIntersectionObserverOptions extends Omit<IntersectionObserverInit, 'root'> {\n enabled?: boolean;\n root?: HookTarget;\n onChange?: (entry: IntersectionObserverEntry) => void;\n}\n\n/** The intersection observer return type */\nexport interface UseIntersectionObserverReturn {\n entry?: IntersectionObserverEntry;\n inView: boolean;\n}\n\nexport interface UseIntersectionObserver {\n <Target extends Element>(\n options?: UseIntersectionObserverOptions,\n target?: never\n ): UseIntersectionObserverReturn & { ref: StateRef<Target> };\n\n (target: HookTarget, options?: UseIntersectionObserverOptions): UseIntersectionObserverReturn;\n}\n\n/**\n * @name useIntersectionObserver\n * @description - Hook that gives you intersection observer state\n * @category Browser\n *\n * @browserapi IntersectionObserver https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver\n *\n * @overload\n * @param {HookTarget} target The target element to detect intersection\n * @param {boolean} [options.enabled=true] The IntersectionObserver options\n * @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected\n * @param {HookTarget} [options.root] The root element to observe\n * @returns {UseIntersectionObserverReturn} An object containing the state and the supported status\n *\n * @example\n * const { ref, entry, inView } = useIntersectionObserver();\n *\n * @overload\n * @template Target The target element\n * @param {boolean} [options.enabled=true] The IntersectionObserver options\n * @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected\n * @param {HookTarget} [options.root] The root element to observe\n * @returns {UseIntersectionObserverReturn & { ref: StateRef<Target> }} A React ref to attach to the target element\n *\n * @example\n * const { entry, inView } = useIntersectionObserver(ref);\n */\nexport const useIntersectionObserver = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const options = (target ? params[1] : params[0]) as UseIntersectionObserverOptions | undefined;\n const enabled = options?.enabled ?? true;\n\n const [entry, setEntry] = useState<IntersectionObserverEntry>();\n\n const internalRef = useRefState<Element>();\n const internalOnChangeRef = useRef<UseIntersectionObserverOptions['onChange']>(options?.onChange);\n internalOnChangeRef.current = options?.onChange;\n\n useEffect(() => {\n if (!enabled && !target && !internalRef.state) return;\n\n const element = target ? getElement(target) : internalRef.current;\n if (!element) return;\n\n const observer = new IntersectionObserver(\n ([entry]) => {\n setEntry(entry);\n internalOnChangeRef.current?.(entry);\n },\n {\n ...options,\n root: options?.root ? (getElement(options.root) as Document | Element) : document\n }\n );\n\n observer.observe(element as Element);\n\n return () => {\n observer.disconnect();\n };\n }, [target, internalRef.state, options?.rootMargin, options?.threshold, options?.root, enabled]);\n\n if (target) return { entry, inView: !!entry?.isIntersecting };\n return {\n ref: internalRef,\n entry,\n inView: !!entry?.isIntersecting\n };\n}) as UseIntersectionObserver;\n"],"names":["useIntersectionObserver","params","target","isTarget","options","enabled","entry","setEntry","useState","internalRef","useRefState","internalOnChangeRef","useRef","useEffect","element","getElement","observer","_a"],"mappings":";;;;AA2Da,MAAAA,IAA2B,IAAIC,MAAkB;AACt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Observer/useIntersectionObserver.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { getElement, isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useRefState } from '../useRefState/useRefState';\n\n/** The intersection observer options type */\nexport interface UseIntersectionObserverOptions extends Omit<IntersectionObserverInit, 'root'> {\n enabled?: boolean;\n root?: HookTarget;\n onChange?: (entry: IntersectionObserverEntry) => void;\n}\n\n/** The intersection observer return type */\nexport interface UseIntersectionObserverReturn {\n entry?: IntersectionObserverEntry;\n inView: boolean;\n}\n\nexport interface UseIntersectionObserver {\n <Target extends Element>(\n options?: UseIntersectionObserverOptions,\n target?: never\n ): UseIntersectionObserverReturn & { ref: StateRef<Target> };\n\n (target: HookTarget, options?: UseIntersectionObserverOptions): UseIntersectionObserverReturn;\n}\n\n/**\n * @name useIntersectionObserver\n * @description - Hook that gives you intersection observer state\n * @category Browser\n *\n * @browserapi IntersectionObserver https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver\n *\n * @overload\n * @param {HookTarget} target The target element to detect intersection\n * @param {boolean} [options.enabled=true] The IntersectionObserver options\n * @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected\n * @param {HookTarget} [options.root] The root element to observe\n * @returns {UseIntersectionObserverReturn} An object containing the state\n *\n * @example\n * const { ref, entry, inView } = useIntersectionObserver();\n *\n * @overload\n * @template Target The target element\n * @param {boolean} [options.enabled=true] The IntersectionObserver options\n * @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected\n * @param {HookTarget} [options.root] The root element to observe\n * @returns {UseIntersectionObserverReturn & { ref: StateRef<Target> }} A React ref to attach to the target element\n *\n * @example\n * const { entry, inView } = useIntersectionObserver(ref);\n */\nexport const useIntersectionObserver = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const options = (target ? params[1] : params[0]) as UseIntersectionObserverOptions | undefined;\n const enabled = options?.enabled ?? true;\n\n const [entry, setEntry] = useState<IntersectionObserverEntry>();\n\n const internalRef = useRefState<Element>();\n const internalOnChangeRef = useRef<UseIntersectionObserverOptions['onChange']>(options?.onChange);\n internalOnChangeRef.current = options?.onChange;\n\n useEffect(() => {\n if (!enabled && !target && !internalRef.state) return;\n\n const element = target ? getElement(target) : internalRef.current;\n if (!element) return;\n\n const observer = new IntersectionObserver(\n ([entry]) => {\n setEntry(entry);\n internalOnChangeRef.current?.(entry);\n },\n {\n ...options,\n root: options?.root ? (getElement(options.root) as Document | Element) : document\n }\n );\n\n observer.observe(element as Element);\n\n return () => {\n observer.disconnect();\n };\n }, [target, internalRef.state, options?.rootMargin, options?.threshold, options?.root, enabled]);\n\n if (target) return { entry, inView: !!entry?.isIntersecting };\n return {\n ref: internalRef,\n entry,\n inView: !!entry?.isIntersecting\n };\n}) as UseIntersectionObserver;\n"],"names":["useIntersectionObserver","params","target","isTarget","options","enabled","entry","setEntry","useState","internalRef","useRefState","internalOnChangeRef","useRef","useEffect","element","getElement","observer","_a"],"mappings":";;;;AA2Da,MAAAA,IAA2B,IAAIC,MAAkB;AACtD,QAAAC,IAAUC,EAASF,EAAO,CAAC,CAAC,IAAIA,EAAO,CAAC,IAAI,QAC5CG,IAAWF,IAASD,EAAO,CAAC,IAAIA,EAAO,CAAC,GACxCI,KAAUD,KAAA,gBAAAA,EAAS,YAAW,IAE9B,CAACE,GAAOC,CAAQ,IAAIC,EAAoC,GAExDC,IAAcC,EAAqB,GACnCC,IAAsBC,EAAmDR,KAAA,gBAAAA,EAAS,QAAQ;AA2B5F,SA1BJO,EAAoB,UAAUP,KAAA,gBAAAA,EAAS,UAEvCS,EAAU,MAAM;AACd,QAAI,CAACR,KAAW,CAACH,KAAU,CAACO,EAAY,MAAO;AAE/C,UAAMK,IAAUZ,IAASa,EAAWb,CAAM,IAAIO,EAAY;AAC1D,QAAI,CAACK,EAAS;AAEd,UAAME,IAAW,IAAI;AAAA,MACnB,CAAC,CAACV,CAAK,MAAM;;AACX,QAAAC,EAASD,CAAK,IACdW,IAAAN,EAAoB,YAApB,QAAAM,EAAA,KAAAN,GAA8BL;AAAAA,MAChC;AAAA,MACA;AAAA,QACE,GAAGF;AAAA,QACH,MAAMA,KAAA,QAAAA,EAAS,OAAQW,EAAWX,EAAQ,IAAI,IAA2B;AAAA,MAAA;AAAA,IAE7E;AAEA,WAAAY,EAAS,QAAQF,CAAkB,GAE5B,MAAM;AACX,MAAAE,EAAS,WAAW;AAAA,IACtB;AAAA,EACC,GAAA,CAACd,GAAQO,EAAY,OAAOL,KAAA,gBAAAA,EAAS,YAAYA,KAAA,gBAAAA,EAAS,WAAWA,KAAA,gBAAAA,EAAS,MAAMC,CAAO,CAAC,GAE3FH,IAAe,EAAE,OAAAI,GAAO,QAAQ,CAAC,EAACA,KAAA,QAAAA,EAAO,gBAAe,IACrD;AAAA,IACL,KAAKG;AAAA,IACL,OAAAH;AAAA,IACA,QAAQ,CAAC,EAACA,KAAA,QAAAA,EAAO;AAAA,EACnB;AACF;"}
@@ -1,14 +1,18 @@
1
- import { useState as t } from "react";
2
- import { useInterval as m } from "../useInterval/useInterval.mjs";
3
- const i = () => {
4
- const e = performance && "memory" in performance, [r, o] = t({
5
- jsHeapSizeLimit: 0,
6
- totalJSHeapSize: 0,
7
- usedJSHeapSize: 0
8
- });
9
- return m(() => o(performance.memory), 1e3, { immediately: e }), { supported: e, value: r };
1
+ import { useState as o } from "react";
2
+ import { useInterval as t } from "../useInterval/useInterval.mjs";
3
+ const n = () => {
4
+ const e = performance && "memory" in performance && !!performance.memory, [r, m] = o(
5
+ (performance == null ? void 0 : performance.memory) ?? {
6
+ jsHeapSizeLimit: 0,
7
+ totalJSHeapSize: 0,
8
+ usedJSHeapSize: 0
9
+ }
10
+ );
11
+ return t(() => m(performance.memory), 1e3, {
12
+ immediately: e
13
+ }), { supported: e, value: r };
10
14
  };
11
15
  export {
12
- i as useMemory
16
+ n as useMemory
13
17
  };
14
18
  //# sourceMappingURL=useMemory.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"useMemory.mjs","sources":["../../../../src/hooks/useMemory/useMemory.ts"],"sourcesContent":["import { useState } from 'react';\n\nimport { useInterval } from '../useInterval/useInterval';\n\ndeclare global {\n interface Performance {\n memory: {\n readonly jsHeapSizeLimit: number;\n readonly totalJSHeapSize: number;\n readonly usedJSHeapSize: number;\n };\n }\n}\n\n/** The use memory return type */\nexport interface UseMemoryReturn {\n /** The memory supported status */\n supported: boolean;\n /** The current memory usage */\n value: Performance['memory'];\n}\n\n/**\n * @name useMemory\n * @description - Hook that gives you current memory usage\n * @category Browser\n *\n * @browserapi performance.memory https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory\n *\n * @returns {UseMemoryReturn} An object containing the current memory usage\n *\n * @example\n * const { supported, value } = useMemory();\n */\nexport const useMemory = (): UseMemoryReturn => {\n const supported = performance && 'memory' in performance;\n const [value, setValue] = useState<Performance['memory']>({\n jsHeapSizeLimit: 0,\n totalJSHeapSize: 0,\n usedJSHeapSize: 0\n });\n\n useInterval(() => setValue(performance.memory), 1000, { immediately: supported });\n\n return { supported, value };\n};\n"],"names":["useMemory","supported","value","setValue","useState","useInterval"],"mappings":";;AAkCO,MAAMA,IAAY,MAAuB;AACxC,QAAAC,IAAY,eAAe,YAAY,aACvC,CAACC,GAAOC,CAAQ,IAAIC,EAAgC;AAAA,IACxD,iBAAiB;AAAA,IACjB,iBAAiB;AAAA,IACjB,gBAAgB;AAAA,EAAA,CACjB;AAEW,SAAAC,EAAA,MAAMF,EAAS,YAAY,MAAM,GAAG,KAAM,EAAE,aAAaF,GAAW,GAEzE,EAAE,WAAAA,GAAW,OAAAC,EAAM;AAC5B;"}
1
+ {"version":3,"file":"useMemory.mjs","sources":["../../../../src/hooks/useMemory/useMemory.ts"],"sourcesContent":["import { useState } from 'react';\n\nimport { useInterval } from '../useInterval/useInterval';\n\ndeclare global {\n interface Performance {\n memory: {\n readonly jsHeapSizeLimit: number;\n readonly totalJSHeapSize: number;\n readonly usedJSHeapSize: number;\n };\n }\n}\n\n/** The use memory return type */\nexport interface UseMemoryReturn {\n /** The memory supported status */\n supported: boolean;\n /** The current memory usage */\n value: Performance['memory'];\n}\n\n/**\n * @name useMemory\n * @description - Hook that gives you current memory usage\n * @category Browser\n *\n * @browserapi performance.memory https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory\n *\n * @returns {UseMemoryReturn} An object containing the current memory usage\n *\n * @example\n * const { supported, value } = useMemory();\n */\nexport const useMemory = (): UseMemoryReturn => {\n const supported = performance && 'memory' in performance && !!performance.memory;\n const [value, setValue] = useState<Performance['memory']>(\n performance?.memory ?? {\n jsHeapSizeLimit: 0,\n totalJSHeapSize: 0,\n usedJSHeapSize: 0\n }\n );\n\n useInterval(() => setValue(performance.memory), 1000, {\n immediately: supported\n });\n\n return { supported, value };\n};\n"],"names":["useMemory","supported","value","setValue","useState","useInterval"],"mappings":";;AAkCO,MAAMA,IAAY,MAAuB;AAC9C,QAAMC,IAAY,eAAe,YAAY,eAAe,CAAC,CAAC,YAAY,QACpE,CAACC,GAAOC,CAAQ,IAAIC;AAAA,KACxB,2CAAa,WAAU;AAAA,MACrB,iBAAiB;AAAA,MACjB,iBAAiB;AAAA,MACjB,gBAAgB;AAAA,IAAA;AAAA,EAEpB;AAEA,SAAAC,EAAY,MAAMF,EAAS,YAAY,MAAM,GAAG,KAAM;AAAA,IACpD,aAAaF;AAAA,EAAA,CACd,GAEM,EAAE,WAAAA,GAAW,OAAAC,EAAM;AAC5B;"}
@@ -0,0 +1,63 @@
1
+ import { useState as u, useEffect as d } from "react";
2
+ const l = (a = "history") => {
3
+ const { search: i, hash: e } = window.location;
4
+ let r = "";
5
+ if (a === "history" && (r = i), a === "hash-params" && (r = e.replace(/^#/, "")), a === "hash") {
6
+ const s = e.indexOf("?");
7
+ r = ~s ? e.slice(s) : "";
8
+ }
9
+ return new URLSearchParams(r);
10
+ }, p = (a, i) => {
11
+ const e = a.toString(), { search: r, hash: t } = window.location;
12
+ if (i === "history") return `${e ? `?${e}` : ""}${t}`;
13
+ if (i === "hash-params")
14
+ return `${r}${e ? `#${e}` : ""}`;
15
+ if (i === "hash") {
16
+ const s = t.indexOf("?"), c = s > -1 ? t.slice(0, s) : t;
17
+ return `${r}${c}${e ? `?${e}` : ""}`;
18
+ }
19
+ throw new Error("Invalid mode");
20
+ }, S = (a, i, e = "replace") => {
21
+ const r = new URLSearchParams();
22
+ Object.entries(i).forEach(([s, c]) => {
23
+ Array.isArray(c) ? c.forEach((h) => r.set(s, String(h))) : r.set(s, String(c));
24
+ });
25
+ const t = p(r, a);
26
+ return e === "replace" && window.history.replaceState({}, "", t), e === "push" && window.history.pushState({}, "", t), r;
27
+ }, y = (a, i = {}) => {
28
+ const { mode: e = "history", write: r = "replace" } = i, t = (n) => typeof n == "string" ? t(new URLSearchParams(n)) : n instanceof URLSearchParams ? Array.from(n.entries()).reduce(
29
+ (o, [f, w]) => {
30
+ if (w === "undefined") return o;
31
+ try {
32
+ o[f] = JSON.parse(w);
33
+ } catch {
34
+ o[f] = w;
35
+ }
36
+ return o;
37
+ },
38
+ {}
39
+ ) : n, [s, c] = u(t(a ?? {})), h = (n, o = "replace") => {
40
+ const f = S(e, { ...s, ...n }, o ?? r);
41
+ c(t(f));
42
+ };
43
+ return d(() => {
44
+ h(s);
45
+ const n = () => {
46
+ const o = l(e);
47
+ h(t(o));
48
+ };
49
+ return window.addEventListener("popstate", n), e !== "history" && window.addEventListener("hashchange", n), () => {
50
+ window.removeEventListener("popstate", n), e !== "history" && window.removeEventListener("hashchange", n);
51
+ };
52
+ }, [e]), {
53
+ value: s,
54
+ set: h
55
+ };
56
+ };
57
+ export {
58
+ p as createQueryString,
59
+ l as getUrlSearchParams,
60
+ S as setUrlSearchParams,
61
+ y as useUrlSearchParams
62
+ };
63
+ //# sourceMappingURL=useUrlSearchParams.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUrlSearchParams.mjs","sources":["../../../../src/hooks/useUrlSearchParams/useUrlSearchParams.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nexport const getUrlSearchParams = (mode: UrlSearchParamsMode = 'history') => {\n const { search, hash } = window.location;\n\n let path = '';\n\n if (mode === 'history') path = search;\n if (mode === 'hash-params') path = hash.replace(/^#/, '');\n if (mode === 'hash') {\n const index = hash.indexOf('?');\n path = ~index ? hash.slice(index) : '';\n }\n\n const searchParams = new URLSearchParams(path);\n\n return searchParams;\n};\n\nexport const createQueryString = (searchParams: URLSearchParams, mode: UrlSearchParamsMode) => {\n const searchParamsString = searchParams.toString();\n const { search, hash } = window.location;\n\n if (mode === 'history') return `${searchParamsString ? `?${searchParamsString}` : ''}${hash}`;\n if (mode === 'hash-params')\n return `${search}${searchParamsString ? `#${searchParamsString}` : ''}`;\n\n if (mode === 'hash') {\n const index = hash.indexOf('?');\n const base = index > -1 ? hash.slice(0, index) : hash;\n\n return `${search}${base}${searchParamsString ? `?${searchParamsString}` : ''}`;\n }\n\n throw new Error('Invalid mode');\n};\n\nexport const setUrlSearchParams = <Params extends UrlParams>(\n mode: UrlSearchParamsMode,\n params: Partial<Params>,\n write: 'push' | 'replace' = 'replace'\n) => {\n const searchParams = new URLSearchParams();\n\n Object.entries(params).forEach(([key, param]) => {\n if (Array.isArray(param)) {\n param.forEach((value) => searchParams.set(key, String(value)));\n } else {\n searchParams.set(key, String(param));\n }\n });\n\n const query = createQueryString(searchParams, mode);\n if (write === 'replace') window.history.replaceState({}, '', query);\n if (write === 'push') window.history.pushState({}, '', query);\n\n return searchParams;\n};\n\n/** The url params type */\nexport type UrlParams = Record<string, any>;\n\n/** The url search params mod */\nexport type UrlSearchParamsMode = 'hash-params' | 'hash' | 'history';\n\n/** The use url search params set options type */\nexport interface UseUrlSearchParamsSetOptions {\n /** The mode to use for writing to the URL */\n write?: 'push' | 'replace';\n}\n\n/** The use url search params options type */\nexport interface UseUrlSearchParamsOptions {\n /** The mode to use for writing to the URL */\n mode?: UrlSearchParamsMode;\n /** The mode to use for writing to the URL */\n write?: 'push' | 'replace';\n}\n\n/** The use url search params return type */\nexport interface UseUrlSearchParamsReturn<Params extends UrlParams> {\n value: Params;\n set: (params: Partial<Params>) => void;\n}\n\n/**\n * @name useUrlSearchParams\n * @description - Hook that provides reactive URLSearchParams\n * @category Browser\n *\n * @overload\n * @template Value The type of the url param values\n * @param {UrlSearchParamsMode} mode The URL mode\n * @param {UseUrlSearchParamsOptions<Value>} [options] The URL mode\n * @returns {UseUrlSearchParamsReturn<Value>} The object with value and function for change value\n *\n * @example\n * const { value, set } = useUrlSearchParams('history');\n */\nexport const useUrlSearchParams = <\n Params extends UrlParams,\n SearchParams extends string | UrlParams | URLSearchParams = UrlParams\n>(\n initialValue?: SearchParams,\n options: UseUrlSearchParamsOptions = {}\n): UseUrlSearchParamsReturn<Params> => {\n const { mode = 'history', write: writeMode = 'replace' } = options;\n\n const deserializer = (searchParams: string | UrlParams | URLSearchParams) => {\n if (typeof searchParams === 'string') {\n return deserializer(new URLSearchParams(searchParams));\n }\n\n if (searchParams instanceof URLSearchParams) {\n return Array.from(searchParams.entries()).reduce(\n (acc, [key, value]) => {\n if (value === 'undefined') return acc;\n try {\n acc[key] = JSON.parse(value);\n } catch {\n acc[key] = value;\n }\n return acc;\n },\n {} as Record<string, any>\n );\n }\n\n return searchParams;\n };\n\n const [value, setValue] = useState(deserializer(initialValue ?? {}) as Params);\n\n const set = (params: Partial<Params>, write: 'push' | 'replace' = 'replace') => {\n const searchParams = setUrlSearchParams(mode, { ...value, ...params }, write ?? writeMode);\n setValue(deserializer(searchParams) as Params);\n };\n\n useEffect(() => {\n set(value);\n\n const onParamsChange = () => {\n const searchParams = getUrlSearchParams(mode);\n set(deserializer(searchParams) as Params);\n };\n\n window.addEventListener('popstate', onParamsChange);\n if (mode !== 'history') window.addEventListener('hashchange', onParamsChange);\n\n return () => {\n window.removeEventListener('popstate', onParamsChange);\n if (mode !== 'history') window.removeEventListener('hashchange', onParamsChange);\n };\n }, [mode]);\n\n return {\n value,\n set\n };\n};\n"],"names":["getUrlSearchParams","mode","search","hash","path","index","createQueryString","searchParams","searchParamsString","base","setUrlSearchParams","params","write","key","param","value","query","useUrlSearchParams","initialValue","options","writeMode","deserializer","acc","setValue","useState","set","useEffect","onParamsChange"],"mappings":";AAEa,MAAAA,IAAqB,CAACC,IAA4B,cAAc;AAC3E,QAAM,EAAE,QAAAC,GAAQ,MAAAC,EAAK,IAAI,OAAO;AAEhC,MAAIC,IAAO;AAIX,MAFIH,MAAS,cAAkBG,IAAAF,IAC3BD,MAAS,kBAAeG,IAAOD,EAAK,QAAQ,MAAM,EAAE,IACpDF,MAAS,QAAQ;AACb,UAAAI,IAAQF,EAAK,QAAQ,GAAG;AAC9B,IAAAC,IAAO,CAACC,IAAQF,EAAK,MAAME,CAAK,IAAI;AAAA,EAAA;AAK/B,SAFc,IAAI,gBAAgBD,CAAI;AAG/C,GAEaE,IAAoB,CAACC,GAA+BN,MAA8B;AACvF,QAAAO,IAAqBD,EAAa,SAAS,GAC3C,EAAE,QAAAL,GAAQ,MAAAC,EAAK,IAAI,OAAO;AAE5B,MAAAF,MAAS,UAAW,QAAO,GAAGO,IAAqB,IAAIA,CAAkB,KAAK,EAAE,GAAGL,CAAI;AAC3F,MAAIF,MAAS;AACX,WAAO,GAAGC,CAAM,GAAGM,IAAqB,IAAIA,CAAkB,KAAK,EAAE;AAEvE,MAAIP,MAAS,QAAQ;AACb,UAAAI,IAAQF,EAAK,QAAQ,GAAG,GACxBM,IAAOJ,IAAQ,KAAKF,EAAK,MAAM,GAAGE,CAAK,IAAIF;AAE1C,WAAA,GAAGD,CAAM,GAAGO,CAAI,GAAGD,IAAqB,IAAIA,CAAkB,KAAK,EAAE;AAAA,EAAA;AAGxE,QAAA,IAAI,MAAM,cAAc;AAChC,GAEaE,IAAqB,CAChCT,GACAU,GACAC,IAA4B,cACzB;AAC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