Pembungkus yang menarik isinya ke arah mouse di dekatnya, atau mendorongnya menjauh, lalu kembali memegas. Lapisan dalam bisa bergerak lebih jauh.
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";
}Contoh
Ikon sosial magnetis di 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";
}Tautan navbar dengan aksi utama yang lebih kuat
@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 yang menghindari kursor
Catch the voucher if you can
Catch the voucher if you can
Catch the voucher if you can
Catch the voucher if you can
Catch the voucher if you can
@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";
}Instalasi
npx markasui@latest add magnetpnpm dlx markasui@latest add magnetyarn dlx markasui@latest add magnetbunx --bun markasui@latest add magnet- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Pasang dependensi:
npm install alpinejs - Salin source komponen di bawah ini ke
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>- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
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>
);
}- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
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>- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
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>- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
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>- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
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));
});
}
}Pemakaian
<x-markas.magnet>
<x-markas.button size="lg">
Mulai sekarang <span data-depth="0.5">→</span>
</x-markas.button>
</x-markas.magnet>Butuh Alpine.js.
import { Magnet } from "@/components/markas/Magnet";
<Magnet strength={0.5} radius={32}>
<a
href="/kontak"
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"
>
Potongan Rp 50.000
</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>Satu script kecil yang dibundel menjalankan setiap magnet lewat elemen markas-magnet.
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 {}- Dalam jarak
radiuspx dari tepinya, isi mengikuti mouse sebesarstrength(0 sampai 1) dan kembali memegas dengan sedikit lewat saat mouse menjauh. mode="repel"justru menghindari kursor. Pakai hanya untuk elemen dekoratif yang jenaka.- Elemen di dalam dengan
data-depth="0.5"bergerak 50% lebih jauh, sehingga panah atau ikon di dalam tombol terasa berlapis. - Pembungkusnya
w-fit; atribut danclassjatuh ke sana. Ia hanya mendengarkan saat terlihat di layar, mengabaikan sentuhan, dan diam dengan reduced motion.
Props
| Nama | Tipe | Default | Keterangan |
|---|---|---|---|
strength | number | 0.35 | Bagian jarak kursor yang diikuti isi, dari 0 sampai 1. Untuk repel menentukan seberapa jauh isi didorong, sebagai bagian dari radius. |
radius | number | 80 | Jarak dalam px di luar tepi isi tempat tarikan mulai. Tarikan membesar halus saat kursor mendekat. |
mode | "attract" | "repel" | "attract" | Bergerak ke arah kursor, atau menghindar darinya. |
children / slot | ReactNode / slot | - | Isinya, biasanya satu tombol, tautan, atau ikon. Elemen di dalam dengan data-depth="0.5" bergerak lebih jauh sebanyak itu, misalnya panah di tombol. |
Aksesibilitas
- Tarikan hanya visual; tombol atau tautan di dalam tetap punya role, nama, dan urutan tab yang sama.
- Fokus keyboard di dalam membuat isi sedikit terangkat sehingga fokus mudah diikuti.
- Input sentuh diabaikan, dan dengan reduced motion isi tetap diam.
Komponen terkait
Komponen markasui lain dengan class yang sama di setiap framework.
- Bottom NavigationBar bawah gaya aplikasi mobile. Pill meluncur ke item terpilih, labelnya muncul, dan bar bisa menciut jadi ikon saat konten discroll.
- Card StackTumpukan kartu bertingkat. Geser, tombol panah, atau tombol navigasi membuang kartu teratas lalu kartu berikutnya naik. Berputar, dengan indikator posisi.
- Chat MessagePesan dalam percakapan AI. Gelembung user di kanan, balasan asisten dengan avatar, aksi saat hover, indikator mengetik, dan efek streaming token opsional.
- Currency InputKolom uang yang memformat saat mengetik (Rp 12.500.000), menjaga posisi kursor, dan mengirim angka murni. Opsional chip nominal cepat.
- DockDock ala macOS. Ikon membesar halus mengikuti kursor atau fokus, label muncul di atas, item terbuka bertitik, dan memantul saat dibuka.
- Drag ElementsArea kartu atau stiker yang bisa diseret dengan momentum ringan di dalam batasnya. Panah menggeser item yang fokus, Escape mengembalikannya.