Git Product home page Git Product logo

Comments (2)

eavichay avatar eavichay commented on May 28, 2024

Hi.

Edited: Until constructible style-sheets will be fully implemented, this is a suggestion - based on native behavior of browsers.

Using the shadow root is NOT mandatory. You can visit the documentation website - http:slimjs.com and see the source maps.

If you still wish to use shadow root, I recommend either using internal style nodes with @import statements or sharing styles using a custom element for style-sharing.

Modulizing the parts you wish to import into your component improves performance, in case you choose to go with shadow DOMs.

Check out this implementation (taken from a production code) - modulizing css into blocks and sharing between shadow DOMs.

const valueCache: Record<string, Promise<string>> = typeof (<any>window)['AppContext'] === 'object' ? (<any>window)['AppContext'].getConfig('styleCache') : {};

export class StyleLoader extends HTMLElement {
    connectedCallback () {
        const href = this.getAttribute('href');
        const name = this.getAttribute('name');

        if (!name || !href) {
            return;
        }
        if (valueCache[name] && typeof (<any>valueCache[name]).resolve === 'function') {
            const resolve = (<any>valueCache[name]).resolve;
            fetch(href)
                .then(r => r.text())
                .then(styleText => resolve(styleText.replace(/([\n|\r|\t])/gm, '')))
                .catch(() => {});
        }
        if (!valueCache[name]) {
            valueCache[name] = new Promise((resolve, reject) => {
                fetch(href).then(r => r.text()).then(styleText => resolve(styleText.replace(/([\n|\r|\t])/gm, ''))).catch(reject);
            });
        }
    }
}

export class SharedStyle extends HTMLStyleElement {
    public styleLoaded = false;
    constructor() {
        super();
        this.applyStyle();
    }

    connectedCallback() {
        this.applyStyle();
    }

    applyStyle() {
        if (this.styleLoaded) {
            return;
        }
        const name = this.getAttribute('name');
        if (name) {
            let p = valueCache[name];
            if (!p) {
                let resolve;
                p = new Promise<string>(res => resolve = res);
                (<any>p).resolve = resolve;
                valueCache[name] = p;
            }
            p.then(text => this.innerHTML = text);
            this.styleLoaded = true;
        }
    }
}

customElements.define('shared-style', SharedStyle, {extends: 'style'});
customElements.define('style-loader', StyleLoader);

the usage is simple:
in the main HTML

<style-loader href="/path/to/buttons.css" name="buttons"></style-loader>
<style-loader href="/path/to/menus.css" name="menus"></style-loader>

in your shadow root (any component)

<style is="shared-style" name="buttons"></style>

This implementation is performant, I'm using it on production on several projects, as long as you keep you CSS "blocks" separate correctly, it's super-fast.

from slim.js.

naiteon avatar naiteon commented on May 28, 2024

Thank you so much Avichay Eyal !

Mazel tov! It's a great library my friend

רודריגו

from slim.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.