@tristan2828/ui-foundation 4.123.0 → 4.124.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/conventions/docs/cell-patterns.md +105 -41
- package/conventions/docs/design-language.md +33 -24
- package/dist/components/app/color-field.d.ts +22 -0
- package/dist/components/app/color-field.js +84 -0
- package/dist/components/ui/radio-group.d.ts +5 -0
- package/dist/components/ui/radio-group.js +11 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/lib/color-choices.d.ts +31 -0
- package/dist/lib/color-choices.js +57 -0
- package/package.json +1 -1
|
@@ -3,47 +3,48 @@
|
|
|
3
3
|
<!-- index: written by `npm run docs:index` in the package from the headings below; never edit it by hand -->
|
|
4
4
|
## Index
|
|
5
5
|
|
|
6
|
-
- [The rules that apply to every cell](#the-rules-that-apply-to-every-cell) · lines
|
|
7
|
-
- [1. Plain text](#1-plain-text) · lines
|
|
8
|
-
- [2. Icon + label](#2-icon--label) · lines
|
|
9
|
-
- [3. Icon in a badge](#3-icon-in-a-badge) · lines
|
|
10
|
-
- [4. Icon only, label in a tooltip](#4-icon-only-label-in-a-tooltip) · lines
|
|
11
|
-
- [Several values: the icons side by side, every name in one tooltip](#several-values-the-icons-side-by-side-every-name-in-one-tooltip) · lines
|
|
12
|
-
- [5. Enum → tone-mapped badge](#5-enum--tone-mapped-badge) · lines
|
|
13
|
-
- [6. Multi-value enum, clustered](#6-multi-value-enum-clustered) · lines
|
|
14
|
-
- [7. Link as an outline button](#7-link-as-an-outline-button) · lines
|
|
15
|
-
- [8. Date, relative](#8-date-relative) · lines
|
|
16
|
-
- [9. Ordinal scale (bounded integer)](#9-ordinal-scale-bounded-integer) · lines
|
|
17
|
-
- [Three variants the package draws: a step bar, a star and a score](#three-variants-the-package-draws-a-step-bar-a-star-and-a-score) · lines
|
|
18
|
-
- [10. Summary in the cell, detail in a popover](#10-summary-in-the-cell-detail-in-a-popover) · lines
|
|
19
|
-
- [A second example: what a row depends on](#a-second-example-what-a-row-depends-on) · lines
|
|
20
|
-
- [11. Number or currency — Unproven](#11-number-or-currency--unproven) · lines
|
|
21
|
-
- [12. Boolean](#12-boolean) · lines
|
|
22
|
-
- [13. Progress bar — Unproven](#13-progress-bar--unproven) · lines
|
|
23
|
-
- [The compact form: `ProgressRing`](#the-compact-form-progressring) · lines
|
|
24
|
-
- [14. Avatar and name — Unproven](#14-avatar-and-name--unproven) · lines
|
|
25
|
-
- [15. Yes/no you flip in the row](#15-yesno-you-flip-in-the-row) · lines
|
|
26
|
-
- [A variant: a pressed icon inside another value's cell](#a-variant-a-pressed-icon-inside-another-values-cell) · lines
|
|
27
|
-
- [16. Title linking to the record's view](#16-title-linking-to-the-records-view) · lines
|
|
28
|
-
- [17. Quick action on the view](#17-quick-action-on-the-view) · lines
|
|
29
|
-
- [18. Editable value](#18-editable-value) · lines
|
|
30
|
-
- [Presets: one line per field](#presets-one-line-per-field) · lines
|
|
31
|
-
- [Several fields edited as one](#several-fields-edited-as-one) · lines
|
|
32
|
-
- [A link and its label](#a-link-and-its-label) · lines
|
|
33
|
-
- [A list of sub-items](#a-list-of-sub-items) · lines
|
|
34
|
-
- [In a table's rows](#in-a-tables-rows) · lines
|
|
35
|
-
- [A table of a few short columns: `width="content"`](#a-table-of-a-few-short-columns-widthcontent) · lines
|
|
36
|
-
- [19. Stage circle for an ordered status](#19-stage-circle-for-an-ordered-status) · lines
|
|
37
|
-
- [20. Count linking to the related records](#20-count-linking-to-the-related-records) · lines
|
|
38
|
-
- [21. Rows grouped under headings, and a pill filter](#21-rows-grouped-under-headings-and-a-pill-filter) · lines
|
|
39
|
-
- [22. A highlighted row, and rows that are done](#22-a-highlighted-row-and-rows-that-are-done) · lines
|
|
40
|
-
- [24. A list's rows: hover, lines, rows per page](#24-a-lists-rows-hover-lines-rows-per-page) · lines
|
|
41
|
-
- [23. A Group menu, and a record read beside the list](#23-a-group-menu-and-a-record-read-beside-the-list) · lines
|
|
42
|
-
- [24. A stored colour beside its name](#24-a-stored-colour-beside-its-name) · lines
|
|
43
|
-
- [
|
|
44
|
-
|
|
45
|
-
- [
|
|
46
|
-
- [
|
|
6
|
+
- [The rules that apply to every cell](#the-rules-that-apply-to-every-cell) · lines 76–99
|
|
7
|
+
- [1. Plain text](#1-plain-text) · lines 101–156
|
|
8
|
+
- [2. Icon + label](#2-icon--label) · lines 158–181
|
|
9
|
+
- [3. Icon in a badge](#3-icon-in-a-badge) · lines 183–196
|
|
10
|
+
- [4. Icon only, label in a tooltip](#4-icon-only-label-in-a-tooltip) · lines 198–275
|
|
11
|
+
- [Several values: the icons side by side, every name in one tooltip](#several-values-the-icons-side-by-side-every-name-in-one-tooltip) · lines 223–275
|
|
12
|
+
- [5. Enum → tone-mapped badge](#5-enum--tone-mapped-badge) · lines 277–297
|
|
13
|
+
- [6. Multi-value enum, clustered](#6-multi-value-enum-clustered) · lines 299–311
|
|
14
|
+
- [7. Link as an outline button](#7-link-as-an-outline-button) · lines 313–334
|
|
15
|
+
- [8. Date, relative](#8-date-relative) · lines 336–373
|
|
16
|
+
- [9. Ordinal scale (bounded integer)](#9-ordinal-scale-bounded-integer) · lines 375–434
|
|
17
|
+
- [Three variants the package draws: a step bar, a star and a score](#three-variants-the-package-draws-a-step-bar-a-star-and-a-score) · lines 393–434
|
|
18
|
+
- [10. Summary in the cell, detail in a popover](#10-summary-in-the-cell-detail-in-a-popover) · lines 436–486
|
|
19
|
+
- [A second example: what a row depends on](#a-second-example-what-a-row-depends-on) · lines 454–486
|
|
20
|
+
- [11. Number or currency — Unproven](#11-number-or-currency--unproven) · lines 488–528
|
|
21
|
+
- [12. Boolean](#12-boolean) · lines 530–560
|
|
22
|
+
- [13. Progress bar — Unproven](#13-progress-bar--unproven) · lines 562–598
|
|
23
|
+
- [The compact form: `ProgressRing`](#the-compact-form-progressring) · lines 582–598
|
|
24
|
+
- [14. Avatar and name — Unproven](#14-avatar-and-name--unproven) · lines 600–617
|
|
25
|
+
- [15. Yes/no you flip in the row](#15-yesno-you-flip-in-the-row) · lines 619–728
|
|
26
|
+
- [A variant: a pressed icon inside another value's cell](#a-variant-a-pressed-icon-inside-another-values-cell) · lines 645–728
|
|
27
|
+
- [16. Title linking to the record's view](#16-title-linking-to-the-records-view) · lines 730–776
|
|
28
|
+
- [17. Quick action on the view](#17-quick-action-on-the-view) · lines 778–851
|
|
29
|
+
- [18. Editable value](#18-editable-value) · lines 853–1556
|
|
30
|
+
- [Presets: one line per field](#presets-one-line-per-field) · lines 1188–1256
|
|
31
|
+
- [Several fields edited as one](#several-fields-edited-as-one) · lines 1258–1294
|
|
32
|
+
- [A link and its label](#a-link-and-its-label) · lines 1296–1320
|
|
33
|
+
- [A list of sub-items](#a-list-of-sub-items) · lines 1322–1455
|
|
34
|
+
- [In a table's rows](#in-a-tables-rows) · lines 1457–1542
|
|
35
|
+
- [A table of a few short columns: `width="content"`](#a-table-of-a-few-short-columns-widthcontent) · lines 1544–1556
|
|
36
|
+
- [19. Stage circle for an ordered status](#19-stage-circle-for-an-ordered-status) · lines 1558–1624
|
|
37
|
+
- [20. Count linking to the related records](#20-count-linking-to-the-related-records) · lines 1626–1690
|
|
38
|
+
- [21. Rows grouped under headings, and a pill filter](#21-rows-grouped-under-headings-and-a-pill-filter) · lines 1692–1790
|
|
39
|
+
- [22. A highlighted row, and rows that are done](#22-a-highlighted-row-and-rows-that-are-done) · lines 1792–1821
|
|
40
|
+
- [24. A list's rows: hover, lines, rows per page](#24-a-lists-rows-hover-lines-rows-per-page) · lines 1823–1861
|
|
41
|
+
- [23. A Group menu, and a record read beside the list](#23-a-group-menu-and-a-record-read-beside-the-list) · lines 1863–1939
|
|
42
|
+
- [24. A stored colour beside its name](#24-a-stored-colour-beside-its-name) · lines 1941–2027
|
|
43
|
+
- [Picking one on a form: `ColorField`](#picking-one-on-a-form-colorfield) · lines 1966–2027
|
|
44
|
+
- [25. A list's tools: views, filters, columns, row actions, a phone's rows](#25-a-lists-tools-views-filters-columns-row-actions-a-phones-rows) · lines 2029–2254
|
|
45
|
+
- [The framed look, and sibling lists beside the title](#the-framed-look-and-sibling-lists-beside-the-title) · lines 2183–2254
|
|
46
|
+
- [26. A record's related list, capped](#26-a-records-related-list-capped) · lines 2256–2292
|
|
47
|
+
- [27. A repeat and its next time](#27-a-repeat-and-its-next-time) · lines 2294–2343
|
|
47
48
|
|
|
48
49
|
<!-- /index -->
|
|
49
50
|
|
|
@@ -1962,6 +1963,69 @@ import { ColorSwatch } from '@tristan2828/ui-foundation'
|
|
|
1962
1963
|
The package's fixture `app/ColorSwatch` "In a table" renders it; its
|
|
1963
1964
|
checks measure the ring at 3:1 in both themes and every palette.
|
|
1964
1965
|
|
|
1966
|
+
### Picking one on a form: `ColorField`
|
|
1967
|
+
|
|
1968
|
+
The same colour on the record's form (4.124): a grid of named swatches
|
|
1969
|
+
and **Custom**, which opens the browser's own colour picker. One control
|
|
1970
|
+
for a react-hook-form `Controller`, like `DateField`.
|
|
1971
|
+
|
|
1972
|
+
```tsx
|
|
1973
|
+
import { ColorField, storedColor } from '@tristan2828/ui-foundation'
|
|
1974
|
+
|
|
1975
|
+
<Field data-invalid={!!errors.color || undefined}>
|
|
1976
|
+
<FieldLabel id="label-color-label" htmlFor="label-color">Colour</FieldLabel>
|
|
1977
|
+
<Controller
|
|
1978
|
+
control={form.control}
|
|
1979
|
+
name="color"
|
|
1980
|
+
rules={{ validate: (value) => !value || !!storedColor(value) || 'Pick a colour' }}
|
|
1981
|
+
render={({ field, fieldState }) => (
|
|
1982
|
+
<ColorField
|
|
1983
|
+
id="label-color"
|
|
1984
|
+
aria-labelledby="label-color-label"
|
|
1985
|
+
value={field.value}
|
|
1986
|
+
onChange={field.onChange}
|
|
1987
|
+
onBlur={field.onBlur}
|
|
1988
|
+
invalid={fieldState.invalid}
|
|
1989
|
+
/>
|
|
1990
|
+
)}
|
|
1991
|
+
/>
|
|
1992
|
+
<FieldError errors={[errors.color]} />
|
|
1993
|
+
</Field>
|
|
1994
|
+
```
|
|
1995
|
+
|
|
1996
|
+
- **It's one radio group.** One tab stop; the arrow keys move and pick;
|
|
1997
|
+
Tab goes on to Custom's picker, then out. Each swatch's name is its
|
|
1998
|
+
radio's label, written beside it (the word carries it, as above). The
|
|
1999
|
+
picked one shows its radio's dot and a primary border, not only its
|
|
2000
|
+
colour.
|
|
2001
|
+
- **Name the group with `aria-labelledby`** (the `FieldLabel`'s `id`): a
|
|
2002
|
+
label's `htmlFor` can't name a group. Still pass `id` and `htmlFor`, so
|
|
2003
|
+
`<FieldLabel required>` marks the group `aria-required`.
|
|
2004
|
+
- **The value out is always `storedColor`'s form** (`#c0392b`). A value in
|
|
2005
|
+
is compared in full, so `C0392B` or `#FFF` still shows as its named
|
|
2006
|
+
colour. One that's none of the named colours (a hex typed in the old
|
|
2007
|
+
text box) shows **Custom** picked, its hex beside it, and stays as it
|
|
2008
|
+
is until someone picks. A value that isn't a colour shows nothing
|
|
2009
|
+
picked.
|
|
2010
|
+
- **Custom by pointer opens the picker; by keyboard it only picks**, and
|
|
2011
|
+
the picker is the next tab stop. Picking Custom keeps the current
|
|
2012
|
+
colour until a colour is chosen in the picker.
|
|
2013
|
+
- **The default set** is `COLOR_CHOICES`: twelve named hues at a middle
|
|
2014
|
+
lightness that read on a card in light, dark and every palette (3:1,
|
|
2015
|
+
2.3:1 on Palm LCD's mid panel). An app with its own names passes
|
|
2016
|
+
`colors={[{ name: 'Hale Navy', color: '#434c56' }, ...]}`; each name is
|
|
2017
|
+
shown, so make them words a person would say.
|
|
2018
|
+
- **Replacing a text box and a swatch preview**: the stored values don't
|
|
2019
|
+
change. Swap the `Input` for `ColorField` in the same `Controller`;
|
|
2020
|
+
values already stored that aren't in the set show as Custom.
|
|
2021
|
+
|
|
2022
|
+
The package's fixture `app/ColorField` renders it on a card (a named
|
|
2023
|
+
colour, a Custom hex, a required one left empty, an app's own list, a
|
|
2024
|
+
refused value); its checks run axe, the token check, each swatch's ring
|
|
2025
|
+
and the picker's edge at 3:1, and each default fill against the card, in
|
|
2026
|
+
both themes and every palette. `e2e/color-field.spec.ts` drives the
|
|
2027
|
+
pointer, the keyboard and Custom.
|
|
2028
|
+
|
|
1965
2029
|
## 25. A list's tools: views, filters, columns, row actions, a phone's rows
|
|
1966
2030
|
|
|
1967
2031
|
The rest of the list page standard (4.86): what sits above a list's rows,
|
|
@@ -14,30 +14,30 @@
|
|
|
14
14
|
- [Categorical colour](#categorical-colour) · lines 370–440
|
|
15
15
|
- [Heat scale](#heat-scale) · lines 442–503
|
|
16
16
|
- [Depth ramps](#depth-ramps) · lines 505–590
|
|
17
|
-
- [A stored colour](#a-stored-colour) · lines 592–
|
|
18
|
-
- [Shapes and sizes](#shapes-and-sizes) · lines
|
|
19
|
-
- [Typography](#typography) · lines
|
|
20
|
-
- [Words on screen: Title Case and sentence case](#words-on-screen-title-case-and-sentence-case) · lines
|
|
21
|
-
- [Written text (notes)](#written-text-notes) · lines
|
|
22
|
-
- [Density](#density) · lines
|
|
23
|
-
- [Rows on this device](#rows-on-this-device) · lines
|
|
24
|
-
- [Widget cards (hub pages)](#widget-cards-hub-pages) · lines
|
|
25
|
-
- [Module cards (a hub's places)](#module-cards-a-hubs-places) · lines
|
|
26
|
-
- [Notices](#notices) · lines
|
|
27
|
-
- [A value over time](#a-value-over-time) · lines
|
|
28
|
-
- [Several values over time](#several-values-over-time) · lines
|
|
29
|
-
- [A list as a board](#a-list-as-a-board) · lines
|
|
30
|
-
- [A list as cards](#a-list-as-cards) · lines
|
|
31
|
-
- [Key numbers on a record](#key-numbers-on-a-record) · lines
|
|
32
|
-
- [Parts of the page standards](#parts-of-the-page-standards) · lines
|
|
33
|
-
- [A few equal things](#a-few-equal-things) · lines
|
|
34
|
-
- [An overview page](#an-overview-page) · lines
|
|
35
|
-
- [A record's header and empty values](#a-records-header-and-empty-values) · lines
|
|
36
|
-
- [A record's tabs, mark and facts](#a-records-tabs-mark-and-facts) · lines
|
|
37
|
-
- [Picking at random](#picking-at-random) · lines
|
|
38
|
-
- [Filling in what's missing](#filling-in-whats-missing) · lines
|
|
39
|
-
- [Values edited in place: the presets' looks](#values-edited-in-place-the-presets-looks) · lines
|
|
40
|
-
- [The sidebar and the phone's bar](#the-sidebar-and-the-phones-bar) · lines
|
|
17
|
+
- [A stored colour](#a-stored-colour) · lines 592–649
|
|
18
|
+
- [Shapes and sizes](#shapes-and-sizes) · lines 651–672
|
|
19
|
+
- [Typography](#typography) · lines 674–845
|
|
20
|
+
- [Words on screen: Title Case and sentence case](#words-on-screen-title-case-and-sentence-case) · lines 708–741
|
|
21
|
+
- [Written text (notes)](#written-text-notes) · lines 743–845
|
|
22
|
+
- [Density](#density) · lines 847–940
|
|
23
|
+
- [Rows on this device](#rows-on-this-device) · lines 913–940
|
|
24
|
+
- [Widget cards (hub pages)](#widget-cards-hub-pages) · lines 942–1056
|
|
25
|
+
- [Module cards (a hub's places)](#module-cards-a-hubs-places) · lines 1058–1135
|
|
26
|
+
- [Notices](#notices) · lines 1137–1165
|
|
27
|
+
- [A value over time](#a-value-over-time) · lines 1167–1253
|
|
28
|
+
- [Several values over time](#several-values-over-time) · lines 1216–1253
|
|
29
|
+
- [A list as a board](#a-list-as-a-board) · lines 1255–1306
|
|
30
|
+
- [A list as cards](#a-list-as-cards) · lines 1308–1354
|
|
31
|
+
- [Key numbers on a record](#key-numbers-on-a-record) · lines 1356–1392
|
|
32
|
+
- [Parts of the page standards](#parts-of-the-page-standards) · lines 1394–1464
|
|
33
|
+
- [A few equal things](#a-few-equal-things) · lines 1466–1550
|
|
34
|
+
- [An overview page](#an-overview-page) · lines 1552–1623
|
|
35
|
+
- [A record's header and empty values](#a-records-header-and-empty-values) · lines 1624–1687
|
|
36
|
+
- [A record's tabs, mark and facts](#a-records-tabs-mark-and-facts) · lines 1689–1813
|
|
37
|
+
- [Picking at random](#picking-at-random) · lines 1815–1846
|
|
38
|
+
- [Filling in what's missing](#filling-in-whats-missing) · lines 1848–1915
|
|
39
|
+
- [Values edited in place: the presets' looks](#values-edited-in-place-the-presets-looks) · lines 1917–1942
|
|
40
|
+
- [The sidebar and the phone's bar](#the-sidebar-and-the-phones-bar) · lines 1944–2040
|
|
41
41
|
|
|
42
42
|
<!-- /index -->
|
|
43
43
|
|
|
@@ -639,6 +639,15 @@ palette side by side), with axe, the token check and the ring's ratio
|
|
|
639
639
|
measured in light, dark and every palette. Cell pattern 24 is it in a
|
|
640
640
|
column.
|
|
641
641
|
|
|
642
|
+
**Picking one on a form is `ColorField`** (4.124): the same swatches,
|
|
643
|
+
each beside its name, as one radio group, and Custom for the browser's
|
|
644
|
+
own picker. Its default twelve (`COLOR_CHOICES`) sit at a middle
|
|
645
|
+
lightness, so each fill clears 3:1 against a card in light, dark and the
|
|
646
|
+
dark palettes, and 2.3:1 on Palm LCD, whose mid-light panel no middle
|
|
647
|
+
colour clears 3:1 on (the ring shows the edge there). The picked one is
|
|
648
|
+
marked by its radio's dot and a primary border, never by its colour
|
|
649
|
+
alone. Cell pattern 24 has its markup.
|
|
650
|
+
|
|
642
651
|
## Shapes and sizes
|
|
643
652
|
|
|
644
653
|
The shared style guide's, the same in every app with or without a
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ColorChoice } from '../../lib/color-choices.js';
|
|
2
|
+
export type ColorFieldProps = {
|
|
3
|
+
/** The radio group's id, for the field's `<FieldLabel htmlFor>` (its `required` marks the group). */
|
|
4
|
+
id?: string;
|
|
5
|
+
/** The stored colour: a hex, with or without `#`, in 3, 4, 6 or 8 digits. Missing or invalid picks nothing. */
|
|
6
|
+
value: string | null | undefined;
|
|
7
|
+
/** Called with the picked colour in `storedColor`'s form (`#c0392b`). */
|
|
8
|
+
onChange: (value: string) => void;
|
|
9
|
+
/** Called when focus leaves the whole field (react-hook-form's `onBlur`). */
|
|
10
|
+
onBlur?: () => void;
|
|
11
|
+
/** The named colours offered, in order. Defaults to `COLOR_CHOICES`, twelve that read on a card everywhere. */
|
|
12
|
+
colors?: readonly ColorChoice[];
|
|
13
|
+
/** The value was refused: `aria-invalid` on the group and a destructive outline. */
|
|
14
|
+
invalid?: boolean;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
className?: string;
|
|
17
|
+
/** Name the group: the FieldLabel's id (a label's `htmlFor` can't name a group). */
|
|
18
|
+
'aria-labelledby'?: string;
|
|
19
|
+
'aria-label'?: string;
|
|
20
|
+
'aria-describedby'?: string;
|
|
21
|
+
};
|
|
22
|
+
export declare function ColorField({ id, value, onChange, onBlur, colors, invalid, disabled, className, ...aria }: ColorFieldProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// A stored colour on a form (a label's colour, a calendar's): a grid of
|
|
3
|
+
// named swatches and Custom, one radio group, for a react-hook-form
|
|
4
|
+
// `Controller` like DateField. The value is a hex string, always
|
|
5
|
+
// `storedColor`'s form (`#c0392b`), and the design language's "A stored
|
|
6
|
+
// colour" is the why of the swatches themselves (ColorSwatch).
|
|
7
|
+
//
|
|
8
|
+
// - One radio group (shadcn's RadioGroup on Base UI): one tab stop, the
|
|
9
|
+
// arrow keys move and pick, Space picks. Each choice is a radio with its
|
|
10
|
+
// ColorSwatch, and the swatch's name is the radio's label, written out:
|
|
11
|
+
// the colour is never the only way to tell. The picked one shows its
|
|
12
|
+
// radio's dot and a primary border, so it's marked by more than its
|
|
13
|
+
// colour; the focused one wears the focus ring.
|
|
14
|
+
// - Custom is the last choice. Clicking it opens the browser's own colour
|
|
15
|
+
// picker (`<input type="color">`, beside it, which is also a button of
|
|
16
|
+
// its own); reaching it by the keyboard only picks it, and Tab goes on to
|
|
17
|
+
// the picker. The chosen hex is written beside it.
|
|
18
|
+
// - A value that's none of the named colours (a hex typed elsewhere,
|
|
19
|
+
// uppercase or 3-digit included) shows Custom picked, with its hex.
|
|
20
|
+
// Colours compare in full (`#FFF` is `#ffffff`), so a named colour stored
|
|
21
|
+
// another way still shows as its name. A value that isn't a colour
|
|
22
|
+
// (missing, empty, a typo) shows nothing picked.
|
|
23
|
+
// - The swatch fills are data (ColorSwatch's inline style); everything
|
|
24
|
+
// around them is semantic tokens.
|
|
25
|
+
import { useRef, useState } from 'react';
|
|
26
|
+
import { cn } from 'cn';
|
|
27
|
+
import { RadioGroup, RadioGroupItem } from '../ui/radio-group.js';
|
|
28
|
+
import { choiceIndex, COLOR_CHOICES, pickerHex } from '../../lib/color-choices.js';
|
|
29
|
+
import { storedColor } from '../../lib/stored-color.js';
|
|
30
|
+
import { ColorSwatch } from './color-swatch.js';
|
|
31
|
+
const CUSTOM = 'custom';
|
|
32
|
+
// A choice's box: the radio and its swatch, a primary border when picked
|
|
33
|
+
// (the dot says it too), the focus ring on the one the keyboard is on.
|
|
34
|
+
const CHOICE = cn('flex h-(--control-height,--spacing(8)) min-w-0 cursor-pointer items-center gap-2 rounded-md border border-transparent px-2 type-body text-foreground', 'hover:bg-muted has-data-checked:border-primary has-data-checked:bg-primary/12', 'has-[:focus-visible]:border-ring has-[:focus-visible]:ring-3 has-[:focus-visible]:ring-ring/50', 'has-data-disabled:cursor-not-allowed has-data-disabled:opacity-50');
|
|
35
|
+
export function ColorField({ id, value, onChange, onBlur, colors = COLOR_CHOICES, invalid = false, disabled = false, className, ...aria }) {
|
|
36
|
+
const picker = useRef(null);
|
|
37
|
+
// Custom picked by the person while the value is still a named colour:
|
|
38
|
+
// it stays picked until they pick a named one.
|
|
39
|
+
const [customPicked, setCustomPicked] = useState(false);
|
|
40
|
+
const index = choiceIndex(value, colors);
|
|
41
|
+
const hex = storedColor(value);
|
|
42
|
+
const picked = customPicked || (index === -1 && hex) ? CUSTOM : index === -1 ? null : String(index);
|
|
43
|
+
const choose = (next) => {
|
|
44
|
+
if (next === CUSTOM) {
|
|
45
|
+
setCustomPicked(true);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const choice = colors[Number(next)];
|
|
49
|
+
const color = storedColor(choice?.color);
|
|
50
|
+
if (!color)
|
|
51
|
+
return;
|
|
52
|
+
setCustomPicked(false);
|
|
53
|
+
if (color !== hex)
|
|
54
|
+
onChange(color);
|
|
55
|
+
};
|
|
56
|
+
// A pointer's click on Custom (its radio or its word) opens the picker;
|
|
57
|
+
// a label's forwarded click and the keyboard's (`detail` 0) don't.
|
|
58
|
+
const openPicker = (event) => {
|
|
59
|
+
if (disabled || event.detail === 0)
|
|
60
|
+
return;
|
|
61
|
+
const input = picker.current;
|
|
62
|
+
if (!input)
|
|
63
|
+
return;
|
|
64
|
+
try {
|
|
65
|
+
input.showPicker();
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
input.click();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const leave = (event) => {
|
|
72
|
+
if (!event.currentTarget.contains(event.relatedTarget))
|
|
73
|
+
onBlur?.();
|
|
74
|
+
};
|
|
75
|
+
return (_jsx("div", { "data-slot": "color-field", className: cn('min-w-0', className), onBlur: leave, children: _jsxs(RadioGroup, { id: id, value: picked, onValueChange: choose, disabled: disabled, "aria-invalid": invalid || undefined, className: cn('grid grid-cols-[repeat(auto-fill,minmax(7.5rem,1fr))] gap-1 rounded-md', invalid && 'ring-3 ring-destructive/20 dark:ring-destructive/40'), ...aria, children: [colors.map((choice, at) => (_jsxs("label", { "data-slot": "color-field-choice", className: CHOICE, children: [_jsx(RadioGroupItem, { value: String(at), "aria-invalid": invalid || undefined }), _jsx(ColorSwatch, { color: choice.color, label: choice.name })] }, `${choice.name}-${at}`))), _jsxs("div", { "data-slot": "color-field-custom", className: "col-span-full flex min-w-0 flex-wrap items-center gap-2", children: [_jsxs("label", { className: cn(CHOICE, 'w-fit'), onClick: openPicker, children: [_jsx(RadioGroupItem, { value: CUSTOM, "aria-invalid": invalid || undefined }), "Custom"] }), _jsx("input", { ref: picker, type: "color", "data-slot": "color-field-picker", "aria-label": "Custom colour", value: pickerHex(value), disabled: disabled, onChange: (event) => {
|
|
76
|
+
const color = storedColor(event.target.value);
|
|
77
|
+
if (!color)
|
|
78
|
+
return;
|
|
79
|
+
setCustomPicked(true);
|
|
80
|
+
onChange(color);
|
|
81
|
+
},
|
|
82
|
+
// The radio group's arrow keys are for its radios, not this button.
|
|
83
|
+
onKeyDown: (event) => event.stopPropagation(), className: cn('box-border size-(--control-height,--spacing(8)) shrink-0 cursor-pointer rounded-md border border-muted-foreground bg-transparent p-0.5 outline-none', 'focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50', '[&::-webkit-color-swatch-wrapper]:p-0 [&::-webkit-color-swatch]:rounded-sm [&::-webkit-color-swatch]:border-0 [&::-moz-color-swatch]:rounded-sm [&::-moz-color-swatch]:border-0') }), picked === CUSTOM && hex && (_jsx("span", { "data-slot": "color-field-hex", className: "type-body text-muted-foreground tabular-nums", children: hex }))] })] }) }));
|
|
84
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Radio as RadioPrimitive } from "@base-ui/react/radio";
|
|
2
|
+
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group";
|
|
3
|
+
declare function RadioGroup({ className, ...props }: RadioGroupPrimitive.Props): import("react").JSX.Element;
|
|
4
|
+
declare function RadioGroupItem({ className, ...props }: RadioPrimitive.Root.Props): import("react").JSX.Element;
|
|
5
|
+
export { RadioGroup, RadioGroupItem };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Radio as RadioPrimitive } from "@base-ui/react/radio";
|
|
3
|
+
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group";
|
|
4
|
+
import { cn } from "cn";
|
|
5
|
+
function RadioGroup({ className, ...props }) {
|
|
6
|
+
return (_jsx(RadioGroupPrimitive, { "data-slot": "radio-group", className: cn("grid w-full gap-2", className), ...props }));
|
|
7
|
+
}
|
|
8
|
+
function RadioGroupItem({ className, ...props }) {
|
|
9
|
+
return (_jsx(RadioPrimitive.Root, { "data-slot": "radio-group-item", className: cn("group/radio-group-item peer relative flex aspect-square size-4 shrink-0 rounded-full border border-input outline-none group-has-[:focus-visible]/field-label:ring-0 group-has-[:focus-visible]/field-label:not-data-checked:border-input after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 aria-invalid:aria-checked:border-primary dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 data-checked:border-primary data-checked:bg-primary data-checked:text-primary-foreground group-has-[:focus-visible]/field-label:data-checked:border-primary dark:data-checked:bg-primary", className), ...props, children: _jsx(RadioPrimitive.Indicator, { "data-slot": "radio-group-indicator", className: "flex size-4 items-center justify-center", children: _jsx("span", { className: "absolute top-1/2 left-1/2 size-2 -translate-x-1/2 -translate-y-1/2 rounded-full bg-primary-foreground" }) }) }));
|
|
10
|
+
}
|
|
11
|
+
export { RadioGroup, RadioGroupItem };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export { FillInField, FillInInput, FillInSelect, type FillInFieldKind, type Fill
|
|
|
8
8
|
export { Artwork, ArtworkTag, ArtworkToggle, type ArtworkAspect, type ArtworkProps, type ArtworkToggleProps, } from './components/app/artwork.js';
|
|
9
9
|
export { ColorSwatch, type ColorSwatchProps } from './components/app/color-swatch.js';
|
|
10
10
|
export { storedColor } from './lib/stored-color.js';
|
|
11
|
+
export { ColorField, type ColorFieldProps } from './components/app/color-field.js';
|
|
12
|
+
export { COLOR_CHOICES, type ColorChoice } from './lib/color-choices.js';
|
|
11
13
|
export { CountLink, type CountLinkProps } from './components/app/count-link.js';
|
|
12
14
|
export { DataTable, type DataTableCardLayout, type DataTableOpens, type DataTableOpensKind, type DataTableGrouping, type DataTableLook, type DataTableProps, type DataTableTotals, type DataTableWidth, type SortingState, } from './components/app/data-table.js';
|
|
13
15
|
export { type DataTableColumnsMenu, type DataTablePhoneRow, type DataTableRowAction, type DataTableView, type DataTableViewAll, } from './components/app/data-table-tools.js';
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,8 @@ export { FillInField, FillInInput, FillInSelect, } from './components/app/fill-i
|
|
|
12
12
|
export { Artwork, ArtworkTag, ArtworkToggle, } from './components/app/artwork.js';
|
|
13
13
|
export { ColorSwatch } from './components/app/color-swatch.js';
|
|
14
14
|
export { storedColor } from './lib/stored-color.js';
|
|
15
|
+
export { ColorField } from './components/app/color-field.js';
|
|
16
|
+
export { COLOR_CHOICES } from './lib/color-choices.js';
|
|
15
17
|
export { CountLink } from './components/app/count-link.js';
|
|
16
18
|
export { DataTable, } from './components/app/data-table.js';
|
|
17
19
|
export {} from './components/app/data-table-tools.js';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** One named colour a ColorField offers: its name is shown and is its accessible label. */
|
|
2
|
+
export type ColorChoice = {
|
|
3
|
+
name: string;
|
|
4
|
+
color: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* ColorField's default set: twelve hues at a middle lightness, so each fill
|
|
8
|
+
* clears 3:1 against `card` in light, dark and every dark palette, and 2.3:1
|
|
9
|
+
* on Palm LCD's mid-light panel (measured in e2e/fixtures-visual.spec.ts).
|
|
10
|
+
* The swatch's ring shows its edge on any surface either way. An app with
|
|
11
|
+
* its own names or colours passes its own `colors`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const COLOR_CHOICES: readonly ColorChoice[];
|
|
14
|
+
/**
|
|
15
|
+
* A stored colour in full: `storedColor`'s form with a 3- or 4-digit hex
|
|
16
|
+
* spelt out (`#fff` → `#ffffff`, `#fff8` → `#ffffff88`), so two spellings of
|
|
17
|
+
* one colour compare equal. `null` when it isn't a colour.
|
|
18
|
+
*/
|
|
19
|
+
export declare function fullHex(value: string | null | undefined): string | null;
|
|
20
|
+
/**
|
|
21
|
+
* The index of the choice a stored value is, or `-1` when it's none of them
|
|
22
|
+
* (Custom), compared in full so `#FFF`, `fff` and `#ffffff` are one colour.
|
|
23
|
+
* A value that isn't a colour (missing, empty, a typo) is `-1` too.
|
|
24
|
+
*/
|
|
25
|
+
export declare function choiceIndex(value: string | null | undefined, colors: readonly ColorChoice[]): number;
|
|
26
|
+
/**
|
|
27
|
+
* What `<input type="color">` can show for a stored value: `#rrggbb` only,
|
|
28
|
+
* so a short hex is spelt out and an alpha dropped. Black when there's no
|
|
29
|
+
* colour, the input's own default.
|
|
30
|
+
*/
|
|
31
|
+
export declare function pickerHex(value: string | null | undefined): string;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// The value handling behind ColorField: which of a set of named colours a
|
|
2
|
+
// stored value is, and what the browser's own colour picker can show. Its
|
|
3
|
+
// own file so it's tested without a browser (tests/color-field.test.tsx).
|
|
4
|
+
import { storedColor } from './stored-color.js';
|
|
5
|
+
/**
|
|
6
|
+
* ColorField's default set: twelve hues at a middle lightness, so each fill
|
|
7
|
+
* clears 3:1 against `card` in light, dark and every dark palette, and 2.3:1
|
|
8
|
+
* on Palm LCD's mid-light panel (measured in e2e/fixtures-visual.spec.ts).
|
|
9
|
+
* The swatch's ring shows its edge on any surface either way. An app with
|
|
10
|
+
* its own names or colours passes its own `colors`.
|
|
11
|
+
*/
|
|
12
|
+
export const COLOR_CHOICES = [
|
|
13
|
+
{ name: 'Red', color: '#dc2626' },
|
|
14
|
+
{ name: 'Orange', color: '#dd5208' },
|
|
15
|
+
{ name: 'Amber', color: '#c27006' },
|
|
16
|
+
{ name: 'Olive', color: '#5f8a12' },
|
|
17
|
+
{ name: 'Green', color: '#16883f' },
|
|
18
|
+
{ name: 'Teal', color: '#0d8a80' },
|
|
19
|
+
{ name: 'Cyan', color: '#0b86a8' },
|
|
20
|
+
{ name: 'Blue', color: '#2563eb' },
|
|
21
|
+
{ name: 'Indigo', color: '#5b5ee8' },
|
|
22
|
+
{ name: 'Purple', color: '#9b3fec' },
|
|
23
|
+
{ name: 'Pink', color: '#d62a74' },
|
|
24
|
+
{ name: 'Grey', color: '#737373' },
|
|
25
|
+
];
|
|
26
|
+
/**
|
|
27
|
+
* A stored colour in full: `storedColor`'s form with a 3- or 4-digit hex
|
|
28
|
+
* spelt out (`#fff` → `#ffffff`, `#fff8` → `#ffffff88`), so two spellings of
|
|
29
|
+
* one colour compare equal. `null` when it isn't a colour.
|
|
30
|
+
*/
|
|
31
|
+
export function fullHex(value) {
|
|
32
|
+
const hex = storedColor(value);
|
|
33
|
+
if (!hex)
|
|
34
|
+
return null;
|
|
35
|
+
if (hex.length > 5)
|
|
36
|
+
return hex;
|
|
37
|
+
return `#${[...hex.slice(1)].map((digit) => digit + digit).join('')}`;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The index of the choice a stored value is, or `-1` when it's none of them
|
|
41
|
+
* (Custom), compared in full so `#FFF`, `fff` and `#ffffff` are one colour.
|
|
42
|
+
* A value that isn't a colour (missing, empty, a typo) is `-1` too.
|
|
43
|
+
*/
|
|
44
|
+
export function choiceIndex(value, colors) {
|
|
45
|
+
const hex = fullHex(value);
|
|
46
|
+
if (!hex)
|
|
47
|
+
return -1;
|
|
48
|
+
return colors.findIndex((choice) => fullHex(choice.color) === hex);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* What `<input type="color">` can show for a stored value: `#rrggbb` only,
|
|
52
|
+
* so a short hex is spelt out and an alpha dropped. Black when there's no
|
|
53
|
+
* colour, the input's own default.
|
|
54
|
+
*/
|
|
55
|
+
export function pickerHex(value) {
|
|
56
|
+
return fullHex(value)?.slice(0, 7) ?? '#000000';
|
|
57
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tristan2828/ui-foundation",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.124.0",
|
|
4
4
|
"description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|