|
1 | | -import { useControllableState } from "@radix-ui/react-use-controllable-state"; |
2 | | -import { useState } from "react"; |
3 | | - |
4 | | -import { dataAttr, elementProps, inputProps } from "@seed-design/dom-utils"; |
5 | | - |
6 | | -export interface UseSwitchStateProps { |
7 | | - checked?: boolean; |
8 | | - |
9 | | - defaultChecked?: boolean; |
10 | | - |
11 | | - onCheckedChange?: (checked: boolean) => void; |
12 | | -} |
13 | | - |
14 | | -export function useSwitchState(props: UseSwitchStateProps) { |
15 | | - const [isChecked, setIsChecked] = useControllableState({ |
16 | | - prop: props.checked, |
17 | | - defaultProp: props.defaultChecked, |
18 | | - onChange: props.onCheckedChange, |
19 | | - }); |
20 | | - const [isHovered, setIsHovered] = useState(false); |
21 | | - const [isActive, setIsActive] = useState(false); |
22 | | - const [isFocused, setIsFocused] = useState(false); |
23 | | - const [isFocusVisible, setIsFocusVisible] = useState(false); |
24 | | - |
25 | | - return { |
26 | | - isChecked, |
27 | | - setIsChecked, |
28 | | - isHovered, |
29 | | - setIsHovered, |
30 | | - isActive, |
31 | | - setIsActive, |
32 | | - isFocused, |
33 | | - setIsFocused, |
34 | | - isFocusVisible, |
35 | | - setIsFocusVisible, |
36 | | - }; |
37 | | -} |
38 | | - |
39 | | -export interface UseSwitchProps extends UseSwitchStateProps { |
40 | | - disabled?: boolean; |
41 | | - |
42 | | - invalid?: boolean; |
43 | | -} |
44 | | - |
45 | | -export function useSwitch(props: UseSwitchProps) { |
46 | | - const { checked, defaultChecked, disabled, invalid, onCheckedChange, ...restProps } = props; |
47 | | - const { |
48 | | - setIsChecked, |
49 | | - isChecked, |
50 | | - setIsHovered, |
51 | | - isHovered, |
52 | | - setIsActive, |
53 | | - isActive, |
54 | | - setIsFocused, |
55 | | - isFocused, |
56 | | - setIsFocusVisible, |
57 | | - isFocusVisible, |
58 | | - } = useSwitchState(props); |
59 | | - |
60 | | - const stateProps = { |
61 | | - "data-checked": dataAttr(isChecked), |
62 | | - "data-hover": dataAttr(isHovered), |
63 | | - "data-active": dataAttr(isActive), |
64 | | - "data-focus": dataAttr(isFocused), |
65 | | - "data-focus-visible": dataAttr(isFocusVisible), |
66 | | - "data-disabled": dataAttr(props.disabled), |
67 | | - }; |
68 | | - |
69 | | - const isControlled = checked != null; |
70 | | - |
71 | | - return { |
72 | | - isChecked, |
73 | | - setIsChecked, |
74 | | - isFocused, |
75 | | - setIsFocused, |
76 | | - setIsFocusVisible, |
77 | | - |
78 | | - restProps, |
79 | | - stateProps, |
80 | | - rootProps: elementProps({ |
81 | | - ...stateProps, |
82 | | - onPointerMove() { |
83 | | - setIsHovered(true); |
84 | | - }, |
85 | | - onPointerDown() { |
86 | | - setIsActive(true); |
87 | | - }, |
88 | | - onPointerUp() { |
89 | | - setIsActive(false); |
90 | | - }, |
91 | | - onPointerLeave() { |
92 | | - setIsHovered(false); |
93 | | - setIsActive(false); |
94 | | - }, |
95 | | - }), |
96 | | - |
97 | | - controlProps: elementProps({ |
98 | | - ...stateProps, |
99 | | - "aria-hidden": true, |
100 | | - }), |
101 | | - |
102 | | - thumbProps: elementProps({ |
103 | | - ...stateProps, |
104 | | - "aria-hidden": true, |
105 | | - }), |
106 | | - |
107 | | - hiddenInputProps: inputProps({ |
108 | | - type: "checkbox", |
109 | | - role: "switch", |
110 | | - checked: isControlled ? isChecked : undefined, |
111 | | - disabled: props.disabled, |
112 | | - "aria-invalid": props.invalid, |
113 | | - ...stateProps, |
114 | | - onChange(event) { |
115 | | - setIsChecked(event.currentTarget.checked); |
116 | | - setIsFocusVisible(event.target.matches(":focus-visible")); |
117 | | - }, |
118 | | - onFocus(event) { |
119 | | - setIsFocused(true); |
120 | | - setIsFocusVisible(event.target.matches(":focus-visible")); |
121 | | - }, |
122 | | - onBlur() { |
123 | | - setIsFocused(false); |
124 | | - setIsFocusVisible(false); |
125 | | - }, |
126 | | - onKeyDown(event) { |
127 | | - if (event.key === " ") { |
128 | | - setIsActive(true); |
129 | | - } |
130 | | - }, |
131 | | - onKeyUp(event) { |
132 | | - if (event.key === " ") { |
133 | | - setIsActive(false); |
134 | | - } |
135 | | - }, |
136 | | - }), |
137 | | - }; |
138 | | -} |
| 1 | +export { |
| 2 | + SwitchRoot, |
| 3 | + SwitchControl, |
| 4 | + SwitchThumb, |
| 5 | + SwitchHiddenInput, |
| 6 | + type SwitchRootProps, |
| 7 | + type SwitchControlProps, |
| 8 | + type SwitchThumbProps, |
| 9 | + type SwitchHiddenInputProps, |
| 10 | +} from "./Switch"; |
| 11 | + |
| 12 | +export { useSwitchContext, type UseSwitchContext } from "./useSwitchContext"; |
| 13 | + |
| 14 | +export * as Switch from "./Switch.namespace"; |
0 commit comments