Background Blocks
A fully customizable grid with active blocks for interactive layouts
A fully customizable grid with active blocks for interactive layouts
npm install framer-motion
1import { cn } from '@/lib/utils';2import React, { useEffect, useState } from 'react';34function Blocks({5activeDivs,6divClass,7classname,8activeDivsClass,9containerRef,10}: {11activeDivsClass?: string;12activeDivs?: any;13divClass?: string;14classname?: string;15containerRef: React.RefObject<HTMLDivElement>;16}) {17const [blocks, setBlocks] = useState<JSX.Element[]>([]);1819useEffect(() => {20const updateBlocks = () => {21const container = containerRef.current;22if (container) {23const containerWidth = container.clientWidth;24const containerHeight = container.clientHeight;25const blockSize = containerWidth * 0.06; // Using 6% of section width for the block size2627const columns = Math.floor(containerWidth / blockSize);28const rows = Math.floor(containerHeight / blockSize);2930const newBlocks = Array.from({ length: columns }, (_, columnIndex) => (31<div key={columnIndex} className='w-[6vw] h-full'>32{Array.from({ length: rows }, (_, rowIndex) => (33<div34key={rowIndex}35className={cn(36`h-[6vh] w-full border-[1px] border-[#5dcece09] ${37// @ts-ignore38activeDivs[columnIndex]?.has(rowIndex)39? `${activeDivsClass}`40: ''41}`,42divClass43)}44style={{ height: `${blockSize}px` }}45></div>46))}47</div>48));4950setBlocks(newBlocks);51}52};5354updateBlocks();55window.addEventListener('resize', updateBlocks);5657return () => window.removeEventListener('resize', updateBlocks);58}, [activeDivs, activeDivsClass, divClass, containerRef]);5960return (61<div62className={cn(63'flex h-full overflow-hidden top-0 -inset-0 left-0 absolute',64classname65)}66>67{blocks}68</div>69);70}7172export default Blocks;
Prop | Type | Default | Description |
---|---|---|---|
activeDivsClass | string | undefined | The class name applied to active blocks. |
activeDivs | Object with number keys | undefined | A mapping where each key is a column index and each value is a set of row indices for active blocks. |
divClass | string | undefined | The class name applied to each block. |
classname | string | undefined | Additional class name applied to the outer container. |
containerRef | React.RefObject | undefined | A React ref object for the container element used to calculate dimensions. |