@geee-be/react-utils 1.0.4 → 1.0.6
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/.storybook/main.js +1 -1
- package/CHANGELOG.md +12 -0
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/ripple.js +46 -0
- package/dist/hooks/use-history-state.js +4 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
- package/src/helpers/index.ts +1 -0
- package/src/helpers/ripple.ts +80 -0
- package/src/hooks/use-history-state.ts +4 -0
- package/src/index.ts +1 -0
package/.storybook/main.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/** @type { import('@storybook/react-vite').StorybookConfig } */
|
2
2
|
const config = {
|
3
|
-
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|
|
3
|
+
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
|
4
4
|
addons: [
|
5
5
|
'@storybook/addon-links',
|
6
6
|
'@storybook/addon-essentials',
|
package/CHANGELOG.md
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './ripple.js';
|
@@ -0,0 +1,46 @@
|
|
1
|
+
const RIPPLE_MS = 500;
|
2
|
+
export const createRipple = (event) => {
|
3
|
+
const element = event.currentTarget;
|
4
|
+
if (!element || !(element instanceof HTMLElement))
|
5
|
+
return;
|
6
|
+
element.style.position = 'relative';
|
7
|
+
element.style.overflow = 'hidden';
|
8
|
+
const rect = element.getBoundingClientRect();
|
9
|
+
const radius = findFurthestPoint(event.clientX, element.offsetWidth, rect.left, event.clientY, element.offsetHeight, rect.top);
|
10
|
+
const circle = document.createElement('span');
|
11
|
+
applyStyles(circle, rect, radius, { x: event.clientX, y: event.clientY });
|
12
|
+
applyAnimation(circle);
|
13
|
+
element.appendChild(circle);
|
14
|
+
setTimeout(() => circle.remove(), RIPPLE_MS);
|
15
|
+
};
|
16
|
+
const findFurthestPoint = (clickPointX, elementWidth, offsetX, clickPointY, elementHeight, offsetY) => {
|
17
|
+
const x = clickPointX - offsetX > elementWidth / 2 ? 0 : elementWidth;
|
18
|
+
const y = clickPointY - offsetY > elementHeight / 2 ? 0 : elementHeight;
|
19
|
+
return Math.hypot(x - (clickPointX - offsetX), y - (clickPointY - offsetY));
|
20
|
+
};
|
21
|
+
const applyAnimation = (element) => {
|
22
|
+
element.animate([
|
23
|
+
{
|
24
|
+
transform: 'scale(0)',
|
25
|
+
opacity: 0.5,
|
26
|
+
},
|
27
|
+
{
|
28
|
+
transform: 'scale(1.5)',
|
29
|
+
opacity: 0,
|
30
|
+
},
|
31
|
+
], {
|
32
|
+
duration: RIPPLE_MS,
|
33
|
+
easing: 'ease-out',
|
34
|
+
fill: 'forwards',
|
35
|
+
});
|
36
|
+
};
|
37
|
+
const applyStyles = (element, rect, radius, position) => {
|
38
|
+
element.classList.add('ripple');
|
39
|
+
element.style.backgroundColor = 'currentColor';
|
40
|
+
element.style.borderRadius = '50%';
|
41
|
+
element.style.pointerEvents = 'none';
|
42
|
+
element.style.position = 'absolute';
|
43
|
+
element.style.left = `${position.x - rect.left - radius}px`;
|
44
|
+
element.style.top = `${position.y - rect.top - radius}px`;
|
45
|
+
element.style.width = element.style.height = `${radius * 2}px`;
|
46
|
+
};
|
@@ -2,6 +2,10 @@
|
|
2
2
|
import { useEffect, useState } from 'react';
|
3
3
|
import { deserialize, getInitialValue, serialize } from './state.util.js';
|
4
4
|
export const useHistoryState = (key, initialValue, replace = true, options) => {
|
5
|
+
if (!history) {
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
7
|
+
return [undefined, () => { }];
|
8
|
+
}
|
5
9
|
const [value, setValue] = useState(
|
6
10
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
7
11
|
() => deserialize(history.state?.[key], options?.fromSerializable) ?? getInitialValue(initialValue));
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './ripple.js';
|
@@ -0,0 +1,80 @@
|
|
1
|
+
import type { MouseEventHandler } from 'react';
|
2
|
+
|
3
|
+
const RIPPLE_MS = 500;
|
4
|
+
|
5
|
+
export const createRipple: MouseEventHandler<HTMLElement> = (event) => {
|
6
|
+
const element = event.currentTarget;
|
7
|
+
if (!element || !(element instanceof HTMLElement)) return;
|
8
|
+
|
9
|
+
element.style.position = 'relative';
|
10
|
+
element.style.overflow = 'hidden';
|
11
|
+
|
12
|
+
const rect = element.getBoundingClientRect();
|
13
|
+
|
14
|
+
const radius = findFurthestPoint(
|
15
|
+
event.clientX,
|
16
|
+
element.offsetWidth,
|
17
|
+
rect.left,
|
18
|
+
event.clientY,
|
19
|
+
element.offsetHeight,
|
20
|
+
rect.top,
|
21
|
+
);
|
22
|
+
|
23
|
+
const circle = document.createElement('span');
|
24
|
+
|
25
|
+
applyStyles(circle, rect, radius, { x: event.clientX, y: event.clientY });
|
26
|
+
applyAnimation(circle);
|
27
|
+
|
28
|
+
element.appendChild(circle);
|
29
|
+
|
30
|
+
setTimeout(() => circle.remove(), RIPPLE_MS);
|
31
|
+
};
|
32
|
+
|
33
|
+
const findFurthestPoint = (
|
34
|
+
clickPointX: number,
|
35
|
+
elementWidth: number,
|
36
|
+
offsetX: number,
|
37
|
+
clickPointY: number,
|
38
|
+
elementHeight: number,
|
39
|
+
offsetY: number,
|
40
|
+
): number => {
|
41
|
+
const x = clickPointX - offsetX > elementWidth / 2 ? 0 : elementWidth;
|
42
|
+
const y = clickPointY - offsetY > elementHeight / 2 ? 0 : elementHeight;
|
43
|
+
return Math.hypot(x - (clickPointX - offsetX), y - (clickPointY - offsetY));
|
44
|
+
};
|
45
|
+
|
46
|
+
const applyAnimation = (element: HTMLElement): void => {
|
47
|
+
element.animate(
|
48
|
+
[
|
49
|
+
{
|
50
|
+
transform: 'scale(0)',
|
51
|
+
opacity: 0.5,
|
52
|
+
},
|
53
|
+
{
|
54
|
+
transform: 'scale(1.5)',
|
55
|
+
opacity: 0,
|
56
|
+
},
|
57
|
+
],
|
58
|
+
{
|
59
|
+
duration: RIPPLE_MS,
|
60
|
+
easing: 'ease-out',
|
61
|
+
fill: 'forwards',
|
62
|
+
},
|
63
|
+
);
|
64
|
+
};
|
65
|
+
|
66
|
+
const applyStyles = (
|
67
|
+
element: HTMLElement,
|
68
|
+
rect: { top: number; left: number },
|
69
|
+
radius: number,
|
70
|
+
position: { x: number; y: number },
|
71
|
+
): void => {
|
72
|
+
element.classList.add('ripple');
|
73
|
+
element.style.backgroundColor = 'currentColor';
|
74
|
+
element.style.borderRadius = '50%';
|
75
|
+
element.style.pointerEvents = 'none';
|
76
|
+
element.style.position = 'absolute';
|
77
|
+
element.style.left = `${position.x - rect.left - radius}px`;
|
78
|
+
element.style.top = `${position.y - rect.top - radius}px`;
|
79
|
+
element.style.width = element.style.height = `${radius * 2}px`;
|
80
|
+
};
|
@@ -11,6 +11,10 @@ export const useHistoryState = <T, S = T>(
|
|
11
11
|
replace = true,
|
12
12
|
options?: SerializationOptions<T, S>,
|
13
13
|
): [T, Dispatch<SetStateAction<T>>] => {
|
14
|
+
if (!history) {
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
16
|
+
return [undefined as any, () => {}];
|
17
|
+
}
|
14
18
|
const [value, setValue] = useState<T>(
|
15
19
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
16
20
|
() => deserialize(history.state?.[key], options?.fromSerializable) ?? getInitialValue(initialValue),
|
package/src/index.ts
CHANGED