@og-ui/core 0.0.1 → 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 (53) hide show
  1. package/LICENSE +21 -0
  2. package/dist/base.css +81 -0
  3. package/dist/components/accordion.css +69 -0
  4. package/dist/components/alert.css +44 -0
  5. package/dist/components/avatar.css +57 -0
  6. package/dist/components/badge.css +56 -0
  7. package/dist/components/button.css +114 -0
  8. package/dist/components/card.css +93 -0
  9. package/dist/components/checkbox.css +104 -0
  10. package/dist/components/dialog.css +170 -0
  11. package/dist/components/input.css +67 -0
  12. package/dist/components/pagination.css +52 -0
  13. package/dist/components/popover.css +71 -0
  14. package/dist/components/progress.css +53 -0
  15. package/dist/components/select.css +51 -0
  16. package/dist/components/skeleton.css +33 -0
  17. package/dist/components/slider.css +56 -0
  18. package/dist/components/surface.css +40 -0
  19. package/dist/components/switch.css +44 -0
  20. package/dist/components/table.css +48 -0
  21. package/dist/components/tabs.css +62 -0
  22. package/dist/components/toast.css +106 -0
  23. package/dist/components/tooltip.css +40 -0
  24. package/dist/og-ui.css +1507 -0
  25. package/dist/og-ui.js +312 -0
  26. package/dist/og-ui.min.css +2 -0
  27. package/package.json +32 -6
  28. package/src/_mixins.scss +90 -0
  29. package/src/base.scss +100 -0
  30. package/src/components/_accordion.scss +81 -0
  31. package/src/components/_alert.scss +56 -0
  32. package/src/components/_avatar.scss +52 -0
  33. package/src/components/_badge.scss +55 -0
  34. package/src/components/_button.scss +116 -0
  35. package/src/components/_card.scss +79 -0
  36. package/src/components/_checkbox.scss +100 -0
  37. package/src/components/_dialog.scss +142 -0
  38. package/src/components/_input.scss +70 -0
  39. package/src/components/_pagination.scss +58 -0
  40. package/src/components/_popover.scss +78 -0
  41. package/src/components/_progress.scss +68 -0
  42. package/src/components/_select.scss +45 -0
  43. package/src/components/_skeleton.scss +44 -0
  44. package/src/components/_slider.scss +71 -0
  45. package/src/components/_surface.scss +27 -0
  46. package/src/components/_switch.scss +46 -0
  47. package/src/components/_table.scss +59 -0
  48. package/src/components/_tabs.scss +69 -0
  49. package/src/components/_toast.scss +113 -0
  50. package/src/components/_tooltip.scss +52 -0
  51. package/src/js/og-ui.js +312 -0
  52. package/src/og-ui.scss +28 -0
  53. package/README.md +0 -5
