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

Batch fixed position style updates #337

Merged
merged 1 commit into from
May 7, 2024
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
4 changes: 2 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export function isInView(el: HTMLElement): boolean {
);
}

export function set(el?: Element | HTMLElement | null, styles?: Style, ignoreCache = false) {
if (!el || !(el instanceof HTMLElement) || !styles) return;
export function set(el: Element | HTMLElement | null | undefined, styles: Style, ignoreCache = false) {
if (!el || !(el instanceof HTMLElement)) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

styles is always defined in the codebase so this check is redundant

let originalStyles: Style = {};

Object.entries(styles).forEach(([key, value]: [string, string]) => {
Expand Down
27 changes: 13 additions & 14 deletions src/use-position-fixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function usePositionFixed({
hasBeenOpened: boolean;
preventScrollRestoration: boolean;
}) {
const [activeUrl, setActiveUrl] = React.useState(typeof window !== 'undefined' ? window.location.href : '');
const [activeUrl, setActiveUrl] = React.useState(() => (typeof window !== 'undefined' ? window.location.href : ''));
const scrollPos = React.useRef(0);

const setPositionFixed = React.useCallback(() => {
Expand All @@ -26,20 +26,23 @@ export function usePositionFixed({
top: document.body.style.top,
left: document.body.style.left,
height: document.body.style.height,
right: 'unset',
};

// Update the dom inside an animation frame
const { scrollX, innerHeight } = window;

document.body.style.setProperty('position', 'fixed', 'important');
document.body.style.top = `${-scrollPos.current}px`;
document.body.style.left = `${-scrollX}px`;
document.body.style.right = '0px';
document.body.style.height = 'auto';
Object.assign(document.body.style, {
top: `${-scrollPos.current}px`,
left: `${-scrollX}px`,
right: '0px',
height: 'auto',
});

setTimeout(
window.setTimeout(
() =>
requestAnimationFrame(() => {
window.requestAnimationFrame(() => {
// Attempt to check if the bottom bar appeared due to the position change
const bottomBarHeight = innerHeight - window.innerHeight;
if (bottomBarHeight && scrollPos.current >= innerHeight) {
Expand All @@ -59,13 +62,9 @@ export function usePositionFixed({
const x = -parseInt(document.body.style.left, 10);

// Restore styles
document.body.style.position = previousBodyPosition.position;
document.body.style.top = previousBodyPosition.top;
document.body.style.left = previousBodyPosition.left;
document.body.style.height = previousBodyPosition.height;
document.body.style.right = 'unset';
Object.assign(document.body.style, previousBodyPosition);

requestAnimationFrame(() => {
window.requestAnimationFrame(() => {
if (preventScrollRestoration && activeUrl !== window.location.href) {
setActiveUrl(window.location.href);
return;
Expand Down Expand Up @@ -101,7 +100,7 @@ export function usePositionFixed({
!isStandalone && setPositionFixed();

if (!modal) {
setTimeout(() => {
window.setTimeout(() => {
restorePositionSetting();
}, 500);
}
Expand Down
Loading