Skip to content
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

fix blocking all scrolling when RemoveScroll is used within ShadowRoot #98

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/SideEffect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let idCounter = 0;
let lockStack: any[] = [];

export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {
const shouldPreventQueue = React.useRef<Array<{ name: string; delta: number[]; target: any; should: boolean }>>([]);
const shouldPreventQueue = React.useRef<Array<{ name: string; delta: number[]; target: any; should: boolean; shadowParent?: HTMLElement | null }>>([]);
const touchStartRef = React.useRef([0, 0]);
const activeAxis = React.useRef<Axis | undefined>();
const [id] = React.useState(idCounter++);
Expand Down Expand Up @@ -114,7 +114,7 @@ export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {

const delta = 'deltaY' in event ? getDeltaXY(event) : getTouchXY(event);
const sourceEvent = shouldPreventQueue.current.filter(
(e) => e.name === event.type && e.target === event.target && deltaCompare(e.delta, delta)
(e) => e.name === event.type && (e.target === event.target || event.target === e.shadowParent) && deltaCompare(e.delta, delta)
)[0];

// self event, and should be canceled
Expand Down Expand Up @@ -145,7 +145,7 @@ export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {
}, []);

const shouldCancel = React.useCallback((name: string, delta: number[], target: any, should: boolean) => {
const event = { name, delta, target, should };
const event = { name, delta, target, should, shadowParent: getOutermostShadowParent(target) };
shouldPreventQueue.current.push(event);

setTimeout(() => {
Expand Down Expand Up @@ -197,3 +197,15 @@ export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {
</React.Fragment>
);
}

function getOutermostShadowParent(node: Node | null): HTMLElement | null {
let shadowParent: HTMLElement | null = null;
while (node !== null) {
if (node instanceof ShadowRoot) {
shadowParent = node.host as HTMLElement;
node = node.host;
}
node = node.parentNode;
}
return shadowParent
}
3 changes: 3 additions & 0 deletions src/handleScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export const handleScroll = (
let availableScrollTop = 0;

do {
if (target instanceof ShadowRoot) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"when" this happens?
Description of commit is about "mobile", but how it's related?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the event comes from within a ShadowRoot, then the target's .parentNode() up the tree will eventually be the ShadowRoot, and we need to step out of it with .host (because a ShadowRoot's parentNode is null). I'm not exactly sure why this fixes it, but touch scrolling doesn't work without this in my use case. I already wanted to do this regardless, because it seems like the "right" thing to do with regards to ShadowDOM, but it also ended up fixing the issue on mobile.

target = target.host as HTMLElement;
}
const [position, scroll, capacity] = getScrollVariables(axis, target);

const elementScroll = scroll - capacity - directionFactor * position;
Expand Down