# Code Block

Blok kode monospace dengan nama file opsional, nomor baris, dan tombol salin. Teks polos tanpa pewarnaan sintaks.

- Kategori: Data
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/code-block.json
- CLI: `npx markasui@latest add code-block`

## Kapan dipakai

Menampilkan potongan kode, perintah, atau file konfigurasi yang dibaca atau disalin user di aplikasi, seperti setup API key atau langkah instalasi.

- Kirim kode lewat prop code sebagai string, jangan lewat children atau slot
- Pakai title untuk nama file atau Terminal, bukan untuk kalimat
- Jangan memakai Code block untuk kode inline, gunakan elemen code di dalam teks

## Kode

### Blade

Simpan di `resources/views/components/markas/code-block.blade.php`. Dependensi: alpinejs.

```blade
@props([
    'code' => '',
    'title' => null,
    'showLineNumbers' => false,
    'copyLabel' => 'Copy code',
    'copiedLabel' => 'Copied',
])

@php
    $root = 'overflow-hidden rounded-xl border border-line bg-subtle';
    $header = 'flex items-center gap-2 border-b border-line py-1.5 pr-1.5 pl-4';
    $titleClass = 'min-w-0 truncate text-xs font-medium text-muted';
    $copyButton = 'ml-auto inline-flex h-7 shrink-0 items-center gap-1.5 rounded-md px-2 text-xs font-medium text-muted transition-colors duration-150 ease-out hover:bg-surface hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none';
    $icon = 'size-4';
    $pre = 'overflow-x-auto p-4 font-mono text-sm leading-6 text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-primary';
    $lineClass = 'block min-h-6 whitespace-pre';
    $lineNumber = 'mr-4 inline-block min-w-6 text-right text-muted select-none';

    $lines = preg_split('/\r?\n/', preg_replace('/\r?\n$/', '', (string) $code));
@endphp

<div
    x-data="{
        copied: false,
        timer: null,
        async copy() {
            try {
                await navigator.clipboard.writeText(@js((string) $code));
            } catch {
                this.copied = false;
                return;
            }
            this.copied = true;
            clearTimeout(this.timer);
            this.timer = setTimeout(() => (this.copied = false), 2000);
        },
        destroy() {
            clearTimeout(this.timer);
        },
    }"
    {{ $attributes->class([$root]) }}
>
    <div class="{{ $header }}">
        @if ($title)
            <span class="{{ $titleClass }}">{{ $title }}</span>
        @endif
        <button
            type="button"
            data-state="idle"
            :data-state="copied ? 'copied' : 'idle'"
            @click="copy()"
            class="{{ $copyButton }}"
        >
            <svg x-show="copied" x-cloak aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="{{ $icon }}" style="display: none"><path d="m5 12 5 5L20 7" /></svg>
            <svg x-show="! copied" aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="{{ $icon }}"><rect x="8" y="8" width="12" height="12" rx="2" /><path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" /></svg>
            <span aria-live="polite" x-text="copied ? @js($copiedLabel) : @js($copyLabel)">{{ $copyLabel }}</span>
        </button>
    </div>
    <pre tabindex="0" class="{{ $pre }}"><code>@foreach ($lines as $index => $line)<span class="{{ $lineClass }}">@if ($showLineNumbers)<span aria-hidden="true" class="{{ $lineNumber }}">{{ $index + 1 }}</span>@endif{{ $line }}</span>@endforeach</code></pre>
</div>
```

```blade
@php
    $code = <<<'CODE'
pnpm add markasui
pnpm markasui add carousel
CODE;
@endphp

<x-markas.code-block :code="$code" title="Terminal" copy-label="Salin kode" copied-label="Tersalin" />
```

Butuh Alpine.js untuk tombol salin.

### React

Simpan di `src/components/markas/CodeBlock.tsx`. Tanpa dependensi tambahan.

