This repository was archived by the owner on Apr 16, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +26
-14
lines changed Expand file tree Collapse file tree 2 files changed +26
-14
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,10 @@ function App() {
17
17
setBubble (! showBubble);
18
18
};
19
19
20
- useLax (); // use once in the top level element
20
+ // use once in the top level element
21
+ // you can configure breakpoints and className
22
+ // useLax({ className: 'nice' });
23
+ useLax ();
21
24
22
25
return (
23
26
< div>
@@ -31,16 +34,13 @@ function App() {
31
34
}
32
35
33
36
function Bubble () {
34
- const ref = useLaxElement (); // use it in every component added dynamically
37
+ // use it in every component added dynamically
38
+ // it will add the className passed to `useLax`, which defaults to `lax`
39
+ const ref = useLaxElement ();
35
40
36
- // add `lax` in the className attribute and the data-lax-preset attribute
37
- // on every component that you want to animate
41
+ // `lax` (or `nice` in our example) will be added to the classList of the element
38
42
return (
39
- < div
40
- ref= {ref}
41
- className= " lax bubble"
42
- data- lax- preset= " leftToRight fadeInOut"
43
- / >
43
+ < div ref= {ref} className= " bubble" data- lax- preset= " leftToRight fadeInOut" / >
44
44
);
45
45
}
46
46
```
Original file line number Diff line number Diff line change 1
1
import lax from 'lax.js' ;
2
2
import * as React from 'react' ;
3
3
4
- function useLax ( ) {
4
+ interface LaxSetupOptions {
5
+ breakpoints ?: { [ k : string ] : any } ;
6
+ className ?: string ;
7
+ }
8
+
9
+ let selector = 'lax' ;
10
+
11
+ function useLax ( { breakpoints, className } : LaxSetupOptions = { } ) {
5
12
const requestRef = React . useRef < number > ( ) ;
13
+ selector = className || selector ;
6
14
7
15
React . useEffect ( ( ) => {
8
- lax . setup ( ) ;
16
+ lax . setup ( { breakpoints , selector : `. ${ selector } ` } ) ;
9
17
10
18
const updateLax = ( ) => {
11
19
lax . update ( window . scrollY ) ;
@@ -19,15 +27,19 @@ function useLax() {
19
27
window . cancelAnimationFrame ( requestRef . current ) ;
20
28
}
21
29
} ;
22
- } , [ ] ) ;
30
+ } , [ breakpoints , className ] ) ;
23
31
}
24
32
25
- function useLaxElement < T > ( ) {
26
- const ref = React . useRef < T > ( ) ;
33
+ function useLaxElement ( ) {
34
+ const ref = React . useRef < Element > ( ) ;
27
35
28
36
React . useEffect ( ( ) => {
29
37
const currentNode = ref . current ;
30
38
39
+ if ( currentNode && currentNode . classList ) {
40
+ currentNode . classList . add ( selector ) ;
41
+ }
42
+
31
43
lax . addElement ( currentNode ) ;
32
44
33
45
return ( ) => {
You can’t perform that action at this time.
0 commit comments