@@ -0,0 +1,113 @@
1
+ @use '../mixins' as *;
2
+
3
+ // Created by og-ui.js: ogToast('Saved', { variant: 'success', description: '…' })
4
+ // Lives in a single aria-live region in the corner. Markup if you render it yourself:
5
+ // <ol class="og-toast-region" aria-live="polite"><li class="og-toast og-toast--success">…</li></ol>
6
+ .og-toast-region {
7
+ position: fixed;
8
+ right: 1rem;
9
+ bottom: 1rem;
10
+ z-index: 2147483600;
11
+ display: flex;
12
+ flex-direction: column;
13
+ gap: 0.5rem;
14
+ width: min(22rem, calc(100vw - 2rem));
15
+ margin: 0;
16
+ padding: 0;
17
+ list-style: none;
18
+ pointer-events: none;
19
+ }
20
+
21
+ .og-toast {
22
+ @include surface(overlay);
23
+
24
+ --_c: var(--og-color-text-muted);
25
+
26
+ position: relative;
27
+ display: grid;
28
+ grid-template-columns: auto 1fr auto;
29
+ align-items: start;
30
+ gap: 0.25rem 0.75rem;
31
+ padding: 0.875rem 1rem;
32
+ border-radius: var(--og-radius-md);
33
+ color: var(--og-color-text);
34
+ font-family: var(--og-font);
35
+ font-size: 0.875rem;
36
+ pointer-events: auto;
37
+ animation: og-toast-in var(--og-motion-duration) var(--og-easing);
38
+
39
+ &::before {
40
+ content: '';
41
+ width: 0.5rem;
42
+ height: 0.5rem;
43
+ margin-top: 0.375rem;
44
+ border-radius: var(--og-radius-full);
45
+ background: var(--_c);
46
+ }
47
+
48
+ &__title {
49
+ font-weight: var(--og-font-weight-strong);
50
+ }
51
+
52
+ &__description {
53
+ grid-column: 2;
54
+ color: var(--og-color-text-muted);
55
+ }
56
+
57
+ &__close {
58
+ grid-row: 1;
59
+ grid-column: 3;
60
+ display: inline-grid;
61
+ place-items: center;
62
+ width: 1.5rem;
63
+ height: 1.5rem;
64
+ border: 0;
65
+ border-radius: var(--og-radius-sm);
66
+ background: transparent;
67
+ color: var(--og-color-text-muted);
68
+ cursor: pointer;
69
+
70
+ &::after {
71
+ @include icon($icon-close, 0.875rem);
72
+ }
73
+
74
+ @include focus-ring;
75
+ }
76
+
77
+ &--success {
78
+ --_c: var(--og-color-success);
79
+ }
80
+
81
+ &--warning {
82
+ --_c: var(--og-color-warning);
83
+ }
84
+
85
+ &--danger {
86
+ --_c: var(--og-color-danger);
87
+ }
88
+
89
+ &--accent {
90
+ --_c: var(--og-color-accent);
91
+ }
92
+
93
+ &[data-leaving] {
94
+ opacity: 0;
95
+ transform: translateY(6px);
96
+ transition:
97
+ opacity var(--og-motion-duration) var(--og-easing),
98
+ transform var(--og-motion-duration) var(--og-easing);
99
+ }
100
+ }
101
+
102
+ @keyframes og-toast-in {
103
+ from {
104
+ opacity: 0;
105
+ transform: translateY(8px) scale(0.98);
106
+ }
107
+ }
108
+
109
+ @media (prefers-reduced-motion: reduce) {
110
+ .og-toast {
111
+ animation: none;
112
+ }
113
+ }
@@ -0,0 +1,52 @@
1
+ // <span class="og-tooltip" data-tip="Copy link"><button aria-label="Copy link">…</button></span>
2
+ // CSS-only: shows on hover and keyboard focus. Put the same text in aria-label so screen
3
+ // readers announce it (the tooltip itself is decorative). og-ui.js lets Esc hide it.
4
+ // Placement: data-tip-side="bottom" (default top).
5
+ .og-tooltip {
6
+ position: relative;
7
+ display: inline-flex;
8
+
9
+ &::after {
10
+ content: attr(data-tip);
11
+ position: absolute;
12
+ bottom: calc(100% + 0.5rem);
13
+ left: 50%;
14
+ z-index: 50;
15
+ padding: 0.375rem 0.625rem;
16
+ border-radius: var(--og-radius-sm);
17
+ background: var(--og-color-text);
18
+ color: var(--og-color-bg);
19
+ font-family: var(--og-font);
20
+ font-size: 0.75rem;
21
+ font-weight: 500;
22
+ line-height: 1.3;
23
+ white-space: nowrap;
24
+ opacity: 0;
25
+ transform: translate(-50%, 4px);
26
+ transition:
27
+ opacity var(--og-motion-duration) var(--og-easing),
28
+ transform var(--og-motion-duration) var(--og-easing);
29
+ pointer-events: none;
30
+ }
31
+
32
+ &:hover::after,
33
+ &:focus-within::after {
34
+ opacity: 1;
35
+ transform: translate(-50%, 0);
36
+ transition-delay: 300ms;
37
+ }
38
+
39
+ &:focus-within::after {
40
+ transition-delay: 0ms;
41
+ }
42
+
43
+ // Esc hides it until focus and the pointer leave (og-ui.js; WCAG 1.4.13).
44
+ &[data-og-tip-hidden]::after {
45
+ opacity: 0;
46
+ }
47
+
48
+ &[data-tip-side='bottom']::after {
49
+ top: calc(100% + 0.5rem);
50
+ bottom: auto;
51
+ }
52
+ }
@@ -0,0 +1,312 @@
1
+ /**
2
+ * OG UI behaviours: the few things HTML and CSS can't do alone.
3
+ *
4
+ * Everything else is native: <dialog> traps focus, popovers light-dismiss, <details>
5
+ * opens and closes, form inputs are real inputs. This file only adds:
6
+ * - data-og-open / data-og-close for dialogs and drawers, plus click-outside to close
7
+ * - placing popovers and menus next to the button that opened them
8
+ * - arrow-key navigation for menus (role="menu") and tabs ([data-og-tabs])
9
+ * - the filled part of range sliders (.og-slider)
10
+ * - ogToast() notifications
11
+ *
12
+ * It uses event delegation on the document, so content added later just works.
13
+ * Import once: import { initOgUI, ogToast } from '@og-ui/core/og-ui.js'; initOgUI();
14
+ * No framework, no dependencies, about 3 KB gzipped.
15
+ */
16
+
17
+ let started = false;
18
+
19
+ /** Start listening. Safe to call more than once. */
20
+ export function initOgUI(root = document) {
21
+ syncSliders(root);
22
+ if (started) return;
23
+ started = true;
24
+ // Screen readers only announce changes to a live region that already existed.
25
+ toastRegion();
26
+
27
+ document.addEventListener('click', onClick);
28
+ document.addEventListener('keydown', onKeydown);
29
+ document.addEventListener('focusout', onTooltipLeave);
30
+ document.addEventListener('mouseout', onTooltipLeave);
31
+ document.addEventListener('input', onInput);
32
+ document.addEventListener('toggle', onToggle, true);
33
+ window.addEventListener('resize', repositionOpenPopovers, { passive: true });
34
+ window.addEventListener('scroll', repositionOpenPopovers, { passive: true, capture: true });
35
+ new MutationObserver(onThemeChange).observe(document.documentElement, {
36
+ attributes: true,
37
+ subtree: true,
38
+ attributeFilter: ['data-og-theme', 'data-og-mode'],
39
+ });
40
+ }
41
+
42
+ // ---------------------------------------------------------------------------
43
+ // Theme switches: no transitions for the frame the theme changes in
44
+
45
+ let switching = 0;
46
+
47
+ function onThemeChange() {
48
+ // Runs as a microtask, before the browser recalculates styles for the change.
49
+ const html = document.documentElement;
50
+ html.setAttribute('data-og-switching', '');
51
+ cancelAnimationFrame(switching);
52
+ switching = requestAnimationFrame(() => {
53
+ switching = requestAnimationFrame(() => html.removeAttribute('data-og-switching'));
54
+ });
55
+ }
56
+
57
+ // ---------------------------------------------------------------------------
58
+ // Dialogs and drawers
59
+
60
+ function onClick(event) {
61
+ const target = event.target;
62
+ if (!(target instanceof Element)) return;
63
+
64
+ // aria-disabled links stay focusable so screen readers find them, but must not activate.
65
+ if (target.closest('a[aria-disabled="true"]')) {
66
+ event.preventDefault();
67
+ return;
68
+ }
69
+
70
+ const opener = target.closest('[data-og-open]');
71
+ if (opener) {
72
+ const dialog = document.getElementById(opener.getAttribute('data-og-open'));
73
+ if (dialog instanceof HTMLDialogElement) dialog.showModal();
74
+ return;
75
+ }
76
+
77
+ const closer = target.closest('[data-og-close]');
78
+ if (closer) {
79
+ closer.closest('dialog')?.close(closer.getAttribute('data-og-close') || undefined);
80
+ return;
81
+ }
82
+
83
+ // A click on the dialog element itself (not its content) is a click on the backdrop.
84
+ if (target instanceof HTMLDialogElement && target.matches('.og-dialog') && target.open) {
85
+ const box = target.getBoundingClientRect();
86
+ const inside =
87
+ event.clientX >= box.left &&
88
+ event.clientX <= box.right &&
89
+ event.clientY >= box.top &&
90
+ event.clientY <= box.bottom;
91
+ if (!inside) target.close();
92
+ return;
93
+ }
94
+
95
+ // Choosing a menu item closes its menu.
96
+ const item = target.closest('[role="menuitem"]');
97
+ const menu = item?.closest('[popover]');
98
+ if (menu && !item.hasAttribute('aria-haspopup')) menu.hidePopover();
99
+
100
+ // Remember which button opened a popover, to place the popover next to it.
101
+ const invoker = target.closest('[popovertarget]');
102
+ if (invoker) lastInvoker.set(invoker.getAttribute('popovertarget'), invoker);
103
+
104
+ // Tabs
105
+ const tab = target.closest('[role="tab"]');
106
+ if (tab?.closest('[data-og-tabs]')) selectTab(tab);
107
+ }
108
+
109
+ // ---------------------------------------------------------------------------
110
+ // Popovers and menus
111
+
112
+ const lastInvoker = new Map();
113
+
114
+ function onToggle(event) {
115
+ const el = event.target;
116
+ if (!(el instanceof HTMLElement) || !el.matches('.og-popover')) return;
117
+ if (event.newState !== 'open') return;
118
+
119
+ placePopover(el);
120
+ if (el.getAttribute('role') === 'menu') {
121
+ const items = menuItems(el);
122
+ items[0]?.focus();
123
+ }
124
+ }
125
+
126
+ function placePopover(el) {
127
+ const trigger =
128
+ lastInvoker.get(el.id) || document.querySelector(`[popovertarget="${CSS.escape(el.id)}"]`);
129
+ if (!trigger) return;
130
+
131
+ const gap = 6;
132
+ const t = trigger.getBoundingClientRect();
133
+ const p = el.getBoundingClientRect();
134
+ const vw = document.documentElement.clientWidth;
135
+ const vh = document.documentElement.clientHeight;
136
+
137
+ let top = t.bottom + gap;
138
+ if (top + p.height > vh - 8 && t.top - gap - p.height > 8) top = t.top - gap - p.height;
139
+
140
+ let left = t.left;
141
+ if (left + p.width > vw - 8) left = Math.max(8, t.right - p.width);
142
+
143
+ el.style.top = `${Math.round(top)}px`;
144
+ el.style.left = `${Math.round(left)}px`;
145
+ }
146
+
147
+ function repositionOpenPopovers() {
148
+ document.querySelectorAll('.og-popover:popover-open').forEach(placePopover);
149
+ }
150
+
151
+ function menuItems(menu) {
152
+ return [...menu.querySelectorAll('[role="menuitem"]:not([disabled]):not([aria-disabled="true"])')];
153
+ }
154
+
155
+ // ---------------------------------------------------------------------------
156
+ // Keyboard: tooltips, menus and tabs
157
+
158
+ function onKeydown(event) {
159
+ const target = event.target;
160
+ if (!(target instanceof Element)) return;
161
+
162
+ // Esc hides an open tooltip without moving focus (WCAG 1.4.13).
163
+ if (event.key === 'Escape') {
164
+ document
165
+ .querySelectorAll('.og-tooltip:hover, .og-tooltip:focus-within')
166
+ .forEach((tip) => tip.setAttribute('data-og-tip-hidden', ''));
167
+ }
168
+
169
+ const menu = target.closest('[role="menu"]');
170
+ if (menu) {
171
+ const items = menuItems(menu);
172
+ const i = items.indexOf(target);
173
+ const go = (n) => {
174
+ event.preventDefault();
175
+ items[(n + items.length) % items.length]?.focus();
176
+ };
177
+ if (event.key === 'ArrowDown') go(i + 1);
178
+ else if (event.key === 'ArrowUp') go(i - 1);
179
+ else if (event.key === 'Home') go(0);
180
+ else if (event.key === 'End') go(items.length - 1);
181
+ else if (event.key === 'Tab') menu.hidePopover?.();
182
+ return;
183
+ }
184
+
185
+ const tab = target.closest('[role="tab"]');
186
+ const tabs = tab?.closest('[data-og-tabs]');
187
+ if (tabs) {
188
+ const list = [...tabs.querySelectorAll('[role="tab"]:not([disabled])')];
189
+ const i = list.indexOf(tab);
190
+ const vertical = tab.closest('[role="tablist"]')?.getAttribute('aria-orientation') === 'vertical';
191
+ const next = vertical ? 'ArrowDown' : 'ArrowRight';
192
+ const prev = vertical ? 'ArrowUp' : 'ArrowLeft';
193
+ let to = null;
194
+ if (event.key === next) to = list[(i + 1) % list.length];
195
+ else if (event.key === prev) to = list[(i - 1 + list.length) % list.length];
196
+ else if (event.key === 'Home') to = list[0];
197
+ else if (event.key === 'End') to = list[list.length - 1];
198
+ if (to) {
199
+ event.preventDefault();
200
+ selectTab(to);
201
+ to.focus();
202
+ }
203
+ }
204
+ }
205
+
206
+ /** A tooltip hidden with Esc shows again once focus and the pointer have left it. */
207
+ function onTooltipLeave(event) {
208
+ const tip = event.target instanceof Element && event.target.closest('.og-tooltip[data-og-tip-hidden]');
209
+ if (tip && !tip.contains(event.relatedTarget)) tip.removeAttribute('data-og-tip-hidden');
210
+ }
211
+
212
+ function selectTab(tab) {
213
+ const tabs = tab.closest('[data-og-tabs]');
214
+ tabs.querySelectorAll('[role="tab"]').forEach((t) => {
215
+ const on = t === tab;
216
+ t.setAttribute('aria-selected', String(on));
217
+ t.tabIndex = on ? 0 : -1;
218
+ const panel = document.getElementById(t.getAttribute('aria-controls'));
219
+ if (panel) panel.hidden = !on;
220
+ });
221
+ tabs.dispatchEvent(new CustomEvent('og-tab-change', { detail: { tab }, bubbles: true }));
222
+ }
223
+
224
+ // ---------------------------------------------------------------------------
225
+ // Sliders
226
+
227
+ function onInput(event) {
228
+ if (event.target instanceof HTMLInputElement && event.target.matches('.og-slider')) {
229
+ syncSlider(event.target);
230
+ }
231
+ }
232
+
233
+ function syncSlider(input) {
234
+ const min = Number(input.min || 0);
235
+ const max = Number(input.max || 100);
236
+ const pct = max === min ? 0 : ((Number(input.value) - min) / (max - min)) * 100;
237
+ input.style.setProperty('--_value', `${pct}%`);
238
+ }
239
+
240
+ function syncSliders(root) {
241
+ root.querySelectorAll?.('.og-slider').forEach(syncSlider);
242
+ }
243
+
244
+ // ---------------------------------------------------------------------------
245
+ // Toasts
246
+
247
+ let region = null;
248
+
249
+ function toastRegion() {
250
+ if (!region || !region.isConnected) {
251
+ region = document.createElement('ol');
252
+ region.className = 'og-toast-region';
253
+ region.setAttribute('aria-live', 'polite');
254
+ region.setAttribute('aria-label', 'Notifications');
255
+ document.body.append(region);
256
+ }
257
+ return region;
258
+ }
259
+
260
+ /**
261
+ * Show a notification.
262
+ * @param {string} title
263
+ * @param {{ description?: string, variant?: 'success'|'warning'|'danger'|'accent', duration?: number }} [options]
264
+ * duration in ms; 0 keeps it until closed. Default 4000.
265
+ * @returns {() => void} call to dismiss early
266
+ */
267
+ export function ogToast(title, options = {}) {
268
+ const { description, variant, duration = 4000 } = options;
269
+ const toast = document.createElement('li');
270
+ toast.className = `og-toast${variant ? ` og-toast--${variant}` : ''}`;
271
+ if (variant === 'danger') toast.setAttribute('role', 'alert');
272
+
273
+ const titleEl = document.createElement('div');
274
+ titleEl.className = 'og-toast__title';
275
+ titleEl.textContent = title;
276
+ toast.append(titleEl);
277
+
278
+ if (description) {
279
+ const desc = document.createElement('div');
280
+ desc.className = 'og-toast__description';
281
+ desc.textContent = description;
282
+ toast.append(desc);
283
+ }
284
+
285
+ const close = document.createElement('button');
286
+ close.className = 'og-toast__close';
287
+ close.type = 'button';
288
+ close.setAttribute('aria-label', 'Dismiss');
289
+ toast.append(close);
290
+
291
+ let timer;
292
+ const dismiss = () => {
293
+ clearTimeout(timer);
294
+ toast.setAttribute('data-leaving', '');
295
+ const ms = parseFloat(getComputedStyle(toast).transitionDuration) * 1000 || 0;
296
+ setTimeout(() => toast.remove(), ms + 20);
297
+ };
298
+ close.addEventListener('click', dismiss);
299
+
300
+ // Pause while hovered or focused, so people can read it.
301
+ const start = () => {
302
+ if (duration > 0) timer = setTimeout(dismiss, duration);
303
+ };
304
+ toast.addEventListener('mouseenter', () => clearTimeout(timer));
305
+ toast.addEventListener('mouseleave', start);
306
+ toast.addEventListener('focusin', () => clearTimeout(timer));
307
+ toast.addEventListener('focusout', start);
308
+
309
+ toastRegion().append(toast);
310
+ start();
311
+ return dismiss;
312
+ }
package/src/og-ui.scss ADDED
@@ -0,0 +1,28 @@
1
+ // Everything in one file. For single components, use dist/components/<name>.css.
2
+ @use 'base';
3
+ @use 'components/surface';
4
+ // Actions & display
5
+ @use 'components/button';
6
+ @use 'components/badge';
7
+ @use 'components/avatar';
8
+ @use 'components/card';
9
+ // Forms
10
+ @use 'components/input';
11
+ @use 'components/select';
12
+ @use 'components/checkbox';
13
+ @use 'components/switch';
14
+ @use 'components/slider';
15
+ // Overlays
16
+ @use 'components/dialog';
17
+ @use 'components/popover';
18
+ @use 'components/tooltip';
19
+ // Feedback
20
+ @use 'components/toast';
21
+ @use 'components/alert';
22
+ @use 'components/progress';
23
+ @use 'components/skeleton';
24
+ // Navigation & data
25
+ @use 'components/tabs';
26
+ @use 'components/accordion';
27
+ @use 'components/table';
28
+ @use 'components/pagination';
package/README.md DELETED
@@ -1,5 +0,0 @@
1
- # @og-ui/core
2
-
3
- Core styles and theme contract for **OG UI**, a themeable UI component library.
4
-
5
- Work in progress. Follow along at https://ui.ogom.in