@yamada-ui/segmented-control 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Hirotomo Yamada
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,28 @@
1
+ # @yamada-ui/segmented-control
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ pnpm add @yamada-ui/segmented-control
7
+ ```
8
+
9
+ or
10
+
11
+ ```sh
12
+ $ yarn add @yamada-ui/segmented-control
13
+ ```
14
+
15
+ or
16
+
17
+ ```sh
18
+ $ npm install @yamada-ui/segmented-control
19
+ ```
20
+
21
+ ## Contribution
22
+
23
+ Wouldn't you like to contribute? That's amazing! We have prepared a [contribution guide](https://github.com/hirotomoyamada/yamada-ui/blob/main/CONTRIBUTING.md) to assist you.
24
+
25
+ ## Licence
26
+
27
+ This package is licensed under the terms of the
28
+ [MIT license](https://github.com/hirotomoyamada/yamada-ui/blob/main/LICENSE).
@@ -0,0 +1,274 @@
1
+ // src/segmented-control.tsx
2
+ import {
3
+ ui,
4
+ forwardRef,
5
+ useMultiComponentStyle,
6
+ omitThemeProps
7
+ } from "@yamada-ui/core";
8
+ import { useControllableState } from "@yamada-ui/use-controllable-state";
9
+ import { createDescendant } from "@yamada-ui/use-descendant";
10
+ import { trackFocusVisible } from "@yamada-ui/use-focus-visible";
11
+ import { useResizeObserver } from "@yamada-ui/use-resize-observer";
12
+ import {
13
+ ariaAttr,
14
+ createContext,
15
+ cx,
16
+ dataAttr,
17
+ getValidChildren,
18
+ handlerAll,
19
+ mergeRefs,
20
+ omitObject,
21
+ useCallbackRef
22
+ } from "@yamada-ui/utils";
23
+ import {
24
+ useCallback,
25
+ useEffect,
26
+ useId,
27
+ useRef,
28
+ useState
29
+ } from "react";
30
+ import { jsx, jsxs } from "react/jsx-runtime";
31
+ var { DescendantsContextProvider, useDescendants, useDescendant } = createDescendant();
32
+ var [SegmentedControlProvider, useSegmentedControl] = createContext({
33
+ strict: false,
34
+ name: "SegmentedControlContext"
35
+ });
36
+ var SegmentedControl = forwardRef((props, ref) => {
37
+ const [styles, mergedProps] = useMultiComponentStyle("SegmentedControl", props);
38
+ let { className, id, name, isReadOnly, isDisabled, children, ...rest } = omitThemeProps(mergedProps);
39
+ id = id != null ? id : useId();
40
+ name = name != null ? name : `segmented-control-${useId()}`;
41
+ rest.onChange = useCallbackRef(rest.onChange);
42
+ const descendants = useDescendants();
43
+ const [focusedIndex, setFocusedIndex] = useState(-1);
44
+ const [isFocusVisible, setIsFocusVisible] = useState(false);
45
+ const [observerRef, containerRect] = useResizeObserver();
46
+ const containerRef = useRef(null);
47
+ const labelRefs = useRef(/* @__PURE__ */ new Map());
48
+ const [activePosition, setActivePosition] = useState({
49
+ width: 0,
50
+ height: 0,
51
+ x: 0,
52
+ y: 0
53
+ });
54
+ const [value, setValue] = useControllableState({
55
+ value: rest.value,
56
+ defaultValue: rest.defaultValue,
57
+ onChange: rest.onChange
58
+ });
59
+ useEffect(() => {
60
+ return trackFocusVisible(setIsFocusVisible);
61
+ }, []);
62
+ useEffect(() => {
63
+ const el = labelRefs.current.get(value);
64
+ if (!el || !containerRef.current || !observerRef.current)
65
+ return;
66
+ const { paddingLeft, paddingTop } = getComputedStyle(containerRef.current);
67
+ const gutterX = parseFloat(paddingLeft) || 0;
68
+ const gutterY = parseFloat(paddingTop) || 0;
69
+ let { width, height } = el.getBoundingClientRect();
70
+ const x = el.offsetLeft - gutterX;
71
+ const y = el.offsetTop - gutterY;
72
+ width = width * (el.offsetWidth / width) || 0;
73
+ height = height * (el.offsetWidth / width) || 0;
74
+ setActivePosition({ width, height, x, y });
75
+ }, [focusedIndex, containerRect, labelRefs, observerRef, value]);
76
+ const onChange = useCallback(
77
+ (ev) => {
78
+ if (isDisabled || isReadOnly) {
79
+ ev.preventDefault();
80
+ return;
81
+ }
82
+ setValue(ev.target.value);
83
+ },
84
+ [isDisabled, isReadOnly, setValue]
85
+ );
86
+ const onFocus = useCallback(
87
+ (index, skip) => {
88
+ if (isDisabled)
89
+ return;
90
+ if (skip) {
91
+ const next = descendants.enabledNextValue(index);
92
+ if (next)
93
+ setFocusedIndex(next.index);
94
+ } else {
95
+ setFocusedIndex(index);
96
+ }
97
+ },
98
+ [descendants, isDisabled]
99
+ );
100
+ const onBlur = useCallback(() => setFocusedIndex(-1), []);
101
+ const getContainerProps = useCallback(
102
+ (props2 = {}, ref2 = null) => ({
103
+ ...omitObject(rest, ["value", "defaultValue", "onChange"]),
104
+ ...props2,
105
+ ref: mergeRefs(containerRef, observerRef, ref2),
106
+ id,
107
+ "aria-disabled": ariaAttr(isDisabled),
108
+ "aria-readonly": ariaAttr(isReadOnly),
109
+ onBlur: handlerAll(props2.onBlur, onBlur)
110
+ }),
111
+ [id, isDisabled, isReadOnly, observerRef, onBlur, rest]
112
+ );
113
+ const getActiveProps = useCallback(
114
+ (props2 = {}, ref2 = null) => {
115
+ const { width, height, x, y } = activePosition;
116
+ return {
117
+ ...props2,
118
+ ref: ref2,
119
+ style: {
120
+ position: "absolute",
121
+ zIndex: 1,
122
+ width,
123
+ height,
124
+ transform: `translate(${x}px, ${y}px)`
125
+ }
126
+ };
127
+ },
128
+ [activePosition]
129
+ );
130
+ const getInputProps = useCallback(
131
+ ({ index, ...props2 } = {}, ref2 = null) => {
132
+ var _a, _b, _c, _d;
133
+ const disabled = (_b = (_a = props2.disabled) != null ? _a : props2.isDisabled) != null ? _b : isDisabled;
134
+ const readOnly = (_d = (_c = props2.readOnly) != null ? _c : props2.isReadOnly) != null ? _d : isReadOnly;
135
+ const checked = props2.value === value;
136
+ return {
137
+ ...omitObject(props2, ["isDisabled", "isReadOnly"]),
138
+ ref: ref2,
139
+ id: `${id}-${index}`,
140
+ type: "radio",
141
+ name,
142
+ disabled: disabled || readOnly,
143
+ readOnly,
144
+ checked,
145
+ "aria-disabled": ariaAttr(disabled),
146
+ "aria-readonly": ariaAttr(readOnly),
147
+ "data-checked": dataAttr(checked),
148
+ "data-focus": dataAttr(index === focusedIndex),
149
+ style: {
150
+ border: "0px",
151
+ clip: "rect(0px, 0px, 0px, 0px)",
152
+ height: "1px",
153
+ width: "1px",
154
+ margin: "-1px",
155
+ padding: "0px",
156
+ overflow: "hidden",
157
+ whiteSpace: "nowrap",
158
+ position: "absolute"
159
+ },
160
+ onChange: handlerAll(
161
+ props2.onChange,
162
+ (ev) => !disabled && !readOnly ? onChange(ev) : {}
163
+ )
164
+ };
165
+ },
166
+ [isDisabled, isReadOnly, value, id, name, focusedIndex, onChange]
167
+ );
168
+ const getLabelProps = useCallback(
169
+ ({ index, ...props2 } = {}, ref2 = null) => {
170
+ var _a, _b, _c, _d;
171
+ const disabled = (_b = (_a = props2.disabled) != null ? _a : props2.isDisabled) != null ? _b : isDisabled;
172
+ const readOnly = (_d = (_c = props2.readOnly) != null ? _c : props2.isReadOnly) != null ? _d : isReadOnly;
173
+ const checked = props2.value === value;
174
+ const focused = index === focusedIndex;
175
+ return {
176
+ props: props2,
177
+ ref: mergeRefs((node) => labelRefs.current.set(props2.value, node), ref2),
178
+ "aria-disabled": ariaAttr(disabled),
179
+ "aria-readonly": ariaAttr(readOnly),
180
+ "data-checked": dataAttr(checked),
181
+ "data-focus": dataAttr(focused),
182
+ "data-focus-visible": dataAttr(focused && isFocusVisible),
183
+ onFocus: handlerAll(props2.onFocus, () => onFocus(index, disabled || readOnly)),
184
+ ...disabled || readOnly ? {
185
+ _hover: {},
186
+ _active: {},
187
+ _focus: {},
188
+ _invalid: {},
189
+ _focusVisible: {}
190
+ } : {},
191
+ style: { position: "relative", zIndex: 2 }
192
+ };
193
+ },
194
+ [focusedIndex, isDisabled, isFocusVisible, isReadOnly, onFocus, value]
195
+ );
196
+ const css = {
197
+ position: "relative",
198
+ display: "inline-flex",
199
+ alignItems: "center",
200
+ ...styles.container
201
+ };
202
+ const validChildren = getValidChildren(children);
203
+ if (value == null && rest.defaultValue == null) {
204
+ for (const child of validChildren) {
205
+ if (child.type !== SegmentedControlButton)
206
+ continue;
207
+ const value2 = child.props.value;
208
+ setValue(value2);
209
+ break;
210
+ }
211
+ }
212
+ return /* @__PURE__ */ jsx(DescendantsContextProvider, { value: descendants, children: /* @__PURE__ */ jsx(SegmentedControlProvider, { value: { getInputProps, getLabelProps, styles }, children: /* @__PURE__ */ jsxs(
213
+ ui.div,
214
+ {
215
+ ...getContainerProps({}, ref),
216
+ className: cx("ui-segmented-control", className),
217
+ __css: css,
218
+ children: [
219
+ /* @__PURE__ */ jsx(
220
+ ui.span,
221
+ {
222
+ className: "ui-segmented-control-active",
223
+ ...getActiveProps(),
224
+ __css: styles.active
225
+ }
226
+ ),
227
+ validChildren
228
+ ]
229
+ }
230
+ ) }) });
231
+ });
232
+ var SegmentedControlButton = forwardRef(
233
+ ({ className, disabled, readOnly, isDisabled, isReadOnly, value, onChange, children, ...rest }, ref) => {
234
+ const { getInputProps, getLabelProps, styles } = useSegmentedControl();
235
+ const { index, register } = useDescendant({
236
+ disabled: isDisabled || isReadOnly
237
+ });
238
+ const props = {
239
+ index,
240
+ value,
241
+ onChange,
242
+ disabled,
243
+ readOnly,
244
+ isDisabled,
245
+ isReadOnly
246
+ };
247
+ const css = {
248
+ cursor: "pointer",
249
+ flex: "1 1 0%",
250
+ display: "inline-flex",
251
+ justifyContent: "center",
252
+ alignItems: "center",
253
+ ...styles.button
254
+ };
255
+ return /* @__PURE__ */ jsxs(
256
+ ui.label,
257
+ {
258
+ ...getLabelProps(omitObject(props, ["onChange"])),
259
+ className: cx("ui-segmented-control-button", className),
260
+ __css: css,
261
+ ...rest,
262
+ children: [
263
+ /* @__PURE__ */ jsx(ui.input, { ...getInputProps(props, mergeRefs(register, ref)) }),
264
+ /* @__PURE__ */ jsx(ui.span, { children })
265
+ ]
266
+ }
267
+ );
268
+ }
269
+ );
270
+
271
+ export {
272
+ SegmentedControl,
273
+ SegmentedControlButton
274
+ };
@@ -0,0 +1,3 @@
1
+ export { SegmentedControl, SegmentedControlButton, SegmentedControlButtonProps, SegmentedControlProps } from './segmented-control.js';
2
+ import '@yamada-ui/core';
3
+ import 'react';
package/dist/index.js ADDED
@@ -0,0 +1,280 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ SegmentedControl: () => SegmentedControl,
24
+ SegmentedControlButton: () => SegmentedControlButton
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/segmented-control.tsx
29
+ var import_core = require("@yamada-ui/core");
30
+ var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
31
+ var import_use_descendant = require("@yamada-ui/use-descendant");
32
+ var import_use_focus_visible = require("@yamada-ui/use-focus-visible");
33
+ var import_use_resize_observer = require("@yamada-ui/use-resize-observer");
34
+ var import_utils = require("@yamada-ui/utils");
35
+ var import_react = require("react");
36
+ var import_jsx_runtime = require("react/jsx-runtime");
37
+ var { DescendantsContextProvider, useDescendants, useDescendant } = (0, import_use_descendant.createDescendant)();
38
+ var [SegmentedControlProvider, useSegmentedControl] = (0, import_utils.createContext)({
39
+ strict: false,
40
+ name: "SegmentedControlContext"
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)
95
+ 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)`
131
+ }
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;
216
+ }
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,
227
+ {
228
+ className: "ui-segmented-control-active",
229
+ ...getActiveProps(),
230
+ __css: styles.active
231
+ }
232
+ ),
233
+ validChildren
234
+ ]
235
+ }
236
+ ) }) });
237
+ });
238
+ var SegmentedControlButton = (0, import_core.forwardRef)(
239
+ ({ className, disabled, readOnly, isDisabled, isReadOnly, value, onChange, children, ...rest }, ref) => {
240
+ const { getInputProps, getLabelProps, styles } = useSegmentedControl();
241
+ const { index, register } = useDescendant({
242
+ disabled: isDisabled || isReadOnly
243
+ });
244
+ const props = {
245
+ index,
246
+ value,
247
+ onChange,
248
+ disabled,
249
+ readOnly,
250
+ isDisabled,
251
+ isReadOnly
252
+ };
253
+ const css = {
254
+ cursor: "pointer",
255
+ flex: "1 1 0%",
256
+ display: "inline-flex",
257
+ justifyContent: "center",
258
+ alignItems: "center",
259
+ ...styles.button
260
+ };
261
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
262
+ import_core.ui.label,
263
+ {
264
+ ...getLabelProps((0, import_utils.omitObject)(props, ["onChange"])),
265
+ className: (0, import_utils.cx)("ui-segmented-control-button", className),
266
+ __css: css,
267
+ ...rest,
268
+ children: [
269
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.ui.input, { ...getInputProps(props, (0, import_utils.mergeRefs)(register, ref)) }),
270
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.ui.span, { children })
271
+ ]
272
+ }
273
+ );
274
+ }
275
+ );
276
+ // Annotate the CommonJS export names for ESM import in node:
277
+ 0 && (module.exports = {
278
+ SegmentedControl,
279
+ SegmentedControlButton
280
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,8 @@
1
+ import {
2
+ SegmentedControl,
3
+ SegmentedControlButton
4
+ } from "./chunk-4HOIPB3J.mjs";
5
+ export {
6
+ SegmentedControl,
7
+ SegmentedControlButton
8
+ };
@@ -0,0 +1,22 @@
1
+ import * as _yamada_ui_core from '@yamada-ui/core';
2
+ import { HTMLUIProps, ThemeProps } from '@yamada-ui/core';
3
+ import { ChangeEventHandler } from 'react';
4
+
5
+ type SegmentedControlOptions = {
6
+ name?: string;
7
+ value?: string;
8
+ defaultValue?: string;
9
+ onChange?: (value: string) => void;
10
+ isReadOnly?: boolean;
11
+ isDisabled?: boolean;
12
+ };
13
+ type SegmentedControlProps = Omit<HTMLUIProps<'div'>, 'onChange'> & ThemeProps<'SegmentedControl'> & SegmentedControlOptions;
14
+ declare const SegmentedControl: _yamada_ui_core.Component<"div", SegmentedControlProps>;
15
+ type SegmentedControlButtonOptions = {
16
+ value: string | number;
17
+ onChange?: ChangeEventHandler<HTMLInputElement>;
18
+ };
19
+ type SegmentedControlButtonProps = Omit<HTMLUIProps<'label'>, 'onChange'> & Pick<SegmentedControlProps, 'isDisabled' | 'isReadOnly'> & SegmentedControlButtonOptions;
20
+ declare const SegmentedControlButton: _yamada_ui_core.Component<"input", SegmentedControlButtonProps>;
21
+
22
+ export { SegmentedControl, SegmentedControlButton, SegmentedControlButtonProps, SegmentedControlProps };
@@ -0,0 +1,278 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/segmented-control.tsx
21
+ var segmented_control_exports = {};
22
+ __export(segmented_control_exports, {
23
+ SegmentedControl: () => SegmentedControl,
24
+ SegmentedControlButton: () => SegmentedControlButton
25
+ });
26
+ module.exports = __toCommonJS(segmented_control_exports);
27
+ var import_core = require("@yamada-ui/core");
28
+ var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
29
+ var import_use_descendant = require("@yamada-ui/use-descendant");
30
+ var import_use_focus_visible = require("@yamada-ui/use-focus-visible");
31
+ var import_use_resize_observer = require("@yamada-ui/use-resize-observer");
32
+ var import_utils = require("@yamada-ui/utils");
33
+ var import_react = require("react");
34
+ var import_jsx_runtime = require("react/jsx-runtime");
35
+ var { DescendantsContextProvider, useDescendants, useDescendant } = (0, import_use_descendant.createDescendant)();
36
+ var [SegmentedControlProvider, useSegmentedControl] = (0, import_utils.createContext)({
37
+ strict: false,
38
+ name: "SegmentedControlContext"
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)
93
+ 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)`
129
+ }
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;
214
+ }
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,
225
+ {
226
+ className: "ui-segmented-control-active",
227
+ ...getActiveProps(),
228
+ __css: styles.active
229
+ }
230
+ ),
231
+ validChildren
232
+ ]
233
+ }
234
+ ) }) });
235
+ });
236
+ var SegmentedControlButton = (0, import_core.forwardRef)(
237
+ ({ className, disabled, readOnly, isDisabled, isReadOnly, value, onChange, children, ...rest }, ref) => {
238
+ const { getInputProps, getLabelProps, styles } = useSegmentedControl();
239
+ const { index, register } = useDescendant({
240
+ disabled: isDisabled || isReadOnly
241
+ });
242
+ const props = {
243
+ index,
244
+ value,
245
+ onChange,
246
+ disabled,
247
+ readOnly,
248
+ isDisabled,
249
+ isReadOnly
250
+ };
251
+ const css = {
252
+ cursor: "pointer",
253
+ flex: "1 1 0%",
254
+ display: "inline-flex",
255
+ justifyContent: "center",
256
+ alignItems: "center",
257
+ ...styles.button
258
+ };
259
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
260
+ import_core.ui.label,
261
+ {
262
+ ...getLabelProps((0, import_utils.omitObject)(props, ["onChange"])),
263
+ className: (0, import_utils.cx)("ui-segmented-control-button", className),
264
+ __css: css,
265
+ ...rest,
266
+ children: [
267
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.ui.input, { ...getInputProps(props, (0, import_utils.mergeRefs)(register, ref)) }),
268
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.ui.span, { children })
269
+ ]
270
+ }
271
+ );
272
+ }
273
+ );
274
+ // Annotate the CommonJS export names for ESM import in node:
275
+ 0 && (module.exports = {
276
+ SegmentedControl,
277
+ SegmentedControlButton
278
+ });
@@ -0,0 +1,8 @@
1
+ import {
2
+ SegmentedControl,
3
+ SegmentedControlButton
4
+ } from "./chunk-4HOIPB3J.mjs";
5
+ export {
6
+ SegmentedControl,
7
+ SegmentedControlButton
8
+ };
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@yamada-ui/segmented-control",
3
+ "version": "0.1.0",
4
+ "description": "Yamada UI segmented control components",
5
+ "keywords": [
6
+ "yamada",
7
+ "yamada ui",
8
+ "react",
9
+ "emotion",
10
+ "component",
11
+ "segmented-control",
12
+ "ui",
13
+ "uikit",
14
+ "styled",
15
+ "style-props",
16
+ "styled-component",
17
+ "css-in-js"
18
+ ],
19
+ "author": "Hirotomo Yamada <hirotomo.yamada@avap.co.jp>",
20
+ "license": "MIT",
21
+ "main": "dist/index.js",
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "sideEffects": false,
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/hirotomoyamada/yamada-ui",
32
+ "directory": "packages/components/segmented-control"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/hirotomoyamada/yamada-ui/issues"
36
+ },
37
+ "dependencies": {
38
+ "@yamada-ui/core": "0.1.0",
39
+ "@yamada-ui/utils": "0.1.0",
40
+ "@yamada-ui/use-resize-observer": "0.1.0",
41
+ "@yamada-ui/use-focus-visible": "0.1.0",
42
+ "@yamada-ui/use-controllable-state": "0.1.0",
43
+ "@yamada-ui/use-descendant": "0.1.0"
44
+ },
45
+ "devDependencies": {
46
+ "react": "^18.0.0",
47
+ "clean-package": "2.2.0"
48
+ },
49
+ "peerDependencies": {
50
+ "react": ">=18"
51
+ },
52
+ "clean-package": "../../../clean-package.config.json",
53
+ "tsup": {
54
+ "clean": true,
55
+ "target": "es2019",
56
+ "format": [
57
+ "cjs",
58
+ "esm"
59
+ ]
60
+ },
61
+ "module": "dist/index.mjs",
62
+ "types": "dist/index.d.ts",
63
+ "exports": {
64
+ ".": {
65
+ "types": "./dist/index.d.ts",
66
+ "import": "./dist/index.mjs",
67
+ "require": "./dist/index.js"
68
+ },
69
+ "./package.json": "./package.json"
70
+ },
71
+ "scripts": {
72
+ "dev": "pnpm build:fast -- --watch",
73
+ "build": "tsup src --dts",
74
+ "build:fast": "tsup src",
75
+ "clean": "rimraf dist .turbo",
76
+ "typecheck": "tsc --noEmit",
77
+ "gen:docs": "tsx ../../../scripts/generate-docs"
78
+ }
79
+ }