-
Notifications
You must be signed in to change notification settings - Fork 0
Web Components
This page is not revised in a professional format. These are just notes for my reference when on a company computer, ripped directly out of my evernote with no formatting... I hope anyone coming across this gets some use out of them.
- Web Components consist of 4 main features which can be used separately or all together:
- Custom Elements - APIs to define new HTML elements
- Shadow DOM - Encapsulated DOM and styling, with composition
- HTML Imports - Declarative methods of importing HTML documents into other documents
- HTML Templates - The
- Web Components encapsulate HTML, CSS, but not JavaScript
- Use a Immediately Invoked Function Expression for this
- createdCallback function serves this purpose.
- Use a Immediately Invoked Function Expression for this
- Uses in the underlying architecture of Angular 2 https://www.webcomponents.org/introduction
Official Dom and shadow DOM documentation https://dom.spec.whatwg.org/#shadow-trees https://w3c.github.io/webcomponents/spec/shadow/
Official Custom Element Documentation https://html.spec.whatwg.org/multipage/scripting.html#custom-elements
Differences between Shadow Dom v0 and v1 ( and other stuff ) https://hayato.io/2016/shadowdomv1/
Talks about the different ways people want to go about web components. There seems to be a lot of disagreement. https://hacks.mozilla.org/2015/06/the-state-of-web-components/
Great website for general web topics https://developers.google.com/web/
- Requirements
- name must contain a dash
- prototype must extend HTMLElement
- Allows us to bundle markup and styles into custom html elements.
- fully encapsulate all their html and css
- Allows lifecycle reactions
- connectedCallback - Called when the element is inserted into the DOM
- disconnectedCallback - Called when the element is removed from the DOM
- attributeChangedCallback - Called when an attribute of the element is added, removed, updated, or replaced
- Creating a custom element with “Upgrade” method ( old syntax )
- 1) Create prototype extending HTML Element
- 2) Create createCallback method in document object
- When the browsers parser finds a custom html tag it will immediately check its createCallback function. If found, it will run immediately.
- This is good for setting up the custom element.
- Can use it to create shadow Dom and clone template into it.
- We pass the tag name and prototype to a new method on the document, called registerElement, and after that we're ready to go.
- Creating a custom element with synchronous constructor method
- Use getters and setters to define properties
- Set properties in html by using the “data-“ assignment
- Set properties with javascript with “.” notation
- Create and Declare elements
- Enable by using the Document.registerElement() method
- Declare the element
- Use HTML
- <load-button></load-button>
- Use JavaScript
- Style and Adding Markup
- Style within the connectedCallback()
- V1 Standard Concepts
- customElements global ( replaces document.registerElement )
- the customElements global is used to define a custom element
- call customElements.define() with the tag name you want to create and a class that represents that element
- Why?
- Allows for separation of concern from browser and developer.
- Dev can’t access the Shadow DOM in the same way they would access nested elements
- Browser can render and modify that code in the same way it would any other elements.
- Allows for encapsulation of CSS styling, preventing external styling from leaking into the object
- Can still fire events that can be picked up by other elements in the doc.
- Shadow Tree
- Scoped subtree in an element
- Element that it is attached to is called a shadow Host
- Shadow Host
- Call createShadowRoot on an element in the DOM to attach a ShadowHost to it.
- This is the only piece visible to the user, how they interact with it.
- Kind of like a method signature, its where they would put their parameters.
- Shadow Root
- document fragment returned by createShadowRoot
- ShadowRoot and its children are hidden from the user, but the browser uses it like normal html/css
- Shadow Boundary
- The thing that actually encapsulates the shadow DOM
- prevents css from bleeding into shadow DOM
- prevents javascript from going into shadow root
- Must always be connected to existing elements in the DOM either through scripting or manual attachment
- Attach shadow dom to element with element.attachShadow({mode : ‘open’}) or element.attachShadow({mode : ‘closed’})
- Essential for creating custom elements.
- Without shadow DOM, different custom elements could interact in unwanted ways.
- Two modes for shadow DOM
- Open: You can access the Shadow DOM with the shadowRoot property of the HTMLElement (this was the default in v0)
- Closed: You can’t access the Shadow Dom with the shadowRoot property, it returns null instead ( the video tag is an example of closed mode. javascript can’t access it)
- Certain elements can’t host a shadow tree
- If that element already has its own internal shadow dom (input or texture)
- if it doesn’t make sense for that element to host a shadow dom ( img )
- Insertion Points
- Analogous to creating parameters for a function call
- If you want the images from the image slider to be provide by the user you must use this.
- Use the <content> tag to pull items into the shadow DOM
- <content> tag uses CSS selectors to pick elements from the shadow host and project them into the shadow DOM
- The projection itself is technically known as the Insertion Point
- Shadow dom has some specific css selectors
- It allows you to store markup and reuses it later. (so like object oriented html?)
- Everything inside a template is considered inert by browsers.
- This means that nothing in the template is rendered until we activate it with javascript