Skip to content

Commit

Permalink
fix: gradient overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
rxyhn committed Sep 8, 2024
1 parent 301b37e commit 193ccd4
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions src/components/Boxes.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import React, { useState, useEffect } from "react";
"use client";

import React, { useState, useEffect, useRef } from "react";

interface GridProps {
className?: string;
}

const Boxes: React.FC<GridProps> = ({ className = "" }) => {
export default function Boxes({ className = "" }: GridProps) {
const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
const containerRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const updateDimensions = () => {
setDimensions({
width: window.innerWidth,
height: window.innerHeight,
});
if (containerRef.current) {
const { scrollWidth, scrollHeight } = document.documentElement;
setDimensions({
width: window.innerWidth,
height: Math.max(window.innerHeight, scrollHeight),
});
}
};

window.addEventListener("resize", updateDimensions);
window.addEventListener("scroll", updateDimensions);
updateDimensions();

return () => window.removeEventListener("resize", updateDimensions);
return () => {
window.removeEventListener("resize", updateDimensions);
window.removeEventListener("scroll", updateDimensions);
};
}, []);

const getCellSize = () => {
Expand All @@ -30,12 +40,13 @@ const Boxes: React.FC<GridProps> = ({ className = "" }) => {

const cellSize = getCellSize();
const columns = Math.ceil(dimensions.width / cellSize);
const rows = Math.ceil(dimensions.height / 2 / cellSize); // Only half the screen height
const rows = Math.ceil(dimensions.height / cellSize);

return (
<div
className={`absolute h-1/2 w-full overflow-hidden ${className}`}
style={{ zIndex: -1 }}
ref={containerRef}
className={`absolute w-full overflow-hidden ${className}`}
style={{ height: `${dimensions.height}px`, zIndex: -1 }}
>
{/* Left gradient overlay */}
<div className="absolute left-0 top-0 h-full w-1/2">
Expand All @@ -47,11 +58,13 @@ const Boxes: React.FC<GridProps> = ({ className = "" }) => {
className="relative flex w-full divide-x divide-dashed divide-neutral-700"
style={{ height: `${cellSize}px` }}
>
{Array.from({ length: columns / 2 }).map((_, colIndex) => (
<div key={colIndex} className="relative w-full bg-black">
<div className="absolute inset-0.5 bg-black" />
</div>
))}
{Array.from({ length: Math.ceil(columns / 2) }).map(
(_, colIndex) => (
<div key={colIndex} className="relative w-full bg-black">
<div className="absolute inset-0.5 bg-black" />
</div>
),
)}
</div>
))}
</div>
Expand All @@ -67,17 +80,17 @@ const Boxes: React.FC<GridProps> = ({ className = "" }) => {
className="relative flex w-full divide-x divide-dashed divide-neutral-700"
style={{ height: `${cellSize}px` }}
>
{Array.from({ length: columns / 2 }).map((_, colIndex) => (
<div key={colIndex} className="relative w-full bg-black">
<div className="absolute inset-0.5 bg-black" />
</div>
))}
{Array.from({ length: Math.ceil(columns / 2) }).map(
(_, colIndex) => (
<div key={colIndex} className="relative w-full bg-black">
<div className="absolute inset-0.5 bg-black" />
</div>
),
)}
</div>
))}
</div>
</div>
</div>
);
};

export default Boxes;
}

0 comments on commit 193ccd4

Please sign in to comment.