Skip to content

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

Buttons

Free

Magnetic Button for Vue UI SDK

Leans toward the pointer while it's over it and springs back when it leaves. For Vue with UI SDK, when a hero call to action should invite the click.

On this page7 sections

As the pointer moves over the button, the button leans toward it, following it across its face. When the pointer leaves, it springs back into place with a small overshoot.

Use it for one call to action on a landing page or a hero, where a little motion earns a second look. Don't use it in forms, toolbars or dense layouts, where a moving target is harder to hit; for a quieter answer to the pointer, use the Spotlight Button. It wraps the Button, so variant and size work as usual. Each pointer move writes the offset into two CSS variables that translate the button, and a CSS transition makes the release springy, so nothing re-renders while it moves. It stays still on touch screens, while disabled, and when the system asks for reduced motion.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Usage

<script setup lang="ts">
import { MagneticButton } from "@/components/ui/magnetic-button";
</script>
<template>
  <MagneticButton>Get started</MagneticButton>
</template>

Demos

Intensity

intensity is how strongly the button follows the pointer: subtle travels a sixth of the pointer's distance from its center, medium (the default) a third and strong a half. Keep strong for a large button with room around it, since it slides well out of its place.

Preview width

Accessibility

The movement is visual only: the button is announced and activated as any Button, and focus, Enter and Space work as usual. The pull follows a mouse or a pen, never a touch, and it's off when the system asks for reduced motion, so the button stays where it was laid out.

Enter
Activate the button.
Space
Activate the button.

FAQ

How does the spring work without an animation library?

While the pointer is over the button, it follows with a short ease-out transition. When the pointer leaves, the offset goes back to zero over half a second with a cubic-bezier that overshoots, which reads as a spring. A data-pulled attribute on the button, set while it follows, switches between the two timings.

The pull feels too floaty. How do I tone it down?

Set intensity="subtle", so the button travels a sixth of the pointer's distance from its center instead of a third.

Does it start moving before the pointer is over it?

No. It follows the pointer only while the pointer is over the button, so it never moves toward a pointer that's just passing by, and it costs nothing while the pointer is elsewhere on the page.

Does the moving button push its neighbors?

No. The pull is a CSS translate, which moves the button without changing the layout, so nothing around it shifts. Leave some room around it so it doesn't slide over its neighbors.

What happens on a touch screen?

Nothing moves. The pull only follows a mouse or a pen, because a finger has no hover and the button would jump under it. A tap presses it as usual.

Can I make it full width?

Yes. Pass className="w-full": there's no wrapper, so flex or grid layouts place the button directly. A wide button moves just as far, so subtle often suits it best.

What happens with reduced motion?

The button stays in place. It still presses, focuses and activates as usual.

API Reference

MagneticButton

PropType
clickEffect?boolean

Apply a click effect to the button.

Default
true
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled button in the tab order with aria-disabled instead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing.

Default
false
intensity?"subtle" | "medium" | "strong"

How strongly the button follows the pointer: subtle travels a sixth of the pointer's distance from its center, medium a third and strong a half.

Type
"subtle" | "medium" | "strong"
Default
"medium"
isLoading?boolean

Show a loading indicator.

Default
false
pendingAnnouncement?string

What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Default
"Loading"
type?"button" | "reset" | "submit"

What a native button does in a form: "submit" submits it and "reset" resets it. Ignored when the button renders as something else.

Type
"button" | "reset" | "submit"
Default
"button"
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

EventType
@click( event: MouseEvent, details?: ButtonActionDetails ) → unknown

Runs when the button is pressed. Return a Promise to keep it pending (data-pending) until it settles. details.source says how it was pressed; a wrapper passing the click on may leave it out.

Type
( event: MouseEvent, details?: ButtonActionDetails ) → unknown
Parameters

event – React.MouseEvent<HTMLButtonElement>

details? – ButtonActionDetails

Returns
unknown
CSS variableType
--magnetic-button-xstring

How far the button is pulled across, in pixels.

--magnetic-button-ystring

How far the button is pulled down, in pixels.