@weichenlin/swell-ui 0.1.0 → 0.3.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Hand-built React 19 components styled with Tailwind CSS v4: Badge, Button, Card, Checkbox,
4
4
  CheckboxGroup, Dialog, Drawer, Input (plus Search, Password and Copy), Meter, Select, Sidebar,
5
- Switch, Tabs, TabNav, ThemeSwitcher, Toast and Tooltip.
5
+ Switch, Table, Tabs, TabNav, ThemeSwitcher, Toast and Tooltip.
6
6
 
7
7
  The package ships ESM, type declarations and two CSS files. Your app compiles the CSS with its own
8
8
  Tailwind v4, so the design tokens become your utilities too (`bg-primary`, `text-fg`, ...).
@@ -38,32 +38,180 @@ export function App() {
38
38
  }
39
39
  ```
40
40
 
41
- ### Override tokens
41
+ ### Table
42
42
 
43
- Redefine any token after the import. Light values go in `@theme`; dark values in the same two dark
44
- blocks `theme.css` uses:
43
+ Pass the current rows, column definitions, and a stable row ID. Add `search`, `sorting`, `pagination`,
44
+ or `selection` to show controlled UI. The caller handles filtering, sorting, and fetching or slicing
45
+ rows. The pagination `rowCount` is the total after filtering, before paging.
46
+
47
+ ```tsx
48
+ import { Table } from '@weichenlin/swell-ui'
49
+
50
+ ;<Table
51
+ title="Deployments"
52
+ data={deployments}
53
+ getRowId={(row) => row.id}
54
+ columns={[
55
+ { id: 'service', header: 'Service', rowHeader: true, cell: (row) => row.service },
56
+ { id: 'duration', header: 'Duration', numeric: true, cell: (row) => row.duration },
57
+ ]}
58
+ emptyState="No deployments yet"
59
+ noResults="No matching deployments"
60
+ />
61
+ ```
62
+
63
+ ### Change the brand
64
+
65
+ The default look is neutral gray with a near-black primary. A rebrand is two tokens, set once,
66
+ after the import:
45
67
 
46
68
  ```css
47
69
  @import 'tailwindcss';
48
70
  @import '@weichenlin/swell-ui/styles.css';
49
71
 
