Skip to content

Commit a1d41fa

Browse files
authored
fix: make lockScroll property work (#670)
1 parent 58a6e1d commit a1d41fa

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

build.washingtonpost.com/docs/foundations/color.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ description: Color token values are defined by their context. There are two cont
2020
---
2121

2222
<BR />
23-
## General Concepts
23+
## General Concepts
2424

2525
Color themes and device color modes aren't synonymous; themes are tailored to apps, while modes are system-wide settings that impact device theming.
2626

packages/kit/src/scrim/Scrim.tsx

+22-36
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import React, { useEffect, useState, useTransition } from "react";
2-
import { styled, theme, css as wpCSS, keyframes } from "../theme";
1+
import React, { useEffect, useState } from "react";
2+
import { styled, theme, globalCss, keyframes } from "../theme";
33
import type * as WPDS from "../theme";
44
import type { Token } from "@stitches/react/types/theme";
55
import type { StandardLonghandProperties } from "@stitches/react/types/css";
66

77
const NAME = "Scrim";
88

9-
const scrimTransition = `opacity ${theme.transitions.normal} ${theme.transitions.inOut}`;
10-
119
const fadeInAnimation = keyframes({
1210
from: { opacity: 0 },
1311
to: { opacity: 1 },
@@ -21,18 +19,16 @@ const fadeOutAnimation = keyframes({
2119
const StyledContainer = styled("div", {
2220
backgroundColor: theme.colors.alpha50,
2321
position: "fixed",
24-
transition: scrimTransition,
2522
inset: 0,
2623
contentVisibility: "auto",
2724
"@reducedMotion": {
28-
transition: "none",
25+
animation: "none",
2926
},
3027
"&[data-state='open']": {
3128
animation: `${fadeInAnimation} ${theme.transitions.normal} ${theme.transitions.inOut}`,
32-
opacity: 1,
3329
},
3430
"&[data-state='closed']": {
35-
fadeOutAnimation: `${fadeOutAnimation} ${theme.transitions.normal} ${theme.transitions.inOut}`,
31+
animation: `${fadeOutAnimation} ${theme.transitions.normal} ${theme.transitions.inOut}`,
3632
opacity: 0,
3733
},
3834
});
@@ -50,9 +46,8 @@ interface ScrimProps extends React.ComponentPropsWithRef<"div"> {
5046
| Token<"shell", string, "zIndices", "wpds">;
5147
}
5248

53-
const htmlGlobalCSS = wpCSS({
49+
const htmlGlobalCSS = globalCss({
5450
html: {
55-
contain: "layout style",
5651
"&[data-scrim-state='open']": {
5752
maxHeight: "100vh",
5853
overflow: "hidden",
@@ -69,37 +64,35 @@ export const Scrim = React.forwardRef<HTMLDivElement, ScrimProps>(
6964
{ lockScroll = true, open, zIndex = theme.zIndices.shell, css, ...props },
7065
ref
7166
) => {
72-
const [isPending, startTransition] = useTransition();
67+
const [shouldRender, setShouldRender] = useState(false);
7368

7469
htmlGlobalCSS();
7570

7671
React.useEffect(() => {
7772
if (!lockScroll || typeof window === "undefined") return;
7873

79-
startTransition(() => {
80-
if (open) {
81-
document.body.style.marginRight = `${
82-
window.innerWidth - document.body.clientWidth
83-
}px`;
84-
document.documentElement.dataset.scrimState = "open";
85-
}
86-
87-
if (!open) {
88-
document.documentElement.dataset.scrimState = "closed";
89-
document.body.style.marginRight = "";
90-
}
91-
});
92-
}, [open, lockScroll]);
74+
if (open) {
75+
// this calculation needs to take place before the html is locked
76+
document.body.style.marginRight = `${
77+
window.innerWidth - document.body.clientWidth
78+
}px`;
79+
80+
document.documentElement.dataset.scrimState = "open";
81+
}
82+
83+
if (!open) {
84+
document.body.style.marginRight = "";
9385

94-
const handleTransitionEnd = () => {
95-
if (!isPending && !open) {
9686
document.documentElement.dataset.scrimState = "closed";
87+
}
88+
}, [open, lockScroll]);
89+
90+
const handleAnimationEnd = () => {
91+
if (!open) {
9792
setShouldRender(false);
9893
}
9994
};
10095

101-
const [shouldRender, setShouldRender] = useState(false);
102-
10396
useEffect(() => {
10497
if (open) {
10598
setShouldRender(true);
@@ -112,20 +105,13 @@ export const Scrim = React.forwardRef<HTMLDivElement, ScrimProps>(
112105
}
113106
}, [open]);
114107

115-
const handleAnimationEnd = () => {
116-
if (!isPending && !open) {
117-
setShouldRender(false);
118-
}
119-
};
120-
121108
return shouldRender ? (
122109
<StyledContainer
123110
data-state={open ? "open" : "closed"}
124111
ref={ref}
125112
css={{ ...css, zIndex: zIndex }}
126113
aria-hidden={true}
127114
{...props}
128-
onTransitionEnd={handleTransitionEnd}
129115
onAnimationEnd={handleAnimationEnd}
130116
></StyledContainer>
131117
) : null;

0 commit comments

Comments
 (0)