@joniguerini/lume 0.0.0-stage → 1.9.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.
- package/LICENSE +25 -0
- package/README.md +62 -2
- package/base.css +165 -0
- package/bin/lumeui.mjs +163 -0
- package/components/alert.css +156 -0
- package/components/banner.css +98 -0
- package/components/button.css +291 -0
- package/components/calendar.css +532 -0
- package/components/carousel.css +175 -0
- package/components/chart.css +813 -0
- package/components/chat.css +670 -0
- package/components/checkbox.css +212 -0
- package/components/chip.css +224 -0
- package/components/code.css +36 -0
- package/components/color-picker.css +192 -0
- package/components/command.css +148 -0
- package/components/content.css +211 -0
- package/components/editor.css +174 -0
- package/components/feedback.css +320 -0
- package/components/filters.css +103 -0
- package/components/form.css +455 -0
- package/components/image-viewer.css +237 -0
- package/components/layers.css +138 -0
- package/components/link.css +87 -0
- package/components/loading.css +260 -0
- package/components/menu.css +180 -0
- package/components/modal.css +209 -0
- package/components/navigation.css +507 -0
- package/components/notifications.css +186 -0
- package/components/number-field.css +52 -0
- package/components/otp.css +119 -0
- package/components/panel.css +208 -0
- package/components/picker.css +207 -0
- package/components/rating.css +143 -0
- package/components/resizable.css +173 -0
- package/components/scrollbar.css +110 -0
- package/components/select.css +477 -0
- package/components/shell.css +837 -0
- package/components/slider.css +189 -0
- package/components/sortable.css +287 -0
- package/components/surfaces.css +182 -0
- package/components/switch.css +115 -0
- package/components/table.css +228 -0
- package/components/tabs.css +190 -0
- package/components/tags.css +83 -0
- package/components/toast.css +164 -0
- package/components/toolbar.css +80 -0
- package/components/upload.css +181 -0
- package/ds.css +48 -0
- package/fonts/Geist-LICENSE.txt +92 -0
- package/fonts/Geist-Variable.woff2 +0 -0
- package/fonts/GeistSerif-Regular.woff2 +0 -0
- package/fonts/JetBrainsMono-LICENSE.txt +93 -0
- package/fonts/JetBrainsMono-Variable.woff2 +0 -0
- package/fonts.css +29 -0
- package/icons/icons.d.ts +5 -0
- package/icons/icons.js +266 -0
- package/js/back-to-top.d.ts +2 -0
- package/js/back-to-top.js +62 -0
- package/js/banner.js +87 -0
- package/js/calendar.d.ts +3 -0
- package/js/calendar.js +472 -0
- package/js/carousel.d.ts +2 -0
- package/js/carousel.js +258 -0
- package/js/charts/bar.js +361 -0
- package/js/charts/core.js +604 -0
- package/js/charts/donut.js +206 -0
- package/js/charts/figures.js +97 -0
- package/js/charts/heatmap.js +410 -0
- package/js/charts/line.js +268 -0
- package/js/charts/scatter.js +211 -0
- package/js/charts.d.ts +12 -0
- package/js/charts.js +27 -0
- package/js/code-block.d.ts +3 -0
- package/js/code-block.js +32 -0
- package/js/color-picker.d.ts +4 -0
- package/js/color-picker.js +448 -0
- package/js/command-palette.js +233 -0
- package/js/composer.js +573 -0
- package/js/context-menu.d.ts +2 -0
- package/js/context-menu.js +154 -0
- package/js/date-picker.d.ts +2 -0
- package/js/date-picker.js +430 -0
- package/js/drawer.js +48 -0
- package/js/editor.d.ts +5 -0
- package/js/editor.js +615 -0
- package/js/field-support.js +77 -0
- package/js/file-upload.d.ts +2 -0
- package/js/file-upload.js +230 -0
- package/js/filters.js +266 -0
- package/js/form.d.ts +2 -0
- package/js/form.js +409 -0
- package/js/i18n.d.ts +14 -0
- package/js/i18n.js +83 -0
- package/js/image-viewer.d.ts +4 -0
- package/js/image-viewer.js +275 -0
- package/js/menu.d.ts +4 -0
- package/js/menu.js +237 -0
- package/js/messages.js +908 -0
- package/js/modal.d.ts +7 -0
- package/js/modal.js +183 -0
- package/js/notifications.d.ts +4 -0
- package/js/notifications.js +204 -0
- package/js/number-field.d.ts +2 -0
- package/js/number-field.js +175 -0
- package/js/otp-field.d.ts +2 -0
- package/js/otp-field.js +107 -0
- package/js/pagination.d.ts +3 -0
- package/js/pagination.js +143 -0
- package/js/popover.d.ts +2 -0
- package/js/popover.js +155 -0
- package/js/progress-ring.js +48 -0
- package/js/rating.d.ts +3 -0
- package/js/rating.js +130 -0
- package/js/resizable.d.ts +2 -0
- package/js/resizable.js +311 -0
- package/js/scrollbar.d.ts +2 -0
- package/js/scrollbar.js +273 -0
- package/js/segmented.js +60 -0
- package/js/select.d.ts +3 -0
- package/js/select.js +784 -0
- package/js/shell.d.ts +6 -0
- package/js/shell.js +373 -0
- package/js/slider.d.ts +2 -0
- package/js/slider.js +89 -0
- package/js/sortable.d.ts +5 -0
- package/js/sortable.js +463 -0
- package/js/stepper.d.ts +2 -0
- package/js/stepper.js +72 -0
- package/js/tabs.js +99 -0
- package/js/tag-field.d.ts +2 -0
- package/js/tag-field.js +177 -0
- package/js/theme.d.ts +9 -0
- package/js/theme.js +114 -0
- package/js/time-picker.d.ts +2 -0
- package/js/time-picker.js +279 -0
- package/js/toast.d.ts +17 -0
- package/js/toast.js +171 -0
- package/js/toolbar.d.ts +2 -0
- package/js/toolbar.js +107 -0
- package/js/tooltip.d.ts +2 -0
- package/js/tooltip.js +208 -0
- package/js/tree.d.ts +2 -0
- package/js/tree.js +168 -0
- package/lume.css +11574 -0
- package/lume.d.ts +104 -0
- package/lume.js +67 -0
- package/package.json +66 -4
- package/react/Alert.d.ts +24 -0
- package/react/Alert.js +21 -0
- package/react/AppShell.d.ts +67 -0
- package/react/AppShell.js +94 -0
- package/react/Avatar.d.ts +39 -0
- package/react/Avatar.js +40 -0
- package/react/Badge.d.ts +17 -0
- package/react/Badge.js +10 -0
- package/react/Button.d.ts +32 -0
- package/react/Button.js +17 -0
- package/react/Calendar.d.ts +48 -0
- package/react/Calendar.js +67 -0
- package/react/Carousel.d.ts +30 -0
- package/react/Carousel.js +38 -0
- package/react/Charts.d.ts +186 -0
- package/react/Charts.js +58 -0
- package/react/Chip.d.ts +34 -0
- package/react/Chip.js +26 -0
- package/react/Choice.d.ts +72 -0
- package/react/Choice.js +51 -0
- package/react/ColorPicker.d.ts +43 -0
- package/react/ColorPicker.js +73 -0
- package/react/Content.d.ts +58 -0
- package/react/Content.js +42 -0
- package/react/Controls.d.ts +97 -0
- package/react/Controls.js +118 -0
- package/react/Editor.d.ts +28 -0
- package/react/Editor.js +57 -0
- package/react/Field.d.ts +90 -0
- package/react/Field.js +65 -0
- package/react/Form.d.ts +36 -0
- package/react/Form.js +45 -0
- package/react/Icon.d.ts +13 -0
- package/react/Icon.js +8 -0
- package/react/IconButton.d.ts +19 -0
- package/react/IconButton.js +12 -0
- package/react/ImageViewer.d.ts +20 -0
- package/react/ImageViewer.js +22 -0
- package/react/InlineAction.d.ts +15 -0
- package/react/InlineAction.js +12 -0
- package/react/Layers.d.ts +118 -0
- package/react/Layers.js +115 -0
- package/react/Link.d.ts +32 -0
- package/react/Link.js +26 -0
- package/react/Loading.d.ts +67 -0
- package/react/Loading.js +49 -0
- package/react/Navigation.d.ts +181 -0
- package/react/Navigation.js +197 -0
- package/react/Notices.d.ts +43 -0
- package/react/Notices.js +89 -0
- package/react/Panel.d.ts +57 -0
- package/react/Panel.js +33 -0
- package/react/Pickers.d.ts +83 -0
- package/react/Pickers.js +70 -0
- package/react/Rating.d.ts +39 -0
- package/react/Rating.js +41 -0
- package/react/Resizable.d.ts +44 -0
- package/react/Resizable.js +30 -0
- package/react/Select.d.ts +53 -0
- package/react/Select.js +79 -0
- package/react/Sortable.d.ts +52 -0
- package/react/Sortable.js +113 -0
- package/react/Table.d.ts +60 -0
- package/react/Table.js +34 -0
- package/react/Toolbar.d.ts +45 -0
- package/react/Toolbar.js +45 -0
- package/react/cx.d.ts +2 -0
- package/react/cx.js +5 -0
- package/react/icon-names.d.ts +1 -0
- package/react/icon-names.js +2 -0
- package/react/index.d.ts +42 -0
- package/react/index.js +52 -0
- package/react/useController.d.ts +8 -0
- package/react/useController.js +16 -0
- package/react/useLocale.d.ts +5 -0
- package/react/useLocale.js +15 -0
- package/tokens.css +739 -0
- package/tokens.js +4911 -0
package/js/modal.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Gerado por scripts/build-react.mjs. Não edite.
|
|
2
|
+
export declare function enhanceModal(dialog: HTMLDialogElement, options?: { dismissible?: boolean; onClose?: (value?: string) => void }): { el: HTMLDialogElement; open(): Promise<string | undefined>; close(value?: string): void; destroy(): void; readonly isOpen: boolean };
|
|
3
|
+
export declare function createModal(options?: Record<string, unknown>): { el: HTMLDialogElement; open(): Promise<string | undefined>; close(value?: string): void; destroy(): void; readonly isOpen: boolean };
|
|
4
|
+
export declare function openModal(options?: Record<string, unknown>): Promise<string | undefined>;
|
|
5
|
+
export declare function confirmModal(options?: Record<string, unknown>): Promise<boolean>;
|
|
6
|
+
export declare function modalHtml(options?: Record<string, unknown>): string;
|
|
7
|
+
export declare function modalClasses(options?: { size?: string; tone?: string }): string;
|
package/js/modal.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modal — diálogo modal sobre <dialog> (showModal): foco preso pelo navegador,
|
|
3
|
+
* Esc e clique fora fecham, o foco volta ao gatilho e a saída é animada.
|
|
4
|
+
*
|
|
5
|
+
* const escolha = await openModal({
|
|
6
|
+
* eyebrow: 'Projeto', title: 'Publicar alterações?', body: 'Texto ou HTML',
|
|
7
|
+
* size: 'sm' | 'md' | 'lg', tone: 'danger', icon: 'alert-triangle',
|
|
8
|
+
* dismissible: true,
|
|
9
|
+
* actions: [{ label: 'Cancelar', value: 'cancel' }, { label: 'Publicar', value: 'confirm', variant: 'primary' }],
|
|
10
|
+
* }); // → valor da ação, ou undefined se fechou sem escolher
|
|
11
|
+
*
|
|
12
|
+
* const ok = await confirmModal({ title: 'Excluir projeto?', body: '…', confirmLabel: 'Excluir', tone: 'danger' }); // → boolean
|
|
13
|
+
*
|
|
14
|
+
* const modal = createModal({ … }); // controle manual: modal.open(), modal.close(valor), modal.destroy()
|
|
15
|
+
*/
|
|
16
|
+
import { icon } from '../icons/icons.js';
|
|
17
|
+
import { msg } from './i18n.js';
|
|
18
|
+
|
|
19
|
+
let uid = 0;
|
|
20
|
+
const escape = (s) => String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"');
|
|
21
|
+
const reducedMotion = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
22
|
+
|
|
23
|
+
/** Duração da saída, lida do token --ds-duration-fast. */
|
|
24
|
+
function exitDuration(el) {
|
|
25
|
+
const value = getComputedStyle(el).getPropertyValue('--ds-duration-fast').trim();
|
|
26
|
+
return (parseFloat(value) || 120) + 20;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** HTML interno do modal — usado pela API e pelas prévias estáticas da documentação. */
|
|
30
|
+
export function modalHtml({ id = `ds-modal-${++uid}`, eyebrow, title, body, tone, icon: glyph, actions = [], dismissible = true, closeLabel = msg('common.close') } = {}) {
|
|
31
|
+
const lead = glyph ?? (tone === 'danger' ? 'alert-triangle' : null);
|
|
32
|
+
return `
|
|
33
|
+
<header class="ds-modal__header">
|
|
34
|
+
${lead ? `<span class="ds-modal__icon">${icon(lead, { size: 20 })}</span>` : ''}
|
|
35
|
+
<div class="ds-modal__heading">
|
|
36
|
+
${eyebrow ? `<p class="ds-modal__eyebrow">${escape(eyebrow)}</p>` : ''}
|
|
37
|
+
<h2 class="ds-modal__title" id="${id}-title">${escape(title)}</h2>
|
|
38
|
+
</div>
|
|
39
|
+
${dismissible ? `<button type="button" class="ds-icon-btn ds-icon-btn--muted ds-modal__close" data-ds-modal-close aria-label="${escape(closeLabel)}">${icon('x', { size: 20 })}</button>` : ''}
|
|
40
|
+
</header>
|
|
41
|
+
${body !== undefined && body !== null ? `<div class="ds-modal__body" id="${id}-body">${typeof body === 'string' ? body : ''}</div>` : ''}
|
|
42
|
+
${
|
|
43
|
+
actions.length
|
|
44
|
+
? `<footer class="ds-modal__footer">${actions
|
|
45
|
+
.map(
|
|
46
|
+
(a) => `<button type="button" class="ds-btn ds-btn--${a.variant ?? 'secondary'}" data-ds-modal-close="${escape(a.value ?? '')}"${a.autofocus ? ' data-ds-autofocus' : ''}>${a.icon ? icon(a.icon, { size: 18 }) : ''}${escape(a.label)}</button>`,
|
|
47
|
+
)
|
|
48
|
+
.join('')}</footer>`
|
|
49
|
+
: ''
|
|
50
|
+
}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const modalClasses = ({ size = 'md', tone } = {}) => ['ds-modal', size !== 'md' && `ds-modal--${size}`, tone && `ds-modal--${tone}`].filter(Boolean).join(' ');
|
|
54
|
+
|
|
55
|
+
/** Liga o comportamento a um <dialog class="ds-modal"> existente. */
|
|
56
|
+
export function enhanceModal(dialog, { dismissible = true, onClose } = {}) {
|
|
57
|
+
let returnFocus = null;
|
|
58
|
+
let closing = false;
|
|
59
|
+
let resolve = null;
|
|
60
|
+
let timer;
|
|
61
|
+
|
|
62
|
+
const onCancel = (event) => {
|
|
63
|
+
event.preventDefault();
|
|
64
|
+
if (dismissible) close();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const onClick = (event) => {
|
|
68
|
+
if (event.target === dialog && dismissible) {
|
|
69
|
+
const r = dialog.getBoundingClientRect();
|
|
70
|
+
const inside = event.clientX >= r.left && event.clientX <= r.right && event.clientY >= r.top && event.clientY <= r.bottom;
|
|
71
|
+
if (!inside) close();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const btn = event.target.closest('[data-ds-modal-close]');
|
|
75
|
+
if (btn && dialog.contains(btn)) close(btn.dataset.dsModalClose || undefined);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// fechado por fora (dialog.close() direto, navegação…): resolve como "sem escolha"
|
|
79
|
+
const onNativeClose = () => {
|
|
80
|
+
if (!resolve) return;
|
|
81
|
+
const done = resolve;
|
|
82
|
+
resolve = null;
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
closing = false;
|
|
85
|
+
dialog.removeAttribute('data-closing');
|
|
86
|
+
onClose?.(undefined);
|
|
87
|
+
done(undefined);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
dialog.addEventListener('cancel', onCancel);
|
|
91
|
+
dialog.addEventListener('click', onClick);
|
|
92
|
+
dialog.addEventListener('close', onNativeClose);
|
|
93
|
+
|
|
94
|
+
function open() {
|
|
95
|
+
if (dialog.open) return Promise.resolve(undefined);
|
|
96
|
+
returnFocus = document.activeElement;
|
|
97
|
+
closing = false;
|
|
98
|
+
dialog.removeAttribute('data-closing');
|
|
99
|
+
dialog.showModal();
|
|
100
|
+
const target =
|
|
101
|
+
dialog.querySelector('[autofocus], [data-ds-autofocus]') ??
|
|
102
|
+
dialog.querySelector('.ds-modal__body :is(input, select, textarea, button, a[href])') ??
|
|
103
|
+
dialog.querySelector('.ds-modal__footer .ds-btn:last-child') ??
|
|
104
|
+
dialog.querySelector('.ds-modal__close');
|
|
105
|
+
target?.focus();
|
|
106
|
+
return new Promise((r) => (resolve = r));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function close(value) {
|
|
110
|
+
if (!dialog.open || closing) return;
|
|
111
|
+
closing = true;
|
|
112
|
+
const finish = () => {
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
const done = resolve;
|
|
115
|
+
resolve = null;
|
|
116
|
+
dialog.removeAttribute('data-closing');
|
|
117
|
+
dialog.close();
|
|
118
|
+
closing = false;
|
|
119
|
+
returnFocus?.focus?.({ preventScroll: true });
|
|
120
|
+
onClose?.(value);
|
|
121
|
+
done?.(value);
|
|
122
|
+
};
|
|
123
|
+
if (reducedMotion()) return finish();
|
|
124
|
+
dialog.setAttribute('data-closing', '');
|
|
125
|
+
timer = setTimeout(finish, exitDuration(dialog));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function destroy() {
|
|
129
|
+
clearTimeout(timer);
|
|
130
|
+
if (dialog.open) dialog.close();
|
|
131
|
+
dialog.removeEventListener('cancel', onCancel);
|
|
132
|
+
dialog.removeEventListener('click', onClick);
|
|
133
|
+
dialog.removeEventListener('close', onNativeClose);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return { el: dialog, open, close, destroy, get isOpen() { return dialog.open; } };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Cria um modal novo no fim do <body>. */
|
|
140
|
+
export function createModal(options = {}) {
|
|
141
|
+
const id = `ds-modal-${++uid}`;
|
|
142
|
+
const dialog = document.createElement('dialog');
|
|
143
|
+
dialog.className = modalClasses(options);
|
|
144
|
+
dialog.setAttribute('aria-labelledby', `${id}-title`);
|
|
145
|
+
const hasBody = options.body !== undefined && options.body !== null;
|
|
146
|
+
if (hasBody) dialog.setAttribute('aria-describedby', `${id}-body`);
|
|
147
|
+
dialog.innerHTML = modalHtml({ ...options, id });
|
|
148
|
+
if (hasBody && typeof options.body !== 'string') dialog.querySelector('.ds-modal__body').append(options.body);
|
|
149
|
+
document.body.append(dialog);
|
|
150
|
+
const api = enhanceModal(dialog, { dismissible: options.dismissible ?? true, onClose: options.onClose });
|
|
151
|
+
return {
|
|
152
|
+
...api,
|
|
153
|
+
destroy() {
|
|
154
|
+
api.destroy();
|
|
155
|
+
dialog.remove();
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Abre um modal e resolve com o valor da ação escolhida (undefined se fechou sem escolher). */
|
|
161
|
+
export function openModal(options = {}) {
|
|
162
|
+
const modal = createModal(options);
|
|
163
|
+
const result = modal.open();
|
|
164
|
+
result.then(() => modal.destroy());
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Confirmação: resolve true se a pessoa confirmou. Em tom de perigo, o foco começa em "Cancelar". */
|
|
169
|
+
export function confirmModal({ title, body, eyebrow, confirmLabel = msg('modal.confirm'), cancelLabel = msg('modal.cancel'), tone, icon: glyph } = {}) {
|
|
170
|
+
const danger = tone === 'danger';
|
|
171
|
+
return openModal({
|
|
172
|
+
title,
|
|
173
|
+
body,
|
|
174
|
+
eyebrow,
|
|
175
|
+
tone,
|
|
176
|
+
icon: glyph,
|
|
177
|
+
size: 'sm',
|
|
178
|
+
actions: [
|
|
179
|
+
{ label: cancelLabel, value: 'cancel', autofocus: danger },
|
|
180
|
+
{ label: confirmLabel, value: 'confirm', variant: danger ? 'danger' : 'primary', autofocus: !danger },
|
|
181
|
+
],
|
|
182
|
+
}).then((value) => value === 'confirm');
|
|
183
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// Gerado por scripts/build-react.mjs. Não edite.
|
|
2
|
+
export interface NotificationItem { id: string; title: string; text?: string; time?: Date | string | number; icon?: string; tone?: 'neutral' | 'brand' | 'info' | 'success' | 'warning' | 'danger'; href?: string; read?: boolean }
|
|
3
|
+
export declare function relativeTime(date: Date | string | number, now?: Date): string;
|
|
4
|
+
export declare function createNotificationCenter(trigger: HTMLElement, options?: { items?: NotificationItem[]; onSelect?: (item: NotificationItem) => void; onChange?: (items: NotificationItem[], unread: number) => void; badge?: boolean; placement?: 'bottom' | 'top' | 'left' | 'right'; align?: 'start' | 'center' | 'end' }): { readonly items: NotificationItem[]; readonly unread: number; readonly isOpen: boolean; open(): void; close(): void; setItems(items: NotificationItem[], options?: { announce?: boolean }): void; add(item: NotificationItem): void; markRead(id: string, read?: boolean): void; markUnread(id: string): void; markAllRead(): void; destroy(): void };
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Central de notificações — o sino abre a lista, com o número das não lidas.
|
|
3
|
+
*
|
|
4
|
+
* <button type="button" class="ds-icon-btn ds-notify-trigger"><ds-icon name="bell"></ds-icon></button>
|
|
5
|
+
*
|
|
6
|
+
* const center = createNotificationCenter(document.querySelector('.ds-notify-trigger'), {
|
|
7
|
+
* items: [
|
|
8
|
+
* { id: 'v035', title: 'Lume 0.35', text: 'Banner e central de notificações.', time: new Date(),
|
|
9
|
+
* icon: 'megaphone', tone: 'brand', href: '#/novidades', read: false },
|
|
10
|
+
* ],
|
|
11
|
+
* onSelect: (item) => {}, // clicou num item (ele já fica lido)
|
|
12
|
+
* onChange: (items, unread) => {},// mudou o que está lido
|
|
13
|
+
* badge: true, // desenha o número no sino (o React desenha o seu)
|
|
14
|
+
* });
|
|
15
|
+
* center.add({ … }); // chega uma nova: vai para o topo e é anunciada ao leitor de tela
|
|
16
|
+
* center.setItems([…], { announce: true }); center.markRead('v035'); center.markAllRead();
|
|
17
|
+
* center.unread; center.items; center.open(); center.close(); center.destroy();
|
|
18
|
+
*
|
|
19
|
+
* Por cima do Popover: o sino tem aria-haspopup="dialog" e aria-expanded, Esc fecha e devolve o foco.
|
|
20
|
+
* O nome do sino traz quantas faltam ler (“Notificações, 2 não lidas”); cada item não lido diz “Não lida”.
|
|
21
|
+
* O tempo é relativo e no idioma do Lume (“há 5 minutos”); depois de uma semana, vira data.
|
|
22
|
+
*/
|
|
23
|
+
import { icon } from '../icons/icons.js';
|
|
24
|
+
import { msg, locale } from './i18n.js';
|
|
25
|
+
import { createPopover } from './popover.js';
|
|
26
|
+
|
|
27
|
+
const escape = (s) => String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
28
|
+
|
|
29
|
+
/** “há 5 minutos”, “ontem”, “3 de nov.” — no idioma do Lume. */
|
|
30
|
+
export function relativeTime(date, now = new Date()) {
|
|
31
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
32
|
+
const s = Math.round((d - now) / 1000);
|
|
33
|
+
const abs = Math.abs(s);
|
|
34
|
+
if (abs < 45) return msg('notifications.now');
|
|
35
|
+
const rtf = new Intl.RelativeTimeFormat(locale(), { numeric: 'auto' });
|
|
36
|
+
if (abs < 3600) return rtf.format(Math.round(s / 60), 'minute');
|
|
37
|
+
if (abs < 86400) return rtf.format(Math.round(s / 3600), 'hour');
|
|
38
|
+
if (abs < 7 * 86400) return rtf.format(Math.round(s / 86400), 'day');
|
|
39
|
+
return new Intl.DateTimeFormat(locale(), { day: 'numeric', month: 'short', ...(d.getFullYear() !== now.getFullYear() ? { year: 'numeric' } : {}) }).format(d);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createNotificationCenter(trigger, options = {}) {
|
|
43
|
+
const o = { items: [], onSelect: null, onChange: null, badge: true, placement: 'bottom', align: 'end', ...options };
|
|
44
|
+
let items = o.items.map((item) => ({ ...item }));
|
|
45
|
+
|
|
46
|
+
const ownLabel = trigger.getAttribute('aria-label');
|
|
47
|
+
trigger.classList.add('ds-notify-trigger');
|
|
48
|
+
let count = null;
|
|
49
|
+
if (o.badge) {
|
|
50
|
+
count = document.createElement('span');
|
|
51
|
+
count.className = 'ds-notify-trigger__count';
|
|
52
|
+
count.setAttribute('aria-hidden', 'true');
|
|
53
|
+
trigger.append(count);
|
|
54
|
+
}
|
|
55
|
+
// anuncia o que chega com add(), sem roubar o foco
|
|
56
|
+
const live = document.createElement('span');
|
|
57
|
+
live.className = 'ds-visually-hidden';
|
|
58
|
+
live.setAttribute('role', 'status');
|
|
59
|
+
trigger.after(live);
|
|
60
|
+
|
|
61
|
+
const content = document.createElement('div');
|
|
62
|
+
content.className = 'ds-notify';
|
|
63
|
+
const popover = createPopover(trigger, {
|
|
64
|
+
content,
|
|
65
|
+
className: 'ds-notify-panel',
|
|
66
|
+
label: msg('notifications.title'),
|
|
67
|
+
placement: o.placement,
|
|
68
|
+
align: o.align,
|
|
69
|
+
// o tempo relativo anda: redesenha a cada abertura
|
|
70
|
+
onOpen: () => {
|
|
71
|
+
render();
|
|
72
|
+
popover.reposition();
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const unread = () => items.filter((item) => !item.read).length;
|
|
77
|
+
const say = (title) => {
|
|
78
|
+
live.textContent = '';
|
|
79
|
+
requestAnimationFrame(() => (live.textContent = msg('notifications.new', title)));
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
function itemHtml(item) {
|
|
83
|
+
const tag = item.href ? 'a' : 'button';
|
|
84
|
+
const attrs = item.href ? `href="${escape(item.href)}"` : 'type="button"';
|
|
85
|
+
const time = item.time ? `<time class="ds-notify__time" datetime="${new Date(item.time).toISOString()}">${escape(relativeTime(item.time))}</time>` : '';
|
|
86
|
+
return `<li class="ds-notify__item"${item.read ? '' : ' data-unread'} data-id="${escape(item.id)}">
|
|
87
|
+
<${tag} class="ds-notify__link" ${attrs}>
|
|
88
|
+
${item.icon ? `<span class="ds-notify__icon${item.tone && item.tone !== 'neutral' ? ` ds-notify__icon--${item.tone}` : ''}">${icon(item.icon, { size: 16 })}</span>` : ''}
|
|
89
|
+
<span class="ds-notify__body">
|
|
90
|
+
<span class="ds-notify__item-title">${escape(item.title)}</span>
|
|
91
|
+
${item.text ? `<span class="ds-notify__text">${escape(item.text)}</span>` : ''}
|
|
92
|
+
${time}
|
|
93
|
+
</span>
|
|
94
|
+
${item.read ? '' : `<span class="ds-dot ds-notify__dot"></span><span class="ds-visually-hidden">${escape(msg('notifications.unreadItem'))}</span>`}
|
|
95
|
+
</${tag}>
|
|
96
|
+
</li>`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function render() {
|
|
100
|
+
const n = unread();
|
|
101
|
+
const active = document.activeElement;
|
|
102
|
+
const had = content.contains(active);
|
|
103
|
+
const focusedId = had ? active.closest('.ds-notify__item')?.dataset.id : null;
|
|
104
|
+
const focusedAll = had && active.matches('[data-notify-all]');
|
|
105
|
+
content.innerHTML = body(n);
|
|
106
|
+
if (had) {
|
|
107
|
+
const back = focusedId ? content.querySelector(`.ds-notify__item[data-id="${CSS.escape(focusedId)}"] .ds-notify__link`) : focusedAll ? content.querySelector('[data-notify-all]') : null;
|
|
108
|
+
(back ?? content.querySelector('.ds-notify__link') ?? popover.el).focus({ preventScroll: true });
|
|
109
|
+
}
|
|
110
|
+
trigger.setAttribute('aria-label', msg('notifications.trigger', n));
|
|
111
|
+
if (count) {
|
|
112
|
+
count.textContent = n > 99 ? '99+' : String(n);
|
|
113
|
+
count.hidden = n === 0;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const body = (n) => `<div class="ds-notify__header">
|
|
118
|
+
<h2 class="ds-notify__title">${escape(msg('notifications.title'))}</h2>
|
|
119
|
+
${n ? `<button type="button" class="ds-inline-action" data-notify-all>${escape(msg('notifications.markAll'))}</button>` : ''}
|
|
120
|
+
</div>
|
|
121
|
+
${items.length ? `<ul class="ds-notify__list" role="list">${items.map(itemHtml).join('')}</ul>` : `<div class="ds-notify__empty">${icon('bell', { size: 24 })}<span>${escape(msg('notifications.empty'))}</span></div>`}`;
|
|
122
|
+
|
|
123
|
+
const changed = () => {
|
|
124
|
+
render();
|
|
125
|
+
o.onChange?.(items.map((item) => ({ ...item })), unread());
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
function markRead(id, read = true) {
|
|
129
|
+
const item = items.find((i) => i.id === id);
|
|
130
|
+
if (!item || !!item.read === read) return;
|
|
131
|
+
item.read = read;
|
|
132
|
+
changed();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function markAllRead() {
|
|
136
|
+
if (!unread()) return;
|
|
137
|
+
items.forEach((item) => (item.read = true));
|
|
138
|
+
changed();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const onClick = (event) => {
|
|
142
|
+
if (event.target.closest('[data-notify-all]')) {
|
|
143
|
+
markAllRead();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const link = event.target.closest('.ds-notify__link');
|
|
147
|
+
if (!link) return;
|
|
148
|
+
const item = items.find((i) => i.id === link.closest('.ds-notify__item').dataset.id);
|
|
149
|
+
if (!item) return;
|
|
150
|
+
if (!item.read) {
|
|
151
|
+
item.read = true;
|
|
152
|
+
changed();
|
|
153
|
+
}
|
|
154
|
+
o.onSelect?.({ ...item });
|
|
155
|
+
if (item.href) popover.close(false);
|
|
156
|
+
};
|
|
157
|
+
const onLocale = () => {
|
|
158
|
+
popover.el.setAttribute('aria-label', msg('notifications.title'));
|
|
159
|
+
render();
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
content.addEventListener('click', onClick);
|
|
163
|
+
document.documentElement.addEventListener('ds:localechange', onLocale);
|
|
164
|
+
render();
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
get items() {
|
|
168
|
+
return items.map((item) => ({ ...item }));
|
|
169
|
+
},
|
|
170
|
+
get unread() {
|
|
171
|
+
return unread();
|
|
172
|
+
},
|
|
173
|
+
get isOpen() {
|
|
174
|
+
return popover.isOpen;
|
|
175
|
+
},
|
|
176
|
+
open: () => popover.open(),
|
|
177
|
+
close: () => popover.close(),
|
|
178
|
+
/** Troca a lista. Com { announce: true }, as que não estavam antes são anunciadas como novas. */
|
|
179
|
+
setItems(next, { announce = false } = {}) {
|
|
180
|
+
const before = new Set(items.map((i) => i.id));
|
|
181
|
+
items = next.map((item) => ({ ...item }));
|
|
182
|
+
if (announce) items.filter((i) => !before.has(i.id) && !i.read).forEach((i) => say(i.title));
|
|
183
|
+
render();
|
|
184
|
+
},
|
|
185
|
+
add(item) {
|
|
186
|
+
items = [{ ...item }, ...items.filter((i) => i.id !== item.id)];
|
|
187
|
+
say(item.title);
|
|
188
|
+
changed();
|
|
189
|
+
},
|
|
190
|
+
markRead,
|
|
191
|
+
markUnread: (id) => markRead(id, false),
|
|
192
|
+
markAllRead,
|
|
193
|
+
destroy() {
|
|
194
|
+
popover.destroy();
|
|
195
|
+
content.removeEventListener('click', onClick);
|
|
196
|
+
document.documentElement.removeEventListener('ds:localechange', onLocale);
|
|
197
|
+
count?.remove();
|
|
198
|
+
live.remove();
|
|
199
|
+
if (ownLabel === null) trigger.removeAttribute('aria-label');
|
|
200
|
+
else trigger.setAttribute('aria-label', ownLabel);
|
|
201
|
+
trigger.classList.remove('ds-notify-trigger');
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Campo numérico — digitar ou usar − e +, com limites, passo e o formato do idioma.
|
|
3
|
+
*
|
|
4
|
+
* <div class="ds-field ds-number"><input class="ds-field__input" id="qtd" name="qtd" type="text"></div>
|
|
5
|
+
*
|
|
6
|
+
* const field = createNumberField(document.querySelector('#qtd'), {
|
|
7
|
+
* value: 1, min: 0, max: 99, step: 1,
|
|
8
|
+
* format: { style: 'currency', currency: 'BRL' }, // opções de Intl.NumberFormat (padrão: número simples)
|
|
9
|
+
* onChange: (value) => …,
|
|
10
|
+
* });
|
|
11
|
+
* field.value; field.setValue(10); field.destroy();
|
|
12
|
+
*
|
|
13
|
+
* O formulário recebe o número puro (1234.5), num input oculto com o name do campo. Ao sair do campo, o valor
|
|
14
|
+
* volta para dentro dos limites. Padrão ARIA: spinbutton. Teclado: ↑/↓ um passo, PageUp/PageDown dez passos,
|
|
15
|
+
* Home/End vão aos limites. Os botões ficam fora da ordem do Tab, porque as setas já fazem o mesmo.
|
|
16
|
+
*/
|
|
17
|
+
import { icon } from '../icons/icons.js';
|
|
18
|
+
import { msg, numberFormat } from './i18n.js';
|
|
19
|
+
import { hiddenValue } from './field-support.js';
|
|
20
|
+
|
|
21
|
+
const REPEAT_DELAY = 400;
|
|
22
|
+
const REPEAT_EVERY = 80;
|
|
23
|
+
|
|
24
|
+
export function createNumberField(input, options = {}) {
|
|
25
|
+
const o = { step: 1, ...options };
|
|
26
|
+
const field = input.closest('.ds-field');
|
|
27
|
+
const hidden = input.name ? hiddenValue(input) : null;
|
|
28
|
+
input.setAttribute('role', 'spinbutton');
|
|
29
|
+
input.inputMode = Number.isInteger(o.step) && !o.format ? 'numeric' : 'decimal';
|
|
30
|
+
input.autocomplete = 'off';
|
|
31
|
+
|
|
32
|
+
let value = o.value ?? null;
|
|
33
|
+
const decimals = () => Math.max(String(o.step).split('.')[1]?.length ?? 0, o.format?.maximumFractionDigits ?? 0);
|
|
34
|
+
const round = (n) => Number(n.toFixed(Math.max(decimals(), 0)));
|
|
35
|
+
const clamp = (n) => Math.min(o.max ?? Infinity, Math.max(o.min ?? -Infinity, n));
|
|
36
|
+
const format = (n) => numberFormat({ maximumFractionDigits: decimals(), ...o.format }).format(n);
|
|
37
|
+
|
|
38
|
+
const makeButton = (delta, name) => {
|
|
39
|
+
const b = document.createElement('button');
|
|
40
|
+
b.type = 'button';
|
|
41
|
+
b.className = 'ds-number__btn';
|
|
42
|
+
b.tabIndex = -1;
|
|
43
|
+
b.dataset.delta = delta;
|
|
44
|
+
b.innerHTML = icon(name, { size: 16 });
|
|
45
|
+
return b;
|
|
46
|
+
};
|
|
47
|
+
const minus = makeButton(-1, 'minus');
|
|
48
|
+
const plus = makeButton(1, 'plus');
|
|
49
|
+
input.before(minus);
|
|
50
|
+
input.after(plus);
|
|
51
|
+
|
|
52
|
+
function sync() {
|
|
53
|
+
input.value = value === null ? '' : format(value);
|
|
54
|
+
if (hidden) hidden.value = value === null ? '' : String(value);
|
|
55
|
+
if (value === null) input.removeAttribute('aria-valuenow');
|
|
56
|
+
else input.setAttribute('aria-valuenow', value);
|
|
57
|
+
if (value !== null && o.format) input.setAttribute('aria-valuetext', format(value));
|
|
58
|
+
else input.removeAttribute('aria-valuetext');
|
|
59
|
+
if (o.min !== undefined) input.setAttribute('aria-valuemin', o.min);
|
|
60
|
+
if (o.max !== undefined) input.setAttribute('aria-valuemax', o.max);
|
|
61
|
+
minus.disabled = input.disabled || (o.min !== undefined && value !== null && value <= o.min);
|
|
62
|
+
plus.disabled = input.disabled || (o.max !== undefined && value !== null && value >= o.max);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function syncTexts() {
|
|
66
|
+
minus.setAttribute('aria-label', msg('number.decrease'));
|
|
67
|
+
plus.setAttribute('aria-label', msg('number.increase'));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function set(next) {
|
|
71
|
+
const changed = next !== value;
|
|
72
|
+
value = next;
|
|
73
|
+
sync();
|
|
74
|
+
if (changed) {
|
|
75
|
+
o.onChange?.(value);
|
|
76
|
+
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const stepBy = (steps) => set(clamp(round((value ?? o.min ?? 0) + (value === null ? 0 : steps * o.step))));
|
|
81
|
+
|
|
82
|
+
/** “1.234,5”, “1,234.5” ou “R$ 12,50” → número, conforme os separadores do idioma. */
|
|
83
|
+
function parse(text) {
|
|
84
|
+
const parts = numberFormat({ minimumFractionDigits: 1 }).formatToParts(1234.5);
|
|
85
|
+
const group = parts.find((p) => p.type === 'group')?.value ?? '';
|
|
86
|
+
const decimal = parts.find((p) => p.type === 'decimal')?.value ?? '.';
|
|
87
|
+
const clean = text.split(group).join('').replace(decimal, '.').replace(/[^\d.-]/g, '');
|
|
88
|
+
const n = Number(clean);
|
|
89
|
+
return clean && !Number.isNaN(n) ? n : null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const commit = () => {
|
|
93
|
+
const text = input.value.trim();
|
|
94
|
+
const n = text ? parse(text) : null;
|
|
95
|
+
if (text && n === null) return sync(); // não é número: volta ao último valor
|
|
96
|
+
set(n === null ? null : clamp(round(n)));
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const onKey = (event) => {
|
|
100
|
+
const moves = {
|
|
101
|
+
ArrowUp: () => stepBy(1),
|
|
102
|
+
ArrowDown: () => stepBy(-1),
|
|
103
|
+
PageUp: () => stepBy(10),
|
|
104
|
+
PageDown: () => stepBy(-10),
|
|
105
|
+
Home: () => o.min !== undefined && set(o.min),
|
|
106
|
+
End: () => o.max !== undefined && set(o.max),
|
|
107
|
+
};
|
|
108
|
+
if (moves[event.key]) {
|
|
109
|
+
event.preventDefault();
|
|
110
|
+
commitIfTyped();
|
|
111
|
+
moves[event.key]();
|
|
112
|
+
} else if (event.key === 'Enter') commit();
|
|
113
|
+
};
|
|
114
|
+
const commitIfTyped = () => {
|
|
115
|
+
if (input.value !== (value === null ? '' : format(value))) commit();
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// segurar o botão repete o passo
|
|
119
|
+
let timer = null;
|
|
120
|
+
const stop = () => {
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
clearInterval(timer);
|
|
123
|
+
timer = null;
|
|
124
|
+
document.removeEventListener('pointerup', stop);
|
|
125
|
+
document.removeEventListener('pointercancel', stop);
|
|
126
|
+
};
|
|
127
|
+
const onPress = (event) => {
|
|
128
|
+
const b = event.target.closest('[data-delta]');
|
|
129
|
+
if (!b || b.disabled || event.button !== 0) return;
|
|
130
|
+
event.preventDefault();
|
|
131
|
+
input.focus({ preventScroll: true });
|
|
132
|
+
commitIfTyped();
|
|
133
|
+
const delta = Number(b.dataset.delta);
|
|
134
|
+
stepBy(delta);
|
|
135
|
+
timer = setTimeout(() => {
|
|
136
|
+
timer = setInterval(() => {
|
|
137
|
+
stepBy(delta);
|
|
138
|
+
if (b.disabled) stop();
|
|
139
|
+
}, REPEAT_EVERY);
|
|
140
|
+
}, REPEAT_DELAY);
|
|
141
|
+
document.addEventListener('pointerup', stop);
|
|
142
|
+
document.addEventListener('pointercancel', stop);
|
|
143
|
+
};
|
|
144
|
+
const onLocale = () => {
|
|
145
|
+
syncTexts();
|
|
146
|
+
sync();
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
input.addEventListener('keydown', onKey);
|
|
150
|
+
input.addEventListener('blur', commit);
|
|
151
|
+
field.addEventListener('pointerdown', onPress);
|
|
152
|
+
document.documentElement.addEventListener('ds:localechange', onLocale);
|
|
153
|
+
|
|
154
|
+
syncTexts();
|
|
155
|
+
sync();
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
get value() {
|
|
159
|
+
return value;
|
|
160
|
+
},
|
|
161
|
+
setValue(next) {
|
|
162
|
+
value = next === null ? null : clamp(round(next));
|
|
163
|
+
sync();
|
|
164
|
+
},
|
|
165
|
+
destroy() {
|
|
166
|
+
stop();
|
|
167
|
+
minus.remove();
|
|
168
|
+
plus.remove();
|
|
169
|
+
input.removeEventListener('keydown', onKey);
|
|
170
|
+
input.removeEventListener('blur', commit);
|
|
171
|
+
field.removeEventListener('pointerdown', onPress);
|
|
172
|
+
document.documentElement.removeEventListener('ds:localechange', onLocale);
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|