@jungherz-de/notionkit-elements 1.8.0 → 1.9.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/README.md +12 -12
- package/dist/components/nk-calendar-view.js +138 -0
- package/dist/components/nk-calendar.js +356 -0
- package/dist/components/nk-menu.js +2 -14
- package/dist/components/nk-table-view.js +9 -3
- package/dist/components/shared/dates-a32DcW1l.js +67 -0
- package/dist/components/shared/floating-B8CwbXnV.js +22 -0
- package/dist/notionkit-elements.esm.js +582 -20
- package/dist/notionkit-elements.js +583 -19
- package/dist/notionkit-elements.min.js +1 -1
- package/package.json +3 -3
- package/src/components/data/nk-calendar-view.js +136 -0
- package/src/components/data/nk-table-view.js +9 -3
- package/src/components/forms/nk-calendar.js +354 -0
- package/src/components/overlays/nk-menu.js +2 -14
- package/src/index.js +2 -0
- package/src/util/dates.js +65 -0
- package/src/util/floating.js +20 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jungherz-de/notionkit-elements",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Vanilla JavaScript Web Components wrapping the NotionKit CSS foundation – <nk-*> custom elements with Shadow DOM, automatic theme sync and native form participation.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Jungherz GmbH <info@jungherz.com> (https://www.jungherz.com)",
|
|
@@ -59,10 +59,10 @@
|
|
|
59
59
|
"dark-mode"
|
|
60
60
|
],
|
|
61
61
|
"peerDependencies": {
|
|
62
|
-
"@jungherz-de/notionkit": ">=1.
|
|
62
|
+
"@jungherz-de/notionkit": ">=1.9.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@jungherz-de/notionkit": "^1.
|
|
65
|
+
"@jungherz-de/notionkit": "^1.9.0",
|
|
66
66
|
"@playwright/test": "^1.50.0",
|
|
67
67
|
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
68
68
|
"@rollup/plugin-terser": "^0.4.0",
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { NkElement } from '../../base.js';
|
|
2
|
+
import { isoOf, parseDay, isoWeek, resolveLocale, firstWeekday, weekdayNames, monthGrid, stepMonth, weekLabel, CHEVRONS } from '../../util/dates.js';
|
|
3
|
+
|
|
4
|
+
// <nk-calendar-view name="calendar" label="📅 Calendar" date-key="due" weeks></nk-calendar-view>
|
|
5
|
+
// The fourth database view (NotionKit 1.9.0): a month, the rows as cards on
|
|
6
|
+
// their dates. `date-key` names the date column (default: the first one),
|
|
7
|
+
// `title-key` the title (default: the title column); dates are YYYY-MM-DD or
|
|
8
|
+
// D.M.YYYY, a range { start, end } stands on its start. month (YYYY-MM;
|
|
9
|
+
// default today's), weeks (the ISO week in front of each row), week-start
|
|
10
|
+
// (0 = Sunday … 6 = Saturday; default from the locale), weekend (weekdays
|
|
11
|
+
// washed like the days of other months, "6,0"), today, locale and the texts
|
|
12
|
+
// today-label, prev-label, next-label, week-label.
|
|
13
|
+
// Standalone: view.columns = […]; view.rows = […]. Inside <nk-database> the
|
|
14
|
+
// database pushes the data. A card fires nk-select { row, id, value } like a
|
|
15
|
+
// row of the table, Today and ‹ › nk-month { month }.
|
|
16
|
+
// → <div class="nk-calendar-view weeks"><div class="cv-head"><div class="cv-title">May 2026</div><button class="cal-nav">Today</button>‹ ›</div>
|
|
17
|
+
// <div class="cv-grid"><div class="cv-wd">W</div>…<div class="cv-week">21</div><div class="cv-day today"><span class="cv-num">20</span><button class="cv-item">🗃️ …</button></div>…</div></div>
|
|
18
|
+
class NkCalendarView extends NkElement {
|
|
19
|
+
static get observedAttributes() {
|
|
20
|
+
return ['name', 'label', 'badge', 'count', 'date-key', 'title-key', 'month', 'weeks', 'week-start', 'weekend', 'today', 'locale',
|
|
21
|
+
'today-label', 'prev-label', 'next-label', 'week-label'];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
render() {
|
|
25
|
+
this._box = this.createElement('div', ['nk-calendar-view']);
|
|
26
|
+
const head = this.createElement('div', ['cv-head']);
|
|
27
|
+
this._title = this.createElement('div', ['cv-title']);
|
|
28
|
+
this._todayBtn = this.createElement('button', ['cal-nav'], { type: 'button' });
|
|
29
|
+
this._prev = this.createElement('button', ['cal-nav'], { type: 'button' });
|
|
30
|
+
this._prev.innerHTML = CHEVRONS.prev;
|
|
31
|
+
this._next = this.createElement('button', ['cal-nav'], { type: 'button' });
|
|
32
|
+
this._next.innerHTML = CHEVRONS.next;
|
|
33
|
+
head.append(this._title, this._todayBtn, this._prev, this._next);
|
|
34
|
+
this._grid = this.createElement('div', ['cv-grid']);
|
|
35
|
+
this._box.append(head, this._grid);
|
|
36
|
+
this._wrapper.appendChild(this._box);
|
|
37
|
+
this._columns ??= []; this._rows ??= [];
|
|
38
|
+
this._month ??= this._attrMonth() ?? this._todayIso().slice(0, 7);
|
|
39
|
+
this._render();
|
|
40
|
+
this.closest('nk-database')?.requestSync?.();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_attrMonth() { const m = this.getAttribute('month'); return /^\d{4}-\d{2}$/.test(m || '') ? m : null; }
|
|
44
|
+
_todayIso() { return this.getAttribute('today') || isoOf(new Date()); }
|
|
45
|
+
_dateColumn() {
|
|
46
|
+
const key = this.getAttribute('date-key');
|
|
47
|
+
return this._columns.find(c => c.key === key) || this._columns.find(c => c.type === 'date') || null;
|
|
48
|
+
}
|
|
49
|
+
_titleColumn() {
|
|
50
|
+
const key = this.getAttribute('title-key');
|
|
51
|
+
return this._columns.find(c => c.key === key) || this._columns.find(c => c.title) || this._columns[0] || null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
_render() {
|
|
55
|
+
if (!this._grid) return;
|
|
56
|
+
const locale = resolveLocale(this.getAttribute('locale')), weeks = this.getBoolAttr('weeks');
|
|
57
|
+
const n = Number(this.getAttribute('week-start'));
|
|
58
|
+
const ws = this.hasAttribute('week-start') && Number.isInteger(n) && n >= 0 && n <= 6 ? n : firstWeekday(locale);
|
|
59
|
+
const g = monthGrid(this._month, ws), today = this._todayIso();
|
|
60
|
+
const weekend = new Set((this.getAttribute('weekend') || '').split(',').map(s => s.trim()).filter(Boolean).map(Number));
|
|
61
|
+
const dateCol = this._dateColumn(), titleCol = this._titleColumn(), byDay = {};
|
|
62
|
+
if (dateCol) for (const row of this._rows) {
|
|
63
|
+
const d = parseDay(row[dateCol.key]);
|
|
64
|
+
if (d) (byDay[isoOf(d)] ||= []).push(row);
|
|
65
|
+
}
|
|
66
|
+
this._box.classList.toggle('weeks', weeks);
|
|
67
|
+
this._title.textContent = new Intl.DateTimeFormat(locale, { month: 'long', year: 'numeric' }).format(g.first);
|
|
68
|
+
this._todayBtn.textContent = this.getAttribute('today-label') || 'Today';
|
|
69
|
+
this._prev.setAttribute('aria-label', this.getAttribute('prev-label') || 'Previous month');
|
|
70
|
+
this._next.setAttribute('aria-label', this.getAttribute('next-label') || 'Next month');
|
|
71
|
+
|
|
72
|
+
const cell = (cls, text) => { const el = this.createElement('div', [cls]); el.textContent = text; return el; };
|
|
73
|
+
const nodes = [];
|
|
74
|
+
if (weeks) nodes.push(cell('cv-wd', this.getAttribute('week-label') || weekLabel(locale)));
|
|
75
|
+
for (const w of weekdayNames(locale, ws)) nodes.push(cell('cv-wd', w));
|
|
76
|
+
for (let i = 0; i < g.rows * 7; i++) {
|
|
77
|
+
const d = g.day(i), iso = isoOf(d);
|
|
78
|
+
if (weeks && i % 7 === 0) nodes.push(cell('cv-week', isoWeek(d)));
|
|
79
|
+
const day = this.createElement('div', ['cv-day', d.getMonth() !== g.m - 1 && 'out', weekend.has(d.getDay()) && 'off', iso === today && 'today'].filter(Boolean));
|
|
80
|
+
const num = this.createElement('span', ['cv-num']);
|
|
81
|
+
num.textContent = d.getDate();
|
|
82
|
+
day.appendChild(num);
|
|
83
|
+
for (const row of byDay[iso] || []) {
|
|
84
|
+
const item = this.createElement('button', ['cv-item'], { type: 'button', 'data-id': row.id ?? '' });
|
|
85
|
+
const title = titleCol ? String(row[titleCol.key] ?? '') : '';
|
|
86
|
+
item.textContent = row.icon ? `${row.icon} ${title}` : title;
|
|
87
|
+
day.appendChild(item);
|
|
88
|
+
}
|
|
89
|
+
nodes.push(day);
|
|
90
|
+
}
|
|
91
|
+
this._grid.replaceChildren(...nodes);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
_goMonth(month) {
|
|
95
|
+
if (month === this._month) return;
|
|
96
|
+
this._month = month;
|
|
97
|
+
this._render();
|
|
98
|
+
this.emit('nk-month', { month });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setupEvents() {
|
|
102
|
+
this._onClick = (e) => {
|
|
103
|
+
if (e.target.closest('.cal-nav') === this._todayBtn) { this._goMonth(this._todayIso().slice(0, 7)); return; }
|
|
104
|
+
if (e.target.closest('.cal-nav') === this._prev) { this._goMonth(stepMonth(this._month, -1)); return; }
|
|
105
|
+
if (e.target.closest('.cal-nav') === this._next) { this._goMonth(stepMonth(this._month, 1)); return; }
|
|
106
|
+
const item = e.target.closest('.cv-item');
|
|
107
|
+
if (item) { const row = this._rowById(item.dataset.id); this.emit('nk-select', { row, id: row?.id, value: row?.id }); }
|
|
108
|
+
};
|
|
109
|
+
this._box.addEventListener('click', this._onClick);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
teardownEvents() { this._box?.removeEventListener('click', this._onClick); }
|
|
113
|
+
|
|
114
|
+
_rowById(id) { return this._rows.find(r => String(r.id) === String(id)); }
|
|
115
|
+
|
|
116
|
+
onAttributeChanged(name) {
|
|
117
|
+
if (name === 'month') this._month = this._attrMonth() ?? this._month;
|
|
118
|
+
this._render();
|
|
119
|
+
if (name === 'label' || name === 'badge' || name === 'count') this.closest('nk-database')?.requestSync?.();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get name() { return this.getAttribute('name') || 'calendar'; }
|
|
123
|
+
/** The month shown, YYYY-MM. */
|
|
124
|
+
get month() { return this._month; }
|
|
125
|
+
set month(v) { if (!/^\d{4}-\d{2}$/.test(v)) return; if (this._grid) this._goMonth(v); else this._month = v; }
|
|
126
|
+
get columns() { return this._columns; }
|
|
127
|
+
set columns(v) { this._columns = Array.isArray(v) ? v : []; this._render(); }
|
|
128
|
+
get rows() { return this._rows; }
|
|
129
|
+
set rows(v) { this._rows = Array.isArray(v) ? v : []; this._render(); }
|
|
130
|
+
set data({ columns, rows }) { this.setData(columns, rows); }
|
|
131
|
+
setData(columns, rows) { this._columns = columns || []; this._rows = rows || []; this._render(); }
|
|
132
|
+
refresh() { this._render(); }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
customElements.define('nk-calendar-view', NkCalendarView);
|
|
136
|
+
export { NkCalendarView };
|
|
@@ -5,8 +5,10 @@ import { renderPropertyCell, compareBy } from '../../util/property-cell.js';
|
|
|
5
5
|
// `wrap` lets cell text break (`.nk-table.wrap`), like Notion's "wrap column".
|
|
6
6
|
// Standalone: view.columns = […]; view.rows = […]; Inside <nk-database> the
|
|
7
7
|
// database pushes `data`. → <div class="nk-table-wrap"><table class="nk-table">…</table><div class="nk-new-row">+ New page</div></div>
|
|
8
|
-
// Cells are plain markup from renderPropertyCell(); rows fire nk-select
|
|
9
|
-
//
|
|
8
|
+
// Cells are plain markup from renderPropertyCell(); rows fire nk-select
|
|
9
|
+
// { row, id, key, cell } – `key` and `cell` say which cell was hit, to open
|
|
10
|
+
// an editor there, like the date picker under a due date – checkboxes
|
|
11
|
+
// nk-change, headers nk-action { action: 'sort' } (and sort locally
|
|
10
12
|
// with `sortable`), the add row nk-action { action: 'new-row' }.
|
|
11
13
|
class NkTableView extends NkElement {
|
|
12
14
|
static get observedAttributes() { return ['name', 'label', 'badge', 'count', 'new-row', 'new-row-label', 'sortable', 'sort-key', 'sort-dir', 'wrap']; }
|
|
@@ -74,7 +76,11 @@ class NkTableView extends NkElement {
|
|
|
74
76
|
}
|
|
75
77
|
if (e.target.closest('input[type=checkbox], a')) return;
|
|
76
78
|
const tr = e.target.closest('tr[data-id]');
|
|
77
|
-
if (tr)
|
|
79
|
+
if (!tr) return;
|
|
80
|
+
// Which cell: `key` names its column, `cell` is there to anchor an editor to.
|
|
81
|
+
const cell = e.target.closest('td');
|
|
82
|
+
const row = this._rowById(tr.dataset.id);
|
|
83
|
+
this.emit('nk-select', { row, id: row?.id, value: row?.id, key: cell ? this._columns[cell.cellIndex]?.key : undefined, cell });
|
|
78
84
|
};
|
|
79
85
|
this._onChange = (e) => {
|
|
80
86
|
const input = e.target;
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { NkFormElement } from '../../base.js';
|
|
2
|
+
import { isoOf, parseDay, addDays, isoWeek, resolveLocale, firstWeekday, weekdayNames, monthGrid, stepMonth, weekLabel, CHEVRONS } from '../../util/dates.js';
|
|
3
|
+
import { placeUnder } from '../../util/floating.js';
|
|
4
|
+
import { deepActiveElement } from '../../util/focus.js';
|
|
5
|
+
|
|
6
|
+
// <nk-calendar name="due" value="2026-06-02" weeks weekend="6,0"></nk-calendar>
|
|
7
|
+
// <nk-calendar range value="2026-06-08/2026-06-12"></nk-calendar>
|
|
8
|
+
// <nk-calendar floating sheet id="picker"></nk-calendar> picker.show(button)
|
|
9
|
+
// → [<div class="nk-pop floating sheet open">]<div class="nk-calendar weeks">
|
|
10
|
+
// <div class="cal-head"><div class="cal-title">June 2026</div><button class="cal-nav">Today</button>‹ ›</div>
|
|
11
|
+
// <div class="cal-grid"><span class="cal-wd">W</span>…<span class="cal-week">23</span><button class="cal-day selected">2</button>…</div>
|
|
12
|
+
// [<div class="cal-foot"><input class="nk-input" type="time"></div>]</div>
|
|
13
|
+
//
|
|
14
|
+
// Notion's date picker (NotionKit 1.9.0), after GlassKit Elements'
|
|
15
|
+
// glk-calendar, with what a planning tool needs on top: a range, ISO
|
|
16
|
+
// calendar weeks, days not worked, marks and a time.
|
|
17
|
+
// value: YYYY-MM-DD; with `range` the ISO interval "start/end"; with `time`
|
|
18
|
+
// YYYY-MM-DDTHH:MM. Also as the properties value, start, end.
|
|
19
|
+
// month (YYYY-MM shown; default the value's, else today's), min, max, today,
|
|
20
|
+
// locale (default the page's lang), week-start (0 = Sunday … 6 = Saturday;
|
|
21
|
+
// default from the locale, Monday where the browser cannot say), weeks,
|
|
22
|
+
// weekend (weekdays not worked, "6,0" for Saturday and Sunday), range, time,
|
|
23
|
+
// clearable, label and the texts today-label, prev-label, next-label,
|
|
24
|
+
// week-label, time-label, clear-label. `time` belongs to single days; a
|
|
25
|
+
// range has none.
|
|
26
|
+
// days (property, or JSON attribute): { 'YYYY-MM-DD': { off, label, marks } }
|
|
27
|
+
// – `off` greys a holiday, `label` names the day (tooltip and screen
|
|
28
|
+
// reader), `marks` puts up to three dots in the nine colours on it.
|
|
29
|
+
// Keys: arrows by a day or a week, Home/End to the ends of the week,
|
|
30
|
+
// PageUp/PageDown by a month (Shift: a year); Enter or Space picks. Arrows
|
|
31
|
+
// only move, so the picker stays quiet while someone looks around. Days
|
|
32
|
+
// outside min/max are aria-disabled – in the arrow path and announced, never
|
|
33
|
+
// picked. A range takes two picks, in either order. Form-associated: the
|
|
34
|
+
// value goes with the form, reset restores it.
|
|
35
|
+
// Events: nk-change { value, start, end, time } when a pick – for a range the
|
|
36
|
+
// second one –, a new time or Clear changes the value; nk-month { month } when the month
|
|
37
|
+
// changes; nk-toggle { open } with `floating`. `required` makes an empty
|
|
38
|
+
// value invalid.
|
|
39
|
+
// `floating` makes it a popover over the page – show(anchor) puts it under
|
|
40
|
+
// the anchor, right edges aligned (align="start": left edges) – and `sheet`
|
|
41
|
+
// a bottom sheet with 44px cells on a phone, as <nk-menu floating sheet>.
|
|
42
|
+
// A pick closes it – the second of a range; with `time` Escape or a tap
|
|
43
|
+
// outside does. Put a floating calendar directly under <body>.
|
|
44
|
+
const STEPS = { ArrowLeft: -1, ArrowRight: 1, ArrowUp: -7, ArrowDown: 7 };
|
|
45
|
+
const floatSheet = new CSSStyleSheet();
|
|
46
|
+
floatSheet.replaceSync(`:host([floating]) { display: block; position: fixed; z-index: 60; pointer-events: none; }`);
|
|
47
|
+
|
|
48
|
+
class NkCalendar extends NkFormElement {
|
|
49
|
+
static get hostStyles() { return floatSheet; }
|
|
50
|
+
static get observedAttributes() {
|
|
51
|
+
return ['value', 'month', 'min', 'max', 'today', 'locale', 'week-start', 'weeks', 'weekend', 'range', 'time', 'clearable', 'days',
|
|
52
|
+
'label', 'today-label', 'prev-label', 'next-label', 'week-label', 'time-label', 'clear-label', 'floating', 'sheet', 'open', 'align', 'required'];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
render() {
|
|
56
|
+
// A value or month set as a property before the first connect wins over the attributes.
|
|
57
|
+
this._readValue(this._pendingValue ?? this.getAttribute('value'));
|
|
58
|
+
const month = this._pendingMonth || this._attrMonth();
|
|
59
|
+
if (month) this._month = month;
|
|
60
|
+
this._initialValue = this.getAttribute('value') ?? '';
|
|
61
|
+
this._build();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ── value ──
|
|
65
|
+
_readValue(v) {
|
|
66
|
+
const s = String(v ?? '');
|
|
67
|
+
this._start = this._end = this._day = null;
|
|
68
|
+
this._time = '';
|
|
69
|
+
if (this.getBoolAttr('range')) {
|
|
70
|
+
const [a, b] = s.split('/');
|
|
71
|
+
this._start = parseDay(a) ? isoOf(parseDay(a)) : null;
|
|
72
|
+
this._end = parseDay(b) ? isoOf(parseDay(b)) : null;
|
|
73
|
+
} else if (parseDay(s)) {
|
|
74
|
+
this._day = isoOf(parseDay(s));
|
|
75
|
+
this._time = s.match(/T(\d{2}:\d{2})/)?.[1] ?? '';
|
|
76
|
+
}
|
|
77
|
+
// A new value brings its month; without one the month stays.
|
|
78
|
+
const shown = this._day || this._start;
|
|
79
|
+
this._month = shown ? shown.slice(0, 7) : (this._month || this._attrMonth() || this._todayIso().slice(0, 7));
|
|
80
|
+
this._focus = null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
_attrMonth() { const m = this.getAttribute('month'); return /^\d{4}-\d{2}$/.test(m || '') ? m : null; }
|
|
84
|
+
_todayIso() { return this.getAttribute('today') || isoOf(new Date()); }
|
|
85
|
+
|
|
86
|
+
get value() {
|
|
87
|
+
if (this.getBoolAttr('range')) return this._start ? `${this._start}${this._end ? `/${this._end}` : ''}` : '';
|
|
88
|
+
return this._day ? `${this._day}${this.getBoolAttr('time') && this._time ? `T${this._time}` : ''}` : '';
|
|
89
|
+
}
|
|
90
|
+
set value(v) {
|
|
91
|
+
this._readValue(v);
|
|
92
|
+
if (this._grid) this._draw(); else this._pendingValue = String(v ?? '');
|
|
93
|
+
}
|
|
94
|
+
get start() { return this.getBoolAttr('range') ? this._start : this._day; }
|
|
95
|
+
get end() { return this.getBoolAttr('range') ? this._end : this._day; }
|
|
96
|
+
get month() { return this._month; }
|
|
97
|
+
set month(v) {
|
|
98
|
+
if (!/^\d{4}-\d{2}$/.test(v)) return;
|
|
99
|
+
this._month = v; this._focus = null;
|
|
100
|
+
if (this._grid) this._draw(); else this._pendingMonth = v;
|
|
101
|
+
}
|
|
102
|
+
get days() { return this._days ?? {}; }
|
|
103
|
+
set days(v) { this._days = v && typeof v === 'object' ? v : {}; if (this._grid) this._draw(); }
|
|
104
|
+
|
|
105
|
+
// ── markup ──
|
|
106
|
+
_build() {
|
|
107
|
+
const floating = this.getBoolAttr('floating');
|
|
108
|
+
this._cal = this.createElement('div', ['nk-calendar'], { role: 'group' });
|
|
109
|
+
const head = this.createElement('div', ['cal-head']);
|
|
110
|
+
this._title = this.createElement('div', ['cal-title'], { 'aria-live': 'polite' });
|
|
111
|
+
this._todayBtn = this.createElement('button', ['cal-nav'], { type: 'button' });
|
|
112
|
+
this._prev = this.createElement('button', ['cal-nav'], { type: 'button' });
|
|
113
|
+
this._prev.innerHTML = CHEVRONS.prev;
|
|
114
|
+
this._next = this.createElement('button', ['cal-nav'], { type: 'button' });
|
|
115
|
+
this._next.innerHTML = CHEVRONS.next;
|
|
116
|
+
head.append(this._title, this._todayBtn, this._prev, this._next);
|
|
117
|
+
this._grid = this.createElement('div', ['cal-grid']);
|
|
118
|
+
this._foot = this.createElement('div', ['cal-foot']);
|
|
119
|
+
this._timeInput = this.createElement('input', ['nk-input'], { type: 'time' });
|
|
120
|
+
this._clear = this.createElement('button', ['cal-nav'], { type: 'button' });
|
|
121
|
+
this._cal.append(head, this._grid);
|
|
122
|
+
this._pop = floating ? this.createElement('div', ['nk-pop', 'floating']) : null;
|
|
123
|
+
if (this._pop) this._pop.appendChild(this._cal);
|
|
124
|
+
this._wrapper.replaceChildren(this._pop ?? this._cal);
|
|
125
|
+
this._draw();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get _hasTime() { return this.getBoolAttr('time') && !this.getBoolAttr('range'); }
|
|
129
|
+
get _locale() { return resolveLocale(this.getAttribute('locale')); }
|
|
130
|
+
get _weekStart() {
|
|
131
|
+
const n = Number(this.getAttribute('week-start'));
|
|
132
|
+
return this.hasAttribute('week-start') && Number.isInteger(n) && n >= 0 && n <= 6 ? n : firstWeekday(this._locale);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
_syncPop() {
|
|
136
|
+
this._pop?.classList.toggle('sheet', this.getBoolAttr('sheet'));
|
|
137
|
+
this._pop?.classList.toggle('open', this.getBoolAttr('open'));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
_draw() {
|
|
141
|
+
const locale = this._locale, ws = this._weekStart, weeks = this.getBoolAttr('weeks'), range = this.getBoolAttr('range');
|
|
142
|
+
const g = monthGrid(this._month, ws), today = this._todayIso();
|
|
143
|
+
const min = this.getAttribute('min') || '', max = this.getAttribute('max') || '';
|
|
144
|
+
const weekend = new Set((this.getAttribute('weekend') || '').split(',').map(s => s.trim()).filter(Boolean).map(Number));
|
|
145
|
+
let days = this._days;
|
|
146
|
+
if (!days && this.hasAttribute('days')) { try { days = JSON.parse(this.getAttribute('days')); } catch { days = {}; } }
|
|
147
|
+
days ??= {};
|
|
148
|
+
const long = new Intl.DateTimeFormat(locale, { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' });
|
|
149
|
+
this._cal.classList.toggle('weeks', weeks);
|
|
150
|
+
this._syncPop();
|
|
151
|
+
const label = this.getAttribute('label');
|
|
152
|
+
if (label) this._cal.setAttribute('aria-label', label); else this._cal.removeAttribute('aria-label');
|
|
153
|
+
this._title.textContent = new Intl.DateTimeFormat(locale, { month: 'long', year: 'numeric' }).format(g.first);
|
|
154
|
+
this._todayBtn.textContent = this.getAttribute('today-label') || 'Today';
|
|
155
|
+
this._prev.setAttribute('aria-label', this.getAttribute('prev-label') || 'Previous month');
|
|
156
|
+
this._next.setAttribute('aria-label', this.getAttribute('next-label') || 'Next month');
|
|
157
|
+
|
|
158
|
+
// The one tab stop: the focused day, else the chosen one, else the 1st.
|
|
159
|
+
const cells = Array.from({ length: 42 }, (_, i) => isoOf(g.day(i)));
|
|
160
|
+
const chosen = range ? this._start : this._day;
|
|
161
|
+
if (!this._focus || !cells.includes(this._focus)) this._focus = cells.includes(chosen) ? chosen : `${this._month}-01`;
|
|
162
|
+
|
|
163
|
+
const nodes = [];
|
|
164
|
+
if (weeks) nodes.push(this._head(this.getAttribute('week-label') || weekLabel(locale)));
|
|
165
|
+
for (const w of weekdayNames(locale, ws, 2)) nodes.push(this._head(w));
|
|
166
|
+
for (let i = 0; i < 42; i++) {
|
|
167
|
+
const d = g.day(i), iso = cells[i], info = days[iso] || {};
|
|
168
|
+
if (weeks && i % 7 === 0) { const wk = this.createElement('span', ['cal-week'], { 'aria-hidden': 'true' }); wk.textContent = isoWeek(d); nodes.push(wk); }
|
|
169
|
+
const inRange = range && this._start && this._end && iso > this._start && iso < this._end;
|
|
170
|
+
const cls = ['cal-day', d.getMonth() !== g.m - 1 && 'out', (weekend.has(d.getDay()) || info.off) && 'off', iso === today && 'today',
|
|
171
|
+
!range && iso === this._day && 'selected', range && iso === this._start && 'start', range && iso === (this._end || this._start) && this._start && 'end', inRange && 'in-range'].filter(Boolean);
|
|
172
|
+
const day = this.createElement('button', cls, { type: 'button', 'data-date': iso, tabindex: iso === this._focus ? '0' : '-1',
|
|
173
|
+
'aria-pressed': String(cls.includes('selected') || cls.includes('start') || cls.includes('end')),
|
|
174
|
+
'aria-label': `${long.format(d)}${info.label ? `, ${info.label}` : ''}` });
|
|
175
|
+
if (info.label) day.title = info.label;
|
|
176
|
+
if ((min && iso < min) || (max && iso > max)) day.setAttribute('aria-disabled', 'true');
|
|
177
|
+
day.textContent = d.getDate();
|
|
178
|
+
const marks = (info.marks || []).slice(0, 3);
|
|
179
|
+
if (marks.length) {
|
|
180
|
+
const m = this.createElement('span', ['cal-marks']);
|
|
181
|
+
for (const tone of marks) m.appendChild(this.createElement('i', [tone]));
|
|
182
|
+
day.appendChild(m);
|
|
183
|
+
}
|
|
184
|
+
nodes.push(day);
|
|
185
|
+
}
|
|
186
|
+
// The days are drawn anew; focus inside the grid moves to the new tab stop.
|
|
187
|
+
const focused = this._grid.contains(this._shadow.activeElement);
|
|
188
|
+
this._grid.replaceChildren(...nodes);
|
|
189
|
+
if (focused) this.focusDay();
|
|
190
|
+
|
|
191
|
+
const time = this._hasTime, clearable = this.getBoolAttr('clearable');
|
|
192
|
+
this._timeInput.value = this._time;
|
|
193
|
+
this._timeInput.setAttribute('aria-label', this.getAttribute('time-label') || 'Time');
|
|
194
|
+
this._clear.textContent = this.getAttribute('clear-label') || 'Clear';
|
|
195
|
+
this._foot.replaceChildren(...[time && this._timeInput, clearable && this._clear].filter(Boolean));
|
|
196
|
+
if (time || clearable) this._cal.appendChild(this._foot); else this._foot.remove();
|
|
197
|
+
this._syncForm();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
_syncForm() {
|
|
201
|
+
this.setFormValue(this.value);
|
|
202
|
+
if (this.getBoolAttr('required') && !this.value) this.setValidity({ valueMissing: true }, 'Please choose a date.', this._grid?.querySelector('[tabindex="0"]') ?? undefined);
|
|
203
|
+
else this.setValidity({});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
_head(text) {
|
|
207
|
+
const s = this.createElement('span', ['cal-wd'], { 'aria-hidden': 'true' });
|
|
208
|
+
s.textContent = text;
|
|
209
|
+
return s;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ── interaction ──
|
|
213
|
+
_goMonth(month, focus) {
|
|
214
|
+
if (month !== this._month) { this._month = month; this.emit('nk-month', { month }); }
|
|
215
|
+
this._focus = focus ?? null;
|
|
216
|
+
this._draw();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
_pick(iso) {
|
|
220
|
+
const day = this._grid.querySelector(`[data-date="${iso}"]`);
|
|
221
|
+
if (day?.getAttribute('aria-disabled') === 'true') return;
|
|
222
|
+
const before = this.value;
|
|
223
|
+
let done = true;
|
|
224
|
+
if (this.getBoolAttr('range')) {
|
|
225
|
+
if (!this._start || this._end) { this._start = iso; this._end = null; done = false; }
|
|
226
|
+
else if (iso < this._start) { this._end = this._start; this._start = iso; }
|
|
227
|
+
else this._end = iso;
|
|
228
|
+
} else this._day = iso;
|
|
229
|
+
this._focus = iso;
|
|
230
|
+
if (iso.slice(0, 7) !== this._month) this._goMonth(iso.slice(0, 7), iso); else this._draw();
|
|
231
|
+
if (!done) return;
|
|
232
|
+
// Only a new value is a change; the same day again just closes a popover.
|
|
233
|
+
if (this.value !== before) this.emit('nk-change', { value: this.value, start: this.start, end: this.end, time: this._time || null });
|
|
234
|
+
if (this.getBoolAttr('floating') && !this._hasTime) this.close();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
setupEvents() {
|
|
238
|
+
this._onGrid = (e) => { const day = e.target.closest('.cal-day'); if (day) this._pick(day.dataset.date); };
|
|
239
|
+
this._onToday = () => { const t = this._todayIso(); this._goMonth(t.slice(0, 7), t); };
|
|
240
|
+
this._onPrev = () => this._goMonth(stepMonth(this._month, -1));
|
|
241
|
+
this._onNext = () => this._goMonth(stepMonth(this._month, 1));
|
|
242
|
+
this._onKey = (e) => {
|
|
243
|
+
// Enter and Space are the day button's own click.
|
|
244
|
+
const day = e.target.closest?.('.cal-day');
|
|
245
|
+
if (!day) return;
|
|
246
|
+
const date = parseDay(day.dataset.date), ws = this._weekStart, wd = (date.getDay() - ws + 7) % 7;
|
|
247
|
+
let target = null;
|
|
248
|
+
if (STEPS[e.key]) target = addDays(date, STEPS[e.key]);
|
|
249
|
+
else if (e.key === 'Home') target = addDays(date, -wd);
|
|
250
|
+
else if (e.key === 'End') target = addDays(date, 6 - wd);
|
|
251
|
+
else if (e.key === 'PageUp' || e.key === 'PageDown') {
|
|
252
|
+
const months = (e.key === 'PageUp' ? -1 : 1) * (e.shiftKey ? 12 : 1);
|
|
253
|
+
const last = new Date(date.getFullYear(), date.getMonth() + months + 1, 0).getDate();
|
|
254
|
+
target = new Date(date.getFullYear(), date.getMonth() + months, Math.min(date.getDate(), last));
|
|
255
|
+
}
|
|
256
|
+
if (!target) return;
|
|
257
|
+
e.preventDefault();
|
|
258
|
+
const iso = isoOf(target);
|
|
259
|
+
if (iso.slice(0, 7) !== this._month) this._goMonth(iso.slice(0, 7), iso);
|
|
260
|
+
else { this._focus = iso; this._draw(); }
|
|
261
|
+
};
|
|
262
|
+
this._onTime = () => {
|
|
263
|
+
this._time = this._timeInput.value;
|
|
264
|
+
this._syncForm();
|
|
265
|
+
if (this._day) this.emit('nk-change', { value: this.value, start: this.start, end: this.end, time: this._time || null });
|
|
266
|
+
};
|
|
267
|
+
this._onClear = () => {
|
|
268
|
+
if (!this.value) return;
|
|
269
|
+
this._day = this._start = this._end = null; this._time = '';
|
|
270
|
+
this._draw();
|
|
271
|
+
this.emit('nk-change', { value: '', start: null, end: null, time: null });
|
|
272
|
+
};
|
|
273
|
+
// Floating: a tap outside closes it and reaches nothing else; Escape closes (captured, before a modal around it).
|
|
274
|
+
this._onOutside = (e) => {
|
|
275
|
+
const path = e.composedPath();
|
|
276
|
+
if (path.includes(this) || (this._anchor && path.includes(this._anchor))) return;
|
|
277
|
+
e.preventDefault(); e.stopPropagation();
|
|
278
|
+
this.close();
|
|
279
|
+
};
|
|
280
|
+
this._onEscape = (e) => { if (e.key === 'Escape' && this.getBoolAttr('floating') && this.getBoolAttr('open')) { e.stopPropagation(); this.close(); } };
|
|
281
|
+
this._grid.addEventListener('click', this._onGrid);
|
|
282
|
+
this._grid.addEventListener('keydown', this._onKey);
|
|
283
|
+
this._todayBtn.addEventListener('click', this._onToday);
|
|
284
|
+
this._prev.addEventListener('click', this._onPrev);
|
|
285
|
+
this._next.addEventListener('click', this._onNext);
|
|
286
|
+
this._timeInput.addEventListener('change', this._onTime);
|
|
287
|
+
this._clear.addEventListener('click', this._onClear);
|
|
288
|
+
document.addEventListener('keydown', this._onEscape, true);
|
|
289
|
+
if (this.getBoolAttr('open')) document.addEventListener('click', this._onOutside, true);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
teardownEvents() {
|
|
293
|
+
this._grid?.removeEventListener('click', this._onGrid);
|
|
294
|
+
this._grid?.removeEventListener('keydown', this._onKey);
|
|
295
|
+
this._todayBtn?.removeEventListener('click', this._onToday);
|
|
296
|
+
this._prev?.removeEventListener('click', this._onPrev);
|
|
297
|
+
this._next?.removeEventListener('click', this._onNext);
|
|
298
|
+
this._timeInput?.removeEventListener('change', this._onTime);
|
|
299
|
+
this._clear?.removeEventListener('click', this._onClear);
|
|
300
|
+
document.removeEventListener('keydown', this._onEscape, true);
|
|
301
|
+
document.removeEventListener('click', this._onOutside, true);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
onAttributeChanged(name, _old, value) {
|
|
305
|
+
if (name === 'value') { this._readValue(value); this._draw(); return; }
|
|
306
|
+
if (name === 'month') { this.month = value; return; }
|
|
307
|
+
if (name === 'open' || name === 'sheet') { this._syncPop(); if (name === 'open') this._syncOpen(); return; }
|
|
308
|
+
if (name === 'align') return;
|
|
309
|
+
if (name === 'floating') { this.teardownEvents(); this._build(); if (this.isConnected) this.setupEvents(); return; }
|
|
310
|
+
if (name === 'range') this._readValue(this.getAttribute('value'));
|
|
311
|
+
if (name === 'days') this._days = null;
|
|
312
|
+
this._draw();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
_syncOpen() {
|
|
316
|
+
const open = this.getBoolAttr('open');
|
|
317
|
+
if (this._anchor) (this._anchor.shadowRoot?.querySelector('button, a[href]') ?? this._anchor).setAttribute('aria-expanded', String(open));
|
|
318
|
+
if (open) {
|
|
319
|
+
this._returnFocus = deepActiveElement();
|
|
320
|
+
if (this._returnFocus?.matches(':focus-visible')) requestAnimationFrame(() => this.focusDay());
|
|
321
|
+
document.addEventListener('click', this._onOutside, true);
|
|
322
|
+
} else {
|
|
323
|
+
document.removeEventListener('click', this._onOutside, true);
|
|
324
|
+
const back = this._returnFocus;
|
|
325
|
+
this._returnFocus = null;
|
|
326
|
+
const active = deepActiveElement();
|
|
327
|
+
if (back?.isConnected && active && this._shadow.contains(active)) back.focus({ preventScroll: true });
|
|
328
|
+
}
|
|
329
|
+
this.emit('nk-toggle', { open });
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// ── form ──
|
|
333
|
+
resetValue() { this._readValue(this._initialValue); this._draw(); }
|
|
334
|
+
restoreValue(state) { this._readValue(state); this._draw(); }
|
|
335
|
+
onFormDisabled(disabled) { this._cal?.toggleAttribute('inert', disabled); }
|
|
336
|
+
|
|
337
|
+
// ── API ──
|
|
338
|
+
/** Opens under `anchor` (with `floating`), on the month of the value. */
|
|
339
|
+
show(anchor) {
|
|
340
|
+
if (anchor) { this._anchor = anchor; placeUnder(this, anchor, this.getAttribute('align')); }
|
|
341
|
+
const shown = this.start;
|
|
342
|
+
if (shown && shown.slice(0, 7) !== this._month) { this._month = shown.slice(0, 7); this._focus = null; this._draw(); }
|
|
343
|
+
this.setBoolAttr('open', true);
|
|
344
|
+
}
|
|
345
|
+
close() { this.setBoolAttr('open', false); }
|
|
346
|
+
toggle(anchor) { this.getBoolAttr('open') ? this.close() : this.show(anchor); }
|
|
347
|
+
get open() { return this.getBoolAttr('open'); }
|
|
348
|
+
set open(v) { this.setBoolAttr('open', v); }
|
|
349
|
+
/** Moves focus to the day with the tab stop. */
|
|
350
|
+
focusDay() { this._grid?.querySelector('[tabindex="0"]')?.focus(); }
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
customElements.define('nk-calendar', NkCalendar);
|
|
354
|
+
export { NkCalendar };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NkElement } from '../../base.js';
|
|
2
2
|
import { deepActiveElement } from '../../util/focus.js';
|
|
3
|
+
import { placeUnder } from '../../util/floating.js';
|
|
3
4
|
|
|
4
5
|
// <nk-menu>
|
|
5
6
|
// <nk-menu-item type="label">Page</nk-menu-item>
|
|
@@ -20,16 +21,6 @@ import { deepActiveElement } from '../../util/focus.js';
|
|
|
20
21
|
const floatSheet = new CSSStyleSheet();
|
|
21
22
|
floatSheet.replaceSync(`:host([floating]) { display: block; position: fixed; z-index: 60; pointer-events: none; }`);
|
|
22
23
|
|
|
23
|
-
/** The border box of an element, or of the first descendant with one (display: contents hosts). */
|
|
24
|
-
function boxOf(el) {
|
|
25
|
-
if (el.getClientRects().length) return el.getBoundingClientRect();
|
|
26
|
-
for (const child of [...(el.shadowRoot?.children ?? []), ...el.children]) {
|
|
27
|
-
const r = boxOf(child);
|
|
28
|
-
if (r.width || r.height) return r;
|
|
29
|
-
}
|
|
30
|
-
return el.getBoundingClientRect();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
24
|
class NkMenu extends NkElement {
|
|
34
25
|
static get hostStyles() { return floatSheet; }
|
|
35
26
|
static get observedAttributes() { return ['floating', 'sheet', 'open', 'align']; }
|
|
@@ -70,10 +61,7 @@ class NkMenu extends NkElement {
|
|
|
70
61
|
show(anchor) {
|
|
71
62
|
if (anchor) {
|
|
72
63
|
this._anchor = anchor;
|
|
73
|
-
|
|
74
|
-
this.style.top = `${r.bottom + 6}px`;
|
|
75
|
-
if (this.getAttribute('align') === 'start') { this.style.left = `${r.left}px`; this.style.right = ''; }
|
|
76
|
-
else { this.style.right = `${Math.max(8, innerWidth - r.right)}px`; this.style.left = ''; }
|
|
64
|
+
placeUnder(this, anchor, this.getAttribute('align'));
|
|
77
65
|
}
|
|
78
66
|
this.setBoolAttr('open', true);
|
|
79
67
|
}
|
package/src/index.js
CHANGED
|
@@ -88,6 +88,7 @@ export { NkSteps } from './components/page/nk-steps.js';
|
|
|
88
88
|
export { NkBookmark } from './components/content/nk-bookmark.js';
|
|
89
89
|
export { NkCopyField } from './components/forms/nk-copy-field.js';
|
|
90
90
|
export { NkImagePicker } from './components/forms/nk-image-picker.js';
|
|
91
|
+
export { NkCalendar } from './components/forms/nk-calendar.js';
|
|
91
92
|
export { NkPeek } from './components/overlays/nk-peek.js';
|
|
92
93
|
|
|
93
94
|
// Wave 4 – overlays
|
|
@@ -106,6 +107,7 @@ export { NkDatabase } from './components/data/nk-database.js';
|
|
|
106
107
|
export { NkTableView } from './components/data/nk-table-view.js';
|
|
107
108
|
export { NkBoardView } from './components/data/nk-board-view.js';
|
|
108
109
|
export { NkListView } from './components/data/nk-list-view.js';
|
|
110
|
+
export { NkCalendarView } from './components/data/nk-calendar-view.js';
|
|
109
111
|
export { NkFilterBar } from './components/data/nk-filter-bar.js';
|
|
110
112
|
export { NkComments } from './components/data/nk-comments.js';
|
|
111
113
|
export { NkComment } from './components/data/nk-comment.js';
|