{"id":34246,"date":"2016-05-10T17:01:39","date_gmt":"2016-05-10T11:31:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=34246"},"modified":"2016-05-10T17:02:00","modified_gmt":"2016-05-10T11:32:00","slug":"introduction-to-shadow-dom","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/introduction-to-shadow-dom\/","title":{"rendered":"Introduction to Shadow DOM"},"content":{"rendered":"<p style=\"text-align: left\"><strong>Web Components\u00a0<\/strong>consists of several independent technologies, <strong>Shadow Dom<\/strong> is one of them. Web components are reusable user interface widgets that are created using open Web technology. They are fully browser supported or we can say they are part of the browser, so they don&#8217;t need external libraries like jQuery or Polymer.<\/p>\n<p style=\"text-align: left\">Web Components is a combination of four technologies :-<\/p>\n<ol>\n<li style=\"text-align: left\">\u00a0Custom Elements.<\/li>\n<li style=\"text-align: left\">\u00a0HTML Templates.<\/li>\n<li style=\"text-align: left\">\u00a0Shadow Dom.<\/li>\n<li style=\"text-align: left\">\u00a0HTML imports.<\/li>\n<\/ol>\n<p>In this blog we&#8217;ll be focusing on shadow DOM.\u00a0Currently only <strong>Chrome and Opera Browsers<\/strong> supports it. It\u00a0provides encapsulation for styles, JavaScript and templating so that these things remains separate from rest of the DOM.<\/p>\n<p>Now one question arises that why would one wants to keep some code separate from rest of the page? One reason is that on a large site, for\u00a0example, \u00a0if the <strong>css<\/strong> is not carefully organised, then the styling for the navigation can &#8220;leak&#8221; into the main content, where it was not intended to go, or vice-versa.<\/p>\n<p>Now let&#8217;s take an example to understand this problem, you all are familiar with the <strong>audio tag.\u00a0<\/strong>Have you ever seen that your style overriding the style of audio tag. No, this is because audio tag renders its view and styling from shadow dom.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-34266\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/05\/Screenshot-from-2016-05-03-131046.png\" alt=\"Screenshot from 2016-05-03 13:10:46\" width=\"567\" height=\"216\" \/><\/p>\n<p>To enable shadow dom in chrome browser open Developer tools by Ctrl+Shift+I. Click on 3 vertical\u00a0dots on the top right corner of dev tools before close button. Now click on settings there you will find <strong>&#8220;Show user agent shadow DOM&#8221;<\/strong> checkbox under the Elements section in General Tab. By checking that button it will enable shadow dom on one&#8217;s browser.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-34268\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/05\/Screenshot-from-2016-05-03-132900.png\" alt=\"Screenshot from 2016-05-03 13:29:00\" width=\"1273\" height=\"364\" \/><\/p>\n<p>Shadow dom can be appended to any kind of HTML node (e.g., div, button, section etc). With shadow dom, element get a new kind of node associated with them,\u00a0this new node is known as\u00a0<strong>Shadow Root.<\/strong> An element that has a shadow\u00a0root associated with it is called a <strong>shadow host<\/strong>.<\/p>\n<p><strong>Note : &#8211;\u00a0<\/strong>The content of a <strong>shadow host<\/strong> isn\u2019t rendered; the content of the <strong>shadow root<\/strong> is rendered instead.<\/p>\n<p>For example, if you had markup like this:<\/p>\n<p>[code language=&#8221;html&#8221;]<br \/>\n&lt;button id=&quot;btn&quot;&gt;Hello, world!&lt;\/button&gt;<br \/>\n&lt;script&gt;<br \/>\nvar host = document.querySelector(&#8216;#btn&#8217;);<br \/>\nvar root = host.createShadowRoot();<br \/>\nroot.textContent = &#8216;Hello, friends&#8217;;<br \/>\n&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>For output of above code click <a title=\"Shadow dom example\" href=\"https:\/\/plnkr.co\/edit\/6CHxwUaILj37QFRQf883?p=preview\">here<\/a><\/p>\n<p>Here as we can see that instead of\u00a0<strong>&#8220;hello, world&#8221;<\/strong>, <strong>&#8220;hello, friends&#8221;<\/strong> gets rendered on button. Not only that, if JavaScript on the page asks what the button&#8217;s textContent is, it isn\u2019t going to get \u201cHello, friends\u201d, but \u201cHello, world!\u201d because the DOM subtree under the shadow root is encapsulated.<\/p>\n<h2>Content Tag<\/h2>\n<p>If you want to render the content of shadow host then content tag is used to accomplish this task.\u00a0<a title=\"Rendering from host content section\" href=\"https:\/\/plnkr.co\/edit\/duW1CnIXxVGvzmnjBDjr?p=preview\">Demo<\/a><\/p>\n<p>[code language=&#8221;html&#8221;]<br \/>\n&lt;button class=&quot;red&quot;&gt;My Button&lt;\/button&gt;<br \/>\n&lt;script&gt;<br \/>\nvar button = document.querySelector(&#8216;button&#8217;);<br \/>\nvar root = button.createShadowRoot();<br \/>\nroot.innerHTML = &#8216;&lt;content&gt;&lt;\/content&gt;&#8217;;<br \/>\n&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>Content tag is similar to transclude property in Angularjs. If you want to choose which part of shadow host will be rendered by shadow root then select attribute in content tag is used. The value of select attribute can be element&#8217;s <strong>id, class name or node name<\/strong>.\u00a0<a title=\"Use of select attribute\" href=\"https:\/\/plnkr.co\/edit\/9tU68phBXmzs5NCbb5AR?p=preview\">Demo<\/a><\/p>\n<h2>Styling the host element<\/h2>\n<p>The <code>:host css property<\/code> allows you to select and style the element hosting a shadow tree: <a title=\"Styling host element\" href=\"https:\/\/plnkr.co\/edit\/nvX1sr5v3XLG8hJ9W6BA?p=preview\">Demo<\/a><\/p>\n<p>[code language=&#8221;html&#8221;]<br \/>\n&lt;button class=&quot;red&quot;&gt;My Button&lt;\/button&gt;<br \/>\n&lt;script&gt;<br \/>\nvar button = document.querySelector(&#8216;button&#8217;);<br \/>\nvar root = button.createShadowRoot();<br \/>\nroot.innerHTML = &#8216;&lt;style&gt;&#8217; +<br \/>\n    &#8216;:host { text-transform: uppercase; }&#8217; +<br \/>\n    &#8216;&lt;\/style&gt;&#8217; +<br \/>\n    &#8216;&lt;content&gt;&lt;\/content&gt;&#8217;;<br \/>\n&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>Those are the basics of shadow dom. Shadow DOM gives us scoped style encapsulation and a means to let in as much (or as little) of the outside world as we choose. I hope you\u2019ll agree that shadow dom is extremely powerful. For the first time we\u2019ve proper encapsulation without extra baggage of &lt;iframe&gt;s.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Web Components\u00a0consists of several independent technologies, Shadow Dom is one of them. Web components are reusable user interface widgets that are created using open Web technology. They are fully browser supported or we can say they are part of the browser, so they don&#8217;t need external libraries like jQuery or Polymer. Web Components is a [&hellip;]<\/p>\n","protected":false},"author":911,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":25},"categories":[1],"tags":[3041,55,3285,3286,3258],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34246"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/911"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=34246"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34246\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=34246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=34246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=34246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}