Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate <defs> from <template> #5

Open
sidewayss opened this issue Oct 16, 2024 · 0 comments
Open

Separate <defs> from <template> #5

sidewayss opened this issue Oct 16, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@sidewayss
Copy link
Owner

sidewayss commented Oct 16, 2024

Breaking out the template's <defs> into a separate SVG file would have these advantages:

  1. Different shapes/styles for different pages on the same site
  2. In theory, it's more efficient to have only one set of DOM definitions instead of repeating the <defs> in every shadow DOM, but I'm not sure if shadow DOM elements have visibility to a defs in the DOM.
  3. Both files would be more readable than the current single file

But how do you load a single <defs> for the page? You don't want to force the page to include some additional script file. It has to be done from inside BaseElement. The solution that comes to mind is:

class BaseElement {
    static #defsLoaded;                 // new static boolean
    constructor(template, name) {       // new name argument
        if (!BaseElement.#defsLoaded) {
            fetch(`defs-${name}.svg`)   // ...fall back to /html-templates
              .then(rsp => {            // ...fall back to /html-elements
                if (rsp.ok) {
                    rsp.text.then(text => {
                        const parser = new DOMParser;
                        document.insertBefore(parser.parseFromString(text), 
                                              document.body.firstElementChild);
                        BaseElement.#defsLoaded = true;                    
                    }
                }
            }).catch(err => console.error(err.stack ?? err));
        }
    }
}

Seems like a decent idea to me, but I'm not hurrying to implement it.

The same could be done with the template's <style>, but probably best to put it in this same SVG file instead of fetching a separate CSS file.

@sidewayss sidewayss added the enhancement New feature or request label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant