Skip to content

Commit ca2e1af

Browse files
authored
Merge pull request #3 from react18-tools/fix-blocking-overlay
Fix blocking overlay
2 parents fd490ae + 508d2da commit ca2e1af

File tree

6 files changed

+29
-46
lines changed

6 files changed

+29
-46
lines changed

.changeset/brown-birds-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-mouse-trails": patch
3+
---
4+
5+
No need to import styles. Fix blocking overlay

README.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,6 @@ $ npm install react-mouse-trails
3636
$ yarn add react-mouse-trails
3737
```
3838

39-
### Adding CSS
40-
41-
You can import CSS in your global styles or from your component. In the case of Next.js, you can import global styles only in `layout` or `_app` components.
42-
43-
In your stylesheet
44-
45-
```css
46-
@import "react-mouse-trails/dist";
47-
```
48-
49-
or in your component
50-
51-
```ts
52-
import "react-mouse-trail/dist/index.css";
53-
```
54-
55-
Here's a revised version of the usage section:
56-
5739
## Usage
5840

5941
MouseTrail is simple to integrate:

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"dist/**"
1717
],
1818
"scripts": {
19-
"build": "tsup && tsc -p tsconfig-build.json",
19+
"build": "tsup && tsc -p tsconfig-build.json && touch dist/index.css",
2020
"clean": "rm -rf dist",
2121
"dev": "tsup --watch && tsc -p tsconfig-build.json -w",
2222
"typecheck": "tsc --noEmit",

lib/src/client/mouse-trail/mouse-trail.module.scss

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { HTMLProps, useEffect, useRef } from "react";
2-
import styles from "./mouse-trail.module.scss";
1+
/* eslint-disable react/function-component-definition -- for better minification*/
2+
3+
import { type HTMLProps, useEffect, useRef } from "react";
34
import { trails } from "../../utils";
45

56
export interface MouseTrailProps extends HTMLProps<HTMLCanvasElement> {
@@ -13,25 +14,34 @@ export interface MouseTrailProps extends HTMLProps<HTMLCanvasElement> {
1314
* <MouseTrail />
1415
* ```
1516
*/
16-
export const MouseTrail = ({ className, rgb, ...props }: MouseTrailProps) => {
17+
export const MouseTrail = ({ rgb, ...props }: MouseTrailProps): JSX.Element => {
1718
const canvasRef = useRef<HTMLCanvasElement>(null);
1819
useEffect(() => {
1920
const canvas = canvasRef.current;
2021
const gl = canvas?.getContext("webgl");
2122
if (!gl || !canvas) return;
22-
const onResize = () => {
23+
const onResize = (): void => {
2324
canvas.width = innerWidth;
2425
canvas.height = innerHeight;
26+
// canvas.style.width = `${innerWidth}px`;
27+
// canvas.style.height = `${innerHeight}px`;
2528
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
2629
};
2730
onResize();
28-
trails(canvas, gl, rgb);
31+
trails(gl, rgb);
2932
addEventListener("resize", onResize);
3033
// skipcq: JS-0045
3134
return () => {
3235
removeEventListener("resize", onResize);
3336
};
34-
}, []);
35-
const cls = [styles.trail, className ?? ""].join(" ");
36-
return <canvas {...props} className={cls} data-testid="mouse-trail" ref={canvasRef} />;
37+
}, [rgb]);
38+
39+
return (
40+
<canvas
41+
style={{ pointerEvents: "none", position: "fixed", top: 0, left: 0 }}
42+
{...props}
43+
data-testid="mouse-trail"
44+
ref={canvasRef}
45+
/>
46+
);
3747
};

lib/src/utils.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ const fragmentShaderSource = (rgb = [1, 0, 0]) => `
1818
`;
1919

2020
/** Setup trails */
21-
export const trails = (
22-
canvas: HTMLCanvasElement,
23-
gl: WebGLRenderingContext,
24-
rgb?: [number, number, number],
25-
) => {
21+
export const trails = (gl: WebGLRenderingContext, rgb?: [number, number, number]) => {
2622
gl.enable(gl.BLEND);
2723
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
2824

@@ -85,7 +81,7 @@ export const trails = (
8581
let fades: number[] = [];
8682

8783
/** The render loop */
88-
const render = () => {
84+
const render = (): void => {
8985
runningAnim = true;
9086
fades = fades.map(fade => fade / 1.1);
9187
positions = positions.filter((_, index) => fades[index] > 0.001);
@@ -106,9 +102,9 @@ export const trails = (
106102
};
107103
gl.clearColor(0.0, 0.0, 0.0, 0.0);
108104

109-
canvas.addEventListener("mousemove", (event: MouseEvent) => {
110-
const x = (event.clientX / canvas.width) * 2 - 1;
111-
const y = (event.clientY / canvas.height) * -2 + 1;
105+
addEventListener("mousemove", (event: MouseEvent) => {
106+
const x = (event.clientX / innerWidth) * 2 - 1;
107+
const y = (event.clientY / innerHeight) * -2 + 1;
112108

113109
positions.unshift(x, y);
114110
fades.unshift(1);

0 commit comments

Comments
 (0)