50
72
  @theme {
51
- --color-primary: #2563eb;
52
- --radius-md: 6px;
73
+ --color-primary: #f7a501; /* buttons, input focus, checked switches and checkboxes, halos */
74
+ --color-primary-fg: #141413; /* text and marks drawn on the primary fill */
75
+ }
76
+ ```
77
+
78
+ That covers light, dark-from-the-system and `data-theme="dark"` alike. Input focus
79
+ (`--color-input-focus`), checked controls (`--color-control-checked`, `-fg`) and the focus halo
80
+ (`--color-input-halo`) are `var()` references to these two, resolved at runtime, so they follow.
81
+
82
+ To use a different value per scheme, write both on one line with `light-dark()`:
83
+
84
+ ```css
85
+ @theme {
86
+ --color-primary: light-dark(#2563eb, #60a5fa);
87
+ --color-primary-fg: light-dark(#ffffff, #0b1220);
88
+ }
89
+ ```
90
+
91
+ A light brand color (yellow, lime, cyan) can fall under 3:1 against the light page. The buttons
92
+ stay readable through `--color-primary-fg`, but if the focus border or a checked box needs more
93
+ contrast, override just that one:
94
+
95
+ ```css
96
+ @theme {
97
+ --color-input-focus: light-dark(#b37700, #f7a501); /* darker amber on light only */
53
98
  }
99
+ ```
100
+
101
+ Set tokens in `@theme` (or a plain `:root` rule) rather than on an inner element: the derived
102
+ tokens are resolved on the root.
54
103
 
55
- :root[data-theme='dark'] {
56
- --color-primary: #60a5fa;
104
+ ### Change the palette
105
+
106
+ Colors come in two layers. A handful of **base colors** carry literal values; every other color
107
+ token is computed from them at runtime (`var()` and `color-mix()`), so overriding a base color
108
+ carries through to borders, muted text, fields, tints and so on:
109
+
110
+ ```css
111
+ @theme {
112
+ --color-bg: light-dark(#f5f4ed, #141413);
113
+ --color-surface: light-dark(#faf9f5, #30302e);
114
+ --color-fg: light-dark(#141413, #faf9f5);
57
115
  }
58
- :root:not([data-theme='light']) {
59
- @media (prefers-color-scheme: dark) {
60
- --color-primary: #60a5fa;
61
- }
116
+ ```
117
+
118
+ The mix percentages are tuned so the default palette, and a warm one like the example above, pass WCAG contrast (text >=4.5:1,
119
+ field borders >=3:1). A palette far from these (a mid-tone page, low-contrast text) can fall
120
+ short, so check contrast after changing `bg`, `surface` or `fg`. Any computed token can still be
121
+ overridden on its own.
122
+
123
+ Override at the root (`@theme` or `:root`): computed tokens are resolved there, so a base color
124
+ set on an inner element does not reach them.
125
+
126
+ ### Tokens
127
+
128
+ Every pair is written `light-dark(<light>, <dark>)`; override it the same way, once. All color
129
+ tokens are also Tailwind utilities (`bg-surface`, `text-fg-muted`, `border-input-border`, ...).
130
+
131
+ **Base colors**
132
+
133
+ | Token | Used for |
134
+ | --------------------------------------- | ------------------------------------------------------ |
135
+ | `--color-bg` | Page background |
136
+ | `--color-surface` | Cards, panels and text fields |
137
+ | `--color-surface-raised` | Popovers and dialogs |
138
+ | `--color-fg` | Text |
139
+ | `--color-primary`, `--color-primary-fg` | Brand fill and the text on it. Default: `fg` (neutral) |
140
+ | `--color-focus` | Keyboard focus outline (blue) |
141
+ | `--color-{danger,success,warning,info}` | Status tones: solid fill, icon, and danger text |
142
+
143
+ **Computed** (from the base colors; override only to fine-tune)
144
+
145
+ | Token | Used for | Computed from |
146
+ | ------------------------------------------------------------- | ---------------------------------------------------------------- | ------------------------------------------------------------------------ |
147
+ | `--color-accent` | Secondary brand color; unused by components | `primary` |
148
+ | `--color-surface-muted` | Hover fills, secondary button | fg into bg |
149
+ | `--color-fg-strong`, `-fg-muted`, `-fg-subtle` | Text from strong to quiet (`fg-subtle` >=4.5:1 on every surface) | fg into bg |
150
+ | `--color-border`, `-border-strong`, `-ring`, `-ring-deep` | Dividers, hairlines, 1px button and tab-list rings | fg into bg |
151
+ | `--color-inverse`, `-inverse-fg` | `dark` button, tooltips | fg into bg; the page on dark |
152
+ | `--color-input-bg`, `-input-bg-hover` | Text field fill (read-only fields use `bg`) | `surface` |
153
+ | `--color-input-border`, `-input-border-hover` | Field border; also the off track of Switch and box of Checkbox | fg into bg; `fg-strong` |
154
+ | `--color-input-placeholder` | Placeholder text | fg into bg |
155
+ | `--color-input-focus`, `-input-halo` | Focused field border and its 3px ring | `primary`; input-focus at 14% |
156
+ | `--color-input-halo-invalid`, `-invalid-focus` | Same ring for invalid fields | danger at 12% / 22% |
157
+ | `--color-control-checked`, `-control-checked-fg` | Checked Switch/Checkbox fill and the check on it | `primary` / `primary-fg` |
158
+ | `--color-switch-handle`, `-switch-handle-checked` | Switch thumb, off / on | off: `surface-raised` / dark `fg`; on: white or black by track lightness |
159
+ | `--color-popover-bg`, `-popover-border`, `-popover-highlight` | Select picker and dialogs; highlight is the active row | `surface-raised`, fg into it |
160
+ | `--color-dialog-backdrop` | Dimmed page behind Dialog and Drawer | fg at 40%; black at 60% on dark |
161
+ | `--color-neutral` | Neutral status tone | fg into bg |
162
+ | `--color-{tone}-fg` | Text on a solid status fill | `surface` / `bg` |
163
+ | `--color-{tone}-subtle-bg`, `-border` | Tinted badge and its hairline | tone into bg |
164
+ | `--color-{tone}-subtle-fg` | Text on the tinted badge (>=5:1) | tone pushed toward fg |
165
+
166
+ Shadows (`--shadow-input`, `-input-lift`, `-switch-thumb`, `-popover`, `-dialog`, `-whisper`) and
167
+ the input highlight are black or white at low opacity: they shade, so they have no palette color.
168
+ `--container-page` is `max-w-page` (1200px).
169
+
170
+ ### Serif headings
171
+
172
+ ```css
173
+ @import 'tailwindcss';
174
+ @import '@weichenlin/swell-ui/styles.css';
175
+ @import '@weichenlin/swell-ui/typography.css'; /* h1-h3 in the serif stack, weight 500 */
176
+ ```
177
+
178
+ ### Fonts
179
+
180
+ swell-ui sets no font names: text uses Tailwind's `--font-sans` (the system UI stack), and
181
+ `font-serif` / `font-mono` are Tailwind's defaults too. To use a web font, ship its files with
182
+ your app, for example with [Fontsource](https://fontsource.org):
183
+
184
+ ```sh
185
+ pnpm add @fontsource-variable/inter
186
+ ```
187
+
188
+ ```css
189
+ @import 'tailwindcss';
190
+ @import '@fontsource-variable/inter';
191
+ @import '@weichenlin/swell-ui/styles.css';
192
+
193
+ @theme {
194
+ --font-sans: 'Inter Variable', ui-sans-serif, system-ui, sans-serif;
62
195
  }
63
196
  ```
64
197
 
65
- Note that the theme replaces Tailwind's `rounded-sm/md/lg` values (6, 8 and 12px) and adds
66
- `rounded-sharp` (4px), shadows (`shadow-popover`, `shadow-dialog`, ...) and `max-w-page`.
198
+ ### Radius
199
+
200
+ swell-ui does not touch Tailwind's radius scale; components use the stock `rounded-*` classes, so
201
+ `rounded-md` is still 6px in your app. Changing `--radius-*` in your `@theme` changes the
202
+ components too.
203
+
204
+ Coming from 0.1, which replaced the scale: shift each class one step up to keep the old size.
205
+
206
+ | 0.1 class | px | Now |
207
+ | --------------- | --- | ------------- |
208
+ | `rounded-sharp` | 4 | `rounded-sm` |
209
+ | `rounded-sm` | 6 | `rounded-md` |
210
+ | `rounded-md` | 8 | `rounded-lg` |
211
+ | `rounded-lg` | 12 | `rounded-xl` |
212
+ | `rounded-xl` | 16 | `rounded-2xl` |
213
+ | `rounded-2xl` | 24 | `rounded-3xl` |
214
+ | `rounded-3xl` | 32 | `rounded-4xl` |
67
215
 
68
216
  ### Tokens only
69
217
 
@@ -78,7 +226,8 @@ instead:
78
226
  ### `cn`
79
227
 
80
228
  `cn` (clsx + tailwind-merge) is exported and knows the custom tokens, so
81
- `cn('rounded-sharp', 'rounded-md')` gives `rounded-md`.
229
+ `cn('text-fg-muted', 'text-danger')` gives `text-danger` and `cn('shadow-popover', 'shadow-none')`
230
+ gives `shadow-none`.
82
231
 
83
232
  ## Dark mode
84
233
 
@@ -86,6 +235,10 @@ Without a `data-theme` attribute the theme follows the system (`prefers-color-sc
86
235
  `data-theme="light"` or `data-theme="dark"` on `<html>` forces one. The `dark:` variant follows the
87
236
  same rule.
88
237
 
238
+ Under the hood the root's `color-scheme` is `light dark`, `light` or `dark` accordingly, and every
239
+ color token picks its side with `light-dark()`. Browsers support it natively from Chrome 123,
240
+ Safari 17.5 and Firefox 120; Tailwind's production build rewrites it for older targets.
241
+
89
242
  `useTheme` persists the choice and sets the attribute; `ThemeSwitcher` is the toggle button:
90
243
 
91
244
  ```tsx
@@ -166,6 +319,23 @@ links do) closes it.
166
319
  </TabNav>
167
320
  ```
168
321
 
322
+ `Button` takes `asChild` too, for a navigation that should look like a button. It keeps the
323
+ button's look, icons and ripple, and stays a real link (open in a new tab, copy the URL):
324
+
325
+ ```tsx
326
+ <Button asChild intent="secondary" iconLeft={<Plus />}>
327
+ <Link to="/apps/new">New app</Link>
328
+ </Button>
329
+
330
+ <Button asChild icon={<Settings />} aria-label="Settings">
331
+ <Link to="/settings" />
332
+ </Button>
333
+ ```
334
+
335
+ `disabled` (with or without `disabledReason`) works as on the button: the link stays focusable with
336
+ `aria-disabled`, and clicks are stopped before your Link sees them, so it never navigates. `loading`
337
+ and `type` are button-only.
338
+
169
339
  ## Develop
170
340
 
171
341
  Node 26.11.1 (`.node-version`, via fnm) and pnpm 12.
@@ -177,11 +347,11 @@ pnpm test # vitest (jsdom)
177
347
  pnpm typecheck # tsc -b
178
348
  pnpm lint # vp check: format, lint, type-aware lint
179
349
  pnpm knip # unused files, exports and dependencies
180
- pnpm build # vp pack -> dist/index.js, index.d.ts, theme.css, styles.css
350
+ pnpm build # vp pack -> dist/index.js, index.d.ts and the CSS files
181
351
  pnpm build-storybook
182
352
  ```
183
353
 
184
- `src/styles/theme.css` and `src/styles/styles.css` are copied into `dist/` as they are, so the
354
+ The CSS files under `src/styles/` are copied into `dist/` as they are, so the
185
355
  `@source "./*.js"` in `styles.css` is relative to `dist/`. Storybook and the lint use
186
356
  `src/styles/dev.css`, which adds Tailwind and scans `src/` and `stories/`.
187
357
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,21 @@
1
1
  import { ClassValue } from "clsx";
2
2
  import { VariantProps } from "class-variance-authority";
3
- import { ComponentProps, MouseEvent, ReactElement, ReactNode } from "react";
3
+ import { ComponentProps, MouseEvent, MouseEventHandler, ReactElement, ReactNode } from "react";
4
+ //#region src/components/alert/alert.d.ts
5
+ type AlertTone = 'neutral' | 'info' | 'success' | 'warning' | 'danger';
6
+ type AlertProps = Omit<ComponentProps<'div'>, 'title'> & {
7
+ tone?: AlertTone;
8
+ title?: ReactNode;
9
+ /** An action such as a retry button or a link to settings. */
10
+ action?: ReactNode;
11
+ /** Shows the dismiss button. The caller controls whether the Alert stays mounted. */
12
+ onDismiss?: MouseEventHandler<HTMLButtonElement>;
13
+ /** Accessible name of the dismiss button. */
14
+ dismissLabel?: string;
15
+ };
16
+ /** A persistent message shown next to the content it describes. Use a live region for updates that need announcing. */
17
+ export declare function Alert({ tone, title, action, onDismiss, dismissLabel, className, children, ...props }: AlertProps): import("react").JSX.Element;
18
+ //#endregion
4
19
  //#region src/components/badge/badge.d.ts
5
20
  type BadgeTone = 'neutral' | 'info' | 'success' | 'warning' | 'danger';
6
21
  type BadgeVariant = 'subtle' | 'outline' | 'solid';
@@ -63,14 +78,28 @@ type BaseProps = Omit<ComponentProps<'button'>, 'children'> & Omit<VariantProps<
63
78
  /** Material-style press ripple; on by default. */
64
79
  ripple?: boolean;
65
80
  };
81
+ /** With `asChild` the host is your element (a router Link): loading and `type` are button-only. */
82
+ type AsChildProps = {
83
+ /**
84
+ * Render the only child (e.g. a router Link) instead of a `<button>`, with the button's props,
85
+ * classes, ref and content merged into it.
86
+ */
87
+ asChild: true;
88
+ children: ReactElement;
89
+ loading?: never;
90
+ loadingText?: never;
91
+ type?: never;
92
+ };
66
93
  type TextButtonProps = BaseProps & {
67
94
  icon?: never;
68
95
  iconLeft?: ReactNode;
69
96
  iconRight?: ReactNode;
70
- children?: ReactNode;
71
97
  tooltip?: never;
72
98
  tooltipSide?: never;
73
- };
99
+ } & ({
100
+ asChild?: false;
101
+ children?: ReactNode;
102
+ } | AsChildProps);
74
103
  type IconOnlyButtonProps = Omit<BaseProps, 'aria-label' | 'fullWidth'> & {
75
104
  /** Renders a square icon-only button. */
76
105
  icon: NonNullable<ReactNode>;
@@ -80,10 +109,12 @@ type IconOnlyButtonProps = Omit<BaseProps, 'aria-label' | 'fullWidth'> & {
80
109
  tooltip?: boolean;
81
110
  tooltipSide?: TooltipProps['side'];
82
111
  fullWidth?: never;
83
- children?: never;
84
112
  iconLeft?: never;
85
113
  iconRight?: never;
86
- };
114
+ } & ({
115
+ asChild?: false;
116
+ children?: never;
117
+ } | AsChildProps);
87
118
  type ButtonProps = TextButtonProps | IconOnlyButtonProps;
88
119
  export declare function Button(props: ButtonProps): import("react").JSX.Element;
89
120
  //#endregion
@@ -460,6 +491,75 @@ type SwitchProps = NativeSwitchProps & ChoiceLabelProps & {
460
491
  };
461
492
  export declare function Switch({ label, description, error, size, loading, labelPosition, className, inputClassName, disabled, onChange, 'aria-describedby': describedBy, 'aria-invalid': ariaInvalid, ...props }: SwitchProps): import("react").JSX.Element;
462
493
  //#endregion
494
+ //#region src/components/table/table.d.ts
495
+ type TableColumn<T> = {
496
+ id: string;
497
+ header: ReactNode;
498
+ cell: (row: T) => ReactNode;
499
+ sortable?: boolean;
500
+ align?: 'start' | 'center' | 'end';
501
+ numeric?: boolean;
502
+ rowHeader?: boolean;
503
+ className?: string;
504
+ headerClassName?: string;
505
+ };
506
+ type TableSort = {
507
+ id: string;
508
+ direction: 'asc' | 'desc';
509
+ };
510
+ declare const labels: {
511
+ search: string;
512
+ clearSelection: string;
513
+ selectPage: string;
514
+ selectRow: (id: string) => string;
515
+ selectionCount: (count: number) => string;
516
+ scrollRegion: string;
517
+ pagination: string;
518
+ previousPage: string;
519
+ nextPage: string;
520
+ page: (page: number, count: number) => string;
521
+ range: (first: number, last: number, total: number) => string;
522
+ emptyState: string;
523
+ noResults: string;
524
+ };
525
+ type TableProps<T> = Omit<ComponentProps<'table'>, 'title' | 'children' | 'className'> & {
526
+ /** Rows to render on the current page, in display order. */
527
+ data: readonly T[];
528
+ columns: readonly TableColumn<T>[];
529
+ getRowId: (row: T) => string;
530
+ title?: ReactNode;
531
+ emptyState?: ReactNode;
532
+ noResults?: ReactNode;
533
+ /** Indicates filtering when the caller applies filters outside the search field. */
534
+ filtered?: boolean;
535
+ /** Controlled UI. The caller supplies the resulting data. */
536
+ search?: {
537
+ value: string;
538
+ onChange: (value: string) => void;
539
+ };
540
+ sorting?: {
541
+ value: TableSort | null;
542
+ onChange: (value: TableSort | null) => void;
543
+ };
544
+ pagination?: {
545
+ /** Zero-based page index. */
546
+ pageIndex: number;
547
+ pageSize: number;
548
+ rowCount: number;
549
+ onPageChange: (pageIndex: number) => void;
550
+ };
551
+ selection?: {
552
+ ids: ReadonlySet<string>;
553
+ onChange: (ids: Set<string>) => void;
554
+ canSelectRow?: (row: T) => boolean;
555
+ };
556
+ /** Root element classes. Use `tableClassName` for column layout or minimum width. */
557
+ className?: string;
558
+ tableClassName?: string;
559
+ labels?: Partial<typeof labels>;
560
+ };
561
+ export declare function Table<T>({ data, columns, getRowId, title, emptyState, noResults, filtered, search, sorting, pagination, selection, className, tableClassName, labels: labelOverrides, ...props }: TableProps<T>): import("react").JSX.Element;
562
+ //#endregion
463
563
  //#region src/components/tabs/shared.d.ts
464
564
  type TabsVariant = 'line' | 'segmented';
465
565
  type TabsSize = 'sm' | 'md';
@@ -656,4 +756,4 @@ export declare function Toaster(props: ToasterProps): import("react").ReactPorta
656
756
  //#region src/lib/cn.d.ts
657
757
  export declare function cn(...inputs: ClassValue[]): string;
658
758
  //#endregion
659
- export type { BadgeProps, BadgeTone, ButtonProps, CardHeaderProps, CardProps, CheckboxGroupProps, CheckboxProps, CopyInputProps, DialogCloseReason, DialogHeaderProps, DialogProps, DialogSize, DrawerCloseProps, DrawerCloseReason, DrawerHeaderProps, DrawerProps, DrawerSide, DrawerSize, InputProps, MeterProps, MeterSegment, MeterTone, PasswordInputProps, SearchInputProps, SelectGroup, SelectOption, SelectProps, SidebarGroupProps, SidebarItemProps, SidebarProps, SidebarTriggerProps, SwitchProps, SwitchSize, TabListProps, TabNavLinkProps, TabNavProps, TabPanelProps, TabProps, TabsActivationMode, TabsProps, TabsSize, TabsVariant, Theme, ThemeSwitcherProps, ToastActionButton, ToastOptions, ToastPatch, ToastPosition, ToastPromiseOptions, ToastType, ToasterProps, TooltipProps, UseThemeOptions };
759
+ export type { AlertProps, AlertTone, BadgeProps, BadgeTone, ButtonProps, CardHeaderProps, CardProps, CheckboxGroupProps, CheckboxProps, CopyInputProps, DialogCloseReason, DialogHeaderProps, DialogProps, DialogSize, DrawerCloseProps, DrawerCloseReason, DrawerHeaderProps, DrawerProps, DrawerSide, DrawerSize, InputProps, MeterProps, MeterSegment, MeterTone, PasswordInputProps, SearchInputProps, SelectGroup, SelectOption, SelectProps, SidebarGroupProps, SidebarItemProps, SidebarProps, SidebarTriggerProps, SwitchProps, SwitchSize, TabListProps, TabNavLinkProps, TabNavProps, TabPanelProps, TabProps, TableColumn, TableProps, TableSort, TabsActivationMode, TabsProps, TabsSize, TabsVariant, Theme, ThemeSwitcherProps, ToastActionButton, ToastOptions, ToastPatch, ToastPosition, ToastPromiseOptions, ToastType, ToasterProps, TooltipProps, UseThemeOptions };