Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Astro Dot Pattern with Glow

A dot background that reveals a colored glow under the pointer.

Move the pointer

Installation

bunx --bun shadcn@latest add @gnostic/dot-pattern-with-glow
npx shadcn@latest add @gnostic/dot-pattern-with-glow
pnpm dlx shadcn@latest add @gnostic/dot-pattern-with-glow

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/patterns/dot-pattern-with-glow/dot-pattern-with-glow.astro
---
import type { HTMLAttributes } from "astro/types"
import type { DotPatternWithGlowProps } from "./types.ts"

import { mergeStyles } from "../../../lib/merge-styles.ts"

import DotPattern from "../dot-pattern/dot-pattern.astro"
import HoverGlow from "../hover-glow/hover-glow.astro"

/**
 * Dot background with a glow clipped to each dot.
 *
 * HoverGlow tracks the pointer. This component adds the base dots and
 * the mask that shows the glow only inside those dots.
 */
type Props = HTMLAttributes<"div"> & DotPatternWithGlowProps

const {
  glowColor,
  glowSize,
  maskDotSize = 1.25,
  dotLayerProps,
  class: className,
  style: styleProp,
  ...props
} = Astro.props

/** Space between dots. The mask tile uses the same distance. */
const dotSpace = dotLayerProps?.dotSpace ?? 16
/** Radius of each base dot. */
const dotSize = dotLayerProps?.dotSize ?? 1

/** The mask repeats so the glow is visible only inside each dot. */
const style = mergeStyles(
  {
    "--pattern-mask-dot-size": `${maskDotSize}px`,
  },
  styleProp,
)
---

<HoverGlow
  glowColor={glowColor}
  glowSize={glowSize}
  maskImage="radial-gradient(circle, black var(--pattern-mask-dot-size), transparent var(--pattern-mask-dot-size))"
  maskSize={`${dotSpace}px ${dotSpace}px`}
  maskPosition="center"
  class={className}
  style={style}
  {...props}
>
  <slot />
  <DotPattern
    dotSize={dotSize}
    dotSpace={`${dotSpace}px`}
    dotColor={dotLayerProps?.dotColor}
    dotBg={dotLayerProps?.dotBg}
    class={dotLayerProps?.class}
    style={dotLayerProps?.style}
  />
</HoverGlow>
src/components/patterns/dot-pattern-with-glow/index.ts
export { default as DotPatternWithGlow } from './dot-pattern-with-glow.astro'
export type { DotPatternWithGlowProps } from './types.ts'
src/components/patterns/dot-pattern-with-glow/types.ts
/**
 * Options forwarded to the base dot layer.
 * `dotSize` and `dotSpace` also align the glow mask with those dots.
 */

type DotLayerProps = {
    /** Radius of each base dot, in pixels. */
    dotSize?: number
    /** Distance from one base dot to the next, in pixels. */
    dotSpace?: number
    /** CSS color of each base dot. */
    dotColor?: string
    /** CSS color behind the base dots. */
    dotBg?: string
    /** Extra classes on the base dot layer. */
    class?: string
    /** Extra CSS declarations on the base dot layer. */
    style?: astroHTML.JSX.CSSProperties
}

/**
 * Dot background with a glow that follows the pointer.
 *
 * The base dots stay visible. HoverGlow paints the color layer. A dot mask
 * shows that color only inside the dot shapes.
 */
export type DotPatternWithGlowProps = {
    /**
     * CSS color of the glow.
     * Use the color's alpha value to set the glow opacity.
     * The default keeps the glow soft.
     *
     * @default "color-mix(in oklch, var(--primary) 72%, transparent)"
     */
    glowColor?: string
    /**
     * Radius of the glow, in pixels.
     * @default 96
     */
    glowSize?: number
    /**
     * Radius of each dot in the glow mask, in pixels.
     * @default 1.25
     */
    maskDotSize?: number
    /**
     * Props for the base dot layer.
     *
     * @link {DotPatternProps}
     */
    dotLayerProps?: DotLayerProps
}

Usage

The component is the positioned container. Move the pointer over it to show the glow. The slot renders inside that container, under the dot layers.

The pointer effect comes from Hover Glow. This component adds the base dots and a mask, so the color shows only inside each dot.

import { DotPatternWithGlow } from "@/components/patterns/dot-pattern-with-glow"
<DotPatternWithGlow class="h-72">
  <p>Move the pointer</p>
</DotPatternWithGlow>

The glow color defaults to a 40 percent mix of var(--color-primary). Pass var(--color-primary) when the glow should be stronger:

<DotPatternWithGlow class="h-72" glowColor="var(--color-primary)" />

The glow fades out when the pointer leaves the container or the window loses focus.

Examples

Larger glow

glowSize sets the glow radius. maskDotSize sets the radius of each glowing dot. dotLayerProps sets the base dots and keeps the mask on the same grid.

Larger glow

API Reference

DotPatternWithGlowProps

PropType
glowColor?string

CSS color of the glow. Use the color's alpha value to set the glow opacity. The default keeps the glow soft.

Typestring
Default"color-mix(in oklch, var(--primary) 72%, transparent)"
glowSize?number

Radius of the glow, in pixels.

Typenumber
Default96
maskDotSize?number

Radius of each dot in the glow mask, in pixels.

Typenumber
Default1.25
dotLayerProps?DotLayerProps

Props for the base dot layer.

TypeDotLayerProps

See the registry source for the component source.