@geee-be/react-utils 1.0.1 → 1.0.3
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/CHANGELOG.md +12 -0
- package/dist/hooks/index.js +3 -0
- package/dist/hooks/state.util.js +19 -0
- package/dist/hooks/use-broadcast-channel.js +42 -0
- package/dist/hooks/use-history-state.js +16 -0
- package/dist/hooks/use-local-state.js +26 -0
- package/dist/hooks/use-local-state.stories.js +13 -0
- package/dist/index.js +1 -0
- package/package.json +3 -3
- package/tsconfig.json +1 -0
package/CHANGELOG.md
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
export const deserialize = (serialized, fromSerializable) => {
|
2
|
+
if (!serialized || typeof serialized !== 'string')
|
3
|
+
return undefined;
|
4
|
+
try {
|
5
|
+
return fromSerializable ? fromSerializable(JSON.parse(serialized)) : JSON.parse(serialized);
|
6
|
+
}
|
7
|
+
catch {
|
8
|
+
return undefined;
|
9
|
+
}
|
10
|
+
};
|
11
|
+
export const serialize = (value, toSerializable) => {
|
12
|
+
try {
|
13
|
+
return JSON.stringify(toSerializable ? toSerializable(value) : value);
|
14
|
+
}
|
15
|
+
catch {
|
16
|
+
return '';
|
17
|
+
}
|
18
|
+
};
|
19
|
+
export const getInitialValue = (initialValue) => initialValue instanceof Function ? initialValue() : initialValue;
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { useCallback, useEffect, useMemo } from 'react';
|
2
|
+
/**
|
3
|
+
* React hook to create and manage a Broadcast Channel across multiple browser windows.
|
4
|
+
*
|
5
|
+
* @param channelName Static name of channel used across the browser windows.
|
6
|
+
* @param handleMessage Callback to handle the event generated when `message` is received.
|
7
|
+
* @param handleMessageError [optional] Callback to handle the event generated when `error` is received.
|
8
|
+
* @returns A function to send/post message on the channel.
|
9
|
+
* @example
|
10
|
+
* ```tsx
|
11
|
+
* import {useBroadcastChannel} from 'react-broadcast-channel';
|
12
|
+
*
|
13
|
+
* function App () {
|
14
|
+
* const postUserIdMessage = useBroadcastChannel('userId', (e) => alert(e.data));
|
15
|
+
* return (<button onClick={() => postUserIdMessage('ABC123')}>Send UserId</button>);
|
16
|
+
* }
|
17
|
+
* ```
|
18
|
+
* ---
|
19
|
+
* Works in browser that support Broadcast Channel API natively. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API#browser_compatibility).
|
20
|
+
* To support other browsers, install and use [broadcastchannel-polyfill](https://www.npmjs.com/package/broadcastchannel-polyfill).
|
21
|
+
*/
|
22
|
+
export const useBroadcastChannel = (channelName, handleMessage, handleMessageError) => {
|
23
|
+
const channel = useMemo(() => typeof window !== 'undefined' && 'BroadcastChannel' in window
|
24
|
+
? new BroadcastChannel(channelName + '-channel')
|
25
|
+
: null, [channelName]);
|
26
|
+
useChannelEventListener(channel, 'message', handleMessage);
|
27
|
+
useChannelEventListener(channel, 'messageerror', handleMessageError);
|
28
|
+
useEffect(() => () => channel?.close(), [channel]);
|
29
|
+
return useCallback((data) => {
|
30
|
+
channel?.postMessage(data);
|
31
|
+
}, [channel]);
|
32
|
+
};
|
33
|
+
// Helpers
|
34
|
+
/** Hook to subscribe/unsubscribe from channel events. */
|
35
|
+
const useChannelEventListener = (channel, event, handler = () => { }) => {
|
36
|
+
useEffect(() => {
|
37
|
+
if (!channel)
|
38
|
+
return;
|
39
|
+
channel.addEventListener(event, handler);
|
40
|
+
return () => channel.removeEventListener(event, handler);
|
41
|
+
}, [channel, event, handler]);
|
42
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { useEffect, useState } from 'react';
|
2
|
+
import { deserialize, getInitialValue, serialize } from './state.util.js';
|
3
|
+
export const useHistoryState = (key, initialValue, replace = true, options) => {
|
4
|
+
const [value, setValue] = useState(
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
6
|
+
() => deserialize(history.state?.[key], options?.fromSerializable) ?? getInitialValue(initialValue));
|
7
|
+
useEffect(() => {
|
8
|
+
if (replace) {
|
9
|
+
history.replaceState({ ...history.state, [key]: serialize(value, options?.toSerializable) }, '');
|
10
|
+
}
|
11
|
+
else {
|
12
|
+
history.pushState({ ...history.state, [key]: serialize(value, options?.toSerializable) }, '');
|
13
|
+
}
|
14
|
+
}, [key, value]);
|
15
|
+
return [value, setValue];
|
16
|
+
};
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
2
|
+
import { deserialize, getInitialValue, serialize } from './state.util.js';
|
3
|
+
// Provides hook that persist the state with local storage with sync'd updates
|
4
|
+
// NOTE: this only syncs between different window contexts, it doesn't sync between components in the same window
|
5
|
+
export const useLocalState = (key, initialValue, options) => {
|
6
|
+
const readValue = useCallback(() => {
|
7
|
+
const item = localStorage.getItem(key);
|
8
|
+
return deserialize(item, options?.fromSerializable) ?? getInitialValue(initialValue);
|
9
|
+
}, [key, options?.fromSerializable, initialValue]);
|
10
|
+
const [storedValue, setStoredValue] = useState(readValue);
|
11
|
+
const setValue = useCallback((value) => {
|
12
|
+
const newValue = value instanceof Function ? value(storedValue) : value;
|
13
|
+
setStoredValue(newValue);
|
14
|
+
localStorage.setItem(key, serialize(newValue, options?.toSerializable));
|
15
|
+
}, [key, options?.toSerializable, storedValue]);
|
16
|
+
const storageValueChanged = useCallback((ev) => {
|
17
|
+
if (ev.storageArea !== localStorage || ev.key !== key)
|
18
|
+
return;
|
19
|
+
setStoredValue(deserialize(ev.newValue, options?.fromSerializable) ?? getInitialValue(initialValue));
|
20
|
+
}, [key, options?.fromSerializable, initialValue]);
|
21
|
+
useEffect(() => {
|
22
|
+
window.addEventListener('storage', storageValueChanged);
|
23
|
+
return () => window.removeEventListener('storage', storageValueChanged);
|
24
|
+
}, [storageValueChanged]);
|
25
|
+
return [storedValue, setValue];
|
26
|
+
};
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
2
|
+
import { useLocalState } from './use-local-state.js';
|
3
|
+
const meta = {
|
4
|
+
title: 'hooks/useLocalState',
|
5
|
+
};
|
6
|
+
export default meta;
|
7
|
+
export const Primary = {
|
8
|
+
render: () => {
|
9
|
+
const [value, setValue] = useLocalState('story-use-local-state', { val: 1 });
|
10
|
+
console.log('render', value);
|
11
|
+
return (_jsxs("div", { children: [JSON.stringify(value), _jsx("button", { onClick: () => setValue((prev) => ({ val: prev.val + 1 ?? -2 })), children: "Inc" })] }));
|
12
|
+
},
|
13
|
+
};
|
package/dist/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './hooks/index.js';
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@geee-be/react-utils",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.3",
|
4
4
|
"description": "",
|
5
5
|
"keywords": [],
|
6
6
|
"license": "MIT",
|
@@ -25,8 +25,8 @@
|
|
25
25
|
"storybook": "^7.6.7"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
|
-
"react": "
|
29
|
-
"react-dom": "
|
28
|
+
"react": ">=18.0.0",
|
29
|
+
"react-dom": ">=18.0.0"
|
30
30
|
},
|
31
31
|
"scripts": {
|
32
32
|
"prebuild": "rimraf dist/*",
|