# Underline Link

A link with an animated underline in five styles, including a hand drawn scribble. Shows on hover, on keyboard focus, and for the current page.

- Category: Animations
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/underline-link.json
- CLI: `npx markasui@latest add underline-link`

## When to use

Navbar, footer, and inline article links that need a clear, designed hover and focus state.

- The line uses the text color; set the color on the link
- Use active for the current page in a navbar
- Use scribble sparingly, for one or two emphasized links
- No JavaScript; Blade does not need Alpine.js

## Code

### Blade

Save at `resources/views/components/markas/underline-link.blade.php`. No extra dependencies.

```blade
@props([
    'href' => '#',
    'variant' => 'grow-center',
    'active' => false,
])

@php
    $base = 'group relative inline-block rounded-sm focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-primary';
    $lines = [
        'grow-center' => [
            'pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100',
        ],
        'slide' => [
            'pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100',
        ],
        'comes-in-goes-out' => [
            'pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right scale-x-0 group-hover:origin-left group-hover:scale-x-100 group-focus-visible:origin-left group-focus-visible:scale-x-100 group-aria-[current=page]:origin-left group-aria-[current=page]:scale-x-100',
        ],
        'goes-out-comes-in' => [
            'pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right delay-300 group-hover:scale-x-0 group-hover:delay-0 group-focus-visible:scale-x-0 group-focus-visible:delay-0 motion-reduce:delay-0',
            'pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-hover:delay-300 group-focus-visible:scale-x-100 group-focus-visible:delay-300 motion-reduce:delay-0',
        ],
        'scribble' => [],
    ];
    $scribble = 'pointer-events-none absolute inset-x-0 -bottom-2 h-2.5 w-full overflow-visible';
    $scribblePath = '[stroke-dasharray:1_2] [stroke-dashoffset:1] transition-[stroke-dashoffset] duration-500 ease-out group-hover:[stroke-dashoffset:0] group-focus-visible:[stroke-dashoffset:0] group-aria-[current=page]:[stroke-dashoffset:0] motion-reduce:transition-none';

    // Href berskema selain http, https, mailto, dan tel (misalnya javascript:) diganti "#".
    $safeHref = function (string $href): string {
        $value = ltrim(str_replace(["\t", "\n", "\r"], '', $href), "\x00..\x20");
        $hasScheme = preg_match('/^[a-z][a-z0-9+.\-]*:/i', $value) === 1;

        return $hasScheme && preg_match('/^(https?|mailto|tel):/i', $value) !== 1 ? '#' : $href;
    };
@endphp

<a {{ $attributes->merge(['href' => $safeHref((string) $href), 'aria-current' => $active ? 'page' : null])->class([$base]) }}>{{ $slot }}@foreach ($lines[$variant] as $line)<span aria-hidden="true" class="{{ $line }}"></span>@endforeach @if ($variant === 'scribble')<svg aria-hidden="true" viewBox="0 0 100 10" preserveAspectRatio="none" fill="none" class="{{ $scribble }}"><path d="M1 6.5C14 3.5 26 8.5 40 5.5S66 2.5 78 5.5 93 7 99 4" pathLength="1" stroke="currentColor" stroke-width="2" stroke-linecap="round" vector-effect="non-scaling-stroke" class="{{ $scribblePath }}" /></svg>@endif</a>
```

```blade
<nav class="flex gap-5 text-sm font-medium">
    <x-markas.underline-link href="/" active>Home</x-markas.underline-link>
    <x-markas.underline-link href="/docs" variant="comes-in-goes-out">Docs</x-markas.underline-link>
</nav>
<x-markas.underline-link href="/guide" variant="scribble">Migration guide</x-markas.underline-link>
```

### React

Save at `src/components/markas/UnderlineLink.tsx`. No extra dependencies.

