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

Commit ef2b174

Browse files
authored
feat: support breakpoints and className options (#95)
* feat: support breakpoints and className options * docs: update README about options
1 parent 7f2953f commit ef2b174

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ function App() {
1717
setBubble(!showBubble);
1818
};
1919

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();
2124

2225
return (
2326
<div>
@@ -31,16 +34,13 @@ function App() {
3134
}
3235

3336
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();
3540

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
3842
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" />
4444
);
4545
}
4646
```

src/index.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import lax from 'lax.js';
22
import * as React from 'react';
33

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 = {}) {
512
const requestRef = React.useRef<number>();
13+
selector = className || selector;
614

715
React.useEffect(() => {
8-
lax.setup();
16+
lax.setup({ breakpoints, selector: `.${selector}` });
917

1018
const updateLax = () => {
1119
lax.update(window.scrollY);
@@ -19,15 +27,19 @@ function useLax() {
1927
window.cancelAnimationFrame(requestRef.current);
2028
}
2129
};
22-
}, []);
30+
}, [breakpoints, className]);
2331
}
2432

25-
function useLaxElement<T>() {
26-
const ref = React.useRef<T>();
33+
function useLaxElement() {
34+
const ref = React.useRef<Element>();
2735

2836
React.useEffect(() => {
2937
const currentNode = ref.current;
3038

39+
if (currentNode && currentNode.classList) {
40+
currentNode.classList.add(selector);
41+
}
42+
3143
lax.addElement(currentNode);
3244

3345
return () => {

0 commit comments

Comments
 (0)