I am trying to add a custom scrollbar to a web component.
I am not very familiar with web components but i understand that it is difficult to apply pseudo-elements to parts. From my reading, one way to apply styling in the shadow dom that might work is to include the style inline in the javascript of the component? However my js skills aren’t good enough to know where to place it in the js! Can anyone advise, where should the style go in the below to apply styles to modal class within the x-modal component?
Thanks in advance for any help with this.
// js/common/overlay/modal.js
import { animate as animate5, timeline as timeline2 } from "vendor";
var Modal = class extends DialogElement {
connectedCallback() {
super.connectedCallback();
this.setAttribute("aria-modal", "true");
}
get shadowDomTemplate() {
return this.getAttribute("template") || "modal-default-template";
}
get shouldLock() {
return true;
}
get shouldAppendToBody() {
return true;
}
createEnterAnimationControls() {
if (matchesMediaQuery("sm")) {
return animate5(this, { opacity: [0, 1] }, { duration: 0.2 });
} else {
return timeline2([
[this.getShadowPartByName("overlay"), { opacity: [0, 1] }, { duration: 0.3, easing: [0.645, 0.045, 0.355, 1] }],
[this.getShadowPartByName("content"), { transform: ["translateY(100%)", "translateY(0)"] }, { duration: 0.3, easing: [0.645, 0.045, 0.355, 1], at: "<" }]
]);
}
}
createLeaveAnimationControls() {
if (matchesMediaQuery("sm")) {
return animate5(this, { opacity: [1, 0] }, { duration: 0.2 });
} else {
return timeline2([
[this.getShadowPartByName("overlay"), { opacity: [1, 0] }, { duration: 0.3, easing: [0.645, 0.045, 0.355, 1] }],
[this.getShadowPartByName("content"), { transform: ["translateY(0)", "translateY(100%)"] }, { duration: 0.3, easing: [0.645, 0.045, 0.355, 1], at: "<" }]
]);
}
}
};
if (!window.customElements.get("x-modal")) {
window.customElements.define("x-modal", Modal);
}
.modal::-webkit-scrollbar {
background-color: #fff;
width: 16px;
overflow: auto;
} /* SUB background of the scrollbar except button or resizer */
.modal::-webkit-scrollbar-track {
background-color: #fff;
} /* SUB scrollbar itself */
.modal::-webkit-scrollbar-thumb {
background-color: #babac0;
border-radius: 16px;
border: 4px solid #fff;
} /* SUB set button(top and bottom of the scrollbar) */
.modal::-webkit-scrollbar-button {
display: none;
}
2
Answers
I don’t see the constructor in the modal class in your code.
Please modify your code.
And then you need to use Shadow DOM.
A reference code.
We got here because Chromium doesn’t support
::part(name)::webkit-scrollbar
yet.So you want to inject that
webkit
stuff straight into a shadowDOMChanging a
mode:"open"
shadowDOM is doable after it was created in the DOM, but a PITAEasier is to stick to the OOP stuff and extend your own
my-x-modal
from the originalx-modal
Then append an extra
<style>
tag to the shadowDOMFYI The original Wordle was a Web Component. With the above method it was "enhanced" to do:
https://mordle.github.io/?sentence=danny,hacks,super,wordle,wordle,lingo,words
Long blog post:
https://dev.to/dannyengelman/more-wordle-mordle-extending-josh-his-web-components-4li