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" ;
3
3
import type * as WPDS from "../theme" ;
4
4
import type { Token } from "@stitches/react/types/theme" ;
5
5
import type { StandardLonghandProperties } from "@stitches/react/types/css" ;
6
6
7
7
const NAME = "Scrim" ;
8
8
9
- const scrimTransition = `opacity ${ theme . transitions . normal } ${ theme . transitions . inOut } ` ;
10
-
11
9
const fadeInAnimation = keyframes ( {
12
10
from : { opacity : 0 } ,
13
11
to : { opacity : 1 } ,
@@ -21,18 +19,16 @@ const fadeOutAnimation = keyframes({
21
19
const StyledContainer = styled ( "div" , {
22
20
backgroundColor : theme . colors . alpha50 ,
23
21
position : "fixed" ,
24
- transition : scrimTransition ,
25
22
inset : 0 ,
26
23
contentVisibility : "auto" ,
27
24
"@reducedMotion" : {
28
- transition : "none" ,
25
+ animation : "none" ,
29
26
} ,
30
27
"&[data-state='open']" : {
31
28
animation : `${ fadeInAnimation } ${ theme . transitions . normal } ${ theme . transitions . inOut } ` ,
32
- opacity : 1 ,
33
29
} ,
34
30
"&[data-state='closed']" : {
35
- fadeOutAnimation : `${ fadeOutAnimation } ${ theme . transitions . normal } ${ theme . transitions . inOut } ` ,
31
+ animation : `${ fadeOutAnimation } ${ theme . transitions . normal } ${ theme . transitions . inOut } ` ,
36
32
opacity : 0 ,
37
33
} ,
38
34
} ) ;
@@ -50,9 +46,8 @@ interface ScrimProps extends React.ComponentPropsWithRef<"div"> {
50
46
| Token < "shell" , string , "zIndices" , "wpds" > ;
51
47
}
52
48
53
- const htmlGlobalCSS = wpCSS ( {
49
+ const htmlGlobalCSS = globalCss ( {
54
50
html : {
55
- contain : "layout style" ,
56
51
"&[data-scrim-state='open']" : {
57
52
maxHeight : "100vh" ,
58
53
overflow : "hidden" ,
@@ -69,37 +64,35 @@ export const Scrim = React.forwardRef<HTMLDivElement, ScrimProps>(
69
64
{ lockScroll = true , open, zIndex = theme . zIndices . shell , css, ...props } ,
70
65
ref
71
66
) => {
72
- const [ isPending , startTransition ] = useTransition ( ) ;
67
+ const [ shouldRender , setShouldRender ] = useState ( false ) ;
73
68
74
69
htmlGlobalCSS ( ) ;
75
70
76
71
React . useEffect ( ( ) => {
77
72
if ( ! lockScroll || typeof window === "undefined" ) return ;
78
73
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 = "" ;
93
85
94
- const handleTransitionEnd = ( ) => {
95
- if ( ! isPending && ! open ) {
96
86
document . documentElement . dataset . scrimState = "closed" ;
87
+ }
88
+ } , [ open , lockScroll ] ) ;
89
+
90
+ const handleAnimationEnd = ( ) => {
91
+ if ( ! open ) {
97
92
setShouldRender ( false ) ;
98
93
}
99
94
} ;
100
95
101
- const [ shouldRender , setShouldRender ] = useState ( false ) ;
102
-
103
96
useEffect ( ( ) => {
104
97
if ( open ) {
105
98
setShouldRender ( true ) ;
@@ -112,20 +105,13 @@ export const Scrim = React.forwardRef<HTMLDivElement, ScrimProps>(
112
105
}
113
106
} , [ open ] ) ;
114
107
115
- const handleAnimationEnd = ( ) => {
116
- if ( ! isPending && ! open ) {
117
- setShouldRender ( false ) ;
118
- }
119
- } ;
120
-
121
108
return shouldRender ? (
122
109
< StyledContainer
123
110
data-state = { open ? "open" : "closed" }
124
111
ref = { ref }
125
112
css = { { ...css , zIndex : zIndex } }
126
113
aria-hidden = { true }
127
114
{ ...props }
128
- onTransitionEnd = { handleTransitionEnd }
129
115
onAnimationEnd = { handleAnimationEnd }
130
116
> </ StyledContainer >
131
117
) : null ;
0 commit comments