@cosxai/ui 0.14.0 → 0.15.1
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/package.json
CHANGED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { cn } from "../lib/cn";
|
|
2
|
+
|
|
3
|
+
// SegmentedControl — a horizontal set of mutually-exclusive options
|
|
4
|
+
// rendered as one pill-shaped track with a raised "active" segment.
|
|
5
|
+
// Use it where the choice swaps the content below it (mode switch,
|
|
6
|
+
// filter scope), i.e. wherever a pair of radio buttons would be
|
|
7
|
+
// semantically right but visually too quiet. For true page-level
|
|
8
|
+
// navigation reach for router links instead; for on/off state use
|
|
9
|
+
// ToggleSwitch.
|
|
10
|
+
//
|
|
11
|
+
// The caller owns the selection: pass `value` + `onChange`, one
|
|
12
|
+
// option per segment. Per-option `disabled` renders the segment
|
|
13
|
+
// non-interactive with a hint via `title` (a disabled option should
|
|
14
|
+
// tell the user why — pass `disabledHint`).
|
|
15
|
+
//
|
|
16
|
+
// Semantics are a radiogroup (not tabs): arrow keys move selection
|
|
17
|
+
// directly, matching native radio behaviour, and screen readers
|
|
18
|
+
// announce "x of y" positioning for free.
|
|
19
|
+
|
|
20
|
+
export interface SegmentedControlOption<V extends string = string> {
|
|
21
|
+
value: V;
|
|
22
|
+
label: React.ReactNode;
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
/** Shown as the native tooltip on a disabled segment — say WHY. */
|
|
25
|
+
disabledHint?: string;
|
|
26
|
+
"data-testid"?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SegmentedControlProps<V extends string = string> {
|
|
30
|
+
value: V;
|
|
31
|
+
onChange: (next: V) => void;
|
|
32
|
+
options: SegmentedControlOption<V>[];
|
|
33
|
+
/** Disables the whole control (per-option disabled still applies). */
|
|
34
|
+
disabled?: boolean;
|
|
35
|
+
/** "full" stretches the track and gives segments equal width. */
|
|
36
|
+
fit?: "auto" | "full";
|
|
37
|
+
"aria-label"?: string;
|
|
38
|
+
className?: string;
|
|
39
|
+
"data-testid"?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function SegmentedControl<V extends string = string>({
|
|
43
|
+
value,
|
|
44
|
+
onChange,
|
|
45
|
+
options,
|
|
46
|
+
disabled,
|
|
47
|
+
fit = "auto",
|
|
48
|
+
"aria-label": ariaLabel,
|
|
49
|
+
className,
|
|
50
|
+
"data-testid": testId,
|
|
51
|
+
}: SegmentedControlProps<V>) {
|
|
52
|
+
// Arrow keys move selection to the nearest enabled neighbour,
|
|
53
|
+
// wrapping — the native radio-group keyboard model.
|
|
54
|
+
const move = (from: number, step: 1 | -1) => {
|
|
55
|
+
for (let i = 1; i <= options.length; i++) {
|
|
56
|
+
const next = options[(from + step * i + options.length * i) % options.length];
|
|
57
|
+
if (next && !next.disabled) {
|
|
58
|
+
onChange(next.value);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<div
|
|
66
|
+
role="radiogroup"
|
|
67
|
+
aria-label={ariaLabel}
|
|
68
|
+
className={cn("ck-segmented", className)}
|
|
69
|
+
data-testid={testId}
|
|
70
|
+
style={{
|
|
71
|
+
display: fit === "full" ? "flex" : "inline-flex",
|
|
72
|
+
alignItems: "stretch",
|
|
73
|
+
gap: 2,
|
|
74
|
+
padding: 2,
|
|
75
|
+
borderRadius: 8,
|
|
76
|
+
border: "1px solid var(--ck-border-subtle)",
|
|
77
|
+
background: "var(--ck-bg-muted)",
|
|
78
|
+
opacity: disabled ? 0.5 : 1,
|
|
79
|
+
}}
|
|
80
|
+
>
|
|
81
|
+
{options.map((opt, i) => {
|
|
82
|
+
const selected = opt.value === value;
|
|
83
|
+
const optDisabled = disabled || opt.disabled;
|
|
84
|
+
return (
|
|
85
|
+
<button
|
|
86
|
+
key={opt.value}
|
|
87
|
+
type="button"
|
|
88
|
+
role="radio"
|
|
89
|
+
aria-checked={selected}
|
|
90
|
+
disabled={optDisabled}
|
|
91
|
+
title={opt.disabled ? opt.disabledHint : undefined}
|
|
92
|
+
data-testid={opt["data-testid"]}
|
|
93
|
+
// Roving tabindex: the group is one tab stop; arrows walk it.
|
|
94
|
+
tabIndex={selected ? 0 : -1}
|
|
95
|
+
onClick={() => !optDisabled && !selected && onChange(opt.value)}
|
|
96
|
+
onKeyDown={(e) => {
|
|
97
|
+
if (e.key === "ArrowRight" || e.key === "ArrowDown") {
|
|
98
|
+
e.preventDefault();
|
|
99
|
+
move(i, 1);
|
|
100
|
+
} else if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
move(i, -1);
|
|
103
|
+
}
|
|
104
|
+
}}
|
|
105
|
+
style={{
|
|
106
|
+
flex: fit === "full" ? 1 : undefined,
|
|
107
|
+
padding: "6px 14px",
|
|
108
|
+
borderRadius: 6,
|
|
109
|
+
border: "1px solid transparent",
|
|
110
|
+
font: `500 13px/1.2 var(--ck-font-sans)`,
|
|
111
|
+
color: selected
|
|
112
|
+
? "var(--ck-text-primary)"
|
|
113
|
+
: "var(--ck-text-secondary)",
|
|
114
|
+
background: selected ? "var(--ck-bg-surface)" : "transparent",
|
|
115
|
+
boxShadow: selected ? "0 1px 2px rgba(0,0,0,0.08)" : "none",
|
|
116
|
+
borderColor: selected ? "var(--ck-border-subtle)" : "transparent",
|
|
117
|
+
cursor: optDisabled ? "not-allowed" : "pointer",
|
|
118
|
+
opacity: opt.disabled && !disabled ? 0.45 : 1,
|
|
119
|
+
whiteSpace: "nowrap",
|
|
120
|
+
transition: "background 120ms ease, color 120ms ease",
|
|
121
|
+
}}
|
|
122
|
+
>
|
|
123
|
+
{opt.label}
|
|
124
|
+
</button>
|
|
125
|
+
);
|
|
126
|
+
})}
|
|
127
|
+
</div>
|
|
128
|
+
);
|
|
129
|
+
}
|
package/src/primitives/index.ts
CHANGED
|
@@ -32,3 +32,5 @@ export { SignaturePad } from "./SignaturePad";
|
|
|
32
32
|
export type { SignaturePadProps, SignaturePadHandle } from "./SignaturePad";
|
|
33
33
|
export { CopyField } from "./CopyField";
|
|
34
34
|
export type { CopyFieldProps } from "./CopyField";
|
|
35
|
+
export { SegmentedControl } from "./SegmentedControl";
|
|
36
|
+
export type { SegmentedControlProps, SegmentedControlOption } from "./SegmentedControl";
|
package/src/styles/base.css
CHANGED
|
@@ -107,3 +107,18 @@ a:visited { color: var(--ck-accent); }
|
|
|
107
107
|
animation: none;
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
+
|
|
111
|
+
/* Scrollbars — macOS renders auto-hiding overlay bars, but Windows
|
|
112
|
+
(and Linux) render CLASSIC opaque gutters that read as heavy grey
|
|
113
|
+
slabs against the kit's editorial surfaces (QA: Ben, 2026-07-19).
|
|
114
|
+
The standard properties slim them down and tint the thumb from
|
|
115
|
+
the theme's border token so both light and dark chrome stay
|
|
116
|
+
coherent. `scrollbar-color` inherits from :root; `scrollbar-width`
|
|
117
|
+
doesn't, hence the universal selector. Browsers without support
|
|
118
|
+
(older Safari) keep their native overlay bars — already fine. */
|
|
119
|
+
* {
|
|
120
|
+
scrollbar-width: thin;
|
|
121
|
+
}
|
|
122
|
+
:root {
|
|
123
|
+
scrollbar-color: var(--ck-border-strong, rgba(0, 0, 0, 0.25)) transparent;
|
|
124
|
+
}
|