|
5 | 5 |
|
6 | 6 | React hook to use with [lax.js](https://github.com/alexfoxy/lax.js).
|
7 | 7 |
|
8 |
| -### Usage |
| 8 | +## Usage |
9 | 9 |
|
10 | 10 | ```javascript
|
11 | 11 | import React from 'react';
|
12 | 12 | import { useLax, useLaxElement } from 'use-lax';
|
13 | 13 |
|
14 | 14 | function App() {
|
15 |
| - const [showBubble, setBubble] = React.useState(false); |
16 |
| - const toggleBubble = () => { |
17 |
| - setBubble(!showBubble); |
18 |
| - }; |
19 |
| - |
20 |
| - // use once in the top level element |
21 |
| - // you can configure breakpoints and className |
22 |
| - // useLax({ className: 'nice' }); |
23 |
| - useLax(); |
| 15 | + const [showBubble, setBubble] = useState(false); |
| 16 | + const toggleBubble = () => setBubble(!showBubble); |
| 17 | + |
| 18 | + // Use once in the top level element |
| 19 | + // to configure drivers and initial elements |
| 20 | + // https://github.com/alexfoxy/lax.js#setup |
| 21 | + useLax({ |
| 22 | + drivers: [ |
| 23 | + { |
| 24 | + name: 'scrollY', |
| 25 | + getValueFn: () => window.scrollY, |
| 26 | + }, |
| 27 | + ], |
| 28 | + }); |
24 | 29 |
|
25 | 30 | return (
|
26 | 31 | <div>
|
27 | 32 | <button className="toggle-bubble" onClick={toggleBubble}>
|
28 | 33 | Toggle Bubble
|
29 | 34 | </button>
|
30 |
| - <p>{showBubble ? '..now scroll down..' : '^ press the button ^'}</p> |
| 35 | + <p>{showBubble ? '...now scroll down...' : '^ press the button ^'}</p> |
31 | 36 | {showBubble ? <Bubble /> : null}
|
32 | 37 | </div>
|
33 | 38 | );
|
34 | 39 | }
|
35 | 40 |
|
36 | 41 | function Bubble() {
|
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(); |
40 |
| - |
41 |
| - // `lax` (or `nice` in our example) will be added to the classList of the element |
42 |
| - return ( |
43 |
| - <div ref={ref} className="bubble" data-lax-preset="leftToRight fadeInOut" /> |
44 |
| - ); |
| 42 | + // Use it on every component added dynamically |
| 43 | + // and provide the animation driven from the drivers |
| 44 | + const ref = useLaxElement({ |
| 45 | + animationData: { |
| 46 | + scrollY: { |
| 47 | + presets: ['fadeInOut:200:0'], |
| 48 | + translateX: [ |
| 49 | + [0, 'screenHeight'], |
| 50 | + [0, 'screenWidth'], |
| 51 | + ], |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + return <div ref={ref} className="bubble" />; |
45 | 57 | }
|
46 | 58 | ```
|
47 | 59 |
|
48 |
| -See the full example [here](https://codesandbox.io/s/q9882qjxzq). |
49 |
| - |
50 |
| -See the [lax demo](https://alexfox.dev/lax.js/) built with `React` and `use-lax` [here](https://codesandbox.io/s/039krok5ml). |
51 |
| - |
52 |
| -See the [Mario demo](https://alexfox.dev/lax.js/sprite.html) built with `React` and `use-lax` [here](https://codesandbox.io/s/r48kz0okrm). |
| 60 | +- [Full example above](https://codesandbox.io/s/q9882qjxzq) |
| 61 | +- [Lax homepage example](https://codesandbox.io/s/039krok5ml) - [HTML version](https://alexfox.dev/lax.js/) |
| 62 | +- [Mario example](https://codesandbox.io/s/r48kz0okrm) - [HTML version](https://codesandbox.io/s/vcv4k) |
53 | 63 |
|
54 | 64 | ## Contributors
|
55 | 65 |
|
|
0 commit comments