@charcuterie/ui 3.34.0 → 4.1.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.
Files changed (87) hide show
  1. package/dist/ActionTiles/ActionTiles.d.ts +66 -8
  2. package/dist/ActionTiles/ActionTiles.d.ts.map +1 -1
  3. package/dist/ActionTiles/ActionTiles.js +88 -11
  4. package/dist/ActionTiles/ActionTiles.js.map +1 -1
  5. package/dist/Main/Main.d.ts +18 -19
  6. package/dist/Main/Main.d.ts.map +1 -1
  7. package/dist/Main/Main.js +21 -10
  8. package/dist/Main/Main.js.map +1 -1
  9. package/dist/Main/ScrollMemoryProvider.d.ts +38 -0
  10. package/dist/Main/ScrollMemoryProvider.d.ts.map +1 -0
  11. package/dist/Main/ScrollMemoryProvider.js +30 -0
  12. package/dist/Main/ScrollMemoryProvider.js.map +1 -0
  13. package/dist/Main/scrollMemory.d.ts +54 -28
  14. package/dist/Main/scrollMemory.d.ts.map +1 -1
  15. package/dist/Main/scrollMemory.js +81 -40
  16. package/dist/Main/scrollMemory.js.map +1 -1
  17. package/dist/Main/scrollMemoryContext.d.ts +18 -0
  18. package/dist/Main/scrollMemoryContext.d.ts.map +1 -0
  19. package/dist/Main/scrollMemoryContext.js +18 -0
  20. package/dist/Main/scrollMemoryContext.js.map +1 -0
  21. package/dist/Main/useScrollMemory.d.ts +20 -18
  22. package/dist/Main/useScrollMemory.d.ts.map +1 -1
  23. package/dist/Main/useScrollMemory.js +69 -42
  24. package/dist/Main/useScrollMemory.js.map +1 -1
  25. package/dist/PortraitTiles/PortraitAvatar.d.ts +21 -0
  26. package/dist/PortraitTiles/PortraitAvatar.d.ts.map +1 -0
  27. package/dist/PortraitTiles/PortraitAvatar.js +29 -0
  28. package/dist/PortraitTiles/PortraitAvatar.js.map +1 -0
  29. package/dist/PortraitTiles/PortraitTiles.d.ts +162 -0
  30. package/dist/PortraitTiles/PortraitTiles.d.ts.map +1 -0
  31. package/dist/PortraitTiles/PortraitTiles.js +263 -0
  32. package/dist/PortraitTiles/PortraitTiles.js.map +1 -0
  33. package/dist/PortraitTiles/portraitColour.d.ts +49 -0
  34. package/dist/PortraitTiles/portraitColour.d.ts.map +1 -0
  35. package/dist/PortraitTiles/portraitColour.js +55 -0
  36. package/dist/PortraitTiles/portraitColour.js.map +1 -0
  37. package/dist/categoricalStyles.d.ts +27 -0
  38. package/dist/categoricalStyles.d.ts.map +1 -1
  39. package/dist/categoricalStyles.js +49 -0
  40. package/dist/categoricalStyles.js.map +1 -1
  41. package/dist/index.d.ts +7 -2
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +3 -1
  44. package/dist/index.js.map +1 -1
  45. package/dist/reactRouter/ReactRouterAdapter.d.ts +43 -0
  46. package/dist/reactRouter/ReactRouterAdapter.d.ts.map +1 -0
  47. package/dist/reactRouter/ReactRouterAdapter.js +50 -0
  48. package/dist/reactRouter/ReactRouterAdapter.js.map +1 -0
  49. package/dist/reactRouter/index.d.ts +14 -8
  50. package/dist/reactRouter/index.d.ts.map +1 -1
  51. package/dist/reactRouter/index.js +13 -8
  52. package/dist/reactRouter/index.js.map +1 -1
  53. package/dist/tileStyles.d.ts +15 -0
  54. package/dist/tileStyles.d.ts.map +1 -1
  55. package/dist/tileStyles.js +26 -1
  56. package/dist/tileStyles.js.map +1 -1
  57. package/package.json +2 -2
  58. package/src/ActionTiles/ActionTiles.mdx +64 -6
  59. package/src/ActionTiles/ActionTiles.stories.tsx +107 -2
  60. package/src/ActionTiles/ActionTiles.test.tsx +205 -5
  61. package/src/ActionTiles/ActionTiles.tsx +274 -110
  62. package/src/Main/Main.mdx +35 -7
  63. package/src/Main/Main.stories.tsx +129 -64
  64. package/src/Main/Main.test.tsx +58 -0
  65. package/src/Main/Main.tsx +20 -20
  66. package/src/Main/ScrollMemoryProvider.tsx +48 -0
  67. package/src/Main/scrollMemory.test.ts +70 -16
  68. package/src/Main/scrollMemory.ts +98 -40
  69. package/src/Main/scrollMemoryContext.ts +21 -0
  70. package/src/Main/useScrollMemory.ts +83 -43
  71. package/src/PortraitTiles/PortraitAvatar.tsx +49 -0
  72. package/src/PortraitTiles/PortraitTiles.mdx +153 -0
  73. package/src/PortraitTiles/PortraitTiles.stories.tsx +403 -0
  74. package/src/PortraitTiles/PortraitTiles.test.tsx +399 -0
  75. package/src/PortraitTiles/PortraitTiles.tsx +577 -0
  76. package/src/PortraitTiles/portraitColour.test.ts +39 -0
  77. package/src/PortraitTiles/portraitColour.ts +67 -0
  78. package/src/RadioGroup/RadioGroup.stories.tsx +39 -0
  79. package/src/RouterLink/RouterLink.mdx +53 -16
  80. package/src/categoricalStyles.ts +57 -0
  81. package/src/index.ts +12 -0
  82. package/src/reactRouter/ReactRouterAdapter.stories.tsx +120 -0
  83. package/src/reactRouter/ReactRouterAdapter.test.tsx +92 -0
  84. package/src/reactRouter/ReactRouterAdapter.tsx +66 -0
  85. package/src/reactRouter/index.ts +14 -8
  86. package/src/sourceRules.test.ts +19 -3
  87. package/src/tileStyles.ts +30 -1