```tsx
import type { ComponentProps } from "react";

const base =
  "group relative inline-block rounded-sm focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-primary";
const lines = {
  "grow-center": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
  ],
  slide: [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
  ],
  "comes-in-goes-out": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right scale-x-0 group-hover:origin-left group-hover:scale-x-100 group-focus-visible:origin-left group-focus-visible:scale-x-100 group-aria-[current=page]:origin-left group-aria-[current=page]:scale-x-100",
  ],
  "goes-out-comes-in": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right delay-300 group-hover:scale-x-0 group-hover:delay-0 group-focus-visible:scale-x-0 group-focus-visible:delay-0 motion-reduce:delay-0",
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-hover:delay-300 group-focus-visible:scale-x-100 group-focus-visible:delay-300 motion-reduce:delay-0",
  ],
  scribble: [],
};
const scribble =
  "pointer-events-none absolute inset-x-0 -bottom-2 h-2.5 w-full overflow-visible";
const scribblePath =
  "[stroke-dasharray:1_2] [stroke-dashoffset:1] transition-[stroke-dashoffset] duration-500 ease-out group-hover:[stroke-dashoffset:0] group-focus-visible:[stroke-dashoffset:0] group-aria-[current=page]:[stroke-dashoffset:0] motion-reduce:transition-none";

const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
const SAFE_SCHEME = /^(https?|mailto|tel):/i;

const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

/** Href berskema selain http, https, mailto, dan tel (misalnya javascript:) diganti "#". */
const safeHref = (href: string) => {
  const cleaned = href.replace(/[\t\n\r]/g, "");
  const start = [...cleaned].findIndex((char) => char > " ");
  const value = cleaned.slice(Math.max(start, 0));
  return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
};

export type UnderlineLinkProps = Omit<ComponentProps<"a">, "href"> & {
  href: string;
  variant?: keyof typeof lines;
  /** Tandai link halaman aktif (aria-current="page"); garisnya tetap tampil. */
  active?: boolean;
};

export function UnderlineLink({
  href,
  variant = "grow-center",
  active = false,
  className,
  children,
  ...props
}: UnderlineLinkProps) {
  return (
    <a
      href={safeHref(href)}
      aria-current={active ? "page" : undefined}
      {...props}
      className={cx(base, className)}
    >
      {children}
      {lines[variant].map((line, i) => (
        <span key={i} aria-hidden="true" className={line} />
      ))}
      {variant === "scribble" && (
        <svg
          aria-hidden="true"
          viewBox="0 0 100 10"
          preserveAspectRatio="none"
          fill="none"
          className={scribble}
        >
          <path
            d="M1 6.5C14 3.5 26 8.5 40 5.5S66 2.5 78 5.5 93 7 99 4"
            pathLength={1}
            stroke="currentColor"
            strokeWidth={2}
            strokeLinecap="round"
            vectorEffect="non-scaling-stroke"
            className={scribblePath}
          />
        </svg>
      )}
    </a>
  );
}
```

```tsx
import { UnderlineLink } from "@/components/markas/UnderlineLink";

<nav className="flex gap-5 text-sm font-medium">
  <UnderlineLink href="/" active>Home</UnderlineLink>
  <UnderlineLink href="/docs" variant="comes-in-goes-out">Docs</UnderlineLink>
</nav>
<UnderlineLink href="/guide" variant="scribble">Migration guide</UnderlineLink>
```

### Vue

Save at `src/components/markas/UnderlineLink.vue`. No extra dependencies.

```vue
<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    href: string;
    variant?:
      | "grow-center"
      | "slide"
      | "comes-in-goes-out"
      | "goes-out-comes-in"
      | "scribble";
    /** Tandai link halaman aktif (aria-current="page"); garisnya tetap tampil. */
    active?: boolean;
  }>(),
  { variant: "grow-center", active: false },
);

const base =
  "group relative inline-block rounded-sm focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-primary";
const lines = {
  "grow-center": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
  ],
  slide: [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
  ],
  "comes-in-goes-out": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right scale-x-0 group-hover:origin-left group-hover:scale-x-100 group-focus-visible:origin-left group-focus-visible:scale-x-100 group-aria-[current=page]:origin-left group-aria-[current=page]:scale-x-100",
  ],
  "goes-out-comes-in": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right delay-300 group-hover:scale-x-0 group-hover:delay-0 group-focus-visible:scale-x-0 group-focus-visible:delay-0 motion-reduce:delay-0",
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-hover:delay-300 group-focus-visible:scale-x-100 group-focus-visible:delay-300 motion-reduce:delay-0",
  ],
  scribble: [],
};
const scribble =
  "pointer-events-none absolute inset-x-0 -bottom-2 h-2.5 w-full overflow-visible";
const scribblePath =
  "[stroke-dasharray:1_2] [stroke-dashoffset:1] transition-[stroke-dashoffset] duration-500 ease-out group-hover:[stroke-dashoffset:0] group-focus-visible:[stroke-dashoffset:0] group-aria-[current=page]:[stroke-dashoffset:0] motion-reduce:transition-none";

const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
const SAFE_SCHEME = /^(https?|mailto|tel):/i;

/** Href berskema selain http, https, mailto, dan tel (misalnya javascript:) diganti "#". */
const safeHref = (href: string) => {
  const cleaned = href.replace(/[\t\n\r]/g, "");
  const start = [...cleaned].findIndex((char) => char > " ");
  const value = cleaned.slice(Math.max(start, 0));
  return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
};
</script>

<template>
  <a
    :href="safeHref(props.href)"
    :aria-current="props.active ? 'page' : undefined"
    :class="base"
    ><slot /><span
      v-for="(line, i) in lines[props.variant]"
      :key="i"
      aria-hidden="true"
      :class="line"
    ></span
    ><svg
      v-if="props.variant === 'scribble'"
      aria-hidden="true"
      viewBox="0 0 100 10"
      preserveAspectRatio="none"
      fill="none"
      :class="scribble"
    >
      <path
        d="M1 6.5C14 3.5 26 8.5 40 5.5S66 2.5 78 5.5 93 7 99 4"
        pathLength="1"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        vector-effect="non-scaling-stroke"
        :class="scribblePath"
      /></svg
  ></a>
</template>
```

