Skip to content

Commit 66afed7

Browse files
authored
Merge pull request #20 from zJaaal/dev
Dev
2 parents 946f42e + 0bf916c commit 66afed7

28 files changed

+371
-86
lines changed

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# React Scroll Utilities (Beta)
22

3-
React Scroll Utilities is a Lightweight library to track scroll events like, proximity to components, direction of scroll and render a component if it's on screen sight. I'll be adding more features and improving the functionality. I'm also open to read any request or change that you think that would be a good addition. This library only supports vertical scroll at the moment.
3+
React Scroll Utilities is a Lightweight library to track scroll events like, proximity to components, direction of scroll and render a component if it's on screen sight. I'll be adding more features and improving the functionality. I'm also open to read any request or change that you think that would be a good addition.
44

55
## ScrollWatcher Component
66

@@ -26,7 +26,7 @@ You only need to render this component on the top level of your app, it provides
2626

2727
## useProximity hook
2828

29-
This hook lets you know how far is the screen from the component, it returns negative and positive values, where 0 is the top limit of the screen (when you scroll down your component ( out of sight ), proximity will be on negatives values) and 2 is the bottom limit of the screen (when you scroll up your component ( out of sight ), proximity will be more than 2), so your component is on screen if the proximity value is between 0 and 2, were 1 is approximately the center of the screen.
29+
This hook lets you know how far is the screen from the component, it returns an object with two properties: x and y. Which values represents the proximity the component has to the center of the current viewport as a float value. For Y the acceptable value where the component is on sigth is between 0 and 2. For X the acceptable value where the component is on sigth is between 0 and 3.
3030

3131
### Usage
3232

@@ -37,26 +37,38 @@ This hook only take one argument that should be a ref to an HTML Element. This h
3737
```js
3838
//TypeScript
3939
function Example() {
40-
const ref = useRef < HTMLDivElement > null;
40+
const ref = useRef <HTMLDivElement>(null);
4141

42-
const proximity = useProximity(ref);
42+
//I'm destructuring the object but you can easily use it without destructuring
43+
const { x, y } = useProximity(ref);
4344

44-
return <div ref={ref}>{"proximity: " + proximity}</div>;
45+
return (
46+
<div ref={ref}>
47+
<div>{"X proximity: " + x}</div>
48+
<div>{"Y proximity: " + y}</div>
49+
</div>
50+
);
4551
}
4652

4753
//JavaScript
4854
function Example() {
4955
const ref = useRef(null);
5056

51-
const proximity = useProximity(ref);
57+
//I'm destructuring the object but you can easily use it without destructuring
58+
const { x, y } = useProximity(ref);
5259

53-
return <div ref={ref}>{"proximity: " + proximity}</div>;
60+
return (
61+
<div ref={ref}>
62+
<div>{"X proximity: " + x}</div>
63+
<div>{"Y proximity: " + y}</div>
64+
</div>
65+
);
5466
}
5567
```
5668

5769
## Render Component
5870

59-
This component implements the useProximity hook, and will only render your component if proximity is on an acceptable value (between 0 and 2). So you can use it to add animations to your components on render, like an entry transition.
71+
This component implements the useProximity hook, and will only render your component if proximity is on an acceptable value (between 0 and 2 for Y and between 0 and 3 for X). So you can use it to add animations to your components on render, like an entry transition.
6072

6173
### Usage
6274

@@ -82,7 +94,7 @@ This component needs a React Children to work, it also accepts custom styles, th
8294

8395
## useDirection hook
8496

85-
This hooks returns the current direction of the scroll. It returns an string that can be "UP" or "DOWN", its default value is "DOWN".
97+
This hooks returns the current direction of the scroll. It returns an string that can be "UP", "DOWN", "RIGHT" or "LEFT", its default value is "DOWN".
8698

8799
### Usage
88100

@@ -104,6 +116,14 @@ function Example() {
104116
//Do something else
105117
break;
106118
}
119+
case "RIGHT": {
120+
// Do something
121+
break;
122+
}
123+
case "LEFT": {
124+
//Do something else
125+
break;
126+
}
107127
}
108128
}, [direction]);
109129

