@chromvoid/uikit 0.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 (246) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +96 -0
  3. package/dist/components/cv-accordion-item.d.ts +69 -0
  4. package/dist/components/cv-accordion-item.js +176 -0
  5. package/dist/components/cv-accordion.d.ts +79 -0
  6. package/dist/components/cv-accordion.js +310 -0
  7. package/dist/components/cv-alert-dialog.d.ts +86 -0
  8. package/dist/components/cv-alert-dialog.js +393 -0
  9. package/dist/components/cv-alert.d.ts +48 -0
  10. package/dist/components/cv-alert.js +156 -0
  11. package/dist/components/cv-badge.d.ts +56 -0
  12. package/dist/components/cv-badge.js +280 -0
  13. package/dist/components/cv-breadcrumb-item.d.ts +35 -0
  14. package/dist/components/cv-breadcrumb-item.js +64 -0
  15. package/dist/components/cv-breadcrumb.d.ts +39 -0
  16. package/dist/components/cv-breadcrumb.js +160 -0
  17. package/dist/components/cv-button.d.ts +83 -0
  18. package/dist/components/cv-button.js +541 -0
  19. package/dist/components/cv-callout.d.ts +32 -0
  20. package/dist/components/cv-callout.js +221 -0
  21. package/dist/components/cv-card.d.ts +48 -0
  22. package/dist/components/cv-card.js +269 -0
  23. package/dist/components/cv-carousel-slide.d.ts +25 -0
  24. package/dist/components/cv-carousel-slide.js +51 -0
  25. package/dist/components/cv-carousel.d.ts +96 -0
  26. package/dist/components/cv-carousel.js +457 -0
  27. package/dist/components/cv-checkbox.d.ts +84 -0
  28. package/dist/components/cv-checkbox.js +274 -0
  29. package/dist/components/cv-combobox-group.d.ts +15 -0
  30. package/dist/components/cv-combobox-group.js +34 -0
  31. package/dist/components/cv-combobox-option.d.ts +30 -0
  32. package/dist/components/cv-combobox-option.js +66 -0
  33. package/dist/components/cv-combobox.d.ts +135 -0
  34. package/dist/components/cv-combobox.js +834 -0
  35. package/dist/components/cv-command-item.d.ts +30 -0
  36. package/dist/components/cv-command-item.js +68 -0
  37. package/dist/components/cv-command-palette.d.ts +105 -0
  38. package/dist/components/cv-command-palette.js +578 -0
  39. package/dist/components/cv-context-menu.d.ts +98 -0
  40. package/dist/components/cv-context-menu.js +515 -0
  41. package/dist/components/cv-copy-button.d.ts +61 -0
  42. package/dist/components/cv-copy-button.js +318 -0
  43. package/dist/components/cv-date-picker.d.ts +161 -0
  44. package/dist/components/cv-date-picker.js +803 -0
  45. package/dist/components/cv-dialog.d.ts +89 -0
  46. package/dist/components/cv-dialog.js +459 -0
  47. package/dist/components/cv-disclosure.d.ts +57 -0
  48. package/dist/components/cv-disclosure.js +241 -0
  49. package/dist/components/cv-drawer.d.ts +102 -0
  50. package/dist/components/cv-drawer.js +595 -0
  51. package/dist/components/cv-feed-article.d.ts +26 -0
  52. package/dist/components/cv-feed-article.js +52 -0
  53. package/dist/components/cv-feed.d.ts +62 -0
  54. package/dist/components/cv-feed.js +310 -0
  55. package/dist/components/cv-grid-cell.d.ts +30 -0
  56. package/dist/components/cv-grid-cell.js +57 -0
  57. package/dist/components/cv-grid-column.d.ts +30 -0
  58. package/dist/components/cv-grid-column.js +43 -0
  59. package/dist/components/cv-grid-row.d.ts +30 -0
  60. package/dist/components/cv-grid-row.js +42 -0
  61. package/dist/components/cv-grid.d.ts +119 -0
  62. package/dist/components/cv-grid.js +567 -0
  63. package/dist/components/cv-icon.d.ts +57 -0
  64. package/dist/components/cv-icon.js +352 -0
  65. package/dist/components/cv-input.d.ts +127 -0
  66. package/dist/components/cv-input.js +482 -0
  67. package/dist/components/cv-landmark.d.ts +32 -0
  68. package/dist/components/cv-landmark.js +62 -0
  69. package/dist/components/cv-link.d.ts +22 -0
  70. package/dist/components/cv-link.js +99 -0
  71. package/dist/components/cv-listbox-group.d.ts +15 -0
  72. package/dist/components/cv-listbox-group.js +42 -0
  73. package/dist/components/cv-listbox.d.ts +81 -0
  74. package/dist/components/cv-listbox.js +388 -0
  75. package/dist/components/cv-menu-button.d.ts +118 -0
  76. package/dist/components/cv-menu-button.js +822 -0
  77. package/dist/components/cv-menu-group.d.ts +20 -0
  78. package/dist/components/cv-menu-group.js +48 -0
  79. package/dist/components/cv-menu-item.d.ts +52 -0
  80. package/dist/components/cv-menu-item.js +105 -0
  81. package/dist/components/cv-menu.d.ts +62 -0
  82. package/dist/components/cv-menu.js +414 -0
  83. package/dist/components/cv-meter.d.ts +66 -0
  84. package/dist/components/cv-meter.js +154 -0
  85. package/dist/components/cv-number.d.ts +139 -0
  86. package/dist/components/cv-number.js +553 -0
  87. package/dist/components/cv-option.d.ts +30 -0
  88. package/dist/components/cv-option.js +84 -0
  89. package/dist/components/cv-popover.d.ts +87 -0
  90. package/dist/components/cv-popover.js +373 -0
  91. package/dist/components/cv-progress-ring.d.ts +45 -0
  92. package/dist/components/cv-progress-ring.js +169 -0
  93. package/dist/components/cv-progress.d.ts +45 -0
  94. package/dist/components/cv-progress.js +148 -0
  95. package/dist/components/cv-radio-group.d.ts +79 -0
  96. package/dist/components/cv-radio-group.js +398 -0
  97. package/dist/components/cv-radio.d.ts +36 -0
  98. package/dist/components/cv-radio.js +123 -0
  99. package/dist/components/cv-select-group.d.ts +15 -0
  100. package/dist/components/cv-select-group.js +44 -0
  101. package/dist/components/cv-select-option.d.ts +30 -0
  102. package/dist/components/cv-select-option.js +66 -0
  103. package/dist/components/cv-select.d.ts +128 -0
  104. package/dist/components/cv-select.js +666 -0
  105. package/dist/components/cv-sidebar-item.d.ts +26 -0
  106. package/dist/components/cv-sidebar-item.js +142 -0
  107. package/dist/components/cv-sidebar.d.ts +171 -0
  108. package/dist/components/cv-sidebar.js +767 -0
  109. package/dist/components/cv-slider-multi-thumb.d.ts +73 -0
  110. package/dist/components/cv-slider-multi-thumb.js +374 -0
  111. package/dist/components/cv-slider.d.ts +84 -0
  112. package/dist/components/cv-slider.js +328 -0
  113. package/dist/components/cv-spinbutton.d.ts +121 -0
  114. package/dist/components/cv-spinbutton.js +486 -0
  115. package/dist/components/cv-spinner.d.ts +18 -0
  116. package/dist/components/cv-spinner.js +95 -0
  117. package/dist/components/cv-switch.d.ts +81 -0
  118. package/dist/components/cv-switch.js +285 -0
  119. package/dist/components/cv-tab-panel.d.ts +20 -0
  120. package/dist/components/cv-tab-panel.js +37 -0
  121. package/dist/components/cv-tab.d.ts +40 -0
  122. package/dist/components/cv-tab.js +132 -0
  123. package/dist/components/cv-table-cell.d.ts +31 -0
  124. package/dist/components/cv-table-cell.js +49 -0
  125. package/dist/components/cv-table-column.d.ts +37 -0
  126. package/dist/components/cv-table-column.js +63 -0
  127. package/dist/components/cv-table-row.d.ts +30 -0
  128. package/dist/components/cv-table-row.js +45 -0
  129. package/dist/components/cv-table.d.ts +147 -0
  130. package/dist/components/cv-table.js +607 -0
  131. package/dist/components/cv-tabs.d.ts +70 -0
  132. package/dist/components/cv-tabs.js +524 -0
  133. package/dist/components/cv-textarea.d.ts +108 -0
  134. package/dist/components/cv-textarea.js +328 -0
  135. package/dist/components/cv-toast-region.d.ts +39 -0
  136. package/dist/components/cv-toast-region.js +162 -0
  137. package/dist/components/cv-toast.d.ts +67 -0
  138. package/dist/components/cv-toast.js +315 -0
  139. package/dist/components/cv-toolbar-item.d.ts +25 -0
  140. package/dist/components/cv-toolbar-item.js +72 -0
  141. package/dist/components/cv-toolbar-separator.d.ts +25 -0
  142. package/dist/components/cv-toolbar-separator.js +45 -0
  143. package/dist/components/cv-toolbar.d.ts +63 -0
  144. package/dist/components/cv-toolbar.js +295 -0
  145. package/dist/components/cv-tooltip.d.ts +83 -0
  146. package/dist/components/cv-tooltip.js +455 -0
  147. package/dist/components/cv-treegrid-cell.d.ts +30 -0
  148. package/dist/components/cv-treegrid-cell.js +57 -0
  149. package/dist/components/cv-treegrid-column.d.ts +37 -0
  150. package/dist/components/cv-treegrid-column.js +53 -0
  151. package/dist/components/cv-treegrid-row.d.ts +55 -0
  152. package/dist/components/cv-treegrid-row.js +90 -0
  153. package/dist/components/cv-treegrid.d.ts +96 -0
  154. package/dist/components/cv-treegrid.js +632 -0
  155. package/dist/components/cv-treeitem.d.ts +58 -0
  156. package/dist/components/cv-treeitem.js +144 -0
  157. package/dist/components/cv-treeview.d.ts +70 -0
  158. package/dist/components/cv-treeview.js +396 -0
  159. package/dist/components/cv-window-splitter.d.ts +79 -0
  160. package/dist/components/cv-window-splitter.js +316 -0
  161. package/dist/components/index.d.ts +94 -0
  162. package/dist/components/index.js +79 -0
  163. package/dist/dialog/create-dialog-controller.d.ts +31 -0
  164. package/dist/dialog/create-dialog-controller.js +320 -0
  165. package/dist/dialog/index.d.ts +2 -0
  166. package/dist/dialog/index.js +1 -0
  167. package/dist/form-associated/FormAssociatedReatomElement.d.ts +25 -0
  168. package/dist/form-associated/FormAssociatedReatomElement.js +70 -0
  169. package/dist/form-associated/withFormAssociated.d.ts +5 -0
  170. package/dist/form-associated/withFormAssociated.js +1 -0
  171. package/dist/index.d.ts +10 -0
  172. package/dist/index.js +9 -0
  173. package/dist/reatom-lit/ReatomLitElement.d.ts +27 -0
  174. package/dist/reatom-lit/ReatomLitElement.js +118 -0
  175. package/dist/reatom-lit/html.d.ts +4 -0
  176. package/dist/reatom-lit/html.js +10 -0
  177. package/dist/reatom-lit/index.d.ts +4 -0
  178. package/dist/reatom-lit/index.js +4 -0
  179. package/dist/reatom-lit/watch.d.ts +15 -0
  180. package/dist/reatom-lit/watch.js +40 -0
  181. package/dist/reatom-lit/withReatomElement.d.ts +4 -0
  182. package/dist/reatom-lit/withReatomElement.js +57 -0
  183. package/dist/register.d.ts +1 -0
  184. package/dist/register.js +84 -0
  185. package/dist/styles/component-styles.d.ts +4 -0
  186. package/dist/styles/component-styles.js +78 -0
  187. package/dist/theme/cv-theme-provider.d.ts +32 -0
  188. package/dist/theme/cv-theme-provider.js +110 -0
  189. package/dist/theme/index.d.ts +4 -0
  190. package/dist/theme/index.js +2 -0
  191. package/dist/theme/theme-engine.d.ts +4 -0
  192. package/dist/theme/theme-engine.js +67 -0
  193. package/dist/theme/tokens.css +265 -0
  194. package/dist/theme/types.d.ts +7 -0
  195. package/dist/theme/types.js +1 -0
  196. package/dist/toast/create-toast-controller.d.ts +12 -0
  197. package/dist/toast/create-toast-controller.js +12 -0
  198. package/dist/toast/index.d.ts +2 -0
  199. package/dist/toast/index.js +1 -0
  200. package/package.json +146 -0
  201. package/specs/_template.md +110 -0
  202. package/specs/components/accordion.md +207 -0
  203. package/specs/components/alert.md +83 -0
  204. package/specs/components/badge.md +183 -0
  205. package/specs/components/breadcrumb.md +152 -0
  206. package/specs/components/button.md +227 -0
  207. package/specs/components/callout.md +153 -0
  208. package/specs/components/card.md +192 -0
  209. package/specs/components/carousel.md +232 -0
  210. package/specs/components/checkbox.md +141 -0
  211. package/specs/components/combobox.md +427 -0
  212. package/specs/components/context-menu.md +375 -0
  213. package/specs/components/copy-button.md +236 -0
  214. package/specs/components/date-picker.md +290 -0
  215. package/specs/components/dialog.md +184 -0
  216. package/specs/components/disclosure.md +151 -0
  217. package/specs/components/drawer.md +216 -0
  218. package/specs/components/feed.md +266 -0
  219. package/specs/components/grid.md +423 -0
  220. package/specs/components/input.md +237 -0
  221. package/specs/components/landmark.md +92 -0
  222. package/specs/components/link.md +117 -0
  223. package/specs/components/listbox.md +327 -0
  224. package/specs/components/menu.md +508 -0
  225. package/specs/components/meter.md +148 -0
  226. package/specs/components/number.md +268 -0
  227. package/specs/components/option.md +167 -0
  228. package/specs/components/popover.md +207 -0
  229. package/specs/components/progress-ring.md +134 -0
  230. package/specs/components/progress.md +110 -0
  231. package/specs/components/radio.md +208 -0
  232. package/specs/components/select.md +305 -0
  233. package/specs/components/sidebar.md +204 -0
  234. package/specs/components/spinbutton.md +157 -0
  235. package/specs/components/spinner.md +83 -0
  236. package/specs/components/switch.md +145 -0
  237. package/specs/components/table.md +372 -0
  238. package/specs/components/tabs.md +242 -0
  239. package/specs/components/textarea.md +166 -0
  240. package/specs/components/theme.md +364 -0
  241. package/specs/components/toast.md +198 -0
  242. package/specs/components/toolbar.md +258 -0
  243. package/specs/components/tooltip.md +152 -0
  244. package/specs/components/treegrid.md +363 -0
  245. package/specs/components/treeview.md +263 -0
  246. package/specs/components/window-splitter.md +225 -0
