From 2abc7cd40f2ff4eb903878f2c6ed9fea30a45876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20T=C3=B3th?= Date: Tue, 18 Jul 2023 10:21:33 +0200 Subject: [PATCH] fix: optimize css style with using adoptedStyleSheets --- src/option.js | 65 +++++++------ src/selectlist.js | 242 +++++++++++++++++++++------------------------- 2 files changed, 146 insertions(+), 161 deletions(-) diff --git a/src/option.js b/src/option.js index 2e446b7..7fd6f68 100644 --- a/src/option.js +++ b/src/option.js @@ -1,42 +1,44 @@ const template = document.createElement('template'); template.innerHTML = /* html */` - - -`; +:host(:focus-visible) { + outline: -webkit-focus-ring-color auto 1px; +} +`); class OptionElement extends globalThis.HTMLElement { static formAssociated = true; @@ -53,6 +55,7 @@ class OptionElement extends globalThis.HTMLElement { this.attachShadow({ mode: 'open' }); this.shadowRoot.append(template.content.cloneNode(true)); + this.shadowRoot.adoptedStyleSheets = [optionStyles]; } attributeChangedCallback(name, oldVal, newVal) { diff --git a/src/selectlist.js b/src/selectlist.js index 9a225fe..40ad11d 100644 --- a/src/selectlist.js +++ b/src/selectlist.js @@ -4,116 +4,110 @@ const popoverSupported = typeof HTMLElement !== 'undefined' && 'popover' in HTMLElement.prototype; const popoverStyles = popoverSupported ? '' : /* css */` - [popover] { - position: fixed; - z-index: 2147483647; - padding: .25em; - width: fit-content; - border: solid; - background: canvas; - color: canvastext; - overflow: auto; - margin: auto; - } +[popover] { + position: fixed; + z-index: 2147483647; + padding: .25em; + width: fit-content; + border: solid; + background: canvas; + color: canvastext; + overflow: auto; + margin: auto; +} - [popover]:not(.\\:popover-open) { - display: none; - } -`; +[popover]:not(.\\:popover-open) { + display: none; +} +`, + listboxStyles = /* css */` +[behavior="listbox"] { + box-sizing: border-box; + margin: 0; + min-block-size: 1lh; + max-block-size: inherit; + min-inline-size: inherit; + inset: inherit; +} +`, + popoverStyleSheet = new CSSStyleSheet(), + listboxStyleSheet = new CSSStyleSheet(), + selectlistStyleSheet = new CSSStyleSheet(), + documentStyles = new CSSStyleSheet(); + +popoverStyleSheet.replaceSync(popoverStyles); +listboxStyleSheet.replaceSync(listboxStyles); + +selectlistStyleSheet.replaceSync(/* css */ ` +:host { + display: inline-block; + position: relative; + font-size: .875em; +} -const listboxStyles = /* css */` - [behavior="listbox"] { - box-sizing: border-box; - margin: 0; - min-block-size: 1lh; - max-block-size: inherit; - min-inline-size: inherit; - inset: inherit; - } -`; +[part="button"] { + display: inline-flex; + align-items: center; + background-color: field; + cursor: default; + appearance: none; + border-radius: .25em; + padding: .25em; + border-width: 1px; + border-style: solid; + border-color: rgb(118, 118, 118); + border-image: initial; + color: buttontext; + line-height: min(1.3em, 15px); +} + +:host([disabled]) [part="button"] { + background-color: rgba(239, 239, 239, .3); + color: graytext; + opacity: .7; + border-color: rgba(118, 118, 118, .3); +} + +[part="marker"] { + height: 1em; + margin-inline-start: 4px; + opacity: 1; + padding-bottom: 2px; + padding-inline-start: 3px; + padding-inline-end: 3px; + padding-top: 2px; + width: 1.2em; +} + +[part="listbox"] { + box-sizing: border-box; + box-shadow: rgba(0, 0, 0, .13) 0px 12.8px 28.8px, + rgba(0, 0, 0, .11) 0px 0px 9.2px; + min-block-size: 1lh; + border-width: 1px; + border-style: solid; + border-color: rgba(0, 0, 0, .15); + border-image: initial; + border-radius: .25em; + padding: .25em 0; +} +`); /** * CSS @layer: any styles declared outside of a layer will override styles * declared in a layer, regardless of specificity. * https://developer.mozilla.org/en-US/docs/Web/CSS/@layer */ +documentStyles.replaceSync(/* css */ ` +@layer { + ${popoverStyles} + x-selectlist ${listboxStyles} +`); -const headTemplate = document.createElement('template'); -headTemplate.innerHTML = /* html */` - -`; - -document.head.prepend(headTemplate.content.cloneNode(true)); +document.adoptedStyleSheets.push(documentStyles); const template = document.createElement('template'); -template.innerHTML = /* html */` - +template.innerHTML = /* html */ `