```vue
<template>
  <nav class="flex gap-5 text-sm font-medium">
    <UnderlineLink href="/" active>Home</UnderlineLink>
    <UnderlineLink href="/docs" variant="comes-in-goes-out">Docs</UnderlineLink>
  </nav>
  <UnderlineLink href="/guide" variant="scribble"
    >Migration guide</UnderlineLink
  >
</template>
```

### Svelte

Save at `src/lib/components/markas/UnderlineLink.svelte`. No extra dependencies.

```svelte
<script lang="ts">
  import type { Snippet } from "svelte";
  import type { HTMLAnchorAttributes } from "svelte/elements";

  const base =
    "group relative inline-block rounded-sm focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-primary";
  const lines = {
    "grow-center": [
      "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
    ],
    slide: [
      "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
    ],
    "comes-in-goes-out": [
      "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right scale-x-0 group-hover:origin-left group-hover:scale-x-100 group-focus-visible:origin-left group-focus-visible:scale-x-100 group-aria-[current=page]:origin-left group-aria-[current=page]:scale-x-100",
    ],
    "goes-out-comes-in": [
      "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right delay-300 group-hover:scale-x-0 group-hover:delay-0 group-focus-visible:scale-x-0 group-focus-visible:delay-0 motion-reduce:delay-0",
      "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-hover:delay-300 group-focus-visible:scale-x-100 group-focus-visible:delay-300 motion-reduce:delay-0",
    ],
    scribble: [],
  };
  const scribble =
    "pointer-events-none absolute inset-x-0 -bottom-2 h-2.5 w-full overflow-visible";
  const scribblePath =
    "[stroke-dasharray:1_2] [stroke-dashoffset:1] transition-[stroke-dashoffset] duration-500 ease-out group-hover:[stroke-dashoffset:0] group-focus-visible:[stroke-dashoffset:0] group-aria-[current=page]:[stroke-dashoffset:0] motion-reduce:transition-none";

  const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
  const SAFE_SCHEME = /^(https?|mailto|tel):/i;

  /** Href with a scheme other than http, https, mailto, or tel (javascript:, for example) becomes "#". */
  const safeHref = (href: string) => {
    const cleaned = href.replace(/[\t\n\r]/g, "");
    const start = [...cleaned].findIndex((char) => char > " ");
    const value = cleaned.slice(Math.max(start, 0));
    return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
  };

  type Props = Omit<HTMLAnchorAttributes, "href"> & {
    href: string;
    variant?: keyof typeof lines;
    /** Mark the link as the current page (aria-current="page"); the line stays visible. */
    active?: boolean;
    children?: Snippet;
  };

  let {
    href,
    variant = "grow-center",
    active = false,
    class: className,
    children,
    ...rest
  }: Props = $props();
</script>

<a
  href={safeHref(href)}
  aria-current={active ? "page" : undefined}
  {...rest}
  class={[base, className]}
  >{@render children?.()}{#each lines[variant] as line, i (i)}<span
      aria-hidden="true"
      class={line}
    ></span>{/each}{#if variant === "scribble"}<svg
      aria-hidden="true"
      viewBox="0 0 100 10"
      preserveAspectRatio="none"
      fill="none"
      class={scribble}
      ><path
        d="M1 6.5C14 3.5 26 8.5 40 5.5S66 2.5 78 5.5 93 7 99 4"
        pathLength="1"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        vector-effect="non-scaling-stroke"
        class={scribblePath}
      /></svg
    >{/if}</a
>
```

