@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/README.md CHANGED
@@ -131,6 +131,12 @@ See the [main documentation](https://github.com/navidrezadoost/smilodon#readme)
131
131
  - **Full keyboard navigation**: Enter/Space, Arrows, Home/End, PageUp/Down, Escape
132
132
  - **Multi-select modes**: wrap, horizontal, vertical, both (with drag-scroll)
133
133
 
134
+ ### 🎨 CSS Framework Compatibility (`1.5.5`)
135
+ - **Scoped mirrored styles**: class-based framework styles used by custom renderers are mirrored only into the options subtree, not the whole shadow root.
136
+ - **Dark variant support**: `.dark`, `.dark-mode`, and theme-attribute variants propagate into custom-rendered option content.
137
+ - **Stable state hooks**: `classMap`, `data-sm-state`, and Smilodon state classes stay available on custom option roots for hover, active, selected, and disabled styling.
138
+ - **Accessible custom renderers**: nested interactive descendants are normalized by default so option renderers stay compatible with listbox semantics.
139
+
134
140
  ### 🔒 Enterprise Grade
135
141
  - **SOC2 Compliant**: Audit-ready security controls
136
142
  - **CSP Compatible**: No eval(), no inline scripts
@@ -1047,6 +1053,8 @@ enhanced-select {
1047
1053
 
1048
1054
  Framework compatibility guidance also lives in [../../docs/CSS-FRAMEWORK-COMPATIBILITY.md](../../docs/CSS-FRAMEWORK-COMPATIBILITY.md).
1049
1055
 
1056
+ When using Tailwind-style custom renderers, remember that `optionRenderer` may return the option root itself. In that case, put the utility classes and hover/active/selected styling on the returned root element instead of assuming there is a nested wrapper.
1057
+
1050
1058
  **Example 4: Custom Brand Colors**
1051
1059
  ```css
1052
1060
  enhanced-select {
package/dist/index.cjs CHANGED
@@ -2012,6 +2012,8 @@ class EnhancedSelect extends HTMLElement {
2012
2012
  this._mirrorGlobalStylesForCustomOptions = false;
2013
2013
  this._globalStylesObserver = null;
2014
2014
  this._globalStylesContainer = null;
2015
+ this._themeContextObserver = null;
2016
+ this._globalStylesSyncToken = 0;
2015
2017
  this._tracking = { events: [], styles: [], limitations: [] };
2016
2018
  this._suppressBlurClose = false;
2017
2019
  this._renderCycleId = 0;
@@ -2127,6 +2129,7 @@ class EnhancedSelect extends HTMLElement {
2127
2129
  return;
2128
2130
  if (!this._mirrorGlobalStylesForCustomOptions)
2129
2131
  return;
2132
+ this._setupThemeContextMirroring();
2130
2133
  this._mirrorDocumentStylesIntoShadow();
2131
2134
  if (this._globalStylesObserver)
2132
2135
  return;
@@ -2145,6 +2148,10 @@ class EnhancedSelect extends HTMLElement {
2145
2148
  this._globalStylesObserver.disconnect();
2146
2149
  this._globalStylesObserver = null;
2147
2150
  }
2151
+ if (this._themeContextObserver) {
2152
+ this._themeContextObserver.disconnect();
2153
+ this._themeContextObserver = null;
2154
+ }
2148
2155
  if (this._globalStylesContainer) {
2149
2156
  this._globalStylesContainer.remove();
2150
2157
  this._globalStylesContainer = null;
@@ -2161,34 +2168,232 @@ class EnhancedSelect extends HTMLElement {
2161
2168
  this._shadow.appendChild(this._globalStylesContainer);
2162
2169
  }
2163
2170
  const container = this._globalStylesContainer;
2164
- container.innerHTML = '';
2171
+ const syncToken = ++this._globalStylesSyncToken;
2165
2172
  const styleNodes = Array.from(document.querySelectorAll('style,link[rel="stylesheet"]'));
2166
- styleNodes.forEach((node, index) => {
2167
- if (node instanceof HTMLStyleElement) {
2168
- const clonedStyle = document.createElement('style');
2169
- clonedStyle.setAttribute('data-smilodon-global-style', String(index));
2170
- clonedStyle.textContent = node.textContent || '';
2171
- container.appendChild(clonedStyle);
2173
+ void Promise.all(styleNodes.map((node, index) => this._createScopedGlobalStyleNode(node, index))).then((mirroredNodes) => {
2174
+ if (!this._globalStylesContainer || syncToken !== this._globalStylesSyncToken)
2172
2175
  return;
2176
+ container.replaceChildren(...mirroredNodes.filter((node) => Boolean(node)));
2177
+ this._syncMirroredThemeContext();
2178
+ });
2179
+ }
2180
+ _setupThemeContextMirroring() {
2181
+ if (typeof document === 'undefined')
2182
+ return;
2183
+ this._syncMirroredThemeContext();
2184
+ if (this._themeContextObserver)
2185
+ return;
2186
+ this._themeContextObserver = new MutationObserver(() => {
2187
+ this._syncMirroredThemeContext();
2188
+ });
2189
+ const targets = new Set([this, document.documentElement]);
2190
+ if (document.body) {
2191
+ targets.add(document.body);
2192
+ }
2193
+ let ancestor = this.parentElement;
2194
+ while (ancestor) {
2195
+ targets.add(ancestor);
2196
+ ancestor = ancestor.parentElement;
2197
+ }
2198
+ targets.forEach((target) => {
2199
+ this._themeContextObserver?.observe(target, {
2200
+ attributes: true,
2201
+ attributeFilter: ['class', 'dark-mode', 'darkmode', 'data-theme', 'theme'],
2202
+ });
2203
+ });
2204
+ }
2205
+ _syncMirroredThemeContext() {
2206
+ const scopeTarget = this._optionsContainer;
2207
+ if (!scopeTarget)
2208
+ return;
2209
+ const darkSelectors = [
2210
+ '.dark-mode',
2211
+ '.dark',
2212
+ '[dark-mode]',
2213
+ '[darkmode]',
2214
+ '[data-theme="dark"]',
2215
+ '[theme="dark"]',
2216
+ ];
2217
+ const isDark = darkSelectors.some((selector) => this.matches(selector) || Boolean(this.closest(selector)))
2218
+ || document.documentElement.matches('.dark-mode, .dark, [dark-mode], [darkmode], [data-theme="dark"], [theme="dark"]')
2219
+ || Boolean(document.body?.matches('.dark-mode, .dark, [dark-mode], [darkmode], [data-theme="dark"], [theme="dark"]'));
2220
+ scopeTarget.classList.toggle('dark', isDark);
2221
+ scopeTarget.classList.toggle('dark-mode', isDark);
2222
+ if (isDark) {
2223
+ scopeTarget.setAttribute('dark-mode', '');
2224
+ scopeTarget.setAttribute('data-theme', 'dark');
2225
+ scopeTarget.setAttribute('theme', 'dark');
2226
+ }
2227
+ else {
2228
+ scopeTarget.removeAttribute('dark-mode');
2229
+ scopeTarget.removeAttribute('data-theme');
2230
+ scopeTarget.removeAttribute('theme');
2231
+ }
2232
+ }
2233
+ async _createScopedGlobalStyleNode(node, index) {
2234
+ if (node instanceof HTMLStyleElement) {
2235
+ return this._buildScopedStyleElement(node.textContent || '', String(index));
2236
+ }
2237
+ if (node instanceof HTMLLinkElement && node.href) {
2238
+ try {
2239
+ const response = await fetch(node.href, {
2240
+ credentials: node.crossOrigin === 'use-credentials' ? 'include' : 'same-origin',
2241
+ mode: 'cors',
2242
+ });
2243
+ if (!response.ok) {
2244
+ return null;
2245
+ }
2246
+ const cssText = await response.text();
2247
+ return this._buildScopedStyleElement(cssText, String(index));
2173
2248
  }
2174
- if (node instanceof HTMLLinkElement && node.href) {
2175
- const clonedLink = document.createElement('link');
2176
- clonedLink.rel = 'stylesheet';
2177
- clonedLink.href = node.href;
2178
- if (node.media)
2179
- clonedLink.media = node.media;
2180
- if (node.crossOrigin)
2181
- clonedLink.crossOrigin = node.crossOrigin;
2182
- if (node.referrerPolicy)
2183
- clonedLink.referrerPolicy = node.referrerPolicy;
2184
- if (node.integrity)
2185
- clonedLink.integrity = node.integrity;
2186
- if (node.type)
2187
- clonedLink.type = node.type;
2188
- if (node.nonce)
2189
- clonedLink.nonce = node.nonce;
2190
- clonedLink.setAttribute('data-smilodon-global-link', String(index));
2191
- container.appendChild(clonedLink);
2249
+ catch (_error) {
2250
+ return null;
2251
+ }
2252
+ }
2253
+ return null;
2254
+ }
2255
+ _buildScopedStyleElement(cssText, id) {
2256
+ const scopedCss = this._scopeMirroredCssText(cssText);
2257
+ if (!scopedCss.trim())
2258
+ return null;
2259
+ const style = document.createElement('style');
2260
+ style.setAttribute('data-smilodon-global-style', id);
2261
+ style.textContent = scopedCss;
2262
+ return style;
2263
+ }
2264
+ _scopeMirroredCssText(cssText) {
2265
+ if (!cssText.trim())
2266
+ return '';
2267
+ try {
2268
+ if (typeof CSSStyleSheet !== 'undefined') {
2269
+ const sheet = new CSSStyleSheet();
2270
+ sheet.replaceSync(cssText);
2271
+ return this._serializeScopedCssRules(Array.from(sheet.cssRules));
2272
+ }
2273
+ }
2274
+ catch (_error) {
2275
+ return '';
2276
+ }
2277
+ return '';
2278
+ }
2279
+ _serializeScopedCssRules(rules) {
2280
+ return rules.map((rule) => this._serializeScopedCssRule(rule)).filter(Boolean).join('\n');
2281
+ }
2282
+ _serializeScopedCssRule(rule) {
2283
+ if (rule instanceof CSSStyleRule) {
2284
+ const selectors = this._splitSelectorList(rule.selectorText)
2285
+ .map((selector) => this._scopeMirroredSelector(selector))
2286
+ .filter(Boolean);
2287
+ if (selectors.length === 0)
2288
+ return '';
2289
+ return `${selectors.join(', ')} { ${rule.style.cssText} }`;
2290
+ }
2291
+ if (rule instanceof CSSMediaRule) {
2292
+ const nested = this._serializeScopedCssRules(Array.from(rule.cssRules));
2293
+ return nested ? `@media ${rule.conditionText} { ${nested} }` : '';
2294
+ }
2295
+ if (typeof CSSSupportsRule !== 'undefined' && rule instanceof CSSSupportsRule) {
2296
+ const nested = this._serializeScopedCssRules(Array.from(rule.cssRules));
2297
+ return nested ? `@supports ${rule.conditionText} { ${nested} }` : '';
2298
+ }
2299
+ if (rule instanceof CSSFontFaceRule || rule instanceof CSSKeyframesRule) {
2300
+ return rule.cssText;
2301
+ }
2302
+ const nestedRule = rule;
2303
+ if (nestedRule.cssRules) {
2304
+ const nested = this._serializeScopedCssRules(Array.from(nestedRule.cssRules));
2305
+ if (!nested)
2306
+ return '';
2307
+ const ctorName = nestedRule.constructor?.name ?? '';
2308
+ if (ctorName.includes('Layer')) {
2309
+ return `@layer ${nestedRule.name ?? ''} { ${nested} }`;
2310
+ }
2311
+ if (ctorName.includes('Container')) {
2312
+ return `@container ${nestedRule.conditionText ?? ''} { ${nested} }`;
2313
+ }
2314
+ }
2315
+ return '';
2316
+ }
2317
+ _splitSelectorList(selectorText) {
2318
+ const selectors = [];
2319
+ let current = '';
2320
+ let depthRound = 0;
2321
+ let depthSquare = 0;
2322
+ for (const char of selectorText) {
2323
+ if (char === '(')
2324
+ depthRound += 1;
2325
+ if (char === ')')
2326
+ depthRound = Math.max(0, depthRound - 1);
2327
+ if (char === '[')
2328
+ depthSquare += 1;
2329
+ if (char === ']')
2330
+ depthSquare = Math.max(0, depthSquare - 1);
2331
+ if (char === ',' && depthRound === 0 && depthSquare === 0) {
2332
+ if (current.trim())
2333
+ selectors.push(current.trim());
2334
+ current = '';
2335
+ continue;
2336
+ }
2337
+ current += char;
2338
+ }
2339
+ if (current.trim())
2340
+ selectors.push(current.trim());
2341
+ return selectors;
2342
+ }
2343
+ _scopeMirroredSelector(selector) {
2344
+ const scopeClass = `.${EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS}`;
2345
+ let scopedSelector = selector
2346
+ .replace(/(^|\s|>|\+|~)(:root|html|body)(?=\b|\s|[.#[:>+~]|$)/g, `$1${scopeClass}`);
2347
+ const themePatterns = [
2348
+ /\.dark-mode(?![\w-]|\\:)/g,
2349
+ /\.dark(?![\w-]|\\:)/g,
2350
+ /\[dark-mode\]/g,
2351
+ /\[darkmode\]/g,
2352
+ /\[data-theme=(?:"dark"|'dark'|dark)\]/g,
2353
+ /\[theme=(?:"dark"|'dark'|dark)\]/g,
2354
+ ];
2355
+ let injectedThemeScope = false;
2356
+ themePatterns.forEach((pattern) => {
2357
+ scopedSelector = scopedSelector.replace(pattern, (match) => {
2358
+ injectedThemeScope = true;
2359
+ return `${match}${scopeClass}`;
2360
+ });
2361
+ });
2362
+ if (!scopedSelector.includes(EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS) && !injectedThemeScope) {
2363
+ scopedSelector = `${scopeClass} ${scopedSelector}`;
2364
+ }
2365
+ return scopedSelector;
2366
+ }
2367
+ _normalizeCustomOptionAccessibility(optionEl, meta) {
2368
+ if (!optionEl.hasAttribute('aria-label') && !optionEl.hasAttribute('aria-labelledby')) {
2369
+ optionEl.setAttribute('aria-label', meta.label);
2370
+ }
2371
+ const nestedFocusableSelectors = [
2372
+ 'a[href]',
2373
+ 'button',
2374
+ 'input',
2375
+ 'select',
2376
+ 'textarea',
2377
+ 'summary',
2378
+ '[tabindex]',
2379
+ '[contenteditable=""]',
2380
+ '[contenteditable="true"]',
2381
+ '[role="button"]',
2382
+ '[role="link"]',
2383
+ '[role="checkbox"]',
2384
+ '[role="switch"]',
2385
+ '[role="radio"]',
2386
+ '[role="textbox"]',
2387
+ ].join(', ');
2388
+ optionEl.querySelectorAll(nestedFocusableSelectors).forEach((child) => {
2389
+ if (child === optionEl)
2390
+ return;
2391
+ child.tabIndex = -1;
2392
+ if (!child.hasAttribute('data-smilodon-keep-role')) {
2393
+ child.setAttribute('role', 'presentation');
2394
+ }
2395
+ if ((meta.disabled || child.matches('button, input, select, textarea, summary, a[href]')) && !child.hasAttribute('data-smilodon-keep-aria')) {
2396
+ child.setAttribute('aria-hidden', 'true');
2192
2397
  }
2193
2398
  });
2194
2399
  }
@@ -2652,7 +2857,7 @@ class EnhancedSelect extends HTMLElement {
2652
2857
  }
2653
2858
  _createOptionsContainer() {
2654
2859
  const container = document.createElement('div');
2655
- container.className = 'options-container';
2860
+ container.className = `options-container ${EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS}`;
2656
2861
  return container;
2657
2862
  }
2658
2863
  _createLiveRegion() {
@@ -5661,6 +5866,9 @@ class EnhancedSelect extends HTMLElement {
5661
5866
  if (this._config.styles.classNames?.groupHeader) {
5662
5867
  header.classList.add(...this._config.styles.classNames.groupHeader.split(' ').filter(Boolean));
5663
5868
  }
5869
+ if (!header.hasAttribute('role')) {
5870
+ header.setAttribute('role', 'presentation');
5871
+ }
5664
5872
  header.setAttribute('part', 'group-header');
5665
5873
  this._optionsContainer.appendChild(header);
5666
5874
  group.options.forEach(item => {
@@ -5936,9 +6144,6 @@ class EnhancedSelect extends HTMLElement {
5936
6144
  if (!optionEl.getAttribute('role')) {
5937
6145
  optionEl.setAttribute('role', 'option');
5938
6146
  }
5939
- if (!optionEl.getAttribute('aria-label')) {
5940
- optionEl.setAttribute('aria-label', meta.label);
5941
- }
5942
6147
  optionEl.setAttribute('aria-selected', String(meta.selected));
5943
6148
  if (meta.disabled) {
5944
6149
  optionEl.setAttribute('aria-disabled', 'true');
@@ -5955,6 +6160,10 @@ class EnhancedSelect extends HTMLElement {
5955
6160
  // Re-attaching here causes the double-click toggle bug if a child component fails to stopPropagation.
5956
6161
  this._customOptionBoundElements.add(optionEl);
5957
6162
  }
6163
+ this._normalizeCustomOptionAccessibility(optionEl, {
6164
+ label: meta.label,
6165
+ disabled: meta.disabled,
6166
+ });
5958
6167
  return optionEl;
5959
6168
  }
5960
6169
  _addLoadMoreTrigger() {
@@ -5989,6 +6198,7 @@ class EnhancedSelect extends HTMLElement {
5989
6198
  }
5990
6199
  /** live set of all connected instances; used to auto-close siblings */
5991
6200
  EnhancedSelect._instances = new Set();
6201
+ EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS = 'smilodon-mirrored-styles-scope';
5992
6202
  // Register custom element
5993
6203
  if (!customElements.get('enhanced-select')) {
5994
6204
  customElements.define('enhanced-select', EnhancedSelect);