UNPKG

react-tiled-background

Version:

Reusable React component that adds an animated, Excel-style tile grid overlay to any element.

57 lines (56 loc) 1.84 kB
// src/components/TiledBackground.tsx import { useState, useEffect, useRef } from "react"; import { jsx, jsxs } from "react/jsx-runtime"; var TiledBackground = ({ children }) => { const [tiles, setTiles] = useState([]); const wrapperRef = useRef(null); useEffect(() => { const createTiles = () => { if (!wrapperRef.current) return; const style = getComputedStyle(document.documentElement); const tileWidth = parseInt(style.getPropertyValue("--tile-width")) || 100; const tileHeight = parseInt(style.getPropertyValue("--tile-height")) || 30; const { width, height } = wrapperRef.current.getBoundingClientRect(); const cols = Math.ceil(width / tileWidth); const rows = Math.ceil(height / tileHeight); const numTiles = cols * rows; setTiles( Array.from({ length: numTiles }).map((_, i) => { const r = Math.random(); let classes = "tile"; if (r < 0.08) { classes += " tile--pulsing"; } else if (r < 0.19) { classes += " tile--border-pulse"; } return /* @__PURE__ */ jsx( "div", { className: classes, style: { animationDelay: `${Math.random() * 10}s` } }, i ); }) ); }; createTiles(); window.addEventListener("resize", createTiles); return () => window.removeEventListener("resize", createTiles); }, []); return /* @__PURE__ */ jsxs( "div", { className: "tiled-background-wrapper", ref: wrapperRef, children: [ /* @__PURE__ */ jsx("div", { className: "tiles-container", children: tiles }), children ] } ); }; var TiledBackground_default = TiledBackground; export { TiledBackground_default as TiledBackground };