Skip to content

shadcn.io is an independent project built on shadcn/ui

Pro

Magnetic Field Background for Solid

A grid of small needles turns to follow the pointer and two wandering poles. For Solid with Ark UI, when a hero should answer the cursor.

On this page10 sections
UI library: Ark UI

A grid of small needles lines up with a magnetic field, like compasses laid out on a table. Two poles wander the box slowly, so the needles keep turning along the field lines between them, and the pointer is a third, stronger pole: the needles within its reach swing toward it. Needles near a pole are longer and take the accent color. It's a GLSL scene on WebGL2 with no dependency, sized in pixels, and its needles follow your theme: dark on a light page, light on a dark one.

Use it behind a hero or a feature section that should react to the visitor. For a line grid whose cells fill under the pointer, use Interactive Grid Pattern; for points joined by lines as they drift, Constellation; for a still grid of dots with soft lights, Dot Pattern. With reduced motion it holds one still frame.

Playground
Magnetic Field Background, still
Customize

Installation

Part of Pro
Pro unlocks the code and one-line install of every Pro button and text effect, plus the MCP for your AI editor.
Have Pro already? Sign in to open it.Opens here as soon as you're Pro.

Usage

import { MagneticField } from "@/components/ui/magnetic-field";
<MagneticField class="flex min-h-[32rem] flex-col items-center justify-center">
  <h1 class="text-5xl font-semibold">Find the nearest answer</h1>
</MagneticField>

Magnetic Field is a container: whatever you put inside sits on top of the needles, in normal flow. It fills its own box, so give it a height or content. For a backdrop behind the whole page, render it empty with className="fixed inset-0 -z-10".

Presets

Compassthe default lookTry it
<MagneticField />
ArrowsTry it
<MagneticField
  accentColor="#22d3ee"
  gap={34}
  poles={3}
  shape="arrow"
  size={20}
/>
BoldTry it
<MagneticField
  accentColor="#a3e635"
  gap={36}
  lineWidth={3.5}
  size={24}
/>
DotsTry it
<MagneticField accentColor="#8b5cf6" gap={18} shape="dot" size={14} />
Filingsthe default lookTry it
<MagneticField contrast={1.4} gap={14} lineWidth={1} size={11} />
MonoTry it
<MagneticField accentColor="var(--foreground)" glow={0} />
WhirlTry it
<MagneticField accentColor="#f472b6" angle={90} poles={1} reach={360} />

Each preset is a set of props, so copy one and keep tuning. Compass is the default. Filings packs short, thin needles 14 pixels apart, like iron filings on paper, Arrows shows the field's direction around three poles, Dots turns the needles into a dot grid that leans and swells near the poles, Whirl turns every needle 90 degrees so they circle a single pole, and Mono draws it all in your foreground color.

Examples

Hero

The needles far from a pole are short and faint, so copy reads over them anywhere; contrast={1.5} makes them fainter still. The headline and the line under it use the foreground color; the chip and the second button have a translucent ground of their own.

Preview width

Pointer

shape="arrow" draws each needle with a head, and reach={420} widens the pointer's pull: move across the box and the arrows turn to it, then swing back to the wandering poles as you leave. A press sends a ring through the field. The surface is fixed dark here, so the demo passes its own color and accentColor.

Preview width

In a card

shape="dot" makes it a dot grid: each dot leans along the field and swells near a pole. gap={18} packs the dots for a small box, and fade="bottom" clears the lower part of the card for the copy.

Preview width

Customization

Both colors are CSS colors, so theme variables work and follow a theme switch. color is var(--foreground) by default, drawn faint far from the poles; accentColor is the one color the needles take near a pole or the pointer. Set it to var(--primary) or to a brand color.

The surface is the root's own background, bg-background by default; give the root another (bg-slate-950) and pass a color that reads on it. size, gap, lineWidth and reach are in pixels, so the grid keeps its scale in any box. fade masks an edge, opacity turns the whole effect down, and grain adds film grain.

<MagneticField
  accentColor="var(--chart-1)"
  class="min-h-[32rem]"
  fade="radial"
  gap={20}
  shape="dot"
/>

Performance

It's one WebGL2 canvas and one fragment shader: each pixel works out the field at its own needle from at most four poles and measures its distance to one line, so the cost doesn't grow with the number of needles. There's no noise and no simulation. It draws only while it's on screen and the tab is visible, shares one frame loop with every other background on the page, and lowers its own resolution if frames run long. quality sets the render scale yourself, and paused holds the frame.

Accessibility

The needles are decoration: they're hidden from assistive tech and let every click through to your content. With reduced motion it draws one still frame and ignores the pointer. Nothing flashes.

Use the foreground color (text-foreground) for copy on top. At the default settings foreground text keeps more than 5:1 against every pixel of the scene on a dark surface and 8:1 on a light one, the accent needles under the pointer included. A needle is 1.5 pixels thick, so large text reads over it easily; for long small text, raise contrast or lower intensity so the far needles fade further.

