tablefilter 0.7.8 → 0.7.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tablefilter",
3
- "version": "0.7.8",
3
+ "version": "0.7.9",
4
4
  "description": "A Javascript library making HTML tables filterable and a bit more",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -37,7 +37,7 @@
37
37
  "build:demos": "grunt build-demos",
38
38
  "build:test": "node scripts/with-legacy-openssl.js webpack --colors --config webpack.test.config.js",
39
39
  "build:vite": "vite build",
40
- "serve:vite": "vite --port 8080 --strictPort",
40
+ "serve:vite": "npm run build:vite && vite --port 8080 --strictPort",
41
41
  "server": "node scripts/with-legacy-openssl.js webpack-dev-server --colors --hot --config ./webpack.dev.config.js",
42
42
  "test": "grunt test",
43
43
  "test:playwright": "playwright test",
@@ -0,0 +1,386 @@
1
+ /**
2
+ * `<tf-help>`: help instructions in a native modal `<dialog>`.
3
+ *
4
+ * Shadow root: `slot[name="trigger"]` (a slotted control is the
5
+ * control) and `<button part="trigger">` for the element-rendered
6
+ * case; a `<dialog part="dialog">` carrying
7
+ * `<div part="instructions">` with `slot[name="instructions"]` and a
8
+ * `part="instructions-text"` text part; a `<button part="close">` with
9
+ * `part="close-text"`.
10
+ *
11
+ * Attributes: `trigger-text`; `label` (trigger `aria-label`);
12
+ * `instructions-text`; `dialog-label` (dialog accessible name, falling
13
+ * back to `aria-labelledby` on the trigger); `close-text` (close
14
+ * control text and accessible name; the control is unnamed when the
15
+ * attribute is absent); `open` (reflects the dialog state); `disabled`
16
+ * (blocks trigger activation). Empty or absent attributes render
17
+ * empty.
18
+ *
19
+ * The dialog opens with `showModal()`, which traps focus inside it and
20
+ * closes it on `Escape`. A user dismissal (`Escape`, backdrop,
21
+ * close control, trigger toggle) returns focus
22
+ * to the trigger control: the slotted trigger when one is assigned, the
23
+ * element button otherwise, the host when slotted markup hides it. A
24
+ * programmatic close keeps the platform focus restore. Setting `open`
25
+ * opens the dialog, removing it closes the dialog, and `open-change`
26
+ * reports the state with `detail: {open}`.
27
+ *
28
+ * The element ships no class or presentational style; theme it via
29
+ * `::part()` and `--tf-*` custom properties. Importing this module
30
+ * registers the element.
31
+ */
32
+
33
+ const TRIGGER_ID = 'trigger';
34
+ const DIALOG_ID = 'dialog';
35
+
36
+ // Controls and widgets that take focus; used to pick the focus target
37
+ // inside slotted trigger markup. Matches FOCUSABLE_TRIGGER in
38
+ // modules/help.js.
39
+ const FOCUSABLE = 'button:not([disabled]), a[href], ' +
40
+ 'input:not([disabled]), select:not([disabled]), ' +
41
+ 'textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
42
+
43
+ // Cloned into every instance's shadow root. Structure and `part` hooks
44
+ // only: no class or presentational style is baked in.
45
+ const TEMPLATE = document.createElement('template');
46
+ TEMPLATE.innerHTML = `
47
+ <slot name="trigger" part="trigger-slot"></slot>
48
+ <button type="button" part="trigger" id="${TRIGGER_ID}"
49
+ aria-haspopup="dialog" aria-expanded="false"
50
+ aria-controls="${DIALOG_ID}">
51
+ <span part="trigger-text"></span>
52
+ </button>
53
+ <dialog part="dialog" id="${DIALOG_ID}">
54
+ <div part="instructions">
55
+ <slot name="instructions"></slot>
56
+ <span part="instructions-text"></span>
57
+ </div>
58
+ <button type="button" part="close">
59
+ <span part="close-text"></span>
60
+ </button>
61
+ </dialog>
62
+ `;
63
+
64
+ export class TfHelpElement extends HTMLElement {
65
+ constructor() {
66
+ super();
67
+ this.attachShadow({ mode: 'open' });
68
+ this.shadowRoot.appendChild(TEMPLATE.content.cloneNode(true));
69
+ this._triggerSlot =
70
+ this.shadowRoot.querySelector('slot[name="trigger"]');
71
+ this._trigger = this.shadowRoot.querySelector('[part="trigger"]');
72
+ this._triggerText =
73
+ this.shadowRoot.querySelector('[part="trigger-text"]');
74
+ this._dialog = this.shadowRoot.querySelector('[part="dialog"]');
75
+ this._instructionsSlot =
76
+ this.shadowRoot.querySelector('slot[name="instructions"]');
77
+ this._instructionsText =
78
+ this.shadowRoot.querySelector('[part="instructions-text"]');
79
+ this._close = this.shadowRoot.querySelector('[part="close"]');
80
+ this._closeText =
81
+ this.shadowRoot.querySelector('[part="close-text"]');
82
+ this._reportedOpen = false;
83
+ this._userClosed = false;
84
+
85
+ this._trigger.addEventListener('click', () => this._toggle());
86
+ this._triggerSlot.addEventListener('click', () => this._toggle());
87
+ this._triggerSlot.addEventListener(
88
+ 'slotchange', () => this._syncLayout());
89
+ this._instructionsSlot.addEventListener(
90
+ 'slotchange', () => this._syncLayout());
91
+ this._dialog.addEventListener('click', (event) => {
92
+ // A backdrop click lands outside the dialog box; a click on
93
+ // the dialog's border or padding lands on the dialog
94
+ // element but inside the box.
95
+ if (event.target === this._dialog &&
96
+ !this._isInsideDialogBox(event)) {
97
+ this._dismiss();
98
+ return;
99
+ }
100
+ // A slotted element with class `close` dismisses; other
101
+ // content clicks keep the dialog open.
102
+ if (event.composedPath().some((node) =>
103
+ node.nodeType === 1 && node.classList &&
104
+ node.classList.contains('close'))) {
105
+ this._dismiss();
106
+ }
107
+ });
108
+ this._close.addEventListener('click', () => this._dismiss());
109
+ this._dialog.addEventListener(
110
+ 'cancel', () => { this._userClosed = true; });
111
+ this._dialog.addEventListener('close', () => this._onClose());
112
+ }
113
+
114
+ static get observedAttributes() {
115
+ return ['label', 'trigger-text', 'dialog-label',
116
+ 'instructions-text', 'close-text', 'open', 'disabled'];
117
+ }
118
+
119
+ connectedCallback() {
120
+ this._update();
121
+ this._applyOpen();
122
+ }
123
+
124
+ disconnectedCallback() {
125
+ if (this._dialog.open) {
126
+ this._dialog.close();
127
+ }
128
+ }
129
+
130
+ attributeChangedCallback(name) {
131
+ this._update();
132
+ if (name === 'open') {
133
+ this._applyOpen();
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Set the trigger button's accessible name, rendered as `aria-label`.
139
+ * @param {*} label Accessible name
140
+ */
141
+ setLabel(label) {
142
+ this.setAttribute('label', label == null ? '' : String(label));
143
+ }
144
+
145
+ /**
146
+ * Set the trigger text, rendered into the trigger button.
147
+ * @param {*} text Trigger text
148
+ */
149
+ setText(text) {
150
+ this.setAttribute('trigger-text',
151
+ text == null ? '' : String(text));
152
+ }
153
+
154
+ /**
155
+ * Set the dialog's accessible name.
156
+ * @param {*} label Accessible name
157
+ */
158
+ setDialogLabel(label) {
159
+ this.setAttribute('dialog-label',
160
+ label == null ? '' : String(label));
161
+ }
162
+
163
+ /**
164
+ * Set the instructions text.
165
+ * @param {*} text Instructions text
166
+ */
167
+ setInstructionsText(text) {
168
+ this.setAttribute('instructions-text',
169
+ text == null ? '' : String(text));
170
+ }
171
+
172
+ /**
173
+ * Open or close the dialog.
174
+ * @param {Boolean} open Whether the dialog is open
175
+ */
176
+ setOpen(open) {
177
+ if (open) {
178
+ this.setAttribute('open', '');
179
+ } else {
180
+ this.removeAttribute('open');
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Render attributes into the shadow parts.
186
+ */
187
+ _update() {
188
+ if (!this._trigger) {
189
+ return;
190
+ }
191
+
192
+ this._setAriaLabel(this._trigger, 'label');
193
+ this._triggerText.textContent =
194
+ this.getAttribute('trigger-text') || '';
195
+ this._instructionsText.textContent =
196
+ this.getAttribute('instructions-text') || '';
197
+ this._closeText.textContent =
198
+ this.getAttribute('close-text') || '';
199
+ this._setDialogName();
200
+
201
+ if (this.hasAttribute('disabled')) {
202
+ this._trigger.setAttribute('disabled', '');
203
+ } else {
204
+ this._trigger.removeAttribute('disabled');
205
+ }
206
+
207
+ this._syncLayout();
208
+ this._syncExpanded();
209
+ }
210
+
211
+ /**
212
+ * Show the element-rendered trigger control only when the trigger
213
+ * slot has no assigned nodes, and the instructions text part only
214
+ * when the instructions slot has none.
215
+ */
216
+ _syncLayout() {
217
+ if (!this._trigger) {
218
+ return;
219
+ }
220
+ this._trigger.hidden = this._triggerSlot
221
+ .assignedNodes({ flatten: true }).length > 0;
222
+ this._instructionsText.hidden = this._instructionsSlot
223
+ .assignedNodes({ flatten: true }).length > 0;
224
+ }
225
+
226
+ /**
227
+ * Name the dialog from its own attribute, or from the trigger.
228
+ */
229
+ _setDialogName() {
230
+ const label = this.getAttribute('dialog-label') || '';
231
+ if (label) {
232
+ this._dialog.setAttribute('aria-label', label);
233
+ this._dialog.removeAttribute('aria-labelledby');
234
+ } else {
235
+ this._dialog.removeAttribute('aria-label');
236
+ this._dialog.setAttribute('aria-labelledby', TRIGGER_ID);
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Render an attribute as `aria-label` on a shadow element, or remove
242
+ * it when the attribute is absent or empty.
243
+ * @param {Element} el Shadow element to name
244
+ * @param {String} attr Attribute holding the accessible name
245
+ */
246
+ _setAriaLabel(el, attr) {
247
+ const label = this.getAttribute(attr) || '';
248
+ if (label) {
249
+ el.setAttribute('aria-label', label);
250
+ } else {
251
+ el.removeAttribute('aria-label');
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Toggle the dialog from the trigger control.
257
+ */
258
+ _toggle() {
259
+ if (this.hasAttribute('disabled')) {
260
+ return;
261
+ }
262
+ if (this._dialog.open) {
263
+ this._userClosed = true;
264
+ }
265
+ this.setOpen(!this._dialog.open);
266
+ }
267
+
268
+ /**
269
+ * Report whether a pointer event's coordinates fall inside the
270
+ * dialog's border box.
271
+ * @param {MouseEvent} event Pointer event
272
+ */
273
+ _isInsideDialogBox(event) {
274
+ const rect = this._dialog.getBoundingClientRect();
275
+ return event.clientX >= rect.left && event.clientX <= rect.right &&
276
+ event.clientY >= rect.top && event.clientY <= rect.bottom;
277
+ }
278
+
279
+ /**
280
+ * Close the dialog from a user dismissal.
281
+ */
282
+ _dismiss() {
283
+ this._userClosed = true;
284
+ this.setOpen(false);
285
+ }
286
+
287
+ /**
288
+ * Apply the host `open` attribute to the dialog.
289
+ */
290
+ _applyOpen() {
291
+ if (!this.isConnected) {
292
+ return;
293
+ }
294
+ const open = this.hasAttribute('open');
295
+ if (open && !this._dialog.open) {
296
+ this._dialog.showModal();
297
+ } else if (!open && this._dialog.open) {
298
+ this._dialog.close();
299
+ }
300
+ this._syncExpanded();
301
+ this._reportOpen();
302
+ }
303
+
304
+ /**
305
+ * Keep `aria-expanded` in sync with the dialog state.
306
+ */
307
+ _syncExpanded() {
308
+ this._trigger.setAttribute('aria-expanded',
309
+ String(this._dialog.open));
310
+ }
311
+
312
+ /**
313
+ * Dispatch `open-change` when the dialog state changes.
314
+ */
315
+ _reportOpen() {
316
+ const open = this._dialog.open;
317
+ if (open === this._reportedOpen) {
318
+ return;
319
+ }
320
+ this._reportedOpen = open;
321
+ this.dispatchEvent(new CustomEvent('open-change', {
322
+ detail: { open },
323
+ bubbles: true,
324
+ composed: true
325
+ }));
326
+ }
327
+
328
+ /**
329
+ * Sync the host attribute when the dialog closes from the platform
330
+ * (`Escape`), the backdrop or the instructions container.
331
+ */
332
+ _onClose() {
333
+ if (this._dialog.open) {
334
+ return;
335
+ }
336
+ if (this.hasAttribute('open')) {
337
+ this.removeAttribute('open');
338
+ }
339
+ this._syncExpanded();
340
+ this._reportOpen();
341
+ const userClosed = this._userClosed;
342
+ this._userClosed = false;
343
+ if (!userClosed) {
344
+ return;
345
+ }
346
+ // The platform restores focus to the element focused at
347
+ // showModal() after the close event; the deferred call runs
348
+ // after that restoration.
349
+ setTimeout(() => this._focusTrigger());
350
+ }
351
+
352
+ /**
353
+ * Move focus to the slotted trigger control, to the element button
354
+ * when the slot has none, or to the host when slotted markup hides
355
+ * the button.
356
+ */
357
+ _focusTrigger() {
358
+ if (!this.isConnected) {
359
+ return;
360
+ }
361
+ const assigned = this._triggerSlot
362
+ .assignedNodes({ flatten: true })
363
+ .filter((node) => node.nodeType === Node.ELEMENT_NODE);
364
+ const slotted = assigned
365
+ .map((el) => el.matches(FOCUSABLE) ? el
366
+ : el.querySelector(FOCUSABLE))
367
+ .find(Boolean);
368
+ if (slotted) {
369
+ slotted.focus();
370
+ return;
371
+ }
372
+ if (this._trigger.hidden) {
373
+ this.tabIndex = -1;
374
+ this.focus();
375
+ return;
376
+ }
377
+ this._trigger.focus();
378
+ }
379
+ }
380
+
381
+ // Register once: loading this module twice (or on a page that already
382
+ // defined the element) must not throw.
383
+ if (typeof customElements !== 'undefined' &&
384
+ !customElements.get('tf-help')) {
385
+ customElements.define('tf-help', TfHelpElement);
386
+ }