@canonical/prism 0.0.0-stage → 0.51.0-experimental.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 (191) hide show
  1. package/README.md +357 -2
  2. package/dist/esm/index.js +22 -0
  3. package/dist/esm/lib/Card/Card.js +16 -0
  4. package/dist/esm/lib/Card/CardView.js +19 -0
  5. package/dist/esm/lib/Card/index.js +2 -0
  6. package/dist/esm/lib/Card/styles.css +68 -0
  7. package/dist/esm/lib/Card/types.js +1 -0
  8. package/dist/esm/lib/Chip/Chip.js +16 -0
  9. package/dist/esm/lib/Chip/ChipView.js +14 -0
  10. package/dist/esm/lib/Chip/index.js +2 -0
  11. package/dist/esm/lib/Chip/styles.css +11 -0
  12. package/dist/esm/lib/Chip/types.js +1 -0
  13. package/dist/esm/lib/CommandPalette/CommandPalette.js +166 -0
  14. package/dist/esm/lib/CommandPalette/ThingOptions.js +38 -0
  15. package/dist/esm/lib/CommandPalette/index.js +1 -0
  16. package/dist/esm/lib/CommandPalette/styles.css +91 -0
  17. package/dist/esm/lib/CommandPalette/types.js +1 -0
  18. package/dist/esm/lib/Fact/FactValue.js +12 -0
  19. package/dist/esm/lib/Fact/index.js +1 -0
  20. package/dist/esm/lib/Fact/types.js +1 -0
  21. package/dist/esm/lib/Navigation/Navigation.js +46 -0
  22. package/dist/esm/lib/Navigation/index.js +1 -0
  23. package/dist/esm/lib/Navigation/styles.css +44 -0
  24. package/dist/esm/lib/Navigation/types.js +1 -0
  25. package/dist/esm/lib/PageHeader/PageHeader.js +16 -0
  26. package/dist/esm/lib/PageHeader/PageHeaderView.js +18 -0
  27. package/dist/esm/lib/PageHeader/index.js +2 -0
  28. package/dist/esm/lib/PageHeader/readParents.js +19 -0
  29. package/dist/esm/lib/PageHeader/styles.css +67 -0
  30. package/dist/esm/lib/PageHeader/types.js +1 -0
  31. package/dist/esm/lib/Prism/Prism.js +14 -0
  32. package/dist/esm/lib/Prism/context.js +10 -0
  33. package/dist/esm/lib/Prism/index.js +2 -0
  34. package/dist/esm/lib/Prism/types.js +1 -0
  35. package/dist/esm/lib/Prose/Prose.js +26 -0
  36. package/dist/esm/lib/Prose/findMention.js +22 -0
  37. package/dist/esm/lib/Prose/index.js +2 -0
  38. package/dist/esm/lib/Prose/styles.css +10 -0
  39. package/dist/esm/lib/Prose/types.js +1 -0
  40. package/dist/esm/lib/Row/Row.js +16 -0
  41. package/dist/esm/lib/Row/RowView.js +18 -0
  42. package/dist/esm/lib/Row/index.js +2 -0
  43. package/dist/esm/lib/Row/styles.css +28 -0
  44. package/dist/esm/lib/Row/types.js +1 -0
  45. package/dist/esm/lib/SearchBoundary/SearchBoundary.js +37 -0
  46. package/dist/esm/lib/SearchBoundary/index.js +1 -0
  47. package/dist/esm/lib/SearchBoundary/types.js +1 -0
  48. package/dist/esm/lib/SearchPage/SearchPage.js +35 -0
  49. package/dist/esm/lib/SearchPage/index.js +1 -0
  50. package/dist/esm/lib/SearchPage/styles.css +62 -0
  51. package/dist/esm/lib/SearchPage/types.js +1 -0
  52. package/dist/esm/lib/SiteLayout/SiteLayout.js +20 -0
  53. package/dist/esm/lib/SiteLayout/index.js +1 -0
  54. package/dist/esm/lib/SiteLayout/styles.css +112 -0
  55. package/dist/esm/lib/SiteLayout/types.js +1 -0
  56. package/dist/esm/lib/ThingLink/ThingLink.js +10 -0
  57. package/dist/esm/lib/ThingLink/index.js +1 -0
  58. package/dist/esm/lib/ThingLink/types.js +1 -0
  59. package/dist/esm/lib/ThingResults/ThingList.js +11 -0
  60. package/dist/esm/lib/ThingResults/ThingResults.js +36 -0
  61. package/dist/esm/lib/ThingResults/index.js +1 -0
  62. package/dist/esm/lib/ThingResults/types.js +1 -0
  63. package/dist/esm/lib/graphql/PrismCard.js +35 -0
  64. package/dist/esm/lib/graphql/PrismChip.js +17 -0
  65. package/dist/esm/lib/graphql/PrismClasses.js +33 -0
  66. package/dist/esm/lib/graphql/PrismFact.js +17 -0
  67. package/dist/esm/lib/graphql/PrismPageHeader.js +59 -0
  68. package/dist/esm/lib/graphql/PrismRow.js +32 -0
  69. package/dist/esm/lib/graphql/__generated__/PrismCard_node.graphql.js +136 -0
  70. package/dist/esm/lib/graphql/__generated__/PrismChip_node.graphql.js +64 -0
  71. package/dist/esm/lib/graphql/__generated__/PrismClasses_node.graphql.js +92 -0
  72. package/dist/esm/lib/graphql/__generated__/PrismFact_fact.graphql.js +46 -0
  73. package/dist/esm/lib/graphql/__generated__/PrismPageHeader_node.graphql.js +237 -0
  74. package/dist/esm/lib/graphql/__generated__/PrismRow_node.graphql.js +127 -0
  75. package/dist/esm/lib/graphql/index.js +6 -0
  76. package/dist/esm/lib/lens/constants.js +2 -0
  77. package/dist/esm/lib/lens/index.js +3 -0
  78. package/dist/esm/lib/lens/isShortcutKey.js +22 -0
  79. package/dist/esm/lib/lens/readLenses.js +106 -0
  80. package/dist/esm/lib/lens/types.js +1 -0
  81. package/dist/esm/lib/projection/findNearest.js +12 -0
  82. package/dist/esm/lib/projection/index.js +5 -0
  83. package/dist/esm/lib/projection/orderClasses.js +14 -0
  84. package/dist/esm/lib/projection/readHomes.js +43 -0
  85. package/dist/esm/lib/projection/types.js +1 -0
  86. package/dist/esm/lib/projection/useProjection.js +34 -0
  87. package/dist/esm/lib/projection/withLabel.js +9 -0
  88. package/dist/esm/lib/renderPage/index.js +1 -0
  89. package/dist/esm/lib/renderPage/renderPage.js +125 -0
  90. package/dist/esm/lib/renderPage/types.js +1 -0
  91. package/dist/esm/lib/search/filterLenses.js +11 -0
  92. package/dist/esm/lib/search/index.js +3 -0
  93. package/dist/esm/lib/search/searchVariables.js +11 -0
  94. package/dist/esm/lib/search/warmSearch.js +14 -0
  95. package/dist/esm/lib/searchQuery/PrismSearchQuery.js +23 -0
  96. package/dist/esm/lib/searchQuery/__generated__/PrismSearchQuery.graphql.js +304 -0
  97. package/dist/esm/lib/searchQuery/index.js +1 -0
  98. package/dist/esm/lib/warmQuery/index.js +1 -0
  99. package/dist/esm/lib/warmQuery/warmQuery.js +43 -0
  100. package/dist/esm/server.js +6 -0
  101. package/dist/types/index.d.ts +24 -0
  102. package/dist/types/lib/Card/Card.d.ts +8 -0
  103. package/dist/types/lib/Card/CardView.d.ts +11 -0
  104. package/dist/types/lib/Card/index.d.ts +3 -0
  105. package/dist/types/lib/Card/types.d.ts +31 -0
  106. package/dist/types/lib/Chip/Chip.d.ts +8 -0
  107. package/dist/types/lib/Chip/ChipView.d.ts +9 -0
  108. package/dist/types/lib/Chip/index.d.ts +3 -0
  109. package/dist/types/lib/Chip/types.d.ts +24 -0
  110. package/dist/types/lib/CommandPalette/CommandPalette.d.ts +16 -0
  111. package/dist/types/lib/CommandPalette/ThingOptions.d.ts +7 -0
  112. package/dist/types/lib/CommandPalette/index.d.ts +2 -0
  113. package/dist/types/lib/CommandPalette/types.d.ts +45 -0
  114. package/dist/types/lib/Fact/FactValue.d.ts +7 -0
  115. package/dist/types/lib/Fact/index.d.ts +2 -0
  116. package/dist/types/lib/Fact/types.d.ts +5 -0
  117. package/dist/types/lib/Navigation/Navigation.d.ts +15 -0
  118. package/dist/types/lib/Navigation/index.d.ts +2 -0
  119. package/dist/types/lib/Navigation/types.d.ts +6 -0
  120. package/dist/types/lib/PageHeader/PageHeader.d.ts +8 -0
  121. package/dist/types/lib/PageHeader/PageHeaderView.d.ts +10 -0
  122. package/dist/types/lib/PageHeader/index.d.ts +3 -0
  123. package/dist/types/lib/PageHeader/readParents.d.ts +13 -0
  124. package/dist/types/lib/PageHeader/types.d.ts +32 -0
  125. package/dist/types/lib/Prism/Prism.d.ts +8 -0
  126. package/dist/types/lib/Prism/context.d.ts +7 -0
  127. package/dist/types/lib/Prism/index.d.ts +3 -0
  128. package/dist/types/lib/Prism/types.d.ts +67 -0
  129. package/dist/types/lib/Prose/Prose.d.ts +11 -0
  130. package/dist/types/lib/Prose/findMention.d.ts +8 -0
  131. package/dist/types/lib/Prose/index.d.ts +3 -0
  132. package/dist/types/lib/Prose/types.d.ts +7 -0
  133. package/dist/types/lib/Row/Row.d.ts +8 -0
  134. package/dist/types/lib/Row/RowView.d.ts +12 -0
  135. package/dist/types/lib/Row/index.d.ts +3 -0
  136. package/dist/types/lib/Row/types.d.ts +25 -0
  137. package/dist/types/lib/SearchBoundary/SearchBoundary.d.ts +17 -0
  138. package/dist/types/lib/SearchBoundary/index.d.ts +2 -0
  139. package/dist/types/lib/SearchBoundary/types.d.ts +22 -0
  140. package/dist/types/lib/SearchPage/SearchPage.d.ts +15 -0
  141. package/dist/types/lib/SearchPage/index.d.ts +2 -0
  142. package/dist/types/lib/SearchPage/types.d.ts +10 -0
  143. package/dist/types/lib/SiteLayout/SiteLayout.d.ts +17 -0
  144. package/dist/types/lib/SiteLayout/index.d.ts +2 -0
  145. package/dist/types/lib/SiteLayout/types.d.ts +11 -0
  146. package/dist/types/lib/ThingLink/ThingLink.d.ts +8 -0
  147. package/dist/types/lib/ThingLink/index.d.ts +2 -0
  148. package/dist/types/lib/ThingLink/types.d.ts +9 -0
  149. package/dist/types/lib/ThingResults/ThingList.d.ts +4 -0
  150. package/dist/types/lib/ThingResults/ThingResults.d.ts +16 -0
  151. package/dist/types/lib/ThingResults/index.d.ts +2 -0
  152. package/dist/types/lib/ThingResults/types.d.ts +14 -0
  153. package/dist/types/lib/graphql/PrismCard.d.ts +6 -0
  154. package/dist/types/lib/graphql/PrismChip.d.ts +3 -0
  155. package/dist/types/lib/graphql/PrismClasses.d.ts +8 -0
  156. package/dist/types/lib/graphql/PrismFact.d.ts +6 -0
  157. package/dist/types/lib/graphql/PrismPageHeader.d.ts +8 -0
  158. package/dist/types/lib/graphql/PrismRow.d.ts +6 -0
  159. package/dist/types/lib/graphql/__generated__/PrismCard_node.graphql.d.ts +38 -0
  160. package/dist/types/lib/graphql/__generated__/PrismChip_node.graphql.d.ts +23 -0
  161. package/dist/types/lib/graphql/__generated__/PrismClasses_node.graphql.d.ts +33 -0
  162. package/dist/types/lib/graphql/__generated__/PrismFact_fact.graphql.d.ts +20 -0
  163. package/dist/types/lib/graphql/__generated__/PrismPageHeader_node.graphql.d.ts +60 -0
  164. package/dist/types/lib/graphql/__generated__/PrismRow_node.graphql.d.ts +35 -0
  165. package/dist/types/lib/graphql/index.d.ts +6 -0
  166. package/dist/types/lib/lens/constants.d.ts +2 -0
  167. package/dist/types/lib/lens/index.d.ts +4 -0
  168. package/dist/types/lib/lens/isShortcutKey.d.ts +6 -0
  169. package/dist/types/lib/lens/readLenses.d.ts +27 -0
  170. package/dist/types/lib/lens/types.d.ts +48 -0
  171. package/dist/types/lib/projection/findNearest.d.ts +6 -0
  172. package/dist/types/lib/projection/index.d.ts +6 -0
  173. package/dist/types/lib/projection/orderClasses.d.ts +19 -0
  174. package/dist/types/lib/projection/readHomes.d.ts +11 -0
  175. package/dist/types/lib/projection/types.d.ts +15 -0
  176. package/dist/types/lib/projection/useProjection.d.ts +9 -0
  177. package/dist/types/lib/projection/withLabel.d.ts +8 -0
  178. package/dist/types/lib/renderPage/index.d.ts +2 -0
  179. package/dist/types/lib/renderPage/renderPage.d.ts +21 -0
  180. package/dist/types/lib/renderPage/types.d.ts +49 -0
  181. package/dist/types/lib/search/filterLenses.d.ts +6 -0
  182. package/dist/types/lib/search/index.d.ts +3 -0
  183. package/dist/types/lib/search/searchVariables.d.ts +9 -0
  184. package/dist/types/lib/search/warmSearch.d.ts +9 -0
  185. package/dist/types/lib/searchQuery/PrismSearchQuery.d.ts +8 -0
  186. package/dist/types/lib/searchQuery/__generated__/PrismSearchQuery.graphql.d.ts +26 -0
  187. package/dist/types/lib/searchQuery/index.d.ts +1 -0
  188. package/dist/types/lib/warmQuery/index.d.ts +1 -0
  189. package/dist/types/lib/warmQuery/warmQuery.d.ts +19 -0
  190. package/dist/types/server.d.ts +6 -0
  191. package/package.json +107 -4