@@ -1,73 +1,131 @@
1
1
  /**
2
- * What the browser will not remember for you.
2
+ * What the browser will not remember for you, and the one thing it
3
+ * would never have got wrong.
3
4
  *
4
- * `Shell` gives `<main>` the page's only vertical scrollport, so
5
- * the header and the rails stay put while the content moves —
6
- * that is the whole reason `Shell.test.tsx` asserts "only main
7
- * scrolls vertically, keeping the rail available".
8
- *
9
- * It costs exactly one thing, and this module is it. A browser
10
- * remembers **the document scroller's** offset per history entry
11
- * and puts it back on Back. It does that for a cross-document
12
- * load, and `history.scrollRestoration` is the switch for it. It
13
- * does **not** do it for an arbitrary element with
14
- * `overflow-y: auto`, and it does not do it at all for a
15
- * same-document `pushState` navigation, which is every link press
16
- * in a single-page app. So a page that scrolls its own `<main>`
17
- * loses the reader's place on Back, and no browser setting brings
18
- * it back:
5
+ * `Shell` gives `<main>` the page's only vertical scrollport, so the
6
+ * header and the rails stay put while the content moves — that is
7
+ * what `Shell.test.tsx` asserts. It costs exactly one thing, and
8
+ * this module is it. A browser remembers **the document scroller's**
9
+ * offset per history entry and puts it back on Back. It does not do
10
+ * that for an arbitrary element with `overflow-y: auto`, and it does
11
+ * not do it at all for a same-document `pushState` navigation, which
12
+ * is every link press in a single-page app:
19
13
  *
20
14
  * > *"If I scroll, click on an item, then go back, it resets me to
21
15
  * > the top. […] It should remember where the scroll position was
22
16
  * > previously. Isn't this handled by Chrome already?"*
23
17
  *
24
- * The offsets are held **in memory**, not in `sessionStorage`. A
25
- * history key survives a reload, but the DOM it described does
26
- * not: the app re-fetches, re-measures and often re-orders, so a
27
- * pixel offset from before the reload points at a different row.
28
- * Restoring a stale number is worse than restoring nothing,
29
- * because it looks deliberate.
18
+ * ## An entry remembers its PATH as well as its offset
19
+ *
20
+ * Not decoration — the second field is what keeps this from being a
21
+ * worse bug than the one it fixes. A history entry is not a page. A
22
+ * filter chip, an expanded group, a selected tab: each writes a
23
+ * search param, and **`setSearchParams` navigates**, so each mints a
24
+ * history entry the memory has never seen. Treating an unseen entry
25
+ * as "start at the top" therefore threw the reader to the top of the
26
+ * list every time they opened a group — which is exactly what
27
+ * shipped, and what the path comparison exists to prevent.
28
+ *
29
+ * So the rule is about the PAGE, not the entry:
30
+ *
31
+ * - **An entry we have seen** → its remembered offset. This is the
32
+ * whole feature: Back and Forward land where you left them.
33
+ * - **A new entry on the SAME path** → leave the scrollport alone.
34
+ * A search param changed; the reader did not go anywhere. This is
35
+ * also precisely what the app did before this memory existed, so
36
+ * it can never be a regression.
37
+ * - **A new entry on a DIFFERENT path** → the top. A page you have
38
+ * not seen starts at its beginning, which is what a browser does
39
+ * for a document.
40
+ *
41
+ * ## The offsets are in memory, never `sessionStorage`
42
+ *
43
+ * A history key survives a reload; the DOM it described does not.
44
+ * The app re-fetches, re-measures and often re-orders, so a pixel
45
+ * offset from before the reload points at a different row. Restoring
46
+ * a stale number is worse than restoring nothing, because it looks
47
+ * deliberate.
30
48
  */