```tsx
"use client";

import { useEffect, useRef, useState, type ComponentProps } from "react";

const root = "overflow-hidden rounded-xl border border-line bg-subtle";
const header =
  "flex items-center gap-2 border-b border-line py-1.5 pr-1.5 pl-4";
const titleClass = "min-w-0 truncate text-xs font-medium text-muted";
const copyButton =
  "ml-auto inline-flex h-7 shrink-0 items-center gap-1.5 rounded-md px-2 text-xs font-medium text-muted transition-colors duration-150 ease-out hover:bg-surface hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const icon = "size-4";
const pre =
  "overflow-x-auto p-4 font-mono text-sm leading-6 text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-primary";
const lineClass = "block min-h-6 whitespace-pre";
const lineNumber =
  "mr-4 inline-block min-w-6 text-right text-muted select-none";
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

const COPIED_DURATION = 2000;

/** Pecah kode per baris, abaikan satu baris kosong di akhir. */
export const splitLines = (code: string): string[] =>
  code.replace(/\r?\n$/, "").split(/\r?\n/);

export type CodeBlockProps = Omit<ComponentProps<"div">, "children"> & {
  code: string;
  title?: string;
  showLineNumbers?: boolean;
  copyLabel?: string;
  copiedLabel?: string;
};

export function CodeBlock({
  code,
  title,
  showLineNumbers = false,
  copyLabel = "Copy code",
  copiedLabel = "Copied",
  className,
  ...props
}: CodeBlockProps) {
  const [copied, setCopied] = useState(false);
  const timer = useRef<ReturnType<typeof setTimeout>>(undefined);

  useEffect(() => () => clearTimeout(timer.current), []);

  const copy = async () => {
    try {
      await navigator.clipboard.writeText(code);
    } catch {
      setCopied(false);
      return;
    }
    setCopied(true);
    clearTimeout(timer.current);
    timer.current = setTimeout(() => setCopied(false), COPIED_DURATION);
  };

  return (
    <div className={cx(root, className)} {...props}>
      <div className={header}>
        {title && <span className={titleClass}>{title}</span>}
        <button
          type="button"
          data-state={copied ? "copied" : "idle"}
          className={copyButton}
          onClick={copy}
        >
          {copied ? (
            <svg
              aria-hidden="true"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="1.5"
              strokeLinecap="round"
              strokeLinejoin="round"
              className={icon}
            >
              <path d="m5 12 5 5L20 7" />
            </svg>
          ) : (
            <svg
              aria-hidden="true"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="1.5"
              strokeLinecap="round"
              strokeLinejoin="round"
              className={icon}
            >
              <rect x="8" y="8" width="12" height="12" rx="2" />
              <path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" />
            </svg>
          )}
          <span aria-live="polite">{copied ? copiedLabel : copyLabel}</span>
        </button>
      </div>
      <pre tabIndex={0} className={pre}>
        <code>
          {splitLines(code).map((line, index) => (
            <span key={index} className={lineClass}>
              {showLineNumbers && (
                <span aria-hidden="true" className={lineNumber}>
                  {index + 1}
                </span>
              )}
              {line}
            </span>
          ))}
        </code>
      </pre>
    </div>
  );
}
```

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

const code = `pnpm add markasui
pnpm markasui add carousel`;

<CodeBlock
  code={code}
  title="Terminal"
  copyLabel="Salin kode"
  copiedLabel="Tersalin"
/>;
```

### Vue

Simpan di `src/components/markas/CodeBlock.vue`. Tanpa dependensi tambahan.

```vue
<script setup lang="ts">
import { computed, onBeforeUnmount, ref } from "vue";

const props = withDefaults(
  defineProps<{
    code: string;
    title?: string;
    showLineNumbers?: boolean;
    copyLabel?: string;
    copiedLabel?: string;
  }>(),
  {
    title: undefined,
    showLineNumbers: false,
    copyLabel: "Copy code",
    copiedLabel: "Copied",
  },
);

