@directo-ui/astro 0.0.0-stage → 1.0.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 (41) hide show
  1. package/README.md +37 -2
  2. package/components.css +833 -0
  3. package/index.d.ts +31 -0
  4. package/index.js +31 -0
  5. package/package.json +51 -4
  6. package/src/components/Accordion.astro +8 -0
  7. package/src/components/AccordionItem.astro +14 -0
  8. package/src/components/Alert.astro +42 -0
  9. package/src/components/Avatar.astro +21 -0
  10. package/src/components/Badge.astro +13 -0
  11. package/src/components/BottomSheet.astro +23 -0
  12. package/src/components/Breadcrumb.astro +18 -0
  13. package/src/components/Button.astro +31 -0
  14. package/src/components/Card.astro +25 -0
  15. package/src/components/Checkbox.astro +14 -0
  16. package/src/components/DatePicker.astro +25 -0
  17. package/src/components/Divider.astro +11 -0
  18. package/src/components/Dropdown.astro +71 -0
  19. package/src/components/EmptyState.astro +15 -0
  20. package/src/components/IconButton.astro +18 -0
  21. package/src/components/Modal.astro +34 -0
  22. package/src/components/Pagination.astro +30 -0
  23. package/src/components/Progress.astro +20 -0
  24. package/src/components/RadioGroup.astro +24 -0
  25. package/src/components/Select.astro +29 -0
  26. package/src/components/Skeleton.astro +13 -0
  27. package/src/components/Spinner.astro +17 -0
  28. package/src/components/Switch.astro +13 -0
  29. package/src/components/Tabs.astro +60 -0
  30. package/src/components/TextField.astro +24 -0
  31. package/src/components/Textarea.astro +41 -0
  32. package/src/components/Toaster.astro +8 -0
  33. package/src/components/Tooltip.astro +31 -0
  34. package/src/modal.d.ts +3 -0
  35. package/src/modal.js +39 -0
  36. package/src/pagination.d.ts +2 -0
  37. package/src/pagination.js +16 -0
  38. package/src/toast.d.ts +9 -0
  39. package/src/toast.js +66 -0
  40. package/styles.css +3 -0
  41. package/tokens.css +321 -0