31
49
 
32
50
  /**
33
51
  * How many history entries keep an offset. A reader goes back
34
52
  * through a handful of screens, never fifty, so this is a ceiling
35
53
  * against a long session rather than a working set — the map holds
36
- * one small number per entry and evicts the least recently
37
- * written.
54
+ * one small record per entry and evicts the least recently written.
38
55
  */
39
56
  const REMEMBERED_ENTRY_LIMIT = 50
40
57
 
41
- const rememberedOffsets = new Map<string, number>()
58
+ export type ScrollEntry = {
59
+ /** The history entry — react-router's `useLocation().key`. */
60
+ key: string
61
+ /** The page it belongs to — `useLocation().pathname`. */
62
+ path: string
63
+ }
64
+
65
+ type RememberedEntry = {
66
+ offset: number
67
+ path: string
68
+ }
69
+
70
+ const rememberedEntries = new Map<string, RememberedEntry>()
71
+
72
+ /**
73
+ * The entry the scrollport is showing. Module state rather than a
74
+ * ref, because the question it answers — *did the reader change page
75
+ * or only a search param?* — is asked by the incoming entry's effect
76
+ * about the OUTGOING one, and by then the outgoing effect has
77
+ * already been cleaned up.
78
+ */
79
+ let currentEntry: ScrollEntry | null = null
42
80
 
