Shadow DOM
   HOME

TheInfoList



OR:

Web Components are a set of features that provide a standard component model for the Web allowing for encapsulation and
interoperability Interoperability is a characteristic of a product or system to work with other products or systems. While the term was initially defined for information technology or systems engineering services to allow for information exchange, a broader defi ...
of individual HTML elements. Primary technologies used to create them include: * Custom Elements: APIs to define new HTML elements * Shadow DOM: encapsulated DOM and styling, with composition * HTML Templates: HTML fragments that are not rendered, but stored until instantiated via JavaScript


Features


Custom Elements

There are two parts to Custom Elements: autonomous custom elements and customized built-in elements. Autonomous custom elements are
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaSc ...
elements that are entirely separated from native
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaSc ...
elements; they are essentially built from the bottom up using the Custom Elements
API An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how ...
. Customized built-in elements are elements that are built upon native HTML elements to reuse their functionality.


Shadow DOM

Shadow DOM is a functionality that allows the
web browser A web browser is application software for accessing websites. When a user requests a web page from a particular website, the browser retrieves its files from a web server and then displays the page on the user's screen. Browsers are used o ...
to render DOM elements without putting them into the main document DOM tree. This creates a barrier between what the developer and the browser can reach; the developer cannot access the Shadow DOM in the same way they would with nested elements, while the browser can render and modify that code the same way it would with nested elements. The impact of CSS scoped within the Shadow DOM of a particular element is that
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaSc ...
elements can be encapsulated without the risk of CSS styles leaking and affecting elements that they were not supposed to affect. Although these elements are encapsulated with regard to HTML and CSS, they can still fire events that can be picked up by other elements in the document. The scoped subtree in an element is called a shadow tree. The element the shadow tree is attached to is called a shadow host. Shadow DOM must always be connected to an existing element, either through attaching it as a literal element or through
script Script may refer to: Writing systems * Script, a distinctive writing system, based on a repertoire of specific elements or symbols, or that repertoire * Script (styles of handwriting) ** Script typeface, a typeface with characteristics of handw ...
ing. In
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
, Shadow DOMs are attached to an element using Element.attachShadow().


HTML Template

HTML template is a way to insert chunks of HTML that are stamped at will. The syntax of HTML templates looks like this: Scripts will not run, and resources that are inside a template will not be fetched until the template is stamped out.


Browser support

Web Components are supported by current versions of all major browsers. Backward compatibility with older browsers is implemented using
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
-based polyfills.


Libraries

There are many libraries that are built on Web Components with the aim of increasing the level of abstraction when creating custom elements. Some of these libraries ar
X-TagSlim.js
Polymer A polymer (; Greek '' poly-'', "many" + ''-mer'', "part") is a substance or material consisting of very large molecules called macromolecules, composed of many repeating subunits. Due to their broad spectrum of properties, both synthetic a ...

BosonicRiot.jsSalesforce Lightning Web ComponentsDataFormsJS
an
Telepathy
From the above list, Bosonic, Polymer, Telepathy and DataFormsJS, all provide ready-made components that are free to use. These components can be used interchangeably as they are all built on open web technologies. While Bosonic, Polymer & DataFormsJS have far more pre-built components. Telepathy is "closest to the metal" in that its sole focus is on helping the developer to build easily maintained webcomponents rapidly.


Community

There are numerous community efforts for the Web Components ecosystem. WebComponents.org provides an interface to search for any existing Web Components, Custom Elements Everywhere validates whether popular front-end frameworks are compatible and ready to use Web Components standard, with a set of pending bugs and available workarounds. Moreover, Vaadin Tutorials has a dedicated section that shows how those workarounds are used efficiently with example demo apps and similarly related topics.


History

In 2011, Web Components were introduced for the first time by Alex Russell at Fronteers Conference. In 2013,
Polymer A polymer (; Greek '' poly-'', "many" + ''-mer'', "part") is a substance or material consisting of very large molecules called macromolecules, composed of many repeating subunits. Due to their broad spectrum of properties, both synthetic a ...
, a library based on Web Components was released by
Google Google LLC () is an American Multinational corporation, multinational technology company focusing on Search Engine, search engine technology, online advertising, cloud computing, software, computer software, quantum computing, e-commerce, ar ...
. In 2018,
Firefox Mozilla Firefox, or simply Firefox, is a free and open-source web browser developed by the Mozilla Foundation and its subsidiary, the Mozilla Corporation. It uses the Gecko rendering engine to display web pages, which implements current ...
63 enabled Web Components support by default and updated the developer tools to support them.


References


External links

* **
Web Components Specifications
WICG ** WICG specifications
Web Components
at
MDN Web Docs MDN Web Docs, previously Mozilla Developer Network and formerly Mozilla Developer Center, is a documentation repository and learning resource for web developers. It was started by Mozilla in 2005 as a unified place for documentation about open web ...

Browser Support for Custom Elements
at Can I Use?
Custom Elements Everywhere - Framework Support for Custom ElementsOpen Web ComponentsWhat are Web Components?twoBirds
{{W3C standards Web standards Web programming