@jungherz-de/notionkit-elements 1.18.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 +15 -0
- package/README.md +7 -7
- package/SKILL.md +33 -30
- package/dist/components/nk-calendar.js +6 -5
- package/dist/components/nk-cmdk.js +7 -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-sheet.js +5 -5
- package/dist/components/nk-sidebar.js +1 -1
- 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 +192 -47
- package/dist/notionkit-elements.esm.min.js +1 -1
- package/dist/notionkit-elements.esm.min.js.map +1 -1
- package/dist/notionkit-elements.js +192 -47
- package/dist/notionkit-elements.min.js +1 -1
- package/package.json +3 -3
- package/src/components/forms/nk-calendar.js +5 -4
- 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/util/focus.js +6 -0
- package/src/util/layers.js +33 -0
|
@@ -1,5 +1,6 @@
|
|
|
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-
|
|
2
|
+
import { d as deepActiveElement, l as lockScroll, i as inertOutside, f as firstFocusable, u as unlockScroll } from './shared/focus-D55JGjRA.js';
|
|
3
|
+
import { o as openLayer, c as closeLayer } from './shared/layers-D9YYYA5g.js';
|
|
3
4
|
import '@jungherz-de/notionkit/notionkit-styles.js';
|
|
4
5
|
|
|
5
6
|
// <nk-sheet id="more" title="More">
|
|
@@ -53,8 +54,10 @@ class NkSheet extends NkElement {
|
|
|
53
54
|
this._returnFocus = deepActiveElement();
|
|
54
55
|
lockScroll();
|
|
55
56
|
this._undoInert = inertOutside(this);
|
|
57
|
+
openLayer(this, () => this.close());
|
|
56
58
|
requestAnimationFrame(() => (firstFocusable(this) || this._box).focus({ preventScroll: true }));
|
|
57
59
|
} else {
|
|
60
|
+
closeLayer(this);
|
|
58
61
|
unlockScroll();
|
|
59
62
|
this._undoInert?.(); this._undoInert = null;
|
|
60
63
|
const back = this._returnFocus;
|
|
@@ -65,15 +68,12 @@ class NkSheet extends NkElement {
|
|
|
65
68
|
|
|
66
69
|
setupEvents() {
|
|
67
70
|
this._onBackdrop = (e) => { if (e.target === this._backdrop) this.close(); };
|
|
68
|
-
this._onKey = (e) => { if (e.key === 'Escape' && this.getBoolAttr('open')) { e.stopPropagation(); this.close(); } };
|
|
69
71
|
this._backdrop.addEventListener('click', this._onBackdrop);
|
|
70
|
-
document.addEventListener('keydown', this._onKey);
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
teardownEvents() {
|
|
74
75
|
this._backdrop?.removeEventListener('click', this._onBackdrop);
|
|
75
|
-
|
|
76
|
-
if (this._wasOpen) { unlockScroll(); this._undoInert?.(); this._undoInert = null; this._wasOpen = false; }
|
|
76
|
+
if (this._wasOpen) { closeLayer(this); unlockScroll(); this._undoInert?.(); this._undoInert = null; this._wasOpen = false; }
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
onAttributeChanged(name, oldValue, value) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NkElement } from './base.js';
|
|
2
|
-
import { d as deepActiveElement } from './shared/focus-
|
|
2
|
+
import { d as deepActiveElement } from './shared/focus-D55JGjRA.js';
|
|
3
3
|
import '@jungherz-de/notionkit/notionkit-styles.js';
|
|
4
4
|
|
|
5
5
|
// <nk-sidebar slot="sidebar">
|
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import { NkElement } from './base.js';
|
|
2
|
+
import { d as deepActiveElement, c as containsDeep } from './shared/focus-D55JGjRA.js';
|
|
2
3
|
import '@jungherz-de/notionkit/notionkit-styles.js';
|
|
3
4
|
|
|
4
5
|
// <nk-toast id="toast"></nk-toast> … toast.show('Saved');
|
|
5
|
-
//
|
|
6
|
+
// toast.show('Moved to trash', { action: { label: 'Undo', value: 'undo' } });
|
|
7
|
+
// → <div class="nk-toast show">✓ Moved to trash<button class="t-action">Undo</button><button class="t-close">×</button></div>
|
|
6
8
|
// One node, bottom-centred, auto-hides after `duration` ms (default 2200).
|
|
7
9
|
// Above a tab bar at the bottom of the screen it rises 12px over the bar:
|
|
8
10
|
// notionkit.css does that for class markup with :has(), which cannot see
|
|
9
11
|
// into shadow roots, so the element measures the bar when it opens.
|
|
12
|
+
// An action (1.19.0) – from show() or a <button slot="action"> – makes it
|
|
13
|
+
// Notion's "Moved to trash · Undo": a button, and an × that closes. Such a
|
|
14
|
+
// toast stays until one of them is used, unless show() names a duration,
|
|
15
|
+
// and waits while the pointer or the focus is on it. The action fires
|
|
16
|
+
// nk-action { action, value, label } and, unless cancelled, closes it;
|
|
17
|
+
// Escape closes it while the focus is inside.
|
|
18
|
+
const CLOSE_ICON = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12"/></svg>';
|
|
19
|
+
|
|
10
20
|
class NkToast extends NkElement {
|
|
11
21
|
static get observedAttributes() { return ['open', 'duration', 'icon']; }
|
|
12
22
|
|
|
@@ -14,19 +24,37 @@ class NkToast extends NkElement {
|
|
|
14
24
|
this._box = this.createElement('div', ['nk-toast'], { role: 'status', 'aria-live': 'polite' });
|
|
15
25
|
this._icon = document.createElement('span');
|
|
16
26
|
this._msg = document.createElement('span');
|
|
17
|
-
this.
|
|
27
|
+
this._action = this.createElement('button', ['t-action'], { type: 'button' });
|
|
28
|
+
this._actionSlot = this.createElement('slot', [], { name: 'action' });
|
|
29
|
+
this._close = this.createElement('button', ['t-close'], { type: 'button' });
|
|
30
|
+
this._close.innerHTML = CLOSE_ICON;
|
|
31
|
+
this._box.append(this._icon, this._msg, document.createElement('slot'), this._actionSlot);
|
|
18
32
|
this._wrapper.appendChild(this._box);
|
|
19
33
|
this._sync();
|
|
20
34
|
}
|
|
21
35
|
|
|
22
36
|
_sync() {
|
|
23
|
-
|
|
24
|
-
|
|
37
|
+
const open = this.getBoolAttr('open');
|
|
38
|
+
if (open) this._lift();
|
|
39
|
+
this._box.classList.toggle('show', open);
|
|
25
40
|
const icon = this.getAttribute('icon') ?? '✓';
|
|
26
41
|
this._icon.textContent = icon;
|
|
27
42
|
this._icon.style.display = icon ? '' : 'none';
|
|
43
|
+
// An empty message takes no place – with slotted text it added a second gap.
|
|
44
|
+
this._msg.style.display = this._msg.textContent ? '' : 'none';
|
|
45
|
+
// The action from show() and the × sit in the tree only when there is an
|
|
46
|
+
// action; a closed toast keeps them for its fade, out of the tab order.
|
|
47
|
+
const own = this._act, slotted = this._slotted();
|
|
48
|
+
if (own) { this._action.textContent = own.label; if (!this._action.isConnected) this._actionSlot.before(this._action); }
|
|
49
|
+
else this._action.remove();
|
|
50
|
+
if (own || slotted) { if (!this._close.isConnected) this._box.appendChild(this._close); }
|
|
51
|
+
else this._close.remove();
|
|
52
|
+
this._close.setAttribute('aria-label', this.str('close'));
|
|
53
|
+
for (const b of [this._action, this._close]) open ? b.removeAttribute('tabindex') : b.setAttribute('tabindex', '-1');
|
|
28
54
|
}
|
|
29
55
|
|
|
56
|
+
_slotted() { return this._actionSlot?.assignedElements()[0] ?? null; }
|
|
57
|
+
|
|
30
58
|
// A bar counts when it is rendered and ends near the bottom of the viewport –
|
|
31
59
|
// the app's own bar, fixed, sticky or floating. A tab bar shown as a preview
|
|
32
60
|
// somewhere in a page does not lift the toast.
|
|
@@ -41,16 +69,83 @@ class NkToast extends NkElement {
|
|
|
41
69
|
this._box.style.bottom = rect ? `${Math.round(innerHeight - rect.top + 12)}px` : '';
|
|
42
70
|
}
|
|
43
71
|
|
|
44
|
-
/**
|
|
45
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Shows `message` (or the slotted content when omitted) and hides it again
|
|
74
|
+
* after `duration` ms – the attribute's, else 2200; with an `action`
|
|
75
|
+
* ({ label, value }) it stays until the action or the × is used.
|
|
76
|
+
*/
|
|
77
|
+
show(message, { duration, action } = {}) {
|
|
46
78
|
if (message !== undefined) this._msg.textContent = message;
|
|
79
|
+
this._shown = (this._shown ?? 0) + 1;
|
|
80
|
+
this._act = action ? { label: String(action.label ?? action.value ?? ''), value: action.value ?? action.label } : null;
|
|
81
|
+
const attr = this.getAttribute('duration'), set = attr !== null && attr.trim() !== '' && Number.isFinite(Number(attr));
|
|
82
|
+
// Number(null) is 0: without the attribute a toast used to stay for good.
|
|
83
|
+
this._ms = duration ?? (this._act || this._slotted() ? 0 : set ? Number(attr) : 2200);
|
|
84
|
+
if (this.getBoolAttr('open')) this._sync(); else this.setBoolAttr('open', true);
|
|
85
|
+
this._arm();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
close() {
|
|
47
89
|
clearTimeout(this._timer);
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
90
|
+
// The focus leaves with the toast, back to where it came from.
|
|
91
|
+
const active = deepActiveElement();
|
|
92
|
+
if (active && containsDeep(this, active)) {
|
|
93
|
+
this._from?.isConnected && this._from.focus({ preventScroll: true });
|
|
94
|
+
if (containsDeep(this, deepActiveElement())) active.blur();
|
|
95
|
+
}
|
|
96
|
+
this._from = null;
|
|
97
|
+
this.setBoolAttr('open', false);
|
|
51
98
|
}
|
|
52
99
|
|
|
53
|
-
|
|
100
|
+
_arm() {
|
|
101
|
+
clearTimeout(this._timer);
|
|
102
|
+
if (this._ms > 0 && !this._held && this.getBoolAttr('open')) this._timer = setTimeout(() => this.close(), this._ms);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
_take(act) {
|
|
106
|
+
if (!act) return;
|
|
107
|
+
// A handler that shows the next message – "Restored" – keeps it open.
|
|
108
|
+
const shown = this._shown;
|
|
109
|
+
if (this.emit('nk-action', { action: act.value, value: act.value, label: act.label }) && this._shown === shown) this.close();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
setupEvents() {
|
|
113
|
+
this._onClick = (e) => {
|
|
114
|
+
const path = e.composedPath();
|
|
115
|
+
if (path.includes(this._close)) { this.close(); return; }
|
|
116
|
+
if (path.includes(this._action)) { this._take(this._act); return; }
|
|
117
|
+
const slotted = this._slotted();
|
|
118
|
+
if (slotted && path.includes(slotted)) this._take({ value: slotted.value || slotted.getAttribute('value') || slotted.textContent.trim(), label: slotted.textContent.trim() });
|
|
119
|
+
};
|
|
120
|
+
// While the pointer or the focus is on it, a toast with a duration waits.
|
|
121
|
+
this._onHold = (e) => {
|
|
122
|
+
if (e.type === 'focusin' && !this._from) this._from = e.relatedTarget ?? null;
|
|
123
|
+
this._held = true; clearTimeout(this._timer);
|
|
124
|
+
};
|
|
125
|
+
this._onRelease = (e) => {
|
|
126
|
+
if (e.relatedTarget && containsDeep(this, e.relatedTarget)) return;
|
|
127
|
+
this._held = false; this._arm();
|
|
128
|
+
};
|
|
129
|
+
this._onKey = (e) => { if (e.key === 'Escape') { e.stopPropagation(); this.close(); } };
|
|
130
|
+
this._onSlot = () => this._sync();
|
|
131
|
+
this._box.addEventListener('click', this._onClick);
|
|
132
|
+
this._box.addEventListener('pointerover', this._onHold);
|
|
133
|
+
this._box.addEventListener('pointerout', this._onRelease);
|
|
134
|
+
this._box.addEventListener('focusin', this._onHold);
|
|
135
|
+
this._box.addEventListener('focusout', this._onRelease);
|
|
136
|
+
this._box.addEventListener('keydown', this._onKey);
|
|
137
|
+
this._actionSlot.addEventListener('slotchange', this._onSlot);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
teardownEvents() {
|
|
141
|
+
this._box?.removeEventListener('click', this._onClick);
|
|
142
|
+
this._box?.removeEventListener('pointerover', this._onHold);
|
|
143
|
+
this._box?.removeEventListener('pointerout', this._onRelease);
|
|
144
|
+
this._box?.removeEventListener('focusin', this._onHold);
|
|
145
|
+
this._box?.removeEventListener('focusout', this._onRelease);
|
|
146
|
+
this._box?.removeEventListener('keydown', this._onKey);
|
|
147
|
+
this._actionSlot?.removeEventListener('slotchange', this._onSlot);
|
|
148
|
+
}
|
|
54
149
|
|
|
55
150
|
onAttributeChanged(name) {
|
|
56
151
|
this._sync();
|
|
@@ -62,7 +157,8 @@ class NkToast extends NkElement {
|
|
|
62
157
|
get open() { return this.getBoolAttr('open'); }
|
|
63
158
|
set open(v) { this.setBoolAttr('open', v); }
|
|
64
159
|
get message() { return this._msg?.textContent ?? ''; }
|
|
65
|
-
set message(v) { if (this._msg) this._msg.textContent = v; }
|
|
160
|
+
set message(v) { if (this._msg) { this._msg.textContent = v; this._sync(); } }
|
|
161
|
+
onStringsChanged() { if (this._box) this._sync(); }
|
|
66
162
|
}
|
|
67
163
|
|
|
68
164
|
customElements.define('nk-toast', NkToast);
|
|
@@ -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-
|
|
3
|
+
import { d as deepActiveElement } from './shared/focus-D55JGjRA.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]
|
|
@@ -44,6 +44,12 @@ 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
|
+
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
|
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"]';
|
|
@@ -57,4 +63,4 @@ function firstFocusable(root) {
|
|
|
57
63
|
return walk(root);
|
|
58
64
|
}
|
|
59
65
|
|
|
60
|
-
export { deepActiveElement as d, firstFocusable as f, inertOutside as i, lockScroll as l, unlockScroll as u };
|
|
66
|
+
export { containsDeep as c, deepActiveElement as d, firstFocusable as f, inertOutside as i, lockScroll as l, unlockScroll as u };
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
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
|
+
function closeLayer(el) {
|
|
31
|
+
const i = stack.findIndex(layer => layer.el === el);
|
|
32
|
+
if (i >= 0) stack.splice(i, 1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { closeLayer as c, openLayer as o };
|