@@ -0,0 +1,13 @@
1
+ ---
2
+ interface Props {
3
+ lines?: number;
4
+ avatar?: boolean;
5
+ label?: string;
6
+ }
7
+
8
+ const { lines = 3, avatar = false, label = 'Cargando contenido' } = Astro.props;
9
+ ---
10
+ <div role="status" aria-label={label} style={avatar ? 'display:flex;gap:12px;align-items:flex-start' : undefined}>
11
+ {avatar && <span class="ds-skeleton-circle" aria-hidden="true" />}
12
+ <span class="ds-skeleton" aria-hidden="true">{Array.from({ length: Math.max(1, lines) }, () => <span />)}</span>
13
+ </div>
@@ -0,0 +1,17 @@
1
+ ---
2
+ interface Props {
3
+ size?: 'sm' | 'md' | 'lg';
4
+ label?: string;
5
+ showLabel?: boolean;
6
+ /** "dots" renders three pulsing dots. */
7
+ variant?: 'spinner' | 'dots';
8
+ }
9
+
10
+ const { size = 'md', label = 'Cargando', showLabel = false, variant = 'spinner' } = Astro.props;
11
+ ---
12
+ <span class="ds-loading" role="status" aria-label={showLabel ? undefined : label}>
13
+ {variant === 'dots'
14
+ ? <span class="ds-loading-dots" aria-hidden="true"><span /><span /><span /></span>
15
+ : <span class="ds-spinner" data-size={size} aria-hidden="true" />}
16
+ {showLabel && <span>{label}</span>}
17
+ </span>
@@ -0,0 +1,13 @@
1
+ ---
2
+ import type { HTMLAttributes } from 'astro/types';
3
+
4
+ interface Props extends Omit<HTMLAttributes<'input'>, 'type' | 'role'> {
5
+ label: string;
6
+ }
7
+
8
+ const { label, class: className, ...rest } = Astro.props;
9
+ ---
10
+ <label class:list={['ds-choice', className]}>
11
+ <input {...rest} type="checkbox" role="switch" />
12
+ {label}
13
+ </label>
@@ -0,0 +1,60 @@
1
+ ---
2
+ export interface TabItem { value: string; label: string; disabled?: boolean }
3
+ interface Props {
4
+ /** Each item renders the slot with the same name as its value. */
5
+ items: TabItem[];
6
+ value?: string;
7
+ label?: string;
8
+ variant?: 'default' | 'pills' | 'enclosed';
9
+ }
10
+
11
+ const { items, value, label, variant = 'default' } = Astro.props;
12
+ const active = value ?? items.find(item => !item.disabled)?.value;
13
+ const base = `ds-tabs-${Math.random().toString(36).slice(2, 9)}`;
14
+ const panels = await Promise.all(items.map(item => Astro.slots.render(item.value)));
15
+ ---
16
+ <div class="ds-tabs" data-ds-tabs>
17
+ <div class="ds-tabs-list" role="tablist" aria-label={label} data-variant={variant}>
18
+ {items.map(item => (
19
+ <button type="button" class="ds-tab" role="tab" id={`${base}-${item.value}-tab`} aria-controls={`${base}-${item.value}-panel`}
20
+ aria-selected={String(item.value === active)} tabindex={item.value === active ? 0 : -1} disabled={item.disabled}
21
+ data-value={item.value}>{item.label}</button>
22
+ ))}
23
+ </div>
24
+ {items.map((item, index) => (
25
+ <div class="ds-tab-panel" role="tabpanel" tabindex="0" id={`${base}-${item.value}-panel`}
26
+ aria-labelledby={`${base}-${item.value}-tab`} hidden={item.value !== active} set:html={panels[index]} />
27
+ ))}
28
+ </div>
29
+
30
+ <script>
31
+ function initTabs() {
32
+ document.querySelectorAll<HTMLElement>('[data-ds-tabs]:not([data-ds-ready])').forEach(root => {
33
+ root.dataset.dsReady = '';
34
+ const tabs = [...root.querySelectorAll<HTMLButtonElement>(':scope > .ds-tabs-list > [role="tab"]')];
35
+ const select = (tab: HTMLButtonElement, focus = false) => {
36
+ for (const item of tabs) {
37
+ const selected = item === tab;
38
+ item.setAttribute('aria-selected', String(selected));
39
+ item.tabIndex = selected ? 0 : -1;
40
+ const panel = document.getElementById(item.getAttribute('aria-controls') ?? '');
41
+ if (panel) panel.hidden = !selected;
42
+ }
43
+ if (focus) tab.focus();
44
+ root.dispatchEvent(new CustomEvent('ds-change', { detail: tab.dataset.value, bubbles: true }));
45
+ };
46
+ tabs.forEach(tab => tab.addEventListener('click', () => select(tab)));
47
+ root.querySelector(':scope > .ds-tabs-list')?.addEventListener('keydown', event => {
48
+ const key = (event as KeyboardEvent).key;
49
+ const enabled = tabs.filter(tab => !tab.disabled);
50
+ const index = enabled.indexOf(document.activeElement as HTMLButtonElement);
51
+ const next = { ArrowRight: index + 1, ArrowLeft: index - 1, Home: 0, End: enabled.length - 1 }[key];
52
+ if (next === undefined || !enabled.length) return;
53
+ event.preventDefault();
54
+ select(enabled[(next + enabled.length) % enabled.length], true);
55
+ });
56
+ });
57
+ }
58
+ initTabs();
59
+ document.addEventListener('astro:page-load', initTabs);
60
+ </script>
@@ -0,0 +1,24 @@
1
+ ---
2
+ import type { HTMLAttributes } from 'astro/types';
3
+
4
+ interface Props extends Omit<HTMLAttributes<'input'>, 'size'> {
5
+ label?: string;
6
+ supportingText?: string;
7
+ error?: boolean;
8
+ size?: 'sm' | 'md' | 'lg';
9
+ }
10
+
11
+ const { label, supportingText, error = false, size = 'md', id, disabled = false, required = false, type = 'text', ...rest } = Astro.props;
12
+ const fieldId = id ?? `ds-field-${Math.random().toString(36).slice(2, 9)}`;
13
+ const hintId = supportingText ? `${fieldId}-hint` : undefined;
14
+ ---
15
+ <div class="ds-input" data-error={String(error)} data-disabled={String(Boolean(disabled))} data-size={size}>
16
+ {label && <label for={fieldId}>{label}{required && <span class="ds-input-required" aria-hidden="true">*</span>}</label>}
17
+ <div class="ds-input-shell">
18
+ {Astro.slots.has('start') && <span class="ds-input-icon" aria-hidden="true"><slot name="start" /></span>}
19
+ <input {...rest} id={fieldId} type={type} disabled={disabled} required={required}
20
+ aria-invalid={error ? 'true' : undefined} aria-describedby={hintId} />
21
+ {Astro.slots.has('end') && <span class="ds-input-icon" aria-hidden="true"><slot name="end" /></span>}
22
+ </div>
23
+ {supportingText && <small id={hintId}>{supportingText}</small>}
24
+ </div>
@@ -0,0 +1,41 @@
1
+ ---
2
+ import type { HTMLAttributes } from 'astro/types';
3
+
4
+ interface Props extends HTMLAttributes<'textarea'> {
5
+ label?: string;
6
+ supportingText?: string;
7
+ error?: boolean;
8
+ /** Live character counter (requires maxlength). */
9
+ showCount?: boolean;
10
+ value?: string;
11
+ }
12
+
13
+ const { label, supportingText, error = false, showCount = false, id, disabled = false, required = false, rows = 4, maxlength, value = '', ...rest } = Astro.props;
14
+ const fieldId = id ?? `ds-field-${Math.random().toString(36).slice(2, 9)}`;
15
+ const hintId = supportingText ? `${fieldId}-hint` : undefined;
16
+ ---
17
+ <div class="ds-input" data-multiline="true" data-error={String(error)} data-disabled={String(Boolean(disabled))}>
18
+ {label && <label for={fieldId}>{label}{required && <span class="ds-input-required" aria-hidden="true">*</span>}</label>}
19
+ <div class="ds-input-shell">
20
+ <textarea {...rest} id={fieldId} rows={rows} maxlength={maxlength} disabled={disabled} required={required}
21
+ aria-invalid={error ? 'true' : undefined} aria-describedby={hintId} data-ds-count={showCount && maxlength ? '' : undefined}>{value}</textarea>
22
+ </div>
23
+ {(supportingText || (showCount && maxlength)) && (
24
+ <span style="display:flex;justify-content:space-between;gap:12px">
25
+ {supportingText ? <small id={hintId}>{supportingText}</small> : <span />}
26
+ {showCount && maxlength && <span class="ds-input-counter" data-ds-counter-for={fieldId}>{value.length}/{maxlength}</span>}
27
+ </span>
28
+ )}
29
+ </div>
30
+
31
+ <script>
32
+ function initCounters() {
33
+ document.querySelectorAll<HTMLTextAreaElement>('textarea[data-ds-count]:not([data-ds-ready])').forEach(area => {
34
+ area.dataset.dsReady = '';
35
+ const counter = document.querySelector(`[data-ds-counter-for="${area.id}"]`);
36
+ area.addEventListener('input', () => { if (counter) counter.textContent = `${area.value.length}/${area.maxLength}`; });
37
+ });
38
+ }
39
+ initCounters();
40
+ document.addEventListener('astro:page-load', initCounters);
41
+ </script>
@@ -0,0 +1,8 @@
1
+ ---
2
+ interface Props {
3
+ position?: 'bottom-right' | 'top-right' | 'top-center' | 'bottom-center';
4
+ }
5
+
6
+ const { position = 'bottom-right' } = Astro.props;
7
+ ---
8
+ <ol class="ds-toaster" data-position={position} aria-label="Notificaciones" data-ds-toaster></ol>
@@ -0,0 +1,31 @@
1
+ ---
2
+ interface Props {
3
+ text: string;
4
+ position?: 'top' | 'bottom' | 'left' | 'right';
5
+ /** Milliseconds before showing. */
6
+ delay?: number;
7
+ }
8
+
9
+ const { text, position = 'top', delay = 250 } = Astro.props;
10
+ const id = `ds-tip-${Math.random().toString(36).slice(2, 9)}`;
11
+ ---
12
+ <span class="ds-tooltip-wrap" style={`--ds-tooltip-delay:${delay}ms`} data-ds-tooltip={id}>
13
+ <slot />
14
+ <span class="ds-tooltip" role="tooltip" id={id} data-side={position}>{text}</span>
15
+ </span>
16
+
17
+ <script>
18
+ function initTooltips() {
19
+ document.querySelectorAll<HTMLElement>('[data-ds-tooltip]:not([data-ds-ready])').forEach(wrap => {
20
+ wrap.dataset.dsReady = '';
21
+ const trigger = wrap.querySelector('a, button, input, select, textarea, [tabindex]');
22
+ if (trigger && !trigger.hasAttribute('aria-describedby')) trigger.setAttribute('aria-describedby', wrap.dataset.dsTooltip ?? '');
23
+ wrap.addEventListener('keydown', event => { if (event.key === 'Escape') wrap.dataset.dismissed = 'true'; });
24
+ const reset = () => delete wrap.dataset.dismissed;
25
+ wrap.addEventListener('mouseleave', reset);
26
+ wrap.addEventListener('focusout', reset);
27
+ });
28
+ }
29
+ initTooltips();
30
+ document.addEventListener('astro:page-load', initTooltips);
31
+ </script>
package/src/modal.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function initModals(root?: ParentNode): void;
2
+ export declare function openModal(id: string, opener?: HTMLElement): void;
3
+ export declare function closeModal(id: string): void;
package/src/modal.js ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Wires native <dialog data-ds-modal> elements:
3
+ * - any element with data-ds-open="<dialog id>" opens it as a modal,
4
+ * - any [data-ds-close] inside closes it, as does a backdrop click or Escape,
5
+ * - focus returns to the opener. Safe to call repeatedly.
6
+ * @param {ParentNode} [root=document]
7
+ */
8
+ export function initModals(root = document) {
9
+ root.querySelectorAll('[data-ds-open]:not([data-ds-ready])').forEach(trigger => {
10
+ trigger.dataset.dsReady = '';
11
+ trigger.setAttribute('aria-haspopup', 'dialog');
12
+ trigger.addEventListener('click', () => openModal(trigger.dataset.dsOpen ?? '', trigger));
13
+ });
14
+ root.querySelectorAll('dialog[data-ds-modal]:not([data-ds-ready])').forEach(dialog => {
15
+ dialog.dataset.dsReady = '';
16
+ dialog.addEventListener('click', event => {
17
+ const target = event.target;
18
+ if (target === dialog || (target instanceof Element && target.closest('[data-ds-close]'))) dialog.close();
19
+ });
20
+ if (dialog.hasAttribute('data-ds-open-initially')) dialog.showModal();
21
+ });
22
+ }
23
+
24
+ const openers = new WeakMap();
25
+
26
+ /** @param {string} id @param {HTMLElement} [opener] */
27
+ export function openModal(id, opener) {
28
+ const dialog = document.getElementById(id);
29
+ if (!(dialog instanceof HTMLDialogElement) || dialog.open) return;
30
+ if (opener) openers.set(dialog, opener);
31
+ dialog.showModal();
32
+ dialog.addEventListener('close', () => openers.get(dialog)?.focus({ preventScroll: true }), { once: true });
33
+ }
34
+
35
+ /** @param {string} id */
36
+ export function closeModal(id) {
37
+ const dialog = document.getElementById(id);
38
+ if (dialog instanceof HTMLDialogElement) dialog.close();
39
+ }
@@ -0,0 +1,2 @@
1
+ export type PageToken = number | 'ellipsis-start' | 'ellipsis-end';
2
+ export declare function paginationRange(page: number, totalPages: number, siblings?: number): PageToken[];
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Pages to display: always first/last, `siblings` around the current page, and ellipses.
3
+ * @param {number} page @param {number} totalPages @param {number} [siblings=1]
4
+ * @returns {(number | 'ellipsis-start' | 'ellipsis-end')[]}
5
+ */
6
+ export function paginationRange(page, totalPages, siblings = 1) {
7
+ const total = Math.max(1, Math.floor(totalPages));
8
+ const current = Math.min(Math.max(1, Math.floor(page)), total);
9
+ const edge = 3 + siblings * 2;
10
+ if (total <= edge + 2) return Array.from({ length: total }, (_, i) => i + 1);
11
+ const left = Math.max(current - siblings, 1);
12
+ const right = Math.min(current + siblings, total);
13
+ if (left <= 3) return [...Array.from({ length: edge }, (_, i) => i + 1), 'ellipsis-end', total];
14
+ if (right >= total - 2) return [1, 'ellipsis-start', ...Array.from({ length: edge }, (_, i) => total - edge + i + 1)];
15
+ return [1, 'ellipsis-start', ...Array.from({ length: right - left + 1 }, (_, i) => left + i), 'ellipsis-end', total];
16
+ }
package/src/toast.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export interface ToastOptions {
2
+ title: string;
3
+ description?: string;
4
+ variant?: 'info' | 'success' | 'warning' | 'error';
5
+ /** Milliseconds before auto-dismiss; 0 keeps it until closed. Default 5000. */
6
+ duration?: number;
7
+ }
8
+ export declare function toast(options: ToastOptions): HTMLLIElement;
9
+ export declare function dismissToast(item: HTMLElement): void;
package/src/toast.js ADDED
@@ -0,0 +1,66 @@
1
+ // Client helper for <Toaster />. Usage inside a <script>:
2
+ // import { toast } from '@directo-ui/astro/toast';
3
+ // toast({ title: 'Guardado', variant: 'success' });
4
+
5
+ const paths = {
6
+ info: 'M128 24a104 104 0 1 0 104 104A104.11 104.11 0 0 0 128 24Zm-4 48a12 12 0 1 1-12 12 12 12 0 0 1 12-12Zm12 112a16 16 0 0 1-16-16v-40a8 8 0 0 1 0-16 16 16 0 0 1 16 16v40a8 8 0 0 1 0 16Z',
7
+ success: 'M128 24a104 104 0 1 0 104 104A104.11 104.11 0 0 0 128 24Zm45.66 85.66-56 56a8 8 0 0 1-11.32 0l-24-24a8 8 0 0 1 11.32-11.32L112 148.69l50.34-50.35a8 8 0 0 1 11.32 11.32Z',
8
+ warning: 'M236.8 188.09 149.35 36.22a24.76 24.76 0 0 0-42.7 0L19.2 188.09a23.51 23.51 0 0 0 0 23.72A24.35 24.35 0 0 0 40.55 224h174.9a24.35 24.35 0 0 0 21.33-12.19 23.51 23.51 0 0 0 .02-23.72ZM120 104a8 8 0 0 1 16 0v40a8 8 0 0 1-16 0Zm8 88a12 12 0 1 1 12-12 12 12 0 0 1-12 12Z',
9
+ error: 'M128 24a104 104 0 1 0 104 104A104.11 104.11 0 0 0 128 24Zm-8 56a8 8 0 0 1 16 0v56a8 8 0 0 1-16 0Zm8 104a12 12 0 1 1 12-12 12 12 0 0 1-12 12Z',
10
+ };
11
+
12
+ function region() {
13
+ let toaster = document.querySelector('[data-ds-toaster]');
14
+ if (!toaster) {
15
+ toaster = document.createElement('ol');
16
+ toaster.className = 'ds-toaster';
17
+ toaster.setAttribute('aria-label', 'Notificaciones');
18
+ toaster.setAttribute('data-ds-toaster', '');
19
+ document.body.append(toaster);
20
+ }
21
+ return toaster;
22
+ }
23
+
24
+ export function dismissToast(item) {
25
+ if (!item || item.dataset.state === 'closing') return;
26
+ item.dataset.state = 'closing';
27
+ setTimeout(() => item.remove(), 180);
28
+ }
29
+
30
+ /** @param {{ title: string, description?: string, variant?: 'info'|'success'|'warning'|'error', duration?: number }} options */
31
+ export function toast({ title, description, variant = 'info', duration = 5000 }) {
32
+ const toaster = region();
33
+ while (toaster.children.length >= 5) toaster.firstElementChild?.remove();
34
+ const item = document.createElement('li');
35
+ item.className = 'ds-toast';
36
+ item.dataset.variant = variant;
37
+ item.dataset.state = 'open';
38
+ item.setAttribute('role', variant === 'error' ? 'alert' : 'status');
39
+ item.setAttribute('aria-atomic', 'true');
40
+ const icon = document.createElement('span');
41
+ icon.className = 'ds-toast-icon';
42
+ icon.setAttribute('aria-hidden', 'true');
43
+ icon.innerHTML = `<svg viewBox="0 0 256 256" fill="currentColor"><path d="${paths[variant] ?? paths.info}"/></svg>`;
44
+ const content = document.createElement('div');
45
+ content.className = 'ds-toast-content';
46
+ const heading = document.createElement('span');
47
+ heading.className = 'ds-toast-title';
48
+ heading.textContent = title;
49
+ content.append(heading);
50
+ if (description) {
51
+ const text = document.createElement('p');
52
+ text.className = 'ds-toast-description';
53
+ text.textContent = description;
54
+ content.append(text);
55
+ }
56
+ const close = document.createElement('button');
57
+ close.type = 'button';
58
+ close.className = 'ds-toast-close';
59
+ close.setAttribute('aria-label', 'Cerrar notificación');
60
+ close.textContent = '×';
61
+ close.addEventListener('click', () => dismissToast(item));
62
+ item.append(icon, content, close);
63
+ toaster.append(item);
64
+ if (duration > 0) setTimeout(() => dismissToast(item), duration);
65
+ return item;
66
+ }
package/styles.css ADDED
@@ -0,0 +1,3 @@
1
+ /* @directo-ui/astro: design tokens + component styles (generated copies, run `pnpm build`). */
2
+ @import "./tokens.css";
3
+ @import "./components.css";