-
-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shadow-Dom Support #402
Comments
Good day @hallzy :) |
I don't know about "small", but I have created as small of an example as I can. https://jsfiddle.net/ex4p6mja/105/ While creating this, I realized that it isn't actually an issue with being inside a shadow dom, so much as it is with us trying to not pollute the global scope. If I include OverlayScrollbars as a script tag in the main HTML document (or even inside the shadow dom) it works fine. But both of those solutions mean that we have an OverlayScrollbars function attached to the main window object (shadow doms do not have their own window object). So, what we do is use ES modules to import code that we want into the files that we need access to that code. In some cases, as is the case with OverlayScrollbars, there is no module we can import like this. So our workaround is to create a throwaway iframe that we load all of our scripts in that cannot be loaded as a module, and when the script is finished loading we resolve a promise which we can import which has the result of the script execution. There isn't really a way to import modules that I could find in JS fiddle, so I just put it into the same file, but it would normally be imported. Once the promise finishes we call the scrollbar function on the scrollable div I created And the getHTML and getCSS functions at the bottom can be thought of as template imports that we would normally have so that we can easily insert this stuff into the shadow. So basically, we can work with OverlayScrollbars even if we don't have a module, but if we could just import a module and have that work, that would be easiest. And at least with the way we are doing it now with the iframe, we end up with a 30px right and border in chrome which as I mentioned in my previous description, I don't think it supposed to happen. I hope this helps :D My OS is Ubuntu, and the output of OverlayScrollbars.globals() is:
|
Hi @KingSora, just wondering if you've had a chance to look at this yet :) |
@hallzy not yet.. sorry I'm a bit busy, but I'll try to do it this weekend :) |
No problemo 😃 |
Has anyone gotten this working with a viewport / scroll container contained within a shadow DOM element / component? |
Adding my notes to this. I was able to get See this issue for notes: #682 (comment) You also need to manually call |
Hii theree, hope you're well 💗 I'm currently experiencing this issue of the Shadow DOM, I already injected the styles to the elements Shadow DOM using |
Hello, I have the same problem, is there a solution? |
So, for some reason I never went back to this to explain what I ended up doing. Unfortunately, it was for work, and I was laid off 6 months ago and no longer have access to the code and I cannot remember what we ended up doing. we did use this scroll bar though. sorry |
hii there! we finally managed to make it work! we're also developing a web component using Lit and aaalso using Material Design, our component uses /**
* Attaches a scroll event listener to the dropdown element.
* The listener triggers the `_handleScroll` method when the user scrolls within the dropdown.
*
* @private
*/
private _attachScrollListener() {
const scrollbarStylesheet = new CSSStyleSheet();
scrollbarStylesheet.replaceSync(overlayscrollbarStyles);
scrollbarStylesheet.insertRule(`
.os-theme-dark {
--os-handle-bg: var(--cds-menu-scrollbar-thumb-color, var(--cds-primary-color, #849BC6FF)) !important;
--os-handle-bg-hover: var(--cds-menu-scrollbar-thumb-hover-color, var(--cds-menu-scrollbar-thumb-color, var(--cds-primary-color, #849BC6FF))) !important;
--os-handle-bg-active: var(--cds-menu-scrollbar-thumb-active-color, var(--cds-menu-scrollbar-thumb-color, var(--cds-primary-color, #849BC6FF))) !important;
}
`);
this._dropdownRef.value?.shadowRoot?.adoptedStyleSheets.push(scrollbarStylesheet);
const items = this._dropdownRef.value?.shadowRoot?.querySelector('.items') as HTMLDivElement;
const scrollbar = OverlayScrollbars(items, {
scrollbars: { autoHide: 'scroll' },
overflow: { x: 'hidden' },
}, {
scroll: (_, ev) => {
const scrollContainer = ev.target as HTMLDivElement;
if (!scrollContainer) {
return;
}
const { scrollTop, scrollHeight, clientHeight } = scrollContainer;
const scrollPercentage = scrollTop / (scrollHeight - clientHeight);
const scrollbarElement = scrollbar.elements().scrollbarVertical.scrollbar;
scrollbarElement.style.setProperty('--os-scroll-direction', `${scrollPercentage > 0.5 ? 1 - scrollPercentage : scrollPercentage}`);
scrollbarElement.style.setProperty('--os-scroll-percent', `${scrollPercentage}`);
this._handleScroll(ev);
}
});
const viewport = scrollbar.elements().viewport;
viewport.style.setProperty('overflow', `var(${this._menuOverflowCssVar}, hidden scroll)`, 'important');
setInterval(() => scrollbar.update(), 250);
} call this function in cc: @hallzy |
Global OverlayScrollbarsWorks for both Initial & Dynamically Loaded Content as well as Web ComponentsSome background:I had some JS code to implement the requested feature in Tailwind based projects specifically. Tailwind's inline CSS approach has you define your elements' look and behavior with classes. I adapted the code to not only look for all elements with Tailwind's overlay scroll or auto classes but also the [data-overlayscrollbars-initialize] attribute. However, you can change it to look for what ever classes or attributes you want. Have a look at the code below. The following code works with classes or attributes and is style system agnostic
Note regarding a ComputedStyle implementationI tried to implement a similar approach based on the ComputedStyle of all elements and while it worked well for static content it didn't work for dynamically added content. When I revised for dynamically added content it had severe performance issues and rendered the page and browser useless. But just for reference here is the code that works for static pages
Hope some will find this information useful |
I investigated why the The library uses the |
Is your feature request related to a problem? Please describe.
We are developing something that uses a shadow dom, but it seems like this project doesn't work fully inside of a shadow dom.
If I understand the code correctly, chrome on desktop shouldn't have a 30px
border-right
andright
on 2 of the os content wrappers, but that is what we see inside of the shadow dom, so at the very least it seems like the detection that exists to determine if those styles should be applied doesn't work correctly inside a shadow.This basically means that without adding our own code, we end up with different gaps between the content of the scrollable area and the scrollbar for different browsers.
What we have ended up doing in the meantime is calculating the width of the scrollbar on our own, and adding a margin to our content based on that calculation, which works, but having it work built in would be more convenient.
I don't know if there are other things not working as intended, this is just the one that we've noticed.
Also, to make it more convenient to use, it would be nice to be able to easily import the JS as a module. We have a workaround for that right now as well.
Describe the solution you'd like
Support for use inside of a shadow dom, and a JS file that can be imported as a module.
Describe alternatives you've considered
As mentioned, We currently determine the width of the scrollbar ourselves and add a CSS style to account for it.
For importing the JS, we are adding the script tag for that into an iframe, and a promise gives us back the OverlayScrollbars function (a frame is used because our product is embedded onto customer sites, and we are trying to not pollute their global scope).
The text was updated successfully, but these errors were encountered: