@directo-ui/astro 1.2.0 → 1.3.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.
- package/components.css +138 -0
- package/index.d.ts +10 -0
- package/index.js +9 -0
- package/package.json +5 -1
- package/src/components/Autocomplete.astro +106 -0
- package/src/components/DataTable.astro +136 -0
- package/src/components/DateRangePicker.astro +105 -0
- package/src/components/Drawer.astro +40 -0
- package/src/components/FileUpload.astro +152 -0
- package/src/components/FilterBar.astro +66 -0
- package/src/components/Sidebar.astro +64 -0
- package/src/components/Timeline.astro +25 -0
- package/src/files.d.ts +27 -0
- package/src/files.js +52 -0
- package/src/modal.d.ts +3 -3
- package/src/modal.js +50 -39
package/components.css
CHANGED
|
@@ -74,6 +74,7 @@
|
|
|
74
74
|
html body[data-scroll-locked] { margin-right: 0 !important; }
|
|
75
75
|
}
|
|
76
76
|
html:has(dialog.ds-dialog[open]), html:has(dialog.ds-sheet[open]) { overflow: hidden; }
|
|
77
|
+
html:has(dialog.ds-drawer[open]) { overflow: hidden; }
|
|
77
78
|
|
|
78
79
|
[class^="ds-"], [class*=" ds-"] { box-sizing: border-box; }
|
|
79
80
|
/* The hidden attribute must win over component display values. */
|
|
@@ -1492,3 +1493,140 @@ dialog.ds-sheet[open] { animation: ds-sheet-up 280ms var(--ds-ease-out) both; }
|
|
|
1492
1493
|
.dark .ds-wpassenger input.ds-wpassenger-input, [data-theme="dark"] .ds-wpassenger input.ds-wpassenger-input { color: var(--ds-text-primary); }
|
|
1493
1494
|
.dark .ds-wpassenger input.ds-wpassenger-input::placeholder, [data-theme="dark"] .ds-wpassenger input.ds-wpassenger-input::placeholder { color: var(--ds-text-tertiary, #8A92A6); }
|
|
1494
1495
|
.dark .ds-wpassenger-clear, [data-theme="dark"] .ds-wpassenger-clear { color: var(--ds-text-secondary); }
|
|
1496
|
+
|
|
1497
|
+
/* Tables, filters and navigation */
|
|
1498
|
+
.ds-data-table, .ds-filter-bar, .ds-sidebar, .ds-timeline, .ds-date-range, .ds-file-upload {
|
|
1499
|
+
color: var(--ds-text-primary);
|
|
1500
|
+
font: 400 14px/1.5 var(--ds-font-sans);
|
|
1501
|
+
}
|
|
1502
|
+
.ds-data-table { display: grid; gap: 12px; min-inline-size: 0; }
|
|
1503
|
+
.ds-data-scroll { overflow: auto; border: 1px solid var(--ds-border-secondary); border-radius: 12px; }
|
|
1504
|
+
.ds-data-scroll:focus-visible, .ds-sort-button:focus-visible, .ds-filter-chip:focus-visible,
|
|
1505
|
+
.ds-sidebar a:focus-visible, .ds-file-upload input:focus-visible {
|
|
1506
|
+
outline: 2px solid var(--ds-border-focus);
|
|
1507
|
+
outline-offset: 2px;
|
|
1508
|
+
}
|
|
1509
|
+
.ds-data-table table { width: 100%; border-collapse: collapse; text-align: start; background: var(--ds-bg-primary); }
|
|
1510
|
+
.ds-data-table caption { padding: 12px 16px; text-align: start; font-weight: 700; background: var(--ds-bg-primary); }
|
|
1511
|
+
.ds-data-table th, .ds-data-table td { padding: 12px 16px; text-align: start; border-block-start: 1px solid var(--ds-border-secondary); }
|
|
1512
|
+
.ds-data-table th { background: var(--ds-bg-secondary); font-weight: 600; }
|
|
1513
|
+
.ds-data-table td a { color: var(--ds-text-link); }
|
|
1514
|
+
.ds-sort-button { padding: 0; border: 0; background: none; color: inherit; font: inherit; cursor: pointer; text-align: start; }
|
|
1515
|
+
.ds-sort-button span { margin-inline-start: 4px; color: var(--ds-text-link); }
|
|
1516
|
+
.ds-filter-bar { display: grid; min-inline-size: 0; gap: 12px; }
|
|
1517
|
+
.ds-filter-fields, .ds-filter-active, .ds-date-range-fields { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; }
|
|
1518
|
+
.ds-filter-fields > *, .ds-date-range-fields > * { flex: 1 1 155px; min-inline-size: 0; }
|
|
1519
|
+
.ds-filter-active { align-items: center; gap: 8px; }
|
|
1520
|
+
.ds-filter-chip {
|
|
1521
|
+
display: inline-flex; align-items: center; gap: 8px; min-height: 32px; max-width: 100%;
|
|
1522
|
+
padding: 4px 10px; overflow-wrap: anywhere; border: 1px solid var(--ds-border-primary); border-radius: 20px;
|
|
1523
|
+
color: var(--ds-text-primary); background: var(--ds-bg-secondary); font: inherit; cursor: pointer; text-decoration: none;
|
|
1524
|
+
}
|
|
1525
|
+
.ds-filter-chip:hover { background: var(--ds-bg-secondary-hover); }
|
|
1526
|
+
.ds-sidebar { display: grid; gap: 16px; width: 100%; max-width: 260px; padding: 12px; background: var(--ds-bg-primary); }
|
|
1527
|
+
.ds-sidebar p { margin: 0 0 4px; padding-inline: 8px; color: var(--ds-text-secondary); font-weight: 600; }
|
|
1528
|
+
.ds-sidebar ul { display: grid; gap: 2px; list-style: none; padding: 0; margin: 0; }
|
|
1529
|
+
.ds-sidebar ul ul { margin-inline-start: 12px; }
|
|
1530
|
+
.ds-sidebar a { display: block; padding: 8px; color: var(--ds-text-primary); border-radius: 6px; text-decoration: none; }
|
|
1531
|
+
.ds-sidebar a:hover { background: var(--ds-bg-primary-hover); }
|
|
1532
|
+
.ds-sidebar a[aria-current="page"] { background: var(--ds-bg-brand-secondary); color: var(--ds-text-brand-primary); font-weight: 700; }
|
|
1533
|
+
|
|
1534
|
+
/* Drawer (Radix or native dialog) */
|
|
1535
|
+
.ds-drawer-overlay { position: fixed; inset: 0; z-index: 1200; background: var(--ds-overlay); }
|
|
1536
|
+
.ds-drawer {
|
|
1537
|
+
position: fixed; inset: 0 0 0 auto; z-index: 1201; margin: 0; padding: 0; width: min(100%, 440px);
|
|
1538
|
+
height: 100%; max-width: none; max-height: none; border: 0; border-inline-start: 1px solid var(--ds-border-secondary);
|
|
1539
|
+
background: var(--ds-bg-primary); color: var(--ds-text-primary); font: 400 14px/1.5 var(--ds-font-sans);
|
|
1540
|
+
}
|
|
1541
|
+
.ds-drawer[data-side="start"] { inset: 0 auto 0 0; border-inline-start: 0; border-inline-end: 1px solid var(--ds-border-secondary); }
|
|
1542
|
+
.ds-drawer:not(dialog), dialog.ds-drawer[open] { display: flex; flex-direction: column; }
|
|
1543
|
+
.ds-drawer::backdrop { background: var(--ds-overlay); }
|
|
1544
|
+
.ds-drawer-header {
|
|
1545
|
+
display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 16px 24px;
|
|
1546
|
+
border-block-end: 1px solid var(--ds-border-secondary);
|
|
1547
|
+
}
|
|
1548
|
+
.ds-drawer-header h2 { margin: 0; font-size: 20px; }
|
|
1549
|
+
.ds-drawer-body { flex: 1; min-height: 0; overflow: auto; padding: 24px; }
|
|
1550
|
+
|
|
1551
|
+
/* Timeline and form fields */
|
|
1552
|
+
.ds-timeline { list-style: none; margin: 0; padding-inline-start: 0; }
|
|
1553
|
+
.ds-timeline li { position: relative; display: grid; gap: 2px; padding: 0 0 18px 18px; margin-inline-start: 6px; }
|
|
1554
|
+
.ds-timeline li:not(:last-child) { border-inline-start: 1px solid var(--ds-border-primary); }
|
|
1555
|
+
.ds-timeline li:last-child { border-inline-start: 1px solid transparent; padding-block-end: 0; }
|
|
1556
|
+
.ds-timeline li::before {
|
|
1557
|
+
content: ""; position: absolute; inset-inline-start: -5px; inset-block-start: 5px; width: 9px; height: 9px;
|
|
1558
|
+
border-radius: 50%; background: var(--ds-bg-brand-primary);
|
|
1559
|
+
}
|
|
1560
|
+
.ds-timeline time, .ds-timeline li > span { color: var(--ds-text-secondary); }
|
|
1561
|
+
.ds-date-range { padding: 0; margin: 0; border: 0; min-inline-size: 0; }
|
|
1562
|
+
.ds-date-range legend { padding: 0; margin-block-end: 6px; font-weight: 600; }
|
|
1563
|
+
.ds-file-upload { display: grid; gap: 8px; min-inline-size: 0; }
|
|
1564
|
+
.ds-file-upload label { font-weight: 600; }
|
|
1565
|
+
.ds-file-upload input { max-width: 100%; font: inherit; }
|
|
1566
|
+
.ds-file-upload [role="status"] { color: var(--ds-text-error-tertiary); }
|
|
1567
|
+
.ds-file-upload [role="status"]:empty { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
|
|
1568
|
+
.ds-file-upload ul { list-style: none; padding: 0; margin: 0; }
|
|
1569
|
+
.ds-file-upload li { display: flex; gap: 10px; align-items: center; padding-block: 6px; border-block-start: 1px solid var(--ds-border-secondary); }
|
|
1570
|
+
.ds-file-upload img { flex: none; width: 40px; height: 40px; border-radius: 6px; object-fit: cover; }
|
|
1571
|
+
.ds-file-upload li > span { flex: 1; overflow-wrap: anywhere; }
|
|
1572
|
+
|
|
1573
|
+
/* Operational components: attributes added in 1.3 */
|
|
1574
|
+
.ds-data-table[data-density="compact"] th, .ds-data-table[data-density="compact"] td { padding: 6px 12px; }
|
|
1575
|
+
.ds-data-table [data-align="center"] { text-align: center; }
|
|
1576
|
+
.ds-data-table [data-align="end"] { text-align: end; font-variant-numeric: tabular-nums; }
|
|
1577
|
+
.ds-data-table[data-sticky-header] thead th { position: sticky; inset-block-start: 0; z-index: 1; }
|
|
1578
|
+
.ds-data-table tr[data-clickable] { cursor: pointer; transition: background-color var(--ds-duration-fast, .15s) ease; }
|
|
1579
|
+
.ds-data-table tr[data-clickable]:hover > td { background: var(--ds-bg-primary-hover); }
|
|
1580
|
+
.ds-data-table tr[data-clickable]:focus-visible { outline: 2px solid var(--ds-border-focus); outline-offset: -2px; }
|
|
1581
|
+
.ds-filter-bar { margin: 0; }
|
|
1582
|
+
.ds-filter-actions { display: flex; flex-wrap: wrap; justify-content: end; gap: 8px; }
|
|
1583
|
+
.ds-filter-count { color: var(--ds-text-secondary); margin-inline-end: 4px; }
|
|
1584
|
+
.ds-filter-chip[aria-pressed="true"] { border-color: var(--ds-bg-brand-primary); background: var(--ds-bg-brand-secondary); color: var(--ds-text-brand-primary); }
|
|
1585
|
+
.ds-drawer[data-size="sm"] { width: min(100%, 360px); }
|
|
1586
|
+
.ds-drawer[data-size="lg"] { width: min(100%, 640px); }
|
|
1587
|
+
.ds-drawer-header h2 { line-height: 1.3; }
|
|
1588
|
+
.ds-drawer-description { margin: 4px 0 0; color: var(--ds-text-secondary); }
|
|
1589
|
+
.ds-drawer-footer { display: flex; justify-content: end; flex-wrap: wrap; gap: 8px; padding: 16px 24px; border-block-start: 1px solid var(--ds-border-secondary); }
|
|
1590
|
+
.ds-drawer:not(dialog)[data-state="open"], dialog.ds-drawer[open] { animation: ds-drawer-in 260ms var(--ds-ease-out) both; }
|
|
1591
|
+
.ds-drawer[data-side="start"]:not(dialog)[data-state="open"], dialog.ds-drawer[data-side="start"][open] { animation-name: ds-drawer-in-start; }
|
|
1592
|
+
.ds-drawer:not(dialog)[data-state="closed"] { animation: ds-drawer-out 180ms ease-in both; }
|
|
1593
|
+
.ds-drawer[data-side="start"]:not(dialog)[data-state="closed"] { animation-name: ds-drawer-out-start; }
|
|
1594
|
+
.ds-drawer-overlay[data-state="open"], dialog.ds-drawer[open]::backdrop { animation: ds-wroute-fade 200ms ease both; }
|
|
1595
|
+
.ds-drawer-overlay[data-state="closed"] { animation: ds-drawer-fade-out 180ms ease-in both; }
|
|
1596
|
+
@keyframes ds-drawer-in { from { transform: translateX(100%); } to { transform: none; } }
|
|
1597
|
+
@keyframes ds-drawer-in-start { from { transform: translateX(-100%); } to { transform: none; } }
|
|
1598
|
+
@keyframes ds-drawer-out { to { transform: translateX(100%); } }
|
|
1599
|
+
@keyframes ds-drawer-out-start { to { transform: translateX(-100%); } }
|
|
1600
|
+
@keyframes ds-drawer-fade-out { to { opacity: 0; } }
|
|
1601
|
+
.ds-file-upload { border-radius: 12px; transition: outline-color .15s ease, background-color .15s ease; }
|
|
1602
|
+
.ds-file-upload[data-dragging] { outline: 2px dashed var(--ds-bg-brand-primary); outline-offset: 6px; background: color-mix(in srgb, var(--ds-bg-brand-primary) 6%, transparent); }
|
|
1603
|
+
.ds-file-upload[data-disabled] { opacity: .6; }
|
|
1604
|
+
.ds-file-upload > small { color: var(--ds-text-secondary); }
|
|
1605
|
+
.ds-file-upload li > small { flex: none; color: var(--ds-text-secondary); font-variant-numeric: tabular-nums; }
|
|
1606
|
+
.ds-timeline[data-empty] { margin: 0; color: var(--ds-text-secondary); }
|
|
1607
|
+
.ds-timeline[data-density="compact"] li { padding-block-end: 10px; }
|
|
1608
|
+
.ds-timeline li[data-variant="info"]::before { background: var(--ds-bg-brand-primary); }
|
|
1609
|
+
.ds-timeline li[data-variant="success"]::before { background: var(--ds-fg-success-primary); }
|
|
1610
|
+
.ds-timeline li[data-variant="warning"]::before { background: var(--ds-fg-warning-primary); }
|
|
1611
|
+
.ds-timeline li[data-variant="error"]::before { background: var(--ds-fg-error-primary); }
|
|
1612
|
+
.ds-timeline li[aria-current="step"]::before { box-shadow: 0 0 0 4px color-mix(in srgb, var(--ds-bg-brand-primary) 25%, transparent); }
|
|
1613
|
+
.ds-timeline li[aria-current="step"] > strong { color: var(--ds-text-brand-primary); }
|
|
1614
|
+
.ds-date-range-presets { display: flex; flex-wrap: wrap; gap: 8px; margin-block-end: 12px; }
|
|
1615
|
+
.ds-date-range-error { margin: 6px 0 0; color: var(--ds-text-error-tertiary); font-size: 13px; }
|
|
1616
|
+
.ds-date-range-error:empty { display: none; }
|
|
1617
|
+
.ds-date-range:disabled { opacity: .6; }
|
|
1618
|
+
.ds-sidebar .ds-sidebar-link { display: flex; align-items: center; gap: 8px; }
|
|
1619
|
+
.ds-sidebar-text { flex: 1; min-width: 0; }
|
|
1620
|
+
.ds-sidebar-icon { display: inline-flex; flex: none; width: 18px; height: 18px; }
|
|
1621
|
+
.ds-sidebar-icon > svg { width: 100%; height: 100%; }
|
|
1622
|
+
.ds-sidebar-badge { flex: none; min-width: 20px; padding: 0 6px; border-radius: 10px; background: color-mix(in srgb, var(--ds-bg-brand-primary) 12%, transparent); color: var(--ds-text-brand-primary);
|
|
1623
|
+
font-size: 12px; font-weight: 600; line-height: 20px; text-align: center; }
|
|
1624
|
+
.ds-sidebar a[aria-current="page"] .ds-sidebar-badge { background: var(--ds-bg-primary); color: var(--ds-text-brand-primary); }
|
|
1625
|
+
.ds-sidebar span.ds-sidebar-link[aria-disabled="true"] { padding: 8px; color: var(--ds-text-secondary); opacity: .6; cursor: not-allowed; }
|
|
1626
|
+
.ds-sidebar details > summary { padding: 4px 8px; border-radius: 6px; color: var(--ds-text-secondary); font-weight: 600; cursor: pointer; }
|
|
1627
|
+
.ds-sidebar details > summary:hover { background: var(--ds-bg-primary-hover); }
|
|
1628
|
+
.ds-sidebar details > summary:focus-visible { outline: 2px solid var(--ds-border-focus); outline-offset: 2px; }
|
|
1629
|
+
.ds-sidebar details[open] > summary { margin-block-end: 4px; }
|
|
1630
|
+
@media (prefers-reduced-motion: reduce) {
|
|
1631
|
+
.ds-drawer, .ds-drawer-overlay, dialog.ds-drawer::backdrop { animation: none !important; }
|
|
1632
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -29,3 +29,13 @@ export { default as Divider } from './src/components/Divider.astro';
|
|
|
29
29
|
export { default as Toaster } from './src/components/Toaster.astro';
|
|
30
30
|
export { initModals, openModal, closeModal } from './src/modal';
|
|
31
31
|
export { paginationRange } from './src/pagination';
|
|
32
|
+
export { default as DataTable } from './src/components/DataTable.astro';
|
|
33
|
+
export { default as FilterBar } from './src/components/FilterBar.astro';
|
|
34
|
+
export { default as Drawer } from './src/components/Drawer.astro';
|
|
35
|
+
export { default as FileUpload } from './src/components/FileUpload.astro';
|
|
36
|
+
export { default as Timeline } from './src/components/Timeline.astro';
|
|
37
|
+
export { default as DateRangePicker } from './src/components/DateRangePicker.astro';
|
|
38
|
+
export { default as Autocomplete } from './src/components/Autocomplete.astro';
|
|
39
|
+
export { default as Sidebar } from './src/components/Sidebar.astro';
|
|
40
|
+
export { acceptsFile, formatBytes, selectFiles } from './src/files.js';
|
|
41
|
+
export type { FileRejection, FileRejectionReason, FileRules } from './src/files.js';
|
package/index.js
CHANGED
|
@@ -29,3 +29,12 @@ export { default as Divider } from './src/components/Divider.astro';
|
|
|
29
29
|
export { default as Toaster } from './src/components/Toaster.astro';
|
|
30
30
|
export { initModals, openModal, closeModal } from './src/modal.js';
|
|
31
31
|
export { paginationRange } from './src/pagination.js';
|
|
32
|
+
export { default as DataTable } from './src/components/DataTable.astro';
|
|
33
|
+
export { default as FilterBar } from './src/components/FilterBar.astro';
|
|
34
|
+
export { default as Drawer } from './src/components/Drawer.astro';
|
|
35
|
+
export { default as FileUpload } from './src/components/FileUpload.astro';
|
|
36
|
+
export { default as Timeline } from './src/components/Timeline.astro';
|
|
37
|
+
export { default as DateRangePicker } from './src/components/DateRangePicker.astro';
|
|
38
|
+
export { default as Autocomplete } from './src/components/Autocomplete.astro';
|
|
39
|
+
export { default as Sidebar } from './src/components/Sidebar.astro';
|
|
40
|
+
export { acceptsFile, formatBytes, selectFiles } from './src/files.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@directo-ui/astro",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Componentes Astro del sistema de diseño Directo (HTML semántico, sin framework en el cliente)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
"types": "./src/modal.d.ts",
|
|
37
37
|
"default": "./src/modal.js"
|
|
38
38
|
},
|
|
39
|
+
"./files": {
|
|
40
|
+
"types": "./src/files.d.ts",
|
|
41
|
+
"default": "./src/files.js"
|
|
42
|
+
},
|
|
39
43
|
"./pagination": {
|
|
40
44
|
"types": "./src/pagination.d.ts",
|
|
41
45
|
"default": "./src/pagination.js"
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
options: readonly string[];
|
|
6
|
+
name?: string;
|
|
7
|
+
placeholder?: string;
|
|
8
|
+
value?: string;
|
|
9
|
+
supportingText?: string;
|
|
10
|
+
required?: boolean;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
/** Wait after the last key before emitting ds-search. */
|
|
13
|
+
debounceMs?: number;
|
|
14
|
+
/** Minimum characters for ds-search. */
|
|
15
|
+
minChars?: number;
|
|
16
|
+
loadingText?: string;
|
|
17
|
+
/** When false, leaving the field with a value that is not in the list shows invalidMessage. */
|
|
18
|
+
allowCustomValue?: boolean;
|
|
19
|
+
invalidMessage?: string;
|
|
20
|
+
class?: string;
|
|
21
|
+
}
|
|
22
|
+
const {
|
|
23
|
+
id, label, options, name, placeholder, value, supportingText, required = false, disabled = false, debounceMs = 250, minChars = 1,
|
|
24
|
+
loadingText = 'Buscando…', allowCustomValue = true, invalidMessage = 'Elige una opción de la lista.', class: className,
|
|
25
|
+
} = Astro.props;
|
|
26
|
+
---
|
|
27
|
+
<directo-autocomplete class:list={["ds-input", className]} data-size="md" data-error="false" data-disabled={String(disabled)}
|
|
28
|
+
data-debounce={debounceMs} data-min-chars={minChars} data-strict={allowCustomValue ? undefined : ''}
|
|
29
|
+
data-invalid-message={invalidMessage} data-loading-text={loadingText} data-hint={supportingText}>
|
|
30
|
+
<label for={id}>{label}{required && <span class="ds-input-required" aria-hidden="true">*</span>}</label>
|
|
31
|
+
<div class="ds-input-shell">
|
|
32
|
+
<input id={id} type="text" name={name} list={`${id}-options`} autocomplete="off" placeholder={placeholder} value={value}
|
|
33
|
+
required={required} disabled={disabled} aria-describedby={`${id}-hint`} />
|
|
34
|
+
</div>
|
|
35
|
+
<small id={`${id}-hint`} hidden={!supportingText}>{supportingText}</small>
|
|
36
|
+
<datalist id={`${id}-options`}>{[...new Set(options)].map(option => <option value={option}></option>)}</datalist>
|
|
37
|
+
</directo-autocomplete>
|
|
38
|
+
|
|
39
|
+
<script>
|
|
40
|
+
class DirectoAutocompleteElement extends HTMLElement {
|
|
41
|
+
private timer?: ReturnType<typeof setTimeout>;
|
|
42
|
+
private invalid = false;
|
|
43
|
+
private busy = false;
|
|
44
|
+
|
|
45
|
+
connectedCallback() {
|
|
46
|
+
this.addEventListener('input', this.change);
|
|
47
|
+
this.addEventListener('focusout', this.leave);
|
|
48
|
+
}
|
|
49
|
+
disconnectedCallback() {
|
|
50
|
+
this.removeEventListener('input', this.change);
|
|
51
|
+
this.removeEventListener('focusout', this.leave);
|
|
52
|
+
clearTimeout(this.timer);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private get field() { return this.querySelector<HTMLInputElement>('input'); }
|
|
56
|
+
private get options() { return Array.from(this.querySelectorAll('datalist option'), option => (option as HTMLOptionElement).value); }
|
|
57
|
+
|
|
58
|
+
/** Replaces the suggestions (for example with the results of your API after ds-search) and ends the loading state. */
|
|
59
|
+
dsSetOptions(options: readonly string[]) {
|
|
60
|
+
const list = this.querySelector('datalist');
|
|
61
|
+
list?.replaceChildren(...[...new Set(options)].map(value => Object.assign(document.createElement('option'), { value })));
|
|
62
|
+
this.dsSetLoading(false);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Shows the loading text under the field. */
|
|
66
|
+
dsSetLoading(loading: boolean) {
|
|
67
|
+
this.busy = loading;
|
|
68
|
+
this.render();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private change = (event: Event) => {
|
|
72
|
+
const field = this.field;
|
|
73
|
+
if (!field || event.target !== field) return;
|
|
74
|
+
this.invalid = false;
|
|
75
|
+
this.render();
|
|
76
|
+
const value = field.value;
|
|
77
|
+
this.dispatchEvent(new CustomEvent('ds-change', { bubbles: true, detail: value }));
|
|
78
|
+
if (this.options.includes(value)) this.dispatchEvent(new CustomEvent('ds-select', { bubbles: true, detail: value }));
|
|
79
|
+
clearTimeout(this.timer);
|
|
80
|
+
const query = value.trim();
|
|
81
|
+
if (query.length < Number(this.dataset.minChars ?? 1)) return;
|
|
82
|
+
this.timer = setTimeout(() => this.dispatchEvent(new CustomEvent('ds-search', { bubbles: true, detail: query })),
|
|
83
|
+
Math.max(0, Number(this.dataset.debounce ?? 250)));
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
private leave = () => {
|
|
87
|
+
const field = this.field;
|
|
88
|
+
if (!field || !this.hasAttribute('data-strict')) return;
|
|
89
|
+
this.invalid = !!field.value && !this.options.includes(field.value);
|
|
90
|
+
this.render();
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
private render() {
|
|
94
|
+
const field = this.field;
|
|
95
|
+
const hint = this.querySelector('small');
|
|
96
|
+
if (!field || !hint) return;
|
|
97
|
+
const text = this.invalid ? this.dataset.invalidMessage : this.busy ? this.dataset.loadingText : this.dataset.hint;
|
|
98
|
+
hint.textContent = text ?? '';
|
|
99
|
+
hint.hidden = !text;
|
|
100
|
+
this.dataset.error = String(this.invalid);
|
|
101
|
+
if (this.invalid) field.setAttribute('aria-invalid', 'true'); else field.removeAttribute('aria-invalid');
|
|
102
|
+
if (this.busy) field.setAttribute('aria-busy', 'true'); else field.removeAttribute('aria-busy');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!customElements.get('directo-autocomplete')) customElements.define('directo-autocomplete', DirectoAutocompleteElement);
|
|
106
|
+
</script>
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
---
|
|
2
|
+
type Row = Record<string, string | number | undefined>;
|
|
3
|
+
type Direction = 'ascending' | 'descending';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
caption: string;
|
|
7
|
+
columns: readonly { key: string; label: string; sortable?: boolean; align?: 'start' | 'center' | 'end' }[];
|
|
8
|
+
rows: readonly Row[];
|
|
9
|
+
loading?: boolean;
|
|
10
|
+
loadingMessage?: string;
|
|
11
|
+
emptyMessage?: string;
|
|
12
|
+
actionLabel?: string;
|
|
13
|
+
actionHref?: (row: Row) => string;
|
|
14
|
+
/** Column key whose value identifies each row (sent in ds-row-click). Defaults to "id". */
|
|
15
|
+
rowKey?: string;
|
|
16
|
+
/** Sorted on the server before rendering and shown in the header. */
|
|
17
|
+
defaultSort?: { key: string; direction: Direction };
|
|
18
|
+
/** Do not reorder rows in the browser: listen to ds-sort and sort on the server. */
|
|
19
|
+
manualSort?: boolean;
|
|
20
|
+
/** Rows respond to click, Enter and Space and emit ds-row-click. */
|
|
21
|
+
clickableRows?: boolean;
|
|
22
|
+
density?: 'comfortable' | 'compact';
|
|
23
|
+
stickyHeader?: boolean;
|
|
24
|
+
id?: string;
|
|
25
|
+
class?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
caption, columns, rows, loading = false, loadingMessage = 'Cargando registros…', emptyMessage = 'No hay resultados.',
|
|
30
|
+
actionLabel = 'Ver detalle', actionHref, rowKey = 'id', defaultSort, manualSort = false, clickableRows = false,
|
|
31
|
+
density = 'comfortable', stickyHeader = false, id, class: className,
|
|
32
|
+
} = Astro.props;
|
|
33
|
+
const collator = new Intl.Collator('es', { numeric: true, sensitivity: 'base' });
|
|
34
|
+
const sortColumn = defaultSort && columns.find(column => column.key === defaultSort.key && column.sortable);
|
|
35
|
+
const shownRows = sortColumn && !manualSort ? [...rows].sort((left, right) => {
|
|
36
|
+
const a = left[sortColumn.key];
|
|
37
|
+
const b = right[sortColumn.key];
|
|
38
|
+
const order = typeof a === 'number' && typeof b === 'number' ? a - b : collator.compare(String(a ?? ''), String(b ?? ''));
|
|
39
|
+
return defaultSort!.direction === 'ascending' ? order : -order;
|
|
40
|
+
}) : rows;
|
|
41
|
+
const directionOf = (key: string) => sortColumn?.key === key ? defaultSort!.direction : undefined;
|
|
42
|
+
const arrow = (key: string) => directionOf(key) === 'ascending' ? '↑' : directionOf(key) === 'descending' ? '↓' : '↕';
|
|
43
|
+
---
|
|
44
|
+
<directo-sortable-table id={id} class:list={['ds-data-table', className]} data-density={density}
|
|
45
|
+
data-sticky-header={stickyHeader ? '' : undefined} data-manual-sort={manualSort ? '' : undefined}>
|
|
46
|
+
<div class="ds-data-scroll" role="region" aria-label={caption} tabindex="0">
|
|
47
|
+
<table aria-busy={loading}>
|
|
48
|
+
<caption>{caption}</caption>
|
|
49
|
+
<thead><tr>
|
|
50
|
+
{columns.map((column, index) => <th scope="col" data-align={column.align} aria-sort={column.sortable ? directionOf(column.key) : undefined}>
|
|
51
|
+
{column.sortable ? <button type="button" class="ds-sort-button" data-column={index} data-key={column.key}>{column.label} <span aria-hidden="true">{arrow(column.key)}</span></button> : column.label}
|
|
52
|
+
</th>)}
|
|
53
|
+
{actionHref && <th scope="col">Acciones</th>}
|
|
54
|
+
</tr></thead>
|
|
55
|
+
<tbody>
|
|
56
|
+
{loading || !rows.length
|
|
57
|
+
? <tr><td colspan={Math.max(1, columns.length + Number(!!actionHref))}><span role="status">{loading ? loadingMessage : emptyMessage}</span></td></tr>
|
|
58
|
+
: shownRows.map((row, index) => <tr data-row data-index={index} data-key={row[rowKey] === undefined ? undefined : String(row[rowKey])}
|
|
59
|
+
tabindex={clickableRows ? 0 : undefined} data-clickable={clickableRows ? '' : undefined}>
|
|
60
|
+
{columns.map(column => <td data-align={column.align} data-number={typeof row[column.key] === 'number' ? '' : undefined}>{row[column.key]}</td>)}
|
|
61
|
+
{actionHref && <td><a href={actionHref(row)}>{actionLabel}</a></td>}
|
|
62
|
+
</tr>)}
|
|
63
|
+
</tbody>
|
|
64
|
+
</table>
|
|
65
|
+
</div>
|
|
66
|
+
<slot name="pagination" />
|
|
67
|
+
</directo-sortable-table>
|
|
68
|
+
|
|
69
|
+
<script>
|
|
70
|
+
const collator = new Intl.Collator('es', { numeric: true, sensitivity: 'base' });
|
|
71
|
+
const interactive = 'a, button, input, select, textarea, label, [role="button"], [contenteditable="true"]';
|
|
72
|
+
|
|
73
|
+
class DirectoSortableTable extends HTMLElement {
|
|
74
|
+
connectedCallback() {
|
|
75
|
+
this.addEventListener('click', this.handleClick);
|
|
76
|
+
this.addEventListener('keydown', this.handleKey);
|
|
77
|
+
}
|
|
78
|
+
disconnectedCallback() {
|
|
79
|
+
this.removeEventListener('click', this.handleClick);
|
|
80
|
+
this.removeEventListener('keydown', this.handleKey);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private handleClick = (event: MouseEvent) => {
|
|
84
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
85
|
+
const trigger = target?.closest<HTMLButtonElement>('button[data-column]');
|
|
86
|
+
if (trigger) { this.sort(trigger); return; }
|
|
87
|
+
const row = target?.closest<HTMLTableRowElement>('tr[data-clickable]');
|
|
88
|
+
if (!row || !this.contains(row)) return;
|
|
89
|
+
const inner = target?.closest(interactive);
|
|
90
|
+
if (inner && row.contains(inner)) return;
|
|
91
|
+
this.emitRow(row);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
private handleKey = (event: KeyboardEvent) => {
|
|
95
|
+
const row = event.target instanceof HTMLTableRowElement && event.target.hasAttribute('data-clickable') ? event.target : null;
|
|
96
|
+
if (!row || (event.key !== 'Enter' && event.key !== ' ')) return;
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
this.emitRow(row);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
private emitRow(row: HTMLTableRowElement) {
|
|
102
|
+
this.dispatchEvent(new CustomEvent('ds-row-click', {
|
|
103
|
+
bubbles: true, detail: { key: row.dataset.key, index: Number(row.dataset.index), row },
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private sort(trigger: HTMLButtonElement) {
|
|
108
|
+
const body = this.querySelector('tbody');
|
|
109
|
+
const header = trigger.closest('th');
|
|
110
|
+
if (!body || !header) return;
|
|
111
|
+
const column = Number(trigger.dataset.column);
|
|
112
|
+
const direction = header.getAttribute('aria-sort') === 'ascending' ? 'descending' : 'ascending';
|
|
113
|
+
for (const th of this.querySelectorAll('th[aria-sort]')) {
|
|
114
|
+
th.removeAttribute('aria-sort');
|
|
115
|
+
const icon = th.querySelector('span');
|
|
116
|
+
if (icon) icon.textContent = '↕';
|
|
117
|
+
}
|
|
118
|
+
header.setAttribute('aria-sort', direction);
|
|
119
|
+
const icon = trigger.querySelector('span');
|
|
120
|
+
if (icon) icon.textContent = direction === 'ascending' ? '↑' : '↓';
|
|
121
|
+
this.dispatchEvent(new CustomEvent('ds-sort', { bubbles: true, detail: { key: trigger.dataset.key, column, direction } }));
|
|
122
|
+
if (this.hasAttribute('data-manual-sort')) return;
|
|
123
|
+
const rows = Array.from(body.querySelectorAll<HTMLTableRowElement>('tr[data-row]'));
|
|
124
|
+
rows.sort((left, right) => {
|
|
125
|
+
const a = left.cells[column];
|
|
126
|
+
const b = right.cells[column];
|
|
127
|
+
const x = a.textContent ?? '';
|
|
128
|
+
const y = b.textContent ?? '';
|
|
129
|
+
const order = a.hasAttribute('data-number') && b.hasAttribute('data-number') ? Number(x) - Number(y) : collator.compare(x, y);
|
|
130
|
+
return direction === 'ascending' ? order : -order;
|
|
131
|
+
});
|
|
132
|
+
body.append(...rows);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (!customElements.get('directo-sortable-table')) customElements.define('directo-sortable-table', DirectoSortableTable);
|
|
136
|
+
</script>
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
---
|
|
2
|
+
import TextField from './TextField.astro';
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
id: string;
|
|
6
|
+
label?: string;
|
|
7
|
+
startLabel?: string;
|
|
8
|
+
endLabel?: string;
|
|
9
|
+
start?: string;
|
|
10
|
+
end?: string;
|
|
11
|
+
startName?: string;
|
|
12
|
+
endName?: string;
|
|
13
|
+
min?: string;
|
|
14
|
+
max?: string;
|
|
15
|
+
required?: boolean;
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
/** Your own error message (server validation). The picker also warns when the end is before the start. */
|
|
18
|
+
error?: string;
|
|
19
|
+
/** Quick ranges shown as buttons. */
|
|
20
|
+
presets?: readonly { label: string; range: { start: string; end: string } }[];
|
|
21
|
+
class?: string;
|
|
22
|
+
}
|
|
23
|
+
const {
|
|
24
|
+
id, label = 'Rango de fechas', startLabel = 'Desde', endLabel = 'Hasta', start = '', end = '', startName, endName, min, max,
|
|
25
|
+
required = false, disabled = false, error, presets = [], class: className,
|
|
26
|
+
} = Astro.props;
|
|
27
|
+
const startMax = end && (!max || end < max) ? end : max;
|
|
28
|
+
const endMin = start && (!min || start > min) ? start : min;
|
|
29
|
+
const orderMessage = 'La fecha final no puede ser anterior a la inicial.';
|
|
30
|
+
const message = error ?? (start && end && end < start ? orderMessage : '');
|
|
31
|
+
---
|
|
32
|
+
<directo-date-range data-message={orderMessage}>
|
|
33
|
+
<fieldset id={id} class:list={['ds-date-range', className]} data-min={min} data-max={max} disabled={disabled}
|
|
34
|
+
data-error={error} aria-describedby={`${id}-error`}>
|
|
35
|
+
<legend>{label}{required && <span class="ds-input-required" aria-hidden="true">*</span>}</legend>
|
|
36
|
+
{presets.length > 0 && <div class="ds-date-range-presets">
|
|
37
|
+
{presets.map(preset => <button type="button" class="ds-filter-chip" data-start={preset.range.start} data-end={preset.range.end}
|
|
38
|
+
aria-pressed={String(preset.range.start === start && preset.range.end === end)}>{preset.label}</button>)}
|
|
39
|
+
</div>}
|
|
40
|
+
<div class="ds-date-range-fields">
|
|
41
|
+
<TextField id={`${id}-start`} label={startLabel} type="date" name={startName} value={start} min={min} max={startMax}
|
|
42
|
+
required={required} error={!!message} />
|
|
43
|
+
<TextField id={`${id}-end`} label={endLabel} type="date" name={endName} value={end} min={endMin} max={max}
|
|
44
|
+
required={required} error={!!message} />
|
|
45
|
+
</div>
|
|
46
|
+
<p id={`${id}-error`} class="ds-date-range-error" role="status">{message}</p>
|
|
47
|
+
</fieldset>
|
|
48
|
+
</directo-date-range>
|
|
49
|
+
|
|
50
|
+
<script>
|
|
51
|
+
class DirectoDateRange extends HTMLElement {
|
|
52
|
+
connectedCallback() {
|
|
53
|
+
this.addEventListener('input', this.sync);
|
|
54
|
+
this.addEventListener('click', this.preset);
|
|
55
|
+
}
|
|
56
|
+
disconnectedCallback() {
|
|
57
|
+
this.removeEventListener('input', this.sync);
|
|
58
|
+
this.removeEventListener('click', this.preset);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Current range (YYYY-MM-DD). */
|
|
62
|
+
get dsValue() {
|
|
63
|
+
const [start, end] = this.querySelectorAll<HTMLInputElement>('input[type="date"]');
|
|
64
|
+
return { start: start?.value ?? '', end: end?.value ?? '' };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Sets both dates and emits ds-change (and ds-complete when valid). */
|
|
68
|
+
dsSetValue(range: { start: string; end: string }) {
|
|
69
|
+
const [start, end] = this.querySelectorAll<HTMLInputElement>('input[type="date"]');
|
|
70
|
+
if (!start || !end) return;
|
|
71
|
+
start.value = range.start;
|
|
72
|
+
end.value = range.end;
|
|
73
|
+
this.sync();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private preset = (event: MouseEvent) => {
|
|
77
|
+
const button = event.target instanceof Element ? event.target.closest<HTMLButtonElement>('button[data-start]') : null;
|
|
78
|
+
if (button) this.dsSetValue({ start: button.dataset.start ?? '', end: button.dataset.end ?? '' });
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
private sync = () => {
|
|
82
|
+
const fields = this.querySelector('fieldset');
|
|
83
|
+
const [start, end] = this.querySelectorAll<HTMLInputElement>('input[type="date"]');
|
|
84
|
+
if (!fields || !start || !end) return;
|
|
85
|
+
const { min = '', max = '' } = fields.dataset;
|
|
86
|
+
start.max = end.value && (!max || end.value < max) ? end.value : max;
|
|
87
|
+
end.min = start.value && (!min || start.value > min) ? start.value : min;
|
|
88
|
+
const reversed = !!start.value && !!end.value && end.value < start.value;
|
|
89
|
+
const message = reversed ? this.dataset.message ?? '' : fields.dataset.error ?? '';
|
|
90
|
+
const status = this.querySelector('.ds-date-range-error');
|
|
91
|
+
if (status) status.textContent = message;
|
|
92
|
+
for (const input of [start, end]) {
|
|
93
|
+
input.closest('.ds-input')?.setAttribute('data-error', String(!!message));
|
|
94
|
+
if (message) input.setAttribute('aria-invalid', 'true'); else input.removeAttribute('aria-invalid');
|
|
95
|
+
}
|
|
96
|
+
for (const button of this.querySelectorAll<HTMLButtonElement>('button[data-start]')) {
|
|
97
|
+
button.setAttribute('aria-pressed', String(button.dataset.start === start.value && button.dataset.end === end.value));
|
|
98
|
+
}
|
|
99
|
+
const value = { start: start.value, end: end.value };
|
|
100
|
+
this.dispatchEvent(new CustomEvent('ds-change', { bubbles: true, detail: value }));
|
|
101
|
+
if (value.start && value.end && !reversed) this.dispatchEvent(new CustomEvent('ds-complete', { bubbles: true, detail: value }));
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (!customElements.get('directo-date-range')) customElements.define('directo-date-range', DirectoDateRange);
|
|
105
|
+
</script>
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
id: string;
|
|
4
|
+
title: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
side?: 'start' | 'end';
|
|
7
|
+
/** Width: sm 360px, md 440px, lg 640px. */
|
|
8
|
+
size?: 'sm' | 'md' | 'lg';
|
|
9
|
+
/** Opens when the page loads. */
|
|
10
|
+
open?: boolean;
|
|
11
|
+
closeOnBackdrop?: boolean;
|
|
12
|
+
closeOnEscape?: boolean;
|
|
13
|
+
closeLabel?: string;
|
|
14
|
+
class?: string;
|
|
15
|
+
}
|
|
16
|
+
const {
|
|
17
|
+
id, title, description, side = 'end', size = 'md', open = false, closeOnBackdrop = true, closeOnEscape = true,
|
|
18
|
+
closeLabel = 'Cerrar panel', class: className,
|
|
19
|
+
} = Astro.props;
|
|
20
|
+
---
|
|
21
|
+
<dialog id={id} class:list={['ds-drawer', className]} data-side={side} data-size={size} data-ds-modal
|
|
22
|
+
data-ds-open-initially={open ? '' : undefined} data-ds-backdrop={closeOnBackdrop ? undefined : 'static'}
|
|
23
|
+
data-ds-escape={closeOnEscape ? undefined : 'false'}
|
|
24
|
+
aria-labelledby={`${id}-title`} aria-describedby={description ? `${id}-description` : undefined}>
|
|
25
|
+
<header class="ds-drawer-header">
|
|
26
|
+
<div>
|
|
27
|
+
<h2 id={`${id}-title`}>{title}</h2>
|
|
28
|
+
{description && <p class="ds-drawer-description" id={`${id}-description`}>{description}</p>}
|
|
29
|
+
</div>
|
|
30
|
+
<button type="button" class="ds-sheet-close" aria-label={closeLabel} data-ds-close>×</button>
|
|
31
|
+
</header>
|
|
32
|
+
<div class="ds-drawer-body"><slot /></div>
|
|
33
|
+
{Astro.slots.has('footer') && <footer class="ds-drawer-footer"><slot name="footer" /></footer>}
|
|
34
|
+
</dialog>
|
|
35
|
+
|
|
36
|
+
<script>
|
|
37
|
+
import { initModals } from '../modal.js';
|
|
38
|
+
initModals();
|
|
39
|
+
document.addEventListener('astro:page-load', () => initModals());
|
|
40
|
+
</script>
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
name?: string;
|
|
6
|
+
accept?: string;
|
|
7
|
+
multiple?: boolean;
|
|
8
|
+
/** Maximum size of each file, in bytes. */
|
|
9
|
+
maxBytes?: number;
|
|
10
|
+
/** Maximum number of files (with multiple). */
|
|
11
|
+
maxFiles?: number;
|
|
12
|
+
required?: boolean;
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
supportingText?: string;
|
|
15
|
+
/** Allows dropping files onto the component. */
|
|
16
|
+
dropzone?: boolean;
|
|
17
|
+
class?: string;
|
|
18
|
+
}
|
|
19
|
+
const {
|
|
20
|
+
id, label, name, accept, multiple = false, maxBytes, maxFiles, required = false, disabled = false, supportingText,
|
|
21
|
+
dropzone = true, class: className,
|
|
22
|
+
} = Astro.props;
|
|
23
|
+
const describedBy = [supportingText ? `${id}-hint` : '', `${id}-error`].filter(Boolean).join(' ');
|
|
24
|
+
---
|
|
25
|
+
<directo-file-picker class:list={['ds-file-upload', className]} data-max-bytes={maxBytes} data-max-files={maxFiles}
|
|
26
|
+
data-dropzone={dropzone ? '' : undefined} data-disabled={disabled ? '' : undefined} data-required={required ? '' : undefined}>
|
|
27
|
+
<label for={id}>{label}{required && <span class="ds-input-required" aria-hidden="true">*</span>}</label>
|
|
28
|
+
{supportingText && <small id={`${id}-hint`}>{supportingText}</small>}
|
|
29
|
+
<input id={id} type="file" name={name} accept={accept} multiple={multiple} required={required} disabled={disabled}
|
|
30
|
+
aria-describedby={describedBy} />
|
|
31
|
+
<div id={`${id}-error`} role="status"></div>
|
|
32
|
+
<ul hidden></ul>
|
|
33
|
+
</directo-file-picker>
|
|
34
|
+
|
|
35
|
+
<script>
|
|
36
|
+
import { formatBytes, selectFiles } from '../files.js';
|
|
37
|
+
|
|
38
|
+
class DirectoFilePicker extends HTMLElement {
|
|
39
|
+
private files: File[] = [];
|
|
40
|
+
private urls: string[] = [];
|
|
41
|
+
|
|
42
|
+
connectedCallback() {
|
|
43
|
+
this.addEventListener('change', this.select);
|
|
44
|
+
this.addEventListener('dragover', this.dragOver);
|
|
45
|
+
this.addEventListener('dragleave', this.dragLeave);
|
|
46
|
+
this.addEventListener('drop', this.drop);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
disconnectedCallback() {
|
|
50
|
+
this.removeEventListener('change', this.select);
|
|
51
|
+
this.removeEventListener('dragover', this.dragOver);
|
|
52
|
+
this.removeEventListener('dragleave', this.dragLeave);
|
|
53
|
+
this.removeEventListener('drop', this.drop);
|
|
54
|
+
this.releasePreviews();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Files currently kept by the component. */
|
|
58
|
+
get dsFiles(): readonly File[] { return this.files; }
|
|
59
|
+
|
|
60
|
+
private get input() { return this.querySelector<HTMLInputElement>('input[type="file"]'); }
|
|
61
|
+
private get canDrop() { return this.hasAttribute('data-dropzone') && !this.input?.disabled; }
|
|
62
|
+
|
|
63
|
+
private dragOver = (event: DragEvent) => {
|
|
64
|
+
if (!this.canDrop) return;
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
this.setAttribute('data-dragging', '');
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
private dragLeave = (event: DragEvent) => {
|
|
70
|
+
if (!this.contains(event.relatedTarget as Node | null)) this.removeAttribute('data-dragging');
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
private drop = (event: DragEvent) => {
|
|
74
|
+
if (!this.canDrop) return;
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
this.removeAttribute('data-dragging');
|
|
77
|
+
this.add(Array.from(event.dataTransfer?.files ?? []));
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
private select = (event: Event) => {
|
|
81
|
+
if (event.target !== this.input) return;
|
|
82
|
+
this.add(Array.from(this.input?.files ?? []));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
private add(incoming: File[]) {
|
|
86
|
+
const input = this.input;
|
|
87
|
+
if (!input) return;
|
|
88
|
+
const number = (value?: string) => value === undefined || value === '' ? undefined : Number(value);
|
|
89
|
+
const result = selectFiles(this.files, incoming, {
|
|
90
|
+
accept: input.accept, multiple: input.multiple,
|
|
91
|
+
maxBytes: number(this.dataset.maxBytes), maxFiles: number(this.dataset.maxFiles),
|
|
92
|
+
});
|
|
93
|
+
this.renderFiles(result.files, result.rejections.map(rejection => rejection.message).join(' '));
|
|
94
|
+
if (result.rejections.length) {
|
|
95
|
+
this.dispatchEvent(new CustomEvent('ds-reject', { bubbles: true, detail: result.rejections }));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private releasePreviews() {
|
|
100
|
+
this.urls.forEach(url => URL.revokeObjectURL(url));
|
|
101
|
+
this.urls = [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private renderFiles(files: File[], error = '') {
|
|
105
|
+
const input = this.input;
|
|
106
|
+
const status = this.querySelector('[role="status"]');
|
|
107
|
+
const list = this.querySelector('ul');
|
|
108
|
+
if (!input || !status || !list) return;
|
|
109
|
+
const changed = files.length !== this.files.length || files.some((file, index) => file !== this.files[index]);
|
|
110
|
+
this.files = files;
|
|
111
|
+
const transfer = new DataTransfer();
|
|
112
|
+
files.forEach(file => transfer.items.add(file));
|
|
113
|
+
input.files = transfer.files;
|
|
114
|
+
input.required = this.hasAttribute('data-required') && !files.length;
|
|
115
|
+
if (error) input.setAttribute('aria-invalid', 'true'); else input.removeAttribute('aria-invalid');
|
|
116
|
+
this.releasePreviews();
|
|
117
|
+
status.textContent = error;
|
|
118
|
+
list.hidden = !files.length;
|
|
119
|
+
list.replaceChildren(...files.map((file, index) => {
|
|
120
|
+
const item = document.createElement('li');
|
|
121
|
+
if (file.type.startsWith('image/')) {
|
|
122
|
+
const image = document.createElement('img');
|
|
123
|
+
image.src = URL.createObjectURL(file);
|
|
124
|
+
image.alt = '';
|
|
125
|
+
this.urls.push(image.src);
|
|
126
|
+
item.append(image);
|
|
127
|
+
}
|
|
128
|
+
const name = document.createElement('span');
|
|
129
|
+
name.textContent = file.name;
|
|
130
|
+
const size = document.createElement('small');
|
|
131
|
+
size.textContent = formatBytes(file.size);
|
|
132
|
+
const button = document.createElement('button');
|
|
133
|
+
button.type = 'button';
|
|
134
|
+
button.className = 'ds-button';
|
|
135
|
+
button.dataset.variant = 'text';
|
|
136
|
+
button.dataset.size = 'sm';
|
|
137
|
+
button.disabled = input.disabled;
|
|
138
|
+
button.textContent = 'Quitar';
|
|
139
|
+
button.setAttribute('aria-label', 'Quitar ' + file.name);
|
|
140
|
+
button.addEventListener('click', () => {
|
|
141
|
+
this.renderFiles(files.filter((_, position) => position !== index));
|
|
142
|
+
this.dispatchEvent(new CustomEvent('ds-remove', { bubbles: true, detail: file }));
|
|
143
|
+
input.focus();
|
|
144
|
+
});
|
|
145
|
+
item.append(name, size, button);
|
|
146
|
+
return item;
|
|
147
|
+
}));
|
|
148
|
+
if (changed) this.dispatchEvent(new CustomEvent('ds-change', { bubbles: true, detail: files }));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (!customElements.get('directo-file-picker')) customElements.define('directo-file-picker', DirectoFilePicker);
|
|
152
|
+
</script>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface ActiveFilter {
|
|
3
|
+
label: string;
|
|
4
|
+
/** Link that removes the filter (server-rendered pages). */
|
|
5
|
+
removeHref?: string;
|
|
6
|
+
/** Without removeHref the chip is a button that emits ds-filter-remove with this id. */
|
|
7
|
+
id?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
label?: string;
|
|
12
|
+
activeFilters?: readonly ActiveFilter[];
|
|
13
|
+
/** Link that removes every filter. */
|
|
14
|
+
clearHref?: string;
|
|
15
|
+
/** Without clearHref: shows a button that emits ds-filters-clear. */
|
|
16
|
+
clearable?: boolean;
|
|
17
|
+
clearLabel?: string;
|
|
18
|
+
/** Number of results, announced to screen readers. */
|
|
19
|
+
resultCount?: number;
|
|
20
|
+
resultSingular?: string;
|
|
21
|
+
resultPlural?: string;
|
|
22
|
+
/** Renders a GET search form that sends the named fields to this URL (use "" for the same page). */
|
|
23
|
+
action?: string;
|
|
24
|
+
id?: string;
|
|
25
|
+
class?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
label = 'Filtros', activeFilters = [], clearHref, clearable = false, clearLabel = 'Limpiar filtros',
|
|
30
|
+
resultCount, resultSingular = 'resultado', resultPlural = 'resultados', action, id, class: className,
|
|
31
|
+
} = Astro.props;
|
|
32
|
+
const Tag = action === undefined ? 'div' : 'form';
|
|
33
|
+
const formProps = action === undefined ? { role: 'group' } : { role: 'search', method: 'get', action };
|
|
34
|
+
---
|
|
35
|
+
<directo-filter-bar style="display: contents">
|
|
36
|
+
<Tag id={id} class:list={['ds-filter-bar', className]} aria-label={label} {...formProps}>
|
|
37
|
+
<div class="ds-filter-fields"><slot /></div>
|
|
38
|
+
{Astro.slots.has('actions') && <div class="ds-filter-actions"><slot name="actions" /></div>}
|
|
39
|
+
{(activeFilters.length > 0 || resultCount !== undefined) && <div class="ds-filter-active">
|
|
40
|
+
{resultCount !== undefined && <span class="ds-filter-count" role="status">{resultCount} {resultCount === 1 ? resultSingular : resultPlural}</span>}
|
|
41
|
+
{activeFilters.map(filter => filter.removeHref !== undefined
|
|
42
|
+
? <a class="ds-filter-chip" href={filter.removeHref} aria-label={`Quitar filtro: ${filter.label}`}>{filter.label} <span aria-hidden="true">×</span></a>
|
|
43
|
+
: <button class="ds-filter-chip" type="button" data-filter-id={filter.id} aria-label={`Quitar filtro: ${filter.label}`}>{filter.label} <span aria-hidden="true">×</span></button>)}
|
|
44
|
+
{activeFilters.length > 0 && clearHref && <a href={clearHref} class="ds-button" data-variant="text" data-size="sm">{clearLabel}</a>}
|
|
45
|
+
{activeFilters.length > 0 && !clearHref && clearable && <button type="button" class="ds-button" data-variant="text" data-size="sm" data-filters-clear>{clearLabel}</button>}
|
|
46
|
+
</div>}
|
|
47
|
+
</Tag>
|
|
48
|
+
</directo-filter-bar>
|
|
49
|
+
|
|
50
|
+
<script>
|
|
51
|
+
class DirectoFilterBarElement extends HTMLElement {
|
|
52
|
+
connectedCallback() { this.addEventListener('click', this.handleClick); }
|
|
53
|
+
disconnectedCallback() { this.removeEventListener('click', this.handleClick); }
|
|
54
|
+
|
|
55
|
+
private handleClick = (event: MouseEvent) => {
|
|
56
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
57
|
+
const chip = target?.closest<HTMLButtonElement>('button[data-filter-id]');
|
|
58
|
+
if (chip) {
|
|
59
|
+
this.dispatchEvent(new CustomEvent('ds-filter-remove', { bubbles: true, detail: chip.dataset.filterId }));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (target?.closest('button[data-filters-clear]')) this.dispatchEvent(new CustomEvent('ds-filters-clear', { bubbles: true }));
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (!customElements.get('directo-filter-bar')) customElements.define('directo-filter-bar', DirectoFilterBarElement);
|
|
66
|
+
</script>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface SidebarLink {
|
|
3
|
+
label: string;
|
|
4
|
+
href: string;
|
|
5
|
+
/** Small counter or text at the end. */
|
|
6
|
+
badge?: string | number;
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
groups: readonly {
|
|
12
|
+
label: string;
|
|
13
|
+
items: readonly (SidebarLink & { children?: readonly SidebarLink[] })[];
|
|
14
|
+
/** The group title opens and closes the group. */
|
|
15
|
+
collapsible?: boolean;
|
|
16
|
+
/** Initial state of a collapsible group (open by default). */
|
|
17
|
+
defaultOpen?: boolean;
|
|
18
|
+
}[];
|
|
19
|
+
activeHref?: string;
|
|
20
|
+
label?: string;
|
|
21
|
+
id?: string;
|
|
22
|
+
class?: string;
|
|
23
|
+
}
|
|
24
|
+
const { groups, activeHref, label = 'Navegación', id, class: className } = Astro.props;
|
|
25
|
+
const hasBadge = (item: SidebarLink) => item.badge !== undefined && item.badge !== '';
|
|
26
|
+
---
|
|
27
|
+
<directo-sidebar style="display: contents">
|
|
28
|
+
<nav id={id} class:list={['ds-sidebar', className]} aria-label={label}>
|
|
29
|
+
{Astro.slots.has('header') && <div class="ds-sidebar-header"><slot name="header" /></div>}
|
|
30
|
+
{groups.map(group => {
|
|
31
|
+
const list = <ul>{group.items.map(item => <li>
|
|
32
|
+
{item.disabled
|
|
33
|
+
? <span class="ds-sidebar-link" aria-disabled="true"><span class="ds-sidebar-text">{item.label}</span>{hasBadge(item) && <span class="ds-sidebar-badge">{item.badge}</span>}</span>
|
|
34
|
+
: <a class="ds-sidebar-link" href={item.href} aria-current={activeHref === item.href ? 'page' : undefined}><span class="ds-sidebar-text">{item.label}</span>{hasBadge(item) && <span class="ds-sidebar-badge">{item.badge}</span>}</a>}
|
|
35
|
+
{!!item.children?.length && <ul>{item.children.map(child => <li>
|
|
36
|
+
{child.disabled
|
|
37
|
+
? <span class="ds-sidebar-link" aria-disabled="true"><span class="ds-sidebar-text">{child.label}</span>{hasBadge(child) && <span class="ds-sidebar-badge">{child.badge}</span>}</span>
|
|
38
|
+
: <a class="ds-sidebar-link" href={child.href} aria-current={activeHref === child.href ? 'page' : undefined}><span class="ds-sidebar-text">{child.label}</span>{hasBadge(child) && <span class="ds-sidebar-badge">{child.badge}</span>}</a>}
|
|
39
|
+
</li>)}</ul>}
|
|
40
|
+
</li>)}</ul>;
|
|
41
|
+
return group.collapsible
|
|
42
|
+
? <details class="ds-sidebar-group" open={group.defaultOpen ?? true}><summary>{group.label}</summary>{list}</details>
|
|
43
|
+
: <section class="ds-sidebar-group" aria-label={group.label}><p>{group.label}</p>{list}</section>;
|
|
44
|
+
})}
|
|
45
|
+
{Astro.slots.has('footer') && <div class="ds-sidebar-footer"><slot name="footer" /></div>}
|
|
46
|
+
</nav>
|
|
47
|
+
</directo-sidebar>
|
|
48
|
+
|
|
49
|
+
<script>
|
|
50
|
+
class DirectoSidebarElement extends HTMLElement {
|
|
51
|
+
connectedCallback() { this.addEventListener('click', this.navigate); }
|
|
52
|
+
disconnectedCallback() { this.removeEventListener('click', this.navigate); }
|
|
53
|
+
|
|
54
|
+
/** Emits ds-navigate (cancelable). Call event.preventDefault() on it to route without reloading the page. */
|
|
55
|
+
private navigate = (event: MouseEvent) => {
|
|
56
|
+
const link = event.target instanceof Element ? event.target.closest<HTMLAnchorElement>('a.ds-sidebar-link') : null;
|
|
57
|
+
if (!link) return;
|
|
58
|
+
const detail = { href: link.getAttribute('href') ?? '', label: link.querySelector('.ds-sidebar-text')?.textContent ?? '' };
|
|
59
|
+
const allowed = this.dispatchEvent(new CustomEvent('ds-navigate', { bubbles: true, cancelable: true, detail }));
|
|
60
|
+
if (!allowed) event.preventDefault();
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (!customElements.get('directo-sidebar')) customElements.define('directo-sidebar', DirectoSidebarElement);
|
|
64
|
+
</script>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
items: readonly {
|
|
4
|
+
id: string; title: string; dateTime?: string; dateLabel?: string; actor?: string; description?: string;
|
|
5
|
+
/** Color of the dot. */
|
|
6
|
+
variant?: 'default' | 'info' | 'success' | 'warning' | 'error';
|
|
7
|
+
/** Marks the step that is happening now (aria-current="step"). */
|
|
8
|
+
current?: boolean;
|
|
9
|
+
}[];
|
|
10
|
+
label?: string;
|
|
11
|
+
emptyMessage?: string;
|
|
12
|
+
density?: 'comfortable' | 'compact';
|
|
13
|
+
id?: string;
|
|
14
|
+
class?: string;
|
|
15
|
+
}
|
|
16
|
+
const { items, label = 'Historial', emptyMessage = 'Sin eventos registrados.', density = 'comfortable', id, class: className } = Astro.props;
|
|
17
|
+
---
|
|
18
|
+
{items.length ? <ol id={id} class:list={['ds-timeline', className]} aria-label={label} data-density={density}>
|
|
19
|
+
{items.map(item => <li data-variant={item.variant && item.variant !== 'default' ? item.variant : undefined} aria-current={item.current ? 'step' : undefined}>
|
|
20
|
+
<strong>{item.title}</strong>
|
|
21
|
+
{item.dateTime && <time datetime={item.dateTime}>{item.dateLabel ?? item.dateTime}</time>}
|
|
22
|
+
{item.actor && <span>{item.actor}</span>}
|
|
23
|
+
{item.description && <div>{item.description}</div>}
|
|
24
|
+
</li>)}
|
|
25
|
+
</ol> : <p id={id} class:list={['ds-timeline', className]} data-empty="true">{emptyMessage}</p>}
|
package/src/files.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type FileRejectionReason = 'type' | 'size' | 'count';
|
|
2
|
+
export interface FileRejection {
|
|
3
|
+
file: File;
|
|
4
|
+
reason: FileRejectionReason;
|
|
5
|
+
message: string;
|
|
6
|
+
}
|
|
7
|
+
export interface FileRules {
|
|
8
|
+
/** Same syntax as the input accept attribute: ".pdf", "image/*", "application/pdf". */
|
|
9
|
+
accept?: string;
|
|
10
|
+
/** Maximum size of each file, in bytes. */
|
|
11
|
+
maxBytes?: number;
|
|
12
|
+
/** Maximum number of files kept at once (only with multiple). */
|
|
13
|
+
maxFiles?: number;
|
|
14
|
+
multiple?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** 1536 → "1.5 KB". */
|
|
17
|
+
export declare function formatBytes(bytes: number): string;
|
|
18
|
+
/** True when the file matches one of the accept rules (or there are none). */
|
|
19
|
+
export declare function acceptsFile(file: Pick<File, 'name' | 'type'>, accept?: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Adds `incoming` to the `current` selection: with multiple the files are appended (without duplicates),
|
|
22
|
+
* otherwise the first valid file replaces the selection. Returns the kept files and why the others were left out.
|
|
23
|
+
*/
|
|
24
|
+
export declare function selectFiles(current: readonly File[], incoming: readonly File[], rules?: FileRules): {
|
|
25
|
+
files: File[];
|
|
26
|
+
rejections: FileRejection[];
|
|
27
|
+
};
|
package/src/files.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// File selection rules shared by FileUpload in React, Angular and Astro (same file in the three packages).
|
|
2
|
+
/** 1536 → "1.5 KB". */
|
|
3
|
+
export function formatBytes(bytes) {
|
|
4
|
+
if (!Number.isFinite(bytes) || bytes < 1024)
|
|
5
|
+
return `${Math.max(0, Math.round(bytes || 0))} B`;
|
|
6
|
+
const units = ['KB', 'MB', 'GB'];
|
|
7
|
+
let value = bytes / 1024;
|
|
8
|
+
let unit = 0;
|
|
9
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
10
|
+
value /= 1024;
|
|
11
|
+
unit++;
|
|
12
|
+
}
|
|
13
|
+
return `${Number(value.toFixed(value < 10 ? 1 : 0))} ${units[unit]}`;
|
|
14
|
+
}
|
|
15
|
+
/** True when the file matches one of the accept rules (or there are none). */
|
|
16
|
+
export function acceptsFile(file, accept) {
|
|
17
|
+
const rules = accept?.toLowerCase().split(',').map(rule => rule.trim()).filter(Boolean) ?? [];
|
|
18
|
+
if (!rules.length)
|
|
19
|
+
return true;
|
|
20
|
+
const type = file.type.toLowerCase();
|
|
21
|
+
const name = file.name.toLowerCase();
|
|
22
|
+
return rules.some(rule => rule.startsWith('.') ? name.endsWith(rule)
|
|
23
|
+
: rule.endsWith('/*') ? type.startsWith(rule.slice(0, -1)) : type === rule);
|
|
24
|
+
}
|
|
25
|
+
const sameFile = (a, b) => a.name === b.name && a.size === b.size && a.lastModified === b.lastModified;
|
|
26
|
+
/**
|
|
27
|
+
* Adds `incoming` to the `current` selection: with multiple the files are appended (without duplicates),
|
|
28
|
+
* otherwise the first valid file replaces the selection. Returns the kept files and why the others were left out.
|
|
29
|
+
*/
|
|
30
|
+
export function selectFiles(current, incoming, rules = {}) {
|
|
31
|
+
const rejections = [];
|
|
32
|
+
const files = rules.multiple ? [...current] : [];
|
|
33
|
+
const limit = rules.multiple ? (rules.maxFiles && rules.maxFiles > 0 ? Math.floor(rules.maxFiles) : Infinity) : 1;
|
|
34
|
+
for (const file of incoming) {
|
|
35
|
+
if (!acceptsFile(file, rules.accept)) {
|
|
36
|
+
rejections.push({ file, reason: 'type', message: `${file.name}: formato no admitido.` });
|
|
37
|
+
}
|
|
38
|
+
else if (rules.maxBytes !== undefined && file.size > rules.maxBytes) {
|
|
39
|
+
rejections.push({ file, reason: 'size', message: `${file.name}: supera el tamaño máximo (${formatBytes(rules.maxBytes)}).` });
|
|
40
|
+
}
|
|
41
|
+
else if (files.some(kept => sameFile(kept, file))) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
else if (files.length >= limit) {
|
|
45
|
+
rejections.push({ file, reason: 'count', message: `${file.name}: solo se permiten ${limit} archivo${limit === 1 ? '' : 's'}.` });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
files.push(file);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { files, rejections };
|
|
52
|
+
}
|
package/src/modal.d.ts
CHANGED
|
@@ -1,3 +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;
|
|
1
|
+
export declare function initModals(root?: ParentNode | Event): void;
|
|
2
|
+
export declare function openModal(id: string, opener?: HTMLElement): void;
|
|
3
|
+
export declare function closeModal(id: string): void;
|
package/src/modal.js
CHANGED
|
@@ -1,39 +1,50 @@
|
|
|
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
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
+
* - data-ds-backdrop="static" keeps it open on backdrop clicks; data-ds-escape="false" ignores Escape.
|
|
7
|
+
* - the dialog emits ds-open and ds-close (bubbling CustomEvents) besides the native close event.
|
|
8
|
+
* @param {ParentNode | Event} [root=document]
|
|
9
|
+
*/
|
|
10
|
+
export function initModals(root = document) {
|
|
11
|
+
// Also works as an event listener (astro:page-load passes an Event).
|
|
12
|
+
const scope = root && typeof root.querySelectorAll === 'function' ? root : document;
|
|
13
|
+
scope.querySelectorAll('[data-ds-open]:not([data-ds-ready])').forEach(trigger => {
|
|
14
|
+
trigger.dataset.dsReady = '';
|
|
15
|
+
trigger.setAttribute('aria-haspopup', 'dialog');
|
|
16
|
+
trigger.addEventListener('click', () => openModal(trigger.dataset.dsOpen ?? '', trigger));
|
|
17
|
+
});
|
|
18
|
+
scope.querySelectorAll('dialog[data-ds-modal]:not([data-ds-ready])').forEach(dialog => {
|
|
19
|
+
dialog.dataset.dsReady = '';
|
|
20
|
+
dialog.addEventListener('click', event => {
|
|
21
|
+
const target = event.target;
|
|
22
|
+
const backdrop = target === dialog && dialog.dataset.dsBackdrop !== 'static';
|
|
23
|
+
if (backdrop || (target instanceof Element && target.closest('[data-ds-close]'))) dialog.close();
|
|
24
|
+
});
|
|
25
|
+
dialog.addEventListener('cancel', event => { if (dialog.dataset.dsEscape === 'false') event.preventDefault(); });
|
|
26
|
+
dialog.addEventListener('close', () => dialog.dispatchEvent(new CustomEvent('ds-close', { bubbles: true })));
|
|
27
|
+
if (dialog.hasAttribute('data-ds-open-initially') && !dialog.open) {
|
|
28
|
+
dialog.showModal();
|
|
29
|
+
dialog.dispatchEvent(new CustomEvent('ds-open', { bubbles: true, detail: { opener: undefined } }));
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const openers = new WeakMap();
|
|
35
|
+
|
|
36
|
+
/** @param {string} id @param {HTMLElement} [opener] */
|
|
37
|
+
export function openModal(id, opener) {
|
|
38
|
+
const dialog = document.getElementById(id);
|
|
39
|
+
if (!(dialog instanceof HTMLDialogElement) || dialog.open) return;
|
|
40
|
+
if (opener) openers.set(dialog, opener);
|
|
41
|
+
dialog.showModal();
|
|
42
|
+
dialog.dispatchEvent(new CustomEvent('ds-open', { bubbles: true, detail: { opener } }));
|
|
43
|
+
dialog.addEventListener('close', () => openers.get(dialog)?.focus({ preventScroll: true }), { once: true });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @param {string} id */
|
|
47
|
+
export function closeModal(id) {
|
|
48
|
+
const dialog = document.getElementById(id);
|
|
49
|
+
if (dialog instanceof HTMLDialogElement) dialog.close();
|
|
50
|
+
}
|