Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit 7e1758c

Browse files
authored
fix: support preset usage (#638)
* fix: support preset usage * docs: update README
1 parent 1945b39 commit 7e1758c

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

README.md

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,61 @@
55

66
React hook to use with [lax.js](https://github.com/alexfoxy/lax.js).
77

8-
### Usage
8+
## Usage
99

1010
```javascript
1111
import React from 'react';
1212
import { useLax, useLaxElement } from 'use-lax';
1313

1414
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+
});
2429

2530
return (
2631
<div>
2732
<button className="toggle-bubble" onClick={toggleBubble}>
2833
Toggle Bubble
2934
</button>
30-
<p>{showBubble ? '..now scroll down..' : '^ press the button ^'}</p>
35+
<p>{showBubble ? '...now scroll down...' : '^ press the button ^'}</p>
3136
{showBubble ? <Bubble /> : null}
3237
</div>
3338
);
3439
}
3540

3641
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" />;
4557
}
4658
```
4759

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)
5363

5464
## Contributors
5565

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,19 @@ export interface LaxInitOptions {
128128
elements?: LaxElement[];
129129
}
130130

131+
declare global {
132+
interface Window {
133+
lax: typeof lax;
134+
}
135+
}
136+
131137
function useLax({ drivers, elements }: LaxInitOptions = {}) {
138+
React.useEffect(() => {
139+
if (!window.lax) {
140+
window.lax = lax;
141+
}
142+
}, []);
143+
132144
React.useEffect(() => {
133145
lax.init();
134146

0 commit comments

Comments
 (0)