lume / element

Fast and simple custom elements.
https://lume.io
MIT License
117 stars 4 forks source link

inject element's styles at the end of the ShadowRoot children #12

Closed trusktr closed 4 months ago

trusktr commented 5 months ago

otherwise the following will fail if styles are prepended:

        template = html`
            <!-- Element styles are currently prepended here. -->

            <!-- import a lib, f.e. bootstrap. -->
            <link rel="stylesheet" href="./lib.css" />

            <!-- Because lib.css ends up *after* element styles, element styles do not override lib styles. -->
            <div class="lib-btn"></div>
        `

Current workaround is to define styleRoot where styles will be appended:

        #styleRoot = document.createElement('div')

        get styleRoot() {
            return this.#styleRoot
        }

        template = html`
            <!-- import a lib, f.e. bootstrap. -->
            <link rel="stylesheet" href="./lib.css" />

            <!-- Element styles are prepended here in the styleRoot. -->
            ${this.styleRoot}

            <!-- Styles work as expected, element styles overrode lib styles. -->
            <div class="lib-btn"></div>
        `

PR:

trusktr commented 5 months ago

Related, once we implement

then the problem is solved in that case because adopted style sheets behave similar to styles appended to the end of ShadowRoot.