@gbgr/react-headless 0.1.0
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/LICENSE +21 -0
- package/README.md +41 -0
- package/dist/accordion/useAccordion.d.ts +38 -0
- package/dist/accordion/useAccordion.d.ts.map +1 -0
- package/dist/accordion/useAccordion.js +125 -0
- package/dist/button/useButton.d.ts +25 -0
- package/dist/button/useButton.d.ts.map +1 -0
- package/dist/button/useButton.js +129 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/internal/composeEventHandlers.d.ts +7 -0
- package/dist/internal/composeEventHandlers.d.ts.map +1 -0
- package/dist/internal/composeEventHandlers.js +9 -0
- package/dist/internal/types.d.ts +4 -0
- package/dist/internal/types.d.ts.map +1 -0
- package/dist/internal/types.js +1 -0
- package/dist/mode-toggle/useModeToggle.d.ts +15 -0
- package/dist/mode-toggle/useModeToggle.d.ts.map +1 -0
- package/dist/mode-toggle/useModeToggle.js +39 -0
- package/dist/text-field/useTextField.d.ts +14 -0
- package/dist/text-field/useTextField.d.ts.map +1 -0
- package/dist/text-field/useTextField.js +26 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 gbgr
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
## @gbgr/react-headless
|
|
2
|
+
|
|
3
|
+
React headless primitives package.
|
|
4
|
+
|
|
5
|
+
### Button
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useButton } from "@gbgr/react-headless";
|
|
9
|
+
|
|
10
|
+
export function Example() {
|
|
11
|
+
const { buttonProps } = useButton({
|
|
12
|
+
type: "button",
|
|
13
|
+
onPress: () => console.log("pressed"),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<button {...buttonProps}>
|
|
18
|
+
Save
|
|
19
|
+
</button>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Button interaction state machine
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { useButton } from "@gbgr/react-headless";
|
|
28
|
+
|
|
29
|
+
export function Example() {
|
|
30
|
+
const { buttonProps, state } = useButton({
|
|
31
|
+
disabled: false,
|
|
32
|
+
loading: false,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<button {...buttonProps}>
|
|
37
|
+
State: {state}
|
|
38
|
+
</button>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { DataAttributes } from "../internal/types";
|
|
3
|
+
export type AccordionType = "single" | "multiple";
|
|
4
|
+
type AccordionSingleValue = string | null;
|
|
5
|
+
type AccordionMultipleValue = string[];
|
|
6
|
+
export type UseAccordionProps = {
|
|
7
|
+
type?: AccordionType;
|
|
8
|
+
value?: AccordionSingleValue | AccordionMultipleValue;
|
|
9
|
+
defaultValue?: AccordionSingleValue | AccordionMultipleValue;
|
|
10
|
+
onValueChange?: (value: AccordionSingleValue | AccordionMultipleValue) => void;
|
|
11
|
+
/**
|
|
12
|
+
* Only applies to `type="single"`.
|
|
13
|
+
* - `true`: clicking an open item can close it (value becomes `null`)
|
|
14
|
+
* - `false`: at least one item stays open once opened
|
|
15
|
+
*/
|
|
16
|
+
collapsible?: boolean;
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
};
|
|
19
|
+
type GetItemOptions = {
|
|
20
|
+
value: string;
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
};
|
|
23
|
+
type GetTriggerOptions = Omit<GetItemOptions, "disabled"> & {
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
};
|
|
26
|
+
export declare function useAccordion(props: UseAccordionProps): {
|
|
27
|
+
type: AccordionType;
|
|
28
|
+
value: AccordionSingleValue | AccordionMultipleValue;
|
|
29
|
+
disabled: boolean;
|
|
30
|
+
isItemOpen: (itemValue: string) => boolean;
|
|
31
|
+
setItemOpen: (itemValue: string, nextOpen: boolean) => void;
|
|
32
|
+
toggleItem: (itemValue: string) => void;
|
|
33
|
+
getItemProps: (options: GetItemOptions, props?: React.HTMLAttributes<HTMLDivElement>) => React.HTMLAttributes<HTMLDivElement> & DataAttributes;
|
|
34
|
+
getTriggerProps: (options: GetTriggerOptions, props?: React.ButtonHTMLAttributes<HTMLButtonElement>) => React.ButtonHTMLAttributes<HTMLButtonElement> & DataAttributes;
|
|
35
|
+
getContentProps: (options: Pick<GetItemOptions, "value">, props?: React.HTMLAttributes<HTMLDivElement>) => React.HTMLAttributes<HTMLDivElement> & DataAttributes;
|
|
36
|
+
};
|
|
37
|
+
export {};
|
|
38
|
+
//# sourceMappingURL=useAccordion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAccordion.d.ts","sourceRoot":"","sources":["../../src/accordion/useAccordion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAG9B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAEvD,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,UAAU,CAAA;AAEjD,KAAK,oBAAoB,GAAG,MAAM,GAAG,IAAI,CAAA;AACzC,KAAK,sBAAsB,GAAG,MAAM,EAAE,CAAA;AAEtC,MAAM,MAAM,iBAAiB,GAAG;IAC/B,IAAI,CAAC,EAAE,aAAa,CAAA;IACpB,KAAK,CAAC,EAAE,oBAAoB,GAAG,sBAAsB,CAAA;IACrD,YAAY,CAAC,EAAE,oBAAoB,GAAG,sBAAsB,CAAA;IAC5D,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,oBAAoB,GAAG,sBAAsB,KAAK,IAAI,CAAA;IAC9E;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,KAAK,cAAc,GAAG;IACrB,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,KAAK,iBAAiB,GAAG,IAAI,CAAC,cAAc,EAAE,UAAU,CAAC,GAAG;IAC3D,QAAQ,CAAC,EAAE,OAAO,CAAA;CAClB,CAAA;AAMD,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB;;;;4BAwCvC,MAAM;6BAUN,MAAM,YAAY,OAAO;4BA2BzB,MAAM;4BA2BR,cAAc,UAAU,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,KAOhE,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,GAAG,cAAc;+BAOjD,iBAAiB,UAClB,KAAK,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,KAyBhD,KAAK,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,GAAG,cAAc;+BAO1D,IAAI,CAAC,cAAc,EAAE,OAAO,CAAC,UAC9B,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,KAevC,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,GAAG,cAAc;EAgB5D"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { composeEventHandlers } from "../internal/composeEventHandlers";
|
|
3
|
+
function toIdSegment(value) {
|
|
4
|
+
return encodeURIComponent(value).replace(/%/g, "_");
|
|
5
|
+
}
|
|
6
|
+
export function useAccordion(props) {
|
|
7
|
+
const { type = "single", value, defaultValue, onValueChange, collapsible = false, disabled = false, } = props;
|
|
8
|
+
const baseId = React.useId();
|
|
9
|
+
const [uncontrolledSingle, setUncontrolledSingle] = React.useState(typeof defaultValue === "string" ? defaultValue : null);
|
|
10
|
+
const [uncontrolledMultiple, setUncontrolledMultiple] = React.useState(Array.isArray(defaultValue) ? defaultValue : []);
|
|
11
|
+
const currentValue = value ??
|
|
12
|
+
(type === "multiple" ? uncontrolledMultiple : uncontrolledSingle);
|
|
13
|
+
const setValue = React.useCallback((next) => {
|
|
14
|
+
if (value === undefined) {
|
|
15
|
+
if (type === "multiple") {
|
|
16
|
+
setUncontrolledMultiple(Array.isArray(next) ? next : []);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
setUncontrolledSingle(typeof next === "string" ? next : null);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
onValueChange?.(next);
|
|
23
|
+
}, [type, value, onValueChange]);
|
|
24
|
+
const isItemOpen = React.useCallback((itemValue) => {
|
|
25
|
+
if (type === "multiple") {
|
|
26
|
+
return Array.isArray(currentValue) && currentValue.includes(itemValue);
|
|
27
|
+
}
|
|
28
|
+
return currentValue === itemValue;
|
|
29
|
+
}, [type, currentValue]);
|
|
30
|
+
const setItemOpen = React.useCallback((itemValue, nextOpen) => {
|
|
31
|
+
if (disabled)
|
|
32
|
+
return;
|
|
33
|
+
if (type === "multiple") {
|
|
34
|
+
const prev = Array.isArray(currentValue) ? currentValue : [];
|
|
35
|
+
const next = nextOpen
|
|
36
|
+
? prev.includes(itemValue)
|
|
37
|
+
? prev
|
|
38
|
+
: [...prev, itemValue]
|
|
39
|
+
: prev.filter((v) => v !== itemValue);
|
|
40
|
+
setValue(next);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (nextOpen) {
|
|
44
|
+
setValue(itemValue);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (collapsible) {
|
|
48
|
+
setValue(null);
|
|
49
|
+
}
|
|
50
|
+
}, [type, currentValue, setValue, disabled, collapsible]);
|
|
51
|
+
const toggleItem = React.useCallback((itemValue) => {
|
|
52
|
+
setItemOpen(itemValue, !isItemOpen(itemValue));
|
|
53
|
+
}, [isItemOpen, setItemOpen]);
|
|
54
|
+
const getItemIds = React.useCallback((itemValue) => {
|
|
55
|
+
const seg = toIdSegment(itemValue);
|
|
56
|
+
return {
|
|
57
|
+
triggerId: `${baseId}-accordion-trigger-${seg}`,
|
|
58
|
+
contentId: `${baseId}-accordion-content-${seg}`,
|
|
59
|
+
};
|
|
60
|
+
}, [baseId]);
|
|
61
|
+
const getItemState = React.useCallback((itemValue, itemDisabled) => {
|
|
62
|
+
const open = isItemOpen(itemValue);
|
|
63
|
+
const itemIsDisabled = disabled || itemDisabled;
|
|
64
|
+
return { open, itemIsDisabled };
|
|
65
|
+
}, [disabled, isItemOpen]);
|
|
66
|
+
const getItemProps = React.useCallback((options, props) => {
|
|
67
|
+
const { value: itemValue, disabled: itemDisabled } = options;
|
|
68
|
+
const { open, itemIsDisabled } = getItemState(itemValue, itemDisabled);
|
|
69
|
+
return {
|
|
70
|
+
...props,
|
|
71
|
+
"data-state": open ? "open" : "closed",
|
|
72
|
+
"data-disabled": itemIsDisabled ? "" : undefined,
|
|
73
|
+
};
|
|
74
|
+
}, [getItemState]);
|
|
75
|
+
const getTriggerProps = React.useCallback((options, props) => {
|
|
76
|
+
const { value: itemValue, disabled: itemDisabled } = options;
|
|
77
|
+
const ids = getItemIds(itemValue);
|
|
78
|
+
const { open, itemIsDisabled } = getItemState(itemValue, itemDisabled);
|
|
79
|
+
const handleClickInternal = (event) => {
|
|
80
|
+
if (itemIsDisabled) {
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
event.stopPropagation();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
toggleItem(itemValue);
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
...props,
|
|
89
|
+
id: props?.id ?? ids.triggerId,
|
|
90
|
+
type: "button",
|
|
91
|
+
disabled: itemIsDisabled,
|
|
92
|
+
"aria-expanded": open,
|
|
93
|
+
"aria-controls": ids.contentId,
|
|
94
|
+
"data-state": open ? "open" : "closed",
|
|
95
|
+
"data-disabled": itemIsDisabled ? "" : undefined,
|
|
96
|
+
onClick: composeEventHandlers(props?.onClick, handleClickInternal),
|
|
97
|
+
};
|
|
98
|
+
}, [getItemIds, getItemState, toggleItem]);
|
|
99
|
+
const getContentProps = React.useCallback((options, props) => {
|
|
100
|
+
const { value: itemValue } = options;
|
|
101
|
+
const ids = getItemIds(itemValue);
|
|
102
|
+
const open = isItemOpen(itemValue);
|
|
103
|
+
return {
|
|
104
|
+
...props,
|
|
105
|
+
id: props?.id ?? ids.contentId,
|
|
106
|
+
role: "region",
|
|
107
|
+
"aria-labelledby": ids.triggerId,
|
|
108
|
+
"data-state": open ? "open" : "closed",
|
|
109
|
+
"data-hidden": open ? undefined : "",
|
|
110
|
+
hidden: open ? undefined : true,
|
|
111
|
+
"aria-hidden": open ? undefined : true,
|
|
112
|
+
};
|
|
113
|
+
}, [getItemIds, isItemOpen]);
|
|
114
|
+
return {
|
|
115
|
+
type,
|
|
116
|
+
value: currentValue,
|
|
117
|
+
disabled,
|
|
118
|
+
isItemOpen,
|
|
119
|
+
setItemOpen,
|
|
120
|
+
toggleItem,
|
|
121
|
+
getItemProps,
|
|
122
|
+
getTriggerProps,
|
|
123
|
+
getContentProps,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { DataAttributes } from "../internal/types";
|
|
3
|
+
export type ButtonPressEvent = React.MouseEvent | React.KeyboardEvent;
|
|
4
|
+
type InteractionState = "idle" | "hovered" | "pressed" | "disabled" | "loading";
|
|
5
|
+
export type UseButtonProps = Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "disabled" | "type"> & {
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
loading?: boolean;
|
|
8
|
+
type?: "button" | "submit" | "reset";
|
|
9
|
+
onPress?: (event: ButtonPressEvent) => void;
|
|
10
|
+
};
|
|
11
|
+
export declare function useButton(props: UseButtonProps): {
|
|
12
|
+
state: InteractionState;
|
|
13
|
+
isDisabled: boolean;
|
|
14
|
+
isLoading: boolean;
|
|
15
|
+
events: {
|
|
16
|
+
pointerEnter: () => void;
|
|
17
|
+
pointerLeave: () => void;
|
|
18
|
+
pointerDown: (pointerId: number) => void;
|
|
19
|
+
pointerUp: (pointerId: number, isInside: boolean) => void;
|
|
20
|
+
pointerCancel: () => void;
|
|
21
|
+
};
|
|
22
|
+
buttonProps: React.ButtonHTMLAttributes<HTMLButtonElement> & DataAttributes;
|
|
23
|
+
};
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=useButton.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useButton.d.ts","sourceRoot":"","sources":["../../src/button/useButton.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,MAAM,MAAM,gBAAgB,GAAG,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,aAAa,CAAC;AAEtE,KAAK,gBAAgB,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS,CAAC;AAEhF,MAAM,MAAM,cAAc,GAAG,IAAI,CAChC,KAAK,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,EAC7C,UAAU,GAAG,MAAM,CACnB,GAAG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,IAAI,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,OAAO,CAAC;IACrC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAC;CAC5C,CAAC;AAkBF,wBAAgB,SAAS,CAAC,KAAK,EAAE,cAAc;;;;;;;iCA2EnB,MAAM;+BAER,MAAM,YAAY,OAAO;;;iBA+F5C,KAAK,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,GAAG,cAAc;EAEpE"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { composeEventHandlers } from "../internal/composeEventHandlers";
|
|
3
|
+
function isPointInRect(clientX, clientY, rect) {
|
|
4
|
+
return (clientX >= rect.left &&
|
|
5
|
+
clientX <= rect.right &&
|
|
6
|
+
clientY >= rect.top &&
|
|
7
|
+
clientY <= rect.bottom);
|
|
8
|
+
}
|
|
9
|
+
export function useButton(props) {
|
|
10
|
+
const { disabled = false, loading = false, type = "button", onPress, onClick, onPointerEnter, onPointerLeave, onPointerDown, onPointerUp, onPointerCancel, ...buttonProps } = props;
|
|
11
|
+
const isDisabled = disabled || loading;
|
|
12
|
+
const [internalState, setInternalState] = React.useState("idle");
|
|
13
|
+
const isPressedRef = React.useRef(false);
|
|
14
|
+
React.useEffect(() => {
|
|
15
|
+
if (isDisabled) {
|
|
16
|
+
isPressedRef.current = false;
|
|
17
|
+
setInternalState("idle");
|
|
18
|
+
}
|
|
19
|
+
}, [isDisabled]);
|
|
20
|
+
const interactionState = disabled
|
|
21
|
+
? "disabled"
|
|
22
|
+
: loading
|
|
23
|
+
? "loading"
|
|
24
|
+
: internalState;
|
|
25
|
+
const send = React.useCallback((event) => {
|
|
26
|
+
if (isDisabled)
|
|
27
|
+
return;
|
|
28
|
+
setInternalState((prev) => {
|
|
29
|
+
switch (event.type) {
|
|
30
|
+
case "POINTER_ENTER": {
|
|
31
|
+
if (isPressedRef.current)
|
|
32
|
+
return prev;
|
|
33
|
+
return "hovered";
|
|
34
|
+
}
|
|
35
|
+
case "POINTER_LEAVE": {
|
|
36
|
+
if (isPressedRef.current)
|
|
37
|
+
return prev;
|
|
38
|
+
return "idle";
|
|
39
|
+
}
|
|
40
|
+
case "POINTER_DOWN": {
|
|
41
|
+
isPressedRef.current = true;
|
|
42
|
+
return "pressed";
|
|
43
|
+
}
|
|
44
|
+
case "POINTER_UP": {
|
|
45
|
+
isPressedRef.current = false;
|
|
46
|
+
return event.isInside ? "hovered" : "idle";
|
|
47
|
+
}
|
|
48
|
+
case "POINTER_CANCEL": {
|
|
49
|
+
isPressedRef.current = false;
|
|
50
|
+
return "idle";
|
|
51
|
+
}
|
|
52
|
+
default: {
|
|
53
|
+
return prev;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}, [isDisabled]);
|
|
58
|
+
const events = React.useMemo(() => ({
|
|
59
|
+
pointerEnter: () => send({ type: "POINTER_ENTER" }),
|
|
60
|
+
pointerLeave: () => send({ type: "POINTER_LEAVE" }),
|
|
61
|
+
pointerDown: (pointerId) => send({ type: "POINTER_DOWN", pointerId }),
|
|
62
|
+
pointerUp: (pointerId, isInside) => send({ type: "POINTER_UP", pointerId, isInside }),
|
|
63
|
+
pointerCancel: () => send({ type: "POINTER_CANCEL" }),
|
|
64
|
+
}), [send]);
|
|
65
|
+
const handlePointerEnterInternal = React.useCallback(() => {
|
|
66
|
+
events.pointerEnter();
|
|
67
|
+
}, [events]);
|
|
68
|
+
const handlePointerLeaveInternal = React.useCallback(() => {
|
|
69
|
+
events.pointerLeave();
|
|
70
|
+
}, [events]);
|
|
71
|
+
const handlePointerDownInternal = React.useCallback((event) => {
|
|
72
|
+
if (isDisabled)
|
|
73
|
+
return;
|
|
74
|
+
if (event.button !== 0)
|
|
75
|
+
return;
|
|
76
|
+
events.pointerDown(event.pointerId);
|
|
77
|
+
try {
|
|
78
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// ignore
|
|
82
|
+
}
|
|
83
|
+
}, [events, isDisabled]);
|
|
84
|
+
const handlePointerUpInternal = React.useCallback((event) => {
|
|
85
|
+
if (isDisabled)
|
|
86
|
+
return;
|
|
87
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
88
|
+
const isInside = isPointInRect(event.clientX, event.clientY, rect);
|
|
89
|
+
events.pointerUp(event.pointerId, isInside);
|
|
90
|
+
try {
|
|
91
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// ignore
|
|
95
|
+
}
|
|
96
|
+
}, [events, isDisabled]);
|
|
97
|
+
const handlePointerCancelInternal = React.useCallback(() => {
|
|
98
|
+
events.pointerCancel();
|
|
99
|
+
}, [events]);
|
|
100
|
+
const handleClick = React.useCallback((event) => {
|
|
101
|
+
if (isDisabled) {
|
|
102
|
+
event.preventDefault();
|
|
103
|
+
event.stopPropagation();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
onPress?.(event);
|
|
107
|
+
}, [isDisabled, onPress]);
|
|
108
|
+
return {
|
|
109
|
+
state: interactionState,
|
|
110
|
+
isDisabled,
|
|
111
|
+
isLoading: loading,
|
|
112
|
+
events,
|
|
113
|
+
buttonProps: {
|
|
114
|
+
...buttonProps,
|
|
115
|
+
type,
|
|
116
|
+
disabled: isDisabled,
|
|
117
|
+
"aria-disabled": isDisabled ? true : undefined,
|
|
118
|
+
"data-state": interactionState === "idle" ? undefined : interactionState,
|
|
119
|
+
"data-disabled": disabled ? "" : undefined,
|
|
120
|
+
"data-loading": loading ? "" : undefined,
|
|
121
|
+
onClick: composeEventHandlers(onClick, handleClick),
|
|
122
|
+
onPointerEnter: composeEventHandlers(onPointerEnter, handlePointerEnterInternal),
|
|
123
|
+
onPointerLeave: composeEventHandlers(onPointerLeave, handlePointerLeaveInternal),
|
|
124
|
+
onPointerDown: composeEventHandlers(onPointerDown, handlePointerDownInternal),
|
|
125
|
+
onPointerUp: composeEventHandlers(onPointerUp, handlePointerUpInternal),
|
|
126
|
+
onPointerCancel: composeEventHandlers(onPointerCancel, handlePointerCancelInternal),
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { ButtonPressEvent, UseButtonProps } from "./button/useButton";
|
|
2
|
+
export { useButton } from "./button/useButton";
|
|
3
|
+
export type { AccordionType, UseAccordionProps } from "./accordion/useAccordion";
|
|
4
|
+
export { useAccordion } from "./accordion/useAccordion";
|
|
5
|
+
export type { ModeToggleValue, UseModeToggleProps } from "./mode-toggle/useModeToggle";
|
|
6
|
+
export { useModeToggle } from "./mode-toggle/useModeToggle";
|
|
7
|
+
export type { UseTextFieldProps } from "./text-field/useTextField";
|
|
8
|
+
export { useTextField } from "./text-field/useTextField";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACvD,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAC3D,YAAY,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
type Handler<E> = ((event: E) => void) | undefined;
|
|
3
|
+
export declare function composeEventHandlers<E extends React.SyntheticEvent>(originalHandler: Handler<E>, ourHandler: Handler<E>, options?: {
|
|
4
|
+
checkDefaultPrevented?: boolean;
|
|
5
|
+
}): (event: E) => void;
|
|
6
|
+
export {};
|
|
7
|
+
//# sourceMappingURL=composeEventHandlers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composeEventHandlers.d.ts","sourceRoot":"","sources":["../../src/internal/composeEventHandlers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;AAEnD,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,KAAK,CAAC,cAAc,EAClE,eAAe,EAAE,OAAO,CAAC,CAAC,CAAC,EAC3B,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,EACtB,OAAO,CAAC,EAAE;IAAE,qBAAqB,CAAC,EAAE,OAAO,CAAA;CAAE,IAErC,OAAO,CAAC,UAShB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/internal/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG;IAAE,CAAC,GAAG,EAAE,QAAQ,MAAM,EAAE,GAAG,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { DataAttributes } from "../internal/types";
|
|
3
|
+
export type ModeToggleValue = "light" | "dark";
|
|
4
|
+
export type UseModeToggleProps = Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "type" | "role" | "aria-checked"> & {
|
|
5
|
+
value?: ModeToggleValue;
|
|
6
|
+
defaultValue?: ModeToggleValue;
|
|
7
|
+
onValueChange?: (value: ModeToggleValue) => void;
|
|
8
|
+
};
|
|
9
|
+
export declare function useModeToggle(props: UseModeToggleProps): {
|
|
10
|
+
value: "light" | "dark";
|
|
11
|
+
setValue: (nextValue: ModeToggleValue) => void;
|
|
12
|
+
toggleValue: () => void;
|
|
13
|
+
buttonProps: React.ButtonHTMLAttributes<HTMLButtonElement> & DataAttributes;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=useModeToggle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useModeToggle.d.ts","sourceRoot":"","sources":["../../src/mode-toggle/useModeToggle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAG9B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAEvD,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,MAAM,CAAA;AAE9C,MAAM,MAAM,kBAAkB,GAAG,IAAI,CACpC,KAAK,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,EAC7C,MAAM,GAAG,MAAM,GAAG,cAAc,CAChC,GAAG;IACH,KAAK,CAAC,EAAE,eAAe,CAAA;IACvB,YAAY,CAAC,EAAE,eAAe,CAAA;IAC9B,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAA;CAChD,CAAA;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB;;0BAgBzC,eAAe;;iBAsCtB,KAAK,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,GAAG,cAAc;EAEpE"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { composeEventHandlers } from "../internal/composeEventHandlers";
|
|
3
|
+
export function useModeToggle(props) {
|
|
4
|
+
const { value, defaultValue = "light", onValueChange, disabled, onClick, ...buttonProps } = props;
|
|
5
|
+
const [uncontrolledValue, setUncontrolledValue] = React.useState(defaultValue);
|
|
6
|
+
const currentValue = value ?? uncontrolledValue;
|
|
7
|
+
const setValue = React.useCallback((nextValue) => {
|
|
8
|
+
if (value === undefined) {
|
|
9
|
+
setUncontrolledValue(nextValue);
|
|
10
|
+
}
|
|
11
|
+
onValueChange?.(nextValue);
|
|
12
|
+
}, [value, onValueChange]);
|
|
13
|
+
const toggleValue = React.useCallback(() => {
|
|
14
|
+
const next = currentValue === "light" ? "dark" : "light";
|
|
15
|
+
setValue(next);
|
|
16
|
+
}, [currentValue, setValue]);
|
|
17
|
+
const handleClickInternal = React.useCallback((event) => {
|
|
18
|
+
if (disabled) {
|
|
19
|
+
event.preventDefault();
|
|
20
|
+
event.stopPropagation();
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
toggleValue();
|
|
24
|
+
}, [disabled, toggleValue]);
|
|
25
|
+
return {
|
|
26
|
+
value: currentValue,
|
|
27
|
+
setValue,
|
|
28
|
+
toggleValue,
|
|
29
|
+
buttonProps: {
|
|
30
|
+
...buttonProps,
|
|
31
|
+
type: "button",
|
|
32
|
+
role: "switch",
|
|
33
|
+
"aria-checked": currentValue === "dark",
|
|
34
|
+
disabled,
|
|
35
|
+
"data-value": currentValue,
|
|
36
|
+
onClick: composeEventHandlers(onClick, handleClickInternal),
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type UseTextFieldProps = {
|
|
2
|
+
type?: string;
|
|
3
|
+
disabled?: boolean;
|
|
4
|
+
passwordVisible?: boolean;
|
|
5
|
+
defaultPasswordVisible?: boolean;
|
|
6
|
+
onPasswordVisibleChange?: (visible: boolean) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare function useTextField(props: UseTextFieldProps): {
|
|
9
|
+
isPassword: boolean;
|
|
10
|
+
isPasswordVisible: boolean;
|
|
11
|
+
inputType: string;
|
|
12
|
+
togglePasswordVisible: () => void;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=useTextField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTextField.d.ts","sourceRoot":"","sources":["../../src/text-field/useTextField.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,iBAAiB,GAAG;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC,uBAAuB,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAA;CACpD,CAAA;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB;;;;;EAwCpD"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
export function useTextField(props) {
|
|
3
|
+
const { type = "text", disabled = false, passwordVisible, defaultPasswordVisible = false, onPasswordVisibleChange, } = props;
|
|
4
|
+
const isPassword = type === "password";
|
|
5
|
+
const [uncontrolledVisible, setUncontrolledVisible] = React.useState(defaultPasswordVisible);
|
|
6
|
+
const isPasswordVisible = passwordVisible ?? (isPassword ? uncontrolledVisible : false);
|
|
7
|
+
const setPasswordVisible = React.useCallback((next) => {
|
|
8
|
+
if (!isPassword)
|
|
9
|
+
return;
|
|
10
|
+
if (passwordVisible === undefined) {
|
|
11
|
+
setUncontrolledVisible(next);
|
|
12
|
+
}
|
|
13
|
+
onPasswordVisibleChange?.(next);
|
|
14
|
+
}, [isPassword, passwordVisible, onPasswordVisibleChange]);
|
|
15
|
+
const togglePasswordVisible = React.useCallback(() => {
|
|
16
|
+
if (disabled)
|
|
17
|
+
return;
|
|
18
|
+
setPasswordVisible(!isPasswordVisible);
|
|
19
|
+
}, [disabled, setPasswordVisible, isPasswordVisible]);
|
|
20
|
+
return {
|
|
21
|
+
isPassword,
|
|
22
|
+
isPasswordVisible,
|
|
23
|
+
inputType: isPassword ? (isPasswordVisible ? "text" : "password") : type,
|
|
24
|
+
togglePasswordVisible,
|
|
25
|
+
};
|
|
26
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gbgr/react-headless",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./*": "./dist/*"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"react": ">=18",
|
|
23
|
+
"react-dom": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/react": "latest",
|
|
27
|
+
"@types/react-dom": "latest",
|
|
28
|
+
"react": "latest",
|
|
29
|
+
"react-dom": "latest",
|
|
30
|
+
"typescript": "latest"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "rm -rf dist tsconfig.tsbuildinfo && tsc -b tsconfig.json --force",
|
|
34
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
35
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo && find src -type f \\( -name '*.js' -o -name '*.js.map' -o -name '*.d.ts' -o -name '*.d.ts.map' \\) -delete"
|
|
36
|
+
}
|
|
37
|
+
}
|