use in the shadowDom (custom Element)
See original GitHub issueThanks for your great work.
At this moment, I lost a way to apply iconify in the custom elements. Could you help me?
What I did;
- I made a small and simple web component to display some messages on a page. I want to add some iconify icons in that custom element.
- Of course, I’ve properly loaded iconify js (2.0.0-beta) on the page at first.
- Also surely, icons are displayed normally outside of my custom element.
Slot
also works. Icons injected withslot
in shadowDom from the outer page would work as expected.- I could render a specific iconify icon with
Iconify.renderSVG()
in the customElement. This was a very hopeful sign for me because Iconify.js seems working in shadowDom without import JS in each customElement. When I tried with Font-Awesome, I should have injected all CSS rules into shadowDom because there be some issue about@font-face source path
But…
I’ve found only slotted
icon was rendered again correctly. Other’s were not. Maybe my approach was totally wrong.
EDIT
Renderable icons seem being cached
.
**Problem **
// index.html
<mz-test>
<span slot="ic" class="iconify-inline" data-icon="mdi-ab-testing"></span>
</mz-test>
// mz-test shadowDom
<p id="test1">slotted icon: <slot name="ic"></slot></p>
<p id="test2">direct rendering icon slotted:</p>
<p id="test3">direct rendering icon unslotted:</p>
<p id="test4">how to auto-convert??: <span class="iconify" data-icon="noto:angry-face" data-inline="false"></span></p>
// Trying to rendering after connectedCallback
var slotted = Iconify.renderHTML('mdi-ab-testing')
var test2 = myShadowDom.querySelector('#test2')
test2.innerHTML += slotted
var unslotted = Iconify.renderHTML('mdi-account-box')
var test3 = myShadowDom.querySelector('#test3')
test3.innerHTML += unslotted
What is the best approach to use iconify in custom Element? Could you show some hint?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Using shadow DOM - Web Components | MDN
Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with...
Read more >Shadow DOM v1 - Self-Contained Web Components
Shadow DOM is particularly useful when creating custom elements. Use shadow DOM to compartmentalize an element's HTML, CSS, and JS, thus ...
Read more >Shadow DOM - The Modern JavaScript Tutorial
Shadow tree can be used in Custom Elements to hide component internals and apply component-local styles. For example, this <show-hello> element ...
Read more >The Complete Web Component Guide: Shadow DOM - Medium
Combined, we can use them to create custom elements with encapsulated functionality from the rest of our code: Custom elements · HTML templates ......
Read more >Shadow DOM and Custom HTML Elements - DZone Web Dev
First, you define a class ( FlagIcon ), which needs to extend from HTMLElement , which is a web standard. You'd need to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in beta 4.
Changes:
I’ve changed behaviour of scanDOM function. If you call it with a custom root node as first parameter, that node will be temporarily added to list of root nodes, until all placeholders in that node are replaced. This makes it possible to run scanDOM on a shadow DOM root once instead of waiting for API to respond.
That means when you add placeholders to a shadow DOM, you can call scanDOM(shadowRoot) once and all icons will be replaced, even if it they aren’t loaded yet.
Next step is to refactor observer module, so it could be used with multiple root nodes, which would make it possible to support multiple root nodes in observer. It will replace setRoot() with addRoot() and removeRoot() functions. This would give more flexible approach, allowing to automatically observe changes in shadow DOM, then unsubscribe when shadow DOM is no longer needed.