@parallelogram-js/dashboard 0.0.0-stage → 0.1.1

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 (77) hide show
  1. package/CHANGELOG.md +113 -0
  2. package/LICENSE +21 -0
  3. package/README.md +1390 -2
  4. package/dist/parallelogram-dashboard.css +9401 -0
  5. package/dist/parallelogram-dashboard.css.map +1 -0
  6. package/dist/parallelogram-dashboard.min.css +1 -0
  7. package/package.json +72 -4
  8. package/scss/_base.scss +403 -0
  9. package/scss/_datetime.scss +50 -0
  10. package/scss/_icon.scss +40 -0
  11. package/scss/_index.scss +60 -0
  12. package/scss/_mixins.scss +396 -0
  13. package/scss/_tokens.scss +340 -0
  14. package/scss/_utilities.scss +464 -0
  15. package/scss/_variables.scss +219 -0
  16. package/scss/components/_accordion.scss +297 -0
  17. package/scss/components/_actionbar.scss +333 -0
  18. package/scss/components/_alert.scss +183 -0
  19. package/scss/components/_avatar.scss +203 -0
  20. package/scss/components/_badge.scss +176 -0
  21. package/scss/components/_banner.scss +344 -0
  22. package/scss/components/_breadcrumb.scss +220 -0
  23. package/scss/components/_button.scss +264 -0
  24. package/scss/components/_card.scss +256 -0
  25. package/scss/components/_chart.scss +246 -0
  26. package/scss/components/_command.scss +267 -0
  27. package/scss/components/_dashboard.scss +681 -0
  28. package/scss/components/_data-list.scss +280 -0
  29. package/scss/components/_dropdown.scss +318 -0
  30. package/scss/components/_empty.scss +226 -0
  31. package/scss/components/_filebrowser.scss +906 -0
  32. package/scss/components/_form-layout.scss +332 -0
  33. package/scss/components/_input.scss +310 -0
  34. package/scss/components/_list.scss +130 -0
  35. package/scss/components/_media.scss +239 -0
  36. package/scss/components/_modal.scss +298 -0
  37. package/scss/components/_navbar.scss +312 -0
  38. package/scss/components/_notification.scss +59 -0
  39. package/scss/components/_pageheader.scss +99 -0
  40. package/scss/components/_pagination.scss +165 -0
  41. package/scss/components/_progress.scss +333 -0
  42. package/scss/components/_separator.scss +129 -0
  43. package/scss/components/_sheet.scss +286 -0
  44. package/scss/components/_sidebar.scss +324 -0
  45. package/scss/components/_skeleton.scss +246 -0
  46. package/scss/components/_stat.scss +218 -0
  47. package/scss/components/_table.scss +589 -0
  48. package/scss/components/_tabs.scss +266 -0
  49. package/scss/components/_timeline.scss +339 -0
  50. package/scss/components/_toast.scss +259 -0
  51. package/scss/components/_toolbar.scss +301 -0
  52. package/scss/components/_tooltip.scss +250 -0
  53. package/scss/main.scss +6 -0
  54. package/src/js/behaviours/accordion.js +58 -0
  55. package/src/js/behaviours/action-bar-list.js +406 -0
  56. package/src/js/behaviours/action-bar.js +29 -0
  57. package/src/js/behaviours/chart.js +339 -0
  58. package/src/js/behaviours/collapsible.js +34 -0
  59. package/src/js/behaviours/editable-table.js +91 -0
  60. package/src/js/behaviours/expandable.js +80 -0
  61. package/src/js/behaviours/overflow-actions.js +137 -0
  62. package/src/js/behaviours/publish-schedule.js +91 -0
  63. package/src/js/behaviours/search-clear.js +19 -0
  64. package/src/js/behaviours/segmented-control.js +43 -0
  65. package/src/js/behaviours/submenu.js +73 -0
  66. package/src/js/behaviours/table-columns.js +77 -0
  67. package/src/js/behaviours/table-paste.js +305 -0
  68. package/src/js/behaviours/table-reorder.js +252 -0
  69. package/src/js/behaviours/table-sort.js +101 -0
  70. package/src/js/behaviours/tabs.js +148 -0
  71. package/src/js/behaviours/toast-manager.js +106 -0
  72. package/src/js/behaviours/toggle.js +186 -0
  73. package/src/js/behaviours/unsaved-guard.js +88 -0
  74. package/src/js/core-loader.js +26 -0
  75. package/src/js/index.js +85 -0
  76. package/src/js/polyfills/dialog-commands.js +20 -0
  77. package/src/js/util/dom.js +39 -0
