@jungherz-de/notionkit-elements 1.17.0 → 1.19.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/CHANGELOG.md +30 -0
- package/README.md +7 -7
- package/SKILL.md +53 -42
- package/dist/components/nk-calendar.js +6 -5
- package/dist/components/nk-cmdk.js +7 -5
- package/dist/components/nk-copy-field.js +33 -5
- package/dist/components/nk-dialog.js +5 -5
- package/dist/components/nk-menu.js +19 -6
- package/dist/components/nk-modal.js +7 -6
- package/dist/components/nk-peek.js +5 -4
- package/dist/components/nk-pop.js +3 -3
- package/dist/components/nk-progress.js +6 -3
- package/dist/components/nk-sheet.js +5 -5
- package/dist/components/nk-sidebar.js +1 -1
- package/dist/components/nk-tab-bar.js +10 -4
- package/dist/components/nk-toast.js +107 -11
- package/dist/components/nk-tooltip.js +1 -1
- package/dist/components/shared/{focus-C4tbSNND.js → focus-D55JGjRA.js} +7 -1
- package/dist/components/shared/layers-D9YYYA5g.js +35 -0
- package/dist/notionkit-elements.esm.js +241 -59
- package/dist/notionkit-elements.esm.min.js +1 -1
- package/dist/notionkit-elements.esm.min.js.map +1 -1
- package/dist/notionkit-elements.js +241 -59
- package/dist/notionkit-elements.min.js +1 -1
- package/package.json +3 -3
- package/src/components/content/nk-progress.js +6 -3
- package/src/components/forms/nk-calendar.js +5 -4
- package/src/components/forms/nk-copy-field.js +33 -5
- package/src/components/overlays/nk-cmdk.js +7 -5
- package/src/components/overlays/nk-dialog.js +4 -4
- package/src/components/overlays/nk-menu.js +19 -6
- package/src/components/overlays/nk-modal.js +7 -6
- package/src/components/overlays/nk-peek.js +4 -3
- package/src/components/overlays/nk-pop.js +3 -3
- package/src/components/overlays/nk-sheet.js +4 -4
- package/src/components/overlays/nk-toast.js +107 -11
- package/src/components/shell/nk-tab-bar.js +10 -4
- package/src/util/focus.js +6 -0
- package/src/util/layers.js +33 -0
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import { NkElement } from '../../base.js';
|
|
2
|
+
import { deepActiveElement, containsDeep } from '../../util/focus.js';
|
|
2
3
|
|
|
3
4
|
// <nk-toast id="toast"></nk-toast> … toast.show('Saved');
|
|
4
|
-
//
|
|
5
|
+
// toast.show('Moved to trash', { action: { label: 'Undo', value: 'undo' } });
|
|
6
|
+
// → <div class="nk-toast show">✓ Moved to trash<button class="t-action">Undo</button><button class="t-close">×</button></div>
|
|
5
7
|
// One node, bottom-centred, auto-hides after `duration` ms (default 2200).
|
|
6
8
|
// Above a tab bar at the bottom of the screen it rises 12px over the bar:
|
|
7
9
|
// notionkit.css does that for class markup with :has(), which cannot see
|
|
8
10
|
// into shadow roots, so the element measures the bar when it opens.
|
|
11
|
+
// An action (1.19.0) – from show() or a <button slot="action"> – makes it
|
|
12
|
+
// Notion's "Moved to trash · Undo": a button, and an × that closes. Such a
|
|
13
|
+
// toast stays until one of them is used, unless show() names a duration,
|
|
14
|
+
// and waits while the pointer or the focus is on it. The action fires
|
|
15
|
+
// nk-action { action, value, label } and, unless cancelled, closes it;
|
|
16
|
+
// Escape closes it while the focus is inside.
|
|
17
|
+
const CLOSE_ICON = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12"/></svg>';
|
|
18
|
+
|
|
9
19
|
class NkToast extends NkElement {
|
|
10
20
|
static get observedAttributes() { return ['open', 'duration', 'icon']; }
|
|
11
21
|
|
|
@@ -13,19 +23,37 @@ class NkToast extends NkElement {
|
|
|
13
23
|
this._box = this.createElement('div', ['nk-toast'], { role: 'status', 'aria-live': 'polite' });
|
|
14
24
|
this._icon = document.createElement('span');
|
|
15
25
|
this._msg = document.createElement('span');
|
|
16
|
-
this.
|
|
26
|
+
this._action = this.createElement('button', ['t-action'], { type: 'button' });
|
|
27
|
+
this._actionSlot = this.createElement('slot', [], { name: 'action' });
|
|
28
|
+
this._close = this.createElement('button', ['t-close'], { type: 'button' });
|
|
29
|
+
this._close.innerHTML = CLOSE_ICON;
|
|
30
|
+
this._box.append(this._icon, this._msg, document.createElement('slot'), this._actionSlot);
|
|
17
31
|
this._wrapper.appendChild(this._box);
|
|
18
32
|
this._sync();
|
|
19
33
|
}
|
|
20
34
|
|
|
21
35
|
_sync() {
|
|
22
|
-
|
|
23
|
-
|
|
36
|
+
const open = this.getBoolAttr('open');
|
|
37
|
+
if (open) this._lift();
|
|
38
|
+
this._box.classList.toggle('show', open);
|
|
24
39
|
const icon = this.getAttribute('icon') ?? '✓';
|
|
25
40
|
this._icon.textContent = icon;
|
|
26
41
|
this._icon.style.display = icon ? '' : 'none';
|
|
42
|
+
// An empty message takes no place – with slotted text it added a second gap.
|
|
43
|
+
this._msg.style.display = this._msg.textContent ? '' : 'none';
|
|
44
|
+
// The action from show() and the × sit in the tree only when there is an
|
|
45
|
+
// action; a closed toast keeps them for its fade, out of the tab order.
|
|
46
|
+
const own = this._act, slotted = this._slotted();
|
|
47
|
+
if (own) { this._action.textContent = own.label; if (!this._action.isConnected) this._actionSlot.before(this._action); }
|
|
48
|
+
else this._action.remove();
|
|
49
|
+
if (own || slotted) { if (!this._close.isConnected) this._box.appendChild(this._close); }
|
|
50
|
+
else this._close.remove();
|
|
51
|
+
this._close.setAttribute('aria-label', this.str('close'));
|
|
52
|
+
for (const b of [this._action, this._close]) open ? b.removeAttribute('tabindex') : b.setAttribute('tabindex', '-1');
|
|
27
53
|
}
|
|
28
54
|
|
|
55
|
+
_slotted() { return this._actionSlot?.assignedElements()[0] ?? null; }
|
|
56
|
+
|
|
29
57
|
// A bar counts when it is rendered and ends near the bottom of the viewport –
|
|
30
58
|
// the app's own bar, fixed, sticky or floating. A tab bar shown as a preview
|
|
31
59
|
// somewhere in a page does not lift the toast.
|
|
@@ -40,16 +68,83 @@ class NkToast extends NkElement {
|
|
|
40
68
|
this._box.style.bottom = rect ? `${Math.round(innerHeight - rect.top + 12)}px` : '';
|
|
41
69
|
}
|
|
42
70
|
|
|
43
|
-
/**
|
|
44
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Shows `message` (or the slotted content when omitted) and hides it again
|
|
73
|
+
* after `duration` ms – the attribute's, else 2200; with an `action`
|
|
74
|
+
* ({ label, value }) it stays until the action or the × is used.
|
|
75
|
+
*/
|
|
76
|
+
show(message, { duration, action } = {}) {
|
|
45
77
|
if (message !== undefined) this._msg.textContent = message;
|
|
78
|
+
this._shown = (this._shown ?? 0) + 1;
|
|
79
|
+
this._act = action ? { label: String(action.label ?? action.value ?? ''), value: action.value ?? action.label } : null;
|
|
80
|
+
const attr = this.getAttribute('duration'), set = attr !== null && attr.trim() !== '' && Number.isFinite(Number(attr));
|
|
81
|
+
// Number(null) is 0: without the attribute a toast used to stay for good.
|
|
82
|
+
this._ms = duration ?? (this._act || this._slotted() ? 0 : set ? Number(attr) : 2200);
|
|
83
|
+
if (this.getBoolAttr('open')) this._sync(); else this.setBoolAttr('open', true);
|
|
84
|
+
this._arm();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
close() {
|
|
46
88
|
clearTimeout(this._timer);
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
if (
|
|
89
|
+
// The focus leaves with the toast, back to where it came from.
|
|
90
|
+
const active = deepActiveElement();
|
|
91
|
+
if (active && containsDeep(this, active)) {
|
|
92
|
+
this._from?.isConnected && this._from.focus({ preventScroll: true });
|
|
93
|
+
if (containsDeep(this, deepActiveElement())) active.blur();
|
|
94
|
+
}
|
|
95
|
+
this._from = null;
|
|
96
|
+
this.setBoolAttr('open', false);
|
|
50
97
|
}
|
|
51
98
|
|
|
52
|
-
|
|
99
|
+
_arm() {
|
|
100
|
+
clearTimeout(this._timer);
|
|
101
|
+
if (this._ms > 0 && !this._held && this.getBoolAttr('open')) this._timer = setTimeout(() => this.close(), this._ms);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
_take(act) {
|
|
105
|
+
if (!act) return;
|
|
106
|
+
// A handler that shows the next message – "Restored" – keeps it open.
|
|
107
|
+
const shown = this._shown;
|
|
108
|
+
if (this.emit('nk-action', { action: act.value, value: act.value, label: act.label }) && this._shown === shown) this.close();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
setupEvents() {
|
|
112
|
+
this._onClick = (e) => {
|
|
113
|
+
const path = e.composedPath();
|
|
114
|
+
if (path.includes(this._close)) { this.close(); return; }
|
|
115
|
+
if (path.includes(this._action)) { this._take(this._act); return; }
|
|
116
|
+
const slotted = this._slotted();
|
|
117
|
+
if (slotted && path.includes(slotted)) this._take({ value: slotted.value || slotted.getAttribute('value') || slotted.textContent.trim(), label: slotted.textContent.trim() });
|
|
118
|
+
};
|
|
119
|
+
// While the pointer or the focus is on it, a toast with a duration waits.
|
|
120
|
+
this._onHold = (e) => {
|
|
121
|
+
if (e.type === 'focusin' && !this._from) this._from = e.relatedTarget ?? null;
|
|
122
|
+
this._held = true; clearTimeout(this._timer);
|
|
123
|
+
};
|
|
124
|
+
this._onRelease = (e) => {
|
|
125
|
+
if (e.relatedTarget && containsDeep(this, e.relatedTarget)) return;
|
|
126
|
+
this._held = false; this._arm();
|
|
127
|
+
};
|
|
128
|
+
this._onKey = (e) => { if (e.key === 'Escape') { e.stopPropagation(); this.close(); } };
|
|
129
|
+
this._onSlot = () => this._sync();
|
|
130
|
+
this._box.addEventListener('click', this._onClick);
|
|
131
|
+
this._box.addEventListener('pointerover', this._onHold);
|
|
132
|
+
this._box.addEventListener('pointerout', this._onRelease);
|
|
133
|
+
this._box.addEventListener('focusin', this._onHold);
|
|
134
|
+
this._box.addEventListener('focusout', this._onRelease);
|
|
135
|
+
this._box.addEventListener('keydown', this._onKey);
|
|
136
|
+
this._actionSlot.addEventListener('slotchange', this._onSlot);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
teardownEvents() {
|
|
140
|
+
this._box?.removeEventListener('click', this._onClick);
|
|
141
|
+
this._box?.removeEventListener('pointerover', this._onHold);
|
|
142
|
+
this._box?.removeEventListener('pointerout', this._onRelease);
|
|
143
|
+
this._box?.removeEventListener('focusin', this._onHold);
|
|
144
|
+
this._box?.removeEventListener('focusout', this._onRelease);
|
|
145
|
+
this._box?.removeEventListener('keydown', this._onKey);
|
|
146
|
+
this._actionSlot?.removeEventListener('slotchange', this._onSlot);
|
|
147
|
+
}
|
|
53
148
|
|
|
54
149
|
onAttributeChanged(name) {
|
|
55
150
|
this._sync();
|
|
@@ -61,7 +156,8 @@ class NkToast extends NkElement {
|
|
|
61
156
|
get open() { return this.getBoolAttr('open'); }
|
|
62
157
|
set open(v) { this.setBoolAttr('open', v); }
|
|
63
158
|
get message() { return this._msg?.textContent ?? ''; }
|
|
64
|
-
set message(v) { if (this._msg) this._msg.textContent = v; }
|
|
159
|
+
set message(v) { if (this._msg) { this._msg.textContent = v; this._sync(); } }
|
|
160
|
+
onStringsChanged() { if (this._box) this._sync(); }
|
|
65
161
|
}
|
|
66
162
|
|
|
67
163
|
customElements.define('nk-toast', NkToast);
|
|
@@ -13,8 +13,12 @@ import { NkElement } from '../../base.js';
|
|
|
13
13
|
// stylesheet hides it above 860px – there the sidebar is the navigation –
|
|
14
14
|
// and shows it below; `always` shows it at every width (previews, phone
|
|
15
15
|
// frames), `fixed` pins it to the viewport (standalone PWAs) with a spacer
|
|
16
|
-
// holding its place, `floating` makes it a capsule.
|
|
17
|
-
// `drawer` item opens the sidebar instead of
|
|
16
|
+
// holding its place, `floating` makes it a capsule. The item whose value is
|
|
17
|
+
// the bar's `value` is `active`; a `drawer` item opens the sidebar instead of
|
|
18
|
+
// becoming active when clicked. A value no item has – a page without a tab
|
|
19
|
+
// of its own, opened from the drawer – marks the drawer item, as iOS marks
|
|
20
|
+
// "More" (1.18.0); without one no item is active. Without a value the item
|
|
21
|
+
// marked `active`, else the first, is the one.
|
|
18
22
|
class NkTabBar extends NkElement {
|
|
19
23
|
static get observedAttributes() { return ['value', 'always', 'fixed', 'floating', 'label']; }
|
|
20
24
|
|
|
@@ -45,16 +49,18 @@ class NkTabBar extends NkElement {
|
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
_sync() {
|
|
48
|
-
const items = this.items.filter(i => !i.hasAttribute('drawer'));
|
|
52
|
+
const items = this.items.filter(i => !i.hasAttribute('drawer')), drawers = this.items.filter(i => i.hasAttribute('drawer'));
|
|
49
53
|
if (!items.length) return;
|
|
50
54
|
let value = this.getAttribute('value');
|
|
51
|
-
if (value === null
|
|
55
|
+
if (value === null) {
|
|
52
56
|
const preset = items.find(i => i.hasAttribute('active')) || items[0];
|
|
53
57
|
value = this._valueOf(preset);
|
|
54
58
|
}
|
|
59
|
+
const known = items.some(i => this._valueOf(i) === value);
|
|
55
60
|
for (const item of items) {
|
|
56
61
|
if (this._valueOf(item) === value) item.setAttribute('active', ''); else item.removeAttribute('active');
|
|
57
62
|
}
|
|
63
|
+
drawers.forEach((item, n) => { if (!known && n === 0) item.setAttribute('active', ''); else item.removeAttribute('active'); });
|
|
58
64
|
this._value = value;
|
|
59
65
|
}
|
|
60
66
|
|
package/src/util/focus.js
CHANGED
|
@@ -44,6 +44,12 @@ export function deepActiveElement() {
|
|
|
44
44
|
return el;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/** Whether `node` lies inside `root`, across shadow roots – Node.contains() stops at them. */
|
|
48
|
+
export function containsDeep(root, node) {
|
|
49
|
+
for (let n = node; n; n = n.parentNode ?? n.host) if (n === root) return true;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
47
53
|
/** The first tabbable element inside a light-DOM subtree, looking into shadow roots. */
|
|
48
54
|
export function firstFocusable(root) {
|
|
49
55
|
const SEL = 'a[href], button:not([disabled]), input:not([disabled]):not([type=hidden]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]), [contenteditable="true"], [contenteditable="plaintext-only"]';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Escape closes the layer that opened last (NotionKit Elements 1.19.0). The
|
|
2
|
+
// overlays – modal, sheet, dialog, side peek, palette, popover, a floating
|
|
3
|
+
// menu or date picker – join this stack while they are open, and one
|
|
4
|
+
// listener on the document, in the capture phase, hands Escape to the top
|
|
5
|
+
// of it only. Each used to listen for itself: a date picker opened from a
|
|
6
|
+
// dialog and the dialog both listened on the document in the same phase,
|
|
7
|
+
// where stopPropagation() does not part them, so one Escape closed both.
|
|
8
|
+
const stack = [];
|
|
9
|
+
let listening = false;
|
|
10
|
+
|
|
11
|
+
function onKey(e) {
|
|
12
|
+
if (e.key !== 'Escape') return;
|
|
13
|
+
// Escape with the focus in a toast is the toast's, not the dialog's below.
|
|
14
|
+
if (e.composedPath().some(n => n.localName === 'nk-toast')) return;
|
|
15
|
+
for (let i = stack.length - 1; i >= 0; i--) if (!stack[i].el.isConnected) stack.splice(i, 1);
|
|
16
|
+
const top = stack.at(-1);
|
|
17
|
+
if (!top) return;
|
|
18
|
+
e.stopPropagation();
|
|
19
|
+
top.close();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** `el` is open on top of the others; Escape calls `close` until closeLayer(el). */
|
|
23
|
+
export function openLayer(el, close) {
|
|
24
|
+
closeLayer(el);
|
|
25
|
+
stack.push({ el, close });
|
|
26
|
+
if (!listening && typeof document !== 'undefined') { document.addEventListener('keydown', onKey, true); listening = true; }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** `el` is closed or gone. */
|
|
30
|
+
export function closeLayer(el) {
|
|
31
|
+
const i = stack.findIndex(layer => layer.el === el);
|
|
32
|
+
if (i >= 0) stack.splice(i, 1);
|
|
33
|
+
}
|