@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
@@ -0,0 +1,166 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { usePreferredShortcuts } from "@canonical/react-hooks";
3
+ import { useRouter } from "@canonical/router-react";
4
+ import { Suspense, useCallback, useDeferredValue, useEffect, useMemo, useRef, useState, } from "react";
5
+ import { isShortcutKey, PALETTE_KEY, readLenses, } from "../lens/index.js";
6
+ import { SearchBoundary } from "../SearchBoundary/index.js";
7
+ import { filterLenses, searchVariables } from "../search/index.js";
8
+ import ThingOptions from "./ThingOptions.js";
9
+ import "./styles.css";
10
+ const componentCssClassName = "prism-command-palette";
11
+ const OPEN_KEY = PALETTE_KEY;
12
+ /**
13
+ * The ids the combobox points at. A page has one palette, and the server
14
+ * renders the whole document while the browser hydrates only its root, so
15
+ * an id from `useId` would differ between the two: these are fixed.
16
+ */
17
+ const id = componentCssClassName;
18
+ /**
19
+ * A link to the search page that, with scripts, opens a native modal
20
+ * `<dialog>` instead: one combobox whose list holds the lenses matching what
21
+ * the reader types. Arrow keys move through the list, Enter opens the chosen
22
+ * lens, or the search page when none is chosen, and Escape closes the
23
+ * dialog, returning focus to the link. `/` opens it from anywhere, unless
24
+ * the reader has turned shortcuts off.
25
+ *
26
+ * With `graph`, the list also holds the things the graph's `search` field
27
+ * finds, as a second group after the lenses, each drawn as a `Row`;
28
+ * choosing one opens its page. A failed search lists no things.
29
+ */
30
+ export default function CommandPalette({ search, className, graph = false, }) {
31
+ const router = useRouter();
32
+ const lenses = useMemo(() => readLenses(router.routes, (name) => router.buildPath(name)), [router]);
33
+ const searchHref = router.buildPath(search);
34
+ const dialogRef = useRef(null);
35
+ const inputRef = useRef(null);
36
+ const triggerRef = useRef(null);
37
+ const returnFocusRef = useRef(null);
38
+ const [query, setQuery] = useState("");
39
+ const [active, setActive] = useState(-1);
40
+ const [thingCount, setThingCount] = useState(0);
41
+ // How each listed thing is opened, by its position in the list: a thing's
42
+ // option registers it, and one with no page registers none.
43
+ const thingChoices = useRef(new Map());
44
+ const results = filterLenses(lenses, query);
45
+ // The things are looked for a beat behind the typing: while an answer is
46
+ // on its way, the list keeps the last one and the field stays responsive.
47
+ const deferredQuery = useDeferredValue(query);
48
+ const variables = graph ? searchVariables(deferredQuery) : undefined;
49
+ const optionCount = results.length + (variables ? thingCount : 0);
50
+ // The list can shrink under the arrow keys when a slower answer arrives,
51
+ // so the active option is read within it.
52
+ const current = active < optionCount ? active : -1;
53
+ // Once the option the arrow keys reached is gone, nothing is active: the
54
+ // next ArrowDown starts from the top of the list as it is now.
55
+ useEffect(() => {
56
+ if (active !== current)
57
+ setActive(current);
58
+ }, [active, current]);
59
+ const listId = `${id}-list`;
60
+ const optionId = (index) => `${id}-option-${index}`;
61
+ const { value: shortcuts } = usePreferredShortcuts();
62
+ const shortcutsOn = shortcuts !== "off";
63
+ const open = useCallback(() => {
64
+ // The element to give focus back to: the one that had it, unless
65
+ // nothing did; a click on the link leaves the focus on the link.
66
+ returnFocusRef.current =
67
+ document.activeElement instanceof HTMLElement &&
68
+ document.activeElement !== document.body
69
+ ? document.activeElement
70
+ : null;
71
+ setQuery("");
72
+ setActive(-1);
73
+ dialogRef.current?.showModal();
74
+ inputRef.current?.focus();
75
+ }, []);
76
+ const close = () => {
77
+ dialogRef.current?.close();
78
+ };
79
+ const choose = (lens) => {
80
+ close();
81
+ router.navigate(lens.name);
82
+ };
83
+ const openThing = (home, uri) => {
84
+ close();
85
+ // A home route's name is read from the route table at run time, so the
86
+ // router's types cannot check it.
87
+ router.navigate(home, { params: { uri } });
88
+ };
89
+ // Focus stays in the field, so the option the arrow keys reach is brought
90
+ // into view by hand.
91
+ useEffect(() => {
92
+ if (current >= 0) {
93
+ document
94
+ .getElementById(optionId(current))
95
+ ?.scrollIntoView?.({ block: "nearest" });
96
+ }
97
+ });
98
+ useEffect(() => {
99
+ if (!shortcutsOn)
100
+ return;
101
+ const onKeyDown = (event) => {
102
+ if (event.key !== OPEN_KEY || !isShortcutKey(event))
103
+ return;
104
+ if (dialogRef.current?.open)
105
+ return;
106
+ event.preventDefault();
107
+ open();
108
+ };
109
+ document.addEventListener("keydown", onKeyDown);
110
+ return () => document.removeEventListener("keydown", onKeyDown);
111
+ }, [shortcutsOn, open]);
112
+ const onTriggerClick = (event) => {
113
+ if (event.button !== 0)
114
+ return;
115
+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
116
+ return;
117
+ }
118
+ event.preventDefault();
119
+ event.currentTarget.focus();
120
+ open();
121
+ };
122
+ const onInputKeyDown = (event) => {
123
+ if (event.key === "ArrowDown") {
124
+ event.preventDefault();
125
+ setActive((index) => Math.min(index + 1, optionCount - 1));
126
+ }
127
+ else if (event.key === "ArrowUp") {
128
+ event.preventDefault();
129
+ setActive((index) => Math.max(Math.min(index, optionCount) - 1, -1));
130
+ }
131
+ };
132
+ const onSubmit = (event) => {
133
+ event.preventDefault();
134
+ const lens = results[current];
135
+ if (lens) {
136
+ choose(lens);
137
+ return;
138
+ }
139
+ // A thing with no page sends the query to the search page instead.
140
+ const chooseThing = thingChoices.current.get(current);
141
+ if (chooseThing) {
142
+ chooseThing();
143
+ return;
144
+ }
145
+ close();
146
+ router.navigate(search, { search: query ? { q: query } : {} });
147
+ };
148
+ return (_jsxs(_Fragment, { children: [_jsxs("a", { "aria-keyshortcuts": OPEN_KEY, "aria-label": "Search", className: [`${componentCssClassName}-trigger`, className]
149
+ .filter(Boolean)
150
+ .join(" "), href: searchHref, onClick: onTriggerClick, ref: triggerRef, children: ["Search", _jsx("kbd", { className: `${componentCssClassName}-shortcut`, children: OPEN_KEY })] }), _jsx("dialog", { "aria-label": "Search", className: componentCssClassName, onClick: (event) => {
151
+ // A click on the backdrop lands on the dialog element itself.
152
+ if (event.target === event.currentTarget)
153
+ close();
154
+ }, onClose: () => {
155
+ setQuery("");
156
+ setActive(-1);
157
+ // Browsers return focus to where it was on their own; this keeps
158
+ // it so where one does not: the element that had it when the
159
+ // palette opened, or the link.
160
+ (returnFocusRef.current ?? triggerRef.current)?.focus();
161
+ returnFocusRef.current = null;
162
+ }, ref: dialogRef, children: _jsx("search", { children: _jsxs("form", { action: searchHref, className: `${componentCssClassName}-form`, method: "get", onSubmit: onSubmit, children: [_jsx("input", { "aria-activedescendant": current >= 0 ? optionId(current) : undefined, "aria-autocomplete": "list", "aria-controls": listId, "aria-expanded": optionCount > 0, "aria-label": "Search", autoComplete: "off", className: `${componentCssClassName}-input`, name: "q", onChange: (event) => {
163
+ setQuery(event.target.value);
164
+ setActive(-1);
165
+ }, onKeyDown: onInputKeyDown, ref: inputRef, role: "combobox", type: "text", value: query }), _jsxs("div", { "aria-label": graph ? "Results" : "Lenses", className: `${componentCssClassName}-list`, id: listId, role: "listbox", children: [results.length > 0 ? (_jsx("div", { "aria-label": "Lenses", role: "group", children: results.map((lens, index) => (_jsx("div", { "aria-selected": index === current, className: `${componentCssClassName}-option`, id: optionId(index), onClick: () => choose(lens), role: "option", tabIndex: -1, children: lens.label }, lens.name))) })) : null, variables ? (_jsx(SearchBoundary, { fallback: null, text: variables.text, children: (attempt) => (_jsx(Suspense, { fallback: null, children: _jsx(ThingOptions, { active: current, choices: thingChoices.current, fetchKey: attempt, onChoose: openThing, onCount: setThingCount, optionId: optionId, start: results.length, variables: variables }) })) })) : null] }), _jsx("p", { "aria-live": "polite", className: `${componentCssClassName}-count`, children: optionCount === 1 ? "1 result" : `${optionCount} results` })] }) }) })] }));
166
+ }
@@ -0,0 +1,38 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useLayoutEffect } from "react";
3
+ import { useLazyLoadQuery } from "react-relay";
4
+ import { useProjection } from "../projection/index.js";
5
+ import { Row } from "../Row/index.js";
6
+ import { prismSearchQuery, } from "../searchQuery/index.js";
7
+ const componentCssClassName = "prism-command-palette";
8
+ function ThingOption({ node, index, id, selected, choices, onChoose, }) {
9
+ const { uri, home } = useProjection(node, "row");
10
+ const choose = home === undefined ? undefined : () => onChoose(home, uri);
11
+ // The palette opens the active thing on Enter through this; a thing with
12
+ // no page registers nothing. Before paint, like the count.
13
+ useLayoutEffect(() => {
14
+ if (!choose)
15
+ return;
16
+ choices.set(index, choose);
17
+ return () => {
18
+ if (choices.get(index) === choose)
19
+ choices.delete(index);
20
+ };
21
+ }, [choices, index, choose]);
22
+ return (_jsx("div", { "aria-disabled": choose ? undefined : true, "aria-selected": selected, className: `${componentCssClassName}-option`, id: id, onClick: choose, role: "option", tabIndex: -1, children: _jsx(Row, { linked: false, node: node }) }));
23
+ }
24
+ /**
25
+ * The palette's things: a group of options after the lenses, one row each.
26
+ * A thing whose class has no home route is listed but cannot be chosen.
27
+ */
28
+ export default function ThingOptions({ variables, start, active, optionId, choices, fetchKey, onCount, onChoose, }) {
29
+ const { search } = useLazyLoadQuery(prismSearchQuery, variables, { fetchKey });
30
+ const count = search.edges.length;
31
+ // Before the browser paints, so the arrow keys never meet a list longer
32
+ // than the palette knows about. The palette is never open on the server.
33
+ useLayoutEffect(() => {
34
+ onCount(count);
35
+ }, [count, onCount]);
36
+ useLayoutEffect(() => () => onCount(0), [onCount]);
37
+ return count > 0 ? (_jsx("div", { "aria-label": "Things", role: "group", children: search.edges.map(({ node }, index) => (_jsx(ThingOption, { choices: choices, id: optionId(start + index), index: start + index, node: node, onChoose: onChoose, selected: start + index === active }, `${index}:${node.uri}`))) })) : null;
38
+ }
@@ -0,0 +1 @@
1
+ export { default as CommandPalette } from "./CommandPalette.js";
@@ -0,0 +1,91 @@
1
+ /* The trigger reads as one more entry of the navigation column: an atom of
2
+ * 48px. The dialog is a column of the same atoms: the field, then the list. */
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-command-palette-trigger {
8
+ display: flex;
9
+ align-items: center;
10
+ justify-content: space-between;
11
+ gap: var(--space-100);
12
+ min-block-size: var(--space-600);
13
+ padding-block: var(--space-150);
14
+ padding-inline: var(--space-200);
15
+ border-inline-start: var(--space-050) solid transparent;
16
+ line-height: var(--space-300);
17
+ color: var(--color-text);
18
+ text-decoration: none;
19
+ }
20
+
21
+ .prism-command-palette-trigger:hover {
22
+ text-decoration: underline;
23
+ }
24
+
25
+ .prism-command-palette-shortcut {
26
+ color: var(--color-text-muted);
27
+ font: inherit;
28
+ }
29
+
30
+ .prism-command-palette {
31
+ inline-size: min(calc(60 * var(--space-100)), 100% - 2 * var(--space-200));
32
+ margin-block-start: calc(12 * var(--space-100));
33
+ padding: 0;
34
+ border: var(--space-025) solid var(--color-border);
35
+ background: var(--color-background);
36
+ color: var(--color-text);
37
+ }
38
+
39
+ .prism-command-palette-form {
40
+ margin: 0;
41
+ }
42
+
43
+ .prism-command-palette-input {
44
+ box-sizing: border-box;
45
+ display: block;
46
+ inline-size: 100%;
47
+ min-block-size: var(--space-600);
48
+ padding-block: var(--space-150);
49
+ padding-inline: var(--space-200);
50
+ border: 0;
51
+ border-block-end: var(--space-025) solid var(--color-border-muted);
52
+ background: transparent;
53
+ color: inherit;
54
+ font: inherit;
55
+ line-height: var(--space-300);
56
+ }
57
+
58
+ .prism-command-palette-option {
59
+ min-block-size: var(--space-600);
60
+ padding-block: var(--space-150);
61
+ padding-inline: var(--space-200);
62
+ border-inline-start: var(--space-050) solid transparent;
63
+ line-height: var(--space-300);
64
+ cursor: pointer;
65
+ }
66
+
67
+ .prism-command-palette-option[aria-selected="true"] {
68
+ border-inline-start-color: var(--color-text);
69
+ }
70
+
71
+ /* A thing's option holds a row, which brings its own rhythm: the option's
72
+ * padding already gives the row its 48px, so the row adds none. */
73
+ .prism-command-palette-option > .prism-row {
74
+ min-block-size: 0;
75
+ padding-block: 0;
76
+ }
77
+
78
+ /* The count is for a screen reader, announced as the list changes; it is
79
+ * not drawn. */
80
+ .prism-command-palette-count {
81
+ position: absolute;
82
+ inline-size: 1px;
83
+ block-size: 1px;
84
+ margin: -1px;
85
+ padding: 0;
86
+ overflow: hidden;
87
+ clip-path: inset(50%);
88
+ white-space: nowrap;
89
+ border: 0;
90
+ }
91
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useFragment } from "react-relay";
3
+ import { Chip } from "../Chip/index.js";
4
+ import { prismFactFragment } from "../graphql/index.js";
5
+ /**
6
+ * The value of one of a thing's facts: the thing it names, as a chip, or its
7
+ * text.
8
+ */
9
+ export default function FactValue({ fact }) {
10
+ const { value, node } = useFragment(prismFactFragment, fact);
11
+ return node ? _jsx(Chip, { node: node }) : value;
12
+ }
@@ -0,0 +1 @@
1
+ export { default as FactValue } from "./FactValue.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,46 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { usePreferredShortcuts } from "@canonical/react-hooks";
3
+ import { Link, useRouter, useRouterState } from "@canonical/router-react";
4
+ import { useEffect, useMemo } from "react";
5
+ import { isShortcutKey, readCurrentLens, readLenses } from "../lens/index.js";
6
+ import "./styles.css";
7
+ const componentCssClassName = "prism-navigation";
8
+ /**
9
+ * The list of a site's lenses, read from its route table: every route with
10
+ * `meta.lens`, in the order the lenses state. The lens the current page
11
+ * belongs to carries the mark at the leading edge: `aria-current="page"` on
12
+ * its list page, and `"true"` on its other pages, which name it in
13
+ * `meta.lensOf`.
14
+ *
15
+ * A lens's shortcut opens it on a single key press, unless the reader has
16
+ * turned shortcuts off in their preferences. The key is shown either way:
17
+ * the preference lives in the browser, so the server cannot draw it.
18
+ */
19
+ export default function Navigation({ className, "aria-label": ariaLabel = "Lenses", }) {
20
+ const router = useRouter();
21
+ const matchedName = useRouterState(({ match }) => (match?.kind === "route" ? match.name : ""));
22
+ const lenses = useMemo(() => readLenses(router.routes, (name) => router.buildPath(name)), [router]);
23
+ const current = readCurrentLens(router.routes, matchedName || undefined);
24
+ const { value: shortcuts } = usePreferredShortcuts();
25
+ const shortcutsOn = shortcuts !== "off";
26
+ useEffect(() => {
27
+ if (!shortcutsOn)
28
+ return;
29
+ const onKeyDown = (event) => {
30
+ if (!isShortcutKey(event))
31
+ return;
32
+ const lens = lenses.find(({ shortcut }) => shortcut === event.key);
33
+ if (!lens)
34
+ return;
35
+ event.preventDefault();
36
+ router.navigate(lens.name);
37
+ };
38
+ document.addEventListener("keydown", onKeyDown);
39
+ return () => document.removeEventListener("keydown", onKeyDown);
40
+ }, [router, lenses, shortcutsOn]);
41
+ return (_jsx("nav", { "aria-label": ariaLabel, className: [componentCssClassName, className].filter(Boolean).join(" "), children: _jsx("ul", { className: `${componentCssClassName}-list`, children: lenses.map(({ name, label, icon, shortcut }) => (_jsx("li", { children: _jsxs(Link, { "aria-current": name !== current
42
+ ? undefined
43
+ : name === matchedName
44
+ ? "page"
45
+ : "true", "aria-keyshortcuts": shortcut, "aria-label": label, className: `${componentCssClassName}-link`, to: name, children: [icon, _jsx("span", { className: `${componentCssClassName}-label`, children: label }), shortcut === undefined ? null : (_jsx("kbd", { className: `${componentCssClassName}-shortcut`, children: shortcut }))] }) }, name))) }) }));
46
+ }
@@ -0,0 +1 @@
1
+ export { default as Navigation } from "./Navigation.js";
@@ -0,0 +1,44 @@
1
+ /* The lens list. Every link is one atom (48px: a 24px line between two
2
+ * 12px paddings) and keeps the same box whatever its state: the mark for the
3
+ * current lens is a colour change on a border every link already has, at the
4
+ * leading edge, so nothing moves or changes weight when it appears. */
5
+
6
+ /* Layer: lib, the layer @canonical/styles keeps for a library outside the
7
+ * design system, above every design-system layer and below the site's app. */
8
+ @layer lib {
9
+ .prism-navigation-list {
10
+ margin: 0;
11
+ padding: 0;
12
+ list-style: none;
13
+ }
14
+
15
+ .prism-navigation-link {
16
+ display: flex;
17
+ align-items: center;
18
+ gap: var(--space-100);
19
+ min-block-size: var(--space-600);
20
+ padding-block: var(--space-150);
21
+ padding-inline: var(--space-200);
22
+ border-inline-start: var(--space-050) solid transparent;
23
+ line-height: var(--space-300);
24
+ color: var(--color-text);
25
+ text-decoration: none;
26
+ }
27
+
28
+ .prism-navigation-link:hover {
29
+ text-decoration: underline;
30
+ }
31
+
32
+ .prism-navigation-link[aria-current] {
33
+ border-inline-start-color: var(--color-text);
34
+ }
35
+
36
+ .prism-navigation-label {
37
+ flex: 1 1 auto;
38
+ }
39
+
40
+ .prism-navigation-shortcut {
41
+ color: var(--color-text-muted);
42
+ font: inherit;
43
+ }
44
+ }
@@ -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 { prismPageHeaderFragment } from "../graphql/index.js";
4
+ import { useProjection, withLabel } from "../projection/index.js";
5
+ import PageHeaderView from "./PageHeaderView.js";
6
+ /**
7
+ * The header of a thing's page, for any kind of thing: it reads the thing's
8
+ * slots and draws the site's header for the thing's nearest class, or the
9
+ * plain `PageHeaderView`.
10
+ */
11
+ export default function PageHeader({ node, ...props }) {
12
+ const data = useFragment(prismPageHeaderFragment, node);
13
+ const { uri, Override, home } = useProjection(data, "header");
14
+ const View = Override ?? PageHeaderView;
15
+ return (_jsx(View, { ...props, home: home, slots: withLabel(data._meta.slots, data._meta.curie, uri), uri: uri }));
16
+ }
@@ -0,0 +1,18 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Chip } from "../Chip/index.js";
3
+ import { FactValue } from "../Fact/index.js";
4
+ import { Prose } from "../Prose/index.js";
5
+ import readParents from "./readParents.js";
6
+ import "./styles.css";
7
+ const componentCssClassName = "prism-page-header";
8
+ /**
9
+ * The plain header of a thing's page: the thing's parents as chips, farthest
10
+ * first, then its label as the page's heading, its category and location,
11
+ * its version and status as a list of terms, and its description. A slot
12
+ * the thing does not have takes no room.
13
+ */
14
+ export default function PageHeaderView({ uri, slots, home: _home, className, children, ...props }) {
15
+ const { label, category, location, version, status, description } = slots;
16
+ const parents = readParents(uri, slots);
17
+ return (_jsxs("header", { className: [componentCssClassName, className].filter(Boolean).join(" "), ...props, children: [parents.length > 0 ? (_jsx("nav", { "aria-label": "Parents", className: `${componentCssClassName}-parents`, children: _jsx("ol", { children: parents.map((parent) => (_jsx("li", { children: _jsx(Chip, { node: parent.node }) }, parent.uri))) }) })) : null, _jsx("h1", { className: `${componentCssClassName}-label`, dir: "auto", 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] }), version || status ? (_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] })) : null, description ? _jsx(Prose, { children: description }) : null, children] }));
18
+ }
@@ -0,0 +1,2 @@
1
+ export { default as PageHeader } from "./PageHeader.js";
2
+ export { default as PageHeaderView } from "./PageHeaderView.js";
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A thing's parents, farthest first, as many as the slots carry. Parents may
3
+ * form a cycle, so the walk stops at the first thing it has already met,
4
+ * the thing itself included.
5
+ */
6
+ export default function readParents(uri, slots) {
7
+ const seen = new Set([uri]);
8
+ const parents = [];
9
+ const first = slots.parent?.node;
10
+ const second = first?._meta.slots.parent?.node;
11
+ const third = second?._meta.slots.parent?.node;
12
+ for (const parent of [first, second, third]) {
13
+ if (!parent || seen.has(parent.uri))
14
+ break;
15
+ seen.add(parent.uri);
16
+ parents.push({ uri: parent.uri, node: parent });
17
+ }
18
+ return parents.reverse();
19
+ }
@@ -0,0 +1,67 @@
1
+ /* A thing's page header takes the whole row of the page's intrinsic grid
2
+ * and stacks its parts on the 8px rhythm: the parents, the heading, a line
3
+ * with the category in the muted colour and the location in ink, the terms
4
+ * and the description. */
5
+
6
+ /* Layer: lib, the layer @canonical/styles keeps for a library outside the
7
+ * design system, above every design-system layer and below the site's app. */
8
+ @layer lib {
9
+ .prism-page-header {
10
+ display: grid;
11
+ row-gap: var(--space-100);
12
+ padding: var(--space-300) var(--grid-margin);
13
+ }
14
+
15
+ .prism-page-header > * {
16
+ margin: 0;
17
+ min-inline-size: 0;
18
+ }
19
+
20
+ .prism-page-header-parents ol {
21
+ display: flex;
22
+ flex-wrap: wrap;
23
+ column-gap: var(--space-100);
24
+ margin: 0;
25
+ padding: 0;
26
+ list-style: none;
27
+ }
28
+
29
+ .prism-page-header-label {
30
+ overflow-wrap: anywhere;
31
+ }
32
+
33
+ .prism-page-header-kind {
34
+ display: flex;
35
+ flex-wrap: wrap;
36
+ column-gap: var(--space-100);
37
+ line-height: var(--space-300);
38
+ }
39
+
40
+ .prism-page-header-category {
41
+ color: var(--color-text-muted);
42
+ }
43
+
44
+ .prism-page-header-location {
45
+ color: var(--color-text);
46
+ }
47
+
48
+ .prism-page-header-terms {
49
+ display: flex;
50
+ flex-wrap: wrap;
51
+ column-gap: var(--space-300);
52
+ line-height: var(--space-300);
53
+ }
54
+
55
+ .prism-page-header-terms > div {
56
+ display: flex;
57
+ column-gap: var(--space-100);
58
+ }
59
+
60
+ .prism-page-header-terms dt {
61
+ color: var(--color-text-muted);
62
+ }
63
+
64
+ .prism-page-header-terms dd {
65
+ margin: 0;
66
+ }
67
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,14 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useMemo } from "react";
3
+ import PrismContext from "./context.js";
4
+ /** No replacement, the same object on every render. */
5
+ const NO_OVERRIDES = {};
6
+ /**
7
+ * Passes the site's replacements for the four projections, and its
8
+ * component for text from the graph, to everything below it. A site that
9
+ * replaces nothing renders no provider at all.
10
+ */
11
+ export default function Prism({ overrides = NO_OVERRIDES, Text, children, }) {
12
+ const value = useMemo(() => ({ overrides, Text }), [overrides, Text]);
13
+ return (_jsx(PrismContext.Provider, { value: value, children: children }));
14
+ }
@@ -0,0 +1,10 @@
1
+ import { createContext } from "react";
2
+ /**
3
+ * What is in force: no replacement and the plain text, unless a `Prism`
4
+ * provider passes its own.
5
+ */
6
+ const PrismContext = createContext({
7
+ overrides: {},
8
+ Text: undefined,
9
+ });
10
+ export default PrismContext;
@@ -0,0 +1,2 @@
1
+ export { default as PrismContext } from "./context.js";
2
+ export { default as Prism } from "./Prism.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,26 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useContext } from "react";
3
+ import { PrismContext } from "../Prism/index.js";
4
+ import "./styles.css";
5
+ const componentCssClassName = "prism-prose";
6
+ /**
7
+ * No mention yet: the contract cannot say which things a text mentions, so
8
+ * the site's text component receives none until it can.
9
+ */
10
+ const NO_MENTIONS = [];
11
+ /**
12
+ * Text from the graph, such as a description. The site's `Text` component,
13
+ * passed to `Prism`, draws it; the engine reads no markup of its own.
14
+ * Without one, the text is drawn as plain paragraphs, parted on blank lines,
15
+ * each trimmed and otherwise as written: no markup is interpreted and no
16
+ * link is followed.
17
+ */
18
+ export default function Prose({ children, className, ...props }) {
19
+ const { Text } = useContext(PrismContext);
20
+ return (_jsx("div", { className: [componentCssClassName, className].filter(Boolean).join(" "), ...props, children: Text ? (_jsx(Text, { mentions: NO_MENTIONS, text: children })) : (children
21
+ .split(/\n\s*\n/)
22
+ .map((paragraph) => paragraph.trim())
23
+ .filter(Boolean)
24
+ // biome-ignore lint/suspicious/noArrayIndexKey: paragraphs have no identity beyond their place in the text
25
+ .map((paragraph, index) => _jsx("p", { children: paragraph }, index))) }));
26
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * A target with every percent-escape read, reserved characters such as `#`,
3
+ * `/` and `?` included, or as it is when an escape is broken.
4
+ */
5
+ function decode(target) {
6
+ try {
7
+ return decodeURIComponent(target);
8
+ }
9
+ catch {
10
+ return target;
11
+ }
12
+ }
13
+ /**
14
+ * The mentioned thing a link points at, by its IRI or its compact name,
15
+ * written as is or percent-encoded, as a Markdown parser hands a link's
16
+ * target over. A site's link component calls it to draw `<Chip>` for a
17
+ * thing and an ordinary link for anything else.
18
+ */
19
+ export default function findMention(mentions, href) {
20
+ const target = decode(href);
21
+ return mentions.find(({ uri, curie }) => [uri, curie].some((name) => name === href || decode(name) === target));
22
+ }
@@ -0,0 +1,2 @@
1
+ export { default as findMention } from "./findMention.js";
2
+ export { default as Prose } from "./Prose.js";
@@ -0,0 +1,10 @@
1
+ /* The text keeps the design system's own typography; only the last block
2
+ * gives up its space after, so the text ends where its container does. */
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-prose > :last-child {
8
+ margin-block-end: 0;
9
+ }
10
+ }
@@ -0,0 +1 @@
1
+ export {};