@@ -0,0 +1,186 @@
1
+ // ============================================================================
2
+ // Toggle Component
3
+ // ============================================================================
4
+
5
+ export class Toggle {
6
+ /** Open capture-mode toggles across every instance, so opening one dropdown closes the others. */
7
+ static open = new Map();
8
+
9
+ constructor() {
10
+ this.elements = new WeakMap();
11
+ }
12
+
13
+ mount(element) {
14
+ if (this.elements.has(element)) return;
15
+
16
+ const targetSelector = element.dataset.toggleTarget;
17
+ if (!targetSelector) return;
18
+
19
+ const target = document.querySelector(targetSelector);
20
+ if (!target) return;
21
+
22
+ const capture = element.dataset.toggleCapture === 'true';
23
+ const manual = element.dataset.toggleManual === 'true';
24
+ // Optional extra class on the target for markup that is not styled from aria-expanded
25
+ const targetClass = element.dataset.toggleClass || '';
26
+
27
+ // Optional backdrop shown with the target (e.g. .sidebaroverlay); clicking it closes
28
+ const overlay = element.dataset.toggleOverlay ? document.querySelector(element.dataset.toggleOverlay) : null;
29
+
30
+ const state = {
31
+ target,
32
+ targetSelector,
33
+ capture,
34
+ manual,
35
+ targetClass,
36
+ overlay,
37
+ overlayClass: overlay ? `${overlay.classList[0]}--visible` : '',
38
+ isOpen: element.getAttribute('aria-expanded') === 'true'
39
+ || target.hasAttribute('open')
40
+ || (targetClass !== '' && target.classList.contains(targetClass)),
41
+ };
42
+
43
+ overlay?.addEventListener('click', () => this.hide(element));
44
+
45
+ // A [data-toggle-close] button inside the target closes it and hands focus back to the trigger
46
+ target.querySelectorAll('[data-toggle-close]').forEach((close) => {
47
+ close.addEventListener('click', () => {
48
+ this.hide(element);
49
+ element.focus();
50
+ });
51
+ });
52
+
53
+ // Click handler
54
+ element.addEventListener('click', (e) => {
55
+ e.preventDefault();
56
+ e.stopPropagation();
57
+ this.toggle(element);
58
+ });
59
+
60
+ // The stylesheet reads aria-expanded on the trigger, so it must always be present
61
+ element.setAttribute('aria-expanded', String(state.isOpen));
62
+ if (target.id) element.setAttribute('aria-controls', target.id);
63
+
64
+ // Escape key
65
+ document.addEventListener('keydown', (e) => {
66
+ if (e.key === 'Escape' && state.isOpen && !state.manual) {
67
+ e.preventDefault();
68
+ this.hide(element);
69
+ }
70
+ });
71
+
72
+ // Outside click for capture mode
73
+ if (capture) {
74
+ document.addEventListener('click', (e) => {
75
+ if (state.isOpen && !state.manual && !element.contains(e.target) && !target.contains(e.target)) {
76
+ this.hide(element);
77
+ }
78
+ });
79
+ }
80
+
81
+ element.setAttribute('data-toggle-enhanced', 'true');
82
+ this.elements.set(element, state);
83
+ if (state.isOpen && capture && !manual) Toggle.open.set(element, this);
84
+ }
85
+
86
+ toggle(element) {
87
+ const state = this.elements.get(element);
88
+ if (!state) return;
89
+
90
+ if (state.isOpen) {
91
+ this.hide(element);
92
+ } else {
93
+ this.show(element);
94
+ }
95
+ }
96
+
97
+ show(element) {
98
+ const state = this.elements.get(element);
99
+ if (state?.capture && !state.manual) {
100
+ // The trigger's click doesn't reach the document, so other dropdowns never see it as outside
101
+ for (const [other, owner] of Toggle.open) {
102
+ const nested = owner.elements.get(other)?.target.contains(element) || state.target.contains(other);
103
+ if (other !== element && !nested) owner.hide(other);
104
+ }
105
+ }
106
+ this._set(element, true);
107
+ }
108
+
109
+ hide(element) {
110
+ this._set(element, false);
111
+ }
112
+
113
+ _set(element, open) {
114
+ const state = this.elements.get(element);
115
+ if (!state) return;
116
+
117
+ state.isOpen = open;
118
+ element.setAttribute('aria-expanded', String(open));
119
+ if (state.capture && !state.manual) {
120
+ if (open) Toggle.open.set(element, this);
121
+ else Toggle.open.delete(element);
122
+ }
123
+
124
+ if (state.targetClass) {
125
+ state.target.classList.toggle(state.targetClass, open);
126
+ }
127
+ if (state.overlay) {
128
+ state.overlay.classList.toggle(state.overlayClass, open);
129
+ }
130
+
131
+ // <dialog> and <details> targets carry their own native open state
132
+ if (state.target instanceof HTMLDialogElement || state.target instanceof HTMLDetailsElement) {
133
+ state.target.toggleAttribute('open', open);
134
+ }
135
+
136
+ if (state.target.hasAttribute('popover') && state.target.showPopover) {
137
+ this._float(element, state, open);
138
+ }
139
+ }
140
+
141
+ /** Show a [popover] menu in the top layer, pinned to its trigger while it is open. */
142
+ _float(element, state, open) {
143
+ const menu = state.target;
144
+ if (state.reposition) {
145
+ window.removeEventListener('scroll', state.reposition, true);
146
+ window.removeEventListener('resize', state.reposition);
147
+ state.reposition = null;
148
+ }
149
+ if (!open) {
150
+ if (menu.matches(':popover-open')) menu.hidePopover();
151
+ return;
152
+ }
153
+ if (!menu.matches(':popover-open')) menu.showPopover();
154
+ state.reposition = () => Toggle._pin(element, menu);
155
+ state.reposition();
156
+ window.addEventListener('scroll', state.reposition, true);
157
+ window.addEventListener('resize', state.reposition);
158
+ }
159
+
160
+ /** Place a floating menu under its trigger, flipping above or to the other side when there isn't room. */
161
+ static _pin(trigger, menu) {
162
+ const edge = 8;
163
+ const t = trigger.getBoundingClientRect();
164
+ const m = menu.getBoundingClientRect();
165
+ const alignRight = menu.matches('.dropdown__menu--right') || Boolean(trigger.closest('.dropdown--right, .actionbar__more'));
166
+ let left = alignRight ? t.right - m.width : t.left;
167
+ if (left + m.width > window.innerWidth - edge) left = t.right - m.width;
168
+ const above = t.bottom + m.height > window.innerHeight - edge && t.top - m.height > edge;
169
+ menu.style.right = 'auto';
170
+ menu.style.left = `${Math.max(edge, left)}px`;
171
+ menu.style.top = `${above ? t.top - m.height - 8 : t.bottom}px`;
172
+ }
173
+
174
+ /** Close every open dropdown, e.g. when a modal takes over the page. */
175
+ static closeAll() {
176
+ for (const [element, owner] of Toggle.open) owner.hide(element);
177
+ }
178
+
179
+ static enhanceAll(selector = '[data-toggle]', root = document) {
180
+ const instance = new Toggle();
181
+ root.querySelectorAll(selector).forEach(el => instance.mount(el));
182
+ return instance;
183
+ }
184
+ }
185
+
186
+ document.addEventListener('p-modal:open', () => Toggle.closeAll());
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Tracks unsaved edits in a [data-unsaved-guard] area.
3
+ *
4
+ * Each field's value is recorded when the area is enhanced; while any differs (so undoing an edit
5
+ * makes it clean again) the area gets data-dirty, as does the .tabs__trigger of any tab panel
6
+ * holding a changed field, and leaving the page asks first. Fields inside [data-unsaved-ignore]
7
+ * (e.g. rows that save themselves) don't count, and fields added later start clean. Dispatch
8
+ * "unsaved:reset" on the area after saving to take the current values as saved. The area fires
9
+ * "unsaved:change" with { dirty } when its state flips.
10
+ */
11
+ export class UnsavedGuard {
12
+ static dirty = new Set();
13
+
14
+ static {
15
+ window.addEventListener('beforeunload', (event) => {
16
+ if (!UnsavedGuard.dirty.size) return;
17
+ event.preventDefault();
18
+ event.returnValue = '';
19
+ });
20
+ }
21
+
22
+ constructor() {
23
+ this.saved = new WeakMap();
24
+ }
25
+
26
+ mount(area) {
27
+ if (this.saved.has(area)) return;
28
+ this._record(area);
29
+ const check = () => this._check(area);
30
+ area.addEventListener('input', check);
31
+ area.addEventListener('change', check);
32
+ area.addEventListener('unsaved:reset', () => {
33
+ this._record(area);
34
+ this._check(area);
35
+ });
36
+ }
37
+
38
+ /** Native fields, plus custom form fields with a name such as <p-datetime>. */
39
+ _fields(area) {
40
+ return [...area.querySelectorAll('input, select, textarea, [name]')]
41
+ .filter(field => /^(INPUT|SELECT|TEXTAREA)$/.test(field.tagName) || field.localName.includes('-'))
42
+ .filter(field => !['button', 'submit', 'reset', 'file', 'search'].includes(field.type))
43
+ .filter(field => !field.closest('[data-unsaved-ignore]'));
44
+ }
45
+
46
+ /** A disabled field isn't submitted, so it counts as empty; a custom field not yet loaded reads its attribute. */
47
+ _value(field) {
48
+ if (field.disabled || field.hasAttribute('disabled')) return '';
49
+ if (field.type === 'checkbox' || field.type === 'radio') return field.checked;
50
+ return field.value ?? field.getAttribute('value') ?? '';
51
+ }
52
+
53
+ _record(area) {
54
+ this.saved.set(area, new Map(this._fields(area).map(field => [field, this._value(field)])));
55
+ }
56
+
57
+ _check(area) {
58
+ const saved = this.saved.get(area);
59
+ const panels = new Set();
60
+ let dirty = false;
61
+ for (const field of this._fields(area)) {
62
+ if (!saved.has(field)) saved.set(field, this._value(field));
63
+ if (saved.get(field) === this._value(field)) continue;
64
+ dirty = true;
65
+ const panel = field.closest('[data-tab-panel]');
66
+ if (panel) panels.add(panel.id);
67
+ }
68
+
69
+ area.querySelectorAll('.tabs__trigger[data-tab]').forEach((trigger) => {
70
+ const changed = panels.has(trigger.dataset.tab);
71
+ trigger.toggleAttribute('data-dirty', changed);
72
+ if (changed) trigger.setAttribute('aria-description', 'Unsaved changes');
73
+ else trigger.removeAttribute('aria-description');
74
+ });
75
+
76
+ if (dirty === area.hasAttribute('data-dirty')) return;
77
+ area.toggleAttribute('data-dirty', dirty);
78
+ if (dirty) UnsavedGuard.dirty.add(area);
79
+ else UnsavedGuard.dirty.delete(area);
80
+ area.dispatchEvent(new CustomEvent('unsaved:change', { bubbles: true, detail: { dirty } }));
81
+ }
82
+
83
+ static enhanceAll(selector = '[data-unsaved-guard]', root = document) {
84
+ const instance = new UnsavedGuard();
85
+ root.querySelectorAll(selector).forEach(el => instance.mount(el));
86
+ return instance;
87
+ }
88
+ }
@@ -0,0 +1,26 @@
1
+ const WEB_COMPONENTS = {
2
+ 'p-modal': () => import('@parallelogram-js/core/components/PModal.js'),
3
+ 'p-datetime': () => import('@parallelogram-js/core/components/PDatetime.js'),
4
+ };
5
+
6
+ /**
7
+ * Load the @parallelogram-js/core pieces that markup inside root relies on.
8
+ *
9
+ * Each piece is a literal dynamic import, so a bundler splits it into its own chunk and nothing
10
+ * is fetched when a page does not use these components.
11
+ */
12
+ export async function loadParallelogramJs(root) {
13
+ const loads = Object.entries(WEB_COMPONENTS)
14
+ .filter(([tag]) => root.querySelector(tag) && !customElements.get(tag))
15
+ .map(([, load]) => load());
16
+
17
+ if (root.querySelector('[data-modal][data-modal-target]')) {
18
+ loads.push(import('@parallelogram-js/core/components/Modal.js').then(({ default: Modal }) => Modal.enhanceAll()));
19
+ }
20
+
21
+ try {
22
+ await Promise.all(loads);
23
+ } catch (error) {
24
+ console.error('Could not load @parallelogram-js/core, which <p-modal>, <p-datetime> and [data-modal] need.', error);
25
+ }
26
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Parallelogram Dashboard
3
+ *
4
+ * Progressive enhancement for the dashboard components: markup works without JS and is
5
+ * enhanced by init(). State lives in native and ARIA attributes (aria-expanded, aria-selected,
6
+ * hidden, open); the stylesheet reads those, so no state classes are toggled here.
7
+ */
8
+
9
+ import { Accordion } from './behaviours/accordion.js';
10
+ import { Collapsible } from './behaviours/collapsible.js';
11
+ import { Submenu } from './behaviours/submenu.js';
12
+ import { Tabs } from './behaviours/tabs.js';
13
+ import { SegmentedControl } from './behaviours/segmented-control.js';
14
+ import { Toggle } from './behaviours/toggle.js';
15
+ import { Expandable } from './behaviours/expandable.js';
16
+ import { TableColumns } from './behaviours/table-columns.js';
17
+ import { TableSort } from './behaviours/table-sort.js';
18
+ import { TableReorder } from './behaviours/table-reorder.js';
19
+ import { TablePaste } from './behaviours/table-paste.js';
20
+ import { PublishSchedule } from './behaviours/publish-schedule.js';
21
+ import { UnsavedGuard } from './behaviours/unsaved-guard.js';
22
+ import { EditableTable } from './behaviours/editable-table.js';
23
+ import { Chart } from './behaviours/chart.js';
24
+ import { OverflowActions } from './behaviours/overflow-actions.js';
25
+ import { ActionBar } from './behaviours/action-bar.js';
26
+ import { ActionBarList } from './behaviours/action-bar-list.js';
27
+ import { SearchClear } from './behaviours/search-clear.js';
28
+ import { ToastManager } from './behaviours/toast-manager.js';
29
+ import { loadParallelogramJs } from './core-loader.js';
30
+
31
+ export {
32
+ Accordion,
33
+ Collapsible,
34
+ Submenu,
35
+ Tabs,
36
+ SegmentedControl,
37
+ Toggle,
38
+ Expandable,
39
+ TableColumns,
40
+ TableSort,
41
+ TableReorder,
42
+ TablePaste,
43
+ PublishSchedule,
44
+ UnsavedGuard,
45
+ EditableTable,
46
+ Chart,
47
+ OverflowActions,
48
+ ActionBar,
49
+ ActionBarList,
50
+ SearchClear,
51
+ ToastManager,
52
+ };
53
+ export { loadParallelogramJs } from './core-loader.js';
54
+ export { polyfillDialogCommands } from './polyfills/dialog-commands.js';
55
+
56
+ /**
57
+ * Enhance every interactive component inside root.
58
+ *
59
+ * Safe to call repeatedly on freshly rendered markup, e.g. from the playground preview.
60
+ */
61
+ export function init(root = document) {
62
+ loadParallelogramJs(root);
63
+
64
+ return {
65
+ // Action bars and overflow actions run first so the "more" menus they create get their toggle behaviour
66
+ actionbar: ActionBar.enhanceAll('.actionbar', root),
67
+ overflowActions: OverflowActions.enhanceAll('[data-overflow-actions]', root),
68
+ actionbarList: ActionBarList.enhanceAll('.actionbar__list', root),
69
+ accordion: Accordion.enhanceAll('[data-accordion]', root),
70
+ collapsible: Collapsible.enhanceAll('.collapsible', root),
71
+ submenu: Submenu.enhanceAll('.dropdown__submenu', root),
72
+ tabs: Tabs.enhanceAll('[data-tabs]', root),
73
+ segmentedControl: SegmentedControl.enhanceAll('.segmented-control', root),
74
+ toggle: Toggle.enhanceAll('[data-toggle]', root),
75
+ editableTable: EditableTable.enhanceAll('.table--editable', root),
76
+ publishSchedule: PublishSchedule.enhanceAll('[data-publish-schedule]', root),
77
+ unsavedGuard: UnsavedGuard.enhanceAll('[data-unsaved-guard]', root),
78
+ expandable: Expandable.enhanceAll('[data-expandable]', root),
79
+ tableColumns: TableColumns.enhanceAll('[data-table-columns]', root),
80
+ tableSort: TableSort.enhanceAll('[data-table-sort]', root),
81
+ tableReorder: TableReorder.enhanceAll('[data-table-reorder]', root),
82
+ tablePaste: TablePaste.enhanceAll('[data-table-paste]', root),
83
+ chart: Chart.enhanceAll('[data-chart]', root),
84
+ };
85
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Fallback for the Invoker Commands API (button commandfor/command) in browsers without it,
3
+ * covering the dialog commands the overlay components use.
4
+ */
5
+ export function polyfillDialogCommands() {
6
+ if ('commandForElement' in HTMLButtonElement.prototype) return;
7
+
8
+ document.addEventListener('click', (event) => {
9
+ const button = event.target.closest('button[commandfor][command]');
10
+ const target = button && document.getElementById(button.getAttribute('commandfor'));
11
+ if (!(target instanceof HTMLDialogElement)) return;
12
+
13
+ const command = button.getAttribute('command');
14
+ if (command === 'show-modal' && !target.open) {
15
+ target.showModal();
16
+ } else if (command === 'close' && target.open) {
17
+ target.close();
18
+ }
19
+ });
20
+ }
@@ -0,0 +1,39 @@
1
+ let generatedId = 0;
2
+
3
+ /**
4
+ * Give an element an id if it has none, so ARIA relationships can point at it.
5
+ */
6
+ export function ensureId(element, prefix) {
7
+ if (!element.id) {
8
+ generatedId += 1;
9
+ element.id = `${prefix}-${generatedId}`;
10
+ }
11
+ return element.id;
12
+ }
13
+
14
+ export function isDisabled(element) {
15
+ return element.disabled || element.getAttribute('aria-disabled') === 'true';
16
+ }
17
+
18
+ /**
19
+ * Point a disclosure button at the region it controls and make sure it never submits a form.
20
+ */
21
+ export function linkTrigger(trigger, region) {
22
+ if (!trigger.hasAttribute('type')) trigger.type = 'button';
23
+ if (!trigger.hasAttribute('aria-expanded')) trigger.setAttribute('aria-expanded', 'false');
24
+ if (region) trigger.setAttribute('aria-controls', ensureId(region, 'disclosure'));
25
+ }
26
+
27
+ /** Text made safe to put inside HTML markup or a quoted attribute. */
28
+ export function escapeHtml(value) {
29
+ return String(value).replace(/[&<>"']/g, char => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[char]);
30
+ }
31
+
32
+ /**
33
+ * The point a dragged item compares with its neighbours' midpoints: its bottom edge while it sits
34
+ * below its slot (moving down) and its top edge while above (moving up). A neighbour therefore
35
+ * gives way once the dragged item covers half of it, rather than all of it.
36
+ */
37
+ export function leadingEdge(slotTop, top, height) {
38
+ return top > slotTop ? top + height : top;
39
+ }