Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

2.9 KB
Raw Blame
---
title: Pulsating Button
date: 2024-07-23
description: An animated pulsating button useful for capturing attention of users.
author: shikhap04
published: true
---

<ComponentPreview name="pulsating-button-demo" />

## Installation

<Tabs defaultValue="cli">

<TabsList>
  <TabsTrigger value="cli">CLI</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">

```bash
npx shadcn@latest add @magicui/pulsating-button
```

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource name="pulsating-button" />

<Step>Update the import paths to match your project setup.</Step>

<Step>Add the required CSS animations</Step>

Add the following animations to your global CSS file.

```css title="app/globals.css" showLineNumbers {2, 4-12}
@theme inline {
  --animate-pulse: pulse var(--duration) ease-out infinite;
  --animate-pulse-ripple: pulse-ripple var(--duration)
    cubic-bezier(0.16, 1, 0.3, 1) infinite;

  @keyframes pulse {
    0%,
    100% {
      box-shadow: 0 0 0 0 var(--pulse-color, oklch(from var(--bg) l c h / 0.5));
    }
    50% {
      box-shadow: 0 0 0 var(--distance)
        var(--pulse-color, oklch(from var(--bg) l c h / 0.5));
    }
  }

  @keyframes pulse-ripple {
    0% {
      box-shadow: 0 0 0 0 oklch(from var(--pulse-color, var(--bg)) l c h / 1);
    }
    100% {
      box-shadow: 0 0 0 var(--distance)
        oklch(from var(--pulse-color, var(--bg)) l c h / 0);
    }
  }
}
```

</Steps>

</TabsContent>

</Tabs>

## Usage

```tsx showLineNumbers
import { PulsatingButton } from "@/components/ui/pulsating-button"
```

```tsx showLineNumbers
<PulsatingButton>Pulsating Button</PulsatingButton>
```

## Examples

### Ripple Variant

<ComponentPreview name="pulsating-button-demo-2" />

## Props

| Prop         | Type                  | Default                          | Description                                |
| ------------ | --------------------- | -------------------------------- | ------------------------------------------ |
| `children`   | `React.ReactNode`     | `-`                              | The content of the button.                 |
| `className`  | `string`              | `-`                              | Additional class names for the button.     |
| `pulseColor` | `string`              | `Derived from button background` | Any valid CSS color for the pulsing waves. |
| `duration`   | `string`              | `1.5s`                           | The duration of one pulse.                 |
| `distance`   | `string`              | `8px`                            | How far the pulse expands from the button. |
| `variant`    | `"pulse" \| "ripple"` | `pulse`                          | The pulse animation style.                 |

## Credits

- Credit to [@shikhap04](https://github.com/shikhap04) & [@abdmjd1](https://github.com/abdmjd1)