@enablo/swirlcn-ui 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -2
- package/dist/chunk-3DJWDNQL.js +77 -0
- package/dist/chunk-7XUCETA6.js +447 -0
- package/dist/chunk-KZESIYUN.js +18 -0
- package/dist/flip.d.ts +98 -0
- package/dist/flip.js +158 -0
- package/dist/fonts/Inter-italic.var.woff2 +0 -0
- package/dist/fonts/Inter.var.woff2 +0 -0
- package/dist/fonts/OFL.txt +96 -0
- package/dist/index.d.ts +2459 -0
- package/dist/index.js +8439 -0
- package/dist/server.d.ts +144 -0
- package/dist/server.js +88 -0
- package/dist/spacing-BQaEPbUv.d.ts +59 -0
- package/dist/theme.css +146 -0
- package/package.json +84 -3
- package/src/components/accordion.tsx +102 -0
- package/src/components/action-list.tsx +294 -0
- package/src/components/alert-dialog.tsx +155 -0
- package/src/components/autocomplete.tsx +313 -0
- package/src/components/avatar.tsx +258 -0
- package/src/components/badge.tsx +100 -0
- package/src/components/banner.tsx +112 -0
- package/src/components/button-group.tsx +87 -0
- package/src/components/button.tsx +362 -0
- package/src/components/card.tsx +186 -0
- package/src/components/carousel.tsx +245 -0
- package/src/components/checkbox.tsx +125 -0
- package/src/components/chip.tsx +153 -0
- package/src/components/color-input.tsx +98 -0
- package/src/components/data-cell.tsx +230 -0
- package/src/components/date-input.tsx +223 -0
- package/src/components/date-picker.tsx +276 -0
- package/src/components/description-list.tsx +97 -0
- package/src/components/dialog.tsx +405 -0
- package/src/components/empty-state.tsx +63 -0
- package/src/components/file-chip.tsx +206 -0
- package/src/components/file-uploader.tsx +148 -0
- package/src/components/flip-app-header.tsx +104 -0
- package/src/components/flip-brand.tsx +77 -0
- package/src/components/flip-theme.tsx +95 -0
- package/src/components/form-control.tsx +397 -0
- package/src/components/form-group.tsx +26 -0
- package/src/components/heading.tsx +70 -0
- package/src/components/icon.tsx +119 -0
- package/src/components/image-grid.tsx +285 -0
- package/src/components/inline-notification.tsx +167 -0
- package/src/components/inline-status.tsx +100 -0
- package/src/components/input.tsx +390 -0
- package/src/components/label.tsx +28 -0
- package/src/components/lightbox.tsx +314 -0
- package/src/components/link.tsx +46 -0
- package/src/components/list.tsx +40 -0
- package/src/components/menu.tsx +530 -0
- package/src/components/option-list.tsx +582 -0
- package/src/components/pagination.tsx +336 -0
- package/src/components/popover.tsx +194 -0
- package/src/components/progress.tsx +158 -0
- package/src/components/radio-group.tsx +136 -0
- package/src/components/resource-list.tsx +686 -0
- package/src/components/search.tsx +127 -0
- package/src/components/select.tsx +456 -0
- package/src/components/separator.tsx +93 -0
- package/src/components/skeleton-box.tsx +63 -0
- package/src/components/skeleton-text.tsx +66 -0
- package/src/components/spinner.tsx +83 -0
- package/src/components/stack.tsx +86 -0
- package/src/components/status-indicator.tsx +31 -0
- package/src/components/switch.tsx +90 -0
- package/src/components/table.tsx +242 -0
- package/src/components/tabs.tsx +147 -0
- package/src/components/tag.tsx +169 -0
- package/src/components/text.tsx +126 -0
- package/src/components/textarea.tsx +130 -0
- package/src/components/theme-provider.tsx +115 -0
- package/src/components/thumbnail.tsx +250 -0
- package/src/components/time-input.tsx +123 -0
- package/src/components/toast.tsx +217 -0
- package/src/components/toggle-group.tsx +124 -0
- package/src/components/toolbar.tsx +105 -0
- package/src/components/tooltip.tsx +97 -0
- package/src/components/tree-navigation.tsx +194 -0
- package/src/components/tree-view.tsx +292 -0
- package/src/flip.ts +12 -0
- package/src/index.ts +317 -0
- package/src/lib/date-mask.ts +176 -0
- package/src/lib/field-context.ts +86 -0
- package/src/lib/flip-app-script.ts +38 -0
- package/src/lib/flip-host.ts +84 -0
- package/src/lib/flip-theme-css.ts +90 -0
- package/src/lib/focus.ts +18 -0
- package/src/lib/glyphs.ts +137 -0
- package/src/lib/spacing.ts +93 -0
- package/src/lib/theme-context.ts +27 -0
- package/src/lib/theme-script.ts +49 -0
- package/src/lib/use-mobile.ts +17 -0
- package/src/lib/utils.ts +28 -0
- package/src/server.ts +20 -0
- package/src/styles/fonts/Inter-italic.var.woff2 +0 -0
- package/src/styles/fonts/Inter.var.woff2 +0 -0
- package/src/styles/fonts/OFL.txt +96 -0
- package/src/styles/theme.css +146 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { ToggleGroup as ToggleGroupPrimitive } from "radix-ui";
|
|
3
|
+
import { cn } from "../lib/utils";
|
|
4
|
+
|
|
5
|
+
export type ToggleGroupProps = React.ComponentProps<typeof ToggleGroupPrimitive.Root> & {
|
|
6
|
+
/**
|
|
7
|
+
* Swirl's track: `flat` is sunken, `outline` adds a strong border, and
|
|
8
|
+
* `translucent` uses the raised surface for use over images or colour.
|
|
9
|
+
*/
|
|
10
|
+
variant?: "flat" | "outline" | "translucent";
|
|
11
|
+
/** Lift the group off the page with a shadow. */
|
|
12
|
+
elevated?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* With `type="single"`, let a click on the selected option clear the
|
|
15
|
+
* choice. Off by default: as in Swirl, an option stays selected until
|
|
16
|
+
* another is chosen.
|
|
17
|
+
*/
|
|
18
|
+
allowDeselect?: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type SingleRootProps = Extract<
|
|
22
|
+
React.ComponentProps<typeof ToggleGroupPrimitive.Root>,
|
|
23
|
+
{ type: "single" }
|
|
24
|
+
>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Swirl's toggle group: a segmented control whose chosen option lifts onto
|
|
28
|
+
* a white surface. Pass `type="single"` for Swirl's one-of-many behaviour,
|
|
29
|
+
* or `type="multiple"` to allow several. Arrow keys move between options.
|
|
30
|
+
*/
|
|
31
|
+
export function ToggleGroup({
|
|
32
|
+
className,
|
|
33
|
+
variant = "flat",
|
|
34
|
+
elevated = false,
|
|
35
|
+
allowDeselect = false,
|
|
36
|
+
...props
|
|
37
|
+
}: ToggleGroupProps) {
|
|
38
|
+
const single = props.type === "single";
|
|
39
|
+
const [uncontrolled, setUncontrolled] = React.useState(
|
|
40
|
+
single ? ((props as SingleRootProps).defaultValue ?? "") : "",
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const classes = cn(
|
|
44
|
+
"inline-flex items-center gap-1 rounded-[0.5rem] bg-surface-sunken p-0.5 transition-shadow duration-300",
|
|
45
|
+
// An inset ring, not an outline: Radix sets an inline `outline: none`
|
|
46
|
+
// on the group, which would hide Swirl's border.
|
|
47
|
+
variant === "outline" && "ring-1 ring-border-strong ring-inset",
|
|
48
|
+
variant === "translucent" && "bg-surface-raised",
|
|
49
|
+
elevated && "shadow-level-2",
|
|
50
|
+
className,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
if (single && !allowDeselect) {
|
|
54
|
+
// Radix clears a single group when its selected item is pressed again;
|
|
55
|
+
// ignore that empty value so a choice always stays made.
|
|
56
|
+
const { value, defaultValue: _defaultValue, onValueChange, ...rest } = props as SingleRootProps;
|
|
57
|
+
return (
|
|
58
|
+
<ToggleGroupPrimitive.Root
|
|
59
|
+
{...rest}
|
|
60
|
+
type="single"
|
|
61
|
+
className={classes}
|
|
62
|
+
value={value ?? uncontrolled}
|
|
63
|
+
onValueChange={(next) => {
|
|
64
|
+
if (!next) return;
|
|
65
|
+
if (value === undefined) setUncontrolled(next);
|
|
66
|
+
onValueChange?.(next);
|
|
67
|
+
}}
|
|
68
|
+
/>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return <ToggleGroupPrimitive.Root className={classes} {...props} />;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ToggleGroupItemProps
|
|
76
|
+
extends React.ComponentProps<typeof ToggleGroupPrimitive.Item> {
|
|
77
|
+
/** Leading icon, 24px or 20px with the desktop layout. */
|
|
78
|
+
icon?: React.ReactNode;
|
|
79
|
+
/** The option's name. Interchangeable with `children`. */
|
|
80
|
+
label?: string;
|
|
81
|
+
/** Show only the icon, keeping `label` as the accessible name. */
|
|
82
|
+
hideLabel?: boolean;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function ToggleGroupItem({
|
|
86
|
+
className,
|
|
87
|
+
icon,
|
|
88
|
+
label,
|
|
89
|
+
hideLabel = false,
|
|
90
|
+
children,
|
|
91
|
+
...props
|
|
92
|
+
}: ToggleGroupItemProps) {
|
|
93
|
+
const content = children ?? label;
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<ToggleGroupPrimitive.Item
|
|
97
|
+
aria-label={hideLabel ? label : undefined}
|
|
98
|
+
className={cn(
|
|
99
|
+
"group/toggle inline-flex cursor-pointer items-center justify-center gap-2 rounded-[0.375rem] border-none bg-transparent px-2 py-1.5",
|
|
100
|
+
"leading-6 font-medium whitespace-nowrap text-foreground no-underline",
|
|
101
|
+
"desktop:gap-1 desktop:text-sm desktop:leading-5",
|
|
102
|
+
"not-disabled:hover:bg-state-hovered not-disabled:active:bg-state-pressed",
|
|
103
|
+
"data-[state=on]:bg-surface data-[state=on]:text-highlight data-[state=on]:shadow-level-1",
|
|
104
|
+
"data-[state=on]:hover:bg-surface data-[state=on]:active:bg-surface",
|
|
105
|
+
"disabled:cursor-default disabled:text-disabled",
|
|
106
|
+
"outline-focus outline-offset-2 focus:not-focus-visible:outline-none",
|
|
107
|
+
className,
|
|
108
|
+
)}
|
|
109
|
+
{...props}
|
|
110
|
+
>
|
|
111
|
+
{icon && (
|
|
112
|
+
<span
|
|
113
|
+
className={cn(
|
|
114
|
+
"inline-flex size-6 shrink-0 text-icon-default desktop:size-5 [&>svg]:size-full",
|
|
115
|
+
"group-data-[state=on]/toggle:text-highlight",
|
|
116
|
+
)}
|
|
117
|
+
>
|
|
118
|
+
{icon}
|
|
119
|
+
</span>
|
|
120
|
+
)}
|
|
121
|
+
{!hideLabel && content && <span className="max-w-full truncate">{content}</span>}
|
|
122
|
+
</ToggleGroupPrimitive.Item>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { cn } from "../lib/utils";
|
|
3
|
+
import { gap, type SwirlSpacing } from "../lib/spacing";
|
|
4
|
+
|
|
5
|
+
/** The toolbar's controls: every button inside it, in document order. */
|
|
6
|
+
const controls = (toolbar: HTMLElement) =>
|
|
7
|
+
Array.from(toolbar.querySelectorAll<HTMLButtonElement>("button"));
|
|
8
|
+
|
|
9
|
+
export interface ToolbarProps extends React.ComponentProps<"div"> {
|
|
10
|
+
/** Accessible name of the toolbar. */
|
|
11
|
+
label?: string;
|
|
12
|
+
orientation?: "horizontal" | "vertical";
|
|
13
|
+
/** Gap between controls, in Swirl's px steps. Defaults to 8. */
|
|
14
|
+
spacing?: SwirlSpacing;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Swirl's toolbar: a row of buttons and chips that is one tab stop. Arrow
|
|
19
|
+
* keys move between the controls and wrap at the ends. Controls wrap onto
|
|
20
|
+
* more lines when the row runs out of room.
|
|
21
|
+
*/
|
|
22
|
+
export function Toolbar({
|
|
23
|
+
className,
|
|
24
|
+
label,
|
|
25
|
+
orientation = "horizontal",
|
|
26
|
+
spacing = "8",
|
|
27
|
+
onKeyDown,
|
|
28
|
+
onFocus,
|
|
29
|
+
children,
|
|
30
|
+
...props
|
|
31
|
+
}: ToolbarProps) {
|
|
32
|
+
const ref = React.useRef<HTMLDivElement>(null);
|
|
33
|
+
|
|
34
|
+
// Keep exactly one control in the tab order: the last one focused, or the
|
|
35
|
+
// first. Runs after every render and when the children change.
|
|
36
|
+
React.useEffect(() => {
|
|
37
|
+
const toolbar = ref.current;
|
|
38
|
+
if (!toolbar) return;
|
|
39
|
+
const apply = () => {
|
|
40
|
+
const items = controls(toolbar);
|
|
41
|
+
const current = items.find((item) => item.tabIndex === 0 && item.dataset.toolbarStop) ?? items[0];
|
|
42
|
+
items.forEach((item) => {
|
|
43
|
+
item.tabIndex = item === current ? 0 : -1;
|
|
44
|
+
if (item === current) item.dataset.toolbarStop = "";
|
|
45
|
+
else delete item.dataset.toolbarStop;
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
apply();
|
|
49
|
+
const observer = new MutationObserver(apply);
|
|
50
|
+
observer.observe(toolbar, { childList: true, subtree: true });
|
|
51
|
+
return () => observer.disconnect();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const focusItem = (item: HTMLButtonElement | undefined) => {
|
|
55
|
+
const toolbar = ref.current;
|
|
56
|
+
if (!toolbar || !item) return;
|
|
57
|
+
controls(toolbar).forEach((other) => {
|
|
58
|
+
other.tabIndex = other === item ? 0 : -1;
|
|
59
|
+
if (other === item) other.dataset.toolbarStop = "";
|
|
60
|
+
else delete other.dataset.toolbarStop;
|
|
61
|
+
});
|
|
62
|
+
item.focus();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div
|
|
67
|
+
ref={ref}
|
|
68
|
+
role="toolbar"
|
|
69
|
+
aria-label={label}
|
|
70
|
+
aria-orientation={orientation}
|
|
71
|
+
className={cn(
|
|
72
|
+
"flex flex-wrap items-center",
|
|
73
|
+
orientation === "vertical" && "flex-col",
|
|
74
|
+
gap(spacing),
|
|
75
|
+
className,
|
|
76
|
+
)}
|
|
77
|
+
onFocus={(event) => {
|
|
78
|
+
onFocus?.(event);
|
|
79
|
+
// Remember a control focused by click, so Tab returns to it.
|
|
80
|
+
const target = event.target as HTMLElement;
|
|
81
|
+
if (target instanceof HTMLButtonElement && ref.current?.contains(target)) focusItem(target);
|
|
82
|
+
}}
|
|
83
|
+
onKeyDown={(event) => {
|
|
84
|
+
onKeyDown?.(event);
|
|
85
|
+
const toolbar = ref.current;
|
|
86
|
+
if (event.defaultPrevented || !toolbar) return;
|
|
87
|
+
const next = event.key === "ArrowRight" || event.key === "ArrowDown";
|
|
88
|
+
const previous = event.key === "ArrowLeft" || event.key === "ArrowUp";
|
|
89
|
+
if (!next && !previous) return;
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
const items = controls(toolbar);
|
|
92
|
+
const index = items.indexOf(document.activeElement as HTMLButtonElement);
|
|
93
|
+
const to = next
|
|
94
|
+
? (index + 1) % items.length
|
|
95
|
+
: index <= 0
|
|
96
|
+
? items.length - 1
|
|
97
|
+
: index - 1;
|
|
98
|
+
focusItem(items[to]);
|
|
99
|
+
}}
|
|
100
|
+
{...props}
|
|
101
|
+
>
|
|
102
|
+
{children}
|
|
103
|
+
</div>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Tooltip as TooltipPrimitive } from "radix-ui";
|
|
2
|
+
import { cn } from "../lib/utils";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Wrap the app (or a subtree) once so tooltips share hover timing.
|
|
6
|
+
* Radix requires a provider above any `Tooltip`.
|
|
7
|
+
*/
|
|
8
|
+
export const TooltipProvider = TooltipPrimitive.Provider;
|
|
9
|
+
export const TooltipTrigger = TooltipPrimitive.Trigger;
|
|
10
|
+
|
|
11
|
+
export interface TooltipProps
|
|
12
|
+
extends Omit<React.ComponentProps<typeof TooltipPrimitive.Root>, "children"> {
|
|
13
|
+
/** Tooltip text. */
|
|
14
|
+
content: React.ReactNode;
|
|
15
|
+
side?: React.ComponentProps<typeof TooltipPrimitive.Content>["side"];
|
|
16
|
+
align?: React.ComponentProps<typeof TooltipPrimitive.Content>["align"];
|
|
17
|
+
sideOffset?: number;
|
|
18
|
+
/** Swirl's colours: neutral `default`, `info`, or `primary`. */
|
|
19
|
+
intent?: keyof typeof intents;
|
|
20
|
+
/** CSS max-width of the bubble. Swirl's default is 17.5rem. */
|
|
21
|
+
maxWidth?: string;
|
|
22
|
+
/** The element the tooltip describes. */
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
className?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const intents = {
|
|
28
|
+
default: "bg-surface-neutral text-on-status",
|
|
29
|
+
info: "bg-surface-info text-on-status",
|
|
30
|
+
primary: "bg-primary text-on-primary",
|
|
31
|
+
} as const;
|
|
32
|
+
|
|
33
|
+
const arrowIntents = {
|
|
34
|
+
default: "bg-surface-neutral",
|
|
35
|
+
info: "bg-surface-info",
|
|
36
|
+
primary: "bg-primary",
|
|
37
|
+
} as const;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Swirl's tooltip: a short hint on hover or keyboard focus, in a dark bubble
|
|
41
|
+
* 12px from its trigger with a rounded arrow.
|
|
42
|
+
*
|
|
43
|
+
* Collapses Radix's Root/Trigger/Content trio into one component, since a
|
|
44
|
+
* tooltip is almost always "this element, that text". The trigger uses
|
|
45
|
+
* `asChild`, so the child must accept a ref and forward DOM props.
|
|
46
|
+
*/
|
|
47
|
+
export function Tooltip({
|
|
48
|
+
content,
|
|
49
|
+
children,
|
|
50
|
+
side = "top",
|
|
51
|
+
align = "center",
|
|
52
|
+
sideOffset = 12,
|
|
53
|
+
intent = "default",
|
|
54
|
+
maxWidth = "17.5rem",
|
|
55
|
+
className,
|
|
56
|
+
...props
|
|
57
|
+
}: TooltipProps) {
|
|
58
|
+
return (
|
|
59
|
+
<TooltipPrimitive.Root {...props}>
|
|
60
|
+
<TooltipPrimitive.Trigger asChild>{children}</TooltipPrimitive.Trigger>
|
|
61
|
+
<TooltipPrimitive.Portal>
|
|
62
|
+
<TooltipPrimitive.Content
|
|
63
|
+
side={side}
|
|
64
|
+
align={align}
|
|
65
|
+
sideOffset={sideOffset}
|
|
66
|
+
collisionPadding={8}
|
|
67
|
+
style={{ maxWidth }}
|
|
68
|
+
className={cn(
|
|
69
|
+
"z-50 rounded-sm px-3 pt-2 pb-[calc(0.5rem+1px)] text-start leading-6 font-normal shadow-level-2",
|
|
70
|
+
"desktop:text-sm desktop:leading-5",
|
|
71
|
+
intents[intent],
|
|
72
|
+
"data-[state=delayed-open]:animate-in data-[state=delayed-open]:fade-in-0 data-[state=delayed-open]:zoom-in-95",
|
|
73
|
+
"data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95",
|
|
74
|
+
"origin-(--radix-tooltip-content-transform-origin)",
|
|
75
|
+
className,
|
|
76
|
+
)}
|
|
77
|
+
>
|
|
78
|
+
{content}
|
|
79
|
+
{/* Swirl's arrow: an 11px square with soft corners, turned 45deg and
|
|
80
|
+
half tucked under the bubble. */}
|
|
81
|
+
<TooltipPrimitive.Arrow asChild width={11} height={0}>
|
|
82
|
+
<span
|
|
83
|
+
className={cn(
|
|
84
|
+
"block size-[0.6875rem] -translate-y-[calc(50%+1px)] rotate-45 rounded-[0.125rem]",
|
|
85
|
+
arrowIntents[intent],
|
|
86
|
+
)}
|
|
87
|
+
/>
|
|
88
|
+
</TooltipPrimitive.Arrow>
|
|
89
|
+
</TooltipPrimitive.Content>
|
|
90
|
+
</TooltipPrimitive.Portal>
|
|
91
|
+
</TooltipPrimitive.Root>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The raw primitives, for cases the simple wrapper cannot express. */
|
|
96
|
+
export const TooltipRoot = TooltipPrimitive.Root;
|
|
97
|
+
export const TooltipContent = TooltipPrimitive.Content;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { cn } from "../lib/utils";
|
|
3
|
+
import { Tag } from "./tag";
|
|
4
|
+
|
|
5
|
+
const ChevronRightIcon = () => (
|
|
6
|
+
<svg viewBox="0 0 16 16" className="size-4" aria-hidden focusable={false}>
|
|
7
|
+
<path
|
|
8
|
+
d="M5.73333 11.6C5.58889 11.4556 5.51666 11.2778 5.51666 11.0667C5.51666 10.8556 5.58889 10.6778 5.73333 10.5333L8.26666 8.00002L5.73333 5.46668C5.58889 5.32224 5.51666 5.14446 5.51666 4.93335C5.51666 4.72224 5.58889 4.54446 5.73333 4.40002C5.87777 4.25557 6.05555 4.18335 6.26666 4.18335C6.47777 4.18335 6.65555 4.25557 6.8 4.40002L9.86666 7.46668C9.94444 7.54446 10 7.62779 10.0333 7.71668C10.0667 7.80557 10.0833 7.90002 10.0833 8.00002C10.0833 8.10002 10.0667 8.19446 10.0333 8.28335C10 8.37224 9.94444 8.45557 9.86666 8.53335L6.8 11.6C6.65555 11.7445 6.47777 11.8167 6.26666 11.8167C6.05555 11.8167 5.87777 11.7445 5.73333 11.6Z"
|
|
9
|
+
fill="currentColor"
|
|
10
|
+
/>
|
|
11
|
+
</svg>
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const OpenInNewIcon = () => (
|
|
15
|
+
<svg viewBox="0 0 16 16" className="size-4" aria-hidden focusable={false}>
|
|
16
|
+
<path
|
|
17
|
+
d="M3.38334 14.1334C2.96111 14.1334 2.60289 13.9863 2.30867 13.692C2.014 13.3974 1.86667 13.0389 1.86667 12.6167V3.38337C1.86667 2.96114 2.014 2.6027 2.30867 2.30803C2.60289 2.01381 2.96111 1.8667 3.38334 1.8667H7.23334C7.45556 1.8667 7.63889 1.93892 7.78334 2.08337C7.92778 2.22781 8 2.40559 8 2.6167C8 2.83892 7.92778 3.02225 7.78334 3.1667C7.63889 3.31114 7.45556 3.38337 7.23334 3.38337H3.38334V12.6167H12.6167V8.7667C12.6167 8.54448 12.6889 8.36114 12.8333 8.2167C12.9778 8.07225 13.1611 8.00003 13.3833 8.00003C13.5944 8.00003 13.7722 8.07225 13.9167 8.2167C14.0611 8.36114 14.1333 8.54448 14.1333 8.7667V12.6167C14.1333 13.0389 13.9862 13.3974 13.692 13.692C13.3973 13.9863 13.0389 14.1334 12.6167 14.1334H3.38334ZM6.06667 9.93337C5.92222 9.80003 5.85 9.62781 5.85 9.4167C5.85 9.20559 5.92222 9.02781 6.06667 8.88337L11.5667 3.38337H10.1C9.87778 3.38337 9.69445 3.31114 9.55 3.1667C9.40556 3.02225 9.33333 2.83892 9.33333 2.6167C9.33333 2.40559 9.40556 2.22781 9.55 2.08337C9.69445 1.93892 9.87778 1.8667 10.1 1.8667H13.3833C13.5944 1.8667 13.7722 1.93892 13.9167 2.08337C14.0611 2.22781 14.1333 2.40559 14.1333 2.6167V5.90003C14.1333 6.12225 14.0611 6.30559 13.9167 6.45003C13.7722 6.59448 13.5944 6.6667 13.3833 6.6667C13.1611 6.6667 12.9778 6.59448 12.8333 6.45003C12.6889 6.30559 12.6167 6.12225 12.6167 5.90003V4.43337L7.1 9.95003C6.96667 10.0945 6.79733 10.1667 6.592 10.1667C6.38622 10.1667 6.21111 10.0889 6.06667 9.93337Z"
|
|
18
|
+
fill="currentColor"
|
|
19
|
+
/>
|
|
20
|
+
</svg>
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const LevelContext = React.createContext(1);
|
|
24
|
+
|
|
25
|
+
export interface TreeNavigationProps extends React.ComponentProps<"ul"> {
|
|
26
|
+
/** Accessible name of the navigation. */
|
|
27
|
+
label?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Swirl's tree navigation: a sidebar menu of pages, where an item with
|
|
32
|
+
* children opens and closes its section. Every item is in the tab order;
|
|
33
|
+
* Space toggles a section and Right and Left open and close it.
|
|
34
|
+
*/
|
|
35
|
+
export function TreeNavigation({ className, label, ...props }: TreeNavigationProps) {
|
|
36
|
+
return (
|
|
37
|
+
<ul
|
|
38
|
+
role="tree"
|
|
39
|
+
aria-label={label}
|
|
40
|
+
className={cn("m-0 list-none p-0", className)}
|
|
41
|
+
{...props}
|
|
42
|
+
/>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface TreeNavigationItemProps
|
|
47
|
+
extends Omit<React.ComponentProps<"a">, "children"> {
|
|
48
|
+
label: React.ReactNode;
|
|
49
|
+
/** Leading 20px icon. */
|
|
50
|
+
icon?: React.ReactNode;
|
|
51
|
+
/** Marks the item as the current page. */
|
|
52
|
+
active?: boolean;
|
|
53
|
+
/** Links to another site: shows an external-link icon on hover and focus. */
|
|
54
|
+
external?: boolean;
|
|
55
|
+
/** Show a "New" tag after the label. */
|
|
56
|
+
markAsNew?: boolean;
|
|
57
|
+
markAsNewLabel?: string;
|
|
58
|
+
/** Let an item with children open and close. Defaults to true. */
|
|
59
|
+
expandable?: boolean;
|
|
60
|
+
/** Open the section at first. */
|
|
61
|
+
defaultExpanded?: boolean;
|
|
62
|
+
/** Nested `TreeNavigationItem`s, shown in the item's section. */
|
|
63
|
+
children?: React.ReactNode;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function TreeNavigationItem({
|
|
67
|
+
className,
|
|
68
|
+
label,
|
|
69
|
+
icon,
|
|
70
|
+
active = false,
|
|
71
|
+
external = false,
|
|
72
|
+
markAsNew = false,
|
|
73
|
+
markAsNewLabel = "New",
|
|
74
|
+
expandable = true,
|
|
75
|
+
defaultExpanded = false,
|
|
76
|
+
href,
|
|
77
|
+
children,
|
|
78
|
+
onClick,
|
|
79
|
+
onKeyDown,
|
|
80
|
+
...props
|
|
81
|
+
}: TreeNavigationItemProps) {
|
|
82
|
+
const level = React.useContext(LevelContext);
|
|
83
|
+
const id = React.useId();
|
|
84
|
+
const [expanded, setExpanded] = React.useState(defaultExpanded);
|
|
85
|
+
const hasChildren = React.Children.toArray(children).some(React.isValidElement);
|
|
86
|
+
const canToggle = expandable && hasChildren;
|
|
87
|
+
const nested = level > 1;
|
|
88
|
+
|
|
89
|
+
// A link navigates; only an item without a link is a section toggle.
|
|
90
|
+
const Component = (href ? "a" : "button") as React.ElementType;
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<li
|
|
94
|
+
role="treeitem"
|
|
95
|
+
aria-level={level}
|
|
96
|
+
aria-expanded={hasChildren ? expanded : undefined}
|
|
97
|
+
className="w-full list-none rounded-base"
|
|
98
|
+
>
|
|
99
|
+
<Component
|
|
100
|
+
href={href}
|
|
101
|
+
type={href ? undefined : "button"}
|
|
102
|
+
aria-current={active ? "page" : undefined}
|
|
103
|
+
aria-controls={hasChildren ? `${id}-children` : undefined}
|
|
104
|
+
aria-expanded={hasChildren ? expanded : undefined}
|
|
105
|
+
className={cn(
|
|
106
|
+
"group/nav mb-0.5 inline-flex h-12 w-full min-w-0 cursor-pointer items-center gap-0.5 rounded-base border-none bg-transparent pr-3",
|
|
107
|
+
"text-left text-sm leading-5 text-foreground no-underline outline-none",
|
|
108
|
+
nested ? "pl-9 font-normal" : "pl-3 font-medium",
|
|
109
|
+
"hover:bg-background-hovered active:bg-state-pressed",
|
|
110
|
+
active && "bg-background-hovered font-medium text-highlight hover:bg-state-pressed",
|
|
111
|
+
className,
|
|
112
|
+
)}
|
|
113
|
+
onClick={(event: React.MouseEvent<HTMLAnchorElement>) => {
|
|
114
|
+
onClick?.(event);
|
|
115
|
+
if (canToggle && !href && !event.defaultPrevented) {
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
setExpanded((open) => !open);
|
|
118
|
+
}
|
|
119
|
+
}}
|
|
120
|
+
onKeyDown={(event: React.KeyboardEvent<HTMLAnchorElement>) => {
|
|
121
|
+
onKeyDown?.(event);
|
|
122
|
+
if (event.defaultPrevented || !canToggle) return;
|
|
123
|
+
if (event.key === " " && !href) {
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
setExpanded((open) => !open);
|
|
126
|
+
} else if (event.key === "ArrowRight") {
|
|
127
|
+
event.preventDefault();
|
|
128
|
+
setExpanded(true);
|
|
129
|
+
} else if (event.key === "ArrowLeft") {
|
|
130
|
+
event.preventDefault();
|
|
131
|
+
setExpanded(false);
|
|
132
|
+
}
|
|
133
|
+
}}
|
|
134
|
+
{...props}
|
|
135
|
+
>
|
|
136
|
+
<span className="flex min-w-0 flex-1 items-center">
|
|
137
|
+
{icon && (
|
|
138
|
+
<span aria-hidden className="inline-flex max-h-5 shrink-0 pr-2 [&>svg]:size-5">
|
|
139
|
+
{icon}
|
|
140
|
+
</span>
|
|
141
|
+
)}
|
|
142
|
+
{/* Swirl rings the label, not the whole row, on keyboard focus. */}
|
|
143
|
+
<span className="flex min-w-0 flex-1 items-center rounded-xs group-focus-visible/nav:shadow-[0_0_0_0.125rem_var(--color-focus)]">
|
|
144
|
+
<span className="truncate">{label}</span>
|
|
145
|
+
{canToggle && !external && (
|
|
146
|
+
<span
|
|
147
|
+
aria-hidden
|
|
148
|
+
className={cn(
|
|
149
|
+
"ml-1 inline-flex max-h-4 shrink-0 text-icon-default transition-transform duration-300 motion-reduce:transition-none",
|
|
150
|
+
expanded && "rotate-90",
|
|
151
|
+
)}
|
|
152
|
+
>
|
|
153
|
+
<ChevronRightIcon />
|
|
154
|
+
</span>
|
|
155
|
+
)}
|
|
156
|
+
{external && (
|
|
157
|
+
<span
|
|
158
|
+
aria-hidden
|
|
159
|
+
className={cn(
|
|
160
|
+
"ml-1 inline-flex max-h-4 shrink-0 text-icon-default opacity-0 transition-opacity duration-300",
|
|
161
|
+
"group-hover/nav:opacity-100 group-focus-visible/nav:opacity-100",
|
|
162
|
+
)}
|
|
163
|
+
>
|
|
164
|
+
<OpenInNewIcon />
|
|
165
|
+
</span>
|
|
166
|
+
)}
|
|
167
|
+
</span>
|
|
168
|
+
{markAsNew && <Tag size="s" variant="strong" label={markAsNewLabel} className="shrink-0" />}
|
|
169
|
+
</span>
|
|
170
|
+
</Component>
|
|
171
|
+
{hasChildren && (
|
|
172
|
+
// A grid row animates from 0 to the section's height, as Swirl does
|
|
173
|
+
// with its measured height. Closed sections are inert, so their
|
|
174
|
+
// links leave the tab order.
|
|
175
|
+
<div
|
|
176
|
+
className={cn(
|
|
177
|
+
"grid transition-[grid-template-rows] duration-300 ease-out motion-reduce:transition-none",
|
|
178
|
+
expanded ? "grid-rows-[1fr]" : "grid-rows-[0fr]",
|
|
179
|
+
)}
|
|
180
|
+
inert={!expanded}
|
|
181
|
+
>
|
|
182
|
+
<ul
|
|
183
|
+
id={`${id}-children`}
|
|
184
|
+
role="group"
|
|
185
|
+
aria-label={typeof label === "string" ? `${label} submenu` : undefined}
|
|
186
|
+
className="m-0 flex min-h-0 list-none flex-col overflow-hidden p-0"
|
|
187
|
+
>
|
|
188
|
+
<LevelContext.Provider value={level + 1}>{children}</LevelContext.Provider>
|
|
189
|
+
</ul>
|
|
190
|
+
</div>
|
|
191
|
+
)}
|
|
192
|
+
</li>
|
|
193
|
+
);
|
|
194
|
+
}
|