@frontify/fondue 12.0.0-beta.359 → 12.0.0-beta.360
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 +92 -92
- 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/dist/styles.css +1 -1
- package/package.json +1 -1
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useMemoizedId as
|
|
3
|
-
import { useFocusRing as
|
|
4
|
-
import { setInteractionModality as
|
|
5
|
-
import { useRadio as
|
|
6
|
-
import { VisuallyHidden as
|
|
7
|
-
import { useRadioGroupState as
|
|
8
|
-
import { FOCUS_STYLE as
|
|
9
|
-
import { merge as
|
|
10
|
-
import { motion as
|
|
11
|
-
import { forwardRef as
|
|
12
|
-
const
|
|
13
|
-
const { id: p, item: t, disabled:
|
|
1
|
+
import { jsx as o, jsxs as y } from "react/jsx-runtime";
|
|
2
|
+
import { useMemoizedId as k } from "../../hooks/useMemoizedId.es.js";
|
|
3
|
+
import { useFocusRing as E } from "@react-aria/focus";
|
|
4
|
+
import { setInteractionModality as F } from "@react-aria/interactions";
|
|
5
|
+
import { useRadio as M, useRadioGroup as P } from "@react-aria/radio";
|
|
6
|
+
import { VisuallyHidden as W } from "@react-aria/visually-hidden";
|
|
7
|
+
import { useRadioGroupState as $ } from "@react-stately/radio";
|
|
8
|
+
import { FOCUS_STYLE as H } from "../../utilities/focusStyle.es.js";
|
|
9
|
+
import { merge as h } from "../../utilities/merge.es.js";
|
|
10
|
+
import { motion as O } from "framer-motion";
|
|
11
|
+
import { forwardRef as T, useRef as N, useMemo as D } from "react";
|
|
12
|
+
const x = (u) => u.icon !== void 0, C = T((u, m) => {
|
|
13
|
+
const { id: p, item: t, disabled: s, radioGroupState: e, size: f } = u, r = N(null), i = t.id === e.selectedValue, { inputProps: g } = M(
|
|
14
14
|
{
|
|
15
15
|
value: t.id,
|
|
16
|
-
"aria-label":
|
|
17
|
-
isDisabled:
|
|
18
|
-
id:
|
|
16
|
+
"aria-label": x(t) ? t.ariaLabel : t.value.toString(),
|
|
17
|
+
isDisabled: s,
|
|
18
|
+
id: i ? p : void 0
|
|
19
19
|
},
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
), { isFocusVisible:
|
|
23
|
-
var
|
|
24
|
-
|
|
20
|
+
e,
|
|
21
|
+
r
|
|
22
|
+
), { isFocusVisible: l, focusProps: b } = E(), a = () => {
|
|
23
|
+
var d;
|
|
24
|
+
s || (e.setSelectedValue(t.id), (d = r.current) == null || d.focus(), F("pointer"));
|
|
25
25
|
}, v = () => {
|
|
26
26
|
switch (!0) {
|
|
27
|
-
case
|
|
27
|
+
case x(t):
|
|
28
28
|
return "fondue-segmented-controls-item-icon";
|
|
29
29
|
case typeof t.value == "string":
|
|
30
30
|
return "fondue-segmented-controls-item-text";
|
|
@@ -32,40 +32,40 @@ const b = (u) => u.icon !== void 0, I = O((u, m) => {
|
|
|
32
32
|
return "fondue-segmented-controls-item-number";
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
|
-
return /* @__PURE__ */
|
|
35
|
+
return /* @__PURE__ */ o(
|
|
36
36
|
"div",
|
|
37
37
|
{
|
|
38
38
|
ref: m,
|
|
39
|
-
className:
|
|
39
|
+
className: h([
|
|
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
|
+
l && H
|
|
43
43
|
]),
|
|
44
|
-
children: /* @__PURE__ */
|
|
44
|
+
children: /* @__PURE__ */ y(
|
|
45
45
|
"div",
|
|
46
46
|
{
|
|
47
47
|
role: "none",
|
|
48
|
-
onClick:
|
|
48
|
+
onClick: a,
|
|
49
49
|
"data-test-id": v(),
|
|
50
|
-
className:
|
|
50
|
+
className: h([
|
|
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
52
|
f === "small" ? "tw-px-2" : "tw-px-4",
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
i && !s ? "tw-text-text" : "tw-text-text-weak",
|
|
54
|
+
s ? "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
|
-
/* @__PURE__ */
|
|
57
|
+
/* @__PURE__ */ o(W, { children: /* @__PURE__ */ o(
|
|
58
58
|
"input",
|
|
59
59
|
{
|
|
60
60
|
...g,
|
|
61
|
-
...
|
|
61
|
+
...b,
|
|
62
62
|
"data-test-id": "fondue-segmented-controls-input",
|
|
63
|
-
ref:
|
|
63
|
+
ref: r
|
|
64
64
|
}
|
|
65
65
|
) }),
|
|
66
|
-
/* @__PURE__ */
|
|
67
|
-
|
|
68
|
-
t.value && /* @__PURE__ */
|
|
66
|
+
/* @__PURE__ */ y("span", { className: "tw-overflow-hidden tw-text-ellipsis tw-whitespace-nowrap tw-flex", children: [
|
|
67
|
+
x(t) && /* @__PURE__ */ o("span", { className: "tw-leading-3", "aria-label": t.ariaLabel, children: t.icon }),
|
|
68
|
+
t.value && /* @__PURE__ */ o("span", { className: x(t) ? "tw-ml-2" : "", children: t.value.toString() })
|
|
69
69
|
] })
|
|
70
70
|
]
|
|
71
71
|
}
|
|
@@ -74,71 +74,71 @@ const b = (u) => u.icon !== void 0, I = O((u, m) => {
|
|
|
74
74
|
t.id
|
|
75
75
|
);
|
|
76
76
|
});
|
|
77
|
-
|
|
78
|
-
const
|
|
77
|
+
C.displayName = "SegmentedControlsItem";
|
|
78
|
+
const X = ({
|
|
79
79
|
id: u,
|
|
80
80
|
items: m,
|
|
81
81
|
activeItemId: p,
|
|
82
82
|
onChange: t,
|
|
83
|
-
ariaLabel:
|
|
84
|
-
disabled:
|
|
83
|
+
ariaLabel: s = "SegmentedControls",
|
|
84
|
+
disabled: e = !1,
|
|
85
85
|
hugWidth: f = !1,
|
|
86
|
-
size:
|
|
86
|
+
size: r
|
|
87
87
|
}) => {
|
|
88
|
-
const
|
|
89
|
-
|
|
88
|
+
const i = k(u), g = { onChange: t, value: p, label: s, isDisabled: e }, l = $(g), { radioGroupProps: b } = P(g, l), a = N([]), v = D(() => m.map((c, w) => /* @__PURE__ */ o(
|
|
89
|
+
C,
|
|
90
90
|
{
|
|
91
|
-
id:
|
|
92
|
-
item:
|
|
93
|
-
disabled:
|
|
94
|
-
radioGroupState:
|
|
95
|
-
ref: (
|
|
96
|
-
size:
|
|
91
|
+
id: i,
|
|
92
|
+
item: c,
|
|
93
|
+
disabled: e,
|
|
94
|
+
radioGroupState: l,
|
|
95
|
+
ref: (n) => a.current[w] = n,
|
|
96
|
+
size: r
|
|
97
97
|
},
|
|
98
|
-
`fondue-segmented-controls-${
|
|
99
|
-
)), [m,
|
|
100
|
-
var
|
|
101
|
-
let
|
|
102
|
-
for (let
|
|
103
|
-
|
|
104
|
-
return `${
|
|
105
|
-
},
|
|
106
|
-
var
|
|
107
|
-
const
|
|
108
|
-
return `${(((
|
|
109
|
-
},
|
|
110
|
-
return /* @__PURE__ */
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
98
|
+
`fondue-segmented-controls-${i}-item-${c.id}`
|
|
99
|
+
)), [m, i, e, l, r]), d = m.findIndex((c) => c.id === l.selectedValue), I = f ? "" : "tw-w-full", R = f ? "tw-flex" : "tw-grid tw-grid-flow-col tw-auto-cols-fr tw-justify-evenly", L = () => {
|
|
100
|
+
var n;
|
|
101
|
+
let w = r === "small" || f ? -1 : 0;
|
|
102
|
+
for (let S = 0; S < d; S++)
|
|
103
|
+
w += ((n = a.current[S]) == null ? void 0 : n.clientWidth) ?? 0;
|
|
104
|
+
return `${w}px`;
|
|
105
|
+
}, G = () => {
|
|
106
|
+
var n;
|
|
107
|
+
const w = d === a.current.length - 1 ? 1 : -1;
|
|
108
|
+
return `${(((n = a.current[d]) == null ? void 0 : n.clientWidth) ?? 0) + w}px`;
|
|
109
|
+
}, V = L(), j = G();
|
|
110
|
+
return /* @__PURE__ */ o("div", { className: "tw-flex", children: /* @__PURE__ */ y(
|
|
111
|
+
"fieldset",
|
|
112
|
+
{
|
|
113
|
+
...b,
|
|
114
|
+
"data-test-id": "fondue-segmented-controls",
|
|
115
|
+
className: h([
|
|
116
|
+
"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",
|
|
117
|
+
I,
|
|
118
|
+
R
|
|
119
|
+
]),
|
|
120
|
+
children: [
|
|
121
|
+
/* @__PURE__ */ o(
|
|
122
|
+
O.div,
|
|
123
|
+
{
|
|
124
|
+
"aria-hidden": "true",
|
|
125
|
+
animate: { x: V, width: j },
|
|
126
|
+
initial: !1,
|
|
127
|
+
transition: { type: "tween", duration: 0.3 },
|
|
128
|
+
hidden: !p,
|
|
129
|
+
className: h([
|
|
130
|
+
"tw-absolute tw--inset-px tw-h-full tw-box-content tw-border tw-rounded tw-pointer-events-none",
|
|
131
|
+
e ? "tw-border-line-x-strong hover:tw-cursor-not-allowed" : "tw-border-line-xx-strong tw-bg-base"
|
|
132
|
+
])
|
|
133
|
+
}
|
|
134
|
+
),
|
|
135
|
+
v
|
|
136
|
+
]
|
|
137
|
+
}
|
|
138
|
+
) });
|
|
139
139
|
};
|
|
140
|
-
|
|
140
|
+
X.displayName = "FondueSegmentedControls";
|
|
141
141
|
export {
|
|
142
|
-
|
|
142
|
+
X as SegmentedControls
|
|
143
143
|
};
|
|
144
144
|
//# sourceMappingURL=SegmentedControls.es.js.map
|
|
@@ -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, useMemo, useRef } 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\n ? 'tw-text-text tw-bg-base tw-ease-in tw-duration-300'\n : 'tw-bg-box-disabled tw-text-box-disabled-inverse tw-ease-out tw-duration-100',\n disabled\n ? 'tw-text-box-disabled-inverse 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 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 isSmallOrHugWidth = size === 'small' || hugWidth;\n const getSliderX = () => {\n let translateX = isSmallOrHugWidth ? -1 : 0;\n for (let i = 0; i < selectedIndex; i++) {\n translateX += itemsRef.current[i]?.clientWidth ?? 0;\n }\n return `${translateX}px`;\n };\n\n const getSliderWidth = () => {\n const isLastElement = selectedIndex === itemsRef.current.length - 1;\n const baseValue = isSmallOrHugWidth ? 1 : 2;\n const width = isLastElement ? baseValue + 1 : baseValue;\n\n return `${(itemsRef.current[selectedIndex]?.clientWidth ?? 0) + width}px`;\n };\n const sliderTranslation = getSliderX();\n const sliderWidth = getSliderWidth();\n\n return (\n <div className=\"tw-flex\">\n <motion.div\n aria-hidden=\"true\"\n // div border is not included in width so it must be subtracted from translation.\n animate={{ x: sliderTranslation, width: sliderWidth }}\n initial={false}\n transition={{ type: 'tween', duration: 0.3 }}\n hidden={!activeItemId}\n className={merge([\n 'tw-absolute tw-h-9 tw-border tw-rounded tw-pointer-events-none tw-z-10',\n disabled ? 'tw-border-line-x-strong hover:tw-cursor-not-allowed' : 'tw-border-line-xx-strong',\n ])}\n />\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 {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","itemElements","useMemo","index","el","selectedIndex","width","alignment","isSmallOrHugWidth","getSliderX","translateX","getSliderWidth","isLastElement","baseValue","sliderTranslation","sliderWidth","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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AAA,UACAC;AAAA,QAAA,CACH;AAAA,QAEA,UAAAN;AAAA,MAAA;AAAA,IACL;AAAA,EACJ,EAAA,CAAA;AAER;AACAb,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, useMemo, useRef } 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 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 = () => {\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 };\n\n const getSliderWidth = () => {\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 };\n const sliderTranslation = getSliderX();\n const sliderWidth = getSliderWidth();\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={{ x: sliderTranslation, width: sliderWidth }}\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","itemElements","useMemo","index","el","selectedIndex","width","alignment","getSliderX","translateX","i","getSliderWidth","sliderTranslation","sliderWidth","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,IA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