@jungherz-de/notionkit-elements 1.14.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +9 -7
  3. package/SKILL.md +33 -31
  4. package/dist/components/base.js +4 -0
  5. package/dist/components/nk-board-view.js +4 -3
  6. package/dist/components/nk-calendar-view.js +3 -1
  7. package/dist/components/nk-calendar.js +2 -2
  8. package/dist/components/nk-cmdk.js +1 -1
  9. package/dist/components/nk-dialog.js +1 -1
  10. package/dist/components/nk-filter-bar.js +3 -2
  11. package/dist/components/nk-gallery-view.js +4 -3
  12. package/dist/components/nk-list-view.js +4 -3
  13. package/dist/components/nk-menu.js +2 -2
  14. package/dist/components/nk-modal.js +1 -1
  15. package/dist/components/nk-panel.js +4 -1
  16. package/dist/components/nk-peek.js +1 -1
  17. package/dist/components/nk-select.js +13 -8
  18. package/dist/components/nk-sheet.js +1 -1
  19. package/dist/components/nk-sidebar.js +1 -1
  20. package/dist/components/nk-table-view.js +2 -1
  21. package/dist/components/nk-tooltip.js +1 -1
  22. package/dist/components/shared/{focus-BNAChOXO.js → focus-C4tbSNND.js} +7 -2
  23. package/dist/components/shared/property-cell-BSVcETss.js +204 -0
  24. package/dist/notionkit-elements.esm.js +132 -37
  25. package/dist/notionkit-elements.esm.min.js +2 -0
  26. package/dist/notionkit-elements.esm.min.js.map +1 -0
  27. package/dist/notionkit-elements.js +134 -36
  28. package/dist/notionkit-elements.min.js +1 -1
  29. package/package.json +4 -3
  30. package/src/base.js +4 -0
  31. package/src/components/data/nk-board-view.js +3 -3
  32. package/src/components/data/nk-calendar-view.js +2 -1
  33. package/src/components/data/nk-filter-bar.js +3 -2
  34. package/src/components/data/nk-gallery-view.js +3 -3
  35. package/src/components/data/nk-list-view.js +3 -3
  36. package/src/components/forms/nk-calendar.js +1 -1
  37. package/src/components/forms/nk-select.js +13 -8
  38. package/src/components/overlays/nk-menu.js +1 -1
  39. package/src/components/page/nk-panel.js +4 -1
  40. package/src/index.js +1 -1
  41. package/src/util/focus.js +7 -2
  42. package/src/util/property-cell.js +90 -12
  43. package/dist/components/shared/property-cell-DKvv6TIG.js +0 -126
@@ -9,8 +9,10 @@ import '@jungherz-de/notionkit/notionkit-styles.js';
9
9
  // The light-DOM <option>s are copied into the shadow <select>. A
10
10
  // MutationObserver keeps the copy in step when a framework swaps them; the
11
11
  // empty string is a valid value; the live selection survives a rebuild. A
