@yamada-ui/segmented-control 0.2.7 → 0.2.8
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/dist/chunk-4HYHR4JU.mjs +301 -0
- package/dist/index.js +219 -192
- package/dist/index.mjs +1 -1
- package/dist/segmented-control.js +219 -192
- package/dist/segmented-control.mjs +1 -1
- package/package.json +7 -7
- package/dist/chunk-4HOIPB3J.mjs +0 -274
|
@@ -37,204 +37,231 @@ var [SegmentedControlProvider, useSegmentedControl] = (0, import_utils.createCon
|
|
|
37
37
|
strict: false,
|
|
38
38
|
name: "SegmentedControlContext"
|
|
39
39
|
});
|
|
40
|
-
var SegmentedControl = (0, import_core.forwardRef)(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
let { width, height } = el.getBoundingClientRect();
|
|
74
|
-
const x = el.offsetLeft - gutterX;
|
|
75
|
-
const y = el.offsetTop - gutterY;
|
|
76
|
-
width = width * (el.offsetWidth / width) || 0;
|
|
77
|
-
height = height * (el.offsetWidth / width) || 0;
|
|
78
|
-
setActivePosition({ width, height, x, y });
|
|
79
|
-
}, [focusedIndex, containerRect, labelRefs, observerRef, value]);
|
|
80
|
-
const onChange = (0, import_react.useCallback)(
|
|
81
|
-
(ev) => {
|
|
82
|
-
if (isDisabled || isReadOnly) {
|
|
83
|
-
ev.preventDefault();
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
setValue(ev.target.value);
|
|
87
|
-
},
|
|
88
|
-
[isDisabled, isReadOnly, setValue]
|
|
89
|
-
);
|
|
90
|
-
const onFocus = (0, import_react.useCallback)(
|
|
91
|
-
(index, skip) => {
|
|
92
|
-
if (isDisabled)
|
|
40
|
+
var SegmentedControl = (0, import_core.forwardRef)(
|
|
41
|
+
(props, ref) => {
|
|
42
|
+
const [styles, mergedProps] = (0, import_core.useMultiComponentStyle)(
|
|
43
|
+
"SegmentedControl",
|
|
44
|
+
props
|
|
45
|
+
);
|
|
46
|
+
let { className, id, name, isReadOnly, isDisabled, children, ...rest } = (0, import_core.omitThemeProps)(mergedProps);
|
|
47
|
+
id = id != null ? id : (0, import_react.useId)();
|
|
48
|
+
name = name != null ? name : `segmented-control-${(0, import_react.useId)()}`;
|
|
49
|
+
rest.onChange = (0, import_utils.useCallbackRef)(rest.onChange);
|
|
50
|
+
const descendants = useDescendants();
|
|
51
|
+
const [focusedIndex, setFocusedIndex] = (0, import_react.useState)(-1);
|
|
52
|
+
const [isFocusVisible, setIsFocusVisible] = (0, import_react.useState)(false);
|
|
53
|
+
const [observerRef, containerRect] = (0, import_use_resize_observer.useResizeObserver)();
|
|
54
|
+
const containerRef = (0, import_react.useRef)(null);
|
|
55
|
+
const labelRefs = (0, import_react.useRef)(/* @__PURE__ */ new Map());
|
|
56
|
+
const [activePosition, setActivePosition] = (0, import_react.useState)({
|
|
57
|
+
width: 0,
|
|
58
|
+
height: 0,
|
|
59
|
+
x: 0,
|
|
60
|
+
y: 0
|
|
61
|
+
});
|
|
62
|
+
const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
|
|
63
|
+
value: rest.value,
|
|
64
|
+
defaultValue: rest.defaultValue,
|
|
65
|
+
onChange: rest.onChange
|
|
66
|
+
});
|
|
67
|
+
(0, import_react.useEffect)(() => {
|
|
68
|
+
return (0, import_use_focus_visible.trackFocusVisible)(setIsFocusVisible);
|
|
69
|
+
}, []);
|
|
70
|
+
(0, import_react.useEffect)(() => {
|
|
71
|
+
const el = labelRefs.current.get(value);
|
|
72
|
+
if (!el || !containerRef.current || !observerRef.current)
|
|
93
73
|
return;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
ref: (0, import_utils.mergeRefs)(containerRef, observerRef, ref2),
|
|
110
|
-
id,
|
|
111
|
-
"aria-disabled": (0, import_utils.ariaAttr)(isDisabled),
|
|
112
|
-
"aria-readonly": (0, import_utils.ariaAttr)(isReadOnly),
|
|
113
|
-
onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur)
|
|
114
|
-
}),
|
|
115
|
-
[id, isDisabled, isReadOnly, observerRef, onBlur, rest]
|
|
116
|
-
);
|
|
117
|
-
const getActiveProps = (0, import_react.useCallback)(
|
|
118
|
-
(props2 = {}, ref2 = null) => {
|
|
119
|
-
const { width, height, x, y } = activePosition;
|
|
120
|
-
return {
|
|
121
|
-
...props2,
|
|
122
|
-
ref: ref2,
|
|
123
|
-
style: {
|
|
124
|
-
position: "absolute",
|
|
125
|
-
zIndex: 1,
|
|
126
|
-
width,
|
|
127
|
-
height,
|
|
128
|
-
transform: `translate(${x}px, ${y}px)`
|
|
74
|
+
const { paddingLeft, paddingTop } = getComputedStyle(containerRef.current);
|
|
75
|
+
const gutterX = parseFloat(paddingLeft) || 0;
|
|
76
|
+
const gutterY = parseFloat(paddingTop) || 0;
|
|
77
|
+
let { width, height } = el.getBoundingClientRect();
|
|
78
|
+
const x = el.offsetLeft - gutterX;
|
|
79
|
+
const y = el.offsetTop - gutterY;
|
|
80
|
+
width = width * (el.offsetWidth / width) || 0;
|
|
81
|
+
height = height * (el.offsetWidth / width) || 0;
|
|
82
|
+
setActivePosition({ width, height, x, y });
|
|
83
|
+
}, [focusedIndex, containerRect, labelRefs, observerRef, value]);
|
|
84
|
+
const onChange = (0, import_react.useCallback)(
|
|
85
|
+
(ev) => {
|
|
86
|
+
if (isDisabled || isReadOnly) {
|
|
87
|
+
ev.preventDefault();
|
|
88
|
+
return;
|
|
129
89
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
props2
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
90
|
+
setValue(ev.target.value);
|
|
91
|
+
},
|
|
92
|
+
[isDisabled, isReadOnly, setValue]
|
|
93
|
+
);
|
|
94
|
+
const onFocus = (0, import_react.useCallback)(
|
|
95
|
+
(index, skip) => {
|
|
96
|
+
if (isDisabled)
|
|
97
|
+
return;
|
|
98
|
+
if (skip) {
|
|
99
|
+
const next = descendants.enabledNextValue(index);
|
|
100
|
+
if (next)
|
|
101
|
+
setFocusedIndex(next.index);
|
|
102
|
+
} else {
|
|
103
|
+
setFocusedIndex(index);
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
[descendants, isDisabled]
|
|
107
|
+
);
|
|
108
|
+
const onBlur = (0, import_react.useCallback)(() => setFocusedIndex(-1), []);
|
|
109
|
+
const getContainerProps = (0, import_react.useCallback)(
|
|
110
|
+
(props2 = {}, ref2 = null) => ({
|
|
111
|
+
...(0, import_utils.omitObject)(rest, ["value", "defaultValue", "onChange"]),
|
|
112
|
+
...props2,
|
|
113
|
+
ref: (0, import_utils.mergeRefs)(containerRef, observerRef, ref2),
|
|
114
|
+
id,
|
|
115
|
+
"aria-disabled": (0, import_utils.ariaAttr)(isDisabled),
|
|
116
|
+
"aria-readonly": (0, import_utils.ariaAttr)(isReadOnly),
|
|
117
|
+
onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur)
|
|
118
|
+
}),
|
|
119
|
+
[id, isDisabled, isReadOnly, observerRef, onBlur, rest]
|
|
120
|
+
);
|
|
121
|
+
const getActiveProps = (0, import_react.useCallback)(
|
|
122
|
+
(props2 = {}, ref2 = null) => {
|
|
123
|
+
const { width, height, x, y } = activePosition;
|
|
124
|
+
return {
|
|
125
|
+
...props2,
|
|
126
|
+
ref: ref2,
|
|
127
|
+
style: {
|
|
128
|
+
position: "absolute",
|
|
129
|
+
zIndex: 1,
|
|
130
|
+
width,
|
|
131
|
+
height,
|
|
132
|
+
transform: `translate(${x}px, ${y}px)`
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
[activePosition]
|
|
137
|
+
);
|
|
138
|
+
const getInputProps = (0, import_react.useCallback)(
|
|
139
|
+
({ index, ...props2 } = {}, ref2 = null) => {
|
|
140
|
+
var _a, _b, _c, _d;
|
|
141
|
+
const disabled = (_b = (_a = props2.disabled) != null ? _a : props2.isDisabled) != null ? _b : isDisabled;
|
|
142
|
+
const readOnly = (_d = (_c = props2.readOnly) != null ? _c : props2.isReadOnly) != null ? _d : isReadOnly;
|
|
143
|
+
const checked = props2.value === value;
|
|
144
|
+
return {
|
|
145
|
+
...(0, import_utils.omitObject)(props2, ["isDisabled", "isReadOnly"]),
|
|
146
|
+
ref: ref2,
|
|
147
|
+
id: `${id}-${index}`,
|
|
148
|
+
type: "radio",
|
|
149
|
+
name,
|
|
150
|
+
disabled: disabled || readOnly,
|
|
151
|
+
readOnly,
|
|
152
|
+
checked,
|
|
153
|
+
"aria-disabled": (0, import_utils.ariaAttr)(disabled),
|
|
154
|
+
"aria-readonly": (0, import_utils.ariaAttr)(readOnly),
|
|
155
|
+
"data-checked": (0, import_utils.dataAttr)(checked),
|
|
156
|
+
"data-focus": (0, import_utils.dataAttr)(index === focusedIndex),
|
|
157
|
+
style: {
|
|
158
|
+
border: "0px",
|
|
159
|
+
clip: "rect(0px, 0px, 0px, 0px)",
|
|
160
|
+
height: "1px",
|
|
161
|
+
width: "1px",
|
|
162
|
+
margin: "-1px",
|
|
163
|
+
padding: "0px",
|
|
164
|
+
overflow: "hidden",
|
|
165
|
+
whiteSpace: "nowrap",
|
|
166
|
+
position: "absolute"
|
|
167
|
+
},
|
|
168
|
+
onChange: (0, import_utils.handlerAll)(
|
|
169
|
+
props2.onChange,
|
|
170
|
+
(ev) => !disabled && !readOnly ? onChange(ev) : {}
|
|
171
|
+
)
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
[isDisabled, isReadOnly, value, id, name, focusedIndex, onChange]
|
|
175
|
+
);
|
|
176
|
+
const getLabelProps = (0, import_react.useCallback)(
|
|
177
|
+
({ index, ...props2 } = {}, ref2 = null) => {
|
|
178
|
+
var _a, _b, _c, _d;
|
|
179
|
+
const disabled = (_b = (_a = props2.disabled) != null ? _a : props2.isDisabled) != null ? _b : isDisabled;
|
|
180
|
+
const readOnly = (_d = (_c = props2.readOnly) != null ? _c : props2.isReadOnly) != null ? _d : isReadOnly;
|
|
181
|
+
const checked = props2.value === value;
|
|
182
|
+
const focused = index === focusedIndex;
|
|
183
|
+
return {
|
|
184
|
+
props: props2,
|
|
185
|
+
ref: (0, import_utils.mergeRefs)(
|
|
186
|
+
(node) => labelRefs.current.set(props2.value, node),
|
|
187
|
+
ref2
|
|
188
|
+
),
|
|
189
|
+
"aria-disabled": (0, import_utils.ariaAttr)(disabled),
|
|
190
|
+
"aria-readonly": (0, import_utils.ariaAttr)(readOnly),
|
|
191
|
+
"data-checked": (0, import_utils.dataAttr)(checked),
|
|
192
|
+
"data-focus": (0, import_utils.dataAttr)(focused),
|
|
193
|
+
"data-focus-visible": (0, import_utils.dataAttr)(focused && isFocusVisible),
|
|
194
|
+
onFocus: (0, import_utils.handlerAll)(
|
|
195
|
+
props2.onFocus,
|
|
196
|
+
() => onFocus(index, disabled || readOnly)
|
|
197
|
+
),
|
|
198
|
+
...disabled || readOnly ? {
|
|
199
|
+
_hover: {},
|
|
200
|
+
_active: {},
|
|
201
|
+
_focus: {},
|
|
202
|
+
_invalid: {},
|
|
203
|
+
_focusVisible: {}
|
|
204
|
+
} : {},
|
|
205
|
+
style: { position: "relative", zIndex: 2 }
|
|
206
|
+
};
|
|
207
|
+
},
|
|
208
|
+
[focusedIndex, isDisabled, isFocusVisible, isReadOnly, onFocus, value]
|
|
209
|
+
);
|
|
210
|
+
const css = {
|
|
211
|
+
position: "relative",
|
|
212
|
+
display: "inline-flex",
|
|
213
|
+
alignItems: "center",
|
|
214
|
+
...styles.container
|
|
215
|
+
};
|
|
216
|
+
const validChildren = (0, import_utils.getValidChildren)(children);
|
|
217
|
+
if (value == null && rest.defaultValue == null) {
|
|
218
|
+
for (const child of validChildren) {
|
|
219
|
+
if (child.type !== SegmentedControlButton)
|
|
220
|
+
continue;
|
|
221
|
+
const value2 = child.props.value;
|
|
222
|
+
setValue(value2);
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
214
225
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
__css: css,
|
|
222
|
-
children: [
|
|
223
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
224
|
-
import_core.ui.span,
|
|
226
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DescendantsContextProvider, { value: descendants, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
227
|
+
SegmentedControlProvider,
|
|
228
|
+
{
|
|
229
|
+
value: { getInputProps, getLabelProps, styles },
|
|
230
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
231
|
+
import_core.ui.div,
|
|
225
232
|
{
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
__css:
|
|
233
|
+
...getContainerProps({}, ref),
|
|
234
|
+
className: (0, import_utils.cx)("ui-segmented-control", className),
|
|
235
|
+
__css: css,
|
|
236
|
+
children: [
|
|
237
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
238
|
+
import_core.ui.span,
|
|
239
|
+
{
|
|
240
|
+
className: "ui-segmented-control-active",
|
|
241
|
+
...getActiveProps(),
|
|
242
|
+
__css: styles.active
|
|
243
|
+
}
|
|
244
|
+
),
|
|
245
|
+
validChildren
|
|
246
|
+
]
|
|
229
247
|
}
|
|
230
|
-
)
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
});
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
) });
|
|
251
|
+
}
|
|
252
|
+
);
|
|
236
253
|
var SegmentedControlButton = (0, import_core.forwardRef)(
|
|
237
|
-
({
|
|
254
|
+
({
|
|
255
|
+
className,
|
|
256
|
+
disabled,
|
|
257
|
+
readOnly,
|
|
258
|
+
isDisabled,
|
|
259
|
+
isReadOnly,
|
|
260
|
+
value,
|
|
261
|
+
onChange,
|
|
262
|
+
children,
|
|
263
|
+
...rest
|
|
264
|
+
}, ref) => {
|
|
238
265
|
const { getInputProps, getLabelProps, styles } = useSegmentedControl();
|
|
239
266
|
const { index, register } = useDescendant({
|
|
240
267
|
disabled: isDisabled || isReadOnly
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yamada-ui/segmented-control",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "Yamada UI segmented control components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"yamada",
|
|
@@ -35,12 +35,12 @@
|
|
|
35
35
|
"url": "https://github.com/hirotomoyamada/yamada-ui/issues"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@yamada-ui/core": "0.5.
|
|
39
|
-
"@yamada-ui/utils": "0.1.
|
|
40
|
-
"@yamada-ui/use-focus-visible": "0.1.
|
|
41
|
-
"@yamada-ui/use-descendant": "0.1.
|
|
42
|
-
"@yamada-ui/use-resize-observer": "0.1.
|
|
43
|
-
"@yamada-ui/use-controllable-state": "0.1.
|
|
38
|
+
"@yamada-ui/core": "0.5.2",
|
|
39
|
+
"@yamada-ui/utils": "0.1.4",
|
|
40
|
+
"@yamada-ui/use-focus-visible": "0.1.4",
|
|
41
|
+
"@yamada-ui/use-descendant": "0.1.5",
|
|
42
|
+
"@yamada-ui/use-resize-observer": "0.1.4",
|
|
43
|
+
"@yamada-ui/use-controllable-state": "0.1.5"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"react": "^18.0.0",
|