Astro Dot Pattern
A dot background that fills its container.
Installation
bunx --bun shadcn@latest add @gnostic/dot-patternnpx shadcn@latest add @gnostic/dot-patternpnpm dlx shadcn@latest add @gnostic/dot-patternComplete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/patterns/dot-pattern/dot-pattern.astro
---
import type { HTMLAttributes } from "astro/types"
import type { DotPatternProps } from "./types.ts"
import { cn } from "cn"
import { mergeStyles } from "../../../lib/merge-styles.ts"
/**
* Dot background.
*
* The pattern fills its nearest positioned parent.
* Each dot is one tile of a radial-gradient background.
*/
type Props = HTMLAttributes<"div"> & DotPatternProps
const {
dotSize = 1,
dotSpace = "1.5rem",
dotColor = "color-mix(in srgb, var(--foreground) 16%, transparent)",
dotBg = "transparent",
class: className,
style: styleProp,
...props
} = Astro.props
const style = mergeStyles(
{
"--dot-color": dotColor,
"background-color": dotBg,
"background-image": `radial-gradient(circle, var(--dot-color) ${dotSize}px, transparent ${dotSize * 1.2}px)`, // 1.2 multiple adds a small amount of blur around the dots
"background-size": `${dotSpace} ${dotSpace}`,
"background-position": "center",
},
styleProp,
)
---
<div
aria-hidden="true"
data-slot="dot-pattern"
class={cn("pointer-events-none absolute inset-0 size-full", className)}
style={style}
{...props}
></div>
src/components/patterns/dot-pattern/index.ts
export { default as DotPattern } from './dot-pattern.astro'
export type { DotPatternProps } from './types.ts'
src/components/patterns/dot-pattern/types.ts
export type DotPatternProps = {
/**
* Radius of each dot, as a CSS size.
* @default 1
*/
dotSize?: number
/**
* Distance from one dot to the next, as a CSS size.
* The tile is square, so this sets both axes.
* @default "1.5rem"
*/
dotSpace?: string
/**
* Valid CSS color string. Use an opaque color for better contrast.
* @default `color-mix(in srgb, var(--foreground) 16%, transparent)`
*/
dotColor?: string
/**
* Valid CSS color string.
* @default "transparent"
*/
dotBg?: string
}
Usage
Place the pattern inside a positioned parent. The element uses absolute inset-0
and fills that parent.
import { DotPattern } from "@/components/patterns/dot-pattern"
<div class="relative h-72 overflow-hidden">
<DotPattern />
</div>
Examples
Larger dots
dotSize sets the dot radius in pixels. dotSpace sets the distance from one
dot to the next.
Custom colors
dotColor and dotBg accept any CSS color. The default dot color mixes
muted-foreground to 18 percent opacity.
Card background
Put the pattern inside a CardFrame + Card to have a subtle raised effect.
API Reference
DotPatternProps
dotSize?number
Radius of each dot, as a CSS size.
number1dotSpace?string
Distance from one dot to the next, as a CSS size. The tile is square, so this sets both axes.
string"1.5rem"dotColor?string
Valid CSS color string. Use an opaque color for better contrast.
string`color-mix(in srgb, var(--foreground) 16%, transparent)`dotBg?string
Valid CSS color string.
string"transparent"See the registry source for the component source.