```svelte
<script lang="ts">
  import UnderlineLink from "$lib/components/markas/UnderlineLink.svelte";
</script>

<nav class="flex gap-5 text-sm font-medium">
  <UnderlineLink href="/" active>Home</UnderlineLink>
  <UnderlineLink href="/docs" variant="comes-in-goes-out">Docs</UnderlineLink>
</nav>
<UnderlineLink href="/guide" variant="scribble">Migration guide</UnderlineLink>
```

### Astro

Save at `src/components/markas/UnderlineLink.astro`. No extra dependencies.

```astro
---
import type { HTMLAttributes } from "astro/types";

const base =
  "group relative inline-block rounded-sm focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-primary";
const lines = {
  "grow-center": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
  ],
  slide: [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
  ],
  "comes-in-goes-out": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right scale-x-0 group-hover:origin-left group-hover:scale-x-100 group-focus-visible:origin-left group-focus-visible:scale-x-100 group-aria-[current=page]:origin-left group-aria-[current=page]:scale-x-100",
  ],
  "goes-out-comes-in": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right delay-300 group-hover:scale-x-0 group-hover:delay-0 group-focus-visible:scale-x-0 group-focus-visible:delay-0 motion-reduce:delay-0",
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-hover:delay-300 group-focus-visible:scale-x-100 group-focus-visible:delay-300 motion-reduce:delay-0",
  ],
  scribble: [],
};
const scribble =
  "pointer-events-none absolute inset-x-0 -bottom-2 h-2.5 w-full overflow-visible";
const scribblePath =
  "[stroke-dasharray:1_2] [stroke-dashoffset:1] transition-[stroke-dashoffset] duration-500 ease-out group-hover:[stroke-dashoffset:0] group-focus-visible:[stroke-dashoffset:0] group-aria-[current=page]:[stroke-dashoffset:0] motion-reduce:transition-none";

const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
const SAFE_SCHEME = /^(https?|mailto|tel):/i;

/** Href with a scheme other than http, https, mailto, or tel (javascript:, for example) becomes "#". */
const safeHref = (href: string) => {
  const cleaned = href.replace(/[\t\n\r]/g, "");
  const start = [...cleaned].findIndex((char) => char > " ");
  const value = cleaned.slice(Math.max(start, 0));
  return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
};

type Props = Omit<HTMLAttributes<"a">, "href"> & {
  href: string;
  variant?: keyof typeof lines;
  /** Mark the link as the current page (aria-current="page"); the line stays visible. */
  active?: boolean;
};

const {
  href,
  variant = "grow-center",
  active = false,
  class: className,
  ...rest
} = Astro.props;
---

<a
  href={safeHref(href)}
  aria-current={active ? "page" : undefined}
  {...rest}
  class:list={[base, className]}
  ><slot />{lines[variant].map((line) => <span aria-hidden="true" class={line} />)}{
    variant === "scribble" && (
      <svg
        aria-hidden="true"
        viewBox="0 0 100 10"
        preserveAspectRatio="none"
        fill="none"
        class={scribble}
        ><path
          d="M1 6.5C14 3.5 26 8.5 40 5.5S66 2.5 78 5.5 93 7 99 4"
          pathLength="1"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          vector-effect="non-scaling-stroke"
          class={scribblePath}
        /></svg
      >
    )
  }</a
>
```

```astro
---
import UnderlineLink from "@/components/markas/UnderlineLink.astro";
---

<nav class="flex gap-5 text-sm font-medium">
  <UnderlineLink href="/" active>Home</UnderlineLink>
  <UnderlineLink href="/docs" variant="comes-in-goes-out">Docs</UnderlineLink>
</nav>
<UnderlineLink href="/guide" variant="scribble">Migration guide</UnderlineLink>
```

### Angular

Save at `src/app/markas/underline-link.component.ts`. No extra dependencies.

