Wrapper that pulls its content toward a nearby mouse, or pushes it away, then springs back. Inner layers can travel further for depth.
Ready when you are
Free for small teams. Upgrade when you grow.
Ready when you are
Free for small teams. Upgrade when you grow.
Ready when you are
Free for small teams. Upgrade when you grow.
Ready when you are
Free for small teams. Upgrade when you grow.
Ready when you are
Free for small teams. Upgrade when you grow.
@php
$section = 'grid place-items-center gap-5 rounded-xl border border-line bg-surface px-6 py-12 text-center';
$title = 'text-2xl font-semibold text-ink';
$text = 'mt-2 text-sm text-muted';
$icon = 'size-5';
@endphp
<section class="{{ $section }}">
<div>
<p class="{{ $title }}">Ready when you are</p>
<p class="{{ $text }}">Free for small teams. Upgrade when you grow.</p>
</div>
<x-markas.magnet>
<x-markas.button size="lg">Get started <span data-depth="0.5"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class="{{ $icon }}"><path d="M5 12h14"></path><path d="m13 6 6 6-6 6"></path></svg></span></x-markas.button>
</x-markas.magnet>
</section>import { Magnet } from "@/components/markas/Magnet";
import { Button } from "@/components/markas/Button";
const section =
"grid place-items-center gap-5 rounded-xl border border-line bg-surface px-6 py-12 text-center";
const title = "text-2xl font-semibold text-ink";
const text = "mt-2 text-sm text-muted";
const icon = "size-5";
export default function MagnetExample() {
return (
<section className={section}>
<div>
<p className={title}>Ready when you are</p>
<p className={text}>Free for small teams. Upgrade when you grow.</p>
</div>
<Magnet>
<Button size="lg">
Get started{" "}
<span data-depth="0.5">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className={icon}
>
<path d="M5 12h14"></path>
<path d="m13 6 6 6-6 6"></path>
</svg>
</span>
</Button>
</Magnet>
</section>
);
}<script setup lang="ts">
import Magnet from "@/components/markas/Magnet.vue";
import Button from "@/components/markas/Button.vue";
const section =
"grid place-items-center gap-5 rounded-xl border border-line bg-surface px-6 py-12 text-center";
const title = "text-2xl font-semibold text-ink";
const text = "mt-2 text-sm text-muted";
const icon = "size-5";
</script>
<template>
<section :class="section">
<div>
<p :class="title">Ready when you are</p>
<p :class="text">Free for small teams. Upgrade when you grow.</p>
</div>
<Magnet>
<Button size="lg"
>Get started
<span data-depth="0.5"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
:class="icon"
>
<path d="M5 12h14"></path>
<path d="m13 6 6 6-6 6"></path></svg></span
></Button>
</Magnet>
</section>
</template><script lang="ts">
import Magnet from "$lib/components/markas/Magnet.svelte";
import Button from "$lib/components/markas/Button.svelte";
const section =
"grid place-items-center gap-5 rounded-xl border border-line bg-surface px-6 py-12 text-center";
const title = "text-2xl font-semibold text-ink";
const text = "mt-2 text-sm text-muted";
const icon = "size-5";
</script>
<section class={section}>
<div>
<p class={title}>Ready when you are</p>
<p class={text}>Free for small teams. Upgrade when you grow.</p>
</div>
<Magnet>
<Button size="lg"
>Get started
<span data-depth="0.5"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class={icon}
>
<path d="M5 12h14"></path>
<path d="m13 6 6 6-6 6"></path></svg
></span
></Button
>
</Magnet>
</section>---
import Magnet from "@/components/markas/Magnet.astro";
import Button from "@/components/markas/Button.astro";
const section =
"grid place-items-center gap-5 rounded-xl border border-line bg-surface px-6 py-12 text-center";
const title = "text-2xl font-semibold text-ink";
const text = "mt-2 text-sm text-muted";
const icon = "size-5";
---
<section class={section}>
<div>
<p class={title}>Ready when you are</p>
<p class={text}>Free for small teams. Upgrade when you grow.</p>
</div>
<Magnet>
<Button size="lg">Get started
<span data-depth="0.5"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class={icon}>
<path d="M5 12h14"></path>
<path d="m13 6 6 6-6 6"></path></svg></span></Button>
</Magnet>
</section>import { Component } from "@angular/core";
import { Magnet } from "./markas/magnet.component";
import { Button } from "./markas/button.component";
@Component({
selector: "markas-example",
imports: [Magnet, Button],
template: `
<section [class]="section">
<div>
<p [class]="title">Ready when you are</p>
<p [class]="text">Free for small teams. Upgrade when you grow.</p>
</div>
<markas-magnet>
<button markas-button size="lg">
Get started
<span data-depth="0.5"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
[class]="icon"
>
<path d="M5 12h14"></path>
<path d="m13 6 6 6-6 6"></path></svg
></span>
</button>
</markas-magnet>
</section>
`,
})
export default class MagnetExample {
protected readonly section =
"grid place-items-center gap-5 rounded-xl border border-line bg-surface px-6 py-12 text-center";
protected readonly title = "text-2xl font-semibold text-ink";
protected readonly text = "mt-2 text-sm text-muted";
protected readonly icon = "size-5";
}Examples
Magnetic social icons in a footer
@php
$footer = 'flex items-center justify-between gap-4 rounded-xl border border-line bg-surface px-5 py-4';
$text = 'text-sm text-muted';
$row = 'flex items-center gap-3';
$link = 'grid size-10 place-items-center rounded-full border border-line bg-surface text-ink transition-colors duration-150 ease-out hover:bg-subtle focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none';
$icon = 'size-5';
@endphp
<footer class="{{ $footer }}">
<p class="{{ $text }}">Follow MarkasUI</p>
<div class="{{ $row }}">
<x-markas.magnet :strength="0.5" :radius="32"><a href="#" aria-label="Email" class="{{ $link }}"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class="{{ $icon }}"><path d="M4 6h16v12H4z"></path><path d="m4 7 8 6 8-6"></path></svg></a></x-markas.magnet>
<x-markas.magnet :strength="0.5" :radius="32"><a href="#" aria-label="Community chat" class="{{ $link }}"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class="{{ $icon }}"><path d="M4 5h16v11H9l-5 4V5Z"></path></svg></a></x-markas.magnet>
<x-markas.magnet :strength="0.5" :radius="32"><a href="#" aria-label="Videos" class="{{ $link }}"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class="{{ $icon }}"><path d="M4 6h16v12H4z"></path><path d="m10 9 5 3-5 3V9Z"></path></svg></a></x-markas.magnet>
<x-markas.magnet :strength="0.5" :radius="32"><a href="#" aria-label="Website" class="{{ $link }}"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class="{{ $icon }}"><path d="M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Z"></path><path d="M3 12h18"></path><path d="M12 3c2.5 2.7 3.5 5.7 3.5 9s-1 6.3-3.5 9c-2.5-2.7-3.5-5.7-3.5-9s1-6.3 3.5-9Z"></path></svg></a></x-markas.magnet>
</div>
</footer>import { Magnet } from "@/components/markas/Magnet";
const footer =
"flex items-center justify-between gap-4 rounded-xl border border-line bg-surface px-5 py-4";
const text = "text-sm text-muted";
const row = "flex items-center gap-3";
const link =
"grid size-10 place-items-center rounded-full border border-line bg-surface text-ink transition-colors duration-150 ease-out hover:bg-subtle focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const icon = "size-5";
export default function Example() {
return (
<footer className={footer}>
<p className={text}>Follow MarkasUI</p>
<div className={row}>
<Magnet strength={0.5} radius={32}>
<a href="#" aria-label="Email" className={link}>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className={icon}
>
<path d="M4 6h16v12H4z"></path>
<path d="m4 7 8 6 8-6"></path>
</svg>
</a>
</Magnet>
<Magnet strength={0.5} radius={32}>
<a href="#" aria-label="Community chat" className={link}>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className={icon}
>
<path d="M4 5h16v11H9l-5 4V5Z"></path>
</svg>
</a>
</Magnet>
<Magnet strength={0.5} radius={32}>
<a href="#" aria-label="Videos" className={link}>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className={icon}
>
<path d="M4 6h16v12H4z"></path>
<path d="m10 9 5 3-5 3V9Z"></path>
</svg>
</a>
</Magnet>
<Magnet strength={0.5} radius={32}>
<a href="#" aria-label="Website" className={link}>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className={icon}
>
<path d="M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Z"></path>
<path d="M3 12h18"></path>
<path d="M12 3c2.5 2.7 3.5 5.7 3.5 9s-1 6.3-3.5 9c-2.5-2.7-3.5-5.7-3.5-9s1-6.3 3.5-9Z"></path>
</svg>
</a>
</Magnet>
</div>
</footer>
);
}<script setup lang="ts">
import Magnet from "@/components/markas/Magnet.vue";
const footer =
"flex items-center justify-between gap-4 rounded-xl border border-line bg-surface px-5 py-4";
const text = "text-sm text-muted";
const row = "flex items-center gap-3";
const link =
"grid size-10 place-items-center rounded-full border border-line bg-surface text-ink transition-colors duration-150 ease-out hover:bg-subtle focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const icon = "size-5";
</script>
<template>
<footer :class="footer">
<p :class="text">Follow MarkasUI</p>
<div :class="row">
<Magnet :strength="0.5" :radius="32"
><a href="#" aria-label="Email" :class="link"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
:class="icon"
>
<path d="M4 6h16v12H4z"></path>
<path d="m4 7 8 6 8-6"></path></svg></a
></Magnet>
<Magnet :strength="0.5" :radius="32"
><a href="#" aria-label="Community chat" :class="link"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
:class="icon"
>
<path d="M4 5h16v11H9l-5 4V5Z"></path></svg></a
></Magnet>
<Magnet :strength="0.5" :radius="32"
><a href="#" aria-label="Videos" :class="link"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
:class="icon"
>
<path d="M4 6h16v12H4z"></path>
<path d="m10 9 5 3-5 3V9Z"></path></svg></a
></Magnet>
<Magnet :strength="0.5" :radius="32"
><a href="#" aria-label="Website" :class="link"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
:class="icon"
>
<path d="M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Z"></path>
<path d="M3 12h18"></path>
<path
d="M12 3c2.5 2.7 3.5 5.7 3.5 9s-1 6.3-3.5 9c-2.5-2.7-3.5-5.7-3.5-9s1-6.3 3.5-9Z"
></path></svg></a
></Magnet>
</div>
</footer>
</template><script lang="ts">
import Magnet from "$lib/components/markas/Magnet.svelte";
const footer =
"flex items-center justify-between gap-4 rounded-xl border border-line bg-surface px-5 py-4";
const text = "text-sm text-muted";
const row = "flex items-center gap-3";
const link =
"grid size-10 place-items-center rounded-full border border-line bg-surface text-ink transition-colors duration-150 ease-out hover:bg-subtle focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const icon = "size-5";
</script>
<footer class={footer}>
<p class={text}>Follow MarkasUI</p>
<div class={row}>
<Magnet strength={0.5} radius={32}
><!-- svelte-ignore a11y_invalid_attribute -->
<a href="#" aria-label="Email" class={link}
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class={icon}
>
<path d="M4 6h16v12H4z"></path>
<path d="m4 7 8 6 8-6"></path></svg
></a
></Magnet
>
<Magnet strength={0.5} radius={32}
><!-- svelte-ignore a11y_invalid_attribute -->
<a href="#" aria-label="Community chat" class={link}
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class={icon}
>
<path d="M4 5h16v11H9l-5 4V5Z"></path></svg
></a
></Magnet
>
<Magnet strength={0.5} radius={32}
><!-- svelte-ignore a11y_invalid_attribute -->
<a href="#" aria-label="Videos" class={link}
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class={icon}
>
<path d="M4 6h16v12H4z"></path>
<path d="m10 9 5 3-5 3V9Z"></path></svg
></a
></Magnet
>
<Magnet strength={0.5} radius={32}
><!-- svelte-ignore a11y_invalid_attribute -->
<a href="#" aria-label="Website" class={link}
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class={icon}
>
<path d="M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Z"></path>
<path d="M3 12h18"></path>
<path
d="M12 3c2.5 2.7 3.5 5.7 3.5 9s-1 6.3-3.5 9c-2.5-2.7-3.5-5.7-3.5-9s1-6.3 3.5-9Z"
></path></svg
></a
></Magnet
>
</div>
</footer>---
import Magnet from "@/components/markas/Magnet.astro";
const footer =
"flex items-center justify-between gap-4 rounded-xl border border-line bg-surface px-5 py-4";
const text = "text-sm text-muted";
const row = "flex items-center gap-3";
const link =
"grid size-10 place-items-center rounded-full border border-line bg-surface text-ink transition-colors duration-150 ease-out hover:bg-subtle focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const icon = "size-5";
---
<footer class={footer}>
<p class={text}>Follow MarkasUI</p>
<div class={row}>
<Magnet strength={0.5} radius={32}><a href="#" aria-label="Email" class={link}><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class={icon}>
<path d="M4 6h16v12H4z"></path>
<path d="m4 7 8 6 8-6"></path></svg></a></Magnet>
<Magnet strength={0.5} radius={32}><a href="#" aria-label="Community chat" class={link}><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class={icon}>
<path d="M4 5h16v11H9l-5 4V5Z"></path></svg></a></Magnet>
<Magnet strength={0.5} radius={32}><a href="#" aria-label="Videos" class={link}><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class={icon}>
<path d="M4 6h16v12H4z"></path>
<path d="m10 9 5 3-5 3V9Z"></path></svg></a></Magnet>
<Magnet strength={0.5} radius={32}><a href="#" aria-label="Website" class={link}><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class={icon}>
<path d="M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Z"></path>
<path d="M3 12h18"></path>
<path d="M12 3c2.5 2.7 3.5 5.7 3.5 9s-1 6.3-3.5 9c-2.5-2.7-3.5-5.7-3.5-9s1-6.3 3.5-9Z"></path></svg></a></Magnet>
</div>
</footer>import { Component } from "@angular/core";
import { Magnet } from "./markas/magnet.component";
@Component({
selector: "markas-example",
imports: [Magnet],
template: `
<footer [class]="footer">
<p [class]="text">Follow MarkasUI</p>
<div [class]="row">
<markas-magnet [strength]="0.5" [radius]="32"
><a href="#" aria-label="Email" [class]="link"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
[class]="icon"
>
<path d="M4 6h16v12H4z"></path>
<path d="m4 7 8 6 8-6"></path></svg></a
></markas-magnet>
<markas-magnet [strength]="0.5" [radius]="32"
><a href="#" aria-label="Community chat" [class]="link"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
[class]="icon"
>
<path d="M4 5h16v11H9l-5 4V5Z"></path></svg></a
></markas-magnet>
<markas-magnet [strength]="0.5" [radius]="32"
><a href="#" aria-label="Videos" [class]="link"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
[class]="icon"
>
<path d="M4 6h16v12H4z"></path>
<path d="m10 9 5 3-5 3V9Z"></path></svg></a
></markas-magnet>
<markas-magnet [strength]="0.5" [radius]="32"
><a href="#" aria-label="Website" [class]="link"
><svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
[class]="icon"
>
<path d="M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Z"></path>
<path d="M3 12h18"></path>
<path
d="M12 3c2.5 2.7 3.5 5.7 3.5 9s-1 6.3-3.5 9c-2.5-2.7-3.5-5.7-3.5-9s1-6.3 3.5-9Z"
></path></svg></a
></markas-magnet>
</div>
</footer>
`,
})
export default class MagnetSocialExample {
protected readonly footer =
"flex items-center justify-between gap-4 rounded-xl border border-line bg-surface px-5 py-4";
protected readonly text = "text-sm text-muted";
protected readonly row = "flex items-center gap-3";
protected readonly link =
"grid size-10 place-items-center rounded-full border border-line bg-surface text-ink transition-colors duration-150 ease-out hover:bg-subtle focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
protected readonly icon = "size-5";
}Navbar links with a stronger main action
@php
$nav = 'flex items-center justify-between rounded-xl border border-line bg-surface px-4 py-3 max-sm:px-3';
$brand = 'text-base font-semibold text-ink';
$links = 'flex items-center gap-1';
$link = 'block rounded-md px-3 py-1.5 text-sm font-medium text-muted max-sm:px-2 transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none';
@endphp
<nav aria-label="Main" class="{{ $nav }}">
<a href="#" class="{{ $brand }}">Warung</a>
<div class="{{ $links }}">
<x-markas.magnet :strength="0.25" :radius="20"><a href="#" class="{{ $link }}">Menu</a></x-markas.magnet>
<x-markas.magnet :strength="0.25" :radius="20"><a href="#" class="{{ $link }}">Outlets</a></x-markas.magnet>
<x-markas.magnet :strength="0.25" :radius="20"><a href="#" class="{{ $link }}">About</a></x-markas.magnet>
<x-markas.magnet :strength="0.4" :radius="40"><x-markas.button size="sm">Order</x-markas.button></x-markas.magnet>
</div>
</nav>import { Magnet } from "@/components/markas/Magnet";
import { Button } from "@/components/markas/Button";
const nav =
"flex items-center justify-between rounded-xl border border-line bg-surface px-4 py-3 max-sm:px-3";
const brand = "text-base font-semibold text-ink";
const links = "flex items-center gap-1";
const link =
"block rounded-md px-3 py-1.5 text-sm font-medium text-muted max-sm:px-2 transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
export default function Example() {
return (
<nav aria-label="Main" className={nav}>
<a href="#" className={brand}>
Warung
</a>
<div className={links}>
<Magnet strength={0.25} radius={20}>
<a href="#" className={link}>
Menu
</a>
</Magnet>
<Magnet strength={0.25} radius={20}>
<a href="#" className={link}>
Outlets
</a>
</Magnet>
<Magnet strength={0.25} radius={20}>
<a href="#" className={link}>
About
</a>
</Magnet>
<Magnet strength={0.4} radius={40}>
<Button size="sm">Order</Button>
</Magnet>
</div>
</nav>
);
}<script setup lang="ts">
import Magnet from "@/components/markas/Magnet.vue";
import Button from "@/components/markas/Button.vue";
const nav =
"flex items-center justify-between rounded-xl border border-line bg-surface px-4 py-3 max-sm:px-3";
const brand = "text-base font-semibold text-ink";
const links = "flex items-center gap-1";
const link =
"block rounded-md px-3 py-1.5 text-sm font-medium text-muted max-sm:px-2 transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
</script>
<template>
<nav aria-label="Main" :class="nav">
<a href="#" :class="brand">Warung</a>
<div :class="links">
<Magnet :strength="0.25" :radius="20"
><a href="#" :class="link">Menu</a></Magnet
>
<Magnet :strength="0.25" :radius="20"
><a href="#" :class="link">Outlets</a></Magnet
>
<Magnet :strength="0.25" :radius="20"
><a href="#" :class="link">About</a></Magnet
>
<Magnet :strength="0.4" :radius="40"
><Button size="sm">Order</Button></Magnet
>
</div>
</nav>
</template><script lang="ts">
import Magnet from "$lib/components/markas/Magnet.svelte";
import Button from "$lib/components/markas/Button.svelte";
const nav =
"flex items-center justify-between rounded-xl border border-line bg-surface px-4 py-3 max-sm:px-3";
const brand = "text-base font-semibold text-ink";
const links = "flex items-center gap-1";
const link =
"block rounded-md px-3 py-1.5 text-sm font-medium text-muted max-sm:px-2 transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
</script>
<nav aria-label="Main" class={nav}>
<!-- svelte-ignore a11y_invalid_attribute -->
<a href="#" class={brand}>Warung</a>
<div class={links}>
<Magnet strength={0.25} radius={20}
><!-- svelte-ignore a11y_invalid_attribute -->
<a href="#" class={link}>Menu</a></Magnet
>
<Magnet strength={0.25} radius={20}
><!-- svelte-ignore a11y_invalid_attribute -->
<a href="#" class={link}>Outlets</a></Magnet
>
<Magnet strength={0.25} radius={20}
><!-- svelte-ignore a11y_invalid_attribute -->
<a href="#" class={link}>About</a></Magnet
>
<Magnet strength={0.4} radius={40}><Button size="sm">Order</Button></Magnet>
</div>
</nav>---
import Magnet from "@/components/markas/Magnet.astro";
import Button from "@/components/markas/Button.astro";
const nav =
"flex items-center justify-between rounded-xl border border-line bg-surface px-4 py-3 max-sm:px-3";
const brand = "text-base font-semibold text-ink";
const links = "flex items-center gap-1";
const link =
"block rounded-md px-3 py-1.5 text-sm font-medium text-muted max-sm:px-2 transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
---
<nav aria-label="Main" class={nav}>
<a href="#" class={brand}>Warung</a>
<div class={links}>
<Magnet strength={0.25} radius={20}><a href="#" class={link}>Menu</a></Magnet>
<Magnet strength={0.25} radius={20}><a href="#" class={link}>Outlets</a></Magnet>
<Magnet strength={0.25} radius={20}><a href="#" class={link}>About</a></Magnet>
<Magnet strength={0.4} radius={40}><Button size="sm">Order</Button></Magnet>
</div>
</nav>import { Component } from "@angular/core";
import { Magnet } from "./markas/magnet.component";
import { Button } from "./markas/button.component";
@Component({
selector: "markas-example",
imports: [Magnet, Button],
template: `
<nav aria-label="Main" [class]="nav">
<a href="#" [class]="brand">Warung</a>
<div [class]="links">
<markas-magnet [strength]="0.25" [radius]="20"
><a href="#" [class]="link">Menu</a></markas-magnet
>
<markas-magnet [strength]="0.25" [radius]="20"
><a href="#" [class]="link">Outlets</a></markas-magnet
>
<markas-magnet [strength]="0.25" [radius]="20"
><a href="#" [class]="link">About</a></markas-magnet
>
<markas-magnet [strength]="0.4" [radius]="40"
><button markas-button size="sm">Order</button></markas-magnet
>
</div>
</nav>
`,
})
export default class MagnetNavbarExample {
protected readonly nav =
"flex items-center justify-between rounded-xl border border-line bg-surface px-4 py-3 max-sm:px-3";
protected readonly brand = "text-base font-semibold text-ink";
protected readonly links = "flex items-center gap-1";
protected readonly link =
"block rounded-md px-3 py-1.5 text-sm font-medium text-muted max-sm:px-2 transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
}Voucher that dodges the pointer
Catch the voucher if you can
Rp 50.000 off
Catch the voucher if you can
Rp 50.000 off
Catch the voucher if you can
Rp 50.000 off
Catch the voucher if you can
Rp 50.000 off
Catch the voucher if you can
Rp 50.000 off
@php
$card = 'grid h-56 place-items-center rounded-xl border border-dashed border-line bg-subtle text-center';
$hint = 'text-sm text-muted';
$voucher = 'mx-auto mt-3 rounded-lg border-2 border-dashed border-success bg-surface px-4 py-2 text-lg font-semibold text-ink tabular-nums';
@endphp
<div class="{{ $card }}">
<div>
<p class="{{ $hint }}">Catch the voucher if you can</p>
<x-markas.magnet mode="repel" :strength="0.6" :radius="70" :class="$voucher">Rp 50.000 off</x-markas.magnet>
</div>
</div>import { Magnet } from "@/components/markas/Magnet";
const card =
"grid h-56 place-items-center rounded-xl border border-dashed border-line bg-subtle text-center";
const hint = "text-sm text-muted";
const voucher =
"mx-auto mt-3 rounded-lg border-2 border-dashed border-success bg-surface px-4 py-2 text-lg font-semibold text-ink tabular-nums";
export default function Example() {
return (
<div className={card}>
<div>
<p className={hint}>Catch the voucher if you can</p>
<Magnet mode="repel" strength={0.6} radius={70} className={voucher}>
Rp 50.000 off
</Magnet>
</div>
</div>
);
}<script setup lang="ts">
import Magnet from "@/components/markas/Magnet.vue";
const card =
"grid h-56 place-items-center rounded-xl border border-dashed border-line bg-subtle text-center";
const hint = "text-sm text-muted";
const voucher =
"mx-auto mt-3 rounded-lg border-2 border-dashed border-success bg-surface px-4 py-2 text-lg font-semibold text-ink tabular-nums";
</script>
<template>
<div :class="card">
<div>
<p :class="hint">Catch the voucher if you can</p>
<Magnet mode="repel" :strength="0.6" :radius="70" :class="voucher"
>Rp 50.000 off</Magnet
>
</div>
</div>
</template><script lang="ts">
import Magnet from "$lib/components/markas/Magnet.svelte";
const card =
"grid h-56 place-items-center rounded-xl border border-dashed border-line bg-subtle text-center";
const hint = "text-sm text-muted";
const voucher =
"mx-auto mt-3 rounded-lg border-2 border-dashed border-success bg-surface px-4 py-2 text-lg font-semibold text-ink tabular-nums";
</script>
<div class={card}>
<div>
<p class={hint}>Catch the voucher if you can</p>
<Magnet mode="repel" strength={0.6} radius={70} class={voucher}
>Rp 50.000 off</Magnet
>
</div>
</div>---
import Magnet from "@/components/markas/Magnet.astro";
const card =
"grid h-56 place-items-center rounded-xl border border-dashed border-line bg-subtle text-center";
const hint = "text-sm text-muted";
const voucher =
"mx-auto mt-3 rounded-lg border-2 border-dashed border-success bg-surface px-4 py-2 text-lg font-semibold text-ink tabular-nums";
---
<div class={card}>
<div>
<p class={hint}>Catch the voucher if you can</p>
<Magnet mode="repel" strength={0.6} radius={70} class={voucher}>Rp 50.000 off</Magnet>
</div>
</div>import { Component } from "@angular/core";
import { Magnet } from "./markas/magnet.component";
@Component({
selector: "markas-example",
imports: [Magnet],
template: `
<div [class]="card">
<div>
<p [class]="hint">Catch the voucher if you can</p>
<markas-magnet
mode="repel"
[strength]="0.6"
[radius]="70"
[class]="voucher"
>Rp 50.000 off</markas-magnet
>
</div>
</div>
`,
})
export default class MagnetRepelExample {
protected readonly card =
"grid h-56 place-items-center rounded-xl border border-dashed border-line bg-subtle text-center";
protected readonly hint = "text-sm text-muted";
protected readonly voucher =
"mx-auto mt-3 rounded-lg border-2 border-dashed border-success bg-surface px-4 py-2 text-lg font-semibold text-ink tabular-nums";
}Installation
npx markasui@latest add magnetpnpm dlx markasui@latest add magnetyarn dlx markasui@latest add magnetbunx --bun markasui@latest add magnet- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Install the dependency:
npm install alpinejs - Copy the component source below to
resources/views/components/markas/magnet.blade.php.
resources/views/components/markas/magnet.blade.php
@props([
'strength' => 0.35,
'radius' => 80,
'mode' => 'attract',
])
@php
$base = 'w-fit transition-[translate,scale] duration-500 ease-[cubic-bezier(0.34,1.56,0.64,1)] has-[:focus-visible]:scale-105 data-[state=active]:duration-150 data-[state=active]:ease-out motion-reduce:transition-none [&_[data-depth]]:inline-block [&_[data-depth]]:transition-[translate] [&_[data-depth]]:duration-500 [&_[data-depth]]:ease-[cubic-bezier(0.34,1.56,0.64,1)] data-[state=active]:[&_[data-depth]]:duration-150 motion-reduce:[&_[data-depth]]:transition-none';
@endphp
{{-- Pulls the element toward (or pushes it away from) a mouse within radius px of its edges, then lets it spring back. Children with data-depth="0.5" travel that much further. Listens only while on screen. --}}
<div
wire:ignore.self
x-data="{
stop: null,
init() {
if (matchMedia('(prefers-reduced-motion: reduce)').matches) return;
const o = { strength: @js((float) $strength), radius: @js((float) $radius), mode: @js($mode) };
const root = this.$el;
let x = 0;
let y = 0;
const set = (nx, ny) => {
x = nx;
y = ny;
if (x || y) root.dataset.state = 'active';
else delete root.dataset.state;
root.style.translate = x || y ? `${x}px ${y}px` : '';
for (const layer of root.querySelectorAll('[data-depth]')) {
const depth = Number(layer.dataset.depth) || 0;
layer.style.translate = x || y ? `${x * depth}px ${y * depth}px` : '';
}
};
const move = (e) => {
if (e.pointerType === 'touch') return;
const r = root.getBoundingClientRect();
const dx = e.clientX - (r.left + r.width / 2 - x);
const dy = e.clientY - (r.top + r.height / 2 - y);
const gap = Math.hypot(Math.max(Math.abs(dx) - r.width / 2, 0), Math.max(Math.abs(dy) - r.height / 2, 0));
const near = Math.max(1 - gap / o.radius, 0);
if (o.mode === 'repel') {
const push = (near * o.radius * o.strength) / (Math.hypot(dx, dy) || 1);
return set(-dx * push, -dy * push);
}
set(dx * near * o.strength, dy * near * o.strength);
};
const reset = () => set(0, 0);
const html = document.documentElement;
const listen = (on) => {
if (on) {
document.addEventListener('pointermove', move);
html.addEventListener('pointerleave', reset);
return;
}
document.removeEventListener('pointermove', move);
html.removeEventListener('pointerleave', reset);
reset();
};
const observer = typeof IntersectionObserver === 'undefined' ? null : new IntersectionObserver(([entry]) => listen(!! entry?.isIntersecting));
if (observer) observer.observe(root);
else listen(true);
this.stop = () => {
observer?.disconnect();
listen(false);
};
},
destroy() {
this.stop?.();
},
}"
{{ $attributes->class([$base]) }}
>
{{ $slot }}
</div>- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/Magnet.tsx.
src/components/markas/Magnet.tsx
"use client";
import { useEffect, useRef, type ComponentProps } from "react";
const base =
"w-fit transition-[translate,scale] duration-500 ease-[cubic-bezier(0.34,1.56,0.64,1)] has-[:focus-visible]:scale-105 data-[state=active]:duration-150 data-[state=active]:ease-out motion-reduce:transition-none [&_[data-depth]]:inline-block [&_[data-depth]]:transition-[translate] [&_[data-depth]]:duration-500 [&_[data-depth]]:ease-[cubic-bezier(0.34,1.56,0.64,1)] data-[state=active]:[&_[data-depth]]:duration-150 motion-reduce:[&_[data-depth]]:transition-none";
export type MagnetMode = "attract" | "repel";
type Options = { strength: number; radius: number; mode: MagnetMode };
const cx = (...c: (string | false | null | undefined)[]) =>
c.filter(Boolean).join(" ");
const still = () =>
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
/**
* Pull the element toward (or push it away from) a mouse within `radius` px of its edges, then let
* it spring back. Children with data-depth="0.5" travel that much further. Listens only while on screen.
*/
export function startMagnet(root: HTMLElement, o: Options) {
let x = 0;
let y = 0;
const set = (nx: number, ny: number) => {
x = nx;
y = ny;
if (x || y) root.dataset.state = "active";
else delete root.dataset.state;
root.style.translate = x || y ? `${x}px ${y}px` : "";
for (const layer of root.querySelectorAll<HTMLElement>("[data-depth]")) {
const depth = Number(layer.dataset.depth) || 0;
layer.style.translate = x || y ? `${x * depth}px ${y * depth}px` : "";
}
};
const move = (e: PointerEvent) => {
if (e.pointerType === "touch") return;
const r = root.getBoundingClientRect();
const dx = e.clientX - (r.left + r.width / 2 - x);
const dy = e.clientY - (r.top + r.height / 2 - y);
const gap = Math.hypot(
Math.max(Math.abs(dx) - r.width / 2, 0),
Math.max(Math.abs(dy) - r.height / 2, 0),
);
const near = Math.max(1 - gap / o.radius, 0);
if (o.mode === "repel") {
const push = (near * o.radius * o.strength) / (Math.hypot(dx, dy) || 1);
return set(-dx * push, -dy * push);
}
set(dx * near * o.strength, dy * near * o.strength);
};
const reset = () => set(0, 0);
const html = document.documentElement;
const listen = (on: boolean) => {
if (on) {
document.addEventListener("pointermove", move);
html.addEventListener("pointerleave", reset);
return;
}
document.removeEventListener("pointermove", move);
html.removeEventListener("pointerleave", reset);
reset();
};
const observer =
typeof IntersectionObserver === "undefined"
? undefined
: new IntersectionObserver(([entry]) => listen(!!entry?.isIntersecting));
if (observer) observer.observe(root);
else listen(true);
return () => {
observer?.disconnect();
listen(false);
};
}
export type MagnetProps = ComponentProps<"div"> & {
/** Share of the pointer distance the element follows, 0 to 1. For repel, how far it is pushed. */
strength?: number;
/** Distance in px beyond the element edges where the pull starts. */
radius?: number;
/** Move toward the pointer or away from it. */
mode?: MagnetMode;
};
export function Magnet({
strength = 0.35,
radius = 80,
mode = "attract",
className,
children,
...props
}: MagnetProps) {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!ref.current || still()) return;
return startMagnet(ref.current, { strength, radius, mode });
}, [strength, radius, mode]);
return (
<div ref={ref} {...props} className={cx(base, className)}>
{children}
</div>
);
}- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/Magnet.vue.
src/components/markas/Magnet.vue
<script setup lang="ts">
import { onBeforeUnmount, onMounted, useTemplateRef, watch } from "vue";
const props = withDefaults(
defineProps<{
/** Share of the pointer distance the element follows, 0 to 1. For repel, how far it is pushed. */
strength?: number;
/** Distance in px beyond the element edges where the pull starts. */
radius?: number;
/** Move toward the pointer or away from it. */
mode?: "attract" | "repel";
}>(),
{ strength: 0.35, radius: 80, mode: "attract" },
);
const base =
"w-fit transition-[translate,scale] duration-500 ease-[cubic-bezier(0.34,1.56,0.64,1)] has-[:focus-visible]:scale-105 data-[state=active]:duration-150 data-[state=active]:ease-out motion-reduce:transition-none [&_[data-depth]]:inline-block [&_[data-depth]]:transition-[translate] [&_[data-depth]]:duration-500 [&_[data-depth]]:ease-[cubic-bezier(0.34,1.56,0.64,1)] data-[state=active]:[&_[data-depth]]:duration-150 motion-reduce:[&_[data-depth]]:transition-none";
type MagnetMode = "attract" | "repel";
type Options = { strength: number; radius: number; mode: MagnetMode };
const still = () =>
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
/**
* Pull the element toward (or push it away from) a mouse within `radius` px of its edges, then let
* it spring back. Children with data-depth="0.5" travel that much further. Listens only while on screen.
*/
function startMagnet(root: HTMLElement, o: Options) {
let x = 0;
let y = 0;
const set = (nx: number, ny: number) => {
x = nx;
y = ny;
if (x || y) root.dataset.state = "active";
else delete root.dataset.state;
root.style.translate = x || y ? `${x}px ${y}px` : "";
for (const layer of root.querySelectorAll<HTMLElement>("[data-depth]")) {
const depth = Number(layer.dataset.depth) || 0;
layer.style.translate = x || y ? `${x * depth}px ${y * depth}px` : "";
}
};
const move = (e: PointerEvent) => {
if (e.pointerType === "touch") return;
const r = root.getBoundingClientRect();
const dx = e.clientX - (r.left + r.width / 2 - x);
const dy = e.clientY - (r.top + r.height / 2 - y);
const gap = Math.hypot(
Math.max(Math.abs(dx) - r.width / 2, 0),
Math.max(Math.abs(dy) - r.height / 2, 0),
);
const near = Math.max(1 - gap / o.radius, 0);
if (o.mode === "repel") {
const push = (near * o.radius * o.strength) / (Math.hypot(dx, dy) || 1);
return set(-dx * push, -dy * push);
}
set(dx * near * o.strength, dy * near * o.strength);
};
const reset = () => set(0, 0);
const html = document.documentElement;
const listen = (on: boolean) => {
if (on) {
document.addEventListener("pointermove", move);
html.addEventListener("pointerleave", reset);
return;
}
document.removeEventListener("pointermove", move);
html.removeEventListener("pointerleave", reset);
reset();
};
const observer =
typeof IntersectionObserver === "undefined"
? undefined
: new IntersectionObserver(([entry]) => listen(!!entry?.isIntersecting));
if (observer) observer.observe(root);
else listen(true);
return () => {
observer?.disconnect();
listen(false);
};
}
const rootEl = useTemplateRef<HTMLDivElement>("rootRef");
let stop = () => {};
function start() {
stop();
stop = () => {};
if (rootEl.value && !still()) stop = startMagnet(rootEl.value, { ...props });
}
onMounted(start);
watch(() => [props.strength, props.radius, props.mode], start, {
flush: "post",
});
onBeforeUnmount(() => stop());
</script>
<template>
<div ref="rootRef" :class="base">
<slot />
</div>
</template>- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/lib/components/markas/Magnet.svelte.
src/lib/components/markas/Magnet.svelte
<script lang="ts">
import type { Snippet } from "svelte";
import type { HTMLAttributes } from "svelte/elements";
const base =
"w-fit transition-[translate,scale] duration-500 ease-[cubic-bezier(0.34,1.56,0.64,1)] has-[:focus-visible]:scale-105 data-[state=active]:duration-150 data-[state=active]:ease-out motion-reduce:transition-none [&_[data-depth]]:inline-block [&_[data-depth]]:transition-[translate] [&_[data-depth]]:duration-500 [&_[data-depth]]:ease-[cubic-bezier(0.34,1.56,0.64,1)] data-[state=active]:[&_[data-depth]]:duration-150 motion-reduce:[&_[data-depth]]:transition-none";
type MagnetMode = "attract" | "repel";
type Options = { strength: number; radius: number; mode: MagnetMode };
const still = () =>
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
/**
* Pull the element toward (or push it away from) a mouse within `radius` px of its edges, then
* let it spring back. Children with data-depth="0.5" travel that much further. Listens only
* while on screen.
*/
function startMagnet(root: HTMLElement, o: Options) {
let x = 0;
let y = 0;
const set = (nx: number, ny: number) => {
x = nx;
y = ny;
if (x || y) root.dataset.state = "active";
else delete root.dataset.state;
root.style.translate = x || y ? `${x}px ${y}px` : "";
for (const layer of root.querySelectorAll<HTMLElement>("[data-depth]")) {
const depth = Number(layer.dataset.depth) || 0;
layer.style.translate = x || y ? `${x * depth}px ${y * depth}px` : "";
}
};
const move = (e: PointerEvent) => {
if (e.pointerType === "touch") return;
const r = root.getBoundingClientRect();
const dx = e.clientX - (r.left + r.width / 2 - x);
const dy = e.clientY - (r.top + r.height / 2 - y);
const gap = Math.hypot(
Math.max(Math.abs(dx) - r.width / 2, 0),
Math.max(Math.abs(dy) - r.height / 2, 0),
);
const near = Math.max(1 - gap / o.radius, 0);
if (o.mode === "repel") {
const push = (near * o.radius * o.strength) / (Math.hypot(dx, dy) || 1);
return set(-dx * push, -dy * push);
}
set(dx * near * o.strength, dy * near * o.strength);
};
const reset = () => set(0, 0);
const html = document.documentElement;
const listen = (on: boolean) => {
if (on) {
document.addEventListener("pointermove", move);
html.addEventListener("pointerleave", reset);
return;
}
document.removeEventListener("pointermove", move);
html.removeEventListener("pointerleave", reset);
reset();
};
const observer =
typeof IntersectionObserver === "undefined"
? undefined
: new IntersectionObserver(([entry]) =>
listen(!!entry?.isIntersecting),
);
if (observer) observer.observe(root);
else listen(true);
return () => {
observer?.disconnect();
listen(false);
};
}
type Props = HTMLAttributes<HTMLDivElement> & {
/** Share of the pointer distance the element follows, 0 to 1. For repel, how far it is pushed. */
strength?: number;
/** Distance in px beyond the element edges where the pull starts. */
radius?: number;
/** Move toward the pointer or away from it. */
mode?: MagnetMode;
children?: Snippet;
};
let {
strength = 0.35,
radius = 80,
mode = "attract",
class: className,
children,
...rest
}: Props = $props();
let root = $state<HTMLDivElement>();
$effect(() => {
const options = { strength, radius, mode };
if (!root || still()) return;
return startMagnet(root, options);
});
</script>
<div bind:this={root} {...rest} class={[base, className]}>
{@render children?.()}
</div>- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/components/markas/Magnet.astro.
src/components/markas/Magnet.astro
---
import type { HTMLAttributes } from "astro/types";
const base =
"w-fit transition-[translate,scale] duration-500 ease-[cubic-bezier(0.34,1.56,0.64,1)] has-[:focus-visible]:scale-105 data-[state=active]:duration-150 data-[state=active]:ease-out motion-reduce:transition-none [&_[data-depth]]:inline-block [&_[data-depth]]:transition-[translate] [&_[data-depth]]:duration-500 [&_[data-depth]]:ease-[cubic-bezier(0.34,1.56,0.64,1)] data-[state=active]:[&_[data-depth]]:duration-150 motion-reduce:[&_[data-depth]]:transition-none";
type Props = HTMLAttributes<"div"> & {
/** Share of the pointer distance the element follows, 0 to 1. For repel, how far it is pushed. */
strength?: number;
/** Distance in px beyond the element edges where the pull starts. */
radius?: number;
/** Move toward the pointer or away from it. */
mode?: "attract" | "repel";
};
const {
strength = 0.35,
radius = 80,
mode = "attract",
class: className,
...rest
} = Astro.props;
---
<markas-magnet
class="contents"
data-strength={Number(strength)}
data-radius={Number(radius)}
data-mode={mode}
>
<div {...rest} class:list={[base, className]}><slot /></div>
</markas-magnet>
<script>
// Per instance: each wrapper listens to the pointer only while its content is on screen.
class Magnet extends HTMLElement {
#stop = () => {};
connectedCallback() {
const root = this.firstElementChild as HTMLElement | null;
if (!root) return;
if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches)
return;
const strength = Number(this.dataset.strength);
const radius = Number(this.dataset.radius);
const repel = this.dataset.mode === "repel";
let x = 0;
let y = 0;
const set = (nx: number, ny: number) => {
x = nx;
y = ny;
if (x || y) root.dataset.state = "active";
else delete root.dataset.state;
root.style.translate = x || y ? `${x}px ${y}px` : "";
for (const layer of root.querySelectorAll<HTMLElement>(
"[data-depth]",
)) {
const depth = Number(layer.dataset.depth) || 0;
layer.style.translate = x || y ? `${x * depth}px ${y * depth}px` : "";
}
};
const move = (e: PointerEvent) => {
if (e.pointerType === "touch") return;
const r = root.getBoundingClientRect();
const dx = e.clientX - (r.left + r.width / 2 - x);
const dy = e.clientY - (r.top + r.height / 2 - y);
const gap = Math.hypot(
Math.max(Math.abs(dx) - r.width / 2, 0),
Math.max(Math.abs(dy) - r.height / 2, 0),
);
const near = Math.max(1 - gap / radius, 0);
if (repel) {
const push = (near * radius * strength) / (Math.hypot(dx, dy) || 1);
return set(-dx * push, -dy * push);
}
set(dx * near * strength, dy * near * strength);
};
const reset = () => set(0, 0);
const html = document.documentElement;
const listen = (on: boolean) => {
if (on) {
document.addEventListener("pointermove", move);
html.addEventListener("pointerleave", reset);
return;
}
document.removeEventListener("pointermove", move);
html.removeEventListener("pointerleave", reset);
reset();
};
const observer =
typeof IntersectionObserver === "undefined"
? undefined
: new IntersectionObserver(([entry]) =>
listen(!!entry?.isIntersecting),
);
if (observer) observer.observe(root);
else listen(true);
this.#stop = () => {
observer?.disconnect();
listen(false);
};
}
disconnectedCallback() {
this.#stop();
}
}
if (!customElements.get("markas-magnet"))
customElements.define("markas-magnet", Magnet);
</script>- Make sure Tailwind CSS v4 and markas.css are installed. See Installation.
- Copy the component source below to
src/app/markas/magnet.component.ts.
src/app/markas/magnet.component.ts
import {
afterRenderEffect,
ChangeDetectionStrategy,
Component,
computed,
type ElementRef,
input,
numberAttribute,
viewChild,
} from "@angular/core";
const base =
"w-fit transition-[translate,scale] duration-500 ease-[cubic-bezier(0.34,1.56,0.64,1)] has-[:focus-visible]:scale-105 data-[state=active]:duration-150 data-[state=active]:ease-out motion-reduce:transition-none [&_[data-depth]]:inline-block [&_[data-depth]]:transition-[translate] [&_[data-depth]]:duration-500 [&_[data-depth]]:ease-[cubic-bezier(0.34,1.56,0.64,1)] data-[state=active]:[&_[data-depth]]:duration-150 motion-reduce:[&_[data-depth]]:transition-none";
export type MagnetMode = "attract" | "repel";
type Options = { strength: number; radius: number; mode: MagnetMode };
const still = () =>
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
/**
* Pull the element toward (or push it away from) a mouse within `radius` px of its edges, then let
* it spring back. Children with data-depth="0.5" travel that much further. Listens only while on screen.
*/
function startMagnet(root: HTMLElement, o: Options) {
let x = 0;
let y = 0;
const set = (nx: number, ny: number) => {
x = nx;
y = ny;
if (x || y) root.dataset.state = "active";
else delete root.dataset.state;
root.style.translate = x || y ? `${x}px ${y}px` : "";
for (const layer of root.querySelectorAll<HTMLElement>("[data-depth]")) {
const depth = Number(layer.dataset.depth) || 0;
layer.style.translate = x || y ? `${x * depth}px ${y * depth}px` : "";
}
};
const move = (e: PointerEvent) => {
if (e.pointerType === "touch") return;
const r = root.getBoundingClientRect();
const dx = e.clientX - (r.left + r.width / 2 - x);
const dy = e.clientY - (r.top + r.height / 2 - y);
const gap = Math.hypot(
Math.max(Math.abs(dx) - r.width / 2, 0),
Math.max(Math.abs(dy) - r.height / 2, 0),
);
const near = Math.max(1 - gap / o.radius, 0);
if (o.mode === "repel") {
const push = (near * o.radius * o.strength) / (Math.hypot(dx, dy) || 1);
return set(-dx * push, -dy * push);
}
set(dx * near * o.strength, dy * near * o.strength);
};
const reset = () => set(0, 0);
const html = document.documentElement;
const listen = (on: boolean) => {
if (on) {
document.addEventListener("pointermove", move);
html.addEventListener("pointerleave", reset);
return;
}
document.removeEventListener("pointermove", move);
html.removeEventListener("pointerleave", reset);
reset();
};
const observer =
typeof IntersectionObserver === "undefined"
? undefined
: new IntersectionObserver(([entry]) => listen(!!entry?.isIntersecting));
if (observer) observer.observe(root);
else listen(true);
return () => {
observer?.disconnect();
listen(false);
};
}
/**
* Wraps one element, usually a button or link. The host stays out of layout; the user's class
* lands on the moving wrapper.
*/
@Component({
selector: "markas-magnet",
changeDetection: ChangeDetectionStrategy.OnPush,
host: { class: "contents" },
template: `<div #rootEl [class]="classes()"><ng-content /></div>`,
})
export class Magnet {
/** Share of the pointer distance the element follows, 0 to 1. For repel, how far it is pushed. */
readonly strength = input(0.35, { transform: numberAttribute });
/** Distance in px beyond the element edges where the pull starts. */
readonly radius = input(80, { transform: numberAttribute });
/** Move toward the pointer or away from it. */
readonly mode = input<MagnetMode>("attract");
readonly userClass = input("", { alias: "class" });
protected readonly classes = computed(() =>
[base, this.userClass()].filter(Boolean).join(" "),
);
private readonly root = viewChild.required<ElementRef<HTMLElement>>("rootEl");
constructor() {
afterRenderEffect((onCleanup) => {
const options = {
strength: this.strength(),
radius: this.radius(),
mode: this.mode(),
};
if (still()) return;
onCleanup(startMagnet(this.root().nativeElement, options));
});
}
}Usage
<x-markas.magnet>
<x-markas.button size="lg">
Get started <span data-depth="0.5">→</span>
</x-markas.button>
</x-markas.magnet>Needs Alpine.js.
import { Magnet } from "@/components/markas/Magnet";
<Magnet strength={0.5} radius={32}>
<a
href="/contact"
aria-label="Email"
className="grid size-10 place-items-center rounded-full border border-line"
>
...
</a>
</Magnet>;<template>
<Magnet
mode="repel"
:strength="0.6"
:radius="70"
class="rounded-lg border-2 border-dashed px-4 py-2"
>
Rp 50.000 off
</Magnet>
</template><script lang="ts">
import Button from "$lib/components/markas/Button.svelte";
import Magnet from "$lib/components/markas/Magnet.svelte";
</script>
<Magnet>
<Button size="lg">Get started <span data-depth="0.5">→</span></Button>
</Magnet>---
import Button from "@/components/markas/Button.astro";
import Magnet from "@/components/markas/Magnet.astro";
---
<Magnet>
<Button size="lg">Get started <span data-depth="0.5">→</span></Button>
</Magnet>A small bundled script runs each magnet through the markas-magnet element.
import { Component } from "@angular/core";
import { Button } from "./markas/button.component";
import { Magnet } from "./markas/magnet.component";
@Component({
selector: "app-cta",
imports: [Button, Magnet],
template: `
<markas-magnet [strength]="0.5" [radius]="32">
<button markas-button size="lg">
Get started <span data-depth="0.5">→</span>
</button>
</markas-magnet>
`,
})
export class Cta {}- Within
radiuspx of its edges the content follows the mouse bystrength(0 to 1) and springs back with a slight overshoot when the mouse moves away. mode="repel"dodges the pointer instead. Keep it for playful, decorative elements.- Inner elements with
data-depth="0.5"travel 50% further, which makes an arrow or icon inside a button feel layered. - The wrapper is
w-fit; attributes andclassgo to it. It listens only while on screen, ignores touch, and stays still with reduced motion.
Props
| Name | Type | Default | Description |
|---|---|---|---|
strength | number | 0.35 | Share of the pointer distance the content follows, from 0 to 1. For repel it sets how far the content is pushed, as a share of radius. |
radius | number | 80 | Distance in px beyond the content edges where the pull starts. It grows smoothly as the pointer comes closer. |
mode | "attract" | "repel" | "attract" | Move toward the pointer, or dodge away from it. |
children / slot | ReactNode / slot | - | The content, usually one button, link, or icon. Inner elements with data-depth="0.5" travel that much further, such as the arrow in a button. |
Accessibility
- The pull is visual only; the button or link inside keeps its role, name, and position in the tab order.
- Keyboard focus inside lifts the content slightly so the focus is easy to follow.
- Touch input is ignored, and with reduced motion the content stays still.
Related components
More markasui components with the same classes in every framework.
- Bottom NavigationMobile app style bottom bar. A pill glides to the selected item, its label rises in, and the bar can shrink to icons while content scrolls.
- Card StackLayered stack of cards. Swipe, arrow keys, or buttons throw the top card away and the next one rises. Loops, with a position indicator.
- Chat MessageMessage in an AI conversation. User bubbles on the right, assistant replies with an avatar, hover actions, a typing indicator, and optional token streaming.
- Currency InputMoney field that formats as you type (Rp 12.500.000), keeps the caret in place, and posts the plain number. Optional quick amount chips.
- DockmacOS style dock. Icons grow smoothly toward the pointer or focus, show a label above, mark open items with a dot, and bounce on launch.
- Drag ElementsArea of cards or stickers you can drag around with light momentum, kept inside its bounds. Arrow keys move a focused item, Escape puts it back.