Skip to content

Commit

Permalink
isolate component instances
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Apr 4, 2021
1 parent 5e5f1ac commit f08d5e1
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions docs/src/modules/components/AppNavDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import { pageToTitleI18n } from 'docs/src/modules/utils/helpers';
import PageContext from 'docs/src/modules/components/PageContext';
import { useUserLanguage, useTranslate } from 'docs/src/modules/utils/i18n';

let savedScrollTop = null;
const savedScrollTop = {};

function PersistScroll(props) {
const { children, enabled } = props;
const { slot, children, enabled } = props;
const rootRef = React.useRef();

React.useLayoutEffect(() => {
Expand All @@ -30,7 +30,7 @@ function PersistScroll(props) {
return undefined;
}

parent.scrollTop = savedScrollTop;
parent.scrollTop = savedScrollTop[slot];

const activeBox = activeElement.getBoundingClientRect();

Expand All @@ -39,16 +39,17 @@ function PersistScroll(props) {
}

return () => {
savedScrollTop = parent.scrollTop;
savedScrollTop[slot] = parent.scrollTop;
};
}, [enabled]);
}, [enabled, slot]);

return <div ref={rootRef}>{children}</div>;
}

PersistScroll.propTypes = {
children: PropTypes.node,
enabled: PropTypes.bool,
children: PropTypes.node.isRequired,
enabled: PropTypes.bool.isRequired,
slot: PropTypes.string.isRequired,
};

const styles = (theme) => ({
Expand Down Expand Up @@ -199,7 +200,9 @@ function AppNavDrawer(props) {
keepMounted: true,
}}
>
<PersistScroll enabled={mobileOpen}>{drawer}</PersistScroll>
<PersistScroll slot="swipeable" enabled={mobileOpen}>
{drawer}
</PersistScroll>
</SwipeableDrawer>
) : null}
{disablePermanent || mobile ? null : (
Expand All @@ -211,7 +214,9 @@ function AppNavDrawer(props) {
variant="permanent"
open
>
<PersistScroll enabled>{drawer}</PersistScroll>
<PersistScroll slot="side" enabled>
{drawer}
</PersistScroll>
</Drawer>
</Hidden>
)}
Expand Down

0 comments on commit f08d5e1

Please sign in to comment.