12
- // value set before its option exists – a framework sets properties before
13
- // children – waits and is applied once the option arrives.
12
+ // value – attribute or property – whose option is not there yet waits and
13
+ // wins over the browser's preselection once the option arrives, unless
14
+ // someone has chosen another option in the meantime; settling on it fires
15
+ // no nk-change.
14
16
  class NkSelect extends NkFormElement {
15
17
  static get observedAttributes() { return ['value', 'name', 'disabled', 'required', 'compact', 'wide', 'aria-label']; }
16
18
  static get observesLightDom() { return true; }
@@ -20,6 +22,7 @@ class NkSelect extends NkFormElement {
20
22
  this._forwardAll();
21
23
  this._wrapper.appendChild(this._select);
22
24
  this._defaultValue = this.getAttribute('value');
25
+ this._pending = this._defaultValue;
23
26
  // Children may not be parsed yet when connectedCallback runs.
24
27
  this.projectLightDom();
25
28
  requestAnimationFrame(() => this.projectLightDom());
@@ -53,11 +56,11 @@ class NkSelect extends NkFormElement {
53
56
  this._select.innerHTML = '';
54
57
  for (const node of nodes) this._select.appendChild(node.cloneNode(true));
55
58
 
56
- // A value that waited for its option first; then the live selection when
57
- // it survived; otherwise the value attribute; otherwise the browser
58
- // default (first option / `selected`).
59
- if (this._pending != null && this._applyValue(this._pending)) this._pending = null;
60
- else if (!this._applyValue(previous)) this._applyValue(this.getAttribute('value'));
59
+ // A value that waits for its option first – it keeps waiting while the
60
+ // option is missing; else the live selection when it survived; else the
61
+ // browser default (first option / `selected`).
62
+ if (this._pending != null) { if (this._applyValue(this._pending)) this._pending = null; }
63
+ else this._applyValue(previous);
61
64
  this._syncFormValue();
62
65
  }
63
66
 
@@ -86,7 +89,8 @@ class NkSelect extends NkFormElement {
86
89
  onAttributeChanged(name) {
87
90
  if (!this._select) return;
88
91
  if (name === 'value') {
89
- this._applyValue(this.getAttribute('value'));
92
+ const value = this.getAttribute('value');
93
+ this._pending = value === null || this._applyValue(value) ? null : value;
90
94
  this._syncFormValue();
91
95
  return;
92
96
  }
@@ -100,6 +104,7 @@ class NkSelect extends NkFormElement {
100
104
  }
101
105
 
102
106
  resetValue() {
107
+ this._pending = null;
103
108
  if (!this._applyValue(this._defaultValue)) this._select.selectedIndex = 0;
104
109
  this._syncFormValue();
105
110
  }
@@ -1,5 +1,5 @@
1
1
  import { NkElement } from './base.js';
2
- import { d as deepActiveElement, l as lockScroll, i as inertOutside, f as firstFocusable, u as unlockScroll } from './shared/focus-BNAChOXO.js';
2
+ import { d as deepActiveElement, l as lockScroll, i as inertOutside, f as firstFocusable, u as unlockScroll } from './shared/focus-C4tbSNND.js';
3
3
  import '@jungherz-de/notionkit/notionkit-styles.js';
4
4
 
5
5
  // <nk-sheet id="more" title="More">
@@ -1,5 +1,5 @@
1
1
  import { NkElement } from './base.js';
2
- import { d as deepActiveElement } from './shared/focus-BNAChOXO.js';
2
+ import { d as deepActiveElement } from './shared/focus-C4tbSNND.js';
3
3
  import '@jungherz-de/notionkit/notionkit-styles.js';
4
4
 
5
5
  // <nk-sidebar slot="sidebar">
@@ -1,7 +1,8 @@
1
1
  import { NkElement } from './base.js';
2
- import { c as compareBy, r as renderPropertyCell } from './shared/property-cell-DKvv6TIG.js';
2
+ import { c as compareBy, r as renderPropertyCell } from './shared/property-cell-BSVcETss.js';
3
3
  import '@jungherz-de/notionkit/notionkit-styles.js';
4
4
  import './shared/avatar-CzPZN3uP.js';
5
+ import './shared/dates-a32DcW1l.js';
5
6
 
6
7
  // <nk-table-view name="table" label="▦ Table" count new-row sortable wrap></nk-table-view>
7
8
  // `wrap` lets cell text break (`.nk-table.wrap`), like Notion's "wrap column".
@@ -1,6 +1,6 @@
1
1
  import { NkElement } from './base.js';
2
2
  import { a as placeNear } from './shared/floating-DOK73b_D.js';
3
- import { d as deepActiveElement } from './shared/focus-BNAChOXO.js';
3
+ import { d as deepActiveElement } from './shared/focus-C4tbSNND.js';
4
4
  import '@jungherz-de/notionkit/notionkit-styles.js';
5
5
 
6
6
  // <nk-tooltip></nk-tooltip> one for the page: every [data-tooltip]
@@ -15,7 +15,12 @@ function unlockScroll() {
15
15
  if (locks > 0 && --locks === 0) document.documentElement.style.overflow = previousOverflow;
16
16
  }
17
17
 
18
- /** Makes every sibling along `el`'s ancestor chain inert (shadow hosts included); returns the undo. */
18
+ // Floating layers under <body> that open from inside an overlay – a menu, a
19
+ // date picker – and the tooltip and toast stay usable: made inert, a date
20
+ // picker opened from a dialog showed but took no click.
21
+ const LAYERS = 'nk-menu[floating], nk-calendar[floating], nk-tooltip, nk-toast';
22
+
23
+ /** Makes every sibling along `el`'s ancestor chain inert (shadow hosts included), floating layers aside; returns the undo. */
19
24
  function inertOutside(el) {
20
25
  const made = [];
21
26
  let node = el;
@@ -23,7 +28,7 @@ function inertOutside(el) {
23
28
  const parent = node.parentNode instanceof ShadowRoot ? node.parentNode : node.parentNode;
24
29
  if (!parent) break;
25
30
  for (const sib of parent.children) {
26
- if (sib === node || sib.inert || /^(SCRIPT|STYLE|LINK|TEMPLATE)$/.test(sib.tagName)) continue;
31
+ if (sib === node || sib.inert || /^(SCRIPT|STYLE|LINK|TEMPLATE)$/.test(sib.tagName) || sib.matches(LAYERS)) continue;
27
32
  sib.inert = true;
28
33
  made.push(sib);
29
34
  }
@@ -0,0 +1,204 @@
1
+ import { i as initialsOf, p as paintAvatar } from './avatar-CzPZN3uP.js';
2
+ import { r as resolveLocale } from './dates-a32DcW1l.js';
3
+
4
+ // Polymorphic property renderer for database views. Returns plain DOM built
5
+ // from NotionKit classes – deliberately not a custom element: every cell rule
6
+ // starts with `.nk-table`, so a cell inside its own shadow root would never
7
+ // be styled. Exported for consumers who render tables themselves.
8
+ //
9
+ // column: { key, label, type, icon, options: [{ value, label, color }], title }
10
+ // types: text | select | multi-select | date | person | checkbox | url | number | progress | actions
11
+ // url: a string is an address outside, opened in a new tab; { href, label,
12
+ // target } a link of your own – to another page of the app, with its text.
13
+ // actions: buttons for the row from column.actions [{ action, label, icon,
14
+ // danger, disabled, tooltip }]; the row's value – action names or objects –
15
+ // picks which of them it shows, all of them without one.
16
+ // text: a string, or { text, desc, color, tooltip, sort } – one of Notion's
17
+ // nine text colours for a tone, a quiet second line under it, the whole
18
+ // text in a tooltip (served by <nk-tooltip>), a value to sort by.
19
+ // date: the value as given, or with column.format ('short', 'relative' or
20
+ // Intl.DateTimeFormat options) and column.locale formatted from an ISO date
21
+ // or date-time, D.M.YYYY, a Date or a timestamp; { start, end } is a range.
22
+ // Sorting (compareBy) goes by column.sortKey – another field of the row –
23
+ // when there is one, else by a value's `sort`, a date's time, a select's
24
+ // place among its options; empty cells come last in both directions.
25
+
26
+ const el = (tag, cls, text) => { const n = document.createElement(tag); if (cls) n.className = cls; if (text != null) n.textContent = text; return n; };
27
+ const COLORS = ['gray', 'brown', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'red'];
28
+
29
+ function optionFor(column, value) {
30
+ const opts = column.options || [];
31
+ return opts.find(o => o.value === value) || opts.find(o => o.label === value) || null;
32
+ }
33
+
34
+ function tagFor(column, value) {
35
+ const opt = optionFor(column, value);
36
+ const color = opt?.color && COLORS.includes(opt.color) ? opt.color : 'gray'; // Notion's default option colour
37
+ return el('span', `nk-tag ${color}`, opt?.label ?? String(value));
38
+ }
39
+
40
+ /** The text of a value: a string as it is, a text, link or person object's text, label or name. */
41
+ const textOf = v => (v && typeof v === 'object' ? v.text ?? v.label ?? v.name ?? '' : String(v ?? ''));
42
+
43
+ /** The time of a date value in ms: an ISO date – local midnight – or date-time, D.M.YYYY, a Date, a timestamp, a range's start; NaN when it is none. */
44
+ function timeOf(v) {
45
+ if (v instanceof Date) return v.getTime();
46
+ if (typeof v === 'number') return v;
47
+ if (v && typeof v === 'object') return timeOf(v.start);
48
+ const s = String(v ?? '').trim();
49
+ let m = s.match(/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/);
50
+ if (m) return new Date(+m[3], +m[2] - 1, +m[1]).getTime();
51
+ m = s.match(/^(\d{4})-(\d{2})-(\d{2})$/);
52
+ if (m) return new Date(+m[1], +m[2] - 1, +m[3]).getTime();
53
+ return /^\d{4}-\d{2}-\d{2}T/.test(s) ? Date.parse(s) : NaN;
54
+ }
55
+
56
+ /**
57
+ * A date as its column shows it: as given without column.format; 'short' –
58
+ * day and month, the year when it is not this one; 'relative' – minutes or
59
+ * hours ago or ahead within a day, else the short date; or the options of
60
+ * Intl.DateTimeFormat. A range { start, end } stands as start → end.
61
+ */
62
+ function formatDate(column, value) {
63
+ if (value && typeof value === 'object' && !(value instanceof Date)) return [value.start, value.end].filter(Boolean).map(v => formatDate(column, v)).join(' → ');
64
+ const format = column.format, t = timeOf(value);
65
+ if (!format || Number.isNaN(t)) return String(value ?? '');
66
+ const d = new Date(t), locale = column.locale || resolveLocale();
67
+ if (format === 'relative') {
68
+ const minutes = Math.round((t - Date.now()) / 60000), rel = new Intl.RelativeTimeFormat(locale, { numeric: 'auto', style: 'short' });
69
+ if (Math.abs(minutes) < 60) return rel.format(minutes, 'minute');
70
+ if (Math.abs(minutes) < 24 * 60) return rel.format(Math.round(minutes / 60), 'hour');
71
+ }
72
+ if (format === 'short' || format === 'relative') {
73
+ const year = d.getFullYear() === new Date().getFullYear() ? {} : { year: 'numeric' };
74
+ return d.toLocaleDateString(locale, { day: '2-digit', month: '2-digit', ...year });
75
+ }
76
+ return typeof format === 'object' ? new Intl.DateTimeFormat(locale, format).format(d) : String(value);
77
+ }
78
+
79
+ function renderPropertyCell(column, value, row = {}) {
80
+ const type = column.type || 'text';
81
+ if (type === 'actions') return actions(column, value);
82
+ if (value === undefined || value === null || value === '') {
83
+ if (type === 'checkbox') return checkbox(column, false, row);
84
+ if (type === 'progress') return progress(0);
85
+ return el('span', type === 'person' ? 'person-cell' : null, type === 'text' && column.title ? '' : '—');
86
+ }
87
+ switch (type) {
88
+ case 'select': return tagFor(column, value);
89
+ case 'multi-select': {
90
+ const wrap = el('span');
91
+ wrap.style.display = 'inline-flex'; wrap.style.gap = '4px'; wrap.style.flexWrap = 'wrap';
92
+ for (const v of Array.isArray(value) ? value : String(value).split(',').map(s => s.trim()).filter(Boolean)) wrap.appendChild(tagFor(column, v));
93
+ return wrap;
94
+ }
95
+ case 'date': return el('span', 'date-cell', formatDate(column, value));
96
+ case 'person': {
97
+ const cell = el('span', 'person-cell');
98
+ const p = typeof value === 'string' ? { name: value } : value;
99
+ // color: one of Notion's nine names, or any CSS background; none – the avatar gradient.
100
+ const avatar = el('span', 'nk-avatar small', p.initials || p.avatar || initialsOf(p.name));
101
+ paintAvatar(avatar, p.color);
102
+ cell.append(avatar, document.createTextNode(' ' + (p.name || '')));
103
+ return cell;
104
+ }
105
+ case 'checkbox': return checkbox(column, !!value, row);
106
+ case 'url': {
107
+ const link = typeof value === 'object' ? value : { href: String(value), label: String(value).replace(/^https?:\/\//, ''), target: '_blank' };
108
+ const a = el('a', null, link.label ?? link.href);
109
+ a.href = link.href ?? '';
110
+ if (link.target) a.target = link.target;
111
+ if (link.target === '_blank') a.rel = 'noopener';
112
+ return a;
113
+ }
114
+ // column.locale / column.format: Intl.NumberFormat locale and options, e.g. { minimumFractionDigits: 1 }.
115
+ case 'number': return el('span', null, typeof value === 'number' ? value.toLocaleString(column.locale, column.format) : String(value));
116
+ case 'progress': return progress(Number(value) || 0);
117
+ default: {
118
+ const o = typeof value === 'object' ? value : { text: value };
119
+ let t;
120
+ if (column.title) {
121
+ t = el('span', 'row-title');
122
+ if (row.icon) t.append(document.createTextNode(row.icon + ' '));
123
+ t.append(document.createTextNode(String(o.text ?? '')));
124
+ } else if (typeof value === 'object') {
125
+ t = el('span', o.color && COLORS.includes(o.color) ? `td-text ${o.color}` : 'td-text', String(o.text ?? ''));
126
+ } else return el('span', null, String(value));
127
+ if (o.tooltip) t.dataset.tooltip = o.tooltip;
128
+ if (!o.desc) return t;
129
+ const frag = document.createDocumentFragment();
130
+ frag.append(t, el('span', 'td-desc', String(o.desc)));
131
+ return frag;
132
+ }
133
+ }
134
+ }
135
+
136
+ function actions(column, value) {
137
+ const all = column.actions || [];
138
+ const list = value == null ? all
139
+ : (Array.isArray(value) ? value : [value]).map(v => typeof v === 'string' ? all.find(a => a.action === v) ?? { action: v, label: v } : v);
140
+ const wrap = el('span', 'row-actions');
141
+ for (const a of list) {
142
+ const b = el('button', `nk-btn ${a.danger ? 'danger' : 'secondary'} small`, [a.icon, a.label ?? a.action].filter(Boolean).join(' '));
143
+ b.type = 'button';
144
+ b.dataset.action = a.action;
145
+ b.disabled = !!a.disabled;
146
+ if (a.tooltip) b.dataset.tooltip = a.tooltip;
147
+ wrap.appendChild(b);
148
+ }
149
+ return wrap;
150
+ }
151
+
152
+ function checkbox(column, checked, row) {
153
+ const label = el('label', 'nk-check');
154
+ const input = el('input');
155
+ input.type = 'checkbox'; input.checked = checked;
156
+ input.dataset.key = column.key;
157
+ if (row.id != null) input.dataset.rowId = row.id;
158
+ label.appendChild(input);
159
+ return label;
160
+ }
161
+
162
+ function progress(value) {
163
+ const frag = document.createDocumentFragment();
164
+ const bar = el('span', 'nk-progress');
165
+ const fill = el('i'); fill.style.width = `${Math.max(0, Math.min(100, value))}%`;
166
+ bar.appendChild(fill);
167
+ frag.append(bar, el('span', 'nk-progress-label', `${Math.round(value)}%`));
168
+ return frag;
169
+ }
170
+
171
+ /** A select's place among its options: the order Notion sorts by. */
172
+ function rankOf(column, value) {
173
+ const i = (column.options || []).findIndex(o => o.value === value || o.label === value);
174
+ return i < 0 ? (column.options || []).length : i;
175
+ }
176
+
177
+ /** What a column sorts a row by – see the head of this file. */
178
+ function sortValue(column, row) {
179
+ if (column.sortKey) return row[column.sortKey];
180
+ const v = row[column.key];
181
+ if (v === undefined || v === null || v === '') return null;
182
+ if (typeof v === 'object' && !Array.isArray(v) && 'sort' in v) return v.sort;
183
+ switch (column.type || 'text') {
184
+ case 'number': case 'progress': return Number(v);
185
+ case 'checkbox': return v ? 1 : 0;
186
+ case 'date': { const t = timeOf(v); return Number.isNaN(t) ? String(v) : t; }
187
+ case 'select': return rankOf(column, v);
188
+ case 'multi-select': { const first = (Array.isArray(v) ? v : String(v).split(','))[0]; return first == null ? null : rankOf(column, String(first).trim()); }
189
+ default: return textOf(v);
190
+ }
191
+ }
192
+
193
+ /** Sort comparator for a column; empty cells come last whichever the direction. */
194
+ function compareBy(column, dir = 1) {
195
+ const empty = v => v === null || v === undefined || v === '' || Number.isNaN(v);
196
+ return (a, b) => {
197
+ const x = sortValue(column, a), y = sortValue(column, b);
198
+ if (empty(x) || empty(y)) return empty(x) === empty(y) ? 0 : empty(x) ? 1 : -1;
199
+ if (typeof x === 'number' && typeof y === 'number') return (x - y) * dir;
200
+ return String(x).localeCompare(String(y), undefined, { numeric: true }) * dir;
201
+ };
202
+ }
203
+
204
+ export { textOf as a, compareBy as c, formatDate as f, renderPropertyCell as r, tagFor as t };