FAQ

How do I change the colors?

color is the needles' color and accentColor is the one they take near a pole or the pointer. Both accept any CSS color: hex, oklch() or a theme variable such as var(--primary), which follows your theme when it changes. For one color throughout, set accentColor to the same value as color, as the Mono preset does.

Does it work on a light page?

Yes, with no prop to set. The needles are drawn in var(--foreground), so they're dark on a light page and light on a dark one, and the default accent reads on both. On a surface you fix yourself, such as bg-slate-950 in a light theme, pass a color that reads on it, since the theme variable still resolves to the theme's own.

What does it do on a phone, where there is no pointer?

The poles keep wandering, so the needles keep turning by themselves and the scene looks alive without any input. Touch never moves the field, so scrolling the page isn't disturbed. drift sets how fast the poles wander, and drift={0} holds them in place.

How do I make the grid denser or the needles larger?

gap is the distance between two needles and size is a needle's length, both in pixels. A needle never grows past the gap, so raise both for a bolder field (gap={36} size={24} lineWidth={3.5}) or lower both for a fine one (gap={14} size={11} lineWidth={1}). The grid is centered, and a needle the edge would cut is left out.

How do I put it behind the whole page?

Render it empty with className="fixed inset-0 -z-10". It still follows the pointer there, since it listens on the window, over links and buttons too. To put it behind one section, wrap that section's content in it and give it a height, such as min-h-svh.

Will it slow my page down?

No. It's among the lightest scenes in the collection: a few additions and one line distance for each pixel, on the GPU, and nothing at all while it's off screen or the tab is hidden. A grid of 3,000 needles costs the same as one of 300, since no needle is a DOM element.

Does it work with Next.js and server components?

Yes. Import it into a server component and it renders its box and your content on the server; the canvas starts in the browser and fades in over the surface. It needs WebGL2, which every current browser has. Without it the box keeps its background, or the fallback you pass.

What happens with reduced motion?

It draws one still frame of the field, with the poles where they stand at that moment, and the pointer no longer moves the needles. The frame is the same one every time, so the page looks the same on each visit.

API Reference

MagneticField

data-slot="magnetic-field"

PropType
accentColor?string

The color of the needles close to a pole or to the pointer, any CSS color.

Default
"#ff5b2e"
angle?number

How far every needle is turned from the field's direction, in degrees, from -180 to 180. At 0 the needles point at the poles; at 90 they circle them.

Default
0
color?string

The color of the needles, any CSS color. The default follows your theme: dark needles on a light page, light ones on a dark page.

Default
"var(--foreground)"
contrast?number

How much stronger the needles near a pole are than those far from it, from 0 (all alike) to 2 (far ones short and faint).

Default
1
drift?number

How fast the poles wander when the pointer is away, from 0 (they hold their place) to 2.

Default
1
fade?"none" | "radial" | "top" | "bottom" | "left" | "right"

Which edges fade out to the surface: radial keeps the middle, the others name the side that fades.

Type
"none" | "radial" | "top" | "bottom" | "left" | "right"
Default
"none"
fallback?string

What shows until the first frame and in browsers without WebGL2: a CSS background value that looks like the scene.

gap?number

The distance between two needles, in pixels, from 10 to 64.

Default
26
glow?number

How much soft light sits on the poles and under the pointer, from 0 to 2.

Default
1
grain?number

Film grain over the scene, from 0 to 1.

Default
0
intensity?number

How strongly the needles show.

Default
1
interactive?boolean

Whether the scene follows the pointer and answers a press. Touch never moves it, so scrolling a page on a phone isn't disturbed.

Default
true
lineWidth?number

The thickness of a needle, in pixels, from 0.5 to 4. A dot is about twice as wide.

Default
1.5
opacity?number

How strongly the scene shows over the surface, from 0 to 1.

Default
1
paused?boolean

Holds the current frame.

Default
false
poles?number

How many poles wander the box, from 1 to 3. One draws every needle to a single point; two or three bend the needles along field lines between them.

Default
2
quality?number

Render scale, from 0.25 to 1, of the screen's pixels (capped at 2×). Lower is cheaper and softer.

Default
1
reach?number

How far the pointer's pull reaches, in pixels, from 60 to 600.

Default
280
seed?number

Another path for the wandering poles.

Default
0
shape?"line" | "dot" | "arrow"

How a needle is drawn: a line, an arrow that shows which way the field runs (out of one pole, into the next, toward the pointer), or a dot that leans the same way.

Type
"line" | "dot" | "arrow"
Default
"line"
size?number

The length of a needle, in pixels, from 4 to 48; it never grows past the gap. For a dot, how far it can lean.

Default
16
speed?number

Animation speed: 1 is normal, 0 holds the frame.

Default
1
time?number

A fixed moment, in seconds: draws it and doesn't animate.