@smilodon/core 1.5.4 → 1.5.5

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/dist/index.umd.js CHANGED
@@ -2016,6 +2016,8 @@
2016
2016
  this._mirrorGlobalStylesForCustomOptions = false;
2017
2017
  this._globalStylesObserver = null;
2018
2018
  this._globalStylesContainer = null;
2019
+ this._themeContextObserver = null;
2020
+ this._globalStylesSyncToken = 0;
2019
2021
  this._tracking = { events: [], styles: [], limitations: [] };
2020
2022
  this._suppressBlurClose = false;
2021
2023
  this._renderCycleId = 0;
@@ -2131,6 +2133,7 @@
2131
2133
  return;
2132
2134
  if (!this._mirrorGlobalStylesForCustomOptions)
2133
2135
  return;
2136
+ this._setupThemeContextMirroring();
2134
2137
  this._mirrorDocumentStylesIntoShadow();
2135
2138
  if (this._globalStylesObserver)
2136
2139
  return;
@@ -2149,6 +2152,10 @@
2149
2152
  this._globalStylesObserver.disconnect();
2150
2153
  this._globalStylesObserver = null;
2151
2154
  }
2155
+ if (this._themeContextObserver) {
2156
+ this._themeContextObserver.disconnect();
2157
+ this._themeContextObserver = null;
2158
+ }
2152
2159
  if (this._globalStylesContainer) {
2153
2160
  this._globalStylesContainer.remove();
2154
2161
  this._globalStylesContainer = null;
@@ -2165,34 +2172,232 @@
2165
2172
  this._shadow.appendChild(this._globalStylesContainer);
2166
2173
  }
2167
2174
  const container = this._globalStylesContainer;
2168
- container.innerHTML = '';
2175
+ const syncToken = ++this._globalStylesSyncToken;
2169
2176
  const styleNodes = Array.from(document.querySelectorAll('style,link[rel="stylesheet"]'));
