Skip to content

Commit

Permalink
fix: correct shadow dom traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Oct 9, 2023
1 parent 0fe6cf9 commit 55d302b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/handleScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ export const handleScroll = (
let availableScrollTop = 0;

do {
if (target instanceof ShadowRoot) {
target = target.host as HTMLElement;
}
const [position, scroll, capacity] = getScrollVariables(axis, target);

const elementScroll = scroll - capacity - directionFactor * position;
Expand All @@ -104,7 +101,11 @@ export const handleScroll = (
}
}

target = target.parentNode as any;
if (target instanceof ShadowRoot) {
target = target.host as HTMLElement;
} else {
target = target.parentNode as HTMLElement;
}
} while (
// portaled content
(!targetInLock && target !== document.body) ||
Expand Down
43 changes: 43 additions & 0 deletions stories/shadow-dom.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { RemoveScroll } from '../src';

function LongList() {
return (
<div style={{ backgroundColor: 'white' }}>
<RemoveScroll>
<div style={{ overflow: 'auto', height: '100vh' }}>
{new Array(100).fill(0).map((_, idx) => (
<div key={idx}>Inside {idx}</div>
))}
</div>
</RemoveScroll>

{new Array(100).fill(0).map((_, idx) => (
<div key={idx}>Outside {idx}</div>
))}
</div>
);
}

export function ScrollInShadowDom() {
const [portal, setPortal] = React.useState<any>(null);
const setRef = React.useCallback((ref) => {
if (ref) {
const shadowRoot = ref.attachShadow({ mode: 'open' });
setPortal(ReactDOM.createPortal(<LongList />, shadowRoot));
}
}, []);

return (
<div style={{ height: 300, overflow: 'scroll', padding: 50, backgroundColor: 'yellow' }}>
<div ref={setRef} />
{portal}
</div>
);
}

export default {
title: 'shadow dom',
};

0 comments on commit 55d302b

Please sign in to comment.