@@ -0,0 +1,119 @@
1
+ import { type GridCellId, type GridFocusStrategy, type GridSelectionMode } from '@chromvoid/headless-ui/grid';
2
+ import type { PropertyValues } from 'lit';
3
+ import { ReatomLitElement } from '../reatom-lit/ReatomLitElement.js';
4
+ export interface CVGridEventDetail {
5
+ value: string | null;
6
+ activeCell: GridCellId | null;
7
+ selectedValues: string[];
8
+ }
9
+ export declare class CVGrid extends ReatomLitElement {
10
+ static elementName: string;
11
+ static get properties(): {
12
+ value: {
13
+ type: StringConstructor;
14
+ reflect: boolean;
15
+ };
16
+ selectedValues: {
17
+ attribute: boolean;
18
+ };
19
+ selectionMode: {
20
+ type: StringConstructor;
21
+ attribute: string;
22
+ reflect: boolean;
23
+ };
24
+ focusStrategy: {
25
+ type: StringConstructor;
26
+ attribute: string;
27
+ reflect: boolean;
28
+ };
29
+ selectionFollowsFocus: {
30
+ type: BooleanConstructor;
31
+ attribute: string;
32
+ reflect: boolean;
33
+ };
34
+ pageSize: {
35
+ type: NumberConstructor;
36
+ attribute: string;
37
+ reflect: boolean;
38
+ };
39
+ readOnly: {
40
+ type: BooleanConstructor;
41
+ attribute: string;
42
+ reflect: boolean;
43
+ };
44
+ ariaLabel: {
45
+ type: StringConstructor;
46
+ attribute: string;
47
+ };
48
+ ariaLabelledBy: {
49
+ type: StringConstructor;
50
+ attribute: string;
51
+ };
52
+ totalRowCount: {
53
+ type: NumberConstructor;
54
+ attribute: string;
55
+ reflect: boolean;
56
+ };
57
+ totalColumnCount: {
58
+ type: NumberConstructor;
59
+ attribute: string;
60
+ reflect: boolean;
61
+ };
62
+ };
63
+ value: string;
64
+ selectedValues: string[];
65
+ selectionMode: GridSelectionMode;
66
+ focusStrategy: GridFocusStrategy;
67
+ selectionFollowsFocus: boolean;
68
+ pageSize: number;
69
+ readOnly: boolean;
70
+ ariaLabel: string;
71
+ ariaLabelledBy: string;
72
+ totalRowCount: number;
73
+ totalColumnCount: number;
74
+ private readonly idBase;
75
+ private columnRecords;
76
+ private rowRecords;
77
+ private cellRecords;
78
+ private validCellMap;
79
+ private cellListeners;
80
+ private childObserver;
81
+ private model;
82
+ constructor();
83
+ static styles: import("lit").CSSResult[];
84
+ static define(): void;
85
+ connectedCallback(): void;
86
+ disconnectedCallback(): void;
87
+ private observeChildren;
88
+ private disconnectChildObserver;
89
+ willUpdate(changedProperties: PropertyValues): void;
90
+ updated(changedProperties: PropertyValues): void;
91
+ private resolveIndex;
92
+ private getColumnElements;
93
+ private getRowElements;
94
+ private getCellElements;
95
+ private ensureColumnValue;
96
+ private ensureRowValue;
97
+ private resolveCellColumnId;
98
+ private cellFromKey;
99
+ private keyFromCell;
100
+ private captureSnapshot;
101
+ private rebuildModelFromSlot;
102
+ private detachCellListeners;
103
+ private attachCellListeners;
104
+ private syncControlledValuesFromModel;
105
+ private syncElementsFromModel;
106
+ private getEventDetail;
107
+ private dispatchInput;
108
+ private dispatchChange;
109
+ private applyInteractionResult;
110
+ private setSelectedValuesInModel;
111
+ private focusActiveCell;
112
+ private handleCellFocus;
113
+ private handleCellClick;
114
+ private handleGridKeyDown;
115
+ private handleColumnsSlotChange;
116
+ private handleRowsSlotChange;
117
+ private handleRowSlotChange;
118
+ protected render(): import("lit").TemplateResult<1>;
119
+ }
@@ -0,0 +1,567 @@
1
+ import { createGrid, } from '@chromvoid/headless-ui/grid';
2
+ import { css, html, nothing } from 'lit';
3
+ import { ReatomLitElement } from '../reatom-lit/ReatomLitElement.js';
4
+ import { CVGridCell } from './cv-grid-cell.js';
5
+ import { CVGridColumn } from './cv-grid-column.js';
6
+ import { CVGridRow } from './cv-grid-row.js';
7
+ const gridKeysToPrevent = new Set([
8
+ 'ArrowUp',
9
+ 'ArrowDown',
10
+ 'ArrowLeft',
11
+ 'ArrowRight',
12
+ 'Home',
13
+ 'End',
14
+ 'PageUp',
15
+ 'PageDown',
16
+ 'Enter',
17
+ ' ',
18
+ 'Spacebar',
19
+ ]);
20
+ const cellKey = (rowId, colId) => `${rowId}::${colId}`;
21
+ const parseCellKey = (value) => {
22
+ const [rowId, colId, ...rest] = value.split('::');
23
+ if (rest.length > 0 || !rowId || !colId)
24
+ return null;
25
+ return { rowId, colId };
26
+ };
27
+ const sameSetMembers = (left, right) => {
28
+ if (left.length !== right.length)
29
+ return false;
30
+ const rightSet = new Set(right);
31
+ return left.every((value) => rightSet.has(value));
32
+ };
33
+ let cvGridNonce = 0;
34
+ export class CVGrid extends ReatomLitElement {
35
+ static elementName = 'cv-grid';
36
+ static get properties() {
37
+ return {
38
+ value: { type: String, reflect: true },
39
+ selectedValues: { attribute: false },
40
+ selectionMode: { type: String, attribute: 'selection-mode', reflect: true },
41
+ focusStrategy: { type: String, attribute: 'focus-strategy', reflect: true },
42
+ selectionFollowsFocus: { type: Boolean, attribute: 'selection-follows-focus', reflect: true },
43
+ pageSize: { type: Number, attribute: 'page-size', reflect: true },
44
+ readOnly: { type: Boolean, attribute: 'readonly', reflect: true },
45
+ ariaLabel: { type: String, attribute: 'aria-label' },
46
+ ariaLabelledBy: { type: String, attribute: 'aria-labelledby' },
47
+ totalRowCount: { type: Number, attribute: 'total-row-count', reflect: true },
48
+ totalColumnCount: { type: Number, attribute: 'total-column-count', reflect: true },
49
+ };
50
+ }
51
+ idBase = `cv-grid-${++cvGridNonce}`;
52
+ columnRecords = [];
53
+ rowRecords = [];
54
+ cellRecords = [];
55
+ validCellMap = new Map();
56
+ cellListeners = new WeakMap();
57
+ childObserver = null;
58
+ model;
59
+ constructor() {
60
+ super();
61
+ this.value = '';
62
+ this.selectedValues = [];
63
+ this.selectionMode = 'single';
64
+ this.focusStrategy = 'roving-tabindex';
65
+ this.selectionFollowsFocus = false;
66
+ this.pageSize = 10;
67
+ this.readOnly = false;
68
+ this.ariaLabel = '';
69
+ this.ariaLabelledBy = '';
70
+ this.totalRowCount = 0;
71
+ this.totalColumnCount = 0;
72
+ this.model = createGrid({
73
+ idBase: this.idBase,
74
+ rows: [],
75
+ columns: [],
76
+ ariaLabel: 'Grid',
77
+ });
78
+ }
79
+ static styles = [
80
+ css `
81
+ :host {
82
+ display: block;
83
+ }
84
+
85
+ [part='base'] {
86
+ display: table;
87
+ inline-size: 100%;
88
+ border-collapse: collapse;
89
+ border-spacing: 0;
90
+ border: 1px solid var(--cv-color-border, #2a3245);
91
+ border-radius: var(--cv-radius-md, 10px);
92
+ overflow: hidden;
93
+ background: var(--cv-color-surface, #141923);
94
+ }
95
+
96
+ [part='head'] {
97
+ display: table-header-group;
98
+ }
99
+
100
+ [part='head-row'] {
101
+ display: table-row;
102
+ }
103
+
104
+ [part='body'] {
105
+ display: table-row-group;
106
+ }
107
+ `,
108
+ ];
109
+ static define() {
110
+ if (!customElements.get(this.elementName)) {
111
+ customElements.define(this.elementName, this);
112
+ }
113
+ }
114
+ connectedCallback() {
115
+ super.connectedCallback();
116
+ this.rebuildModelFromSlot(false, false);
117
+ this.observeChildren();
118
+ }
119
+ disconnectedCallback() {
120
+ super.disconnectedCallback();
121
+ this.detachCellListeners();
122
+ this.disconnectChildObserver();
123
+ }
124
+ observeChildren() {
125
+ this.disconnectChildObserver();
126
+ this.childObserver = new MutationObserver((mutations) => {
127
+ let needsRebuild = false;
128
+ for (const mutation of mutations) {
129
+ for (const node of mutation.addedNodes) {
130
+ if (node instanceof HTMLElement) {
131
+ const tag = node.tagName.toLowerCase();
132
+ if (tag === CVGridColumn.elementName || tag === CVGridRow.elementName) {
133
+ needsRebuild = true;
134
+ }
135
+ }
136
+ }
137
+ for (const node of mutation.removedNodes) {
138
+ if (node instanceof HTMLElement) {
139
+ const tag = node.tagName.toLowerCase();
140
+ if (tag === CVGridColumn.elementName || tag === CVGridRow.elementName) {
141
+ needsRebuild = true;
142
+ }
143
+ }
144
+ }
145
+ }
146
+ if (needsRebuild) {
147
+ this.rebuildModelFromSlot(true, true);
148
+ }
149
+ });
150
+ this.childObserver.observe(this, { childList: true });
151
+ }
152
+ disconnectChildObserver() {
153
+ if (this.childObserver) {
154
+ this.childObserver.disconnect();
155
+ this.childObserver = null;
156
+ }
157
+ }
158
+ willUpdate(changedProperties) {
159
+ super.willUpdate(changedProperties);
160
+ if (changedProperties.has('selectionMode') ||
161
+ changedProperties.has('focusStrategy') ||
162
+ changedProperties.has('selectionFollowsFocus') ||
163
+ changedProperties.has('pageSize') ||
164
+ changedProperties.has('readOnly') ||
165
+ changedProperties.has('ariaLabel') ||
166
+ changedProperties.has('ariaLabelledBy') ||
167
+ changedProperties.has('totalRowCount') ||
168
+ changedProperties.has('totalColumnCount')) {
169
+ this.rebuildModelFromSlot(true, false);
170
+ return;
171
+ }
172
+ if (changedProperties.has('value')) {
173
+ const normalized = this.value.trim();
174
+ if (this.value !== normalized) {
175
+ this.value = normalized;
176
+ }
177
+ const nextCell = parseCellKey(normalized);
178
+ const modelActiveKey = this.captureSnapshot().activeKey;
179
+ const nextKey = nextCell ? cellKey(nextCell.rowId, nextCell.colId) : null;
180
+ if (nextCell && nextKey != null && nextKey !== modelActiveKey && this.validCellMap.has(nextKey)) {
181
+ const previous = this.captureSnapshot();
182
+ this.model.actions.setActiveCell(nextCell);
183
+ this.applyInteractionResult(previous);
184
+ }
185
+ }
186
+ if (changedProperties.has('selectedValues')) {
187
+ const modelSelected = this.captureSnapshot().selectedKeys;
188
+ const normalized = [
189
+ ...new Set(this.selectedValues.map((value) => value.trim()).filter((value) => value.length > 0)),
190
+ ];
191
+ if (sameSetMembers(normalized, modelSelected)) {
192
+ return;
193
+ }
194
+ const previous = this.captureSnapshot();
195
+ this.setSelectedValuesInModel(normalized);
196
+ this.applyInteractionResult(previous);
197
+ }
198
+ }
199
+ updated(changedProperties) {
200
+ super.updated(changedProperties);
201
+ if (!changedProperties.has('value') && !changedProperties.has('selectedValues')) {
202
+ this.syncElementsFromModel();
203
+ }
204
+ }
205
+ resolveIndex(value) {
206
+ if (!Number.isFinite(value) || value < 1) {
207
+ return undefined;
208
+ }
209
+ return Math.floor(value);
210
+ }
211
+ getColumnElements() {
212
+ return Array.from(this.children).filter((element) => element.tagName.toLowerCase() === CVGridColumn.elementName);
213
+ }
214
+ getRowElements() {
215
+ return Array.from(this.children).filter((element) => element.tagName.toLowerCase() === CVGridRow.elementName);
216
+ }
217
+ getCellElements(row) {
218
+ return Array.from(row.children).filter((element) => element.tagName.toLowerCase() === CVGridCell.elementName);
219
+ }
220
+ ensureColumnValue(column, index) {
221
+ const normalized = column.value?.trim();
222
+ if (normalized)
223
+ return normalized;
224
+ const fallback = `column-${index + 1}`;
225
+ column.value = fallback;
226
+ return fallback;
227
+ }
228
+ ensureRowValue(row, index) {
229
+ const normalized = row.value?.trim();
230
+ if (normalized)
231
+ return normalized;
232
+ const fallback = `row-${index + 1}`;
233
+ row.value = fallback;
234
+ return fallback;
235
+ }
236
+ resolveCellColumnId(cell, index) {
237
+ const normalized = cell.column?.trim();
238
+ if (normalized)
239
+ return normalized;
240
+ const fallback = this.columnRecords[index]?.id ?? '';
241
+ cell.column = fallback;
242
+ return fallback;
243
+ }
244
+ cellFromKey(key) {
245
+ return parseCellKey(key);
246
+ }
247
+ keyFromCell(cell) {
248
+ if (!cell)
249
+ return null;
250
+ return cellKey(cell.rowId, cell.colId);
251
+ }
252
+ captureSnapshot() {
253
+ return {
254
+ activeKey: this.keyFromCell(this.model.state.activeCellId()),
255
+ selectedKeys: [...this.model.state.selectedCellIds()],
256
+ };
257
+ }
258
+ rebuildModelFromSlot(preserveState, requestRender = true) {
259
+ const previous = preserveState
260
+ ? this.captureSnapshot()
261
+ : {
262
+ activeKey: this.value.trim() || null,
263
+ selectedKeys: [
264
+ ...new Set(this.selectedValues.map((value) => value.trim()).filter((value) => value.length > 0)),
265
+ ],
266
+ };
267
+ this.detachCellListeners();
268
+ this.columnRecords = [];
269
+ this.rowRecords = [];
270
+ this.cellRecords = [];
271
+ this.validCellMap.clear();
272
+ this.columnRecords = this.getColumnElements().map((element, index) => {
273
+ const id = this.ensureColumnValue(element, index);
274
+ element.slot = 'columns';
275
+ return {
276
+ id,
277
+ index: this.resolveIndex(element.index),
278
+ disabled: element.disabled,
279
+ element,
280
+ };
281
+ });
282
+ const validColumnIds = new Set(this.columnRecords.map((column) => column.id));
283
+ const disabledCells = [];
284
+ this.rowRecords = this.getRowElements().map((row, rowIndex) => {
285
+ const id = this.ensureRowValue(row, rowIndex);
286
+ row.slot = 'rows';
287
+ const cells = this.getCellElements(row).map((cell, cellIndex) => {
288
+ const colId = this.resolveCellColumnId(cell, cellIndex);
289
+ const key = cellKey(id, colId);
290
+ const valid = validColumnIds.has(colId);
291
+ const record = {
292
+ key,
293
+ rowId: id,
294
+ colId,
295
+ disabled: cell.disabled,
296
+ valid,
297
+ element: cell,
298
+ };
299
+ this.cellRecords.push(record);
300
+ if (valid) {
301
+ this.validCellMap.set(key, record);
302
+ if (record.disabled) {
303
+ disabledCells.push({ rowId: id, colId });
304
+ }
305
+ }
306
+ return record;
307
+ });
308
+ return {
309
+ id,
310
+ index: this.resolveIndex(row.index),
311
+ disabled: row.disabled,
312
+ cells,
313
+ element: row,
314
+ };
315
+ });
316
+ const initialActiveCell = previous.activeKey && this.validCellMap.has(previous.activeKey)
317
+ ? this.cellFromKey(previous.activeKey)
318
+ : null;
319
+ const initialSelectedCells = previous.selectedKeys
320
+ .filter((key) => this.validCellMap.has(key))
321
+ .slice(0, this.selectionMode === 'single' ? 1 : undefined)
322
+ .map((key) => this.cellFromKey(key))
323
+ .filter((cell) => cell != null);
324
+ const normalizedAriaLabel = this.ariaLabel.trim();
325
+ const normalizedAriaLabelledBy = this.ariaLabelledBy.trim();
326
+ this.model = createGrid({
327
+ idBase: this.idBase,
328
+ rows: this.rowRecords.map((row) => ({
329
+ id: row.id,
330
+ index: row.index,
331
+ disabled: row.disabled,
332
+ })),
333
+ columns: this.columnRecords.map((column) => ({
334
+ id: column.id,
335
+ index: column.index,
336
+ disabled: column.disabled,
337
+ })),
338
+ disabledCells,
339
+ ariaLabel: normalizedAriaLabel || (!normalizedAriaLabelledBy ? 'Grid' : undefined),
340
+ ariaLabelledBy: normalizedAriaLabelledBy || undefined,
341
+ focusStrategy: this.focusStrategy,
342
+ selectionMode: this.selectionMode,
343
+ selectionFollowsFocus: this.selectionFollowsFocus,
344
+ pageSize: this.pageSize > 0 ? this.pageSize : 1,
345
+ totalRowCount: this.totalRowCount > 0 ? this.totalRowCount : undefined,
346
+ totalColumnCount: this.totalColumnCount > 0 ? this.totalColumnCount : undefined,
347
+ initialActiveCellId: initialActiveCell,
348
+ initialSelectedCellIds: initialSelectedCells,
349
+ isReadOnly: this.readOnly,
350
+ });
351
+ this.attachCellListeners();
352
+ this.syncElementsFromModel();
353
+ this.syncControlledValuesFromModel();
354
+ if (requestRender) {
355
+ this.requestUpdate();
356
+ }
357
+ }
358
+ detachCellListeners() {
359
+ for (const record of this.cellRecords) {
360
+ const listeners = this.cellListeners.get(record.element);
361
+ if (!listeners)
362
+ continue;
363
+ record.element.removeEventListener('focus', listeners.focus);
364
+ record.element.removeEventListener('click', listeners.click);
365
+ this.cellListeners.delete(record.element);
366
+ }
367
+ }
368
+ attachCellListeners() {
369
+ for (const record of this.cellRecords) {
370
+ if (!record.valid)
371
+ continue;
372
+ const focus = () => this.handleCellFocus(record.rowId, record.colId);
373
+ const click = (event) => this.handleCellClick(event, record.rowId, record.colId);
374
+ record.element.addEventListener('focus', focus);
375
+ record.element.addEventListener('click', click);
376
+ this.cellListeners.set(record.element, { focus, click });
377
+ }
378
+ }
379
+ syncControlledValuesFromModel() {
380
+ const activeKey = this.keyFromCell(this.model.state.activeCellId()) ?? '';
381
+ const selectedKeys = [...this.model.state.selectedCellIds()];
382
+ this.value = activeKey;
383
+ this.selectedValues = selectedKeys;
384
+ }
385
+ syncElementsFromModel() {
386
+ const gridProps = this.model.contracts.getGridProps();
387
+ for (const [index, column] of this.columnRecords.entries()) {
388
+ column.element.slot = 'columns';
389
+ column.element.setAttribute('role', 'columnheader');
390
+ column.element.setAttribute('aria-colindex', String(column.index ?? index + 1));
391
+ if (column.disabled) {
392
+ column.element.setAttribute('aria-disabled', 'true');
393
+ }
394
+ else {
395
+ column.element.removeAttribute('aria-disabled');
396
+ }
397
+ }
398
+ for (const row of this.rowRecords) {
399
+ const rowProps = this.model.contracts.getRowProps(row.id);
400
+ row.element.id = rowProps.id;
401
+ row.element.slot = 'rows';
402
+ row.element.setAttribute('role', rowProps.role);
403
+ row.element.setAttribute('aria-rowindex', String(rowProps['aria-rowindex']));
404
+ for (const cell of row.cells) {
405
+ cell.element.hidden = !cell.valid;
406
+ if (!cell.valid)
407
+ continue;
408
+ const cellProps = this.model.contracts.getCellProps(cell.rowId, cell.colId);
409
+ cell.element.id = cellProps.id;
410
+ cell.element.setAttribute('role', cellProps.role);
411
+ cell.element.setAttribute('tabindex', cellProps.tabindex);
412
+ cell.element.setAttribute('aria-colindex', String(cellProps['aria-colindex']));
413
+ cell.element.setAttribute('aria-selected', cellProps['aria-selected']);
414
+ cell.element.setAttribute('data-active', cellProps['data-active']);
415
+ if (cellProps['aria-readonly']) {
416
+ cell.element.setAttribute('aria-readonly', cellProps['aria-readonly']);
417
+ }
418
+ else {
419
+ cell.element.removeAttribute('aria-readonly');
420
+ }
421
+ if (cellProps['aria-disabled']) {
422
+ cell.element.setAttribute('aria-disabled', cellProps['aria-disabled']);
423
+ }
424
+ else {
425
+ cell.element.removeAttribute('aria-disabled');
426
+ }
427
+ cell.element.active = cellProps['data-active'] === 'true';
428
+ cell.element.selected = cellProps['aria-selected'] === 'true';
429
+ cell.element.disabled = cellProps['aria-disabled'] === 'true';
430
+ }
431
+ }
432
+ const root = this.shadowRoot?.querySelector('[part="base"]');
433
+ if (root) {
434
+ root.id = gridProps.id;
435
+ }
436
+ }
437
+ getEventDetail() {
438
+ return {
439
+ value: this.value.trim() || null,
440
+ activeCell: this.model.state.activeCellId(),
441
+ selectedValues: [...this.model.state.selectedCellIds()],
442
+ };
443
+ }
444
+ dispatchInput(detail) {
445
+ this.dispatchEvent(new CustomEvent('cv-input', {
446
+ detail,
447
+ bubbles: true,
448
+ composed: true,
449
+ }));
450
+ }
451
+ dispatchChange(detail) {
452
+ this.dispatchEvent(new CustomEvent('cv-change', {
453
+ detail,
454
+ bubbles: true,
455
+ composed: true,
456
+ }));
457
+ }
458
+ applyInteractionResult(previous) {
459
+ this.syncElementsFromModel();
460
+ const next = this.captureSnapshot();
461
+ this.syncControlledValuesFromModel();
462
+ const activeChanged = previous.activeKey !== next.activeKey;
463
+ const selectionChanged = !sameSetMembers(previous.selectedKeys, next.selectedKeys);
464
+ if (!activeChanged && !selectionChanged)
465
+ return;
466
+ const detail = this.getEventDetail();
467
+ this.dispatchInput(detail);
468
+ this.dispatchChange(detail);
469
+ }
470
+ setSelectedValuesInModel(values) {
471
+ const parsed = values
472
+ .map((key) => this.cellFromKey(key))
473
+ .filter((cell) => cell != null)
474
+ .filter((cell) => this.validCellMap.has(cellKey(cell.rowId, cell.colId)));
475
+ this.model.state.selectedCellIds.set(new Set());
476
+ if (parsed.length === 0)
477
+ return;
478
+ if (this.selectionMode === 'single') {
479
+ const first = parsed[0];
480
+ if (first) {
481
+ this.model.actions.selectCell(first);
482
+ }
483
+ return;
484
+ }
485
+ for (const cell of parsed) {
486
+ this.model.actions.toggleCellSelection(cell);
487
+ }
488
+ }
489
+ focusActiveCell() {
490
+ if (this.focusStrategy !== 'roving-tabindex')
491
+ return;
492
+ const activeKey = this.keyFromCell(this.model.state.activeCellId());
493
+ if (!activeKey)
494
+ return;
495
+ const record = this.validCellMap.get(activeKey);
496
+ if (!record || record.element.disabled)
497
+ return;
498
+ record.element.focus();
499
+ }
500
+ handleCellFocus(rowId, colId) {
501
+ const previous = this.captureSnapshot();
502
+ this.model.contracts.getCellProps(rowId, colId).onFocus();
503
+ this.applyInteractionResult(previous);
504
+ }
505
+ handleCellClick(event, rowId, colId) {
506
+ const props = this.model.contracts.getCellProps(rowId, colId);
507
+ if (props['aria-disabled'] === 'true')
508
+ return;
509
+ const previous = this.captureSnapshot();
510
+ const cell = { rowId, colId };
511
+ this.model.actions.setActiveCell(cell);
512
+ if (this.selectionMode === 'multiple' && (event.metaKey || event.ctrlKey)) {
513
+ this.model.actions.toggleCellSelection(cell);
514
+ }
515
+ else {
516
+ this.model.actions.selectCell(cell);
517
+ }
518
+ this.applyInteractionResult(previous);
519
+ this.focusActiveCell();
520
+ }
521
+ handleGridKeyDown(event) {
522
+ if (gridKeysToPrevent.has(event.key)) {
523
+ event.preventDefault();
524
+ }
525
+ const previous = this.captureSnapshot();
526
+ this.model.actions.handleKeyDown(event);
527
+ this.applyInteractionResult(previous);
528
+ this.focusActiveCell();
529
+ }
530
+ handleColumnsSlotChange() {
531
+ this.rebuildModelFromSlot(true, true);
532
+ }
533
+ handleRowsSlotChange() {
534
+ this.rebuildModelFromSlot(true, true);
535
+ }
536
+ handleRowSlotChange() {
537
+ this.rebuildModelFromSlot(true, true);
538
+ }
539
+ render() {
540
+ const gridProps = this.model.contracts.getGridProps();
541
+ return html `
542
+ <div
543
+ id=${gridProps.id}
544
+ role=${gridProps.role}
545
+ tabindex=${gridProps.tabindex}
546
+ aria-label=${gridProps['aria-label'] ?? nothing}
547
+ aria-labelledby=${gridProps['aria-labelledby'] ?? nothing}
548
+ aria-multiselectable=${gridProps['aria-multiselectable']}
549
+ aria-colcount=${String(gridProps['aria-colcount'])}
550
+ aria-rowcount=${String(gridProps['aria-rowcount'])}
551
+ aria-activedescendant=${gridProps['aria-activedescendant'] ?? nothing}
552
+ part="base"
553
+ @keydown=${this.handleGridKeyDown}
554
+ >
555
+ <div role="rowgroup" part="head">
556
+ <div role="row" part="head-row">
557
+ <slot name="columns" @slotchange=${this.handleColumnsSlotChange}></slot>
558
+ </div>
559
+ </div>
560
+
561
+ <div role="rowgroup" part="body" @cv-grid-row-slotchange=${this.handleRowSlotChange}>
562
+ <slot name="rows" @slotchange=${this.handleRowsSlotChange}></slot>
563
+ </div>
564
+ </div>
565
+ `;
566
+ }
567
+ }