@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 +8 -0
- package/dist/index.cjs +239 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +239 -29
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.umd.js +239 -29
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/components/enhanced-select.d.ts +13 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2010,6 +2010,8 @@ class EnhancedSelect extends HTMLElement {
|
|
|
2010
2010
|
this._mirrorGlobalStylesForCustomOptions = false;
|
|
2011
2011
|
this._globalStylesObserver = null;
|
|
2012
2012
|
this._globalStylesContainer = null;
|
|
2013
|
+
this._themeContextObserver = null;
|
|
2014
|
+
this._globalStylesSyncToken = 0;
|
|
2013
2015
|
this._tracking = { events: [], styles: [], limitations: [] };
|
|
2014
2016
|
this._suppressBlurClose = false;
|
|
2015
2017
|
this._renderCycleId = 0;
|
|
@@ -2125,6 +2127,7 @@ class EnhancedSelect extends HTMLElement {
|
|
|
2125
2127
|
return;
|
|
2126
2128
|
if (!this._mirrorGlobalStylesForCustomOptions)
|
|
2127
2129
|
return;
|
|
2130
|
+
this._setupThemeContextMirroring();
|
|
2128
2131
|
this._mirrorDocumentStylesIntoShadow();
|
|
2129
2132
|
if (this._globalStylesObserver)
|
|
2130
2133
|
return;
|
|
@@ -2143,6 +2146,10 @@ class EnhancedSelect extends HTMLElement {
|
|
|
2143
2146
|
this._globalStylesObserver.disconnect();
|
|
2144
2147
|
this._globalStylesObserver = null;
|
|
2145
2148
|
}
|
|
2149
|
+
if (this._themeContextObserver) {
|
|
2150
|
+
this._themeContextObserver.disconnect();
|
|
2151
|
+
this._themeContextObserver = null;
|
|
2152
|
+
}
|
|
2146
2153
|
if (this._globalStylesContainer) {
|
|
2147
2154
|
this._globalStylesContainer.remove();
|
|
2148
2155
|
this._globalStylesContainer = null;
|
|
@@ -2159,34 +2166,232 @@ class EnhancedSelect extends HTMLElement {
|
|
|
2159
2166
|
this._shadow.appendChild(this._globalStylesContainer);
|
|
2160
2167
|
}
|
|
2161
2168
|
const container = this._globalStylesContainer;
|
|
2162
|
-
|
|
2169
|
+
const syncToken = ++this._globalStylesSyncToken;
|
|
2163
2170
|
const styleNodes = Array.from(document.querySelectorAll('style,link[rel="stylesheet"]'));
|
|
2164
|
-
styleNodes.
|
|
2165
|
-
if (
|
|
2166
|
-
const clonedStyle = document.createElement('style');
|
|
2167
|
-
clonedStyle.setAttribute('data-smilodon-global-style', String(index));
|
|
2168
|
-
clonedStyle.textContent = node.textContent || '';
|
|
2169
|
-
container.appendChild(clonedStyle);
|
|
2171
|
+
void Promise.all(styleNodes.map((node, index) => this._createScopedGlobalStyleNode(node, index))).then((mirroredNodes) => {
|
|
2172
|
+
if (!this._globalStylesContainer || syncToken !== this._globalStylesSyncToken)
|
|
2170
2173
|
return;
|
|
2174
|
+
container.replaceChildren(...mirroredNodes.filter((node) => Boolean(node)));
|
|
2175
|
+
this._syncMirroredThemeContext();
|
|
2176
|
+
});
|
|
2177
|
+
}
|
|
2178
|
+
_setupThemeContextMirroring() {
|
|
2179
|
+
if (typeof document === 'undefined')
|
|
2180
|
+
return;
|
|
2181
|
+
this._syncMirroredThemeContext();
|
|
2182
|
+
if (this._themeContextObserver)
|
|
2183
|
+
return;
|
|
2184
|
+
this._themeContextObserver = new MutationObserver(() => {
|
|
2185
|
+
this._syncMirroredThemeContext();
|
|
2186
|
+
});
|
|
2187
|
+
const targets = new Set([this, document.documentElement]);
|
|
2188
|
+
if (document.body) {
|
|
2189
|
+
targets.add(document.body);
|
|
2190
|
+
}
|
|
2191
|
+
let ancestor = this.parentElement;
|
|
2192
|
+
while (ancestor) {
|
|
2193
|
+
targets.add(ancestor);
|
|
2194
|
+
ancestor = ancestor.parentElement;
|
|
2195
|
+
}
|
|
2196
|
+
targets.forEach((target) => {
|
|
2197
|
+
this._themeContextObserver?.observe(target, {
|
|
2198
|
+
attributes: true,
|
|
2199
|
+
attributeFilter: ['class', 'dark-mode', 'darkmode', 'data-theme', 'theme'],
|
|
2200
|
+
});
|
|
2201
|
+
});
|
|
2202
|
+
}
|
|
2203
|
+
_syncMirroredThemeContext() {
|
|
2204
|
+
const scopeTarget = this._optionsContainer;
|
|
2205
|
+
if (!scopeTarget)
|
|
2206
|
+
return;
|
|
2207
|
+
const darkSelectors = [
|
|
2208
|
+
'.dark-mode',
|
|
2209
|
+
'.dark',
|
|
2210
|
+
'[dark-mode]',
|
|
2211
|
+
'[darkmode]',
|
|
2212
|
+
'[data-theme="dark"]',
|
|
2213
|
+
'[theme="dark"]',
|
|
2214
|
+
];
|
|
2215
|
+
const isDark = darkSelectors.some((selector) => this.matches(selector) || Boolean(this.closest(selector)))
|
|
2216
|
+
|| document.documentElement.matches('.dark-mode, .dark, [dark-mode], [darkmode], [data-theme="dark"], [theme="dark"]')
|
|
2217
|
+
|| Boolean(document.body?.matches('.dark-mode, .dark, [dark-mode], [darkmode], [data-theme="dark"], [theme="dark"]'));
|
|
2218
|
+
scopeTarget.classList.toggle('dark', isDark);
|
|
2219
|
+
scopeTarget.classList.toggle('dark-mode', isDark);
|
|
2220
|
+
if (isDark) {
|
|
2221
|
+
scopeTarget.setAttribute('dark-mode', '');
|
|
2222
|
+
scopeTarget.setAttribute('data-theme', 'dark');
|
|
2223
|
+
scopeTarget.setAttribute('theme', 'dark');
|
|
2224
|
+
}
|
|
2225
|
+
else {
|
|
2226
|
+
scopeTarget.removeAttribute('dark-mode');
|
|
2227
|
+
scopeTarget.removeAttribute('data-theme');
|
|
2228
|
+
scopeTarget.removeAttribute('theme');
|
|
2229
|
+
}
|
|
2230
|
+
}
|
|
2231
|
+
async _createScopedGlobalStyleNode(node, index) {
|
|
2232
|
+
if (node instanceof HTMLStyleElement) {
|
|
2233
|
+
return this._buildScopedStyleElement(node.textContent || '', String(index));
|
|
2234
|
+
}
|
|
2235
|
+
if (node instanceof HTMLLinkElement && node.href) {
|
|
2236
|
+
try {
|
|
2237
|
+
const response = await fetch(node.href, {
|
|
2238
|
+
credentials: node.crossOrigin === 'use-credentials' ? 'include' : 'same-origin',
|
|
2239
|
+
mode: 'cors',
|
|
2240
|
+
});
|
|
2241
|
+
if (!response.ok) {
|
|
2242
|
+
return null;
|
|
2243
|
+
}
|
|
2244
|
+
const cssText = await response.text();
|
|
2245
|
+
return this._buildScopedStyleElement(cssText, String(index));
|
|
2171
2246
|
}
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2247
|
+
catch (_error) {
|
|
2248
|
+
return null;
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
return null;
|
|
2252
|
+
}
|
|
2253
|
+
_buildScopedStyleElement(cssText, id) {
|
|
2254
|
+
const scopedCss = this._scopeMirroredCssText(cssText);
|
|
2255
|
+
if (!scopedCss.trim())
|
|
2256
|
+
return null;
|
|
2257
|
+
const style = document.createElement('style');
|
|
2258
|
+
style.setAttribute('data-smilodon-global-style', id);
|
|
2259
|
+
style.textContent = scopedCss;
|
|
2260
|
+
return style;
|
|
2261
|
+
}
|
|
2262
|
+
_scopeMirroredCssText(cssText) {
|
|
2263
|
+
if (!cssText.trim())
|
|
2264
|
+
return '';
|
|
2265
|
+
try {
|
|
2266
|
+
if (typeof CSSStyleSheet !== 'undefined') {
|
|
2267
|
+
const sheet = new CSSStyleSheet();
|
|
2268
|
+
sheet.replaceSync(cssText);
|
|
2269
|
+
return this._serializeScopedCssRules(Array.from(sheet.cssRules));
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
catch (_error) {
|
|
2273
|
+
return '';
|
|
2274
|
+
}
|
|
2275
|
+
return '';
|
|
2276
|
+
}
|
|
2277
|
+
_serializeScopedCssRules(rules) {
|
|
2278
|
+
return rules.map((rule) => this._serializeScopedCssRule(rule)).filter(Boolean).join('\n');
|
|
2279
|
+
}
|
|
2280
|
+
_serializeScopedCssRule(rule) {
|
|
2281
|
+
if (rule instanceof CSSStyleRule) {
|
|
2282
|
+
const selectors = this._splitSelectorList(rule.selectorText)
|
|
2283
|
+
.map((selector) => this._scopeMirroredSelector(selector))
|
|
2284
|
+
.filter(Boolean);
|
|
2285
|
+
if (selectors.length === 0)
|
|
2286
|
+
return '';
|
|
2287
|
+
return `${selectors.join(', ')} { ${rule.style.cssText} }`;
|
|
2288
|
+
}
|
|
2289
|
+
if (rule instanceof CSSMediaRule) {
|
|
2290
|
+
const nested = this._serializeScopedCssRules(Array.from(rule.cssRules));
|
|
2291
|
+
return nested ? `@media ${rule.conditionText} { ${nested} }` : '';
|
|
2292
|
+
}
|
|
2293
|
+
if (typeof CSSSupportsRule !== 'undefined' && rule instanceof CSSSupportsRule) {
|
|
2294
|
+
const nested = this._serializeScopedCssRules(Array.from(rule.cssRules));
|
|
2295
|
+
return nested ? `@supports ${rule.conditionText} { ${nested} }` : '';
|
|
2296
|
+
}
|
|
2297
|
+
if (rule instanceof CSSFontFaceRule || rule instanceof CSSKeyframesRule) {
|
|
2298
|
+
return rule.cssText;
|
|
2299
|
+
}
|
|
2300
|
+
const nestedRule = rule;
|
|
2301
|
+
if (nestedRule.cssRules) {
|
|
2302
|
+
const nested = this._serializeScopedCssRules(Array.from(nestedRule.cssRules));
|
|
2303
|
+
if (!nested)
|
|
2304
|
+
return '';
|
|
2305
|
+
const ctorName = nestedRule.constructor?.name ?? '';
|
|
2306
|
+
if (ctorName.includes('Layer')) {
|
|
2307
|
+
return `@layer ${nestedRule.name ?? ''} { ${nested} }`;
|
|
2308
|
+
}
|
|
2309
|
+
if (ctorName.includes('Container')) {
|
|
2310
|
+
return `@container ${nestedRule.conditionText ?? ''} { ${nested} }`;
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
return '';
|
|
2314
|
+
}
|
|
2315
|
+
_splitSelectorList(selectorText) {
|
|
2316
|
+
const selectors = [];
|
|
2317
|
+
let current = '';
|
|
2318
|
+
let depthRound = 0;
|
|
2319
|
+
let depthSquare = 0;
|
|
2320
|
+
for (const char of selectorText) {
|
|
2321
|
+
if (char === '(')
|
|
2322
|
+
depthRound += 1;
|
|
2323
|
+
if (char === ')')
|
|
2324
|
+
depthRound = Math.max(0, depthRound - 1);
|
|
2325
|
+
if (char === '[')
|
|
2326
|
+
depthSquare += 1;
|
|
2327
|
+
if (char === ']')
|
|
2328
|
+
depthSquare = Math.max(0, depthSquare - 1);
|
|
2329
|
+
if (char === ',' && depthRound === 0 && depthSquare === 0) {
|
|
2330
|
+
if (current.trim())
|
|
2331
|
+
selectors.push(current.trim());
|
|
2332
|
+
current = '';
|
|
2333
|
+
continue;
|
|
2334
|
+
}
|
|
2335
|
+
current += char;
|
|
2336
|
+
}
|
|
2337
|
+
if (current.trim())
|
|
2338
|
+
selectors.push(current.trim());
|
|
2339
|
+
return selectors;
|
|
2340
|
+
}
|
|
2341
|
+
_scopeMirroredSelector(selector) {
|
|
2342
|
+
const scopeClass = `.${EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS}`;
|
|
2343
|
+
let scopedSelector = selector
|
|
2344
|
+
.replace(/(^|\s|>|\+|~)(:root|html|body)(?=\b|\s|[.#[:>+~]|$)/g, `$1${scopeClass}`);
|
|
2345
|
+
const themePatterns = [
|
|
2346
|
+
/\.dark-mode(?![\w-]|\\:)/g,
|
|
2347
|
+
/\.dark(?![\w-]|\\:)/g,
|
|
2348
|
+
/\[dark-mode\]/g,
|
|
2349
|
+
/\[darkmode\]/g,
|
|
2350
|
+
/\[data-theme=(?:"dark"|'dark'|dark)\]/g,
|
|
2351
|
+
/\[theme=(?:"dark"|'dark'|dark)\]/g,
|
|
2352
|
+
];
|
|
2353
|
+
let injectedThemeScope = false;
|
|
2354
|
+
themePatterns.forEach((pattern) => {
|
|
2355
|
+
scopedSelector = scopedSelector.replace(pattern, (match) => {
|
|
2356
|
+
injectedThemeScope = true;
|
|
2357
|
+
return `${match}${scopeClass}`;
|
|
2358
|
+
});
|
|
2359
|
+
});
|
|
2360
|
+
if (!scopedSelector.includes(EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS) && !injectedThemeScope) {
|
|
2361
|
+
scopedSelector = `${scopeClass} ${scopedSelector}`;
|
|
2362
|
+
}
|
|
2363
|
+
return scopedSelector;
|
|
2364
|
+
}
|
|
2365
|
+
_normalizeCustomOptionAccessibility(optionEl, meta) {
|
|
2366
|
+
if (!optionEl.hasAttribute('aria-label') && !optionEl.hasAttribute('aria-labelledby')) {
|
|
2367
|
+
optionEl.setAttribute('aria-label', meta.label);
|
|
2368
|
+
}
|
|
2369
|
+
const nestedFocusableSelectors = [
|
|
2370
|
+
'a[href]',
|
|
2371
|
+
'button',
|
|
2372
|
+
'input',
|
|
2373
|
+
'select',
|
|
2374
|
+
'textarea',
|
|
2375
|
+
'summary',
|
|
2376
|
+
'[tabindex]',
|
|
2377
|
+
'[contenteditable=""]',
|
|
2378
|
+
'[contenteditable="true"]',
|
|
2379
|
+
'[role="button"]',
|
|
2380
|
+
'[role="link"]',
|
|
2381
|
+
'[role="checkbox"]',
|
|
2382
|
+
'[role="switch"]',
|
|
2383
|
+
'[role="radio"]',
|
|
2384
|
+
'[role="textbox"]',
|
|
2385
|
+
].join(', ');
|
|
2386
|
+
optionEl.querySelectorAll(nestedFocusableSelectors).forEach((child) => {
|
|
2387
|
+
if (child === optionEl)
|
|
2388
|
+
return;
|
|
2389
|
+
child.tabIndex = -1;
|
|
2390
|
+
if (!child.hasAttribute('data-smilodon-keep-role')) {
|
|
2391
|
+
child.setAttribute('role', 'presentation');
|
|
2392
|
+
}
|
|
2393
|
+
if ((meta.disabled || child.matches('button, input, select, textarea, summary, a[href]')) && !child.hasAttribute('data-smilodon-keep-aria')) {
|
|
2394
|
+
child.setAttribute('aria-hidden', 'true');
|
|
2190
2395
|
}
|
|
2191
2396
|
});
|
|
2192
2397
|
}
|
|
@@ -2650,7 +2855,7 @@ class EnhancedSelect extends HTMLElement {
|
|
|
2650
2855
|
}
|
|
2651
2856
|
_createOptionsContainer() {
|
|
2652
2857
|
const container = document.createElement('div');
|
|
2653
|
-
container.className =
|
|
2858
|
+
container.className = `options-container ${EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS}`;
|
|
2654
2859
|
return container;
|
|
2655
2860
|
}
|
|
2656
2861
|
_createLiveRegion() {
|
|
@@ -5659,6 +5864,9 @@ class EnhancedSelect extends HTMLElement {
|
|
|
5659
5864
|
if (this._config.styles.classNames?.groupHeader) {
|
|
5660
5865
|
header.classList.add(...this._config.styles.classNames.groupHeader.split(' ').filter(Boolean));
|
|
5661
5866
|
}
|
|
5867
|
+
if (!header.hasAttribute('role')) {
|
|
5868
|
+
header.setAttribute('role', 'presentation');
|
|
5869
|
+
}
|
|
5662
5870
|
header.setAttribute('part', 'group-header');
|
|
5663
5871
|
this._optionsContainer.appendChild(header);
|
|
5664
5872
|
group.options.forEach(item => {
|
|
@@ -5934,9 +6142,6 @@ class EnhancedSelect extends HTMLElement {
|
|
|
5934
6142
|
if (!optionEl.getAttribute('role')) {
|
|
5935
6143
|
optionEl.setAttribute('role', 'option');
|
|
5936
6144
|
}
|
|
5937
|
-
if (!optionEl.getAttribute('aria-label')) {
|
|
5938
|
-
optionEl.setAttribute('aria-label', meta.label);
|
|
5939
|
-
}
|
|
5940
6145
|
optionEl.setAttribute('aria-selected', String(meta.selected));
|
|
5941
6146
|
if (meta.disabled) {
|
|
5942
6147
|
optionEl.setAttribute('aria-disabled', 'true');
|
|
@@ -5953,6 +6158,10 @@ class EnhancedSelect extends HTMLElement {
|
|
|
5953
6158
|
// Re-attaching here causes the double-click toggle bug if a child component fails to stopPropagation.
|
|
5954
6159
|
this._customOptionBoundElements.add(optionEl);
|
|
5955
6160
|
}
|
|
6161
|
+
this._normalizeCustomOptionAccessibility(optionEl, {
|
|
6162
|
+
label: meta.label,
|
|
6163
|
+
disabled: meta.disabled,
|
|
6164
|
+
});
|
|
5956
6165
|
return optionEl;
|
|
5957
6166
|
}
|
|
5958
6167
|
_addLoadMoreTrigger() {
|
|
@@ -5987,6 +6196,7 @@ class EnhancedSelect extends HTMLElement {
|
|
|
5987
6196
|
}
|
|
5988
6197
|
/** live set of all connected instances; used to auto-close siblings */
|
|
5989
6198
|
EnhancedSelect._instances = new Set();
|
|
6199
|
+
EnhancedSelect._MIRRORED_STYLES_SCOPE_CLASS = 'smilodon-mirrored-styles-scope';
|
|
5990
6200
|
// Register custom element
|
|
5991
6201
|
if (!customElements.get('enhanced-select')) {
|
|
5992
6202
|
customElements.define('enhanced-select', EnhancedSelect);
|