const root = "overflow-hidden rounded-xl border border-line bg-subtle";
const header =
  "flex items-center gap-2 border-b border-line py-1.5 pr-1.5 pl-4";
const titleClass = "min-w-0 truncate text-xs font-medium text-muted";
const copyButton =
  "ml-auto inline-flex h-7 shrink-0 items-center gap-1.5 rounded-md px-2 text-xs font-medium text-muted transition-colors duration-150 ease-out hover:bg-surface hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const icon = "size-4";
const pre =
  "overflow-x-auto p-4 font-mono text-sm leading-6 text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-primary";
const lineClass = "block min-h-6 whitespace-pre";
const lineNumber =
  "mr-4 inline-block min-w-6 text-right text-muted select-none";

const COPIED_DURATION = 2000;

const lines = computed(() => props.code.replace(/\r?\n$/, "").split(/\r?\n/));
const copied = ref(false);
let timer: ReturnType<typeof setTimeout> | undefined;

async function copy() {
  try {
    await navigator.clipboard.writeText(props.code);
  } catch {
    copied.value = false;
    return;
  }
  copied.value = true;
  clearTimeout(timer);
  timer = setTimeout(() => (copied.value = false), COPIED_DURATION);
}

onBeforeUnmount(() => clearTimeout(timer));
</script>

<template>
  <div :class="root">
    <div :class="header">
      <span v-if="props.title" :class="titleClass">{{ props.title }}</span>
      <button
        type="button"
        :data-state="copied ? 'copied' : 'idle'"
        :class="copyButton"
        @click="copy"
      >
        <svg
          v-if="copied"
          aria-hidden="true"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          :class="icon"
        >
          <path d="m5 12 5 5L20 7" />
        </svg>
        <svg
          v-else
          aria-hidden="true"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          :class="icon"
        >
          <rect x="8" y="8" width="12" height="12" rx="2" />
          <path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" />
        </svg>
        <span aria-live="polite">{{
          copied ? props.copiedLabel : props.copyLabel
        }}</span>
      </button>
    </div>
    <pre
      tabindex="0"
      :class="pre"
    ><code><span v-for="(line, index) in lines" :key="index" :class="lineClass"><span v-if="props.showLineNumbers" aria-hidden="true" :class="lineNumber">{{ index + 1 }}</span>{{ line }}</span></code></pre>
  </div>
</template>
```

```vue
<script setup lang="ts">
const code = `pnpm add markasui
pnpm markasui add carousel`;
</script>

<template>
  <CodeBlock
    :code="code"
    title="Terminal"
    copy-label="Salin kode"
    copied-label="Tersalin"
  />
</template>
```

### Svelte

Simpan di `src/lib/components/markas/CodeBlock.svelte`. Tanpa dependensi tambahan.

```svelte
<script lang="ts" module>
  /** Split code into lines, ignoring one trailing empty line. */
  export const splitLines = (code: string): string[] =>
    code.replace(/\r?\n$/, "").split(/\r?\n/);
</script>