@@ -119,4 +139,4 @@ function Example() {
119139

120140
## Directions enum
121141

122-
This lib provides an enum for TypeScript users, it just has two properties at the moment: Directions.up and Directions.down, that returns the value of "UP" and "DOWN", you can use it as helper for useDirection hook.
142+
This lib provides an enum for TypeScript users, it just has two properties at the moment: Directions.up, Directions.down, Directions.right and Directions.left that returns the value of "UP", "DOWN" "RIGHT" and "LEFT", you can use it as helper for useDirection hook.

index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
5-
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
65
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
76
<title>Vite + React + TS</title>
87
</head>

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "react-scroll-utilities",
33
"private": false,
4-
"version": "0.2.8",
4+
"version": "0.3.0",
5+
"license": "MIT",
56
"type": "module",
67
"main": "src/lib/index.ts",
78
"homepage": "https://github.com/zJaaal/react-scroll-utilities#readme",

public/vite.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { useLayoutEffect, useRef, useState } from "react";
1+
import { useRef } from "react";
22
import useProximity from "./lib/hooks/useProximity";
33

44
function App() {
55
const ref = useRef<HTMLDivElement>(null);
66

77
const proximity = useProximity(ref);
8-
98
return (
109
<div className="test" ref={ref}>
11-
{"proximity: " + proximity}
10+
{"proximity Y: " + proximity.y}
11+
{"proximity X: " + proximity.x}
1212
</div>
1313
);
1414
}

src/__test__/Render.test.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("Render Component", () => {
1111
HTMLElement.prototype.getBoundingClientRect = getBoundingClientRectMock;
1212
});
1313

14-
it("should render on a proximity between 0 and 2", () => {
14+
it("should render on a proximity between 0 and 2 for Y", () => {
1515
const RenderTestComponent = render(
1616
<ScrollWatcher>
1717
<Render>Hi i'm being rendered</Render>
@@ -20,12 +20,17 @@ describe("Render Component", () => {
2020
fireEvent.scroll(window, {
2121
target: { scrollY: window.innerHeight / 2 },
2222
});
23+
24+
fireEvent.scroll(window, {
25+
target: { scrollX: window.innerWidth / 2 },
26+
});
27+
2328
expect(
2429
RenderTestComponent.getByText("Hi i'm being rendered"),
25-
"Component should render when the proximity is between 0 and 2"
30+
"Component should render when the proximity is between 0 and 2 for Y and between 0 and 3 for X"
2631
).toBeTruthy();
2732
}),
28-
it("should not render out of a proximity between 0 and 2", () => {
33+
it("should not render out of a proximity between 0 and 2 for Y and out of between 0 and 3 for X", () => {
2934
const RenderTestComponent = render(
3035
<ScrollWatcher>
3136
<Render>Hi i'm not being rendered</Render>
@@ -36,9 +41,13 @@ describe("Render Component", () => {
3641
target: { scrollY: window.innerHeight * 2 },
3742
});
3843

44+
fireEvent.scroll(window, {
45+
target: { scrollX: window.innerWidth * 2 },
46+
});
47+
3948
expect(
4049
RenderTestComponent.queryByText("Hi i'm not being rendered"),
41-
"Component should be out of bounds of proximity acceptance"
50+
"Component should be out of bounds of proximity acceptance for Y and and out of between 0 and 3 for X"
4251
).toBeFalsy();
4352
});
4453
});

src/__test__/components/UseProximity.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useRef } from "react";
2+
import useProximity from "../../lib/hooks/useProximity";
3+
4+
const UseProximityX = () => {
5+
const ref = useRef<HTMLDivElement>(null);
6+
7+
const { x } = useProximity(ref);
8+
9+
return (
10+
<div ref={ref} role="log">
11+
{x}
12+
</div>
13+
);
14+
};
15+
16+
export default UseProximityX;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useRef } from "react";
2+
import useProximity from "../../lib/hooks/useProximity";
3+
4+
const UseProximityY = () => {
5+
const ref = useRef<HTMLDivElement>(null);
6+
7+
const { y } = useProximity(ref);
8+
9+
return (
10+
<div ref={ref} role="log">
11+
{y}
12+
</div>
13+
);
14+
};
15+
16+
export default UseProximityY;

0 commit comments

Comments
 (0)