```ts
import {
  booleanAttribute,
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from "@angular/core";

const base =
  "group relative inline-block rounded-sm focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-primary";
const lines = {
  "grow-center": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
  ],
  slide: [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-focus-visible:scale-x-100 group-aria-[current=page]:scale-x-100",
  ],
  "comes-in-goes-out": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right scale-x-0 group-hover:origin-left group-hover:scale-x-100 group-focus-visible:origin-left group-focus-visible:scale-x-100 group-aria-[current=page]:origin-left group-aria-[current=page]:scale-x-100",
  ],
  "goes-out-comes-in": [
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-right delay-300 group-hover:scale-x-0 group-hover:delay-0 group-focus-visible:scale-x-0 group-focus-visible:delay-0 motion-reduce:delay-0",
    "pointer-events-none absolute inset-x-0 -bottom-0.5 h-0.5 rounded-full bg-current transition-transform duration-300 ease-out motion-reduce:transition-none origin-left scale-x-0 group-hover:scale-x-100 group-hover:delay-300 group-focus-visible:scale-x-100 group-focus-visible:delay-300 motion-reduce:delay-0",
  ],
  scribble: [],
};
const scribble =
  "pointer-events-none absolute inset-x-0 -bottom-2 h-2.5 w-full overflow-visible";
const scribblePath =
  "[stroke-dasharray:1_2] [stroke-dashoffset:1] transition-[stroke-dashoffset] duration-500 ease-out group-hover:[stroke-dashoffset:0] group-focus-visible:[stroke-dashoffset:0] group-aria-[current=page]:[stroke-dashoffset:0] motion-reduce:transition-none";

const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
const SAFE_SCHEME = /^(https?|mailto|tel):/i;

/** Href with a scheme other than http, https, mailto, or tel (javascript:, for example) becomes "#". */
const safeHref = (href: string) => {
  const cleaned = href.replace(/[\t\n\r]/g, "");
  const start = [...cleaned].findIndex((char) => char > " ");
  const value = cleaned.slice(Math.max(start, 0));
  return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
};

export type UnderlineLinkVariant = keyof typeof lines;

/**
 * Attribute selector on the native link, so aria-*, target, rel, and the user's class stay on it.
 * The href is checked: a scheme other than http, https, mailto, or tel becomes "#".
 */
@Component({
  selector: "a[markas-underline-link]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    "[attr.href]": "safeHref()",
    "[attr.aria-current]": "active() ? 'page' : null",
    "[class]": "base",
  },
  template: `<ng-content />
    @for (line of lines(); track $index) {
      <span aria-hidden="true" [class]="line"></span>
    }
    @if (variant() === "scribble") {
      <svg
        aria-hidden="true"
        viewBox="0 0 100 10"
        preserveAspectRatio="none"
        fill="none"
        [class]="scribble"
      >
        <path
          d="M1 6.5C14 3.5 26 8.5 40 5.5S66 2.5 78 5.5 93 7 99 4"
          pathLength="1"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          vector-effect="non-scaling-stroke"
          [class]="scribblePath"
        />
      </svg>
    }`,
})
export class UnderlineLink {
  readonly href = input.required<string>();
  readonly variant = input<UnderlineLinkVariant>("grow-center");
  /** Mark the link as the current page (aria-current="page"); the line stays visible. */
  readonly active = input(false, { transform: booleanAttribute });

  protected readonly base = base;
  protected readonly scribble = scribble;
  protected readonly scribblePath = scribblePath;
  protected readonly safeHref = computed(() => safeHref(this.href()));
  protected readonly lines = computed(() => lines[this.variant()]);
}
```

```ts
import { Component } from "@angular/core";
import { UnderlineLink } from "./markas/underline-link.component";

@Component({
  selector: "app-site-nav",
  imports: [UnderlineLink],
  template: `
    <nav class="flex gap-5 text-sm font-medium">
      <a markas-underline-link href="/" active>Home</a>
      <a markas-underline-link href="/docs" variant="comes-in-goes-out">Docs</a>
    </nav>
    <a markas-underline-link href="/guide" variant="scribble"
      >Migration guide</a
    >
  `,
})
export class SiteNav {}
```

Angular uses an attribute selector on the native link, so `target`, `rel`, `aria-*`, and `class` stay on it. No script in Astro: the line is pure CSS.

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `href` | `string` | - | Link target. Schemes other than http, https, mailto, and tel are replaced with #. |
| `variant` | `"grow-center" \| "slide" \| "comes-in-goes-out" \| "goes-out-comes-in" \| "scribble"` | "grow-center" | grow-center grows from the middle, slide grows from and returns to the left, comes-in-goes-out enters from the left and leaves to the right, goes-out-comes-in is visible at rest and redraws itself, scribble draws a hand drawn stroke. |
| `active` | `boolean` | false | Mark the link as the current page with aria-current="page" and keep the line visible. |

## Accessibility

- A native a element; the line and scribble are decorative and aria-hidden.
- The line also appears on keyboard focus, next to a visible focus outline.
- active sets aria-current="page", so the current page is announced, not only drawn.
- With reduced motion, the line appears without a transition.

## Notes

- Pure CSS, no JavaScript. The line appears on hover, on keyboard focus, and stays visible with `active`, which also sets `aria-current="page"`.
- The line and the scribble use `currentColor`, so they follow the text color of the link.
- The link is `inline-block` so the line sits under the whole label; keep labels on one line.
- With reduced motion, the line appears without a transition.
