@geee-be/react-utils 1.0.4 → 1.0.5

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,11 @@
1
1
  # @geee-be/react-utils
2
2
 
3
+ ## 1.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - daeeaa9: Added ripple helper
8
+
3
9
  ## 1.0.4
4
10
 
5
11
  ### 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
+ };
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.5",
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
+ };
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
+ export * from './helpers/index.js';
1
2
  export * from './hooks/index.js';