<script lang="ts">
  import { onDestroy } from "svelte";
  import type { HTMLAttributes } from "svelte/elements";

  const root = "overflow-hidden rounded-xl border border-line bg-subtle";
  const header =
    "flex items-center gap-2 border-b border-line py-1.5 pr-1.5 pl-4";
  const titleClass = "min-w-0 truncate text-xs font-medium text-muted";
  const copyButton =
    "ml-auto inline-flex h-7 shrink-0 items-center gap-1.5 rounded-md px-2 text-xs font-medium text-muted transition-colors duration-150 ease-out hover:bg-surface hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
  const icon = "size-4";
  const pre =
    "overflow-x-auto p-4 font-mono text-sm leading-6 text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-primary";
  const lineClass = "block min-h-6 whitespace-pre";
  const lineNumber =
    "mr-4 inline-block min-w-6 text-right text-muted select-none";

  const COPIED_DURATION = 2000;

  type Props = Omit<HTMLAttributes<HTMLDivElement>, "children"> & {
    code: string;
    title?: string;
    showLineNumbers?: boolean;
    copyLabel?: string;
    copiedLabel?: string;
  };

  let {
    code,
    title,
    showLineNumbers = false,
    copyLabel = "Copy code",
    copiedLabel = "Copied",
    class: className,
    ...rest
  }: Props = $props();

  let copied = $state(false);
  let timer: ReturnType<typeof setTimeout> | undefined;
  const lines = $derived(splitLines(code));

  async function copy() {
    try {
      await navigator.clipboard.writeText(code);
    } catch {
      copied = false;
      return;
    }
    copied = true;
    clearTimeout(timer);
    timer = setTimeout(() => (copied = false), COPIED_DURATION);
  }

  onDestroy(() => clearTimeout(timer));
</script>

