How to use Material Design Icons in a web component?

后端 未结 1 456
天涯浪人
天涯浪人 2021-01-24 23:50

Looks like that mdi is not working inside web components, or do I miss something?

I want to develop a web component that encapsulates it\'s dependencies, adding the link

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 00:11

    The @font-face CSS at-rule for the font you want to use must be declared in the main document, not in the Shadow DOM.

    Because in your case it is defined in the materialdesignicons.min.css file, you'll need to load it in the main document via a global .

    Note that the CSS file won't be loaded twice thanks to the browser's cache.

    Alternately, you could add it in the light DOM of the web component, or you could just declare the @font-face at-rule (copied from the materialdesignicons.css file).

    Here is a running example:

    customElements.define( "x-webcomponent", class extends HTMLElement {
        constructor() {
          super()
          this.attachShadow({ mode: "open" })
          this.shadowRoot.innerHTML = `
            
            `
        }
        connectedCallback () {
          this.innerHTML = ``
        }
    } )

    0 讨论(0)
提交回复
热议问题