UIPin
ComponentsIconsIllustrationsMotionTemplatesApps
Boards
UIPin

Free, open-source UI components, icons, illustrations, motion, templates and apps, in one search. Every item links to its source and licence.

UIPin is free. If it saved you time, you can keep it running:

Buy me a coffee
ComponentsIconsIllustrationsMotionTemplatesAppsLibrariesCompareNew this weekMCP for agentsBoardsPrivacy

Components, icons and artwork belong to their authors and are shown under their open-source licences.

Animate UI/Hexagon Background

Backgrounds
1 of 10
Compiling

Theme it

Every preview, live

Brand colour

Corner radius

Font

Recolours shadcn’s tokens, Tailwind’s accent colours and colours set in the code. Red, amber and green classes stay, since they usually mean error, warning or success.

10 versions·2 libraries

  • Hexagon Background
  • Bubble Background
  • Fireworks Background
  • Glyph Background
  • Gradient Background
  • Gravity Stars Background
  • Hole Background
  • Stars Background
  • Topography Background
  • Magic UI

Hexagon Background

A background component featuring an interactive hexagon grid.

npx shadcn@latest add https://animate-ui.com/r/backgrounds-hexagon.json
Open in v0 Docs Repo
Library
Animate UI
License
MIT + Commons Clause

Free to use in your apps; you can’t resell the components themselves.

Stack
React · Tailwind CSS
Packages
cn
Size
126 lines

Code

@components/animate-ui/backgrounds/hexagon.tsx

'use client';

import * as React from 'react';

import { cn } from 'cn';

const range = (length: number) => Array.from({ length }, (_, i) => i);

type HexagonBackgroundProps = React.ComponentProps<'div'> & {
  hexagonProps?: React.ComponentProps<'div'>;
  hexagonSize?: number;
  hexagonMargin?: number;
};

function HexagonBackground({
  ref,
  className,
  children,
  hexagonProps,
  hexagonSize = 75,
  hexagonMargin = 3,
  ...props
}: HexagonBackgroundProps) {
  const hexagonWidth = hexagonSize;
  const hexagonHeight = hexagonSize * 1.1;
  // Rows a slanted side apart, so every border is hexagonMargin thick.
  const corner = hexagonHeight / 4;
  const side = Math.hypot(corner, hexagonWidth / 2);
  const rowSpacing =
    hexagonHeight -
    corner +
    (hexagonMargin * (2 * side - corner)) / hexagonWidth;
  const computedMarginTop = rowSpacing - hexagonHeight;
  const oddRowMarginLeft = -(hexagonSize / 2);
  const evenRowMarginLeft = hexagonMargin / 2;

  const containerRef = React.useRef<HTMLDivElement>(null);
  React.useImperativeHandle(ref, () => containerRef.current as HTMLDivElement);

  const [gridDimensions, setGridDimensions] = React.useState({
    rows: 0,
    columns: 0,
  });

  React.useLayoutEffect(() => {
    const container = containerRef.current;
    if (!container) return;
    const update = () => {
      const { width, height } = container.getBoundingClientRect();
      const rows = Math.ceil(height / rowSpacing) + 1;
      const columns = Math.ceil(width / hexagonWidth) + 1;
      setGridDimensions((prev) =>
        prev.rows === rows && prev.columns === columns
          ? prev
          : { rows, columns },
      );
    };
    update();
    const resizeObserver = new ResizeObserver(update);
    resizeObserver.observe(container);
    return () => resizeObserver.disconnect();
  }, [rowSpacing, hexagonWidth]);

  return (
    <div
      ref={containerRef}
      data-slot="hexagon-background"
      className={cn(
        'relative isolate size-full overflow-hidden dark:bg-neutral-900 bg-neutral-100',
        className,
      )}
      {...props}
      style={
        {
          '--hexagon-margin': `${hexagonMargin}px`,
          ...props.style,
        } as React.CSSProperties
      }
    >
      <div
        aria-hidden="true"
        className="absolute top-0 left-0 -z-10 size-full overflow-hidden text-base leading-6"
      >
        {range(gridDimensions.rows).map((rowIndex) => (
          <div
            key={rowIndex}
            style={{
              marginTop: computedMarginTop,
              marginLeft:
                ((rowIndex + 1) % 2 === 0
                  ? evenRowMarginLeft
                  : oddRowMarginLeft) - 10,
            }}
            className="flex w-max"
          >
            {range(gridDimensions.columns).map((colIndex) => (
              <div
                key={colIndex}
                {...hexagonProps}
                style={{
                  width: hexagonWidth,
                  height: hexagonHeight,
                  marginLeft: hexagonMargin,
                  ...hexagonProps?.style,
                }}
                className={cn(
                  'relative',
                  '[clip-path:polygon(50%_0%,_100%_25%,_100%_75%,_50%_100%,_0%_75%,_0%_25%)]',
                  "before:content-[''] before:absolute before:top-0 before:left-0 before:w-full before:h-full dark:before:bg-neutral-950 before:bg-white before:opacity-100 before:transition-colors before:duration-1000",
                  "after:content-[''] after:absolute after:inset-[var(--hexagon-margin)] dark:after:bg-neutral-950 after:bg-white",
                  'after:[clip-path:polygon(50%_0%,_100%_25%,_100%_75%,_50%_100%,_0%_75%,_0%_25%)]',
                  'hover:before:bg-neutral-200 dark:hover:before:bg-neutral-800 hover:before:opacity-100 hover:before:duration-0 dark:hover:after:bg-neutral-900 hover:after:bg-neutral-100 hover:after:opacity-100 hover:after:duration-0',
                  hexagonProps?.className,
                )}
              />
            ))}
          </div>
        ))}
      </div>
      {children}
    </div>
  );
}

export { HexagonBackground, type HexagonBackgroundProps };

More like this

Hexagon Pattern

Magic UI·Backgrounds

Animated Grid Pattern

Magic UI·Backgrounds

Safari

Magic UI·Media & 3D

Grid Pattern

Magic UI·Backgrounds

Glyph Matrix

Magic UI·Cursor & effects

Drape Background

Kokonut UI·Backgrounds

Map

Eldora UI·Media & 3D

Side Panel

Cult UI·Other

Border Trail

Motion Primitives·Cursor & effects

8-bit XP Bar

8bitcn·Feedback & loaders

Card Nav

React Bits·NavigationMIT+

Striped Pattern

Magic UI·Backgrounds

Glyph Background

Animate UI·BackgroundsMIT+

Scroll Index

Kokonut UI·Text effects

Carousel

shadcn/ui·Media & 3D