@m4l/core 0.1.15 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/hooks/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { useFlags, useFlagsPresent } from './useFlags';
3
3
  export { useHostTools } from './useHostTools';
4
4
  export { useDomain } from './useDomain';
5
5
  export { useLocalStorage } from './useLocalStorage';
6
+ export { useLocalStorageWithListener } from './useLocalStorageWithListener';
6
7
  export { useModuleDictionary } from './useModuleDictionary';
7
8
  export { useModulePrivileges } from './useModulePrivileges';
8
9
  export { useModuleSkeleton } from './useModuleSkeleton';
@@ -1,4 +1,4 @@
1
- import { useState } from "react";
1
+ import { useState, useEffect } from "react";
2
2
  function useLocalStorage(key, initialValue) {
3
3
  const [value, setValue] = useState(() => {
4
4
  try {
@@ -18,4 +18,29 @@ function useLocalStorage(key, initialValue) {
18
18
  };
19
19
  return [value, setValueInLocalStorage];
20
20
  }
21
- export { useLocalStorage as u };
21
+ function useLocalStorageWithListener(key, defaultValue) {
22
+ const [value, setValue] = useState(() => {
23
+ const storedValue = localStorage.getItem(key);
24
+ return storedValue === null ? defaultValue : JSON.parse(storedValue);
25
+ });
26
+ useEffect(() => {
27
+ const listener = (e) => {
28
+ if (e.storageArea === localStorage && e.key === key) {
29
+ setValue(e.newValue ? JSON.parse(e.newValue) : e.newValue);
30
+ }
31
+ };
32
+ window.addEventListener("storage", listener);
33
+ return () => {
34
+ window.removeEventListener("storage", listener);
35
+ };
36
+ }, [key, defaultValue]);
37
+ const setValueInLocalStorage = (newValue) => {
38
+ setValue((currentValue) => {
39
+ const result = typeof newValue === "function" ? newValue(currentValue) : newValue;
40
+ localStorage.setItem(key, JSON.stringify(result));
41
+ return result;
42
+ });
43
+ };
44
+ return [value, setValueInLocalStorage];
45
+ }
46
+ export { useLocalStorageWithListener as a, useLocalStorage as u };
@@ -0,0 +1,2 @@
1
+ export declare function useLocalStorageWithListener<ValueType>(key: string, defaultValue: ValueType): any[];
2
+ export default useLocalStorageWithListener;
package/index.js CHANGED
@@ -11,7 +11,7 @@ export { u as useEnvironment } from "./hooks/useEnvironment/index.js";
11
11
  export { u as useFlags, a as useFlagsPresent } from "./hooks/useFlags/index.js";
12
12
  export { u as useHostTools } from "./hooks/useHostTools/index.js";
13
13
  export { u as useDomain } from "./hooks/useDomain/index.js";
14
- export { u as useLocalStorage } from "./hooks/useLocalStorage/index.js";
14
+ export { u as useLocalStorage, a as useLocalStorageWithListener } from "./hooks/useLocalStorage/index.js";
15
15
  export { u as useModuleDictionary } from "./hooks/useModuleDictionary/index.js";
16
16
  export { u as useModulePrivileges } from "./hooks/useModulePrivileges/index.js";
17
17
  export { u as useModuleSkeleton } from "./hooks/useModuleSkeleton/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m4l/core",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "license": "UNLICENSED",
5
5
  "author": "M4L Team",
6
6
  "dependencies": {