I have a simple custom element whose scrollbars I have styled, in the light DOM with the ::-webkit-scrollbar family of pseudo elements.
customElements.define(
'my-element',
class extends HTMLElement {
connectedCallback() {
this.attachShadow({mode: 'open'})
.innerHTML = `
<style>
:host {
display: block;
overflow: scroll;
}
</style>
<slot></slot>`
}
}
)
my-element {
width: 50vw;
height: 50vh;
border: 4px solid green;
}
p {
border: 4px solid red;
width: 75vw;
}
my-element::-webkit-scrollbar {
block-size: 1rem;
}
my-element::-webkit-scrollbar-track {
background-color: black
}
my-element::-webkit-scrollbar-thumb {
background-color: red;
}
<my-element>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Totam iste esse eum molestias dolore fuga cupiditate! Ducimus est omnis ipsum aperiam, esse beatae dolorum repudiandae veritatis delectus labore dignissimos ratione!</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Totam iste esse eum molestias dolore fuga cupiditate! Ducimus est omnis ipsum aperiam, esse beatae dolorum repudiandae veritatis delectus labore dignissimos ratione!</p>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Vitae sunt vel dignissimos mollitia explicabo? Praesentium tempore nesciunt possimus non ex. Fugit, distinctio! Animi magni debitis totam est neque quia et?</p>
</my-element>
BUT, I need to be able to move the light DOM scrollbar styling into the element itself, presumably using the shadow dom. I've tried adding
::-webkit-scrollbar {
block-size: 1rem;
}
::-webkit-scrollbar-track {
background-color: black
}
::-webkit-scrollbar-thumb {
background-color: red;
}
to the shadow dom's style but the scrollbars don't get styled. My bet there is some selector magic I need!
So what do I need to do?