@contember/react-utils 1.3.0-alpha.8 → 1.3.0-rc.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 (90) hide show
  1. package/dist/development/context/createContext.cjs +10 -0
  2. package/dist/development/context/createContext.cjs.map +1 -0
  3. package/dist/development/context/createContext.js +10 -0
  4. package/dist/development/context/createContext.js.map +1 -0
  5. package/dist/development/context/{createNonNullableContextFactory.cjs → createRequiredContext.cjs} +6 -5
  6. package/dist/development/context/createRequiredContext.cjs.map +1 -0
  7. package/dist/development/context/{createNonNullableContextFactory.js → createRequiredContext.js} +6 -5
  8. package/dist/development/context/createRequiredContext.js.map +1 -0
  9. package/dist/development/hooks/useAutoHeightTextArea.cjs +1 -1
  10. package/dist/development/hooks/useAutoHeightTextArea.cjs.map +1 -1
  11. package/dist/development/hooks/useAutoHeightTextArea.js +1 -1
  12. package/dist/development/hooks/useAutoHeightTextArea.js.map +1 -1
  13. package/dist/development/hooks/useStoredState.cjs +89 -12
  14. package/dist/development/hooks/useStoredState.cjs.map +1 -1
  15. package/dist/development/hooks/useStoredState.js +91 -14
  16. package/dist/development/hooks/useStoredState.js.map +1 -1
  17. package/dist/development/index.cjs +11 -4
  18. package/dist/development/index.cjs.map +1 -1
  19. package/dist/development/index.js +12 -5
  20. package/dist/development/theming/contexts.cjs +2 -2
  21. package/dist/development/theming/contexts.cjs.map +1 -1
  22. package/dist/development/theming/contexts.js +2 -2
  23. package/dist/development/theming/contexts.js.map +1 -1
  24. package/dist/production/context/createContext.cjs +10 -0
  25. package/dist/production/context/createContext.cjs.map +1 -0
  26. package/dist/production/context/createContext.js +10 -0
  27. package/dist/production/context/createContext.js.map +1 -0
  28. package/dist/production/context/{createNonNullableContextFactory.cjs → createRequiredContext.cjs} +6 -5
  29. package/dist/production/context/createRequiredContext.cjs.map +1 -0
  30. package/dist/production/context/{createNonNullableContextFactory.js → createRequiredContext.js} +6 -5
  31. package/dist/production/context/createRequiredContext.js.map +1 -0
  32. package/dist/production/hooks/useAutoHeightTextArea.cjs +1 -1
  33. package/dist/production/hooks/useAutoHeightTextArea.cjs.map +1 -1
  34. package/dist/production/hooks/useAutoHeightTextArea.js +1 -1
  35. package/dist/production/hooks/useAutoHeightTextArea.js.map +1 -1
  36. package/dist/production/hooks/useStoredState.cjs +89 -12
  37. package/dist/production/hooks/useStoredState.cjs.map +1 -1
  38. package/dist/production/hooks/useStoredState.js +91 -14
  39. package/dist/production/hooks/useStoredState.js.map +1 -1
  40. package/dist/production/index.cjs +11 -4
  41. package/dist/production/index.cjs.map +1 -1
  42. package/dist/production/index.js +12 -5
  43. package/dist/production/theming/contexts.cjs +2 -2
  44. package/dist/production/theming/contexts.cjs.map +1 -1
  45. package/dist/production/theming/contexts.js +2 -2
  46. package/dist/production/theming/contexts.js.map +1 -1
  47. package/dist/types/context/createContext.d.ts +3 -0
  48. package/dist/types/context/createContext.d.ts.map +1 -0
  49. package/dist/types/context/createRequiredContext.d.ts +3 -0
  50. package/dist/types/context/createRequiredContext.d.ts.map +1 -0
  51. package/dist/types/context/index.d.ts +2 -2
  52. package/dist/types/context/index.d.ts.map +1 -1
  53. package/dist/types/hooks/useArrayMapMemo.d.ts.map +1 -1
  54. package/dist/types/hooks/useCloseOnClickOutside.d.ts +1 -1
  55. package/dist/types/hooks/useCloseOnClickOutside.d.ts.map +1 -1
  56. package/dist/types/hooks/useCloseOnEscape.d.ts.map +1 -1
  57. package/dist/types/hooks/useConstantLengthInvariant.d.ts.map +1 -1
  58. package/dist/types/hooks/useConstantValueInvariant.d.ts.map +1 -1
  59. package/dist/types/hooks/useDebounce.d.ts.map +1 -1
  60. package/dist/types/hooks/useObjectMemo.d.ts.map +1 -1
  61. package/dist/types/hooks/usePreviousValue.d.ts.map +1 -1
  62. package/dist/types/hooks/useStoredState.d.ts +14 -1
  63. package/dist/types/hooks/useStoredState.d.ts.map +1 -1
  64. package/dist/types/referentiallyStable/identityFunction.d.ts.map +1 -1
  65. package/dist/types/referentiallyStable/useReferentiallyStableCallback.d.ts.map +1 -1
  66. package/dist/types/tsconfig.tsbuildinfo +1 -1
  67. package/package.json +3 -3
  68. package/src/context/createContext.ts +9 -0
  69. package/src/context/{createNonNullableContextFactory.ts → createRequiredContext.ts} +6 -4
  70. package/src/context/index.ts +2 -2
  71. package/src/hooks/useAutoHeightTextArea.ts +1 -1
  72. package/src/hooks/useStoredState.ts +103 -14
  73. package/src/theming/contexts.ts +2 -2
  74. package/dist/development/context/createNonNullableContextFactory.cjs.map +0 -1
  75. package/dist/development/context/createNonNullableContextFactory.js.map +0 -1
  76. package/dist/development/context/createOptionalContextFactory.cjs +0 -13
  77. package/dist/development/context/createOptionalContextFactory.cjs.map +0 -1
  78. package/dist/development/context/createOptionalContextFactory.js +0 -13
  79. package/dist/development/context/createOptionalContextFactory.js.map +0 -1
  80. package/dist/production/context/createNonNullableContextFactory.cjs.map +0 -1
  81. package/dist/production/context/createNonNullableContextFactory.js.map +0 -1
  82. package/dist/production/context/createOptionalContextFactory.cjs +0 -13
  83. package/dist/production/context/createOptionalContextFactory.cjs.map +0 -1
  84. package/dist/production/context/createOptionalContextFactory.js +0 -13
  85. package/dist/production/context/createOptionalContextFactory.js.map +0 -1
  86. package/dist/types/context/createNonNullableContextFactory.d.ts +0 -3
  87. package/dist/types/context/createNonNullableContextFactory.d.ts.map +0 -1
  88. package/dist/types/context/createOptionalContextFactory.d.ts +0 -3
  89. package/dist/types/context/createOptionalContextFactory.d.ts.map +0 -1
  90. package/src/context/createOptionalContextFactory.ts +0 -13
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const react = require("react");
4
+ function createContext(name, initialValue) {
5
+ const OptionalContext = react.createContext(initialValue);
6
+ OptionalContext.displayName = name;
7
+ return [OptionalContext, () => react.useContext(OptionalContext)];
8
+ }
9
+ exports.createContext = createContext;
10
+ //# sourceMappingURL=createContext.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createContext.cjs","sources":["../../../src/context/createContext.ts"],"sourcesContent":["import type { Context } from 'react'\nimport { createContext as baseCreateContext, useContext } from 'react'\n\nexport function createContext<T>(name: string, initialValue: T): [Context<T>, () => T] {\n const OptionalContext = baseCreateContext<T>(initialValue)\n OptionalContext.displayName = name\n\n return [OptionalContext, () => useContext(OptionalContext)]\n}\n"],"names":["baseCreateContext","useContext"],"mappings":";;;AAGgB,SAAA,cAAiB,MAAc,cAAwC;AAC/E,QAAA,kBAAkBA,oBAAqB,YAAY;AACzD,kBAAgB,cAAc;AAE9B,SAAO,CAAC,iBAAiB,MAAMC,iBAAW,eAAe,CAAC;AAC5D;;"}
@@ -0,0 +1,10 @@
1
+ import { createContext as createContext$1, useContext } from "react";
2
+ function createContext(name, initialValue) {
3
+ const OptionalContext = createContext$1(initialValue);
4
+ OptionalContext.displayName = name;
5
+ return [OptionalContext, () => useContext(OptionalContext)];
6
+ }
7
+ export {
8
+ createContext
9
+ };
10
+ //# sourceMappingURL=createContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createContext.js","sources":["../../../src/context/createContext.ts"],"sourcesContent":["import type { Context } from 'react'\nimport { createContext as baseCreateContext, useContext } from 'react'\n\nexport function createContext<T>(name: string, initialValue: T): [Context<T>, () => T] {\n const OptionalContext = baseCreateContext<T>(initialValue)\n OptionalContext.displayName = name\n\n return [OptionalContext, () => useContext(OptionalContext)]\n}\n"],"names":["baseCreateContext"],"mappings":";AAGgB,SAAA,cAAiB,MAAc,cAAwC;AAC/E,QAAA,kBAAkBA,gBAAqB,YAAY;AACzD,kBAAgB,cAAc;AAE9B,SAAO,CAAC,iBAAiB,MAAM,WAAW,eAAe,CAAC;AAC5D;"}
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const react = require("react");
4
- function createNonNullableContextFactory(displayName, initialValue) {
5
- const RequiredContext = react.createContext(initialValue ?? null);
4
+ const EmptyContextSymbol = Symbol("EmptyContextSymbol");
5
+ function createRequiredContext(displayName) {
6
+ const RequiredContext = react.createContext(EmptyContextSymbol);
6
7
  RequiredContext.displayName = displayName;
7
8
  const useRequiredContext = () => {
8
9
  const context = react.useContext(RequiredContext);
9
- if (context == null) {
10
+ if (context === EmptyContextSymbol) {
10
11
  throw new Error(`use${displayName} must be used within a ${displayName} provider`);
11
12
  }
12
13
  return context;
13
14
  };
14
15
  return [RequiredContext, useRequiredContext];
15
16
  }
16
- exports.createNonNullableContextFactory = createNonNullableContextFactory;
17
- //# sourceMappingURL=createNonNullableContextFactory.cjs.map
17
+ exports.createRequiredContext = createRequiredContext;
18
+ //# sourceMappingURL=createRequiredContext.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRequiredContext.cjs","sources":["../../../src/context/createRequiredContext.ts"],"sourcesContent":["import type { Context } from 'react'\nimport { createContext, useContext } from 'react'\n\nconst EmptyContextSymbol = Symbol('EmptyContextSymbol')\n\nexport function createRequiredContext<T>(displayName: string): [Context<T>, () => T] {\n const RequiredContext = createContext<T>(EmptyContextSymbol as T)\n RequiredContext.displayName = displayName\n\n const useRequiredContext = (): T => {\n const context = useContext(RequiredContext)\n\n if (context === EmptyContextSymbol) {\n throw new Error(`use${displayName} must be used within a ${displayName} provider`)\n }\n\n return context\n }\n\n return [RequiredContext, useRequiredContext]\n}\n"],"names":["createContext","useContext"],"mappings":";;;AAGA,MAAM,qBAAqB,OAAO,oBAAoB;AAE/C,SAAS,sBAAyB,aAA4C;AAC7E,QAAA,kBAAkBA,oBAAiB,kBAAuB;AAChE,kBAAgB,cAAc;AAE9B,QAAM,qBAAqB,MAAS;AAC5B,UAAA,UAAUC,iBAAW,eAAe;AAE1C,QAAI,YAAY,oBAAoB;AAClC,YAAM,IAAI,MAAM,MAAM,WAAW,0BAA0B,WAAW,WAAW;AAAA,IACnF;AAEO,WAAA;AAAA,EAAA;AAGF,SAAA,CAAC,iBAAiB,kBAAkB;AAC7C;;"}
@@ -1,10 +1,11 @@
1
1
  import { createContext, useContext } from "react";
2
- function createNonNullableContextFactory(displayName, initialValue) {
3
- const RequiredContext = createContext(initialValue ?? null);
2
+ const EmptyContextSymbol = Symbol("EmptyContextSymbol");
3
+ function createRequiredContext(displayName) {
4
+ const RequiredContext = createContext(EmptyContextSymbol);
4
5
  RequiredContext.displayName = displayName;
5
6
  const useRequiredContext = () => {
6
7
  const context = useContext(RequiredContext);
7
- if (context == null) {
8
+ if (context === EmptyContextSymbol) {
8
9
  throw new Error(`use${displayName} must be used within a ${displayName} provider`);
9
10
  }
10
11
  return context;
@@ -12,6 +13,6 @@ function createNonNullableContextFactory(displayName, initialValue) {
12
13
  return [RequiredContext, useRequiredContext];
13
14
  }
14
15
  export {
15
- createNonNullableContextFactory
16
+ createRequiredContext
16
17
  };
17
- //# sourceMappingURL=createNonNullableContextFactory.js.map
18
+ //# sourceMappingURL=createRequiredContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRequiredContext.js","sources":["../../../src/context/createRequiredContext.ts"],"sourcesContent":["import type { Context } from 'react'\nimport { createContext, useContext } from 'react'\n\nconst EmptyContextSymbol = Symbol('EmptyContextSymbol')\n\nexport function createRequiredContext<T>(displayName: string): [Context<T>, () => T] {\n const RequiredContext = createContext<T>(EmptyContextSymbol as T)\n RequiredContext.displayName = displayName\n\n const useRequiredContext = (): T => {\n const context = useContext(RequiredContext)\n\n if (context === EmptyContextSymbol) {\n throw new Error(`use${displayName} must be used within a ${displayName} provider`)\n }\n\n return context\n }\n\n return [RequiredContext, useRequiredContext]\n}\n"],"names":[],"mappings":";AAGA,MAAM,qBAAqB,OAAO,oBAAoB;AAE/C,SAAS,sBAAyB,aAA4C;AAC7E,QAAA,kBAAkB,cAAiB,kBAAuB;AAChE,kBAAgB,cAAc;AAE9B,QAAM,qBAAqB,MAAS;AAC5B,UAAA,UAAU,WAAW,eAAe;AAE1C,QAAI,YAAY,oBAAoB;AAClC,YAAM,IAAI,MAAM,MAAM,WAAW,0BAA0B,WAAW,WAAW;AAAA,IACnF;AAEO,WAAA;AAAA,EAAA;AAGF,SAAA,CAAC,iBAAiB,kBAAkB;AAC7C;"}
@@ -12,7 +12,7 @@ const useAutoHeightTextArea = (textAreaRef, value, minRows, maxRows) => {
12
12
  ref.style.minHeight = "";
13
13
  ref.style.maxHeight = "";
14
14
  if (minRows2 !== maxRows2) {
15
- const height = ref.scrollHeight;
15
+ const height = ref.scrollHeight - ref.clientHeight + ref.offsetHeight;
16
16
  let minHeight;
17
17
  let maxHeight;
18
18
  ref.rows = minRows2;
@@ -1 +1 @@
1
- {"version":3,"file":"useAutoHeightTextArea.cjs","sources":["../../../src/hooks/useAutoHeightTextArea.ts"],"sourcesContent":["import { px } from '@contember/utilities'\nimport { useCallback, useLayoutEffect } from 'react'\nimport { RefObjectOrElement, unwrapRefValue } from './unwrapRefValue'\nimport { useOnElementResize } from './useOnElementResize'\n\nexport const useAutoHeightTextArea = (\n\ttextAreaRef: RefObjectOrElement<HTMLTextAreaElement>,\n\tvalue: string,\n\tminRows: number,\n\tmaxRows: number,\n) => {\n\tconst measure = useCallback(async (ref: HTMLTextAreaElement | null, minRows: number, maxRows: number) => {\n\t\tif (ref) {\n\t\t\tconst rowsAttribute = ref.rows\n\n\t\t\tref.style.height = ''\n\t\t\tref.style.minHeight = ''\n\t\t\tref.style.maxHeight = ''\n\n\t\t\tif (minRows !== maxRows) {\n\t\t\t\tconst height = ref.scrollHeight\n\t\t\t\tlet minHeight: number\n\t\t\t\tlet maxHeight: number\n\n\t\t\t\tref.rows = minRows\n\t\t\t\tminHeight = await ref.getBoundingClientRect().height\n\n\t\t\t\tif (maxRows === Infinity) {\n\t\t\t\t\tmaxHeight = Infinity\n\t\t\t\t} else {\n\t\t\t\t\tref.rows = maxRows\n\t\t\t\t\tmaxHeight = await ref.getBoundingClientRect().height\n\t\t\t\t}\n\n\t\t\t\tref.style.height = px(height)\n\t\t\t\tref.style.minHeight = px(minHeight)\n\t\t\t\tref.style.maxHeight = px(maxHeight)\n\t\t\t}\n\n\t\t\tref.rows = rowsAttribute\n\t\t}\n\t}, [])\n\n\tuseOnElementResize(textAreaRef, () => {\n\t\tmeasure(unwrapRefValue(textAreaRef), minRows, maxRows)\n\t})\n\n\tuseLayoutEffect(() => {\n\t\tmeasure(unwrapRefValue(textAreaRef), minRows, maxRows)\n\t}, [maxRows, measure, minRows, textAreaRef, value])\n}\n"],"names":["useCallback","minRows","maxRows","px","useOnElementResize","unwrapRefValue","useLayoutEffect"],"mappings":";;;;;;AAKO,MAAM,wBAAwB,CACpC,aACA,OACA,SACA,YACI;AACJ,QAAM,UAAUA,MAAA,YAAY,OAAO,KAAiCC,UAAiBC,aAAoB;AACxG,QAAI,KAAK;AACR,YAAM,gBAAgB,IAAI;AAE1B,UAAI,MAAM,SAAS;AACnB,UAAI,MAAM,YAAY;AACtB,UAAI,MAAM,YAAY;AAEtB,UAAID,aAAYC,UAAS;AACxB,cAAM,SAAS,IAAI;AACf,YAAA;AACA,YAAA;AAEJ,YAAI,OAAOD;AACC,oBAAA,MAAM,IAAI,sBAAA,EAAwB;AAE9C,YAAIC,aAAY,UAAU;AACb,sBAAA;AAAA,QAAA,OACN;AACN,cAAI,OAAOA;AACC,sBAAA,MAAM,IAAI,sBAAA,EAAwB;AAAA,QAC/C;AAEI,YAAA,MAAM,SAASC,UAAA,GAAG,MAAM;AACxB,YAAA,MAAM,YAAYA,UAAA,GAAG,SAAS;AAC9B,YAAA,MAAM,YAAYA,UAAA,GAAG,SAAS;AAAA,MACnC;AAEA,UAAI,OAAO;AAAA,IACZ;AAAA,EACD,GAAG,CAAE,CAAA;AAELC,qBAAA,mBAAmB,aAAa,MAAM;AACrC,YAAQC,eAAAA,eAAe,WAAW,GAAG,SAAS,OAAO;AAAA,EAAA,CACrD;AAEDC,QAAAA,gBAAgB,MAAM;AACrB,YAAQD,eAAAA,eAAe,WAAW,GAAG,SAAS,OAAO;AAAA,EAAA,GACnD,CAAC,SAAS,SAAS,SAAS,aAAa,KAAK,CAAC;AACnD;;"}
1
+ {"version":3,"file":"useAutoHeightTextArea.cjs","sources":["../../../src/hooks/useAutoHeightTextArea.ts"],"sourcesContent":["import { px } from '@contember/utilities'\nimport { useCallback, useLayoutEffect } from 'react'\nimport { RefObjectOrElement, unwrapRefValue } from './unwrapRefValue'\nimport { useOnElementResize } from './useOnElementResize'\n\nexport const useAutoHeightTextArea = (\n\ttextAreaRef: RefObjectOrElement<HTMLTextAreaElement>,\n\tvalue: string,\n\tminRows: number,\n\tmaxRows: number,\n) => {\n\tconst measure = useCallback(async (ref: HTMLTextAreaElement | null, minRows: number, maxRows: number) => {\n\t\tif (ref) {\n\t\t\tconst rowsAttribute = ref.rows\n\n\t\t\tref.style.height = ''\n\t\t\tref.style.minHeight = ''\n\t\t\tref.style.maxHeight = ''\n\n\t\t\tif (minRows !== maxRows) {\n\t\t\t\tconst height = ref.scrollHeight - ref.clientHeight + ref.offsetHeight\n\t\t\t\tlet minHeight: number\n\t\t\t\tlet maxHeight: number\n\n\t\t\t\tref.rows = minRows\n\t\t\t\tminHeight = await ref.getBoundingClientRect().height\n\n\t\t\t\tif (maxRows === Infinity) {\n\t\t\t\t\tmaxHeight = Infinity\n\t\t\t\t} else {\n\t\t\t\t\tref.rows = maxRows\n\t\t\t\t\tmaxHeight = await ref.getBoundingClientRect().height\n\t\t\t\t}\n\n\t\t\t\tref.style.height = px(height)\n\t\t\t\tref.style.minHeight = px(minHeight)\n\t\t\t\tref.style.maxHeight = px(maxHeight)\n\t\t\t}\n\n\t\t\tref.rows = rowsAttribute\n\t\t}\n\t}, [])\n\n\tuseOnElementResize(textAreaRef, () => {\n\t\tmeasure(unwrapRefValue(textAreaRef), minRows, maxRows)\n\t})\n\n\tuseLayoutEffect(() => {\n\t\tmeasure(unwrapRefValue(textAreaRef), minRows, maxRows)\n\t}, [maxRows, measure, minRows, textAreaRef, value])\n}\n"],"names":["useCallback","minRows","maxRows","px","useOnElementResize","unwrapRefValue","useLayoutEffect"],"mappings":";;;;;;AAKO,MAAM,wBAAwB,CACpC,aACA,OACA,SACA,YACI;AACJ,QAAM,UAAUA,MAAA,YAAY,OAAO,KAAiCC,UAAiBC,aAAoB;AACxG,QAAI,KAAK;AACR,YAAM,gBAAgB,IAAI;AAE1B,UAAI,MAAM,SAAS;AACnB,UAAI,MAAM,YAAY;AACtB,UAAI,MAAM,YAAY;AAEtB,UAAID,aAAYC,UAAS;AACxB,cAAM,SAAS,IAAI,eAAe,IAAI,eAAe,IAAI;AACrD,YAAA;AACA,YAAA;AAEJ,YAAI,OAAOD;AACC,oBAAA,MAAM,IAAI,sBAAA,EAAwB;AAE9C,YAAIC,aAAY,UAAU;AACb,sBAAA;AAAA,QAAA,OACN;AACN,cAAI,OAAOA;AACC,sBAAA,MAAM,IAAI,sBAAA,EAAwB;AAAA,QAC/C;AAEI,YAAA,MAAM,SAASC,UAAA,GAAG,MAAM;AACxB,YAAA,MAAM,YAAYA,UAAA,GAAG,SAAS;AAC9B,YAAA,MAAM,YAAYA,UAAA,GAAG,SAAS;AAAA,MACnC;AAEA,UAAI,OAAO;AAAA,IACZ;AAAA,EACD,GAAG,CAAE,CAAA;AAELC,qBAAA,mBAAmB,aAAa,MAAM;AACrC,YAAQC,eAAAA,eAAe,WAAW,GAAG,SAAS,OAAO;AAAA,EAAA,CACrD;AAEDC,QAAAA,gBAAgB,MAAM;AACrB,YAAQD,eAAAA,eAAe,WAAW,GAAG,SAAS,OAAO;AAAA,EAAA,GACnD,CAAC,SAAS,SAAS,SAAS,aAAa,KAAK,CAAC;AACnD;;"}
@@ -10,7 +10,7 @@ const useAutoHeightTextArea = (textAreaRef, value, minRows, maxRows) => {
10
10
  ref.style.minHeight = "";
11
11
  ref.style.maxHeight = "";
12
12
  if (minRows2 !== maxRows2) {
13
- const height = ref.scrollHeight;
13
+ const height = ref.scrollHeight - ref.clientHeight + ref.offsetHeight;
14
14
  let minHeight;
15
15
  let maxHeight;
16
16
  ref.rows = minRows2;
@@ -1 +1 @@
1
- {"version":3,"file":"useAutoHeightTextArea.js","sources":["../../../src/hooks/useAutoHeightTextArea.ts"],"sourcesContent":["import { px } from '@contember/utilities'\nimport { useCallback, useLayoutEffect } from 'react'\nimport { RefObjectOrElement, unwrapRefValue } from './unwrapRefValue'\nimport { useOnElementResize } from './useOnElementResize'\n\nexport const useAutoHeightTextArea = (\n\ttextAreaRef: RefObjectOrElement<HTMLTextAreaElement>,\n\tvalue: string,\n\tminRows: number,\n\tmaxRows: number,\n) => {\n\tconst measure = useCallback(async (ref: HTMLTextAreaElement | null, minRows: number, maxRows: number) => {\n\t\tif (ref) {\n\t\t\tconst rowsAttribute = ref.rows\n\n\t\t\tref.style.height = ''\n\t\t\tref.style.minHeight = ''\n\t\t\tref.style.maxHeight = ''\n\n\t\t\tif (minRows !== maxRows) {\n\t\t\t\tconst height = ref.scrollHeight\n\t\t\t\tlet minHeight: number\n\t\t\t\tlet maxHeight: number\n\n\t\t\t\tref.rows = minRows\n\t\t\t\tminHeight = await ref.getBoundingClientRect().height\n\n\t\t\t\tif (maxRows === Infinity) {\n\t\t\t\t\tmaxHeight = Infinity\n\t\t\t\t} else {\n\t\t\t\t\tref.rows = maxRows\n\t\t\t\t\tmaxHeight = await ref.getBoundingClientRect().height\n\t\t\t\t}\n\n\t\t\t\tref.style.height = px(height)\n\t\t\t\tref.style.minHeight = px(minHeight)\n\t\t\t\tref.style.maxHeight = px(maxHeight)\n\t\t\t}\n\n\t\t\tref.rows = rowsAttribute\n\t\t}\n\t}, [])\n\n\tuseOnElementResize(textAreaRef, () => {\n\t\tmeasure(unwrapRefValue(textAreaRef), minRows, maxRows)\n\t})\n\n\tuseLayoutEffect(() => {\n\t\tmeasure(unwrapRefValue(textAreaRef), minRows, maxRows)\n\t}, [maxRows, measure, minRows, textAreaRef, value])\n}\n"],"names":["minRows","maxRows"],"mappings":";;;;AAKO,MAAM,wBAAwB,CACpC,aACA,OACA,SACA,YACI;AACJ,QAAM,UAAU,YAAY,OAAO,KAAiCA,UAAiBC,aAAoB;AACxG,QAAI,KAAK;AACR,YAAM,gBAAgB,IAAI;AAE1B,UAAI,MAAM,SAAS;AACnB,UAAI,MAAM,YAAY;AACtB,UAAI,MAAM,YAAY;AAEtB,UAAID,aAAYC,UAAS;AACxB,cAAM,SAAS,IAAI;AACf,YAAA;AACA,YAAA;AAEJ,YAAI,OAAOD;AACC,oBAAA,MAAM,IAAI,sBAAA,EAAwB;AAE9C,YAAIC,aAAY,UAAU;AACb,sBAAA;AAAA,QAAA,OACN;AACN,cAAI,OAAOA;AACC,sBAAA,MAAM,IAAI,sBAAA,EAAwB;AAAA,QAC/C;AAEI,YAAA,MAAM,SAAS,GAAG,MAAM;AACxB,YAAA,MAAM,YAAY,GAAG,SAAS;AAC9B,YAAA,MAAM,YAAY,GAAG,SAAS;AAAA,MACnC;AAEA,UAAI,OAAO;AAAA,IACZ;AAAA,EACD,GAAG,CAAE,CAAA;AAEL,qBAAmB,aAAa,MAAM;AACrC,YAAQ,eAAe,WAAW,GAAG,SAAS,OAAO;AAAA,EAAA,CACrD;AAED,kBAAgB,MAAM;AACrB,YAAQ,eAAe,WAAW,GAAG,SAAS,OAAO;AAAA,EAAA,GACnD,CAAC,SAAS,SAAS,SAAS,aAAa,KAAK,CAAC;AACnD;"}
1
+ {"version":3,"file":"useAutoHeightTextArea.js","sources":["../../../src/hooks/useAutoHeightTextArea.ts"],"sourcesContent":["import { px } from '@contember/utilities'\nimport { useCallback, useLayoutEffect } from 'react'\nimport { RefObjectOrElement, unwrapRefValue } from './unwrapRefValue'\nimport { useOnElementResize } from './useOnElementResize'\n\nexport const useAutoHeightTextArea = (\n\ttextAreaRef: RefObjectOrElement<HTMLTextAreaElement>,\n\tvalue: string,\n\tminRows: number,\n\tmaxRows: number,\n) => {\n\tconst measure = useCallback(async (ref: HTMLTextAreaElement | null, minRows: number, maxRows: number) => {\n\t\tif (ref) {\n\t\t\tconst rowsAttribute = ref.rows\n\n\t\t\tref.style.height = ''\n\t\t\tref.style.minHeight = ''\n\t\t\tref.style.maxHeight = ''\n\n\t\t\tif (minRows !== maxRows) {\n\t\t\t\tconst height = ref.scrollHeight - ref.clientHeight + ref.offsetHeight\n\t\t\t\tlet minHeight: number\n\t\t\t\tlet maxHeight: number\n\n\t\t\t\tref.rows = minRows\n\t\t\t\tminHeight = await ref.getBoundingClientRect().height\n\n\t\t\t\tif (maxRows === Infinity) {\n\t\t\t\t\tmaxHeight = Infinity\n\t\t\t\t} else {\n\t\t\t\t\tref.rows = maxRows\n\t\t\t\t\tmaxHeight = await ref.getBoundingClientRect().height\n\t\t\t\t}\n\n\t\t\t\tref.style.height = px(height)\n\t\t\t\tref.style.minHeight = px(minHeight)\n\t\t\t\tref.style.maxHeight = px(maxHeight)\n\t\t\t}\n\n\t\t\tref.rows = rowsAttribute\n\t\t}\n\t}, [])\n\n\tuseOnElementResize(textAreaRef, () => {\n\t\tmeasure(unwrapRefValue(textAreaRef), minRows, maxRows)\n\t})\n\n\tuseLayoutEffect(() => {\n\t\tmeasure(unwrapRefValue(textAreaRef), minRows, maxRows)\n\t}, [maxRows, measure, minRows, textAreaRef, value])\n}\n"],"names":["minRows","maxRows"],"mappings":";;;;AAKO,MAAM,wBAAwB,CACpC,aACA,OACA,SACA,YACI;AACJ,QAAM,UAAU,YAAY,OAAO,KAAiCA,UAAiBC,aAAoB;AACxG,QAAI,KAAK;AACR,YAAM,gBAAgB,IAAI;AAE1B,UAAI,MAAM,SAAS;AACnB,UAAI,MAAM,YAAY;AACtB,UAAI,MAAM,YAAY;AAEtB,UAAID,aAAYC,UAAS;AACxB,cAAM,SAAS,IAAI,eAAe,IAAI,eAAe,IAAI;AACrD,YAAA;AACA,YAAA;AAEJ,YAAI,OAAOD;AACC,oBAAA,MAAM,IAAI,sBAAA,EAAwB;AAE9C,YAAIC,aAAY,UAAU;AACb,sBAAA;AAAA,QAAA,OACN;AACN,cAAI,OAAOA;AACC,sBAAA,MAAM,IAAI,sBAAA,EAAwB;AAAA,QAC/C;AAEI,YAAA,MAAM,SAAS,GAAG,MAAM;AACxB,YAAA,MAAM,YAAY,GAAG,SAAS;AAC9B,YAAA,MAAM,YAAY,GAAG,SAAS;AAAA,MACnC;AAEA,UAAI,OAAO;AAAA,IACZ;AAAA,EACD,GAAG,CAAE,CAAA;AAEL,qBAAmB,aAAa,MAAM;AACrC,YAAQ,eAAe,WAAW,GAAG,SAAS,OAAO;AAAA,EAAA,CACrD;AAED,kBAAgB,MAAM;AACrB,YAAQ,eAAe,WAAW,GAAG,SAAS,OAAO;AAAA,EAAA,GACnD,CAAC,SAAS,SAAS,SAAS,aAAa,KAAK,CAAC;AACnD;"}
@@ -1,23 +1,100 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const react = require("react");
4
- const createStoredStateHook = (getStorage) => {
5
- return (key, initializeValue) => {
6
- const storage = getStorage();
7
- const [value, setValue] = react.useState(() => {
8
- const value2 = storage.getItem(key);
9
- const parsedValue = value2 !== null ? JSON.parse(value2) : void 0;
10
- return initializeValue(parsedValue);
11
- });
12
- return [value, react.useCallback((value2) => {
4
+ const urlStateStorage = {
5
+ getItem(key) {
6
+ const searchParams = new URLSearchParams(window.location.search);
7
+ const value = searchParams.get(key[1]);
8
+ return value !== null && value !== void 0 ? JSON.parse(value) : null;
9
+ },
10
+ setItem(key, value) {
11
+ const searchParams = new URLSearchParams(window.location.search);
12
+ searchParams.set(key[1], JSON.stringify(value));
13
+ const newUrl = `${window.location.pathname}?${searchParams.toString()}`;
14
+ window.history.replaceState({}, "", newUrl);
15
+ }
16
+ };
17
+ const createStateStorageFromNativeStorage = (getStorage) => {
18
+ return {
19
+ getItem(key) {
20
+ const value = getStorage().getItem(key.join("-"));
21
+ return value !== null ? JSON.parse(value) : null;
22
+ },
23
+ setItem(key, value) {
24
+ getStorage().setItem(key.join("-"), JSON.stringify(value));
25
+ }
26
+ };
27
+ };
28
+ const sessionStateStorage = createStateStorageFromNativeStorage(() => sessionStorage);
29
+ const localStateStorage = createStateStorageFromNativeStorage(() => localStorage);
30
+ const nullStorage = {
31
+ getItem() {
32
+ return null;
33
+ },
34
+ setItem() {
35
+ }
36
+ };
37
+ const builtInStorages = {
38
+ url: urlStateStorage,
39
+ session: sessionStateStorage,
40
+ local: localStateStorage,
41
+ null: nullStorage
42
+ };
43
+ const useStoredState = (storageOrName, key, initializeValue) => {
44
+ const storage = react.useMemo(() => {
45
+ return getStateStorage(storageOrName);
46
+ }, [storageOrName]);
47
+ const [value, setValue] = react.useState(() => {
48
+ const value2 = storage.getItem(key);
49
+ return initializeValue(value2);
50
+ });
51
+ return [
52
+ value,
53
+ react.useCallback((value2) => {
13
54
  setValue((current) => {
14
55
  const newValue = typeof value2 === "function" ? value2(current) : value2;
15
- storage.setItem(key, JSON.stringify(newValue));
56
+ storage.setItem(key, newValue);
16
57
  return newValue;
17
58
  });
18
- }, [key, storage])];
59
+ }, [key, storage])
60
+ ];
61
+ };
62
+ const useSessionStorageState = (key, initializeValue) => {
63
+ return useStoredState(sessionStateStorage, key, initializeValue);
64
+ };
65
+ const useLocalStorageState = (key, initializeValue) => {
66
+ return useStoredState(localStateStorage, key, initializeValue);
67
+ };
68
+ const getStateStorage = (storageOrName) => {
69
+ if (Array.isArray(storageOrName)) {
70
+ return createFallbackStorage(storageOrName.map((it) => typeof it === "string" ? builtInStorages[it] : it));
71
+ }
72
+ return typeof storageOrName === "string" ? builtInStorages[storageOrName] : storageOrName;
73
+ };
74
+ const createFallbackStorage = (storageTypes) => {
75
+ return {
76
+ getItem(key) {
77
+ for (const storage of storageTypes) {
78
+ const value = storage.getItem(key);
79
+ if (value !== null) {
80
+ return value;
81
+ }
82
+ }
83
+ return null;
84
+ },
85
+ setItem(key, value) {
86
+ for (const storage of storageTypes) {
87
+ storage.setItem(key, value);
88
+ }
89
+ }
19
90
  };
20
91
  };
21
- const useSessionStorageState = createStoredStateHook(() => sessionStorage);
92
+ exports.getStateStorage = getStateStorage;
93
+ exports.localStateStorage = localStateStorage;
94
+ exports.nullStorage = nullStorage;
95
+ exports.sessionStateStorage = sessionStateStorage;
96
+ exports.urlStateStorage = urlStateStorage;
97
+ exports.useLocalStorageState = useLocalStorageState;
22
98
  exports.useSessionStorageState = useSessionStorageState;
99
+ exports.useStoredState = useStoredState;
23
100
  //# sourceMappingURL=useStoredState.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"useStoredState.cjs","sources":["../../../src/hooks/useStoredState.ts"],"sourcesContent":["import { useCallback, useState } from 'react'\nimport { Serializable } from '../types'\n\nexport type SetState<V extends Serializable> = (value: V | ((current: V) => V)) => void;\nexport type ValueInitializer<V extends Serializable> = (storedValue: V | undefined) => V;\n\nconst createStoredStateHook = (getStorage: () => Storage) => {\n\treturn <V extends Serializable>(key: string, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\t\tconst storage = getStorage()\n\t\tconst [value, setValue] = useState<V>(() => {\n\t\t\tconst value = storage.getItem(key)\n\t\t\tconst parsedValue: V | undefined = value !== null ? JSON.parse(value) : undefined\n\t\t\treturn initializeValue(parsedValue)\n\t\t})\n\t\treturn [value, useCallback(value => {\n\t\t\tsetValue(current => {\n\t\t\t\tconst newValue = typeof value === 'function' ? (value as (current: V) => V)(current) : value\n\t\t\t\tstorage.setItem(key, JSON.stringify(newValue))\n\t\t\t\treturn newValue\n\t\t\t})\n\t\t}, [key, storage])]\n\t}\n\n}\n\nexport const useSessionStorageState = createStoredStateHook(() => sessionStorage)\n"],"names":["useState","value","useCallback"],"mappings":";;;AAMA,MAAM,wBAAwB,CAAC,eAA8B;AACrD,SAAA,CAAyB,KAAa,oBAA2D;AACvG,UAAM,UAAU;AAChB,UAAM,CAAC,OAAO,QAAQ,IAAIA,eAAY,MAAM;AACrCC,YAAAA,SAAQ,QAAQ,QAAQ,GAAG;AACjC,YAAM,cAA6BA,WAAU,OAAO,KAAK,MAAMA,MAAK,IAAI;AACxE,aAAO,gBAAgB,WAAW;AAAA,IAAA,CAClC;AACD,WAAO,CAAC,OAAOC,kBAAY,CAAAD,WAAS;AACnC,eAAS,CAAW,YAAA;AACnB,cAAM,WAAW,OAAOA,WAAU,aAAcA,OAA4B,OAAO,IAAIA;AACvF,gBAAQ,QAAQ,KAAK,KAAK,UAAU,QAAQ,CAAC;AACtC,eAAA;AAAA,MAAA,CACP;AAAA,IACC,GAAA,CAAC,KAAK,OAAO,CAAC,CAAC;AAAA,EAAA;AAGpB;AAEa,MAAA,yBAAyB,sBAAsB,MAAM,cAAc;;"}
1
+ {"version":3,"file":"useStoredState.cjs","sources":["../../../src/hooks/useStoredState.ts"],"sourcesContent":["import { useCallback, useMemo, useState } from 'react'\nimport { Serializable } from '../types'\n\nexport type SetState<V extends Serializable> = (value: V | ((current: V) => V)) => void;\nexport type StateStorageKey = [uniquePrefix: string, key: string]\n\nexport type ValueInitializer<V extends Serializable> = (storedValue: V | undefined) => V;\n\nexport interface StateStorage {\n\tgetItem(key: StateStorageKey): Serializable\n\n\tsetItem(key: StateStorageKey, value: Serializable): void\n}\n\n\nexport const urlStateStorage: StateStorage = {\n\tgetItem(key) {\n\t\tconst searchParams = new URLSearchParams(window.location.search)\n\t\tconst value = searchParams.get(key[1])\n\t\treturn value !== null && value !== undefined ? JSON.parse(value) : null\n\t},\n\tsetItem(key, value) {\n\t\tconst searchParams = new URLSearchParams(window.location.search)\n\t\tsearchParams.set(key[1], JSON.stringify(value))\n\t\tconst newUrl = `${window.location.pathname}?${searchParams.toString()}`\n\t\twindow.history.replaceState({}, '', newUrl)\n\t},\n}\n\nconst createStateStorageFromNativeStorage = (getStorage: () => Storage): StateStorage => {\n\treturn {\n\t\tgetItem(key) {\n\t\t\tconst value = getStorage().getItem(key.join('-'))\n\t\t\treturn value !== null ? JSON.parse(value) : null\n\t\t},\n\t\tsetItem(key, value) {\n\t\t\tgetStorage().setItem(key.join('-'), JSON.stringify(value))\n\t\t},\n\t}\n}\n\nexport const sessionStateStorage = createStateStorageFromNativeStorage(() => sessionStorage)\nexport const localStateStorage = createStateStorageFromNativeStorage(() => localStorage)\nexport const nullStorage: StateStorage = {\n\tgetItem() {\n\t\treturn null\n\t},\n\tsetItem() {\n\t},\n}\n\nconst builtInStorages = {\n\turl: urlStateStorage,\n\tsession: sessionStateStorage,\n\tlocal: localStateStorage,\n\tnull: nullStorage,\n}\n\nexport type StateStorageOrName = StateStorage | 'url' | 'session' | 'local' | 'null'\n\nexport const useStoredState = <V extends Serializable>(storageOrName: StateStorageOrName | StateStorageOrName[], key: StateStorageKey, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\tconst storage = useMemo(() => {\n\t\treturn getStateStorage(storageOrName)\n\t}, [storageOrName])\n\n\tconst [value, setValue] = useState<V>(() => {\n\t\tconst value = storage.getItem(key)\n\t\treturn initializeValue(value as V)\n\t})\n\treturn [\n\t\tvalue,\n\t\tuseCallback(value => {\n\t\t\tsetValue(current => {\n\t\t\t\tconst newValue = typeof value === 'function' ? (value as (current: V) => V)(current) : value\n\t\t\t\tstorage.setItem(key, newValue)\n\t\t\t\treturn newValue\n\t\t\t})\n\t\t}, [key, storage]),\n\t]\n}\n\nexport const useSessionStorageState = <V extends Serializable>(key: StateStorageKey, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\treturn useStoredState<V>(sessionStateStorage, key, initializeValue)\n}\n\nexport const useLocalStorageState = <V extends Serializable>(key: StateStorageKey, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\treturn useStoredState<V>(localStateStorage, key, initializeValue)\n}\n\nexport const getStateStorage = (storageOrName: StateStorageOrName | StateStorageOrName[]) => {\n\tif (Array.isArray(storageOrName)) {\n\t\treturn createFallbackStorage(storageOrName.map(it => typeof it === 'string' ? builtInStorages[it] : it))\n\t}\n\treturn typeof storageOrName === 'string' ? builtInStorages[storageOrName] : storageOrName\n}\n\n\nconst createFallbackStorage = (storageTypes: StateStorage[]): StateStorage => {\n\treturn {\n\t\tgetItem(key) {\n\t\t\tfor (const storage of storageTypes) {\n\t\t\t\tconst value = storage.getItem(key)\n\t\t\t\tif (value !== null) {\n\t\t\t\t\treturn value\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn null\n\t\t},\n\t\tsetItem(key, value) {\n\t\t\tfor (const storage of storageTypes) {\n\t\t\t\tstorage.setItem(key, value)\n\t\t\t}\n\t\t},\n\t}\n}\n"],"names":["useMemo","useState","value","useCallback"],"mappings":"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"}
@@ -1,23 +1,100 @@
1
- import { useState, useCallback } from "react";
2
- const createStoredStateHook = (getStorage) => {
3
- return (key, initializeValue) => {
4
- const storage = getStorage();
5
- const [value, setValue] = useState(() => {
6
- const value2 = storage.getItem(key);
7
- const parsedValue = value2 !== null ? JSON.parse(value2) : void 0;
8
- return initializeValue(parsedValue);
9
- });
10
- return [value, useCallback((value2) => {
1
+ import { useMemo, useState, useCallback } from "react";
2
+ const urlStateStorage = {
3
+ getItem(key) {
4
+ const searchParams = new URLSearchParams(window.location.search);
5
+ const value = searchParams.get(key[1]);
6
+ return value !== null && value !== void 0 ? JSON.parse(value) : null;
7
+ },
8
+ setItem(key, value) {
9
+ const searchParams = new URLSearchParams(window.location.search);
10
+ searchParams.set(key[1], JSON.stringify(value));
11
+ const newUrl = `${window.location.pathname}?${searchParams.toString()}`;
12
+ window.history.replaceState({}, "", newUrl);
13
+ }
14
+ };
15
+ const createStateStorageFromNativeStorage = (getStorage) => {
16
+ return {
17
+ getItem(key) {
18
+ const value = getStorage().getItem(key.join("-"));
19
+ return value !== null ? JSON.parse(value) : null;
20
+ },
21
+ setItem(key, value) {
22
+ getStorage().setItem(key.join("-"), JSON.stringify(value));
23
+ }
24
+ };
25
+ };
26
+ const sessionStateStorage = createStateStorageFromNativeStorage(() => sessionStorage);
27
+ const localStateStorage = createStateStorageFromNativeStorage(() => localStorage);
28
+ const nullStorage = {
29
+ getItem() {
30
+ return null;
31
+ },
32
+ setItem() {
33
+ }
34
+ };
35
+ const builtInStorages = {
36
+ url: urlStateStorage,
37
+ session: sessionStateStorage,
38
+ local: localStateStorage,
39
+ null: nullStorage
40
+ };
41
+ const useStoredState = (storageOrName, key, initializeValue) => {
42
+ const storage = useMemo(() => {
43
+ return getStateStorage(storageOrName);
44
+ }, [storageOrName]);
45
+ const [value, setValue] = useState(() => {
46
+ const value2 = storage.getItem(key);
47
+ return initializeValue(value2);
48
+ });
49
+ return [
50
+ value,
51
+ useCallback((value2) => {
11
52
  setValue((current) => {
12
53
  const newValue = typeof value2 === "function" ? value2(current) : value2;
13
- storage.setItem(key, JSON.stringify(newValue));
54
+ storage.setItem(key, newValue);
14
55
  return newValue;
15
56
  });
16
- }, [key, storage])];
57
+ }, [key, storage])
58
+ ];
59
+ };
60
+ const useSessionStorageState = (key, initializeValue) => {
61
+ return useStoredState(sessionStateStorage, key, initializeValue);
62
+ };
63
+ const useLocalStorageState = (key, initializeValue) => {
64
+ return useStoredState(localStateStorage, key, initializeValue);
65
+ };
66
+ const getStateStorage = (storageOrName) => {
67
+ if (Array.isArray(storageOrName)) {
68
+ return createFallbackStorage(storageOrName.map((it) => typeof it === "string" ? builtInStorages[it] : it));
69
+ }
70
+ return typeof storageOrName === "string" ? builtInStorages[storageOrName] : storageOrName;
71
+ };
72
+ const createFallbackStorage = (storageTypes) => {
73
+ return {
74
+ getItem(key) {
75
+ for (const storage of storageTypes) {
76
+ const value = storage.getItem(key);
77
+ if (value !== null) {
78
+ return value;
79
+ }
80
+ }
81
+ return null;
82
+ },
83
+ setItem(key, value) {
84
+ for (const storage of storageTypes) {
85
+ storage.setItem(key, value);
86
+ }
87
+ }
17
88
  };
18
89
  };
19
- const useSessionStorageState = createStoredStateHook(() => sessionStorage);
20
90
  export {
21
- useSessionStorageState
91
+ getStateStorage,
92
+ localStateStorage,
93
+ nullStorage,
94
+ sessionStateStorage,
95
+ urlStateStorage,
96
+ useLocalStorageState,
97
+ useSessionStorageState,
98
+ useStoredState
22
99
  };
23
100
  //# sourceMappingURL=useStoredState.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useStoredState.js","sources":["../../../src/hooks/useStoredState.ts"],"sourcesContent":["import { useCallback, useState } from 'react'\nimport { Serializable } from '../types'\n\nexport type SetState<V extends Serializable> = (value: V | ((current: V) => V)) => void;\nexport type ValueInitializer<V extends Serializable> = (storedValue: V | undefined) => V;\n\nconst createStoredStateHook = (getStorage: () => Storage) => {\n\treturn <V extends Serializable>(key: string, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\t\tconst storage = getStorage()\n\t\tconst [value, setValue] = useState<V>(() => {\n\t\t\tconst value = storage.getItem(key)\n\t\t\tconst parsedValue: V | undefined = value !== null ? JSON.parse(value) : undefined\n\t\t\treturn initializeValue(parsedValue)\n\t\t})\n\t\treturn [value, useCallback(value => {\n\t\t\tsetValue(current => {\n\t\t\t\tconst newValue = typeof value === 'function' ? (value as (current: V) => V)(current) : value\n\t\t\t\tstorage.setItem(key, JSON.stringify(newValue))\n\t\t\t\treturn newValue\n\t\t\t})\n\t\t}, [key, storage])]\n\t}\n\n}\n\nexport const useSessionStorageState = createStoredStateHook(() => sessionStorage)\n"],"names":["value"],"mappings":";AAMA,MAAM,wBAAwB,CAAC,eAA8B;AACrD,SAAA,CAAyB,KAAa,oBAA2D;AACvG,UAAM,UAAU;AAChB,UAAM,CAAC,OAAO,QAAQ,IAAI,SAAY,MAAM;AACrCA,YAAAA,SAAQ,QAAQ,QAAQ,GAAG;AACjC,YAAM,cAA6BA,WAAU,OAAO,KAAK,MAAMA,MAAK,IAAI;AACxE,aAAO,gBAAgB,WAAW;AAAA,IAAA,CAClC;AACD,WAAO,CAAC,OAAO,YAAY,CAAAA,WAAS;AACnC,eAAS,CAAW,YAAA;AACnB,cAAM,WAAW,OAAOA,WAAU,aAAcA,OAA4B,OAAO,IAAIA;AACvF,gBAAQ,QAAQ,KAAK,KAAK,UAAU,QAAQ,CAAC;AACtC,eAAA;AAAA,MAAA,CACP;AAAA,IACC,GAAA,CAAC,KAAK,OAAO,CAAC,CAAC;AAAA,EAAA;AAGpB;AAEa,MAAA,yBAAyB,sBAAsB,MAAM,cAAc;"}
1
+ {"version":3,"file":"useStoredState.js","sources":["../../../src/hooks/useStoredState.ts"],"sourcesContent":["import { useCallback, useMemo, useState } from 'react'\nimport { Serializable } from '../types'\n\nexport type SetState<V extends Serializable> = (value: V | ((current: V) => V)) => void;\nexport type StateStorageKey = [uniquePrefix: string, key: string]\n\nexport type ValueInitializer<V extends Serializable> = (storedValue: V | undefined) => V;\n\nexport interface StateStorage {\n\tgetItem(key: StateStorageKey): Serializable\n\n\tsetItem(key: StateStorageKey, value: Serializable): void\n}\n\n\nexport const urlStateStorage: StateStorage = {\n\tgetItem(key) {\n\t\tconst searchParams = new URLSearchParams(window.location.search)\n\t\tconst value = searchParams.get(key[1])\n\t\treturn value !== null && value !== undefined ? JSON.parse(value) : null\n\t},\n\tsetItem(key, value) {\n\t\tconst searchParams = new URLSearchParams(window.location.search)\n\t\tsearchParams.set(key[1], JSON.stringify(value))\n\t\tconst newUrl = `${window.location.pathname}?${searchParams.toString()}`\n\t\twindow.history.replaceState({}, '', newUrl)\n\t},\n}\n\nconst createStateStorageFromNativeStorage = (getStorage: () => Storage): StateStorage => {\n\treturn {\n\t\tgetItem(key) {\n\t\t\tconst value = getStorage().getItem(key.join('-'))\n\t\t\treturn value !== null ? JSON.parse(value) : null\n\t\t},\n\t\tsetItem(key, value) {\n\t\t\tgetStorage().setItem(key.join('-'), JSON.stringify(value))\n\t\t},\n\t}\n}\n\nexport const sessionStateStorage = createStateStorageFromNativeStorage(() => sessionStorage)\nexport const localStateStorage = createStateStorageFromNativeStorage(() => localStorage)\nexport const nullStorage: StateStorage = {\n\tgetItem() {\n\t\treturn null\n\t},\n\tsetItem() {\n\t},\n}\n\nconst builtInStorages = {\n\turl: urlStateStorage,\n\tsession: sessionStateStorage,\n\tlocal: localStateStorage,\n\tnull: nullStorage,\n}\n\nexport type StateStorageOrName = StateStorage | 'url' | 'session' | 'local' | 'null'\n\nexport const useStoredState = <V extends Serializable>(storageOrName: StateStorageOrName | StateStorageOrName[], key: StateStorageKey, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\tconst storage = useMemo(() => {\n\t\treturn getStateStorage(storageOrName)\n\t}, [storageOrName])\n\n\tconst [value, setValue] = useState<V>(() => {\n\t\tconst value = storage.getItem(key)\n\t\treturn initializeValue(value as V)\n\t})\n\treturn [\n\t\tvalue,\n\t\tuseCallback(value => {\n\t\t\tsetValue(current => {\n\t\t\t\tconst newValue = typeof value === 'function' ? (value as (current: V) => V)(current) : value\n\t\t\t\tstorage.setItem(key, newValue)\n\t\t\t\treturn newValue\n\t\t\t})\n\t\t}, [key, storage]),\n\t]\n}\n\nexport const useSessionStorageState = <V extends Serializable>(key: StateStorageKey, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\treturn useStoredState<V>(sessionStateStorage, key, initializeValue)\n}\n\nexport const useLocalStorageState = <V extends Serializable>(key: StateStorageKey, initializeValue: ValueInitializer<V>): [V, SetState<V>] => {\n\treturn useStoredState<V>(localStateStorage, key, initializeValue)\n}\n\nexport const getStateStorage = (storageOrName: StateStorageOrName | StateStorageOrName[]) => {\n\tif (Array.isArray(storageOrName)) {\n\t\treturn createFallbackStorage(storageOrName.map(it => typeof it === 'string' ? builtInStorages[it] : it))\n\t}\n\treturn typeof storageOrName === 'string' ? builtInStorages[storageOrName] : storageOrName\n}\n\n\nconst createFallbackStorage = (storageTypes: StateStorage[]): StateStorage => {\n\treturn {\n\t\tgetItem(key) {\n\t\t\tfor (const storage of storageTypes) {\n\t\t\t\tconst value = storage.getItem(key)\n\t\t\t\tif (value !== null) {\n\t\t\t\t\treturn value\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn null\n\t\t},\n\t\tsetItem(key, value) {\n\t\t\tfor (const storage of storageTypes) {\n\t\t\t\tstorage.setItem(key, value)\n\t\t\t}\n\t\t},\n\t}\n}\n"],"names":["value"],"mappings":"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"}
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const createNonNullableContextFactory = require("./context/createNonNullableContextFactory.cjs");
4
- const createOptionalContextFactory = require("./context/createOptionalContextFactory.cjs");
3
+ const createRequiredContext = require("./context/createRequiredContext.cjs");
4
+ const createContext = require("./context/createContext.cjs");
5
5
  const Constants = require("./debug-context/Constants.cjs");
6
6
  const Context = require("./debug-context/Context.cjs");
7
7
  const unwrapRefValue = require("./hooks/unwrapRefValue.cjs");
@@ -52,8 +52,8 @@ const useClassName = require("./theming/useClassName.cjs");
52
52
  const useClassNameFactory = require("./theming/useClassNameFactory.cjs");
53
53
  const useThemedClassName = require("./theming/useThemedClassName.cjs");
54
54
  const useThemedClassNameFactory = require("./theming/useThemedClassNameFactory.cjs");
55
- exports.createNonNullableContextFactory = createNonNullableContextFactory.createNonNullableContextFactory;
56
- exports.createOptionalContextFactory = createOptionalContextFactory.createOptionalContextFactory;
55
+ exports.createRequiredContext = createRequiredContext.createRequiredContext;
56
+ exports.createContext = createContext.createContext;
57
57
  exports.isNoopScopedConsole = Constants.isNoopScopedConsole;
58
58
  exports.noopLog = Constants.noopLog;
59
59
  exports.noopLogged = Constants.noopLogged;
@@ -94,7 +94,14 @@ exports.useOnScrollWithin = useOnScrollWithin.useOnScrollWithin;
94
94
  exports.useOnWindowResize = useOnWindowResize.useOnWindowResize;
95
95
  exports.usePreviousValue = usePreviousValue.usePreviousValue;
96
96
  exports.useScrollOffsets = useScrollOffsets.useScrollOffsets;
97
+ exports.getStateStorage = useStoredState.getStateStorage;
98
+ exports.localStateStorage = useStoredState.localStateStorage;
99
+ exports.nullStorage = useStoredState.nullStorage;
100
+ exports.sessionStateStorage = useStoredState.sessionStateStorage;
101
+ exports.urlStateStorage = useStoredState.urlStateStorage;
102
+ exports.useLocalStorageState = useStoredState.useLocalStorageState;
97
103
  exports.useSessionStorageState = useStoredState.useSessionStorageState;
104
+ exports.useStoredState = useStoredState.useStoredState;
98
105
  exports.useUpdatedRef = useUpdatedRef.useUpdatedRef;
99
106
  exports.useWindowSize = useWindowSize.useWindowSize;
100
107
  exports.emptyArray = emptyArray.emptyArray;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,5 +1,5 @@
1
- import { createNonNullableContextFactory } from "./context/createNonNullableContextFactory.js";
2
- import { createOptionalContextFactory } from "./context/createOptionalContextFactory.js";
1
+ import { createRequiredContext } from "./context/createRequiredContext.js";
2
+ import { createContext } from "./context/createContext.js";
3
3
  import { isNoopScopedConsole, noopLog, noopLogged, noopScopedConsole } from "./debug-context/Constants.js";
4
4
  import { DebugChildren, ScopedConsoleContext, useScopedConsoleRef } from "./debug-context/Context.js";
5
5
  import { unwrapRefValue } from "./hooks/unwrapRefValue.js";
@@ -33,7 +33,7 @@ import { useOnScrollWithin } from "./hooks/useOnScrollWithin.js";
33
33
  import { useOnWindowResize } from "./hooks/useOnWindowResize.js";
34
34
  import { usePreviousValue } from "./hooks/usePreviousValue.js";
35
35
  import { useScrollOffsets } from "./hooks/useScrollOffsets.js";
36
- import { useSessionStorageState } from "./hooks/useStoredState.js";
36
+ import { getStateStorage, localStateStorage, nullStorage, sessionStateStorage, urlStateStorage, useLocalStorageState, useSessionStorageState, useStoredState } from "./hooks/useStoredState.js";
37
37
  import { useUpdatedRef } from "./hooks/useUpdatedRef.js";
38
38
  import { useWindowSize } from "./hooks/useWindowSize.js";
39
39
  import { emptyArray } from "./referentiallyStable/emptyArray.js";
@@ -58,20 +58,25 @@ export {
58
58
  DebugChildren,
59
59
  GlobalClassNamePrefixContext,
60
60
  ScopedConsoleContext,
61
- createNonNullableContextFactory,
62
- createOptionalContextFactory,
61
+ createContext,
62
+ createRequiredContext,
63
63
  emptyArray,
64
64
  emptyObject,
65
65
  getChildrenAsLabel,
66
+ getStateStorage,
66
67
  identityFunction,
67
68
  isNoopScopedConsole,
69
+ localStateStorage,
68
70
  noop,
69
71
  noopLog,
70
72
  noopLogged,
71
73
  noopScopedConsole,
74
+ nullStorage,
72
75
  returnFalse,
73
76
  returnTrue,
77
+ sessionStateStorage,
74
78
  unwrapRefValue,
79
+ urlStateStorage,
75
80
  useAbortController,
76
81
  useAddClassNameDuringResize,
77
82
  useArrayMapMemo,
@@ -96,6 +101,7 @@ export {
96
101
  useForceRender,
97
102
  useId,
98
103
  useIsMounted,
104
+ useLocalStorageState,
99
105
  useObjectMemo,
100
106
  useOnElementClickOutsideCallback,
101
107
  useOnElementMouseEnterDelayedCallback,
@@ -108,6 +114,7 @@ export {
108
114
  useScopedConsoleRef,
109
115
  useScrollOffsets,
110
116
  useSessionStorageState,
117
+ useStoredState,
111
118
  useThemedClassName,
112
119
  useThemedClassNameFactory,
113
120
  useUpdatedRef,
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const createOptionalContextFactory = require("../context/createOptionalContextFactory.cjs");
3
+ const createContext = require("../context/createContext.cjs");
4
4
  const DEFAULT_COLOR_SCHEME = "system";
5
- const [ColorSchemeContext, useColorScheme] = createOptionalContextFactory.createOptionalContextFactory(
5
+ const [ColorSchemeContext, useColorScheme] = createContext.createContext(
6
6
  "Interface.ColorScheme",
7
7
  DEFAULT_COLOR_SCHEME
8
8
  );
@@ -1 +1 @@
1
- {"version":3,"file":"contexts.cjs","sources":["../../../src/theming/contexts.ts"],"sourcesContent":["import { createOptionalContextFactory } from '../context'\n\nexport const DEFAULT_COLOR_SCHEME = 'system'\n\n\n/**\n * @group UI\n *\n * @description\n * Provides the current color scheme, possible values are `'light'`, `'dark'` or `'system'`.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme\n *\n */\nexport const [ColorSchemeContext, useColorScheme] = createOptionalContextFactory<string>(\n\t'Interface.ColorScheme',\n\tDEFAULT_COLOR_SCHEME,\n)\n"],"names":["createOptionalContextFactory"],"mappings":";;;AAEO,MAAM,uBAAuB;AAYvB,MAAA,CAAC,oBAAoB,cAAc,IAAIA,6BAAA;AAAA,EACnD;AAAA,EACA;AACD;;;;"}
1
+ {"version":3,"file":"contexts.cjs","sources":["../../../src/theming/contexts.ts"],"sourcesContent":["import { createContext } from '../context'\n\nexport const DEFAULT_COLOR_SCHEME = 'system'\n\n\n/**\n * @group UI\n *\n * @description\n * Provides the current color scheme, possible values are `'light'`, `'dark'` or `'system'`.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme\n *\n */\nexport const [ColorSchemeContext, useColorScheme] = createContext<string>(\n\t'Interface.ColorScheme',\n\tDEFAULT_COLOR_SCHEME,\n)\n"],"names":["createContext"],"mappings":";;;AAEO,MAAM,uBAAuB;AAYvB,MAAA,CAAC,oBAAoB,cAAc,IAAIA,cAAA;AAAA,EACnD;AAAA,EACA;AACD;;;;"}
@@ -1,6 +1,6 @@
1
- import { createOptionalContextFactory } from "../context/createOptionalContextFactory.js";
1
+ import { createContext } from "../context/createContext.js";
2
2
  const DEFAULT_COLOR_SCHEME = "system";
3
- const [ColorSchemeContext, useColorScheme] = createOptionalContextFactory(
3
+ const [ColorSchemeContext, useColorScheme] = createContext(
4
4
  "Interface.ColorScheme",
5
5
  DEFAULT_COLOR_SCHEME
6
6
  );
@@ -1 +1 @@
1
- {"version":3,"file":"contexts.js","sources":["../../../src/theming/contexts.ts"],"sourcesContent":["import { createOptionalContextFactory } from '../context'\n\nexport const DEFAULT_COLOR_SCHEME = 'system'\n\n\n/**\n * @group UI\n *\n * @description\n * Provides the current color scheme, possible values are `'light'`, `'dark'` or `'system'`.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme\n *\n */\nexport const [ColorSchemeContext, useColorScheme] = createOptionalContextFactory<string>(\n\t'Interface.ColorScheme',\n\tDEFAULT_COLOR_SCHEME,\n)\n"],"names":[],"mappings":";AAEO,MAAM,uBAAuB;AAYvB,MAAA,CAAC,oBAAoB,cAAc,IAAI;AAAA,EACnD;AAAA,EACA;AACD;"}
1
+ {"version":3,"file":"contexts.js","sources":["../../../src/theming/contexts.ts"],"sourcesContent":["import { createContext } from '../context'\n\nexport const DEFAULT_COLOR_SCHEME = 'system'\n\n\n/**\n * @group UI\n *\n * @description\n * Provides the current color scheme, possible values are `'light'`, `'dark'` or `'system'`.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme\n *\n */\nexport const [ColorSchemeContext, useColorScheme] = createContext<string>(\n\t'Interface.ColorScheme',\n\tDEFAULT_COLOR_SCHEME,\n)\n"],"names":[],"mappings":";AAEO,MAAM,uBAAuB;AAYvB,MAAA,CAAC,oBAAoB,cAAc,IAAI;AAAA,EACnD;AAAA,EACA;AACD;"}
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const react = require("react");
4
+ function createContext(name, initialValue) {
5
+ const OptionalContext = react.createContext(initialValue);
6
+ OptionalContext.displayName = name;
7
+ return [OptionalContext, () => react.useContext(OptionalContext)];
8
+ }
9
+ exports.createContext = createContext;
10
+ //# sourceMappingURL=createContext.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createContext.cjs","sources":["../../../src/context/createContext.ts"],"sourcesContent":["import type { Context } from 'react'\nimport { createContext as baseCreateContext, useContext } from 'react'\n\nexport function createContext<T>(name: string, initialValue: T): [Context<T>, () => T] {\n const OptionalContext = baseCreateContext<T>(initialValue)\n OptionalContext.displayName = name\n\n return [OptionalContext, () => useContext(OptionalContext)]\n}\n"],"names":["baseCreateContext","useContext"],"mappings":";;;AAGgB,SAAA,cAAiB,MAAc,cAAwC;AAC/E,QAAA,kBAAkBA,oBAAqB,YAAY;AACzD,kBAAgB,cAAc;AAE9B,SAAO,CAAC,iBAAiB,MAAMC,iBAAW,eAAe,CAAC;AAC5D;;"}
@@ -0,0 +1,10 @@
1
+ import { createContext as createContext$1, useContext } from "react";
2
+ function createContext(name, initialValue) {
3
+ const OptionalContext = createContext$1(initialValue);
4
+ OptionalContext.displayName = name;
5
+ return [OptionalContext, () => useContext(OptionalContext)];
6
+ }
7
+ export {
8
+ createContext
9
+ };
10
+ //# sourceMappingURL=createContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createContext.js","sources":["../../../src/context/createContext.ts"],"sourcesContent":["import type { Context } from 'react'\nimport { createContext as baseCreateContext, useContext } from 'react'\n\nexport function createContext<T>(name: string, initialValue: T): [Context<T>, () => T] {\n const OptionalContext = baseCreateContext<T>(initialValue)\n OptionalContext.displayName = name\n\n return [OptionalContext, () => useContext(OptionalContext)]\n}\n"],"names":["baseCreateContext"],"mappings":";AAGgB,SAAA,cAAiB,MAAc,cAAwC;AAC/E,QAAA,kBAAkBA,gBAAqB,YAAY;AACzD,kBAAgB,cAAc;AAE9B,SAAO,CAAC,iBAAiB,MAAM,WAAW,eAAe,CAAC;AAC5D;"}
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const react = require("react");
4
- function createNonNullableContextFactory(displayName, initialValue) {
5
- const RequiredContext = react.createContext(initialValue ?? null);
4
+ const EmptyContextSymbol = Symbol("EmptyContextSymbol");
5
+ function createRequiredContext(displayName) {
6
+ const RequiredContext = react.createContext(EmptyContextSymbol);
6
7
  RequiredContext.displayName = displayName;
7
8
  const useRequiredContext = () => {
8
9
  const context = react.useContext(RequiredContext);
9
- if (context == null) {
10
+ if (context === EmptyContextSymbol) {
10
11
  throw new Error(`use${displayName} must be used within a ${displayName} provider`);
11
12
  }
12
13
  return context;
13
14
  };
14
15
  return [RequiredContext, useRequiredContext];
15
16
  }
16
- exports.createNonNullableContextFactory = createNonNullableContextFactory;
17
- //# sourceMappingURL=createNonNullableContextFactory.cjs.map
17
+ exports.createRequiredContext = createRequiredContext;
18
+ //# sourceMappingURL=createRequiredContext.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRequiredContext.cjs","sources":["../../../src/context/createRequiredContext.ts"],"sourcesContent":["import type { Context } from 'react'\nimport { createContext, useContext } from 'react'\n\nconst EmptyContextSymbol = Symbol('EmptyContextSymbol')\n\nexport function createRequiredContext<T>(displayName: string): [Context<T>, () => T] {\n const RequiredContext = createContext<T>(EmptyContextSymbol as T)\n RequiredContext.displayName = displayName\n\n const useRequiredContext = (): T => {\n const context = useContext(RequiredContext)\n\n if (context === EmptyContextSymbol) {\n throw new Error(`use${displayName} must be used within a ${displayName} provider`)\n }\n\n return context\n }\n\n return [RequiredContext, useRequiredContext]\n}\n"],"names":["createContext","useContext"],"mappings":";;;AAGA,MAAM,qBAAqB,OAAO,oBAAoB;AAE/C,SAAS,sBAAyB,aAA4C;AAC7E,QAAA,kBAAkBA,oBAAiB,kBAAuB;AAChE,kBAAgB,cAAc;AAE9B,QAAM,qBAAqB,MAAS;AAC5B,UAAA,UAAUC,iBAAW,eAAe;AAE1C,QAAI,YAAY,oBAAoB;AAClC,YAAM,IAAI,MAAM,MAAM,WAAW,0BAA0B,WAAW,WAAW;AAAA,IACnF;AAEO,WAAA;AAAA,EAAA;AAGF,SAAA,CAAC,iBAAiB,kBAAkB;AAC7C;;"}