package/README.md CHANGED
@@ -1,3 +1,358 @@
1
- # Temporary Holding Version
1
+ # @canonical/prism
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The site engine of Prism: the navigation, the layout, the command palette, the
4
+ search page and the server render of a site that documents a knowledge graph,
5
+ and the four projections that draw any thing of that graph. It reads the
6
+ site's own route table; it creates no route table and no Relay environment of
7
+ its own, a provider only when the site replaces a projection or its text,
8
+ and it does not know Relay's store.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ bun add @canonical/prism
14
+ ```
15
+
16
+ React, React DOM, `react-relay`, `relay-runtime`, `@canonical/router-core`,
17
+ `@canonical/router-react` and `@canonical/react-hooks` are peer dependencies:
18
+ the site and the engine must share one copy of each, or the router's and the
19
+ preferences' contexts split.
20
+
21
+ ## Styles
22
+
23
+ Each component imports its own stylesheet from its module; a site's bundler
24
+ puts it in the site's stylesheet, which the server's page links. There is no
25
+ separate sheet to link.
26
+
27
+ Every rule sits in the `lib` cascade layer, which `@canonical/styles` declares
28
+ for a library outside the design system: above every design-system layer and
29
+ below `app`, so a site overrides the engine from its own `@layer app`.
30
+
31
+ ## A lens is its routes
32
+
33
+ A lens is a section of the site with an entry in the navigation. It is a set
34
+ of ordinary router routes: its list route carries `meta.lens` (label, place,
35
+ icon, shortcut), and every other route of the lens names the list route in
36
+ `meta.lensOf`, so the navigation marks the lens on those pages too. The
37
+ navigation, the palette and the search page all read the lenses from the
38
+ route table; there is no list to keep beside it.
39
+
40
+ ```tsx
41
+ import type { PrismRouteMeta } from "@canonical/prism";
42
+ import { warmQuery } from "@canonical/prism";
43
+ import { route } from "@canonical/router-core";
44
+
45
+ const componentsRoutes = {
46
+ components: route({
47
+ url: "/components",
48
+ content: ComponentsPage,
49
+ warm: (_params, _search, { context, signal }) =>
50
+ warmQuery(context.environment, componentsQuery, {}, { signal }),
51
+ meta: {
52
+ lens: { label: "Components", order: 2, icon: <ComponentsIcon />, shortcut: "2" },
53
+ },
54
+ }),
55
+ component: route({
56
+ url: "/components/:uri",
57
+ content: ComponentPage,
58
+ meta: { lensOf: "components" },
59
+ }),
60
+ };
61
+
62
+ declare module "@canonical/router-react" {
63
+ interface RouterRegister {
64
+ routes: typeof routes;
65
+ meta: PrismRouteMeta;
66
+ context: { readonly environment: Environment };
67
+ }
68
+ }
69
+ ```
70
+
71
+ The route table is checked on the first render and the first request: a
72
+ malformed `meta.lens`, a lens whose list route takes parameters, two lenses in
73
+ one place or on one key, a lens on the palette's `/`, or a `lensOf` that names
74
+ no lens is an error.
75
+
76
+ ## The frame
77
+
78
+ `SiteLayout` puts the navigation in a column of its own, off the grid at a
79
+ fixed width, and the page on the intrinsic grid of `@canonical/styles`
80
+ (`.grid.intrinsic`), corrected so a phone keeps four columns. Put it in a router
81
+ wrapper every page shares, and it stays mounted while the reader moves.
82
+
83
+ ```tsx
84
+ const siteLayout = wrapper({
85
+ id: "site",
86
+ component: ({ children }) => (
87
+ <SiteLayout
88
+ navigation={
89
+ <>
90
+ <CommandPalette search="search" />
91
+ <Navigation />
92
+ </>
93
+ }
94
+ >
95
+ {children}
96
+ </SiteLayout>
97
+ ),
98
+ });
99
+
100
+ const searchRoute = route({ url: "/search", content: () => <SearchPage /> });
101
+ ```
102
+
103
+ - `Navigation` lists the lenses in their order. A lens's shortcut opens it on
104
+ one key press, unless the reader turned shortcuts off.
105
+ - `CommandPalette` is a link to the search page that, with scripts, opens a
106
+ native `<dialog>` holding one combobox. `/` opens it; Escape closes it and
107
+ gives focus back to the link.
108
+ - `SearchPage` does the palette's job at its own address, reading `?q=`, so
109
+ it works without scripts.
110
+
111
+ ## Drawing a thing: four projections on slots
112
+
113
+ Every node of the graph answers the same eight questions, its slots: `label`,
114
+ `category`, `description`, `facts`, `location`, `parent`, `status` and
115
+ `version`, under `_meta.slots`. Four components draw any thing from them, and
116
+ none knows about a specific kind of thing:
117
+
118
+ | Projection | Where | What it shows |
119
+ | --- | --- | --- |
120
+ | `Chip` | In a sentence, or alone | The label, in the design system's chip |
121
+ | `Row` | One line of a list | The label, the category, the status |
122
+ | `Card` | A tile on the grid, in the design system's card | The label, category, location, description, version, status and three facts |
123
+ | `PageHeader` | The top of a thing's page | The parents, the label as the heading, the category, location, version, status and description |
124
+
125
+ A slot a thing does not have takes no room. Each reads its own fragment,
126
+ which a page spreads in its query (`...PrismChip_node`, `...PrismRow_node`,
127
+ `...PrismCard_node`, `...PrismPageHeader_node`), and links the thing to its
128
+ page when its class has a home route: a route whose only param is `:uri`
129
+ and whose `meta.home` names the class. A label with no text is replaced by
130
+ the thing's compact name, then its IRI, so no link or heading is unnamed.
131
+ The projections and their plain views read Relay fragments (a fact or a
132
+ location that names a thing is drawn as that thing's chip), so they render
133
+ inside the site's `RelayEnvironmentProvider` and its `RouterProvider`.
134
+
135
+ ```tsx
136
+ const routes = group(siteLayout, {
137
+ component: route({
138
+ url: "/components/:uri",
139
+ content: ComponentPage,
140
+ meta: { lensOf: "components", home: "ds:UIBlock" },
141
+ }),
142
+ });
143
+
144
+ function ComponentPage({ params }: { params: { uri: string } }) {
145
+ const { node } = useLazyLoadQuery<ComponentPageQuery>(componentPageQuery, {
146
+ uri: params.uri,
147
+ });
148
+ return node ? <PageHeader node={node} /> : null;
149
+ }
150
+ ```
151
+
152
+ The home route of a thing is the one for its nearest class that has one, so a
153
+ route home to `ds:UIBlock` shows components and subcomponents alike. A class
154
+ is named by its IRI or by its compact name as the graph gives it
155
+ (`_meta.curie`), one way across the route table: a home route with another
156
+ param, two routes for one class, or one class named both ways on two routes
157
+ is refused with an error. The thing's IRI is the route's `uri`.
158
+
159
+ ### Replacing a projection for a class
160
+
161
+ A site replaces any of the four for a class through one map, passed once to
162
+ the `Prism` provider. The keys of each entry are `chip`, `row`, `card` and
163
+ `header`. A replacement receives the props of the plain view it replaces
164
+ (`ChipView`, `RowView`, `CardView`, `PageHeaderView`: the thing's `uri`, its
165
+ `slots` and the name of its `home` route) and can render that view itself.
166
+
167
+ ```tsx
168
+ function TokenCard({ children, ...props }: CardViewProps) {
169
+ return (
170
+ <CardView {...props}>
171
+ <TokenSwatch uri={props.uri} />
172
+ {children}
173
+ </CardView>
174
+ );
175
+ }
176
+
177
+ const overrides = {
178
+ "ds:Token": { card: TokenCard },
179
+ } satisfies ProjectionOverrides;
180
+
181
+ <Prism overrides={overrides}>{/* the site */}</Prism>;
182
+ ```
183
+
184
+ A replacement for a class applies to its subclasses, unless a nearer class
185
+ has its own for the same projection. The graph serves each thing's class
186
+ (`_meta.type`) and every ancestor of it (`superclasses`), each with its own
187
+ ancestors. The engine orders them nearest first by how many ancestors each
188
+ has: an ancestor of a class always has fewer, so every class comes before its
189
+ superclasses whatever order the graph lists them in, and ancestors on
190
+ separate branches keep the graph's order. It then takes the first class with
191
+ a replacement. A provider must list every ancestor in `superclasses`, not
192
+ only the direct ones.
193
+
194
+ ### Text from the graph
195
+
196
+ The engine reads no markup. `Prose` draws a text from the graph, such as a
197
+ description, and on its own draws it as plain paragraphs, parted on blank
198
+ lines, each trimmed and otherwise as written: no markup is interpreted and
199
+ no link is followed. It needs neither Relay nor a router. A site that wants
200
+ more passes its own component as `Text` to `Prism`, beside `overrides`. It
201
+ receives exactly `{ text, mentions }`: the text untouched, and the things it
202
+ mentions, each a `Mention` with its `uri`, its compact name `curie` and the
203
+ `node` a `Chip` draws. **`mentions` is a seam that nothing fills yet:** the
204
+ contract cannot say which things a text mentions, so the list is always
205
+ empty today, and it fills when the contract can serve them.
206
+
207
+ ```tsx
208
+ <Prism overrides={overrides} Text={SiteText}>
209
+ {/* the site */}
210
+ </Prism>
211
+ ```
212
+
213
+ A docs site's `SiteText` is about ten lines, written for the day mentions
214
+ arrive: it hands `text` to the site's Markdown renderer, and gives that
215
+ renderer one replacement for links. The link replacement calls
216
+ `findMention(mentions, href)`, which today finds nothing; when it finds a thing,
217
+ it draws `<Chip node={mention.node} />`, which shows the thing's own label
218
+ rather than the words of the link, and otherwise it draws an ordinary link.
219
+ `findMention` matches a target by IRI or compact name, as written or
220
+ percent-encoded, reserved characters included. Only links are read this way,
221
+ so a bare `ds:Button` in a sentence stays text. Because `Text` is a
222
+ component, it can use hooks and be memoised.
223
+
224
+ ### Finding things
225
+
226
+ `graph` is off by default, and then neither the palette nor the search page
227
+ sends any query. The contract has no `search` field yet, so leave it off
228
+ until it does. With `graph`, the command palette lists the things the graph's `search` field
229
+ finds after the lenses, and the search page lists them below the lenses, each
230
+ drawn as a `Row`. The search route's `warm` fetches them with `warmSearch`,
231
+ given the route's `q`, so the server renders them. On the server the search
232
+ page reads only what `warm` put in the store and never fetches: when the
233
+ answer is not there, because `warm` failed or the route has none, it says
234
+ "Search is unavailable." In the browser, a failed search shows no things in
235
+ the palette, whose lenses still work, and the same sentence on the search
236
+ page; the rest of the page stays.
237
+
238
+ ```tsx
239
+ const searchRoute = route({
240
+ url: "/search",
241
+ search: qSchema, // any Standard Schema that reads `q` as a string
242
+ content: () => <SearchPage graph />,
243
+ warm: (_params, { q }, { context, signal }) =>
244
+ warmSearch(context.environment, q, { signal }),
245
+ });
246
+ ```
247
+
248
+ ### Compiling the fragments in a site
249
+
250
+ A site's own queries spread the projections' fragments, so its
251
+ relay-compiler must read them. They are in the built package's
252
+ `dist/esm/lib/graphql` folder, with their tags kept; the search query is
253
+ apart, in `dist/esm/lib/searchQuery`, and is added only by a site whose
254
+ schema has the `search` field, since it does not compile without it. A
255
+ single-project relay config takes one source folder, so the site uses a
256
+ multi-project config, and its `excludes` must not exclude `node_modules`
257
+ (relay-compiler's default does):
258
+
259
+ ```json
260
+ {
261
+ "sources": {
262
+ "src": "site",
263
+ "node_modules/@canonical/prism/dist/esm/lib/graphql": "site"
264
+ },
265
+ "excludes": ["**/__generated__/**"],
266
+ "projects": {
267
+ "site": {
268
+ "schema": "schema.graphql",
269
+ "language": "typescript",
270
+ "eagerEsModules": true,
271
+ "output": "src/__generated__",
272
+ "schemaConfig": {
273
+ "nodeInterfaceIdField": "uri",
274
+ "nodeInterfaceIdVariableName": "id"
275
+ }
276
+ }
277
+ }
278
+ }
279
+ ```
280
+
281
+ With `graph` on, add `"node_modules/@canonical/prism/dist/esm/lib/searchQuery":
282
+ "site"` to `sources`. The site's schema needs the contract's slots; a test
283
+ compiles exactly this project against the built package.
284
+
285
+ ### The schema the projections are written against
286
+
287
+ The slots are not yet in this repository's contract
288
+ (`@canonical/prism-contract`), and neither is the `search` field. The
289
+ engine's queries are written against the contract with both, which exist
290
+ only in `src/testing/schema/`: `slots.graphql` copies the slots of the
291
+ published contract exactly, and `search.graphql` holds the search field.
292
+ `scripts/writeSchema.ts` merges them into the contract for relay-compiler,
293
+ and the tests and stories answer the queries from fixtures through the same
294
+ files. The compiled artifacts are committed, so the package builds with
295
+ `tsc` alone and a site needs no bundler transform.
296
+
297
+ ## The server step
298
+
299
+ `renderPage` is one step of the site's server:
300
+
301
+ ```ts
302
+ import { renderPage } from "@canonical/prism/server";
303
+
304
+ export default function page(request: Request, html: string) {
305
+ const { environment, readRecords } = createServerEnvironment({ graphqlUrl });
306
+
307
+ return renderPage(request, {
308
+ routes,
309
+ notFound,
310
+ middleware,
311
+ Shell,
312
+ context: { environment },
313
+ data: () => ({ records: readRecords() }),
314
+ html,
315
+ });
316
+ }
317
+ ```
318
+
319
+ It answers GET and HEAD (405 otherwise; HEAD gets GET's status and headers
320
+ without a body), applies the site's `middleware` as the browser's router does,
321
+ resolves the redirect or the status,
322
+ waits for the matched routes' `warm`, then streams the document with the
323
+ router state and what `data` returned embedded as `PageData`. One five-second
324
+ deadline bounds the whole answer: when `warm` has not settled by then, the page
325
+ fails closed with a 503, because a `warm` still running may be a guard about to
326
+ redirect; a deadline passing while the shell renders also answers 503, and a
327
+ stream still running at the deadline is cut. Pages carry
328
+ `Cache-Control: private, no-cache`.
329
+
330
+ The site writes its own `Shell` (its providers around the router's outlet,
331
+ receiving `router` and `context`), its own Relay environments and its own
332
+ browser entry, which reads `PageData` from `window[INITIAL_DATA_KEY]` and
333
+ passes `router` to `createRouter` as `hydratedState`.
334
+
335
+ ## Exports
336
+
337
+ | Export | From | What it is |
338
+ | --- | --- | --- |
339
+ | `Navigation` | `@canonical/prism` | The list of lenses |
340
+ | `Chip`, `Row`, `Card`, `PageHeader` | `@canonical/prism` | The four projections of a thing |
341
+ | `ChipView`, `RowView`, `CardView`, `PageHeaderView` | `@canonical/prism` | Their plain views, for a replacement to reuse |
342
+ | `Prism`, `ProjectionOverrides` | `@canonical/prism` | The provider of a site's replacements and text component, and the map |
343
+ | `Prose`, `TextProps`, `Mention`, `findMention` | `@canonical/prism` | Text from the graph, the site's text component's props, and the lookup of a link's mentioned thing |
344
+ | `warmSearch` | `@canonical/prism` | Fetches a search ahead of the search page |
345
+ | `SiteLayout` | `@canonical/prism` | The frame: navigation column, header band, page |
346
+ | `CommandPalette` | `@canonical/prism` | The search link and dialog |
347
+ | `SearchPage` | `@canonical/prism` | The search page |
348
+ | `warmQuery` | `@canonical/prism` | Fetches a query ahead of the page, in a route's `warm` |
349
+ | `PrismRouteMeta`, `Lens` | `@canonical/prism` | The route meta Prism reads: `lens`, `lensOf` and `home` |
350
+ | `ShellProps`, `PageData` | `@canonical/prism` | What the site's Shell and browser entry receive |
351
+ | `renderPage`, `RenderPageOptions` | `@canonical/prism/server` | The server step |
352
+
353
+ Each projection and its plain view, the provider and `Prose` export their
354
+ props types.
355
+
356
+ CSS classes start with `prism-`. Every colour, size and space comes from the
357
+ design system's tokens, and the chip and the card are the design system's
358
+ own.
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @canonical/prism — the site engine of Prism: a site's navigation, layout,
3
+ * command palette and search page, read from the site's own route table; the
4
+ * four projections that draw any thing of the graph from its slots, and the
5
+ * provider through which a site replaces them, or the component that draws
6
+ * text from the graph; and the helpers its routes fetch their data with.
7
+ * The server step is at `@canonical/prism/server`.
8
+ *
9
+ * @packageDocumentation
10
+ */
11
+ export { Card, CardView, } from "./lib/Card/index.js";
12
+ export { Chip, ChipView, } from "./lib/Chip/index.js";
13
+ export { CommandPalette, } from "./lib/CommandPalette/index.js";
14
+ export { Navigation } from "./lib/Navigation/index.js";
15
+ export { PageHeader, PageHeaderView, } from "./lib/PageHeader/index.js";
16
+ export { Prism, } from "./lib/Prism/index.js";
17
+ export { findMention, Prose } from "./lib/Prose/index.js";
18
+ export { Row, RowView, } from "./lib/Row/index.js";
19
+ export { SearchPage } from "./lib/SearchPage/index.js";
20
+ export { SiteLayout } from "./lib/SiteLayout/index.js";
21
+ export { warmSearch } from "./lib/search/index.js";
22
+ export { warmQuery } from "./lib/warmQuery/index.js";
@@ -0,0 +1,16 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useFragment } from "react-relay";
3
+ import { prismCardFragment } from "../graphql/index.js";
4
+ import { useProjection, withLabel } from "../projection/index.js";
5
+ import CardView from "./CardView.js";
6
+ /**
7
+ * A thing as a card, for any kind of thing: it reads the thing's slots and
8
+ * draws the site's card for the thing's nearest class, or the plain
9
+ * `CardView`.
10
+ */
11
+ export default function Card({ node, linked = true, ...props }) {
12
+ const data = useFragment(prismCardFragment, node);
13
+ const { uri, Override, home } = useProjection(data, "card");
14
+ const View = Override ?? CardView;
15
+ return (_jsx(View, { ...props, home: linked ? home : undefined, slots: withLabel(data._meta.slots, data._meta.curie, uri), uri: uri }));
16
+ }
@@ -0,0 +1,19 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Card as DsCard } from "@canonical/react-ds-global";
3
+ import { FactValue } from "../Fact/index.js";
4
+ import { Prose } from "../Prose/index.js";
5
+ import { ThingLink } from "../ThingLink/index.js";
6
+ import "./styles.css";
7
+ const componentCssClassName = "prism-card";
8
+ /**
9
+ * The plain card, in the design system's card: the thing's label, linked to
10
+ * its page when it has one, its category and location, then its
11
+ * description, and its version, status and facts as a list of terms. A slot
12
+ * the thing does not have takes no room. `children` follow the facts, for an
13
+ * override that adds its own parts.
14
+ */
15
+ export default function CardView({ uri, slots, home, className, children, ...props }) {
16
+ const { label, category, location, description, version, status, facts } = slots;
17
+ const hasTerms = Boolean(version || status || facts.length > 0);
18
+ return (_jsxs(DsCard, { className: [componentCssClassName, className].filter(Boolean).join(" "), ...props, children: [_jsxs(DsCard.Header, { children: [_jsx("p", { className: `${componentCssClassName}-label`, dir: "auto", children: home === undefined ? (label) : (_jsx(ThingLink, { home: home, uri: uri, children: label })) }), _jsxs("p", { className: `${componentCssClassName}-kind`, children: [_jsx("span", { className: `${componentCssClassName}-category`, children: category }), location ? (_jsxs(_Fragment, { children: [" ", _jsx("span", { className: `${componentCssClassName}-location`, children: _jsx(FactValue, { fact: location }) })] })) : null] })] }), description || hasTerms || children ? (_jsxs(DsCard.Content, { children: [description ? _jsx(Prose, { children: description }) : null, hasTerms ? (_jsxs("dl", { className: `${componentCssClassName}-terms`, children: [version ? (_jsxs("div", { children: [_jsx("dt", { children: "Version" }), _jsx("dd", { children: _jsx(FactValue, { fact: version }) })] })) : null, status ? (_jsxs("div", { children: [_jsx("dt", { children: "Status" }), _jsx("dd", { children: _jsx(FactValue, { fact: status }) })] })) : null, facts.map((fact, index) => (_jsxs("div", { children: [_jsx("dt", { children: fact.name }), _jsx("dd", { children: _jsx(FactValue, { fact: fact }) })] }, `${index}-${fact.name}`)))] })) : null, children] })) : null] }));
19
+ }
@@ -0,0 +1,2 @@
1
+ export { default as Card } from "./Card.js";
2
+ export { default as CardView } from "./CardView.js";
@@ -0,0 +1,68 @@
1
+ /* The design system's card, four columns wide on the intrinsic grid (two
2
+ * side by side from eight columns, with no breakpoint), unless a `Cards`
3
+ * group gives it another span. Inside it, one size
4
+ * of text on the 8px rhythm: the label in ink, the category in the muted
5
+ * colour and the location in ink after it (a location is where a thing
6
+ * lives, worth reading; a category is what it is, already implied), and the
7
+ * terms as tight rows parted by hairlines. */
8
+
9
+ /* Layer: lib, the layer @canonical/styles keeps for a library outside the
10
+ * design system, above every design-system layer and below the site's app. */
11
+ @layer lib {
12
+ .prism-card {
13
+ grid-column: span var(--card-span, 4);
14
+ }
15
+
16
+ .prism-card-label,
17
+ .prism-card-kind {
18
+ margin: 0;
19
+ line-height: var(--space-300);
20
+ }
21
+
22
+ .prism-card-kind {
23
+ display: flex;
24
+ flex-wrap: wrap;
25
+ column-gap: var(--space-100);
26
+ }
27
+
28
+ .prism-card-label {
29
+ overflow-wrap: anywhere;
30
+ }
31
+
32
+ .prism-card-category {
33
+ color: var(--color-text-muted);
34
+ }
35
+
36
+ .prism-card-location {
37
+ color: var(--color-text);
38
+ }
39
+
40
+ .prism-card-terms {
41
+ display: grid;
42
+ margin: 0;
43
+ }
44
+
45
+ * + .prism-card-terms {
46
+ margin-block-start: var(--space-200);
47
+ }
48
+
49
+ .prism-card-terms > div {
50
+ display: flex;
51
+ flex-wrap: wrap;
52
+ column-gap: var(--space-200);
53
+ padding-block: var(--space-100);
54
+ border-block-start: var(--dimension-stroke-thickness-medium) solid
55
+ var(--color-border-muted);
56
+ line-height: var(--space-300);
57
+ }
58
+
59
+ .prism-card-terms dt {
60
+ color: var(--color-text-muted);
61
+ }
62
+
63
+ .prism-card-terms dd {
64
+ margin: 0;
65
+ min-inline-size: 0;
66
+ overflow-wrap: anywhere;
67
+ }
68
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useFragment } from "react-relay";
3
+ import { prismChipFragment } from "../graphql/index.js";
4
+ import { useProjection, withLabel } from "../projection/index.js";
5
+ import ChipView from "./ChipView.js";
6
+ /**
7
+ * A thing as a chip, for any kind of thing: it reads the thing's slots and
8
+ * draws the site's chip for the thing's nearest class, or the plain
9
+ * `ChipView`.
10
+ */
11
+ export default function Chip({ node, linked = true, ...props }) {
12
+ const data = useFragment(prismChipFragment, node);
13
+ const { uri, Override, home } = useProjection(data, "chip");
14
+ const View = Override ?? ChipView;
15
+ return (_jsx(View, { ...props, home: linked ? home : undefined, slots: withLabel(data._meta.slots, data._meta.curie, uri), uri: uri }));
16
+ }
@@ -0,0 +1,14 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Chip as DsChip } from "@canonical/react-ds-global";
3
+ import { ThingLink } from "../ThingLink/index.js";
4
+ import "./styles.css";
5
+ const componentCssClassName = "prism-chip";
6
+ /**
7
+ * The plain chip: the thing's own label, in the design system's chip, inline
8
+ * in a sentence or alone. It links to the thing's page when it has one. An
9
+ * override for a class may render it with its own parts around it.
10
+ */
11
+ export default function ChipView({ uri, slots, home, className, ...props }) {
12
+ const chip = _jsx(DsChip, { value: slots.label });
13
+ return (_jsx("span", { className: [componentCssClassName, className].filter(Boolean).join(" "), ...props, dir: "auto", children: home === undefined ? (chip) : (_jsx(ThingLink, { className: `${componentCssClassName}-link`, home: home, uri: uri, children: chip })) }));
14
+ }
@@ -0,0 +1,2 @@
1
+ export { default as Chip } from "./Chip.js";
2
+ export { default as ChipView } from "./ChipView.js";
@@ -0,0 +1,11 @@
1
+ /* A chip sits in a line of text: the link adds nothing to the design
2
+ * system's chip, whose own hover state marks it, but the focus ring. */
3
+
4
+ /* Layer: lib, the layer @canonical/styles keeps for a library outside the
5
+ * design system, above every design-system layer and below the site's app. */
6
+ @layer lib {
7
+ .prism-chip-link {
8
+ color: inherit;
9
+ text-decoration: none;
10
+ }
11
+ }
@@ -0,0 +1 @@
1
+ export {};