43
81
  export const rememberScrollOffset = (
44
- key: string,
82
+ entry: ScrollEntry,
45
83
  offset: number,
46
84
  ) => {
47
- // Delete before set, so insertion order is least-recent-first
48
- // and the eviction below takes the right one. A plain `set` on
49
- // an existing key keeps its original position.
50
- rememberedOffsets.delete(key)
51
- rememberedOffsets.set(key, offset)
85
+ // Delete before set, so insertion order is least-recent-first and
86
+ // the eviction below takes the right one. A plain `set` on an
87
+ // existing key keeps its original position.
88
+ rememberedEntries.delete(entry.key)
89
+ rememberedEntries.set(entry.key, {
90
+ offset,
91
+ path: entry.path,
92
+ })
52
93
 
53
- for (const oldestKey of rememberedOffsets.keys()) {
54
- if (rememberedOffsets.size <= REMEMBERED_ENTRY_LIMIT) {
94
+ for (const oldestKey of rememberedEntries.keys()) {
95
+ if (rememberedEntries.size <= REMEMBERED_ENTRY_LIMIT) {
55
96
  break
56
97
  }
57
98
 
58
- rememberedOffsets.delete(oldestKey)
99
+ rememberedEntries.delete(oldestKey)
59
100
  }
60
101
  }
61
102
 
62
103
  /**
63
- * `0` for an entry nobody has scrolled, which is also the right
64
- * answer for an entry nobody has *seen* — a link press lands at
65
- * the top of the new page.
104
+ * Where the scrollport should go for this entry, or `null` for
105
+ * **leave it alone** — which is a third answer rather than a missing
106
+ * one, and the difference between a search param and a page.
66
107
  */
67
- export const recallScrollOffset = (key: string) =>
68
- rememberedOffsets.get(key) ?? 0
108
+ export const resolveScrollOffset = (entry: ScrollEntry) => {
109
+ const remembered = rememberedEntries.get(entry.key)
110
+
111
+ if (remembered) {
112
+ return remembered.offset
113
+ }
114
+
115
+ const isSamePage =
116
+ currentEntry !== null &&
117
+ currentEntry.path === entry.path
118
+
119
+ return isSamePage ? null : 0
120
+ }
121
+
122
+ /** Called by the hook once it owns the scrollport for this entry. */
123
+ export const enterScrollEntry = (entry: ScrollEntry) => {
124
+ currentEntry = entry
125
+ }
69
126
 
70
127
  /** Test-only. Module state outlives a mount; a test must not. */
71
128
  export const forgetScrollOffsets = () => {
72
- rememberedOffsets.clear()
129
+ rememberedEntries.clear()
130
+ currentEntry = null
73
131
  }
@@ -0,0 +1,21 @@
1
+ import { createContext } from "react"
2
+
3
+ import type { ScrollEntry } from "./scrollMemory.ts"
4
+
5
+ /**
6
+ * How the history entry reaches `Main` without `Main` knowing a
7
+ * router exists.
8
+ *
9
+ * `@charcuterie/ui`'s main entry is router-free and stays that way —
10
+ * `sourceRules.test.ts` asserts it, and the fleet runs five
11
+ * different routers. So the entry arrives as a plain
12
+ * `{ key, path }`, and the one component that knows how to read it
13
+ * from react-router lives in the `./react-router` subpath.
14
+ *
15
+ * A context rather than a prop, because the alternative is a prop
16
+ * every app has to remember on every `Main` it renders. It was a
17
+ * prop first, exactly once, and three of the four apps that needed
18
+ * it did not have it.
19
+ */
20
+ export const ScrollMemoryContext =
21
+ createContext<ScrollEntry | null>(null)
@@ -1,8 +1,10 @@
1
1
  import { useCallback, useLayoutEffect, useRef } from "react"
2
2
 
3
3
  import {
4
- recallScrollOffset,
4
+ enterScrollEntry,
5
5
  rememberScrollOffset,
6
+ resolveScrollOffset,
7
+ type ScrollEntry,
6
8
  } from "./scrollMemory.ts"
7
9
 
8
10
  /**
@@ -11,11 +13,11 @@ import {
11
13
  *
12
14
  * The offset cannot be applied until the scrollport is tall enough
13
15
  * to hold it, and on a client-rendered page it is not: the commit
14
- * that changes the route draws a skeleton, and the rows land a
15
- * fetch later. So the restore re-applies as the content grows, and
16
- * gives up after this. A page that takes longer than three seconds
17
- * to fill has already been read from the top, and moving it then
18
- * is a worse answer than leaving it where the reader put it.
16
+ * that changes the route draws a skeleton, and the rows land a fetch
17
+ * later. So the restore re-applies as the content grows, and gives
18
+ * up after this. A page that takes longer than three seconds to fill
19
+ * has already been read from the top, and moving it then is a worse
20
+ * answer than leaving it where the reader put it.
19
21
  */
20
22
  const RESTORE_WINDOW_MS = 3000
21
23
 
@@ -28,41 +30,42 @@ const READER_INTENT_EVENTS = [
28
30
  ] as const
29
31
 
30
32
  /**
31
- * Remember where this history entry was scrolled to, and put it
32
- * back when the reader returns to it.
33
+ * Remember where this history entry was scrolled to, and put it back
34
+ * when the reader returns to it.
33
35
  *
34
- * `scrollMemory.ts` says why the browser will not. This is the
35
- * part that has to survive the two things a real page does:
36
+ * `scrollMemory.ts` says why the browser will not, and what an entry
37
+ * that has never been seen should do. This is the part that has to
38
+ * survive the two things a real page does:
36
39
  *
37
40
  * 1. **The content arrives late.** An offset applied against a
38
- * 600px skeleton clamps to `0` and looks like it did nothing.
39
- * A `ResizeObserver` on the scrollport and on its content
40
- * column re-applies the offset every time either grows, until
41
- * the offset lands or the window closes.
42
- * 2. **The reader scrolls during all that.** Any wheel, touch,
43
- * key or pointer press ends the restore immediately. The
44
- * alternative is a page that jumps out from under somebody who
45
- * has already started reading.
41
+ * 600px skeleton clamps to `0` and looks like it did nothing. A
42
+ * `ResizeObserver` on the scrollport and on its content column
43
+ * re-applies the offset every time either grows, until the
44
+ * offset lands or the window closes.
45
+ * 2. **The reader scrolls during all that.** Any wheel, touch, key
46
+ * or pointer press ends the restore immediately. The alternative
47
+ * is a page that jumps out from under somebody who has already
48
+ * started reading.
46
49
  *
47
50
  * The save is a passive `scroll` listener, suppressed while a
48
51
  * restore is in flight. Without that suppression the feature eats
49
52
  * itself: writing `scrollTop` on a still-short page clamps to `0`
50
- * and fires a `scroll` event reading `0`, which would be saved
51
- * over the very offset being restored.
53
+ * and fires a `scroll` event reading `0`, which would be saved over
54
+ * the very offset being restored.
52
55
  *
53
- * @param scrollKey Identifies the history entry — react-router's
54
- * `useLocation().key`. `undefined` turns the memory off, which is
55
- * what an app with no router gets.
56
+ * @param entry The history entry and the page it belongs to.
57
+ * `null` turns the memory off, which is what an app that renders
58
+ * no `ScrollMemoryProvider` gets.
56
59
  */
57
60
  export const useScrollMemory = (
58
- scrollKey: string | undefined,
61
+ entry: ScrollEntry | null | undefined,
59
62
  ) => {
60
63
  const elementRef = useRef<HTMLElement | null>(null)
61
64
  const isRestoringRef = useRef(false)
62
65
 
63
- // A ref rather than a query for the `<main>` id: the effect
64
- // below runs in the same commit that mounts the node, and only a
65
- // ref can say which node that was.
66
+ // A ref rather than a query for the `<main>` id: the effect below
67
+ // runs in the same commit that mounts the node, and only a ref can
68
+ // say which node that was.
66
69
  //
67
70
  // `unknown` in, narrowed here, so the returned function is both a
68
71
  // `RefCallback<HTMLElement>` and something `mergeRefs` accepts —
@@ -73,18 +76,43 @@ export const useScrollMemory = (
73
76
  node instanceof HTMLElement ? node : null
74
77
  }, [])
75
78
 
79
+ const key = entry?.key
80
+ const path = entry?.path
81
+
76
82
  useLayoutEffect(() => {
77
83
  const element = elementRef.current
78
84
 
79
- if (!element || scrollKey === undefined) {
85
+ if (
86
+ !element ||
87
+ key === undefined ||
88
+ path === undefined
89
+ ) {
80
90
  return undefined
81
91
  }
82
92
 
83
- const offset = recallScrollOffset(scrollKey)
84
-
85
- // The arrow defers the read of `applyOffset`, so the three
86
- // pieces can be declared in the order they depend on each
87
- // other rather than in the order they are used.
93
+ const currentEntry = { key, path }
94
+
95
+ // `null` means leave the scrollport alone: a search param
96
+ // changed and the reader did not go anywhere. It is read BEFORE
97
+ // this entry becomes the current one, because the answer depends
98
+ // on the page the reader is arriving from.
99
+ const offset = resolveScrollOffset(currentEntry)
100
+
101
+ enterScrollEntry(currentEntry)
102
+
103
+ // Seed the entry now rather than waiting for a `scroll` event.
104
+ // A reader who opens a group and then follows a link never
105
+ // scrolls in between, so without this the entry they came from
106
+ // is one the memory has never seen — and Back would send them to
107
+ // the top of a list they were half-way down.
108
+ rememberScrollOffset(
109
+ currentEntry,
110
+ offset ?? element.scrollTop,
111
+ )
112
+
113
+ // The arrow defers the read of `applyOffset`, so the pieces can
114
+ // be declared in the order they depend on each other rather than
115
+ // in the order they are used.
88
116
  const observer = new ResizeObserver(() => {
89
117
  applyOffset()
90
118
  })
@@ -93,12 +121,9 @@ export const useScrollMemory = (
93
121
  stopRestoring()
94
122
  }, RESTORE_WINDOW_MS)
95
123
 
96
- const stopRestoring = () => {
97
- if (!isRestoringRef.current) {
98
- return
99
- }
124
+ let isRestoring = offset !== null
100
125
 
101
- isRestoringRef.current = false
126
+ const teardown = () => {
102
127
  observer.disconnect()
103
128
  globalThis.clearTimeout(deadline)
104
129
 
@@ -110,8 +135,18 @@ export const useScrollMemory = (
110
135
  }
111
136
  }
112
137
 
138
+ const stopRestoring = () => {
139
+ if (!isRestoring) {
140
+ return
141
+ }
142
+
143
+ isRestoring = false
144
+ isRestoringRef.current = false
145
+ teardown()
146
+ }
147
+
113
148
  const applyOffset = () => {
114
- if (!isRestoringRef.current) {
149
+ if (!isRestoring || offset === null) {
115
150
  return
116
151
  }
117
152
 
@@ -128,10 +163,10 @@ export const useScrollMemory = (
128
163
  return
129
164
  }
130
165
 
131
- rememberScrollOffset(scrollKey, element.scrollTop)
166
+ rememberScrollOffset(currentEntry, element.scrollTop)
132
167
  }
133
168
 
134
- isRestoringRef.current = true
169
+ isRestoringRef.current = isRestoring
135
170
 
136
171
  for (const eventName of READER_INTENT_EVENTS) {
137
172
  element.addEventListener(eventName, stopRestoring, {
@@ -155,10 +190,15 @@ export const useScrollMemory = (
155
190
  applyOffset()
156
191
 
157
192
  return () => {
158
- stopRestoring()
193
+ // Unconditional, unlike `stopRestoring` — a restore that
194
+ // already landed still has an observer and four listeners on
195
+ // an element the next entry is about to use, and a stale
196
+ // observer that survives its effect applies a stale offset.
197
+ isRestoring = false
198
+ teardown()
159
199
  element.removeEventListener("scroll", onScroll)
160
200
  }
161
- }, [scrollKey])
201
+ }, [key, path])
162
202
 
163
203
  return setElement
164
204
  }
@@ -0,0 +1,49 @@
1
+ import type { ReactNode } from "react"
2
+ import { useState } from "react"
3
+
4
+ /**
5
+ * The face, and the one piece of this component that holds state.
6
+ *
7
+ * A portrait grid is the shape that cannot absorb a broken image:
8
+ * a missing `<img>` is a torn hole where a person's face was, next
9
+ * to nine that rendered. `onError` is the only signal a browser
10
+ * gives for a 404, a wrong content type, or a file the user's
11
+ * network stripped — so the fallback the caller already supplied for
12
+ * "no picture" covers "no picture *today*" as well.
13
+ *
14
+ * Its own component because the state is per portrait. Hoisting it
15
+ * to a `Record<string, boolean>` in the set would re-render every
16
+ * tile whenever any one image failed.
17
+ */
18
+ export const PortraitAvatar = ({
19
+ className,
20
+ imageSrc,
21
+ initials,
22
+ }: {
23
+ className: string
24
+ imageSrc: string | undefined
25
+ initials: ReactNode
26
+ }): ReactNode => {
27
+ const [isImageBroken, setIsImageBroken] = useState(false)
28
+
29
+ if (imageSrc === undefined || isImageBroken) {
30
+ return (
31
+ <span aria-hidden className={className}>
32
+ {initials}
33
+ </span>
34
+ )
35
+ }
36
+
37
+ return (
38
+ <img
39
+ // The name is beside it, inside the same tile, so an alt here
40
+ // is the name announced twice.
41
+ alt=""
42
+ className={className}
43
+ onError={() => {
44
+ setIsImageBroken(true)
45
+ }}
46
+ src={imageSrc}
47
+ />
48
+ )
49
+ }
@@ -0,0 +1,153 @@
1
+ import { Canvas, Controls, Meta } from "@storybook/addon-docs/blocks";
2
+
3
+ import * as PortraitTilesStories from "./PortraitTiles.stories";
4
+
5
+ <Meta of={PortraitTilesStories} />
6
+
7
+ # PortraitTiles
8
+
9
+ A set of **people** — or of anything else picked by its face — drawn as round pictures with a
10
+ name and one big number.
11
+
12
+ points-market's "Who's shopping?" is the shape this is taken from. It had been hand-rolled
13
+ there since the app was built, and it is the third tile shape the fleet grew on its own,
14
+ after mux-magic's and Gallery Downloader's "Pick a tool" cards.
15
+
16
+ <Canvas of={PortraitTilesStories.Default} />
17
+
18
+ ## Props
19
+
20
+ <Controls />
21
+
22
+ ## Why this is not an `ActionTiles` variant
23
+
24
+ The two answer different questions.
25
+
26
+ - [`ActionTiles`](/docs/components-actions-actiontiles--docs) asks **what do you want to
27
+ do** — a name, a line of help, an icon that illustrates a verb.
28
+ - This asks **who is this** — a face, and one number that decides it. There is no
29
+ description, because a person is not explained.
30
+
31
+ Collapsing them would mean one component whose `icon` is sometimes a 20px glyph beside the
32
+ title and sometimes a 144px circle above it, with `hint` meaning "what this does" in one mode
33
+ and "points" in the other. That is two components wearing one name.
34
+
35
+ What they **do** share is the box: `tileStyles.ts` holds the border, the radius, the surface
36
+ and the `auto-fill` grid, and both read it. A page carrying both does not show two different
37
+ cards.
38
+
39
+ ## It scales, and the scaling is the point
40
+
41
+ Two axes carry it, and neither is a pixel count in an app.
42
+
43
+ **The container.** `layout="auto"` is a row while the set is narrow and a column once it has
44
+ room, off a `cq-sm` (24rem) query on the set itself. Nothing reads the viewport, so a picker
45
+ in a 320px sidebar on a 2560px monitor gets the row form — which is right, and which a media
46
+ query would have got wrong.
47
+
48
+ **The density.** Every length is a token, so `data-density="kiosk"` grows the type ramp
49
+ underneath it: `text-2xl` is 30px at `comfortable` and 38px at `kiosk`. points-market runs
50
+ `kiosk` today, and that is where its big numbers come from.
51
+
52
+ <Canvas of={PortraitTilesStories.Responsive} />
53
+
54
+ The container and the element querying it **cannot be the same box** — a container query
55
+ matches descendants only. The set renders a wrapper for that reason, the same arrangement
56
+ `EmptyState` uses, and `className` stays on the outer element where a consumer expects it.
57
+
58
+ <Canvas of={PortraitTilesStories.AllVariants} />
59
+
60
+ ## The colour is the person's, not the position's
61
+
62
+ A portrait takes the next hue from the ten-wide
63
+ [categorical palette](/docs/foundations-colour--docs) when it names none, which costs a call
64
+ site nothing and is the right default for a list assembled on the fly.
65
+
66
+ It is the **wrong** default for a household. Add a fifth member and the first four change
67
+ colour — and their colour was the thing the picker was being read by, faster than the names
68
+ were. Any set whose subjects persist should store a `categorical` per subject and pass it,
69
+ the same call Docket makes for a project.
70
+
71
+ `categorical` and not `intent`: an intent is a claim the design system makes — `danger` says
72
+ what happens if you press the thing — and a person is not a warning.
73
+
74
+ <Canvas of={PortraitTilesStories.NamedHues} />
75
+
76
+ ### When the colour is not ours to choose
77
+
78
+ Some colours are not a design decision at all. points-market colours each child to match the
79
+ **NFC card they physically tap**, by a settled household decision, so the picker on the wall
80
+ and the card in their hand have to agree. Ten audited hues cannot promise that, and
81
+ re-theming the colour would be re-theming the card.
82
+
83
+ That is what `color` is for, and it is the same line
84
+ [`Swatch`](/docs/components-data-swatch--docs) draws: a colour that arrived from the world is
85
+ data, not a token. It reaches the paint through custom properties on the tile, because
86
+ Tailwind generates its CSS at build time from source text and a colour out of a database is
87
+ not in the source.
88
+
89
+ Three things happen to that one colour, and only the first uses it raw.
90
+
91
+ - The **fill** is the colour, exactly.
92
+ - The **initials** are black or white, whichever the fill can carry — which is why the third
93
+ portrait below has white letters and the other two have black. Pass six-digit hex:
94
+ `getReadableTextColour` measures hex and nothing else, and falls back to white on anything
95
+ it cannot parse rather than throwing inside your render.
96
+ - The **number** is the colour mixed toward the scheme's own text colour, so a pale card
97
+ colour is still readable on a pale surface — and, in the dark scheme, is pulled the other
98
+ way instead.
99
+
100
+ Reach for this arm only when the colour is a **fact about the subject**. A colour that is
101
+ merely a choice is `categorical`, and it gets re-theming and a contrast guarantee for free.
102
+
103
+ <Canvas of={PortraitTilesStories.DataColours} />
104
+
105
+ ## A picture, and what happens when it 404s
106
+
107
+ `imageSrc` draws a photo; `initials` draws a letter on the hue's solid fill. A portrait grid
108
+ is the shape that cannot absorb a broken image — a missing `<img>` is a torn hole where a
109
+ face was, next to three that loaded — so a load error falls back to the initials the caller
110
+ already supplied.
111
+
112
+ The `<img>` is `alt=""` on purpose. The name is beside it inside the same tile, so an alt
113
+ here is the name announced twice.
114
+
115
+ <Canvas of={PortraitTilesStories.WithPictures} />
116
+
117
+ ## The keyboard is the platform's
118
+
119
+ `href` makes a portrait a real `<a href>`, routed through the injected `RouterLink` for a
120
+ same-origin destination — so middle-click, ctrl-click, "open in a new tab" and "copy link
121
+ address" come from the element rather than from the paint. Without one it is a `<button>`
122
+ that calls `onChoose`. A set may mix the two.
123
+
124
+ Tab reaches **each** portrait. There is no roving tabindex, for the same reason `ActionTiles`
125
+ has none: a radio group borrows one tab stop because exactly one of its options is true at a
126
+ time, and nothing here is true.
127
+
128
+ The group takes a required `label` naming the set — "Who's shopping?" — and `stat` is drawn
129
+ inside the tile with its `hint` so both are part of the accessible name. "1,240" on its own
130
+ tells a screen-reader user nothing.
131
+
132
+ <Canvas of={PortraitTilesStories.AllStates} />
133
+
134
+ ## The lift, and who does not get it
135
+
136
+ Hovering raises the portrait and casts a shadow. It is the one piece of motion in this
137
+ library, kept because it is the thing the owner pointed at in points-market when this moved
138
+ in here.
139
+
140
+ `motion-reduce:hover:translate-y-0` turns the movement off for anyone who has asked the OS
141
+ for less. The shadow and the coloured border stay, so the hover is still legible without it.
142
+
143
+ <Canvas of={PortraitTilesStories.Interactive} />
144
+
145
+ ## What this deliberately does not do
146
+
147
+ - **Hold a selection.** Nothing here is checked; the page moved on. A picker that keeps a
148
+ value is `RadioGroup itemShape="tile"`.
149
+ - **Guarantee contrast on a `color` you supply.** The palette arm is audited in both schemes;
150
+ the data arm cannot be, because the colour is not ours. The initials are picked to suit the
151
+ fill and the number is pulled toward readable, and that is the whole of what it can promise.
152
+ - **Crop or focus a face.** `object-fit: cover` centres, and that is all. An app that needs a
153
+ focal point owns the crop.