Background Blocks

A fully customizable grid with active blocks for interactive layouts

Installation

npm install framer-motion
beam.tsx
import { cn } from '@/lib/utils'
import React, { useEffect, useState } from 'react'
 
function Blocks({
  activeDivs,
  divClass,
  classname,
  activeDivsClass,
  containerRef,
}: {
  activeDivsClass?: string
  activeDivs?: any
  divClass?: string
  classname?: string
  containerRef: React.RefObject<HTMLDivElement>
}) {
  const [blocks, setBlocks] = useState<JSX.Element[]>([])
 
  useEffect(() => {
    const updateBlocks = () => {
      const container = containerRef.current
      if (container) {
        const containerWidth = container.clientWidth
        const containerHeight = container.clientHeight
        const blockSize = containerWidth * 0.06 // Using 6% of section width for the block size
 
        const columns = Math.floor(containerWidth / blockSize)
        const rows = Math.floor(containerHeight / blockSize)
 
        const newBlocks = Array.from({ length: columns }, (_, columnIndex) => (
          <div key={columnIndex} className="w-[6vw] h-full">
            {Array.from({ length: rows }, (_, rowIndex) => (
              <div
                key={rowIndex}
                className={cn(
                  `h-[6vh] w-full border-[1px] border-[#5dcece09] ${
                    // @ts-ignore
                    activeDivs[columnIndex]?.has(rowIndex)
                      ? `${activeDivsClass}`
                      : ''
                  }`,
                  divClass
                )}
                style={{ height: `${blockSize}px` }}
              ></div>
            ))}
          </div>
        ))
 
        setBlocks(newBlocks)
      }
    }
 
    updateBlocks()
    window.addEventListener('resize', updateBlocks)
 
    return () => window.removeEventListener('resize', updateBlocks)
  }, [activeDivs, activeDivsClass, divClass, containerRef])
 
  return (
    <div
      className={cn(
        'flex h-full overflow-hidden top-0 -inset-0 left-0 absolute',
        classname
      )}
    >
      {blocks}
    </div>
  )
}
 
export default Blocks

Props

PropTypeDefaultDescription
activeDivsClassstringundefinedThe class name applied to active blocks.
activeDivsObject with number keysundefinedA mapping where each key is a column index and each value is a set of row indices for active blocks.
divClassstringundefinedThe class name applied to each block.
classnamestringundefinedAdditional class name applied to the outer container.
containerRefReact.RefObjectundefinedA React ref object for the container element used to calculate dimensions.