@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.
@@ -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)((props, ref) => {
41
- const [styles, mergedProps] = (0, import_core.useMultiComponentStyle)("SegmentedControl", props);
42
- let { className, id, name, isReadOnly, isDisabled, children, ...rest } = (0, import_core.omitThemeProps)(mergedProps);
43
- id = id != null ? id : (0, import_react.useId)();
44
- name = name != null ? name : `segmented-control-${(0, import_react.useId)()}`;
45
- rest.onChange = (0, import_utils.useCallbackRef)(rest.onChange);
46
- const descendants = useDescendants();
47
- const [focusedIndex, setFocusedIndex] = (0, import_react.useState)(-1);
48
- const [isFocusVisible, setIsFocusVisible] = (0, import_react.useState)(false);
49
- const [observerRef, containerRect] = (0, import_use_resize_observer.useResizeObserver)();
50
- const containerRef = (0, import_react.useRef)(null);
51
- const labelRefs = (0, import_react.useRef)(/* @__PURE__ */ new Map());
52
- const [activePosition, setActivePosition] = (0, import_react.useState)({
53
- width: 0,
54
- height: 0,
55
- x: 0,
56
- y: 0
57
- });
58
- const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
59
- value: rest.value,
60
- defaultValue: rest.defaultValue,
61
- onChange: rest.onChange
62
- });
63
- (0, import_react.useEffect)(() => {
64
- return (0, import_use_focus_visible.trackFocusVisible)(setIsFocusVisible);
65
- }, []);
66
- (0, import_react.useEffect)(() => {
67
- const el = labelRefs.current.get(value);
68
- if (!el || !containerRef.current || !observerRef.current)
69
- return;
70
- const { paddingLeft, paddingTop } = getComputedStyle(containerRef.current);
71
- const gutterX = parseFloat(paddingLeft) || 0;
72
- const gutterY = parseFloat(paddingTop) || 0;
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
- if (skip) {
95
- const next = descendants.enabledNextValue(index);
96
- if (next)
97
- setFocusedIndex(next.index);
98
- } else {
99
- setFocusedIndex(index);
100
- }
101
- },
102
- [descendants, isDisabled]
103
- );
104
- const onBlur = (0, import_react.useCallback)(() => setFocusedIndex(-1), []);
105
- const getContainerProps = (0, import_react.useCallback)(
106
- (props2 = {}, ref2 = null) => ({
107
- ...(0, import_utils.omitObject)(rest, ["value", "defaultValue", "onChange"]),
108
- ...props2,
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
- [activePosition]
133
- );
134
- const getInputProps = (0, import_react.useCallback)(
135
- ({ index, ...props2 } = {}, ref2 = null) => {
136
- var _a, _b, _c, _d;
137
- const disabled = (_b = (_a = props2.disabled) != null ? _a : props2.isDisabled) != null ? _b : isDisabled;
138
- const readOnly = (_d = (_c = props2.readOnly) != null ? _c : props2.isReadOnly) != null ? _d : isReadOnly;
139
- const checked = props2.value === value;
140
- return {
141
- ...(0, import_utils.omitObject)(props2, ["isDisabled", "isReadOnly"]),
142
- ref: ref2,
143
- id: `${id}-${index}`,
144
- type: "radio",
145
- name,
146
- disabled: disabled || readOnly,
147
- readOnly,
148
- checked,
149
- "aria-disabled": (0, import_utils.ariaAttr)(disabled),
150
- "aria-readonly": (0, import_utils.ariaAttr)(readOnly),
151
- "data-checked": (0, import_utils.dataAttr)(checked),
152
- "data-focus": (0, import_utils.dataAttr)(index === focusedIndex),
153
- style: {
154
- border: "0px",
155
- clip: "rect(0px, 0px, 0px, 0px)",
156
- height: "1px",
157
- width: "1px",
158
- margin: "-1px",
159
- padding: "0px",
160
- overflow: "hidden",
161
- whiteSpace: "nowrap",
162
- position: "absolute"
163
- },
164
- onChange: (0, import_utils.handlerAll)(
165
- props2.onChange,
166
- (ev) => !disabled && !readOnly ? onChange(ev) : {}
167
- )
168
- };
169
- },
170
- [isDisabled, isReadOnly, value, id, name, focusedIndex, onChange]
171
- );
172
- const getLabelProps = (0, import_react.useCallback)(
173
- ({ index, ...props2 } = {}, ref2 = null) => {
174
- var _a, _b, _c, _d;
175
- const disabled = (_b = (_a = props2.disabled) != null ? _a : props2.isDisabled) != null ? _b : isDisabled;
176
- const readOnly = (_d = (_c = props2.readOnly) != null ? _c : props2.isReadOnly) != null ? _d : isReadOnly;
177
- const checked = props2.value === value;
178
- const focused = index === focusedIndex;
179
- return {
180
- props: props2,
181
- ref: (0, import_utils.mergeRefs)((node) => labelRefs.current.set(props2.value, node), ref2),
182
- "aria-disabled": (0, import_utils.ariaAttr)(disabled),
183
- "aria-readonly": (0, import_utils.ariaAttr)(readOnly),
184
- "data-checked": (0, import_utils.dataAttr)(checked),
185
- "data-focus": (0, import_utils.dataAttr)(focused),
186
- "data-focus-visible": (0, import_utils.dataAttr)(focused && isFocusVisible),
187
- onFocus: (0, import_utils.handlerAll)(props2.onFocus, () => onFocus(index, disabled || readOnly)),
188
- ...disabled || readOnly ? {
189
- _hover: {},
190
- _active: {},
191
- _focus: {},
192
- _invalid: {},
193
- _focusVisible: {}
194
- } : {},
195
- style: { position: "relative", zIndex: 2 }
196
- };
197
- },
198
- [focusedIndex, isDisabled, isFocusVisible, isReadOnly, onFocus, value]
199
- );
200
- const css = {
201
- position: "relative",
202
- display: "inline-flex",
203
- alignItems: "center",
204
- ...styles.container
205
- };
206
- const validChildren = (0, import_utils.getValidChildren)(children);
207
- if (value == null && rest.defaultValue == null) {
208
- for (const child of validChildren) {
209
- if (child.type !== SegmentedControlButton)
210
- continue;
211
- const value2 = child.props.value;
212
- setValue(value2);
213
- break;
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
- 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)(
217
- import_core.ui.div,
218
- {
219
- ...getContainerProps({}, ref),
220
- className: (0, import_utils.cx)("ui-segmented-control", className),
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
- className: "ui-segmented-control-active",
227
- ...getActiveProps(),
228
- __css: styles.active
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
- validChildren
232
- ]
233
- }
234
- ) }) });
235
- });
248
+ )
249
+ }
250
+ ) });
251
+ }
252
+ );
236
253
  var SegmentedControlButton = (0, import_core.forwardRef)(
237
- ({ className, disabled, readOnly, isDisabled, isReadOnly, value, onChange, children, ...rest }, ref) => {
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
@@ -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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yamada-ui/segmented-control",
3
- "version": "0.2.7",
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.1",
39
- "@yamada-ui/utils": "0.1.3",
40
- "@yamada-ui/use-focus-visible": "0.1.3",
41
- "@yamada-ui/use-descendant": "0.1.4",
42
- "@yamada-ui/use-resize-observer": "0.1.3",
43
- "@yamada-ui/use-controllable-state": "0.1.4"
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",