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