@fanvue/ui 3.51.0 → 3.52.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/dist/cjs/components/Drawer/Drawer.cjs +11 -3
- package/dist/cjs/components/Drawer/Drawer.cjs.map +1 -1
- package/dist/cjs/components/InlineEmptyState/InlineEmptyState.cjs +42 -0
- package/dist/cjs/components/InlineEmptyState/InlineEmptyState.cjs.map +1 -0
- package/dist/cjs/components/Link/Link.cjs +30 -7
- package/dist/cjs/components/Link/Link.cjs.map +1 -1
- package/dist/cjs/components/SegmentedControl/SegmentedControl.cjs +6 -10
- package/dist/cjs/components/SegmentedControl/SegmentedControl.cjs.map +1 -1
- package/dist/cjs/components/SegmentedControl/segmentedControlClassNames.cjs +54 -0
- package/dist/cjs/components/SegmentedControl/segmentedControlClassNames.cjs.map +1 -0
- package/dist/cjs/components/Table/Table.cjs +31 -2
- package/dist/cjs/components/Table/Table.cjs.map +1 -1
- package/dist/cjs/components/Table/TableRowLink.cjs +55 -0
- package/dist/cjs/components/Table/TableRowLink.cjs.map +1 -0
- package/dist/cjs/index.cjs +4 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/components/Drawer/Drawer.mjs +11 -3
- package/dist/components/Drawer/Drawer.mjs.map +1 -1
- package/dist/components/InlineEmptyState/InlineEmptyState.mjs +25 -0
- package/dist/components/InlineEmptyState/InlineEmptyState.mjs.map +1 -0
- package/dist/components/Link/Link.mjs +31 -8
- package/dist/components/Link/Link.mjs.map +1 -1
- package/dist/components/SegmentedControl/SegmentedControl.mjs +6 -10
- package/dist/components/SegmentedControl/SegmentedControl.mjs.map +1 -1
- package/dist/components/SegmentedControl/segmentedControlClassNames.mjs +54 -0
- package/dist/components/SegmentedControl/segmentedControlClassNames.mjs.map +1 -0
- package/dist/components/Table/Table.mjs +31 -2
- package/dist/components/Table/Table.mjs.map +1 -1
- package/dist/components/Table/TableRowLink.mjs +38 -0
- package/dist/components/Table/TableRowLink.mjs.map +1 -0
- package/dist/index.d.ts +122 -15
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { Slot, Slottable } from "@radix-ui/react-slot";
|
|
4
4
|
import * as React from "react";
|
|
5
5
|
import { cn } from "../../utils/cn.mjs";
|
|
@@ -11,9 +11,25 @@ const SIZE_CLASSES = {
|
|
|
11
11
|
const VARIANT_CLASSES = {
|
|
12
12
|
primary: "text-buttons-link-primary-default hover:text-buttons-link-primary-hover",
|
|
13
13
|
brand: "text-buttons-link-brand-default hover:text-buttons-link-brand-hover",
|
|
14
|
-
inherit: "text-inherit hover:opacity-80"
|
|
14
|
+
inherit: "text-inherit hover:opacity-80",
|
|
15
|
+
subtle: "text-content-secondary hover:text-content-primary"
|
|
15
16
|
};
|
|
16
17
|
const ICON_WRAPPER = "flex shrink-0 items-center justify-center [&>svg]:size-4";
|
|
18
|
+
const UNDERLINE_STYLE = "decoration-from-font decoration-solid [text-underline-position:from-font]";
|
|
19
|
+
function getUnderlineClassName(variant, asChild) {
|
|
20
|
+
if (variant !== "subtle") return cn("underline", UNDERLINE_STYLE);
|
|
21
|
+
return cn(
|
|
22
|
+
"no-underline underline-offset-4",
|
|
23
|
+
asChild ? "hover:underline" : "group-hover/link:underline",
|
|
24
|
+
UNDERLINE_STYLE
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
function getLayoutClassName(variant, size, hasIcon) {
|
|
28
|
+
const hoverGroup = variant === "subtle" && "group/link";
|
|
29
|
+
if (size === "inherit" && !hasIcon) return cn("inline", hoverGroup);
|
|
30
|
+
if (variant !== "subtle") return "inline-flex items-center gap-2";
|
|
31
|
+
return cn(hoverGroup, "inline-flex items-center gap-1", hasIcon && "min-h-8 px-1");
|
|
32
|
+
}
|
|
17
33
|
const Link = React.forwardRef(
|
|
18
34
|
({
|
|
19
35
|
className,
|
|
@@ -29,15 +45,22 @@ const Link = React.forwardRef(
|
|
|
29
45
|
...props
|
|
30
46
|
}, ref) => {
|
|
31
47
|
const Comp = asChild ? Slot : "a";
|
|
32
|
-
const textClasses = cn(
|
|
33
|
-
SIZE_CLASSES[size],
|
|
34
|
-
"underline decoration-from-font decoration-solid [text-underline-position:from-font]"
|
|
35
|
-
);
|
|
48
|
+
const textClasses = cn(SIZE_CLASSES[size], getUnderlineClassName(variant, asChild));
|
|
36
49
|
const disabledProps = disabled ? { "aria-disabled": true, tabIndex: -1 } : {};
|
|
37
50
|
const slotChild = disabled && React.isValidElement(children) && children.type === "a" ? React.cloneElement(children, { href: void 0 }) : children;
|
|
38
51
|
const handleClick = disabled ? (event) => event.preventDefault() : onClick;
|
|
39
52
|
const leftIconNode = leftIcon && /* @__PURE__ */ jsx("span", { className: ICON_WRAPPER, "aria-hidden": "true", children: leftIcon });
|
|
40
|
-
const rightIconNode = rightIcon && /* @__PURE__ */ jsx(
|
|
53
|
+
const rightIconNode = rightIcon && /* @__PURE__ */ jsx(
|
|
54
|
+
"span",
|
|
55
|
+
{
|
|
56
|
+
className: cn(
|
|
57
|
+
ICON_WRAPPER,
|
|
58
|
+
variant === "subtle" && "motion-safe:transition-transform motion-safe:group-hover/link:translate-x-0.5"
|
|
59
|
+
),
|
|
60
|
+
"aria-hidden": "true",
|
|
61
|
+
children: rightIcon
|
|
62
|
+
}
|
|
63
|
+
);
|
|
41
64
|
return /* @__PURE__ */ jsxs(
|
|
42
65
|
Comp,
|
|
43
66
|
{
|
|
@@ -45,7 +68,7 @@ const Link = React.forwardRef(
|
|
|
45
68
|
href: disabled ? void 0 : href,
|
|
46
69
|
onClick: handleClick,
|
|
47
70
|
className: cn(
|
|
48
|
-
size
|
|
71
|
+
getLayoutClassName(variant, size, Boolean(leftIcon || rightIcon)),
|
|
49
72
|
"rounded-2xs",
|
|
50
73
|
variant === "inherit" ? "transition-opacity" : "transition-colors",
|
|
51
74
|
"focus-visible:shadow-focus-ring focus-visible:outline-none",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Link.mjs","sources":["../../../src/components/Link/Link.tsx"],"sourcesContent":["import { Slot, Slottable } from \"@radix-ui/react-slot\";\nimport * as React from \"react\";\nimport { cn } from \"../../utils/cn\";\n\n/**\n * Visual style variant of the link. `inherit` takes the colour of the\n * surrounding text, for links inside tooltips, alerts and other copy that sets\n * its own colour.\n */\nexport type LinkVariant = \"primary\" | \"brand\" | \"inherit\";\n\n/**\n * Text size of the link in pixels. `inherit` keeps the font size and weight of\n * the surrounding text, for a link that sits inside a sentence. Without icons it\n * renders inline, so a long link wraps with the sentence.\n */\nexport type LinkSize = \"16\" | \"14\" | \"inherit\";\n\nexport interface LinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {\n /** Visual style variant of the link. @default \"primary\" */\n variant?: LinkVariant;\n /** Text size of the link in pixels. @default \"16\" */\n size?: LinkSize;\n /** When `true`, greys the link out and blocks interaction. @default false */\n disabled?: boolean;\n /** Icon element displayed before the label. */\n leftIcon?: React.ReactNode;\n /** Icon element displayed after the label. */\n rightIcon?: React.ReactNode;\n /** Merge props onto a child element instead of rendering an `<a>`. @default false */\n asChild?: boolean;\n}\n\nconst SIZE_CLASSES: Record<LinkSize, string> = {\n \"16\": \"typography-body-default-16px-semibold\",\n \"14\": \"typography-body-small-14px-semibold\",\n inherit: \"\",\n};\n\nconst VARIANT_CLASSES: Record<LinkVariant, string> = {\n primary: \"text-buttons-link-primary-default hover:text-buttons-link-primary-hover\",\n brand: \"text-buttons-link-brand-default hover:text-buttons-link-brand-hover\",\n inherit: \"text-inherit hover:opacity-80\",\n};\n\nconst ICON_WRAPPER = \"flex shrink-0 items-center justify-center [&>svg]:size-4\";\n\n/**\n * An inline text link for navigating to a related page or section without\n * using a button. Use `primary` for standard navigation and `brand` for the\n * Fanvue green accent on calls to action within content. Use `inherit` with\n * `size=\"inherit\"` for a link inside copy that sets its own colour and type,\n * such as a tooltip.\n *\n * Renders an underlined `<a>` by default. Pass `asChild` to compose with a\n * router link (e.g. Next.js `Link`). When rendering without a visible label,\n * provide an `aria-label`.\n *\n * @example\n * ```tsx\n * <Link href=\"/pricing\" variant=\"brand\">See our plans</Link>\n * ```\n */\nexport const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(\n (\n {\n className,\n variant = \"primary\",\n size = \"16\",\n disabled = false,\n leftIcon,\n rightIcon,\n asChild = false,\n children,\n href,\n onClick,\n ...props\n },\n ref,\n ) => {\n const Comp = asChild ? Slot : \"a\";\n\n const textClasses = cn(
|
|
1
|
+
{"version":3,"file":"Link.mjs","sources":["../../../src/components/Link/Link.tsx"],"sourcesContent":["import { Slot, Slottable } from \"@radix-ui/react-slot\";\nimport * as React from \"react\";\nimport { cn } from \"../../utils/cn\";\n\n/**\n * Visual style variant of the link. `inherit` takes the colour of the\n * surrounding text, for links inside tooltips, alerts and other copy that sets\n * its own colour. `subtle` is a quiet \"go there\" link for card headers, footers\n * and toolbars: secondary text that turns primary and underlines on hover. With\n * an icon it is at least 32px tall, so it sits level with a 32px control, and a\n * `rightIcon` moves 2px towards the destination on hover unless the user asks\n * for reduced motion.\n */\nexport type LinkVariant = \"primary\" | \"brand\" | \"inherit\" | \"subtle\";\n\n/**\n * Text size of the link in pixels. `inherit` keeps the font size and weight of\n * the surrounding text, for a link that sits inside a sentence. Without icons it\n * renders inline, so a long link wraps with the sentence.\n */\nexport type LinkSize = \"16\" | \"14\" | \"inherit\";\n\nexport interface LinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {\n /** Visual style variant of the link. @default \"primary\" */\n variant?: LinkVariant;\n /** Text size of the link in pixels. @default \"16\" */\n size?: LinkSize;\n /** When `true`, greys the link out and blocks interaction. @default false */\n disabled?: boolean;\n /** Icon element displayed before the label. */\n leftIcon?: React.ReactNode;\n /** Icon element displayed after the label. */\n rightIcon?: React.ReactNode;\n /** Merge props onto a child element instead of rendering an `<a>`. @default false */\n asChild?: boolean;\n}\n\nconst SIZE_CLASSES: Record<LinkSize, string> = {\n \"16\": \"typography-body-default-16px-semibold\",\n \"14\": \"typography-body-small-14px-semibold\",\n inherit: \"\",\n};\n\nconst VARIANT_CLASSES: Record<LinkVariant, string> = {\n primary: \"text-buttons-link-primary-default hover:text-buttons-link-primary-hover\",\n brand: \"text-buttons-link-brand-default hover:text-buttons-link-brand-hover\",\n inherit: \"text-inherit hover:opacity-80\",\n subtle: \"text-content-secondary hover:text-content-primary\",\n};\n\nconst ICON_WRAPPER = \"flex shrink-0 items-center justify-center [&>svg]:size-4\";\n\nconst UNDERLINE_STYLE = \"decoration-from-font decoration-solid [text-underline-position:from-font]\";\n\n/**\n * Underline classes for the label. Every variant is underlined at rest except\n * `subtle`, which underlines on hover only. The label is the root itself under\n * `asChild` and a child `<span>` otherwise, so the hover is read from the root\n * in both cases.\n */\nfunction getUnderlineClassName(variant: LinkVariant, asChild: boolean) {\n if (variant !== \"subtle\") return cn(\"underline\", UNDERLINE_STYLE);\n return cn(\n \"no-underline underline-offset-4\",\n asChild ? \"hover:underline\" : \"group-hover/link:underline\",\n UNDERLINE_STYLE,\n );\n}\n\n/**\n * Layout classes for the root: inline for a link inside a sentence, and a flex\n * row otherwise. `subtle` tightens the gap, and with an icon takes the minimum\n * height and side padding of a 32px control, growing if its label wraps. A `subtle` root is always the hover\n * group its label and icon read from, in both layouts.\n */\nfunction getLayoutClassName(variant: LinkVariant, size: LinkSize, hasIcon: boolean) {\n const hoverGroup = variant === \"subtle\" && \"group/link\";\n if (size === \"inherit\" && !hasIcon) return cn(\"inline\", hoverGroup);\n if (variant !== \"subtle\") return \"inline-flex items-center gap-2\";\n return cn(hoverGroup, \"inline-flex items-center gap-1\", hasIcon && \"min-h-8 px-1\");\n}\n\n/**\n * An inline text link for navigating to a related page or section without\n * using a button. Use `primary` for standard navigation and `brand` for the\n * Fanvue green accent on calls to action within content. Use `inherit` with\n * `size=\"inherit\"` for a link inside copy that sets its own colour and type,\n * such as a tooltip. Use `subtle` for a quiet link beside controls, such as\n * \"Open full page\" in a panel footer.\n *\n * Renders an underlined `<a>` by default. Pass `asChild` to compose with a\n * router link (e.g. Next.js `Link`). When rendering without a visible label,\n * provide an `aria-label`.\n *\n * @example\n * ```tsx\n * <Link href=\"/pricing\" variant=\"brand\">See our plans</Link>\n * ```\n *\n * @example A quiet link with an arrow, level with a 32px control\n * ```tsx\n * <Link href=\"/campaigns/1\" variant=\"subtle\" size=\"14\" rightIcon={<ArrowRightIcon />}>\n * Open full page\n * </Link>\n * ```\n */\nexport const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(\n (\n {\n className,\n variant = \"primary\",\n size = \"16\",\n disabled = false,\n leftIcon,\n rightIcon,\n asChild = false,\n children,\n href,\n onClick,\n ...props\n },\n ref,\n ) => {\n const Comp = asChild ? Slot : \"a\";\n\n const textClasses = cn(SIZE_CLASSES[size], getUnderlineClassName(variant, asChild));\n\n const disabledProps = disabled ? { \"aria-disabled\": true, tabIndex: -1 } : {};\n\n // Radix Slot lets the child's own props win when merging, so a disabled\n // `asChild` link would keep the child's navigable href. Strip it from the\n // child (anchors only) and block default navigation to honor `disabled`.\n const slotChild =\n disabled && React.isValidElement<{ href?: string }>(children) && children.type === \"a\"\n ? React.cloneElement(children, { href: undefined })\n : children;\n\n const handleClick = disabled\n ? (event: React.MouseEvent<HTMLAnchorElement>) => event.preventDefault()\n : onClick;\n\n const leftIconNode = leftIcon && (\n <span className={ICON_WRAPPER} aria-hidden=\"true\">\n {leftIcon}\n </span>\n );\n const rightIconNode = rightIcon && (\n <span\n className={cn(\n ICON_WRAPPER,\n variant === \"subtle\" &&\n \"motion-safe:transition-transform motion-safe:group-hover/link:translate-x-0.5\",\n )}\n aria-hidden=\"true\"\n >\n {rightIcon}\n </span>\n );\n\n return (\n <Comp\n ref={ref}\n href={disabled ? undefined : href}\n onClick={handleClick}\n className={cn(\n getLayoutClassName(variant, size, Boolean(leftIcon || rightIcon)),\n \"rounded-2xs\",\n variant === \"inherit\" ? \"transition-opacity\" : \"transition-colors\",\n \"focus-visible:shadow-focus-ring focus-visible:outline-none\",\n disabled\n ? \"pointer-events-none cursor-not-allowed text-content-disabled\"\n : VARIANT_CLASSES[variant],\n asChild && textClasses,\n className,\n )}\n {...(asChild ? {} : { \"data-testid\": \"link\" })}\n {...disabledProps}\n {...props}\n >\n {leftIconNode}\n {asChild ? (\n <Slottable>{slotChild}</Slottable>\n ) : (\n <span className={textClasses}>{children}</span>\n )}\n {rightIconNode}\n </Comp>\n );\n },\n);\n\nLink.displayName = \"Link\";\n"],"names":[],"mappings":";;;;;AAqCA,MAAM,eAAyC;AAAA,EAC7C,MAAM;AAAA,EACN,MAAM;AAAA,EACN,SAAS;AACX;AAEA,MAAM,kBAA+C;AAAA,EACnD,SAAS;AAAA,EACT,OAAO;AAAA,EACP,SAAS;AAAA,EACT,QAAQ;AACV;AAEA,MAAM,eAAe;AAErB,MAAM,kBAAkB;AAQxB,SAAS,sBAAsB,SAAsB,SAAkB;AACrE,MAAI,YAAY,SAAU,QAAO,GAAG,aAAa,eAAe;AAChE,SAAO;AAAA,IACL;AAAA,IACA,UAAU,oBAAoB;AAAA,IAC9B;AAAA,EAAA;AAEJ;AAQA,SAAS,mBAAmB,SAAsB,MAAgB,SAAkB;AAClF,QAAM,aAAa,YAAY,YAAY;AAC3C,MAAI,SAAS,aAAa,CAAC,QAAS,QAAO,GAAG,UAAU,UAAU;AAClE,MAAI,YAAY,SAAU,QAAO;AACjC,SAAO,GAAG,YAAY,kCAAkC,WAAW,cAAc;AACnF;AA0BO,MAAM,OAAO,MAAM;AAAA,EACxB,CACE;AAAA,IACE;AAAA,IACA,UAAU;AAAA,IACV,OAAO;AAAA,IACP,WAAW;AAAA,IACX;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA,GAAG;AAAA,EAAA,GAEL,QACG;AACH,UAAM,OAAO,UAAU,OAAO;AAE9B,U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
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import { cn } from "../../utils/cn.mjs";
|
|
5
|
+
import { getRootClassName, getSquaredSegmentClassName } from "./segmentedControlClassNames.mjs";
|
|
5
6
|
const sizeClasses = {
|
|
6
7
|
"32": "px-2 py-1 typography-description-12px-semibold",
|
|
7
8
|
"40": "px-3 py-1.75 typography-body-small-14px-semibold",
|
|
@@ -29,7 +30,7 @@ function warnMissingOptionAccessibleName(options) {
|
|
|
29
30
|
}
|
|
30
31
|
function warnUnsupportedCollapsible(appearance, options, hasCollapsedIcon) {
|
|
31
32
|
if (process.env.NODE_ENV !== "production") {
|
|
32
|
-
if (appearance === "pill") {
|
|
33
|
+
if (appearance === "pill" || appearance === "squared") {
|
|
33
34
|
console.warn(
|
|
34
35
|
'SegmentedControl: `collapsible` is only supported for the "plain" and "brand" appearances; ignoring it.'
|
|
35
36
|
);
|
|
@@ -48,6 +49,9 @@ function getSegmentClassName({
|
|
|
48
49
|
isAiSegment,
|
|
49
50
|
disabled
|
|
50
51
|
}) {
|
|
52
|
+
if (appearance === "squared") {
|
|
53
|
+
return getSquaredSegmentClassName({ size, variant, isSelected, disabled });
|
|
54
|
+
}
|
|
51
55
|
return cn(
|
|
52
56
|
"relative inline-flex min-w-0 cursor-pointer items-center justify-center rounded-full",
|
|
53
57
|
"motion-safe:transition-colors motion-safe:duration-150 motion-safe:ease-in-out",
|
|
@@ -254,15 +258,7 @@ const SegmentedControl = React.forwardRef(
|
|
|
254
258
|
ref: setRootRef,
|
|
255
259
|
role: isCollapsed ? "group" : "radiogroup",
|
|
256
260
|
className: cn(
|
|
257
|
-
|
|
258
|
-
variant === "fill" ? "flex w-full" : "inline-flex",
|
|
259
|
-
isCollapsed ? (
|
|
260
|
-
// Collapsed, the toggle itself carries the surface, so the container drops its own
|
|
261
|
-
// chrome and centres the button instead of pinning it to the start of a full-width
|
|
262
|
-
// row — in a narrow rail that offset is the whole width of the column.
|
|
263
|
-
"justify-center"
|
|
264
|
-
) : appearance === "plain" ? "gap-2" : "bg-surface-tertiary p-1",
|
|
265
|
-
disabled && "cursor-not-allowed opacity-50",
|
|
261
|
+
getRootClassName({ appearance, variant, size, isCollapsed, disabled }),
|
|
266
262
|
className
|
|
267
263
|
),
|
|
268
264
|
...props,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SegmentedControl.mjs","sources":["../../../src/components/SegmentedControl/SegmentedControl.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { cn } from \"../../utils/cn\";\n\n/** Height of the segmented control in pixels. */\nexport type SegmentedControlSize = \"32\" | \"40\" | \"48\";\n\n/**\n * Segment layout.\n * - `\"hug\"`: each segment is sized to its content.\n * - `\"fill\"`: the control spans the full width of its container and each segment grows equally.\n */\nexport type SegmentedControlVariant = \"hug\" | \"fill\";\n\n/**\n * Visual style of the control.\n * - `\"pill\"`: the container has a muted background and the selected segment shows a filled pill (default).\n * - `\"plain\"`: no container or selected-pill background; segments are bare content and selection is\n * communicated by color alone. Designed for icon-only toggles (e.g. a list/grid view switch).\n * - `\"brand\"`: like `\"pill\"`, but segments render their `icon` alongside the visible `label`.\n * A selected segment takes the neutral selected pill, except the one marked `ai`, which takes the\n * AI fill and gradient stroke. Designed for prominent toggles such as the Home/Agent navigation\n * switch.\n */\nexport type SegmentedControlAppearance = \"pill\" | \"plain\" | \"brand\";\n\n/** Describes one selectable segment. */\nexport interface SegmentedControlOption {\n /**\n * Display label for the segment. In `pill`/`plain` appearances, when `icon` is provided the\n * segment renders icon-only and this value becomes its accessible name (applied as `aria-label`)\n * instead of visible text. In the `brand` appearance the label is always rendered as visible text\n * (alongside the icon when present).\n */\n label: string;\n /** Value identifier returned via `onChange`. */\n value: string;\n /**\n * Icon to render for the segment. In `pill`/`plain` appearances the segment renders icon-only and\n * `label` becomes required as its accessible name (applied as `aria-label`, no visible text). In\n * the `brand` appearance the icon renders alongside the visible `label`. Required for every option\n * when `collapsible` is set unless a `collapsedIcon` is given, since the collapsed control\n * otherwise shows the selected option's icon alone.\n */\n icon?: React.ReactNode;\n /**\n * Marks this segment as the AI one, so that when it is selected it takes the AI treatment — the\n * translucent `Buttons/AI/Default` fill and the `Buttons/AI` gradient stroke — instead of the\n * neutral selected pill every other segment gets.\n *\n * Set it on the Agent side of a Manage/Agent switch. Without it the AI treatment would follow\n * whichever segment happened to be selected, putting a brand stroke on segments that should not\n * carry one. Only meaningful in the `brand` appearance, and only while the control is expanded —\n * collapsed, the toggle stays the neutral switch affordance whichever option is selected.\n * @default false\n */\n ai?: boolean;\n}\n\nexport interface SegmentedControlProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n /** Height of the control in pixels. @default \"32\" */\n size?: SegmentedControlSize;\n /** Segment layout. @default \"hug\" */\n variant?: SegmentedControlVariant;\n /** Visual style of the control. @default \"pill\" */\n appearance?: SegmentedControlAppearance;\n /** The selectable segments. Designed for two or three mutually exclusive options. */\n options: SegmentedControlOption[];\n /** Currently selected value (controlled). */\n value?: string;\n /** Initially selected value (uncontrolled). Defaults to the first option. */\n defaultValue?: string;\n /** Callback fired when the selected value changes. */\n onChange?: (value: string) => void;\n /** Whether the control is disabled. @default false */\n disabled?: boolean;\n /**\n * When `true`, the control automatically collapses to a single icon-only toggle whenever its\n * container is too narrow to show every segment side by side, and expands again when the space\n * returns. Collapsed, it shows `collapsedIcon` when given and otherwise the currently-selected\n * option's `icon`; clicking it (or pressing Enter/Space) advances to the next option, wrapping\n * around from the last back to the first.\n *\n * Only supported for the icon-bearing `\"plain\"` and `\"brand\"` appearances, and every option must\n * define an `icon` unless `collapsedIcon` is given (there is nothing to show otherwise). Ignored,\n * with a dev-time warning, when those conditions are not met. @default false\n */\n collapsible?: boolean;\n /**\n * Single glyph for the collapsed toggle, replacing the selected option's icon. Use it when the\n * collapsed control should read as one switch affordance rather than as a preview of the current\n * selection — the collapsed navigation rail shows a repeat glyph this way. The button still\n * announces the selected option as its accessible name, so the current state stays available to\n * assistive tech. Only meaningful alongside `collapsible`.\n */\n collapsedIcon?: React.ReactNode;\n}\n\n/** Padding and typography per size. Combined with the container's `p-1` these hit the 32/40/48px heights. */\nconst sizeClasses: Record<SegmentedControlSize, string> = {\n \"32\": \"px-2 py-1 typography-description-12px-semibold\",\n \"40\": \"px-3 py-1.75 typography-body-small-14px-semibold\",\n \"48\": \"px-4 py-2 typography-body-default-16px-semibold\",\n};\n\nfunction warnMissingAccessibleName(ariaLabel?: string, ariaLabelledBy?: string) {\n if (process.env.NODE_ENV !== \"production\") {\n if (!ariaLabel && !ariaLabelledBy) {\n console.warn(\n \"SegmentedControl: no accessible name provided. Pass an `aria-label` or `aria-labelledby` prop.\",\n );\n }\n }\n}\n\nfunction warnMissingOptionAccessibleName(options: SegmentedControlOption[]) {\n if (process.env.NODE_ENV !== \"production\") {\n for (const option of options) {\n if (option.icon && !option.label?.trim()) {\n console.warn(\n `SegmentedControl: icon-only segment \"${option.value}\" is missing a non-empty \\`label\\` to use as its accessible name.`,\n );\n }\n }\n }\n}\n\nfunction warnUnsupportedCollapsible(\n appearance: SegmentedControlAppearance,\n options: SegmentedControlOption[],\n hasCollapsedIcon: boolean,\n) {\n if (process.env.NODE_ENV !== \"production\") {\n if (appearance === \"pill\") {\n console.warn(\n 'SegmentedControl: `collapsible` is only supported for the \"plain\" and \"brand\" appearances; ignoring it.',\n );\n } else if (!hasCollapsedIcon && !options.every((option) => option.icon)) {\n console.warn(\n \"SegmentedControl: `collapsible` requires every option to define an `icon`, or a `collapsedIcon` to show instead; ignoring it.\",\n );\n }\n }\n}\n\nfunction getSegmentClassName({\n appearance,\n size,\n variant,\n isSelected,\n isAiSegment,\n disabled,\n}: {\n appearance: SegmentedControlAppearance;\n size: SegmentedControlSize;\n variant: SegmentedControlVariant;\n isSelected: boolean;\n isAiSegment: boolean;\n disabled: boolean;\n}) {\n return cn(\n \"relative inline-flex min-w-0 cursor-pointer items-center justify-center rounded-full\",\n \"motion-safe:transition-colors motion-safe:duration-150 motion-safe:ease-in-out\",\n \"focus-visible:shadow-focus-ring focus-visible:outline-none\",\n variant === \"fill\" ? \"flex-1\" : \"shrink-0\",\n appearance === \"plain\"\n ? cn(\n // Padding + negative margin enlarge the hit target without changing the\n // visual footprint, which must stay glyph-only to match the design.\n \"-m-1 p-1\",\n isSelected ? \"text-icons-primary\" : \"text-icons-tertiary hover:text-icons-primary\",\n )\n : cn(\n sizeClasses[size],\n // brand renders icon + label together, so space them.\n appearance === \"brand\" && \"gap-1.5\",\n isSelected\n ? appearance === \"brand\" && isAiSegment\n ? // The AI segment: translucent AI fill plus the AI gradient stroke. Scoped to the\n // segment marked `ai` rather than to whatever is selected, so a brand stroke never\n // lands on a segment that should read as plain.\n \"fv-ai-stroke bg-buttons-ai-default text-content-primary\"\n : // Every other selected segment, `brand` included, takes the neutral pill. Its\n // tokens invert with the theme, so this is a white pill on dark and a black one on\n // light.\n \"bg-buttons-primary-default text-content-primary-inverted shadow-sm\"\n : \"text-content-primary hover:bg-buttons-switch-hover\",\n ),\n disabled && \"pointer-events-none\",\n );\n}\n\n/**\n * Footprint of the collapsed toggle per size. A fixed square keeps the collapsed control the same\n * height as the expanded one, whose height comes from the segment padding plus the container's\n * `p-1` — padding the collapsed button alone would fall short of it by that container padding.\n */\nconst collapsedSizeClasses: Record<SegmentedControlSize, string> = {\n \"32\": \"size-8\",\n \"40\": \"size-10\",\n \"48\": \"size-12\",\n};\n\n/**\n * Classes for the single button shown while the control is collapsed. Unlike an expanded segment\n * (which carries asymmetric `px`/`py` padding and so reads slightly wider than tall), the collapsed\n * toggle is forced to a square via a fixed footprint and `aspect-square`, so with `rounded-full` the\n * icon-only control reads as a round button.\n */\nfunction getCollapsedButtonClassName({\n appearance,\n size,\n disabled,\n}: {\n appearance: SegmentedControlAppearance;\n size: SegmentedControlSize;\n disabled: boolean;\n}) {\n return cn(\n \"relative inline-flex aspect-square shrink-0 cursor-pointer items-center justify-center rounded-full\",\n \"motion-safe:transition-colors motion-safe:duration-150 motion-safe:ease-in-out\",\n \"focus-visible:shadow-focus-ring focus-visible:outline-none\",\n appearance === \"plain\"\n ? // Glyph-only: no pill, just the selected icon colour. The negative margin keeps the visual\n // footprint icon-sized while the padding enlarges the hit target.\n \"-m-1 p-1 text-icons-primary\"\n : cn(\n collapsedSizeClasses[size],\n // Neutral circle rather than the brand pill: collapsed, the control is a switch\n // affordance, not a preview of the selected segment.\n \"bg-surface-secondary text-icons-primary hover:bg-buttons-switch-hover\",\n ),\n disabled && \"pointer-events-none\",\n );\n}\n\n/**\n * Watches `rootRef`'s available width against the natural width of the fully expanded control\n * (measured from `measureRef`, an off-screen replica) and returns whether the control should\n * collapse. Comparing available space to a stable off-screen measurement — rather than to the\n * live control, which shrinks when it collapses — avoids the expand/collapse oscillation that a\n * self-referential measurement would cause. Returns `false` until both widths are measurable\n * (SSR, jsdom, or a zero-width layout), so the control renders expanded by default.\n */\nfunction useAutoCollapse(\n rootRef: React.RefObject<HTMLDivElement | null>,\n measureRef: React.RefObject<HTMLDivElement | null>,\n enabled: boolean,\n): boolean {\n const [collapsed, setCollapsed] = React.useState(false);\n\n React.useEffect(() => {\n if (!enabled) {\n setCollapsed(false);\n return;\n }\n const root = rootRef.current;\n const measure = measureRef.current;\n const parent = root?.parentElement;\n if (!root || !measure || !parent) return;\n\n const evaluate = () => {\n const available = parent.getBoundingClientRect().width;\n const required = measure.getBoundingClientRect().width;\n // Both unmeasured (SSR/jsdom/zero-width): leave the current state untouched.\n if (available <= 0 || required <= 0) return;\n setCollapsed(required > available);\n };\n\n evaluate();\n\n const observer = new ResizeObserver(evaluate);\n observer.observe(parent);\n observer.observe(measure);\n return () => observer.disconnect();\n }, [enabled, rootRef, measureRef]);\n\n return enabled && collapsed;\n}\n\n/**\n * A compact selector for choosing between two or three mutually exclusive\n * options where the choice affects the content immediately below it. Use\n * instead of tabs when the options are more like settings or filters than\n * navigation, such as toggling a list/grid view or a monthly/annual price.\n *\n * Rendered as a `radiogroup` with roving-tabindex keyboard navigation. Supports\n * both controlled and uncontrolled usage. With `collapsible`, the icon-bearing\n * `plain`/`brand` appearances shrink to a single cycling icon toggle when space\n * is tight (rendered as a `group` containing one button).\n *\n * @example\n * ```tsx\n * <SegmentedControl\n * options={[\n * { label: \"Net\", value: \"net\" },\n * { label: \"Gross\", value: \"gross\" },\n * ]}\n * value={amount}\n * onChange={setAmount}\n * aria-label=\"Amount type\"\n * />\n * ```\n *\n * @example Icon-only segments (e.g. a list/grid view toggle)\n * ```tsx\n * <SegmentedControl\n * appearance=\"plain\"\n * options={[\n * { label: \"List view\", value: \"list\", icon: <ListViewIcon size={16} /> },\n * { label: \"Grid view\", value: \"grid\", icon: <GridViewIcon size={16} /> },\n * ]}\n * value={view}\n * onChange={setView}\n * aria-label=\"View\"\n * />\n * ```\n *\n * @example Icon + label, with the AI treatment on the Agent segment (the Home/Agent nav switch)\n * ```tsx\n * <SegmentedControl\n * appearance=\"brand\"\n * options={[\n * { label: \"Home\", value: \"home\", icon: <HomeIcon size={16} /> },\n * { label: \"Agent\", value: \"agent\", icon: <AIIcon size={16} />, ai: true },\n * ]}\n * value={mode}\n * onChange={setMode}\n * aria-label=\"Navigation mode\"\n * />\n * ```\n *\n * @example Collapses to a cycling icon toggle when the container is too narrow\n * ```tsx\n * <SegmentedControl\n * appearance=\"plain\"\n * collapsible\n * options={[\n * { label: \"List view\", value: \"list\", icon: <ListViewIcon size={16} /> },\n * { label: \"Grid view\", value: \"grid\", icon: <GridViewIcon size={16} /> },\n * ]}\n * value={view}\n * onChange={setView}\n * aria-label=\"View\"\n * />\n * ```\n *\n * @example A collapsed toggle that shows one switch glyph instead of the selected icon\n * ```tsx\n * <SegmentedControl\n * appearance=\"brand\"\n * collapsible\n * collapsedIcon={<RepeatIcon size={16} />}\n * options={[\n * { label: \"Home\", value: \"home\", icon: <HomeIcon size={16} /> },\n * { label: \"Agent\", value: \"agent\", icon: <AIIcon size={16} filled />, ai: true },\n * ]}\n * value={mode}\n * onChange={setMode}\n * aria-label=\"Navigation mode\"\n * />\n * ```\n */\nexport const SegmentedControl = React.forwardRef<HTMLDivElement, SegmentedControlProps>(\n (\n {\n className,\n size = \"32\",\n variant = \"hug\",\n appearance = \"pill\",\n options,\n value: controlledValue,\n defaultValue,\n onChange,\n disabled = false,\n collapsible = false,\n collapsedIcon,\n ...props\n },\n ref,\n ) => {\n warnMissingAccessibleName(props[\"aria-label\"], props[\"aria-labelledby\"]);\n warnMissingOptionAccessibleName(options);\n if (collapsible) warnUnsupportedCollapsible(appearance, options, collapsedIcon !== undefined);\n\n // Tracks selection for uncontrolled usage; ignored when `value` prop is provided\n const [internalValue, setInternalValue] = React.useState(defaultValue ?? options[0]?.value);\n const isControlled = controlledValue !== undefined;\n const currentValue = isControlled ? controlledValue : internalValue;\n const anySelected = options.some((o) => o.value === currentValue);\n const buttonRefs = React.useRef<(HTMLButtonElement | null)[]>([]);\n\n // Collapsing only makes sense for icon-bearing appearances where there is a glyph to show:\n // either a `collapsedIcon` for every state, or an icon on every option. Anything else falls\n // back to the normal expanded control (a dev warning fires above).\n const canCollapse =\n collapsible &&\n (appearance === \"plain\" || appearance === \"brand\") &&\n (collapsedIcon !== undefined || options.every((option) => option.icon));\n\n const rootRef = React.useRef<HTMLDivElement | null>(null);\n const measureRef = React.useRef<HTMLDivElement | null>(null);\n const setRootRef = React.useCallback(\n (node: HTMLDivElement | null) => {\n rootRef.current = node;\n if (typeof ref === \"function\") ref(node);\n else if (ref) ref.current = node;\n },\n [ref],\n );\n const isCollapsed = useAutoCollapse(rootRef, measureRef, canCollapse);\n\n const handleSelect = (optionValue: string) => {\n if (disabled || optionValue === currentValue) return;\n if (!isControlled) {\n setInternalValue(optionValue);\n }\n onChange?.(optionValue);\n };\n\n const handleKeyDown = (e: React.KeyboardEvent, index: number) => {\n const nextIndex =\n e.key === \"ArrowRight\" || e.key === \"ArrowDown\"\n ? (index + 1) % options.length\n : e.key === \"ArrowLeft\" || e.key === \"ArrowUp\"\n ? (index - 1 + options.length) % options.length\n : null;\n if (nextIndex === null) return;\n e.preventDefault();\n const nextOption = options[nextIndex] as SegmentedControlOption;\n handleSelect(nextOption.value);\n buttonRefs.current[nextIndex]?.focus();\n };\n\n const renderSegment = (option: SegmentedControlOption, index: number) => {\n const isSelected = currentValue === option.value;\n // `brand` shows the label as visible text (with the icon); `pill`/`plain` render\n // icon-only when an icon is given, falling back to the label otherwise.\n const showLabelText = appearance === \"brand\" || !option.icon;\n return (\n // biome-ignore lint/a11y/useSemanticElements: native radio inputs only allow Tab-focus on the checked item; buttons with roving tabindex give full keyboard navigation\n <button\n key={option.value}\n ref={(el) => {\n buttonRefs.current[index] = el;\n }}\n type=\"button\"\n role=\"radio\"\n aria-checked={isSelected}\n tabIndex={isSelected || (!anySelected && index === 0) ? 0 : -1}\n disabled={disabled}\n aria-label={showLabelText ? undefined : option.label}\n onClick={() => handleSelect(option.value)}\n onKeyDown={(e) => handleKeyDown(e, index)}\n className={getSegmentClassName({\n appearance,\n size,\n variant,\n isSelected,\n isAiSegment: option.ai === true,\n disabled,\n })}\n >\n {option.icon && (\n <span className=\"flex shrink-0 items-center justify-center\" aria-hidden=\"true\">\n {option.icon}\n </span>\n )}\n {showLabelText && <span className=\"min-w-0 truncate\">{option.label}</span>}\n </button>\n );\n };\n\n // Off-screen replica of one segment used only to size the expanded control. Rendered as an\n // inert (disabled + `aria-hidden`) button with no `radio` semantics so it stays out of the\n // accessibility tree and tab order, and always with hug sizing so the replica reflects the\n // natural (uncompressed) width even under a `fill` layout.\n const renderMeasureSegment = (option: SegmentedControlOption) => {\n const showLabelText = appearance === \"brand\" || !option.icon;\n return (\n <button\n key={option.value}\n type=\"button\"\n disabled\n aria-hidden=\"true\"\n tabIndex={-1}\n className={getSegmentClassName({\n appearance,\n size,\n variant: \"hug\",\n isSelected: currentValue === option.value,\n isAiSegment: option.ai === true,\n disabled,\n })}\n >\n {option.icon && (\n <span className=\"flex shrink-0 items-center justify-center\" aria-hidden=\"true\">\n {option.icon}\n </span>\n )}\n {showLabelText && <span className=\"min-w-0 truncate\">{option.label}</span>}\n </button>\n );\n };\n\n // Collapsed: a single button showing the selected option's icon that cycles to the next\n // option on activation, wrapping from the last option back to the first.\n const selectedIndex = options.findIndex((option) => option.value === currentValue);\n const selectedOption = options[selectedIndex] ?? options[0];\n const handleCycle = () => {\n if (disabled || options.length === 0) return;\n const from = selectedIndex < 0 ? 0 : selectedIndex;\n const nextOption = options[(from + 1) % options.length] as SegmentedControlOption;\n handleSelect(nextOption.value);\n };\n\n return (\n <div\n ref={setRootRef}\n role={isCollapsed ? \"group\" : \"radiogroup\"}\n className={cn(\n \"relative items-center rounded-full\",\n variant === \"fill\" ? \"flex w-full\" : \"inline-flex\",\n isCollapsed\n ? // Collapsed, the toggle itself carries the surface, so the container drops its own\n // chrome and centres the button instead of pinning it to the start of a full-width\n // row — in a narrow rail that offset is the whole width of the column.\n \"justify-center\"\n : appearance === \"plain\"\n ? \"gap-2\"\n : \"bg-surface-tertiary p-1\",\n disabled && \"cursor-not-allowed opacity-50\",\n className,\n )}\n {...props}\n >\n {isCollapsed && selectedOption ? (\n <button\n type=\"button\"\n disabled={disabled}\n aria-label={selectedOption.label}\n onClick={handleCycle}\n className={getCollapsedButtonClassName({ appearance, size, disabled })}\n >\n {(collapsedIcon ?? selectedOption.icon) && (\n <span className=\"flex shrink-0 items-center justify-center\" aria-hidden=\"true\">\n {collapsedIcon ?? selectedOption.icon}\n </span>\n )}\n </button>\n ) : (\n options.map((option, index) => renderSegment(option, index))\n )}\n\n {canCollapse && (\n // Off-screen replica of the fully expanded control. Kept in the DOM (out of flow, hidden\n // from AT and tab order) so its natural width is always measurable, whether the visible\n // control is currently expanded or collapsed.\n <div\n ref={measureRef}\n aria-hidden=\"true\"\n className={cn(\n \"pointer-events-none invisible absolute top-0 left-0 -z-10 flex items-center whitespace-nowrap rounded-full\",\n appearance === \"plain\" ? \"gap-2\" : \"bg-surface-tertiary p-1\",\n )}\n >\n {options.map((option) => renderMeasureSegment(option))}\n </div>\n )}\n </div>\n );\n },\n);\n\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AA,CACnC;AAAA,YAAA;AAAA,UAAA,IAIJ,QAAQ,IAAI,CAAC,QAAQ,UAAU,cAAc,QAAQ,KAAK,CAAC;AAAA,UAG5D;AAAA;AAAA;AAAA,UAIC;AAAA,YAAC;AAAA,YAAA;AAAA,cACC,KAAK;AAAA,cACL,eAAY;AAAA,cACZ,WAAW;AAAA,gBACT;AAAA,gBACA,eAAe,UAAU,UAAU;AAAA,cAAA;AAAA,cAGpC,kBAAQ,IAAI,CAAC,WAAW,qBAAqB,MAAM,CAAC;AAAA,YAAA;AAAA,UAAA;AAAA,QACvD;AAAA,MAAA;AAAA,IAAA;AAAA,EAIR;AACF;AAEA,iBAAiB,cAAc;"}
|
|
1
|
+
{"version":3,"file":"SegmentedControl.mjs","sources":["../../../src/components/SegmentedControl/SegmentedControl.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { cn } from \"../../utils/cn\";\nimport { getRootClassName, getSquaredSegmentClassName } from \"./segmentedControlClassNames\";\n\n/** Height of the segmented control in pixels. */\nexport type SegmentedControlSize = \"32\" | \"40\" | \"48\";\n\n/**\n * Segment layout.\n * - `\"hug\"`: each segment is sized to its content.\n * - `\"fill\"`: the control spans the full width of its container and each segment grows equally.\n */\nexport type SegmentedControlVariant = \"hug\" | \"fill\";\n\n/**\n * Visual style of the control.\n * - `\"pill\"`: the container has a muted background and the selected segment shows a filled pill (default).\n * - `\"plain\"`: no container or selected-pill background; segments are bare content and selection is\n * communicated by color alone. Designed for icon-only toggles (e.g. a list/grid view switch).\n * - `\"brand\"`: like `\"pill\"`, but segments render their `icon` alongside the visible `label`.\n * A selected segment takes the neutral selected pill, except the one marked `ai`, which takes the\n * AI fill and gradient stroke. Designed for prominent toggles such as the Home/Agent navigation\n * switch.\n * - `\"squared\"`: a bordered track with squared corners, where the selected segment is a raised\n * surface rather than a filled pill and unselected labels are muted. Designed for quiet\n * in-page controls that sit beside buttons of the same height, such as a date-range picker.\n */\nexport type SegmentedControlAppearance = \"pill\" | \"plain\" | \"brand\" | \"squared\";\n\n/** Describes one selectable segment. */\nexport interface SegmentedControlOption {\n /**\n * Display label for the segment. In `pill`/`plain`/`squared` appearances, when `icon` is provided the\n * segment renders icon-only and this value becomes its accessible name (applied as `aria-label`)\n * instead of visible text. In the `brand` appearance the label is always rendered as visible text\n * (alongside the icon when present).\n */\n label: string;\n /** Value identifier returned via `onChange`. */\n value: string;\n /**\n * Icon to render for the segment. In `pill`/`plain`/`squared` appearances the segment renders icon-only and\n * `label` becomes required as its accessible name (applied as `aria-label`, no visible text). In\n * the `brand` appearance the icon renders alongside the visible `label`. Required for every option\n * when `collapsible` is set unless a `collapsedIcon` is given, since the collapsed control\n * otherwise shows the selected option's icon alone.\n */\n icon?: React.ReactNode;\n /**\n * Marks this segment as the AI one, so that when it is selected it takes the AI treatment — the\n * translucent `Buttons/AI/Default` fill and the `Buttons/AI` gradient stroke — instead of the\n * neutral selected pill every other segment gets.\n *\n * Set it on the Agent side of a Manage/Agent switch. Without it the AI treatment would follow\n * whichever segment happened to be selected, putting a brand stroke on segments that should not\n * carry one. Only meaningful in the `brand` appearance, and only while the control is expanded —\n * collapsed, the toggle stays the neutral switch affordance whichever option is selected.\n * @default false\n */\n ai?: boolean;\n}\n\nexport interface SegmentedControlProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n /** Height of the control in pixels. @default \"32\" */\n size?: SegmentedControlSize;\n /** Segment layout. @default \"hug\" */\n variant?: SegmentedControlVariant;\n /** Visual style of the control. @default \"pill\" */\n appearance?: SegmentedControlAppearance;\n /** The selectable segments. Designed for two or three mutually exclusive options. */\n options: SegmentedControlOption[];\n /** Currently selected value (controlled). */\n value?: string;\n /** Initially selected value (uncontrolled). Defaults to the first option. */\n defaultValue?: string;\n /** Callback fired when the selected value changes. */\n onChange?: (value: string) => void;\n /** Whether the control is disabled. @default false */\n disabled?: boolean;\n /**\n * When `true`, the control automatically collapses to a single icon-only toggle whenever its\n * container is too narrow to show every segment side by side, and expands again when the space\n * returns. Collapsed, it shows `collapsedIcon` when given and otherwise the currently-selected\n * option's `icon`; clicking it (or pressing Enter/Space) advances to the next option, wrapping\n * around from the last back to the first.\n *\n * Only supported for the icon-bearing `\"plain\"` and `\"brand\"` appearances, and every option must\n * define an `icon` unless `collapsedIcon` is given (there is nothing to show otherwise). Ignored,\n * with a dev-time warning, when those conditions are not met. @default false\n */\n collapsible?: boolean;\n /**\n * Single glyph for the collapsed toggle, replacing the selected option's icon. Use it when the\n * collapsed control should read as one switch affordance rather than as a preview of the current\n * selection — the collapsed navigation rail shows a repeat glyph this way. The button still\n * announces the selected option as its accessible name, so the current state stays available to\n * assistive tech. Only meaningful alongside `collapsible`.\n */\n collapsedIcon?: React.ReactNode;\n}\n\n/** Padding and typography per size. Combined with the container's `p-1` these hit the 32/40/48px heights. */\nconst sizeClasses: Record<SegmentedControlSize, string> = {\n \"32\": \"px-2 py-1 typography-description-12px-semibold\",\n \"40\": \"px-3 py-1.75 typography-body-small-14px-semibold\",\n \"48\": \"px-4 py-2 typography-body-default-16px-semibold\",\n};\n\nfunction warnMissingAccessibleName(ariaLabel?: string, ariaLabelledBy?: string) {\n if (process.env.NODE_ENV !== \"production\") {\n if (!ariaLabel && !ariaLabelledBy) {\n console.warn(\n \"SegmentedControl: no accessible name provided. Pass an `aria-label` or `aria-labelledby` prop.\",\n );\n }\n }\n}\n\nfunction warnMissingOptionAccessibleName(options: SegmentedControlOption[]) {\n if (process.env.NODE_ENV !== \"production\") {\n for (const option of options) {\n if (option.icon && !option.label?.trim()) {\n console.warn(\n `SegmentedControl: icon-only segment \"${option.value}\" is missing a non-empty \\`label\\` to use as its accessible name.`,\n );\n }\n }\n }\n}\n\nfunction warnUnsupportedCollapsible(\n appearance: SegmentedControlAppearance,\n options: SegmentedControlOption[],\n hasCollapsedIcon: boolean,\n) {\n if (process.env.NODE_ENV !== \"production\") {\n if (appearance === \"pill\" || appearance === \"squared\") {\n console.warn(\n 'SegmentedControl: `collapsible` is only supported for the \"plain\" and \"brand\" appearances; ignoring it.',\n );\n } else if (!hasCollapsedIcon && !options.every((option) => option.icon)) {\n console.warn(\n \"SegmentedControl: `collapsible` requires every option to define an `icon`, or a `collapsedIcon` to show instead; ignoring it.\",\n );\n }\n }\n}\n\nfunction getSegmentClassName({\n appearance,\n size,\n variant,\n isSelected,\n isAiSegment,\n disabled,\n}: {\n appearance: SegmentedControlAppearance;\n size: SegmentedControlSize;\n variant: SegmentedControlVariant;\n isSelected: boolean;\n isAiSegment: boolean;\n disabled: boolean;\n}) {\n if (appearance === \"squared\") {\n return getSquaredSegmentClassName({ size, variant, isSelected, disabled });\n }\n return cn(\n \"relative inline-flex min-w-0 cursor-pointer items-center justify-center rounded-full\",\n \"motion-safe:transition-colors motion-safe:duration-150 motion-safe:ease-in-out\",\n \"focus-visible:shadow-focus-ring focus-visible:outline-none\",\n variant === \"fill\" ? \"flex-1\" : \"shrink-0\",\n appearance === \"plain\"\n ? cn(\n // Padding + negative margin enlarge the hit target without changing the\n // visual footprint, which must stay glyph-only to match the design.\n \"-m-1 p-1\",\n isSelected ? \"text-icons-primary\" : \"text-icons-tertiary hover:text-icons-primary\",\n )\n : cn(\n sizeClasses[size],\n // brand renders icon + label together, so space them.\n appearance === \"brand\" && \"gap-1.5\",\n isSelected\n ? appearance === \"brand\" && isAiSegment\n ? // The AI segment: translucent AI fill plus the AI gradient stroke. Scoped to the\n // segment marked `ai` rather than to whatever is selected, so a brand stroke never\n // lands on a segment that should read as plain.\n \"fv-ai-stroke bg-buttons-ai-default text-content-primary\"\n : // Every other selected segment, `brand` included, takes the neutral pill. Its\n // tokens invert with the theme, so this is a white pill on dark and a black one on\n // light.\n \"bg-buttons-primary-default text-content-primary-inverted shadow-sm\"\n : \"text-content-primary hover:bg-buttons-switch-hover\",\n ),\n disabled && \"pointer-events-none\",\n );\n}\n\n/**\n * Footprint of the collapsed toggle per size. A fixed square keeps the collapsed control the same\n * height as the expanded one, whose height comes from the segment padding plus the container's\n * `p-1` — padding the collapsed button alone would fall short of it by that container padding.\n */\nconst collapsedSizeClasses: Record<SegmentedControlSize, string> = {\n \"32\": \"size-8\",\n \"40\": \"size-10\",\n \"48\": \"size-12\",\n};\n\n/**\n * Classes for the single button shown while the control is collapsed. Unlike an expanded segment\n * (which carries asymmetric `px`/`py` padding and so reads slightly wider than tall), the collapsed\n * toggle is forced to a square via a fixed footprint and `aspect-square`, so with `rounded-full` the\n * icon-only control reads as a round button.\n */\nfunction getCollapsedButtonClassName({\n appearance,\n size,\n disabled,\n}: {\n appearance: SegmentedControlAppearance;\n size: SegmentedControlSize;\n disabled: boolean;\n}) {\n return cn(\n \"relative inline-flex aspect-square shrink-0 cursor-pointer items-center justify-center rounded-full\",\n \"motion-safe:transition-colors motion-safe:duration-150 motion-safe:ease-in-out\",\n \"focus-visible:shadow-focus-ring focus-visible:outline-none\",\n appearance === \"plain\"\n ? // Glyph-only: no pill, just the selected icon colour. The negative margin keeps the visual\n // footprint icon-sized while the padding enlarges the hit target.\n \"-m-1 p-1 text-icons-primary\"\n : cn(\n collapsedSizeClasses[size],\n // Neutral circle rather than the brand pill: collapsed, the control is a switch\n // affordance, not a preview of the selected segment.\n \"bg-surface-secondary text-icons-primary hover:bg-buttons-switch-hover\",\n ),\n disabled && \"pointer-events-none\",\n );\n}\n\n/**\n * Watches `rootRef`'s available width against the natural width of the fully expanded control\n * (measured from `measureRef`, an off-screen replica) and returns whether the control should\n * collapse. Comparing available space to a stable off-screen measurement — rather than to the\n * live control, which shrinks when it collapses — avoids the expand/collapse oscillation that a\n * self-referential measurement would cause. Returns `false` until both widths are measurable\n * (SSR, jsdom, or a zero-width layout), so the control renders expanded by default.\n */\nfunction useAutoCollapse(\n rootRef: React.RefObject<HTMLDivElement | null>,\n measureRef: React.RefObject<HTMLDivElement | null>,\n enabled: boolean,\n): boolean {\n const [collapsed, setCollapsed] = React.useState(false);\n\n React.useEffect(() => {\n if (!enabled) {\n setCollapsed(false);\n return;\n }\n const root = rootRef.current;\n const measure = measureRef.current;\n const parent = root?.parentElement;\n if (!root || !measure || !parent) return;\n\n const evaluate = () => {\n const available = parent.getBoundingClientRect().width;\n const required = measure.getBoundingClientRect().width;\n // Both unmeasured (SSR/jsdom/zero-width): leave the current state untouched.\n if (available <= 0 || required <= 0) return;\n setCollapsed(required > available);\n };\n\n evaluate();\n\n const observer = new ResizeObserver(evaluate);\n observer.observe(parent);\n observer.observe(measure);\n return () => observer.disconnect();\n }, [enabled, rootRef, measureRef]);\n\n return enabled && collapsed;\n}\n\n/**\n * A compact selector for choosing between two or three mutually exclusive\n * options where the choice affects the content immediately below it. Use\n * instead of tabs when the options are more like settings or filters than\n * navigation, such as toggling a list/grid view or a monthly/annual price.\n *\n * Rendered as a `radiogroup` with roving-tabindex keyboard navigation. Supports\n * both controlled and uncontrolled usage. With `collapsible`, the icon-bearing\n * `plain`/`brand` appearances shrink to a single cycling icon toggle when space\n * is tight (rendered as a `group` containing one button).\n *\n * @example\n * ```tsx\n * <SegmentedControl\n * options={[\n * { label: \"Net\", value: \"net\" },\n * { label: \"Gross\", value: \"gross\" },\n * ]}\n * value={amount}\n * onChange={setAmount}\n * aria-label=\"Amount type\"\n * />\n * ```\n *\n * @example Icon-only segments (e.g. a list/grid view toggle)\n * ```tsx\n * <SegmentedControl\n * appearance=\"plain\"\n * options={[\n * { label: \"List view\", value: \"list\", icon: <ListViewIcon size={16} /> },\n * { label: \"Grid view\", value: \"grid\", icon: <GridViewIcon size={16} /> },\n * ]}\n * value={view}\n * onChange={setView}\n * aria-label=\"View\"\n * />\n * ```\n *\n * @example Icon + label, with the AI treatment on the Agent segment (the Home/Agent nav switch)\n * ```tsx\n * <SegmentedControl\n * appearance=\"brand\"\n * options={[\n * { label: \"Home\", value: \"home\", icon: <HomeIcon size={16} /> },\n * { label: \"Agent\", value: \"agent\", icon: <AIIcon size={16} />, ai: true },\n * ]}\n * value={mode}\n * onChange={setMode}\n * aria-label=\"Navigation mode\"\n * />\n * ```\n *\n * @example Collapses to a cycling icon toggle when the container is too narrow\n * ```tsx\n * <SegmentedControl\n * appearance=\"plain\"\n * collapsible\n * options={[\n * { label: \"List view\", value: \"list\", icon: <ListViewIcon size={16} /> },\n * { label: \"Grid view\", value: \"grid\", icon: <GridViewIcon size={16} /> },\n * ]}\n * value={view}\n * onChange={setView}\n * aria-label=\"View\"\n * />\n * ```\n *\n * @example A quiet squared control beside buttons of the same height (e.g. a date-range picker)\n * ```tsx\n * <SegmentedControl\n * appearance=\"squared\"\n * options={[\n * { label: \"7 days\", value: \"7\" },\n * { label: \"30 days\", value: \"30\" },\n * { label: \"90 days\", value: \"90\" },\n * ]}\n * value={range}\n * onChange={setRange}\n * aria-label=\"Date range\"\n * />\n * ```\n *\n * @example A collapsed toggle that shows one switch glyph instead of the selected icon\n * ```tsx\n * <SegmentedControl\n * appearance=\"brand\"\n * collapsible\n * collapsedIcon={<RepeatIcon size={16} />}\n * options={[\n * { label: \"Home\", value: \"home\", icon: <HomeIcon size={16} /> },\n * { label: \"Agent\", value: \"agent\", icon: <AIIcon size={16} filled />, ai: true },\n * ]}\n * value={mode}\n * onChange={setMode}\n * aria-label=\"Navigation mode\"\n * />\n * ```\n */\nexport const SegmentedControl = React.forwardRef<HTMLDivElement, SegmentedControlProps>(\n (\n {\n className,\n size = \"32\",\n variant = \"hug\",\n appearance = \"pill\",\n options,\n value: controlledValue,\n defaultValue,\n onChange,\n disabled = false,\n collapsible = false,\n collapsedIcon,\n ...props\n },\n ref,\n ) => {\n warnMissingAccessibleName(props[\"aria-label\"], props[\"aria-labelledby\"]);\n warnMissingOptionAccessibleName(options);\n if (collapsible) warnUnsupportedCollapsible(appearance, options, collapsedIcon !== undefined);\n\n // Tracks selection for uncontrolled usage; ignored when `value` prop is provided\n const [internalValue, setInternalValue] = React.useState(defaultValue ?? options[0]?.value);\n const isControlled = controlledValue !== undefined;\n const currentValue = isControlled ? controlledValue : internalValue;\n const anySelected = options.some((o) => o.value === currentValue);\n const buttonRefs = React.useRef<(HTMLButtonElement | null)[]>([]);\n\n // Collapsing only makes sense for icon-bearing appearances where there is a glyph to show:\n // either a `collapsedIcon` for every state, or an icon on every option. Anything else falls\n // back to the normal expanded control (a dev warning fires above).\n const canCollapse =\n collapsible &&\n (appearance === \"plain\" || appearance === \"brand\") &&\n (collapsedIcon !== undefined || options.every((option) => option.icon));\n\n const rootRef = React.useRef<HTMLDivElement | null>(null);\n const measureRef = React.useRef<HTMLDivElement | null>(null);\n const setRootRef = React.useCallback(\n (node: HTMLDivElement | null) => {\n rootRef.current = node;\n if (typeof ref === \"function\") ref(node);\n else if (ref) ref.current = node;\n },\n [ref],\n );\n const isCollapsed = useAutoCollapse(rootRef, measureRef, canCollapse);\n\n const handleSelect = (optionValue: string) => {\n if (disabled || optionValue === currentValue) return;\n if (!isControlled) {\n setInternalValue(optionValue);\n }\n onChange?.(optionValue);\n };\n\n const handleKeyDown = (e: React.KeyboardEvent, index: number) => {\n const nextIndex =\n e.key === \"ArrowRight\" || e.key === \"ArrowDown\"\n ? (index + 1) % options.length\n : e.key === \"ArrowLeft\" || e.key === \"ArrowUp\"\n ? (index - 1 + options.length) % options.length\n : null;\n if (nextIndex === null) return;\n e.preventDefault();\n const nextOption = options[nextIndex] as SegmentedControlOption;\n handleSelect(nextOption.value);\n buttonRefs.current[nextIndex]?.focus();\n };\n\n const renderSegment = (option: SegmentedControlOption, index: number) => {\n const isSelected = currentValue === option.value;\n // `brand` shows the label as visible text (with the icon); `pill`/`plain` render\n // icon-only when an icon is given, falling back to the label otherwise.\n const showLabelText = appearance === \"brand\" || !option.icon;\n return (\n // biome-ignore lint/a11y/useSemanticElements: native radio inputs only allow Tab-focus on the checked item; buttons with roving tabindex give full keyboard navigation\n <button\n key={option.value}\n ref={(el) => {\n buttonRefs.current[index] = el;\n }}\n type=\"button\"\n role=\"radio\"\n aria-checked={isSelected}\n tabIndex={isSelected || (!anySelected && index === 0) ? 0 : -1}\n disabled={disabled}\n aria-label={showLabelText ? undefined : option.label}\n onClick={() => handleSelect(option.value)}\n onKeyDown={(e) => handleKeyDown(e, index)}\n className={getSegmentClassName({\n appearance,\n size,\n variant,\n isSelected,\n isAiSegment: option.ai === true,\n disabled,\n })}\n >\n {option.icon && (\n <span className=\"flex shrink-0 items-center justify-center\" aria-hidden=\"true\">\n {option.icon}\n </span>\n )}\n {showLabelText && <span className=\"min-w-0 truncate\">{option.label}</span>}\n </button>\n );\n };\n\n // Off-screen replica of one segment used only to size the expanded control. Rendered as an\n // inert (disabled + `aria-hidden`) button with no `radio` semantics so it stays out of the\n // accessibility tree and tab order, and always with hug sizing so the replica reflects the\n // natural (uncompressed) width even under a `fill` layout.\n const renderMeasureSegment = (option: SegmentedControlOption) => {\n const showLabelText = appearance === \"brand\" || !option.icon;\n return (\n <button\n key={option.value}\n type=\"button\"\n disabled\n aria-hidden=\"true\"\n tabIndex={-1}\n className={getSegmentClassName({\n appearance,\n size,\n variant: \"hug\",\n isSelected: currentValue === option.value,\n isAiSegment: option.ai === true,\n disabled,\n })}\n >\n {option.icon && (\n <span className=\"flex shrink-0 items-center justify-center\" aria-hidden=\"true\">\n {option.icon}\n </span>\n )}\n {showLabelText && <span className=\"min-w-0 truncate\">{option.label}</span>}\n </button>\n );\n };\n\n // Collapsed: a single button showing the selected option's icon that cycles to the next\n // option on activation, wrapping from the last option back to the first.\n const selectedIndex = options.findIndex((option) => option.value === currentValue);\n const selectedOption = options[selectedIndex] ?? options[0];\n const handleCycle = () => {\n if (disabled || options.length === 0) return;\n const from = selectedIndex < 0 ? 0 : selectedIndex;\n const nextOption = options[(from + 1) % options.length] as SegmentedControlOption;\n handleSelect(nextOption.value);\n };\n\n return (\n <div\n ref={setRootRef}\n role={isCollapsed ? \"group\" : \"radiogroup\"}\n className={cn(\n getRootClassName({ appearance, variant, size, isCollapsed, disabled }),\n className,\n )}\n {...props}\n >\n {isCollapsed && selectedOption ? (\n <button\n type=\"button\"\n disabled={disabled}\n aria-label={selectedOption.label}\n onClick={handleCycle}\n className={getCollapsedButtonClassName({ appearance, size, disabled })}\n >\n {(collapsedIcon ?? selectedOption.icon) && (\n <span className=\"flex shrink-0 items-center justify-center\" aria-hidden=\"true\">\n {collapsedIcon ?? selectedOption.icon}\n </span>\n )}\n </button>\n ) : (\n options.map((option, index) => renderSegment(option, index))\n )}\n\n {canCollapse && (\n // Off-screen replica of the fully expanded control. Kept in the DOM (out of flow, hidden\n // from AT and tab order) so its natural width is always measurable, whether the visible\n // control is currently expanded or collapsed.\n <div\n ref={measureRef}\n aria-hidden=\"true\"\n className={cn(\n \"pointer-events-none invisible absolute top-0 left-0 -z-10 flex items-center whitespace-nowrap rounded-full\",\n appearance === \"plain\" ? \"gap-2\" : \"bg-surface-tertiary p-1\",\n )}\n >\n {options.map((option) => renderMeasureSegment(option))}\n </div>\n )}\n </div>\n );\n },\n);\n\nSegmentedControl.displayName = \"SegmentedControl\";\n"],"names":[],"mappings":";;;;;AAuGA,MAAM,cAAoD;AAAA,EACxD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,MAAM;AACR;AAEA,SAAS,0BAA0B,WAAoB,gBAAyB;AAC9E,MAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,QAAI,CAAC,aAAa,CAAC,gBAAgB;AACjC,cAAQ;AAAA,QACN;AAAA,MAAA;AAAA,IAEJ;AAAA,EACF;AACF;AAEA,SAAS,gCAAgC,SAAmC;AAC1E,MAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,eAAW,UAAU,SAAS;AAC5B,UAAI,OAAO,QAAQ,CAAC,OAAO,OAAO,QAAQ;AACxC,gBAAQ;AAAA,UACN,wCAAwC,OAAO,KAAK;AAAA,QAAA;AAAA,MAExD;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,2BACP,YACA,SACA,kBACA;AACA,MAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,QAAI,eAAe,UAAU,eAAe,WAAW;AACrD,cAAQ;AAAA,QACN;AAAA,MAAA;AAAA,IAEJ,WAAW,CAAC,oBAAoB,CAAC,QAAQ,MAAM,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BAAa,WAAW,KAAK;AAAA,IAC/B;AAEA,WACE;AAAA,MAAC;AAAA,MAAA;AAAA,QACC,KAAK;AAAA,QACL,MAAM,cAAc,UAAU;AAAA,QAC9B,WAAW;AAAA,UACT,iBAAiB,EAAE,YAAY,SAAS,MAAM,aAAa,UAAU;AAAA,UACrE;AAAA,QAAA;AAAA,QAED,GAAG;AAAA,QAEH,UAAA;AAAA,UAAA,eAAe,iBACd;AAAA,YAAC;AAAA,YAAA;AAAA,cACC,MAAK;AAAA,cACL;AAAA,cACA,cAAY,eAAe;AAAA,cAC3B,SAAS;AAAA,cACT,WAAW,4BAA4B,EAAE,YAAY,MAAM,UAAU;AAAA,cAEnE,WAAA,iBAAiB,eAAe,SAChC,oBAAC,QAAA,EAAK,WAAU,6CAA4C,eAAY,QACrE,UAAA,iBAAiB,eAAe,KAAA,CACnC;AAAA,YAAA;AAAA,UAAA,IAIJ,QAAQ,IAAI,CAAC,QAAQ,UAAU,cAAc,QAAQ,KAAK,CAAC;AAAA,UAG5D;AAAA;AAAA;AAAA,UAIC;AAAA,YAAC;AAAA,YAAA;AAAA,cACC,KAAK;AAAA,cACL,eAAY;AAAA,cACZ,WAAW;AAAA,gBACT;AAAA,gBACA,eAAe,UAAU,UAAU;AAAA,cAAA;AAAA,cAGpC,kBAAQ,IAAI,CAAC,WAAW,qBAAqB,MAAM,CAAC;AAAA,YAAA;AAAA,UAAA;AAAA,QACvD;AAAA,MAAA;AAAA,IAAA;AAAA,EAIR;AACF;AAEA,iBAAiB,cAAc;"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "../../utils/cn.mjs";
|
|
3
|
+
const squaredSizeClasses = {
|
|
4
|
+
"32": { track: "h-8", segment: "px-3 typography-description-12px-semibold" },
|
|
5
|
+
"40": { track: "h-10", segment: "px-4 typography-body-small-14px-semibold" },
|
|
6
|
+
"48": { track: "h-12", segment: "px-5 typography-body-default-16px-semibold" }
|
|
7
|
+
};
|
|
8
|
+
function getSquaredSegmentClassName({
|
|
9
|
+
size,
|
|
10
|
+
variant,
|
|
11
|
+
isSelected,
|
|
12
|
+
disabled
|
|
13
|
+
}) {
|
|
14
|
+
return cn(
|
|
15
|
+
"relative inline-flex h-full min-w-0 cursor-pointer items-center justify-center rounded-2xs",
|
|
16
|
+
"motion-safe:transition-colors motion-safe:duration-150 motion-safe:ease-in-out",
|
|
17
|
+
"focus-visible:shadow-focus-ring focus-visible:outline-hidden",
|
|
18
|
+
variant === "fill" ? "flex-1" : "shrink-0",
|
|
19
|
+
squaredSizeClasses[size].segment,
|
|
20
|
+
"font-medium tabular-nums",
|
|
21
|
+
isSelected ? "bg-background-primary text-content-primary shadow-sm ring-1 ring-border-primary dark:bg-surface-secondary" : "text-content-secondary hover:bg-buttons-switch-hover hover:text-content-primary",
|
|
22
|
+
disabled && "pointer-events-none"
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
function getTrackClassName(appearance, size) {
|
|
26
|
+
if (appearance === "plain") return "gap-2";
|
|
27
|
+
if (appearance === "squared") {
|
|
28
|
+
return cn(
|
|
29
|
+
"gap-0.5 border border-border-primary bg-surface-secondary p-0.5 dark:bg-background-secondary",
|
|
30
|
+
squaredSizeClasses[size].track
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
return "bg-surface-tertiary p-1";
|
|
34
|
+
}
|
|
35
|
+
function getRootClassName({
|
|
36
|
+
appearance,
|
|
37
|
+
variant,
|
|
38
|
+
size,
|
|
39
|
+
isCollapsed,
|
|
40
|
+
disabled
|
|
41
|
+
}) {
|
|
42
|
+
return cn(
|
|
43
|
+
"relative items-center",
|
|
44
|
+
appearance === "squared" ? "rounded-xs" : "rounded-full",
|
|
45
|
+
variant === "fill" ? "flex w-full" : "inline-flex",
|
|
46
|
+
isCollapsed ? "justify-center" : getTrackClassName(appearance, size),
|
|
47
|
+
disabled && "cursor-not-allowed opacity-50"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
getRootClassName,
|
|
52
|
+
getSquaredSegmentClassName
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=segmentedControlClassNames.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"segmentedControlClassNames.mjs","sources":["../../../src/components/SegmentedControl/segmentedControlClassNames.ts"],"sourcesContent":["import { cn } from \"../../utils/cn\";\nimport type {\n SegmentedControlAppearance,\n SegmentedControlSize,\n SegmentedControlVariant,\n} from \"./SegmentedControl\";\n\n/**\n * Track height, segment padding and typography per size for the `squared` appearance. The track\n * has a fixed height and its segments fill it, so the control stays level with a button of the\n * same `size` whatever the label's line height. Segments add medium weight and tabular figures on\n * top of this: the labels are usually short numeric ranges, and a segment must not change width\n * when its digits do.\n */\nconst squaredSizeClasses: Record<SegmentedControlSize, { track: string; segment: string }> = {\n \"32\": { track: \"h-8\", segment: \"px-3 typography-description-12px-semibold\" },\n \"40\": { track: \"h-10\", segment: \"px-4 typography-body-small-14px-semibold\" },\n \"48\": { track: \"h-12\", segment: \"px-5 typography-body-default-16px-semibold\" },\n};\n\n/**\n * Classes for one segment of the `squared` appearance: a squared button that fills the track,\n * raised when selected and muted otherwise. Its focus ring is a shadow, so the outline is hidden\n * rather than removed: forced-colors modes drop shadows and draw the outline instead.\n */\nexport function getSquaredSegmentClassName({\n size,\n variant,\n isSelected,\n disabled,\n}: {\n size: SegmentedControlSize;\n variant: SegmentedControlVariant;\n isSelected: boolean;\n disabled: boolean;\n}) {\n return cn(\n \"relative inline-flex h-full min-w-0 cursor-pointer items-center justify-center rounded-2xs\",\n \"motion-safe:transition-colors motion-safe:duration-150 motion-safe:ease-in-out\",\n \"focus-visible:shadow-focus-ring focus-visible:outline-hidden\",\n variant === \"fill\" ? \"flex-1\" : \"shrink-0\",\n squaredSizeClasses[size].segment,\n \"font-medium tabular-nums\",\n isSelected\n ? \"bg-background-primary text-content-primary shadow-sm ring-1 ring-border-primary dark:bg-surface-secondary\"\n : \"text-content-secondary hover:bg-buttons-switch-hover hover:text-content-primary\",\n disabled && \"pointer-events-none\",\n );\n}\n\n/**\n * The expanded container's chrome per appearance: a gap between bare glyphs for `plain`, the\n * bordered squared track for `squared`, and the muted pill track for the rest.\n */\nfunction getTrackClassName(appearance: SegmentedControlAppearance, size: SegmentedControlSize) {\n if (appearance === \"plain\") return \"gap-2\";\n if (appearance === \"squared\") {\n return cn(\n \"gap-0.5 border border-border-primary bg-surface-secondary p-0.5 dark:bg-background-secondary\",\n squaredSizeClasses[size].track,\n );\n }\n return \"bg-surface-tertiary p-1\";\n}\n\n/**\n * Classes for the control's root. Collapsed, the toggle itself carries the surface, so the root\n * drops its own chrome and centres the button instead of pinning it to the start of a full-width\n * row: in a narrow rail that offset is the whole width of the column.\n */\nexport function getRootClassName({\n appearance,\n variant,\n size,\n isCollapsed,\n disabled,\n}: {\n appearance: SegmentedControlAppearance;\n variant: SegmentedControlVariant;\n size: SegmentedControlSize;\n isCollapsed: boolean;\n disabled: boolean;\n}) {\n return cn(\n \"relative items-center\",\n appearance === \"squared\" ? \"rounded-xs\" : \"rounded-full\",\n variant === \"fill\" ? \"flex w-full\" : \"inline-flex\",\n isCollapsed ? \"justify-center\" : getTrackClassName(appearance, size),\n disabled && \"cursor-not-allowed opacity-50\",\n );\n}\n"],"names":[],"mappings":";;AAcA,MAAM,qBAAuF;AAAA,EAC3F,MAAM,EAAE,OAAO,OAAO,SAAS,4CAAA;AAAA,EAC/B,MAAM,EAAE,OAAO,QAAQ,SAAS,2CAAA;AAAA,EAChC,MAAM,EAAE,OAAO,QAAQ,SAAS,6CAAA;AAClC;AAOO,SAAS,2BAA2B;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAKG;AACD,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,YAAY,SAAS,WAAW;AAAA,IAChC,mBAAmB,IAAI,EAAE;AAAA,IACzB;AAAA,IACA,aACI,8GACA;AAAA,IACJ,YAAY;AAAA,EAAA;AAEhB;AAMA,SAAS,kBAAkB,YAAwC,MAA4B;AAC7F,MAAI,eAAe,QAAS,QAAO;AACnC,MAAI,eAAe,WAAW;AAC5B,WAAO;AAAA,MACL;AAAA,MACA,mBAAmB,IAAI,EAAE;AAAA,IAAA;AAAA,EAE7B;AACA,SAAO;AACT;AAOO,SAAS,iBAAiB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAMG;AACD,SAAO;AAAA,IACL;AAAA,IACA,eAAe,YAAY,eAAe;AAAA,IAC1C,YAAY,SAAS,gBAAgB;AAAA,IACrC,cAAc,mBAAmB,kBAAkB,YAAY,IAAI;AAAA,IACnE,YAAY;AAAA,EAAA;AAEhB;"}
|
|
@@ -7,6 +7,7 @@ import { CloseIcon } from "../Icons/CloseIcon.mjs";
|
|
|
7
7
|
import { SearchIcon } from "../Icons/SearchIcon.mjs";
|
|
8
8
|
import { TranscationArrowIcon } from "../Icons/TranscationArrowIcon.mjs";
|
|
9
9
|
import { TooltipProvider, Tooltip, TooltipTrigger, TooltipContent } from "../Tooltip/Tooltip.mjs";
|
|
10
|
+
import { openTableRowLink } from "./TableRowLink.mjs";
|
|
10
11
|
const TableSizeContext = React.createContext("md");
|
|
11
12
|
function useTableSize() {
|
|
12
13
|
return React.useContext(TableSizeContext);
|
|
@@ -242,8 +243,35 @@ const TableFooter = React.forwardRef(
|
|
|
242
243
|
);
|
|
243
244
|
TableFooter.displayName = "TableFooter";
|
|
244
245
|
const TableRow = React.forwardRef(
|
|
245
|
-
({ className, ...props }, ref) => {
|
|
246
|
-
|
|
246
|
+
({ className, interactive = false, onClick, ...props }, ref) => {
|
|
247
|
+
const openingRowLink = React.useRef(false);
|
|
248
|
+
return /* @__PURE__ */ jsx(
|
|
249
|
+
"tr",
|
|
250
|
+
{
|
|
251
|
+
ref,
|
|
252
|
+
className: cn(
|
|
253
|
+
"[&:has([data-table-media])>td]:h-14",
|
|
254
|
+
interactive && "cursor-pointer transition-colors duration-200 ease-in-out hover:bg-background-secondary has-[:focus-visible]:bg-background-secondary motion-reduce:transition-none",
|
|
255
|
+
className
|
|
256
|
+
),
|
|
257
|
+
onClick: (event) => {
|
|
258
|
+
if (openingRowLink.current) {
|
|
259
|
+
event.stopPropagation();
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
onClick?.(event);
|
|
263
|
+
if (interactive && !event.defaultPrevented) {
|
|
264
|
+
openingRowLink.current = true;
|
|
265
|
+
try {
|
|
266
|
+
openTableRowLink(event);
|
|
267
|
+
} finally {
|
|
268
|
+
openingRowLink.current = false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
...props
|
|
273
|
+
}
|
|
274
|
+
);
|
|
247
275
|
}
|
|
248
276
|
);
|
|
249
277
|
TableRow.displayName = "TableRow";
|
|
@@ -277,6 +305,7 @@ const CELL_MIN_HEIGHT = {
|
|
|
277
305
|
md: "h-10",
|
|
278
306
|
default: "h-10",
|
|
279
307
|
condensed: "h-10",
|
|
308
|
+
comfortable: "h-12",
|
|
280
309
|
lg: "h-14"
|
|
281
310
|
};
|
|
282
311
|
const CELL_VARIANT_CLASSES = {
|