2170
- styleNodes.forEach((node, index) => {
2171
- if (node instanceof HTMLStyleElement) {
2172
- const clonedStyle = document.createElement('style');
2173
- clonedStyle.setAttribute('data-smilodon-global-style', String(index));
2174
- clonedStyle.textContent = node.textContent || '';
2175
- container.appendChild(clonedStyle);
2177
+ void Promise.all(styleNodes.map((node, index) => this._createScopedGlobalStyleNode(node, index))).then((mirroredNodes) => {
2178
+ if (!this._globalStylesContainer || syncToken !== this._globalStylesSyncToken)
2176
2179
  return;
2180
+ container.replaceChildren(...mirroredNodes.filter((node) => Boolean(node)));
2181
+ this._syncMirroredThemeContext();
2182
+ });
2183
+ }
2184
+ _setupThemeContextMirroring() {
2185
+ if (typeof document === 'undefined')
2186
+ return;
2187
+ this._syncMirroredThemeContext();
2188
+ if (this._themeContextObserver)
2189
+ return;
2190
+ this._themeContextObserver = new MutationObserver(() => {
2191
+ this._syncMirroredThemeContext();
2192
+ });
2193
+ const targets = new Set([this, document.documentElement]);
2194
+ if (document.body) {
2195
+ targets.add(document.body);
2196
+ }
2197
+ let ancestor = this.parentElement;
2198
+ while (ancestor) {
2199
+ targets.add(ancestor);
2200
+ ancestor = ancestor.parentElement;
2201
+ }
2202
+ targets.forEach((target) => {
2203
+ this._themeContextObserver?.observe(target, {
2204
+ attributes: true,
2205
+ attributeFilter: ['class', 'dark-mode', 'darkmode', 'data-theme', 'theme'],
2206
+ });
2207
+ });
2208
+ }
2209
+ _syncMirroredThemeContext() {
2210
+ const scopeTarget = this._optionsContainer;
2211
+ if (!scopeTarget)
2212
+ return;
2213
+ const darkSelectors = [
2214
+ '.dark-mode',
2215
+ '.dark',
2216
+ '[dark-mode]',
2217
+ '[darkmode]',
2218
+ '[data-theme="dark"]',
2219
+ '[theme="dark"]',
2220
+ ];
2221
+ const isDark = darkSelectors.some((selector) => this.matches(selector) || Boolean(this.closest(selector)))
2222
+ || document.documentElement.matches('.dark-mode, .dark, [dark-mode], [darkmode], [data-theme="dark"], [theme="dark"]')
2223
+ || Boolean(document.body?.matches('.dark-mode, .dark, [dark-mode], [darkmode], [data-theme="dark"], [theme="dark"]'));
2224
+ scopeTarget.classList.toggle('dark', isDark);
2225
+ scopeTarget.classList.toggle('dark-mode', isDark);
2226
+ if (isDark) {
2227
+ scopeTarget.setAttribute('dark-mode', '');
2228
+ scopeTarget.setAttribute('data-theme', 'dark');
2229
+ scopeTarget.setAttribute('theme', 'dark');
2230
+ }
2231
+ else {
2232
+ scopeTarget.removeAttribute('dark-mode');
2233
+ scopeTarget.removeAttribute('data-theme');
2234
+ scopeTarget.removeAttribute('theme');
2235
+ }
2236
+ }
2237
+ async _createScopedGlobalStyleNode(node, index) {
2238
+ if (node instanceof HTMLStyleElement) {
2239
+ return this._buildScopedStyleElement(node.textContent || '', String(index));
2240
+ }
2241
+ if (node instanceof HTMLLinkElement && node.href) {
2242
+ try {
2243
+ const response = await fetch(node.href, {
2244
+ credentials: node.crossOrigin === 'use-credentials' ? 'include' : 'same-origin',
2245
+ mode: 'cors',
2246
+ });
2247
+ if (!response.ok) {
2248
+ return null;
2249
+ }
2250
+ const cssText = await response.text();
2251
+ return this._buildScopedStyleElement(cssText, String(index));
2177
2252
  }
2178
- if (node instanceof HTMLLinkElement && node.href) {
2179
- const clonedLink = document.createElement('link');
2180
- clonedLink.rel = 'stylesheet';
2181
- clonedLink.href = node.href;
2182
- if (node.media)
2183
- clonedLink.media = node.media;
2184
- if (node.crossOrigin)
2185
- clonedLink.crossOrigin = node.crossOrigin;
2186
- if (node.referrerPolicy)
2187
- clonedLink.referrerPolicy = node.referrerPolicy;
2188
- if (node.integrity)
2189
- clonedLink.integrity = node.integrity;
2190
- if (node.type)
2191
- clonedLink.type = node.type;
2192
- if (node.nonce)
2193
- clonedLink.nonce = node.nonce;
2194
- clonedLink.setAttribute('data-smilodon-global-link', String(index));
2195
- container.appendChild(clonedLink);
2253
+ catch (_error) {
2254
+ return null;
2255
+ }
2256
+ }
2257
+ return null;
2258
+ }
2259
+ _buildScopedStyleElement(cssText, id) {
2260
+ const scopedCss = this._scopeMirroredCssText(cssText);
2261
+ if (!scopedCss.trim())
2262
+ return null;
2263
+ const style = document.createElement('style');
2264
+ style.setAttribute('data-smilodon-global-style', id);
2265
+ style.textContent = scopedCss;
2266
+ return style;
2267
+ }
2268
+ _scopeMirroredCssText(cssText) {
2269
+ if (!cssText.trim())
2270
+ return '';
2271
+ try {
2272
+ if (typeof CSSStyleSheet !== 'undefined') {
2273
+ const sheet = new CSSStyleSheet();
2274
+ sheet.replaceSync(cssText);
2275
+ return this._serializeScopedCssRules(Array.from(sheet.cssRules));
2276
+ }
2277
+ }
2278
+ catch (_error) {
2279
+ return '';
2280
+ }
2281
+ return '';
2282
+ }
2283
+ _serializeScopedCssRules(rules) {
2284
+ return rules.map((rule) => this._serializeScopedCssRule(rule)).filter(Boolean).join('\n');
2285
+ }
2286
+ _serializeScopedCssRule(rule) {
2287
+ if (rule instanceof CSSStyleRule) {
2288
+ const selectors = this._splitSelectorList(rule.selectorText)
2289
+ .map((selector) => this._scopeMirroredSelector(selector))
2290
+ .filter(Boolean);
2291
+ if (selectors.length === 0)
2292
+ return '';
2293
+ return `${selectors.join(', ')} { ${rule.style.cssText} }`;
2294
+ }
2295
+ if (rule instanceof CSSMediaRule) {
2296
+ const nested = this._serializeScopedCssRules(Array.from(rule.cssRules));
2297
+ return nested ? `@media ${rule.conditionText} { ${nested} }` : '';
2298
+ }
2299
+ if (typeof CSSSupportsRule !== 'undefined' && rule instanceof CSSSupportsRule) {
2300
+ const nested = this._serializeScopedCssRules(Array.from(rule.cssRules));
2301
+ return nested ? `@supports ${rule.conditionText} { ${nested} }` : '';
2302
+ }
2303
+ if (rule instanceof CSSFontFaceRule || rule instanceof CSSKeyframesRule) {
2304
+ return rule.cssText;
2305
+ }
2306
+ const nestedRule = rule;
2307
+ if (nestedRule.cssRules) {
2308
+ const nested = this._serializeScopedCssRules(Array.from(nestedRule.cssRules));
2309
+ if (!nested)
2310
+ return '';
2311
+ const ctorName = nestedRule.constructor?.name ?? '';
2312
+ if (ctorName.includes('Layer')) {
2313
+ return `@layer ${nestedRule.name ?? ''} { ${nested} }`;
2314
+ }
2315
+ if (ctorName.includes('Container')) {
2316
+ return `@container ${nestedRule.conditionText ?? ''} { ${nested} }`;
2317
+ }
2318
+ }
2319
+ return '';
2320
+ }
2321
+ _splitSelectorList(selectorText) {
2322
+ const selectors = [];
2323
+ let current = '';
2324
+ let depthRound = 0;
2325
+ let depthSquare = 0;
2326
+ for (const char of selectorText) {
2327
+ if (char === '(')
2328
+ depthRound += 1;
2329
+ if (char === ')')
2330
+ depthRound = Math.max(0, depthRound - 1);
2331
+ if (char === '[')
2332
+ depthSquare += 1;
2333
+ if (char === ']')
2334
+ depthSquare = Math.max(0, depthSquare - 1);
2335
+ if (char === ',' && depthRound === 0 && depthSquare === 0) {
2336
+ if (current.trim())
2337
+ selectors.push(current.trim());
2338
+ current = '';
2339
+ continue;
2340
+ }
2341
+ current += char;
2342
+ }
2343
+ if (current.trim())
2344
+ selectors.push(current.trim());
2345
+ return selectors;
2346
+ }
2347
+ _scopeMirroredSelector(selector) {
2348
+ const scopeClass = `.${EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS}`;
2349
+ let scopedSelector = selector
2350
+ .replace(/(^|\s|>|\+|~)(:root|html|body)(?=\b|\s|[.#[:>+~]|$)/g, `$1${scopeClass}`);
2351
+ const themePatterns = [
2352
+ /\.dark-mode(?![\w-]|\\:)/g,
2353
+ /\.dark(?![\w-]|\\:)/g,
2354
+ /\[dark-mode\]/g,
2355
+ /\[darkmode\]/g,
2356
+ /\[data-theme=(?:"dark"|'dark'|dark)\]/g,
2357
+ /\[theme=(?:"dark"|'dark'|dark)\]/g,
2358
+ ];
2359
+ let injectedThemeScope = false;
2360
+ themePatterns.forEach((pattern) => {
2361
+ scopedSelector = scopedSelector.replace(pattern, (match) => {
2362
+ injectedThemeScope = true;
2363
+ return `${match}${scopeClass}`;
2364
+ });
2365
+ });
2366
+ if (!scopedSelector.includes(EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS) && !injectedThemeScope) {
2367
+ scopedSelector = `${scopeClass} ${scopedSelector}`;
2368
+ }
2369
+ return scopedSelector;
2370
+ }
2371
+ _normalizeCustomOptionAccessibility(optionEl, meta) {
2372
+ if (!optionEl.hasAttribute('aria-label') && !optionEl.hasAttribute('aria-labelledby')) {
2373
+ optionEl.setAttribute('aria-label', meta.label);
2374
+ }
2375
+ const nestedFocusableSelectors = [
2376
+ 'a[href]',
2377
+ 'button',
2378
+ 'input',
2379
+ 'select',
2380
+ 'textarea',
2381
+ 'summary',
2382
+ '[tabindex]',
2383
+ '[contenteditable=""]',
2384
+ '[contenteditable="true"]',
2385
+ '[role="button"]',
2386
+ '[role="link"]',
2387
+ '[role="checkbox"]',
2388
+ '[role="switch"]',
2389
+ '[role="radio"]',
2390
+ '[role="textbox"]',
2391
+ ].join(', ');
2392
+ optionEl.querySelectorAll(nestedFocusableSelectors).forEach((child) => {
2393
+ if (child === optionEl)
2394
+ return;
2395
+ child.tabIndex = -1;
2396
+ if (!child.hasAttribute('data-smilodon-keep-role')) {
2397
+ child.setAttribute('role', 'presentation');
2398
+ }
2399
+ if ((meta.disabled || child.matches('button, input, select, textarea, summary, a[href]')) && !child.hasAttribute('data-smilodon-keep-aria')) {
2400
+ child.setAttribute('aria-hidden', 'true');
2196
2401
  }
2197
2402
  });
2198
2403
  }
@@ -2656,7 +2861,7 @@
2656
2861
  }
2657
2862
  _createOptionsContainer() {
2658
2863
  const container = document.createElement('div');
2659
- container.className = 'options-container';
2864
+ container.className = `options-container ${EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS}`;
2660
2865
  return container;
2661
2866
  }
2662
2867
  _createLiveRegion() {
@@ -5665,6 +5870,9 @@
5665
5870
  if (this._config.styles.classNames?.groupHeader) {
5666
5871
  header.classList.add(...this._config.styles.classNames.groupHeader.split(' ').filter(Boolean));
5667
5872
  }
5873
+ if (!header.hasAttribute('role')) {
5874
+ header.setAttribute('role', 'presentation');
5875
+ }
5668
5876
  header.setAttribute('part', 'group-header');
5669
5877
  this._optionsContainer.appendChild(header);
5670
5878
  group.options.forEach(item => {
@@ -5940,9 +6148,6 @@
5940
6148
  if (!optionEl.getAttribute('role')) {
5941
6149
  optionEl.setAttribute('role', 'option');
5942
6150
  }
5943
- if (!optionEl.getAttribute('aria-label')) {
5944
- optionEl.setAttribute('aria-label', meta.label);
5945
- }
5946
6151
  optionEl.setAttribute('aria-selected', String(meta.selected));
5947
6152
  if (meta.disabled) {
5948
6153
  optionEl.setAttribute('aria-disabled', 'true');
@@ -5959,6 +6164,10 @@
5959
6164
  // Re-attaching here causes the double-click toggle bug if a child component fails to stopPropagation.
5960
6165
  this._customOptionBoundElements.add(optionEl);
5961
6166
  }
6167
+ this._normalizeCustomOptionAccessibility(optionEl, {
6168
+ label: meta.label,
6169
+ disabled: meta.disabled,
6170
+ });
5962
6171
  return optionEl;
5963
6172
  }
5964
6173
  _addLoadMoreTrigger() {
@@ -5993,6 +6202,7 @@
5993
6202
  }
5994
6203
  /** live set of all connected instances; used to auto-close siblings */
5995
6204
  EnhancedSelect._instances = new Set();
6205
+ EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS = 'smilodon-mirrored-styles-scope';
5996
6206
  // Register custom element
5997
6207
  if (!customElements.get('enhanced-select')) {
5998
6208
  customElements.define('enhanced-select', EnhancedSelect);