I am building a flex custom element which needs to style its children as well as itself. It is a "stack" element that sets its children to have a uniform vertical spacing. (This comes from reading every layout)
The html looks like:
<test-stack> <h3>Hello World</h3> <p>I'm a paragraph</p> <div>And I'm a div with text</div> </test-stack>
The simplified custom element is:
class TestStack extends HTMLElement {
constructor() {
super()
this.attachShadow({ mode: 'open' })
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
display: flex;
flex-direction: column;
justify-content: flex-start;
}
:host > * {
margin-block: 0;
}
:host > * + * {
margin-block-start: 2rem;
}
</style>
`
console.log('render', this.shadowRoot.innerHTML);
}
connectedCallback() {
this.render()
}
attributeChangedCallback() {
this.render()
}
}
customElements.define('test-stack', TestStack)
NOTE: The two selectors
:host > *
:host > * + *
.. obviously do not work but are examples of my trying to style all the children of a test-stack custom element.
So how does a custom element style it's children as well as itself? Flex is a good example of custom elements needing to do this. Maybe simply not possible with the shadow DOM?