1+ import '@vaadin/vaadin-lumo-styles/src/props/icons.css' ;
12import type { CSSResultGroup } from 'lit' ;
3+ import auraCss from '@vaadin/aura/aura.css?inline' ;
4+ import lumoCss from '@vaadin/vaadin-lumo-styles/lumo.css?inline' ;
25import docsCss from 'Frontend/themes/docs/styles.css?inline' ;
36
47function createStylesheet ( css : CSSResultGroup | string ) : CSSStyleSheet {
@@ -11,14 +14,44 @@ function createStylesheet(css: CSSResultGroup | string): CSSStyleSheet {
1114 return stylesheet ;
1215}
1316
14- const docsStylesheet = createStylesheet ( docsCss ) ;
17+ const lumoStyleSheet = createStylesheet ( lumoCss ) ;
18+ const auraStyleSheet = createStylesheet ( auraCss ) ;
19+ const docsStyleSheet = createStylesheet ( docsCss ) ;
20+
21+ function getRootHost ( root : DocumentOrShadowRoot ) {
22+ let host : Element ;
23+ if ( root instanceof ShadowRoot ) {
24+ host = root . host ;
25+ } else {
26+ host = ( root as Document ) . documentElement ;
27+ }
28+ return host ;
29+ }
1530
1631export function applyTheme ( root : DocumentFragment | DocumentOrShadowRoot | HTMLElement ) {
17- // The root parameter type is very broad to handle the default return type of
18- // LitElement.createRenderRoot. In general, we expect this to either be a document or a shadow
19- // root. The adoptedStyleSheets check below makes Typescript accept the parameter type.
20- if ( 'adoptedStyleSheets' in root && ! root . adoptedStyleSheets . includes ( docsStylesheet ) ) {
21- root . adoptedStyleSheets = [ ...root . adoptedStyleSheets , docsStylesheet ] ;
32+ if ( root instanceof ShadowRoot || root instanceof Document ) {
33+ const host = getRootHost ( root ) ;
34+
35+ const updateTheme = ( ) => {
36+ const adoptedStyleSheets = root . adoptedStyleSheets . filter (
37+ ( styleSheet ) => ! [ lumoStyleSheet , auraStyleSheet , docsStyleSheet ] . includes ( styleSheet )
38+ ) ;
39+
40+ if ( host . matches ( '[theme~="aura"]' ) ) {
41+ adoptedStyleSheets . push ( auraStyleSheet ) ;
42+ } else {
43+ adoptedStyleSheets . push ( lumoStyleSheet ) ;
44+ }
45+
46+ root . adoptedStyleSheets = [ ...adoptedStyleSheets , docsStyleSheet ] ;
47+ } ;
48+
49+ new MutationObserver ( updateTheme ) . observe ( host , {
50+ attributes : true ,
51+ attributeFilter : [ 'theme' ] ,
52+ } ) ;
53+
54+ updateTheme ( ) ;
2255 }
2356}
2457
0 commit comments