react-tiled-background
Version:
Reusable React component that adds an animated, Excel-style tile grid overlay to any element.
83 lines (80 loc) • 2.97 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/components/index.ts
var index_exports = {};
__export(index_exports, {
TiledBackground: () => TiledBackground_default
});
module.exports = __toCommonJS(index_exports);
// src/components/TiledBackground.tsx
var import_react = require("react");
var import_jsx_runtime = require("react/jsx-runtime");
var TiledBackground = ({ children }) => {
const [tiles, setTiles] = (0, import_react.useState)([]);
const wrapperRef = (0, import_react.useRef)(null);
(0, import_react.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__ */ (0, import_jsx_runtime.jsx)(
"div",
{
className: classes,
style: { animationDelay: `${Math.random() * 10}s` }
},
i
);
})
);
};
createTiles();
window.addEventListener("resize", createTiles);
return () => window.removeEventListener("resize", createTiles);
}, []);
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
"div",
{
className: "tiled-background-wrapper",
ref: wrapperRef,
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "tiles-container", children: tiles }),
children
]
}
);
};
var TiledBackground_default = TiledBackground;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TiledBackground
});