@frontify/fondue 12.0.0-beta.361 → 12.0.0-beta.362
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/components/SegmentedControls/SegmentedControls.es.js +62 -61
- package/dist/components/SegmentedControls/SegmentedControls.es.js.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { jsx as r, jsxs as
|
|
1
|
+
import { jsx as r, jsxs as I } from "react/jsx-runtime";
|
|
2
2
|
import { useMemoizedId as j } from "../../hooks/useMemoizedId.es.js";
|
|
3
3
|
import { useFocusRing as F } from "@react-aria/focus";
|
|
4
4
|
import { setInteractionModality as M } from "@react-aria/interactions";
|
|
@@ -6,25 +6,25 @@ import { useRadio as P, useRadioGroup as $ } from "@react-aria/radio";
|
|
|
6
6
|
import { VisuallyHidden as H } from "@react-aria/visually-hidden";
|
|
7
7
|
import { useRadioGroupState as O } from "@react-stately/radio";
|
|
8
8
|
import { FOCUS_STYLE as A } from "../../utilities/focusStyle.es.js";
|
|
9
|
-
import { merge as
|
|
9
|
+
import { merge as v } from "../../utilities/merge.es.js";
|
|
10
10
|
import { motion as B } from "framer-motion";
|
|
11
|
-
import { forwardRef as T, useRef as
|
|
12
|
-
const
|
|
13
|
-
const { id: p, item: e, disabled:
|
|
11
|
+
import { forwardRef as T, useRef as E, useState as W, useMemo as X, useCallback as N, useEffect as z } from "react";
|
|
12
|
+
const h = (m) => m.icon !== void 0, k = T((m, f) => {
|
|
13
|
+
const { id: p, item: e, disabled: a, radioGroupState: t, size: d } = m, o = E(null), c = e.id === t.selectedValue, { inputProps: x } = P(
|
|
14
14
|
{
|
|
15
15
|
value: e.id,
|
|
16
|
-
"aria-label":
|
|
17
|
-
isDisabled:
|
|
18
|
-
id:
|
|
16
|
+
"aria-label": h(e) ? e.ariaLabel : e.value.toString(),
|
|
17
|
+
isDisabled: a,
|
|
18
|
+
id: c ? p : void 0
|
|
19
19
|
},
|
|
20
20
|
t,
|
|
21
21
|
o
|
|
22
|
-
), { isFocusVisible:
|
|
22
|
+
), { isFocusVisible: w, focusProps: S } = F(), s = () => {
|
|
23
23
|
var g;
|
|
24
|
-
|
|
25
|
-
},
|
|
24
|
+
a || (t.setSelectedValue(e.id), (g = o.current) == null || g.focus(), M("pointer"));
|
|
25
|
+
}, y = () => {
|
|
26
26
|
switch (!0) {
|
|
27
|
-
case
|
|
27
|
+
case h(e):
|
|
28
28
|
return "fondue-segmented-controls-item-icon";
|
|
29
29
|
case typeof e.value == "string":
|
|
30
30
|
return "fondue-segmented-controls-item-text";
|
|
@@ -35,37 +35,37 @@ const b = (u) => u.icon !== void 0, R = T((u, m) => {
|
|
|
35
35
|
return /* @__PURE__ */ r(
|
|
36
36
|
"div",
|
|
37
37
|
{
|
|
38
|
-
ref:
|
|
39
|
-
className:
|
|
38
|
+
ref: f,
|
|
39
|
+
className: v([
|
|
40
40
|
"tw-relative",
|
|
41
41
|
"after:tw-content-[''] after:tw-border-r after:tw-border-solid after:tw-border-line-strong after:tw-absolute after:tw-right-0 after:tw-h-full last:after:tw-hidden",
|
|
42
|
-
|
|
42
|
+
w && A
|
|
43
43
|
]),
|
|
44
|
-
children: /* @__PURE__ */
|
|
44
|
+
children: /* @__PURE__ */ I(
|
|
45
45
|
"div",
|
|
46
46
|
{
|
|
47
47
|
role: "none",
|
|
48
|
-
onClick:
|
|
49
|
-
"data-test-id":
|
|
50
|
-
className:
|
|
48
|
+
onClick: s,
|
|
49
|
+
"data-test-id": y(),
|
|
50
|
+
className: v([
|
|
51
51
|
"tw-relative tw-w-full tw-py-2 tw-inline-flex tw-justify-center tw-items-center tw-font-sans tw-font-normal tw-h-full tw-text-center",
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
d === "small" ? "tw-px-2" : "tw-px-4",
|
|
53
|
+
c && !a ? "tw-text-text" : "tw-text-text-weak",
|
|
54
|
+
a ? "tw-text-box-disabled-inverse tw-bg-box-disabled hover:tw-cursor-not-allowed" : "hover:tw-text-text hover:tw-cursor-pointer"
|
|
55
55
|
]),
|
|
56
56
|
children: [
|
|
57
57
|
/* @__PURE__ */ r(H, { children: /* @__PURE__ */ r(
|
|
58
58
|
"input",
|
|
59
59
|
{
|
|
60
60
|
...x,
|
|
61
|
-
...
|
|
61
|
+
...S,
|
|
62
62
|
"data-test-id": "fondue-segmented-controls-input",
|
|
63
63
|
ref: o
|
|
64
64
|
}
|
|
65
65
|
) }),
|
|
66
|
-
/* @__PURE__ */
|
|
67
|
-
|
|
68
|
-
e.value && /* @__PURE__ */ r("span", { className:
|
|
66
|
+
/* @__PURE__ */ I("span", { className: "tw-overflow-hidden tw-text-ellipsis tw-whitespace-nowrap tw-flex", children: [
|
|
67
|
+
h(e) && /* @__PURE__ */ r("span", { className: "tw-leading-3", "aria-label": e.ariaLabel, children: e.icon }),
|
|
68
|
+
e.value && /* @__PURE__ */ r("span", { className: h(e) ? "tw-ml-2" : "", children: e.value.toString() })
|
|
69
69
|
] })
|
|
70
70
|
]
|
|
71
71
|
}
|
|
@@ -74,69 +74,70 @@ const b = (u) => u.icon !== void 0, R = T((u, m) => {
|
|
|
74
74
|
e.id
|
|
75
75
|
);
|
|
76
76
|
});
|
|
77
|
-
|
|
77
|
+
k.displayName = "SegmentedControlsItem";
|
|
78
78
|
const U = ({
|
|
79
|
-
id:
|
|
80
|
-
items:
|
|
79
|
+
id: m,
|
|
80
|
+
items: f,
|
|
81
81
|
activeItemId: p,
|
|
82
82
|
onChange: e,
|
|
83
|
-
ariaLabel:
|
|
83
|
+
ariaLabel: a = "SegmentedControls",
|
|
84
84
|
disabled: t = !1,
|
|
85
|
-
hugWidth:
|
|
85
|
+
hugWidth: d = !1,
|
|
86
86
|
size: o
|
|
87
87
|
}) => {
|
|
88
|
-
const
|
|
89
|
-
|
|
88
|
+
const c = j(m), x = { onChange: e, value: p, label: a, isDisabled: t }, w = O(x), { radioGroupProps: S } = $(x, w), s = E([]), [y, g] = W(null), D = X(() => f.map((n, u) => /* @__PURE__ */ r(
|
|
89
|
+
k,
|
|
90
90
|
{
|
|
91
|
-
id:
|
|
92
|
-
item:
|
|
91
|
+
id: c,
|
|
92
|
+
item: n,
|
|
93
93
|
disabled: t,
|
|
94
|
-
radioGroupState:
|
|
95
|
-
ref: (
|
|
94
|
+
radioGroupState: w,
|
|
95
|
+
ref: (l) => s.current[u] = l,
|
|
96
96
|
size: o
|
|
97
97
|
},
|
|
98
|
-
`fondue-segmented-controls-${
|
|
99
|
-
)), [
|
|
100
|
-
var
|
|
101
|
-
let
|
|
102
|
-
for (let C = 0; C <
|
|
103
|
-
|
|
104
|
-
return `${
|
|
105
|
-
}, [
|
|
106
|
-
var
|
|
107
|
-
const
|
|
108
|
-
return `${(((
|
|
109
|
-
}, [
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
98
|
+
`fondue-segmented-controls-${c}-item-${n.id}`
|
|
99
|
+
)), [f, c, t, w, o]), i = f.findIndex((n) => n.id === w.selectedValue), G = d ? "" : "tw-w-full", V = d ? "tw-flex" : "tw-grid tw-grid-flow-col tw-auto-cols-fr tw-justify-evenly", L = N(() => {
|
|
100
|
+
var l;
|
|
101
|
+
let u = o === "small" || d ? -1 : 0;
|
|
102
|
+
for (let C = 0; C < i; C++)
|
|
103
|
+
u += ((l = s.current[C]) == null ? void 0 : l.clientWidth) ?? 0;
|
|
104
|
+
return `${u}px`;
|
|
105
|
+
}, [d, i, o]), R = N(() => {
|
|
106
|
+
var l;
|
|
107
|
+
const u = i === s.current.length - 1 ? 1 : -1;
|
|
108
|
+
return `${(((l = s.current[i]) == null ? void 0 : l.clientWidth) ?? 0) + u}px`;
|
|
109
|
+
}, [i]), b = N(() => {
|
|
110
|
+
const n = s.current ? { x: L(), width: R() } : { x: "0px", width: "0px" };
|
|
111
|
+
g(n);
|
|
112
|
+
}, [R, L]);
|
|
113
|
+
return z(() => (i >= 0 && b(), window.addEventListener("resize", b), () => {
|
|
114
|
+
window.removeEventListener("resize", b);
|
|
115
|
+
}), [i, b]), /* @__PURE__ */ r("div", { className: "tw-flex", children: /* @__PURE__ */ I(
|
|
115
116
|
"fieldset",
|
|
116
117
|
{
|
|
117
|
-
...
|
|
118
|
+
...S,
|
|
118
119
|
"data-test-id": "fondue-segmented-controls",
|
|
119
|
-
className:
|
|
120
|
+
className: v([
|
|
120
121
|
"tw-relative tw-h-9 tw-p-0 tw-border tw-border-solid tw-border-line-strong tw-m-0 tw-bg-base-alt tw-rounded tw-font-sans tw-text-s tw-select-none",
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
G,
|
|
123
|
+
V
|
|
123
124
|
]),
|
|
124
125
|
children: [
|
|
125
126
|
/* @__PURE__ */ r(
|
|
126
127
|
B.div,
|
|
127
128
|
{
|
|
128
129
|
"aria-hidden": "true",
|
|
129
|
-
animate:
|
|
130
|
+
animate: y ?? { x: "0px", width: "0px" },
|
|
130
131
|
initial: !1,
|
|
131
132
|
transition: { type: "tween", duration: 0.3 },
|
|
132
133
|
hidden: !p,
|
|
133
|
-
className:
|
|
134
|
+
className: v([
|
|
134
135
|
"tw-absolute tw--inset-px tw-h-full tw-box-content tw-border tw-rounded tw-pointer-events-none",
|
|
135
136
|
t ? "tw-border-line-x-strong hover:tw-cursor-not-allowed" : "tw-border-line-xx-strong tw-bg-base"
|
|
136
137
|
])
|
|
137
138
|
}
|
|
138
139
|
),
|
|
139
|
-
|
|
140
|
+
D
|
|
140
141
|
]
|
|
141
142
|
}
|
|
142
143
|
) });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SegmentedControls.es.js","sources":["../../../src/components/SegmentedControls/SegmentedControls.tsx"],"sourcesContent":["/* (c) Copyright Frontify Ltd., all rights reserved. */\n\nimport { IconProps } from '@foundation/Icon/IconProps';\nimport { useMemoizedId } from '@hooks/useMemoizedId';\nimport { useFocusRing } from '@react-aria/focus';\nimport { setInteractionModality } from '@react-aria/interactions';\nimport { useRadio, useRadioGroup } from '@react-aria/radio';\nimport { VisuallyHidden } from '@react-aria/visually-hidden';\nimport { RadioGroupState, useRadioGroupState } from '@react-stately/radio';\nimport { FOCUS_STYLE } from '@utilities/focusStyle';\nimport { merge } from '@utilities/merge';\nimport { motion } from 'framer-motion';\nimport { ReactElement, forwardRef, useCallback, useEffect, useMemo, useRef, useState } from 'react';\n\nexport type IconItem = {\n id: string;\n icon: ReactElement<IconProps>;\n value?: string;\n ariaLabel: string;\n};\n\nexport type TextOrNumberItem = {\n id: string;\n value: string | number;\n};\n\nexport type SegmentSize = 'small' | 'medium';\n\nexport type SegmentedControlsProps = {\n id?: string;\n items: (TextOrNumberItem | IconItem)[];\n activeItemId?: string;\n onChange: (id: string) => void;\n ariaLabel?: string;\n disabled?: boolean;\n hugWidth?: boolean;\n size?: SegmentSize;\n};\n\nconst isIconItem = (item: TextOrNumberItem | IconItem): item is IconItem => (item as IconItem).icon !== undefined;\n\ninterface SegmentedControlsItemProps {\n id: string;\n item: TextOrNumberItem | IconItem;\n disabled: boolean;\n radioGroupState: RadioGroupState;\n size?: SegmentSize;\n}\n\nconst SegmentedControlsItem = forwardRef<HTMLDivElement, SegmentedControlsItemProps>((props, ref) => {\n const { id, item, disabled, radioGroupState, size } = props;\n const inputRef = useRef<HTMLInputElement | null>(null);\n const isActive = item.id === radioGroupState.selectedValue;\n const { inputProps } = useRadio(\n {\n value: item.id,\n 'aria-label': isIconItem(item) ? item.ariaLabel : item.value.toString(),\n isDisabled: disabled,\n id: isActive ? id : undefined,\n },\n radioGroupState,\n inputRef,\n );\n const { isFocusVisible, focusProps } = useFocusRing();\n\n const handleMockLabelClick = () => {\n if (!disabled) {\n radioGroupState.setSelectedValue(item.id);\n inputRef.current?.focus();\n setInteractionModality('pointer');\n }\n };\n\n const getSegmentedControlsItemTestId = () => {\n switch (true) {\n case isIconItem(item):\n return 'fondue-segmented-controls-item-icon';\n case typeof item.value === 'string':\n return 'fondue-segmented-controls-item-text';\n default:\n return 'fondue-segmented-controls-item-number';\n }\n };\n\n return (\n <div\n key={item.id}\n ref={ref}\n className={merge([\n 'tw-relative',\n \"after:tw-content-[''] after:tw-border-r after:tw-border-solid after:tw-border-line-strong after:tw-absolute after:tw-right-0 after:tw-h-full last:after:tw-hidden\",\n isFocusVisible && FOCUS_STYLE,\n ])}\n >\n <div\n // TODO: Change element back to label when bug #2380 from @react-aria is fixed\n // https://github.com/adobe/react-spectrum/issues/2380\n role=\"none\"\n onClick={handleMockLabelClick}\n data-test-id={getSegmentedControlsItemTestId()}\n className={merge([\n 'tw-relative tw-w-full tw-py-2 tw-inline-flex tw-justify-center tw-items-center tw-font-sans tw-font-normal tw-h-full tw-text-center',\n size === 'small' ? 'tw-px-2' : 'tw-px-4',\n isActive && !disabled ? 'tw-text-text' : 'tw-text-text-weak',\n disabled\n ? 'tw-text-box-disabled-inverse tw-bg-box-disabled hover:tw-cursor-not-allowed'\n : 'hover:tw-text-text hover:tw-cursor-pointer',\n ])}\n >\n <VisuallyHidden>\n <input\n {...inputProps}\n {...focusProps}\n data-test-id=\"fondue-segmented-controls-input\"\n ref={inputRef}\n />\n </VisuallyHidden>\n <span className=\"tw-overflow-hidden tw-text-ellipsis tw-whitespace-nowrap tw-flex\">\n {isIconItem(item) && (\n <span className=\"tw-leading-3\" aria-label={item.ariaLabel}>\n {item.icon}\n </span>\n )}\n {item.value && <span className={isIconItem(item) ? 'tw-ml-2' : ''}>{item.value.toString()}</span>}\n </span>\n </div>\n </div>\n );\n});\n\nSegmentedControlsItem.displayName = 'SegmentedControlsItem';\n\nexport const SegmentedControls = ({\n id: propId,\n items,\n activeItemId,\n onChange,\n ariaLabel = 'SegmentedControls',\n disabled = false,\n hugWidth = false,\n size,\n}: SegmentedControlsProps): ReactElement => {\n const id = useMemoizedId(propId);\n const groupProps = { onChange, value: activeItemId, label: ariaLabel, isDisabled: disabled };\n const radioGroupState = useRadioGroupState(groupProps);\n const { radioGroupProps } = useRadioGroup(groupProps, radioGroupState);\n const itemsRef = useRef<(HTMLDivElement | null)[]>([]);\n const [activeBorderDimensions, setActiveBorderDimensions] = useState<{ x: string; width: string } | null>(null);\n const itemElements = useMemo(() => {\n return items.map((item, index) => (\n <SegmentedControlsItem\n id={id}\n item={item}\n disabled={disabled}\n radioGroupState={radioGroupState}\n ref={(el) => (itemsRef.current[index] = el)}\n key={`fondue-segmented-controls-${id}-item-${item.id}`}\n size={size}\n />\n ));\n }, [items, id, disabled, radioGroupState, size]);\n const selectedIndex = items.findIndex((item) => item.id === radioGroupState.selectedValue);\n const width = hugWidth ? '' : 'tw-w-full';\n const alignment = hugWidth ? 'tw-flex' : 'tw-grid tw-grid-flow-col tw-auto-cols-fr tw-justify-evenly';\n\n const getSliderX = useCallback(() => {\n const isSmallOrHugWidth = size === 'small' || hugWidth;\n let translateX = isSmallOrHugWidth ? -1 : 0;\n for (let i = 0; i < selectedIndex; i++) {\n translateX += itemsRef.current[i]?.clientWidth ?? 0;\n }\n\n return `${translateX}px`;\n }, [hugWidth, selectedIndex, size]);\n\n const getSliderWidth = useCallback(() => {\n const isLastElement = selectedIndex === itemsRef.current.length - 1;\n const width = isLastElement ? 1 : -1;\n\n return `${(itemsRef.current[selectedIndex]?.clientWidth ?? 0) + width}px`;\n }, [selectedIndex]);\n\n const setSliderDimensions = () => {\n setActiveBorderDimensions({ x: getSliderX(), width: getSliderWidth() });\n };\n\n useEffect(() => {\n setSliderDimensions();\n window.addEventListener('resize', setSliderDimensions);\n\n return () => {\n window.removeEventListener('resize', setSliderDimensions);\n };\n });\n\n return (\n <div className=\"tw-flex\">\n <fieldset\n {...radioGroupProps}\n data-test-id=\"fondue-segmented-controls\"\n className={merge([\n 'tw-relative tw-h-9 tw-p-0 tw-border tw-border-solid tw-border-line-strong tw-m-0 tw-bg-base-alt tw-rounded tw-font-sans tw-text-s tw-select-none',\n width,\n alignment,\n ])}\n >\n <motion.div\n aria-hidden=\"true\"\n // div border is not included in width so it must be subtracted from translation.\n animate={activeBorderDimensions ?? { x: '0px', width: '0px' }}\n initial={false}\n transition={{ type: 'tween', duration: 0.3 }}\n hidden={!activeItemId}\n className={merge([\n 'tw-absolute tw--inset-px tw-h-full tw-box-content tw-border tw-rounded tw-pointer-events-none',\n disabled\n ? 'tw-border-line-x-strong hover:tw-cursor-not-allowed'\n : 'tw-border-line-xx-strong tw-bg-base',\n ])}\n />\n {itemElements}\n </fieldset>\n </div>\n );\n};\nSegmentedControls.displayName = 'FondueSegmentedControls';\n"],"names":["isIconItem","item","SegmentedControlsItem","forwardRef","props","ref","id","disabled","radioGroupState","size","inputRef","useRef","isActive","inputProps","useRadio","isFocusVisible","focusProps","useFocusRing","handleMockLabelClick","_a","setInteractionModality","getSegmentedControlsItemTestId","jsx","merge","FOCUS_STYLE","jsxs","VisuallyHidden","SegmentedControls","propId","items","activeItemId","onChange","ariaLabel","hugWidth","useMemoizedId","groupProps","useRadioGroupState","radioGroupProps","useRadioGroup","itemsRef","activeBorderDimensions","setActiveBorderDimensions","useState","itemElements","useMemo","index","el","selectedIndex","width","alignment","getSliderX","useCallback","translateX","i","getSliderWidth","setSliderDimensions","useEffect","motion"],"mappings":";;;;;;;;;;;AAuCA,MAAMA,IAAa,CAACC,MAAyDA,EAAkB,SAAS,QAUlGC,IAAwBC,EAAuD,CAACC,GAAOC,MAAQ;AACjG,QAAM,EAAE,IAAAC,GAAI,MAAAL,GAAM,UAAAM,GAAU,iBAAAC,GAAiB,MAAAC,EAAS,IAAAL,GAChDM,IAAWC,EAAgC,IAAI,GAC/CC,IAAWX,EAAK,OAAOO,EAAgB,eACvC,EAAE,YAAAK,MAAeC;AAAA,IACnB;AAAA,MACI,OAAOb,EAAK;AAAA,MACZ,cAAcD,EAAWC,CAAI,IAAIA,EAAK,YAAYA,EAAK,MAAM,SAAS;AAAA,MACtE,YAAYM;AAAA,MACZ,IAAIK,IAAWN,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A;AAAA,YAAA,CACT;AAAA,UAAA;AAAA,QACL;AAAA,QACCoC;AAAA,MAAA;AAAA,IAAA;AAAA,EAET,EAAA,CAAA;AAER;AACAhB,EAAkB,cAAc;"}
|
|
1
|
+
{"version":3,"file":"SegmentedControls.es.js","sources":["../../../src/components/SegmentedControls/SegmentedControls.tsx"],"sourcesContent":["/* (c) Copyright Frontify Ltd., all rights reserved. */\n\nimport { IconProps } from '@foundation/Icon/IconProps';\nimport { useMemoizedId } from '@hooks/useMemoizedId';\nimport { useFocusRing } from '@react-aria/focus';\nimport { setInteractionModality } from '@react-aria/interactions';\nimport { useRadio, useRadioGroup } from '@react-aria/radio';\nimport { VisuallyHidden } from '@react-aria/visually-hidden';\nimport { RadioGroupState, useRadioGroupState } from '@react-stately/radio';\nimport { FOCUS_STYLE } from '@utilities/focusStyle';\nimport { merge } from '@utilities/merge';\nimport { motion } from 'framer-motion';\nimport { ReactElement, forwardRef, useCallback, useEffect, useMemo, useRef, useState } from 'react';\n\nexport type IconItem = {\n id: string;\n icon: ReactElement<IconProps>;\n value?: string;\n ariaLabel: string;\n};\n\nexport type TextOrNumberItem = {\n id: string;\n value: string | number;\n};\n\nexport type SegmentSize = 'small' | 'medium';\n\nexport type SegmentedControlsProps = {\n id?: string;\n items: (TextOrNumberItem | IconItem)[];\n activeItemId?: string;\n onChange: (id: string) => void;\n ariaLabel?: string;\n disabled?: boolean;\n hugWidth?: boolean;\n size?: SegmentSize;\n};\n\nconst isIconItem = (item: TextOrNumberItem | IconItem): item is IconItem => (item as IconItem).icon !== undefined;\n\ninterface SegmentedControlsItemProps {\n id: string;\n item: TextOrNumberItem | IconItem;\n disabled: boolean;\n radioGroupState: RadioGroupState;\n size?: SegmentSize;\n}\n\nconst SegmentedControlsItem = forwardRef<HTMLDivElement, SegmentedControlsItemProps>((props, ref) => {\n const { id, item, disabled, radioGroupState, size } = props;\n const inputRef = useRef<HTMLInputElement | null>(null);\n const isActive = item.id === radioGroupState.selectedValue;\n const { inputProps } = useRadio(\n {\n value: item.id,\n 'aria-label': isIconItem(item) ? item.ariaLabel : item.value.toString(),\n isDisabled: disabled,\n id: isActive ? id : undefined,\n },\n radioGroupState,\n inputRef,\n );\n const { isFocusVisible, focusProps } = useFocusRing();\n\n const handleMockLabelClick = () => {\n if (!disabled) {\n radioGroupState.setSelectedValue(item.id);\n inputRef.current?.focus();\n setInteractionModality('pointer');\n }\n };\n\n const getSegmentedControlsItemTestId = () => {\n switch (true) {\n case isIconItem(item):\n return 'fondue-segmented-controls-item-icon';\n case typeof item.value === 'string':\n return 'fondue-segmented-controls-item-text';\n default:\n return 'fondue-segmented-controls-item-number';\n }\n };\n\n return (\n <div\n key={item.id}\n ref={ref}\n className={merge([\n 'tw-relative',\n \"after:tw-content-[''] after:tw-border-r after:tw-border-solid after:tw-border-line-strong after:tw-absolute after:tw-right-0 after:tw-h-full last:after:tw-hidden\",\n isFocusVisible && FOCUS_STYLE,\n ])}\n >\n <div\n // TODO: Change element back to label when bug #2380 from @react-aria is fixed\n // https://github.com/adobe/react-spectrum/issues/2380\n role=\"none\"\n onClick={handleMockLabelClick}\n data-test-id={getSegmentedControlsItemTestId()}\n className={merge([\n 'tw-relative tw-w-full tw-py-2 tw-inline-flex tw-justify-center tw-items-center tw-font-sans tw-font-normal tw-h-full tw-text-center',\n size === 'small' ? 'tw-px-2' : 'tw-px-4',\n isActive && !disabled ? 'tw-text-text' : 'tw-text-text-weak',\n disabled\n ? 'tw-text-box-disabled-inverse tw-bg-box-disabled hover:tw-cursor-not-allowed'\n : 'hover:tw-text-text hover:tw-cursor-pointer',\n ])}\n >\n <VisuallyHidden>\n <input\n {...inputProps}\n {...focusProps}\n data-test-id=\"fondue-segmented-controls-input\"\n ref={inputRef}\n />\n </VisuallyHidden>\n <span className=\"tw-overflow-hidden tw-text-ellipsis tw-whitespace-nowrap tw-flex\">\n {isIconItem(item) && (\n <span className=\"tw-leading-3\" aria-label={item.ariaLabel}>\n {item.icon}\n </span>\n )}\n {item.value && <span className={isIconItem(item) ? 'tw-ml-2' : ''}>{item.value.toString()}</span>}\n </span>\n </div>\n </div>\n );\n});\n\nSegmentedControlsItem.displayName = 'SegmentedControlsItem';\n\nexport const SegmentedControls = ({\n id: propId,\n items,\n activeItemId,\n onChange,\n ariaLabel = 'SegmentedControls',\n disabled = false,\n hugWidth = false,\n size,\n}: SegmentedControlsProps): ReactElement => {\n const id = useMemoizedId(propId);\n const groupProps = { onChange, value: activeItemId, label: ariaLabel, isDisabled: disabled };\n const radioGroupState = useRadioGroupState(groupProps);\n const { radioGroupProps } = useRadioGroup(groupProps, radioGroupState);\n const itemsRef = useRef<(HTMLDivElement | null)[]>([]);\n const [activeBorderDimensions, setActiveBorderDimensions] = useState<{ x: string; width: string } | null>(null);\n const itemElements = useMemo(() => {\n return items.map((item, index) => (\n <SegmentedControlsItem\n id={id}\n item={item}\n disabled={disabled}\n radioGroupState={radioGroupState}\n ref={(el) => (itemsRef.current[index] = el)}\n key={`fondue-segmented-controls-${id}-item-${item.id}`}\n size={size}\n />\n ));\n }, [items, id, disabled, radioGroupState, size]);\n const selectedIndex = items.findIndex((item) => item.id === radioGroupState.selectedValue);\n const width = hugWidth ? '' : 'tw-w-full';\n const alignment = hugWidth ? 'tw-flex' : 'tw-grid tw-grid-flow-col tw-auto-cols-fr tw-justify-evenly';\n\n const getSliderX = useCallback(() => {\n const isSmallOrHugWidth = size === 'small' || hugWidth;\n let translateX = isSmallOrHugWidth ? -1 : 0;\n for (let i = 0; i < selectedIndex; i++) {\n translateX += itemsRef.current[i]?.clientWidth ?? 0;\n }\n\n return `${translateX}px`;\n }, [hugWidth, selectedIndex, size]);\n\n const getSliderWidth = useCallback(() => {\n const isLastElement = selectedIndex === itemsRef.current.length - 1;\n const width = isLastElement ? 1 : -1;\n\n return `${(itemsRef.current[selectedIndex]?.clientWidth ?? 0) + width}px`;\n }, [selectedIndex]);\n\n const setSliderDimensions = useCallback(() => {\n const dimensions = itemsRef.current ? { x: getSliderX(), width: getSliderWidth() } : { x: '0px', width: '0px' };\n setActiveBorderDimensions(dimensions);\n }, [getSliderWidth, getSliderX]);\n\n useEffect(() => {\n if (selectedIndex >= 0) {\n setSliderDimensions();\n }\n window.addEventListener('resize', setSliderDimensions);\n\n return () => {\n window.removeEventListener('resize', setSliderDimensions);\n };\n }, [selectedIndex, setSliderDimensions]);\n\n return (\n <div className=\"tw-flex\">\n <fieldset\n {...radioGroupProps}\n data-test-id=\"fondue-segmented-controls\"\n className={merge([\n 'tw-relative tw-h-9 tw-p-0 tw-border tw-border-solid tw-border-line-strong tw-m-0 tw-bg-base-alt tw-rounded tw-font-sans tw-text-s tw-select-none',\n width,\n alignment,\n ])}\n >\n <motion.div\n aria-hidden=\"true\"\n // div border is not included in width so it must be subtracted from translation.\n animate={activeBorderDimensions ?? { x: '0px', width: '0px' }}\n initial={false}\n transition={{ type: 'tween', duration: 0.3 }}\n hidden={!activeItemId}\n className={merge([\n 'tw-absolute tw--inset-px tw-h-full tw-box-content tw-border tw-rounded tw-pointer-events-none',\n disabled\n ? 'tw-border-line-x-strong hover:tw-cursor-not-allowed'\n : 'tw-border-line-xx-strong tw-bg-base',\n ])}\n />\n {itemElements}\n </fieldset>\n </div>\n );\n};\nSegmentedControls.displayName = 'FondueSegmentedControls';\n"],"names":["isIconItem","item","SegmentedControlsItem","forwardRef","props","ref","id","disabled","radioGroupState","size","inputRef","useRef","isActive","inputProps","useRadio","isFocusVisible","focusProps","useFocusRing","handleMockLabelClick","_a","setInteractionModality","getSegmentedControlsItemTestId","jsx","merge","FOCUS_STYLE","jsxs","VisuallyHidden","SegmentedControls","propId","items","activeItemId","onChange","ariaLabel","hugWidth","useMemoizedId","groupProps","useRadioGroupState","radioGroupProps","useRadioGroup","itemsRef","activeBorderDimensions","setActiveBorderDimensions","useState","itemElements","useMemo","index","el","selectedIndex","width","alignment","getSliderX","useCallback","translateX","i","getSliderWidth","setSliderDimensions","dimensions","useEffect","motion"],"mappings":";;;;;;;;;;;AAuCA,MAAM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