@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.
@@ -1,6 +1,6 @@
1
1
  /** @type { import('@storybook/react-vite').StorybookConfig } */
2
2
  const config = {
3
- stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
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
@@ -1,5 +1,17 @@
1
1
  # @geee-be/react-utils
2
2
 
3
+ ## 1.0.6
4
+
5
+ ### Patch Changes
6
+
7
+ - 5aa7200: attempt to fix server error about missing history
8
+
9
+ ## 1.0.5
10
+
11
+ ### Patch Changes
12
+
13
+ - daeeaa9: Added ripple helper
14
+
3
15
  ## 1.0.4
4
16
 
5
17
  ### Patch Changes
@@ -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
@@ -1 +1,2 @@
1
+ export * from './helpers/index.js';
1
2
  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.4",
3
+ "version": "1.0.6",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "MIT",
@@ -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
@@ -1 +1,2 @@
1
+ export * from './helpers/index.js';
1
2
  export * from './hooks/index.js';