<div class={[root, className]} {...rest}>
  <div class={header}>
    {#if title}<span class={titleClass}>{title}</span>{/if}
    <button
      type="button"
      data-state={copied ? "copied" : "idle"}
      class={copyButton}
      onclick={copy}
    >
      {#if copied}
        <svg
          aria-hidden="true"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          class={icon}
        >
          <path d="m5 12 5 5L20 7" />
        </svg>
      {:else}
        <svg
          aria-hidden="true"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          class={icon}
        >
          <rect x="8" y="8" width="12" height="12" rx="2" />
          <path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" />
        </svg>
      {/if}
      <span aria-live="polite">{copied ? copiedLabel : copyLabel}</span>
    </button>
  </div>
  <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
  <pre tabindex="0" class={pre}><code
      >{#each lines as line, index (index)}<span class={lineClass}
          >{#if showLineNumbers}<span aria-hidden="true" class={lineNumber}
              >{index + 1}</span
            >{/if}{line}</span
        >{/each}</code
    ></pre>
</div>
```

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

  const code = `pnpm add markasui\npnpm markasui init`;
</script>

<CodeBlock {code} title="Terminal" showLineNumbers />
```

### Astro

Simpan di `src/components/markas/CodeBlock.astro`. Tanpa dependensi tambahan.

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

const root = "overflow-hidden rounded-xl border border-line bg-subtle";
const header =
  "flex items-center gap-2 border-b border-line py-1.5 pr-1.5 pl-4";
const titleClass = "min-w-0 truncate text-xs font-medium text-muted";
const copyButton =
  "ml-auto inline-flex h-7 shrink-0 items-center gap-1.5 rounded-md px-2 text-xs font-medium text-muted transition-colors duration-150 ease-out hover:bg-surface hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const icon = "size-4";
const pre =
  "overflow-x-auto p-4 font-mono text-sm leading-6 text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-primary";
const lineClass = "block min-h-6 whitespace-pre";
const lineNumber =
  "mr-4 inline-block min-w-6 text-right text-muted select-none";

type Props = Omit<HTMLAttributes<"div">, "children"> & {
  code: string;
  title?: string;
  showLineNumbers?: boolean;
  copyLabel?: string;
  copiedLabel?: string;
};

const {
  code,
  title,
  showLineNumbers = false,
  copyLabel = "Copy code",
  copiedLabel = "Copied",
  class: className,
  ...rest
} = Astro.props;

// Split code into lines, ignoring one trailing empty line.
const lines = code.replace(/\r?\n$/, "").split(/\r?\n/);
---

<div class:list={[root, className]} {...rest}>
  <div class={header}>
    {title && <span class={titleClass}>{title}</span>}
    <button
      type="button"
      data-state="idle"
      data-markas-code-block-copy={code}
      data-copy-label={copyLabel}
      data-copied-label={copiedLabel}
      class={copyButton}
    >
      <svg
        style="display: none"
        data-icon="copied"
        aria-hidden="true"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="1.5"
        stroke-linecap="round"
        stroke-linejoin="round"
        class={icon}
      >
        <path d="m5 12 5 5L20 7"></path>
      </svg>
      <svg
        data-icon="idle"
        aria-hidden="true"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="1.5"
        stroke-linecap="round"
        stroke-linejoin="round"
        class={icon}
      >
        <rect x="8" y="8" width="12" height="12" rx="2"></rect>
        <path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"
        ></path>
      </svg>
      <span aria-live="polite">{copyLabel}</span>
    </button>
  </div>
  <pre tabindex="0" class={pre}><code>{lines.map((line, index) => <span class={lineClass}>{showLineNumbers && <span aria-hidden="true" class={lineNumber}>{index + 1}</span>}{line}</span>)}</code></pre>
</div>

<script>
  // One listener per page serves every code block, including markup added later.
  const COPIED_DURATION = 2000;
  const timers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>();

  const setCopied = (button: HTMLElement, copied: boolean) => {
    button.dataset.state = copied ? "copied" : "idle";
    button.querySelectorAll<SVGElement>("[data-icon]").forEach((svg) => {
      const shown = svg.dataset.icon === button.dataset.state;
      svg.style.display = shown ? "" : "none";
    });
    const label = button.querySelector("[aria-live]");
    if (label)
      label.textContent =
        (copied ? button.dataset.copiedLabel : button.dataset.copyLabel) ?? "";
  };

  document.addEventListener("click", async (event) => {
    const button = (event.target as Element).closest<HTMLElement>(
      "[data-markas-code-block-copy]",
    );
    if (!button) return;
    try {
      await navigator.clipboard.writeText(
        button.dataset.markasCodeBlockCopy ?? "",
      );
    } catch {
      setCopied(button, false);
      return;
    }
    setCopied(button, true);
    clearTimeout(timers.get(button));
    timers.set(
      button,
      setTimeout(() => setCopied(button, false), COPIED_DURATION),
    );
  });
</script>
```

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

const code = `pnpm add markasui\npnpm markasui init`;
---

<CodeBlock code={code} title="Terminal" showLineNumbers />
```

Script kecil yang dibundel menangani tombol salin semua code block di halaman.

### Angular

Simpan di `src/app/markas/code-block.component.ts`. Tanpa dependensi tambahan.

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

const root = "overflow-hidden rounded-xl border border-line bg-subtle";
const header =
  "flex items-center gap-2 border-b border-line py-1.5 pr-1.5 pl-4";
const titleClass = "min-w-0 truncate text-xs font-medium text-muted";
const copyButton =
  "ml-auto inline-flex h-7 shrink-0 items-center gap-1.5 rounded-md px-2 text-xs font-medium text-muted transition-colors duration-150 ease-out hover:bg-surface hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const icon = "size-4";
const pre =
  "overflow-x-auto p-4 font-mono text-sm leading-6 text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-primary";
const lineClass = "block min-h-6 whitespace-pre";
const lineNumber =
  "mr-4 inline-block min-w-6 text-right text-muted select-none";

const COPIED_DURATION = 2000;

/** Split code into lines, ignoring one trailing empty line. */
export const splitLines = (code: string): string[] =>
  code.replace(/\r?\n$/, "").split(/\r?\n/);

/**
 * Attribute selector on the native root div: the user's class and attributes stay on it.
 * The host title attribute is cleared so the file name never becomes a tooltip.
 */
@Component({
  selector: "div[markas-code-block]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { "[class]": "root", "[attr.title]": "null" },
  template: `
    <div [class]="header">
      @if (title()) {
        <span [class]="titleClass">{{ title() }}</span>
      }
      <button
        type="button"
        [attr.data-state]="copied() ? 'copied' : 'idle'"
        [class]="copyButton"
        (click)="copy()"
      >
        @if (copied()) {
          <svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="1.5"
            stroke-linecap="round"
            stroke-linejoin="round"
            [attr.class]="icon"
          >
            <path d="m5 12 5 5L20 7" />
          </svg>
        } @else {
          <svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="1.5"
            stroke-linecap="round"
            stroke-linejoin="round"
            [attr.class]="icon"
          >
            <rect x="8" y="8" width="12" height="12" rx="2" />
            <path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" />
          </svg>
        }
        <span aria-live="polite">{{
          copied() ? copiedLabel() : copyLabel()
        }}</span>
      </button>
    </div>
    <pre
      tabindex="0"
      [class]="pre"
    ><code>@for (line of lines(); track $index) {<span [class]="lineClass">@if (showLineNumbers()) {<span aria-hidden="true" [class]="lineNumber">{{ $index + 1 }}</span>}{{ line }}</span>}</code></pre>
  `,
})
export class CodeBlock {
  readonly code = input.required<string>();
  readonly title = input<string>();
  readonly showLineNumbers = input(false, { transform: booleanAttribute });
  readonly copyLabel = input("Copy code");
  readonly copiedLabel = input("Copied");

  protected readonly root = root;
  protected readonly header = header;
  protected readonly titleClass = titleClass;
  protected readonly copyButton = copyButton;
  protected readonly icon = icon;
  protected readonly pre = pre;
  protected readonly lineClass = lineClass;
  protected readonly lineNumber = lineNumber;
  protected readonly copied = signal(false);
  protected readonly lines = computed(() => splitLines(this.code()));
  private timer: ReturnType<typeof setTimeout> | undefined;

  constructor() {
    inject(DestroyRef).onDestroy(() => clearTimeout(this.timer));
  }

  protected async copy() {
    try {
      await navigator.clipboard.writeText(this.code());
    } catch {
      this.copied.set(false);
      return;
    }
    this.copied.set(true);
    clearTimeout(this.timer);
    this.timer = setTimeout(() => this.copied.set(false), COPIED_DURATION);
  }
}
```

```ts
import { Component } from "@angular/core";
import { CodeBlock } from "./markas/code-block.component";

@Component({
  selector: "app-example",
  imports: [CodeBlock],
  template: `
    <div markas-code-block [code]="code" title="Terminal" showLineNumbers></div>
  `,
})
export class Example {
  code = "pnpm add markasui\npnpm markasui init";
}
```

## Props

| Nama | Tipe | Default | Keterangan |
| --- | --- | --- | --- |
| `code` | `string` | - | Kode yang ditampilkan dan disalin. Dikirim sebagai string agar ketiga framework merender baris yang sama. |
| `title` | `string` | - | Nama file atau keterangan opsional di header. |
| `showLineNumbers` | `boolean` | false | Tampilkan nomor baris. Nomor disembunyikan dari pembaca layar dan tidak ikut terseleksi. |
| `copyLabel` | `string` | Copy code | Teks tombol salin. Isi "Salin kode" untuk situs berbahasa Indonesia. |
| `copiedLabel` | `string` | Copied | Teks yang tampil dua detik setelah kode tersalin. Isi "Tersalin" untuk situs berbahasa Indonesia. |

## Aksesibilitas

- Elemen pre punya tabindex=0, jadi pengguna keyboard bisa memfokusnya dan menggulir baris panjang secara horizontal.
- Tombol salin adalah button native dengan teks terlihat, dan pesan tersalin diumumkan lewat aria-live.
- Nomor baris memakai aria-hidden sehingga pembaca layar hanya membaca kodenya.

## Catatan

- Kode berupa teks polos tanpa pewarnaan sintaks, jadi ringan dan identik di semua framework.
- Tambahkan `showLineNumbers` (`show-line-numbers` di Blade dan Vue) untuk potongan kode yang panjang.
- Tombol salin memakai `navigator.clipboard` yang butuh HTTPS atau localhost.
