@purpurds/toggle 5.28.1 → 5.28.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@purpurds/toggle",
3
- "version": "5.28.1",
3
+ "version": "5.28.3",
4
4
  "license": "AGPL-3.0-only",
5
5
  "main": "./dist/toggle.cjs.js",
6
6
  "types": "./dist/toggle.d.ts",
@@ -16,10 +16,10 @@
16
16
  "dependencies": {
17
17
  "@radix-ui/react-switch": "~1.0.3",
18
18
  "classnames": "~2.5.0",
19
- "@purpurds/icon": "5.28.1",
20
- "@purpurds/label": "5.28.1",
21
- "@purpurds/paragraph": "5.28.1",
22
- "@purpurds/tokens": "5.28.1"
19
+ "@purpurds/icon": "5.28.3",
20
+ "@purpurds/tokens": "5.28.3",
21
+ "@purpurds/paragraph": "5.28.3",
22
+ "@purpurds/label": "5.28.3"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@rushstack/eslint-patch": "~1.10.0",
@@ -0,0 +1,121 @@
1
+ import type { RefObject } from "react";
2
+ import { useCallback, useEffect, useRef, useState } from "react";
3
+
4
+ // This hook is a clone of useResizeObserver in b2c-sales/hooks
5
+
6
+ type Size = {
7
+ width: number | undefined;
8
+ height: number | undefined;
9
+ };
10
+
11
+ type UseResizeObserverOptions<T extends HTMLElement = HTMLElement> = {
12
+ ref: RefObject<T>;
13
+ onResize?: (size: Size) => void;
14
+ box?: "border-box" | "content-box" | "device-pixel-content-box";
15
+ round?: boolean;
16
+ };
17
+
18
+ const initialSize: Size = {
19
+ width: undefined,
20
+ height: undefined,
21
+ };
22
+
23
+ export function useResizeObserver<T extends HTMLElement = HTMLElement>(
24
+ options: UseResizeObserverOptions<T>
25
+ ): Size {
26
+ const { ref, box = "content-box", round } = options;
27
+ const [{ width, height }, setSize] = useState<Size>(initialSize);
28
+ const isMounted = useIsMounted();
29
+ const previousSize = useRef<Size>({ ...initialSize });
30
+ const onResize = useRef<((size: Size) => void) | undefined>(undefined);
31
+ onResize.current = options.onResize;
32
+
33
+ useEffect(() => {
34
+ if (!ref.current) {
35
+ return;
36
+ }
37
+
38
+ if (typeof window === "undefined" || !("ResizeObserver" in window)) {
39
+ return;
40
+ }
41
+
42
+ const observer = new ResizeObserver(([entry]) => {
43
+ const boxProp =
44
+ // eslint-disable-next-line no-nested-ternary
45
+ box === "border-box"
46
+ ? "borderBoxSize"
47
+ : box === "device-pixel-content-box"
48
+ ? "devicePixelContentBoxSize"
49
+ : "contentBoxSize";
50
+
51
+ const newWidth = extractSize(entry, boxProp, "inlineSize");
52
+ const newHeight = extractSize(entry, boxProp, "blockSize");
53
+
54
+ const hasChanged =
55
+ previousSize.current.width !== newWidth || previousSize.current.height !== newHeight;
56
+
57
+ if (hasChanged) {
58
+ const newSize: Size = {
59
+ width: newWidth && round ? Math.round(newWidth) : newWidth,
60
+ height: newHeight && round ? Math.round(newHeight) : newHeight,
61
+ };
62
+ previousSize.current.width = newWidth;
63
+ previousSize.current.height = newHeight;
64
+
65
+ if (onResize.current) {
66
+ onResize.current(newSize);
67
+ } else {
68
+ if (isMounted()) {
69
+ setSize(newSize);
70
+ }
71
+ }
72
+ }
73
+ });
74
+
75
+ observer.observe(ref.current, { box });
76
+
77
+ return () => {
78
+ observer.disconnect();
79
+ };
80
+ }, [box, ref, isMounted, round]);
81
+
82
+ return { width, height };
83
+ }
84
+
85
+ type BoxSizesKey = keyof Pick<
86
+ ResizeObserverEntry,
87
+ "borderBoxSize" | "contentBoxSize" | "devicePixelContentBoxSize"
88
+ >;
89
+
90
+ function extractSize(
91
+ entry: ResizeObserverEntry,
92
+ box: BoxSizesKey,
93
+ sizeType: keyof ResizeObserverSize
94
+ ): number | undefined {
95
+ if (!entry[box]) {
96
+ if (box === "contentBoxSize") {
97
+ return entry.contentRect[sizeType === "inlineSize" ? "width" : "height"];
98
+ }
99
+ return undefined;
100
+ }
101
+
102
+ return Array.isArray(entry[box])
103
+ ? entry[box][0][sizeType]
104
+ : // eslint-disable-next-line @typescript-eslint/ban-ts-comment
105
+ // @ts-ignore Support Firefox's non-standard behavior
106
+ (entry[box][sizeType] as number);
107
+ }
108
+
109
+ function useIsMounted() {
110
+ const isMounted = useRef(false);
111
+
112
+ useEffect(() => {
113
+ isMounted.current = true;
114
+
115
+ return () => {
116
+ isMounted.current = false;
117
+ };
118
+ }, []);
119
+
120
+ return useCallback(() => isMounted.current, []);
121
+ }
@@ -1,19 +1,21 @@
1
- import { MutableRefObject, useRef, useState, useLayoutEffect } from "react";
2
- import { ToggleProps } from "./toggle";
1
+ import { MutableRefObject, useRef, useState } from "react";
3
2
 
4
- export const useToggleDrag = ({ checked, onChange }: Pick<ToggleProps, "checked" | "onChange">) => {
3
+ import { useResizeObserver } from "./useResizeObserver";
4
+
5
+ type UseToggleDragProps = {
6
+ checked: boolean;
7
+ onChange: ((checked: boolean) => void) | undefined;
8
+ };
9
+
10
+ export const useToggleDrag = ({ checked, onChange }: UseToggleDragProps) => {
5
11
  const trackRef: MutableRefObject<HTMLDivElement | null> = useRef(null);
6
12
  const thumbRef = useRef<HTMLSpanElement>(null);
7
13
  const [dragX, setDragX] = useState<number | undefined>(undefined);
8
14
  const [isDragging, setIsDragging] = useState(false);
9
15
  const [dragStopped, setDragStopped] = useState(0);
10
- const [thumbHeight, setThumbHeight] = useState(0);
11
- const [switchHeight, setSwitchHeight] = useState(0);
12
16
 
13
- useLayoutEffect(() => {
14
- setThumbHeight(thumbRef.current?.clientHeight || 0);
15
- setSwitchHeight(trackRef.current?.clientHeight || 0);
16
- }, [thumbRef.current?.clientHeight, trackRef.current?.clientHeight]);
17
+ const { height: thumbHeight = 0 } = useResizeObserver({ ref: thumbRef, round: true });
18
+ const { height: switchHeight = 0 } = useResizeObserver({ ref: trackRef, round: true });
17
19
 
18
20
  const switchThumbHeightDelta = (switchHeight || 0) - thumbHeight;
19
21
  const maxDragRange = thumbHeight + switchThumbHeightDelta;
package/src/toggle.tsx CHANGED
@@ -5,9 +5,9 @@ import { Paragraph } from "@purpurds/paragraph";
5
5
  import * as Switch from "@radix-ui/react-switch";
6
6
  import c from "classnames";
7
7
 
8
- import styles from "./toggle.module.scss";
9
8
  import { DraggableX } from "./DraggableX";
10
- import { useToggleDrag } from "./useToggleDrag";
9
+ import { useToggleDrag } from "./hooks/useToggleDrag";
10
+ import styles from "./toggle.module.scss";
11
11
 
12
12
  export type ToggleProps = {
13
13
  /**
@@ -1 +0,0 @@
1
- {"version":3,"file":"useToggleDrag.d.ts","sourceRoot":"","sources":["../src/useToggleDrag.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAqC,MAAM,OAAO,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,eAAO,MAAM,aAAa,0BAA2B,IAAI,CAAC,WAAW,EAAE,SAAS,GAAG,UAAU,CAAC;;;;;;;;;oBAqBrE;QAAE,CAAC,EAAE,MAAM,CAAA;KAAE;;;CAiCrC,CAAC"}