@jungherz-de/glasskit-elements 1.12.0 → 1.14.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 (37) hide show
  1. package/README.md +35 -4
  2. package/dist/components/{shared/base-BoN33KPe.js → base.js} +3 -3
  3. package/dist/components/glk-accordion-item.js +1 -1
  4. package/dist/components/glk-accordion.js +1 -1
  5. package/dist/components/glk-avatar.js +1 -1
  6. package/dist/components/glk-badge.js +67 -7
  7. package/dist/components/glk-button.js +1 -1
  8. package/dist/components/glk-card.js +1 -1
  9. package/dist/components/glk-checkbox.js +1 -1
  10. package/dist/components/glk-divider.js +1 -1
  11. package/dist/components/glk-input.js +1 -1
  12. package/dist/components/glk-list-item.js +1 -1
  13. package/dist/components/glk-list.js +1 -1
  14. package/dist/components/glk-modal.js +1 -1
  15. package/dist/components/glk-nav.js +1 -1
  16. package/dist/components/glk-pill.js +1 -1
  17. package/dist/components/glk-popover.js +1 -1
  18. package/dist/components/glk-progress.js +1 -1
  19. package/dist/components/glk-radio.js +1 -1
  20. package/dist/components/glk-range.js +1 -1
  21. package/dist/components/glk-search.js +1 -1
  22. package/dist/components/glk-select.js +1 -1
  23. package/dist/components/glk-status.js +1 -1
  24. package/dist/components/glk-tab-accessory.js +1 -1
  25. package/dist/components/glk-tab-bar.js +1 -1
  26. package/dist/components/glk-tab-dock.js +1 -1
  27. package/dist/components/glk-tab-item.js +1 -1
  28. package/dist/components/glk-textarea.js +1 -1
  29. package/dist/components/glk-title.js +1 -1
  30. package/dist/components/glk-toast.js +1 -1
  31. package/dist/components/glk-toggle.js +1 -1
  32. package/dist/glasskit-elements.esm.js +69 -9
  33. package/dist/glasskit-elements.js +70 -8
  34. package/dist/glasskit-elements.min.js +1 -1
  35. package/package.json +5 -3
  36. package/src/components/content/glk-badge.js +66 -6
  37. package/src/index.js +6 -0
@@ -5,18 +5,70 @@ const VARIANTS = ['primary', 'success', 'error'];
5
5
  class GlkBadge extends GlkElement {
6
6
  static get displayInline() { return true; }
7
7
  static get observedAttributes() {
8
- return ['variant'];
8
+ return ['variant', 'interactive', 'selected'];
9
9
  }
10
10
 
11
11
  render() {
12
- this._badge = this.createElement('span', this._computeClasses());
13
- this._badge.appendChild(document.createElement('slot'));
14
- this._wrapper.appendChild(this._badge);
12
+ // One slot for the lifetime of the element — it moves between hosts when
13
+ // the badge turns into a chip and back, so the light-DOM children never
14
+ // have to be re-assigned.
15
+ this._slot = document.createElement('slot');
16
+ this._mount();
17
+ }
18
+
19
+ // A chip is a real <button>, so the keyboard, the focus ring and the
20
+ // aria-pressed announcement come for free; a plain badge stays a <span>.
21
+ // Toggling `interactive` therefore swaps the element, not just a class.
22
+ _mount() {
23
+ const interactive = this.getBoolAttr('interactive');
24
+ const el = this.createElement(interactive ? 'button' : 'span', this._computeClasses());
25
+ if (interactive) {
26
+ el.type = 'button';
27
+ el.setAttribute('aria-pressed', String(this.getBoolAttr('selected')));
28
+ }
29
+ el.appendChild(this._slot);
30
+
31
+ if (this._badge) {
32
+ this._badge.removeEventListener('click', this._onClick);
33
+ this._badge.replaceWith(el);
34
+ } else {
35
+ this._wrapper.appendChild(el);
36
+ }
37
+ this._badge = el;
38
+ // setupEvents() has not run yet on the very first mount; it attaches then.
39
+ if (this._onClick) el.addEventListener('click', this._onClick);
40
+ }
41
+
42
+ setupEvents() {
43
+ // Forward clicks as glk-click — only for chips, like glk-list-item.
44
+ this._onClick = () => {
45
+ if (this.getBoolAttr('interactive')) {
46
+ this.emit('glk-click');
47
+ }
48
+ };
49
+ this._badge.addEventListener('click', this._onClick);
50
+ }
51
+
52
+ teardownEvents() {
53
+ this._badge?.removeEventListener('click', this._onClick);
54
+ // Forget the handler, or a swap while disconnected would re-attach it and
55
+ // the next connect would add a second one — two glk-click per click.
56
+ this._onClick = null;
15
57
  }
16
58
 
17
59
  onAttributeChanged(name) {
18
- if (name === 'variant' && this._badge) {
19
- this._badge.className = this._computeClasses().join(' ');
60
+ if (!this._badge) return;
61
+ switch (name) {
62
+ case 'interactive':
63
+ this._mount();
64
+ break;
65
+ case 'variant':
66
+ case 'selected':
67
+ this._badge.className = this._computeClasses().join(' ');
68
+ if (this._badge.tagName === 'BUTTON') {
69
+ this._badge.setAttribute('aria-pressed', String(this.getBoolAttr('selected')));
70
+ }
71
+ break;
20
72
  }
21
73
  }
22
74
 
@@ -26,11 +78,19 @@ class GlkBadge extends GlkElement {
26
78
  if (variant && VARIANTS.includes(variant)) {
27
79
  classes.push(`glass-badge--${variant}`);
28
80
  }
81
+ if (this.getBoolAttr('interactive')) classes.push('glass-badge--interactive');
82
+ if (this.getBoolAttr('selected')) classes.push('glass-badge--selected');
29
83
  return classes;
30
84
  }
31
85
 
32
86
  get variant() { return this.getAttribute('variant'); }
33
87
  set variant(v) { this.setAttribute('variant', v); }
88
+
89
+ get interactive() { return this.getBoolAttr('interactive'); }
90
+ set interactive(v) { this.setBoolAttr('interactive', v); }
91
+
92
+ get selected() { return this.getBoolAttr('selected'); }
93
+ set selected(v) { this.setBoolAttr('selected', v); }
34
94
  }
35
95
 
36
96
  customElements.define('glk-badge', GlkBadge);
package/src/index.js CHANGED
@@ -1,6 +1,12 @@
1
1
  // GlassKit Elements — Web Components for GlassKit CSS
2
2
  // https://github.com/JUNGHERZ/GlassKit
3
3
 
4
+ // Base classes — so a project can build its own elements on the same
5
+ // lifecycle, stylesheet adoption, theme sync and emit(). Also reachable as
6
+ // GlassKitElements.GlkElement from the IIFE bundle, and via the stable
7
+ // subpath '@jungherz-de/glasskit-elements/base.js' for per-component setups.
8
+ export { GlkElement, GlkFormElement } from './base.js';
9
+
4
10
  // Navigation
5
11
  export { GlkNav } from './components/navigation/glk-nav.js';
6
12
  export { GlkPill } from './components/navigation/glk-pill.js';