@@ -7,41 +7,53 @@ import { escapeCssSpecifier } from "./shared/css-utils";
7
7
8
8
const componentAttributeId = `data-ws-animate-id` ;
9
9
10
+ const createSelector = ( id : string ) => `[${ componentAttributeId } ="${ id } "] > *` ;
11
+
10
12
const Animate = ( { id } : { id : string } ) => {
11
13
useLayoutEffect ( ( ) => {
12
- const elt = document . querySelector ( `[${ componentAttributeId } ="${ id } "]` ) ;
13
- if ( elt === null ) {
14
+ const selector = createSelector ( id ) ;
15
+ const elements = document . querySelectorAll ( selector ) ;
16
+ if ( elements . length === 0 ) {
14
17
return ;
15
18
}
19
+ const disposables = Array . from ( elements )
20
+ . map ( ( elt ) => {
21
+ if ( elt instanceof HTMLElement ) {
22
+ const animation = elt . animate (
23
+ [ { offset : 0 , transform : "translate(0, 100px)" } ] ,
24
+ {
25
+ fill : "backwards" ,
26
+ duration : 400 ,
27
+ easing : "linear" ,
28
+ }
29
+ ) ;
16
30
17
- if ( elt instanceof HTMLElement ) {
18
- const animation = elt . animate (
19
- [ { offset : 0 , transform : "translate(0, 100px)" } ] ,
20
- {
21
- fill : "backwards" ,
22
- duration : 400 ,
23
- easing : "linear" ,
31
+ return ( ) => {
32
+ animation . cancel ( ) ;
33
+ } ;
24
34
}
25
- ) ;
35
+ } )
36
+ . filter ( ( disposable ) => disposable !== undefined ) ;
26
37
27
- return ( ) => {
28
- animation . cancel ( ) ;
29
- } ;
30
- }
38
+ return ( ) => {
39
+ disposables . forEach ( ( dispose ) => dispose ( ) ) ;
40
+ } ;
31
41
} , [ id ] ) ;
32
42
33
43
return undefined ;
34
44
} ;
35
45
36
46
const StylesBeforeAnimate = ( { id } : { id : string } ) => {
47
+ const selector = createSelector ( id ) ;
48
+
37
49
const styleContent = `
38
50
@keyframes ws-scroll-animation-${ id } {
39
51
from {
40
52
transform: translate(0, 100px);
41
53
}
42
54
}
43
55
44
- [ ${ componentAttributeId } =" ${ id } "] {
56
+ ${ selector } {
45
57
animation-name: ws-scroll-animation-${ id } ;
46
58
animation-fill-mode: backwards;
47
59
animation-play-state: paused;
@@ -52,9 +64,9 @@ const StylesBeforeAnimate = ({ id }: { id: string }) => {
52
64
return < style dangerouslySetInnerHTML = { { __html : styleContent } } /> ;
53
65
} ;
54
66
55
- type ScrollProps = { debug ?: boolean } ;
67
+ type ScrollProps = { debug ?: boolean ; children ?: React . ReactNode } ;
56
68
57
- export const Scroll = ( { debug = false } : ScrollProps ) => {
69
+ export const Scroll = ( { debug = false , children } : ScrollProps ) => {
58
70
const nakedId = useId ( ) ;
59
71
const id = escapeCssSpecifier ( nakedId ) ;
60
72
@@ -74,21 +86,9 @@ export const Scroll = ({ debug = false }: ScrollProps) => {
74
86
< Animate id = { id } />
75
87
</ Suspense >
76
88
</ ClientOnly >
77
- < div
78
- { ...{ [ componentAttributeId ] : id } }
79
- style = { { height : "200px" , width : "200px" , backgroundColor : "red" } }
80
- > </ div >
81
- < div
82
- data-ws-id = { id }
83
- style = { {
84
- height : "100px" ,
85
- width : "100px" ,
86
- backgroundColor : "yellow" ,
87
- position : "fixed" ,
88
- left : 0 ,
89
- top : 0 ,
90
- } }
91
- > </ div >
89
+ < div { ...{ [ componentAttributeId ] : id } } style = { { display : "contents" } } >
90
+ { children }
91
+ </ div >
92
92
</ >
93
93
) ;
94
94
} ;
0 commit comments