@jungherz-de/glasskit-elements 1.20.0 → 1.21.1

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": "@jungherz-de/glasskit-elements",
3
- "version": "1.20.0",
3
+ "version": "1.21.1",
4
4
  "description": "Vanilla JavaScript Web Components wrapping the GlassKit CSS glassmorphism library",
5
5
  "license": "MIT",
6
6
  "homepage": "https://glasskit-elements.jungherz.com",
@@ -49,10 +49,10 @@
49
49
  "vanilla-js"
50
50
  ],
51
51
  "peerDependencies": {
52
- "@jungherz-de/glasskit": ">=1.20.0"
52
+ "@jungherz-de/glasskit": ">=1.21.1"
53
53
  },
54
54
  "devDependencies": {
55
- "@jungherz-de/glasskit": "^1.20.0",
55
+ "@jungherz-de/glasskit": "^1.21.1",
56
56
  "@rollup/plugin-node-resolve": "^15.0.0",
57
57
  "@rollup/plugin-terser": "^0.4.0",
58
58
  "rollup": "^4.0.0"
package/src/base.js CHANGED
@@ -53,9 +53,16 @@ function syncDefaultTokens() {
53
53
 
54
54
  injectDefaultTokens();
55
55
 
56
- // ── Global Theme Sync ──
57
- // Single MutationObserver that watches data-theme on <html>
56
+ // ── Global Theme & Density Sync ──
57
+ // Single MutationObserver that watches data-theme and data-density on <html>
58
58
  // and notifies all GlkElement instances.
59
+ //
60
+ // The density tokens themselves arrive from <html> by inheritance, like every
61
+ // other token: GlassKit's [data-density="compact"] preset is part of
62
+ // tokensCss, not of the components sheet. The wrapper mirrors the attribute
63
+ // anyway, as it mirrors data-theme — a rule keyed on it (a component rule, a
64
+ // subclass's hostStyles) can match inside a shadow root only on an element
65
+ // there.
59
66
 
60
67
  const instances = new Set();
61
68
 
@@ -63,6 +70,11 @@ function getCurrentTheme() {
63
70
  return document.documentElement.getAttribute('data-theme') || 'dark';
64
71
  }
65
72
 
73
+ /** The density on <html>, or null — there is no default to fall back to. */
74
+ function getCurrentDensity() {
75
+ return document.documentElement.getAttribute('data-density');
76
+ }
77
+
66
78
  function syncAllThemes() {
67
79
  const theme = getCurrentTheme();
68
80
  for (const instance of instances) {
@@ -70,14 +82,22 @@ function syncAllThemes() {
70
82
  }
71
83
  }
72
84
 
85
+ function syncAllDensities() {
86
+ const density = getCurrentDensity();
87
+ for (const instance of instances) {
88
+ instance._syncDensity(density);
89
+ }
90
+ }
91
+
73
92
  if (typeof window !== 'undefined' && typeof MutationObserver !== 'undefined') {
74
93
  const observer = new MutationObserver(records => {
75
94
  if (records.some(r => r.attributeName === 'data-theme')) syncAllThemes();
95
+ if (records.some(r => r.attributeName === 'data-density')) syncAllDensities();
76
96
  if (records.some(r => r.attributeName === DEFAULTS_ATTR)) syncDefaultTokens();
77
97
  });
78
98
  observer.observe(document.documentElement, {
79
99
  attributes: true,
80
- attributeFilter: ['data-theme', DEFAULTS_ATTR]
100
+ attributeFilter: ['data-theme', 'data-density', DEFAULTS_ATTR]
81
101
  });
82
102
  }
83
103
 
@@ -211,9 +231,17 @@ export class GlkElement extends HTMLElement {
211
231
  this._wrapper = document.createElement('div');
212
232
  this._wrapper.className = 'glk-wrapper';
213
233
  this._wrapper.setAttribute('data-theme', getCurrentTheme());
234
+ this._syncDensity(getCurrentDensity());
214
235
  this._shadow.appendChild(this._wrapper);
215
236
 
216
237
  this.render();
238
+ } else {
239
+ // Back in the document. While it was out, the observer did not reach
240
+ // it, so a theme or density switched in the meantime is caught up here
241
+ // — the wrapper's data-theme also sets the color-scheme of the native
242
+ // parts inside.
243
+ this._syncTheme(getCurrentTheme());
244
+ this._syncDensity(getCurrentDensity());
217
245
  }
218
246
 
219
247
  // Everything below runs on every connect, not just the first. Moving an
@@ -249,6 +277,16 @@ export class GlkElement extends HTMLElement {
249
277
  }
250
278
  }
251
279
 
280
+ /** Mirrors data-density from <html>; without one there, the wrapper has none. */
281
+ _syncDensity(density) {
282
+ if (!this._wrapper) return;
283
+ if (density) {
284
+ this._wrapper.setAttribute('data-density', density);
285
+ } else {
286
+ this._wrapper.removeAttribute('data-density');
287
+ }
288
+ }
289
+
252
290
  /** Subclasses override to build inner DOM inside this._wrapper. */
253
291
  render() {}
254
292