# Background Boxes

Grid kotak miring, datar, atau berperspektif yang menyala di bawah kursor lalu memudar, hanya dengan CSS hover.

- Kategori: Latar
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/background-boxes.json
- CLI: `npx markasui@latest add background-boxes`

## Kapan dipakai

Hero, section fitur, atau banner promo yang jenaka, di mana hover di latar menjadi bagian keseruan.

- Atur warna latar, padding, dan radius lewat class pada komponen
- Letakkan kartu atau blok teks di atas bg-surface supaya tetap terbaca di atas kotak yang menyala
- Kurangi rows dan cols di area kecil, jangan membuat kotak terlalu kecil

## Kode

### Blade

Simpan di `resources/views/components/markas/background-boxes.blade.php`. Tanpa dependensi tambahan.

```blade
@props([
    'variant' => 'skewed',
    'rows' => 12,
    'cols' => 18,
    'size' => 40,
    'speed' => 'normal',
    'color' => 'primary',
])

@php
    $base = 'relative isolate overflow-hidden';
    $layer = 'absolute inset-0 overflow-hidden';
    $grid = 'absolute top-1/2 left-1/2 grid grid-cols-[repeat(var(--markas-boxes-cols),var(--markas-boxes-size))] auto-rows-[var(--markas-boxes-size)] border-t border-l border-line';
    $cell = 'border-r border-b border-line transition-colors duration-1000 ease-out hover:bg-current/40 hover:duration-0 motion-reduce:transition-none';
    $ember = 'animate-[markas-boxes-glow_var(--markas-boxes-duration)_ease-in-out_infinite] motion-reduce:animate-none';
    $content = 'pointer-events-none relative [&_:is(a,button,input,select,textarea,label,summary)]:pointer-events-auto';
    $variants = [
        'skewed' => ['layer' => '[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]', 'grid' => '[transform:translate(-50%,-50%)_skewX(-48deg)_skewY(14deg)]'],
        'flat' => ['layer' => '[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]', 'grid' => '-translate-x-1/2 -translate-y-1/2'],
        'perspective' => ['layer' => '[perspective:520px] [mask-image:linear-gradient(to_bottom,transparent_25%,black_60%)]', 'grid' => '[transform:translate(-50%,-38%)_rotateX(58deg)]'],
    ];
    $colors = [
        'muted' => 'text-muted',
        'primary' => 'text-primary',
        'success' => 'text-success',
        'warning' => 'text-warning',
        'danger' => 'text-danger',
    ];
    $speeds = ['slow' => '9s', 'normal' => '6s', 'fast' => '3.5s'];
    $keyframes = '@keyframes markas-boxes-glow{0%,100%{box-shadow:none}50%{box-shadow:inset 0 0 0 999px color-mix(in oklab,currentColor 30%,transparent)}}';
    $maxCells = 600;
    $maxSide = 40;
    $emberTarget = 6;
    $emberDelayMs = 700;

    $shape = $variants[$variant] ?? $variants['skewed'];
    $columns = max(1, min((int) $cols, $maxSide));
    $total = min(max(1, min((int) $rows, $maxSide)) * $columns, $maxCells);

    // Sel ke-i. Sekitar 6% sel (dua modulus berbeda supaya tidak membentuk garis) menyala pelan supaya latar terasa hidup tanpa hover.
    $cells = [];
    for ($i = 0; $i < $total; $i++) {
        $lit = ($i * 7) % 13 + ($i * $i) % 11 === $emberTarget;
        $cells[] = $lit ? ['class' => $cell.' '.$ember, 'style' => 'animation-delay: '.(($i % 9) * -$emberDelayMs).'ms'] : ['class' => $cell, 'style' => null];
    }
    $vars = '--markas-boxes-cols: '.$columns.'; --markas-boxes-size: '.(float) $size.'px; --markas-boxes-duration: '.($speeds[$speed] ?? $speeds['normal']);
@endphp

<div {{ $attributes->merge(['style' => $vars])->class([$base]) }}>
    <style>{!! $keyframes !!}</style>
    <div aria-hidden="true" wire:ignore @class([$layer, $shape['layer'], $colors[$color] ?? $colors['primary']])>
        <div @class([$grid, $shape['grid']])>
            @foreach ($cells as $item)
                <span class="{{ $item['class'] }}" @if ($item['style']) style="{{ $item['style'] }}" @endif></span>
            @endforeach
        </div>
    </div>
    <div class="{{ $content }}">{{ $slot }}</div>
</div>
```

```blade
<x-markas.background-boxes variant="skewed" :rows="12" :cols="18" class="rounded-xl bg-surface px-6 py-16">
    <h2 class="text-center text-3xl font-bold text-ink">Desain per blok</h2>
</x-markas.background-boxes>
```

Tidak butuh JavaScript atau Alpine.js.

### React

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

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

const base = "relative isolate overflow-hidden";
const layer = "absolute inset-0 overflow-hidden";
const grid =
  "absolute top-1/2 left-1/2 grid grid-cols-[repeat(var(--markas-boxes-cols),var(--markas-boxes-size))] auto-rows-[var(--markas-boxes-size)] border-t border-l border-line";
const cell =
  "border-r border-b border-line transition-colors duration-1000 ease-out hover:bg-current/40 hover:duration-0 motion-reduce:transition-none";
const ember =
  "animate-[markas-boxes-glow_var(--markas-boxes-duration)_ease-in-out_infinite] motion-reduce:animate-none";
const content =
  "pointer-events-none relative [&_:is(a,button,input,select,textarea,label,summary)]:pointer-events-auto";
const variants = {
  skewed: {
    layer:
      "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
    grid: "[transform:translate(-50%,-50%)_skewX(-48deg)_skewY(14deg)]",
  },
  flat: {
    layer:
      "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
    grid: "-translate-x-1/2 -translate-y-1/2",
  },
  perspective: {
    layer:
      "[perspective:520px] [mask-image:linear-gradient(to_bottom,transparent_25%,black_60%)]",
    grid: "[transform:translate(-50%,-38%)_rotateX(58deg)]",
  },
};
const colors = {
  muted: "text-muted",
  primary: "text-primary",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
};
const speeds = { slow: "9s", normal: "6s", fast: "3.5s" };
const keyframes =
  "@keyframes markas-boxes-glow{0%,100%{box-shadow:none}50%{box-shadow:inset 0 0 0 999px color-mix(in oklab,currentColor 30%,transparent)}}";
const MAX_CELLS = 600;
const MAX_SIDE = 40;
const EMBER_TARGET = 6;
const EMBER_DELAY_MS = 700;
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

const side = (value: number) =>
  Math.max(1, Math.min(Math.trunc(value), MAX_SIDE));

/** Sel ke-i. Sekitar 6% sel (dua modulus berbeda supaya tidak membentuk garis) menyala pelan supaya latar terasa hidup tanpa hover. */
function makeCells(rows: number, cols: number) {
  const columns = side(cols);
  const total = Math.min(side(rows) * columns, MAX_CELLS);
  return Array.from({ length: total }, (_, index) => {
    const lit = ((index * 7) % 13) + ((index * index) % 11) === EMBER_TARGET;
    return lit
      ? { lit, style: { animationDelay: `${(index % 9) * -EMBER_DELAY_MS}ms` } }
      : { lit, style: undefined };
  });
}

export type BackgroundBoxesProps = ComponentProps<"div"> & {
  variant?: keyof typeof variants;
  rows?: number;
  cols?: number;
  size?: number;
  speed?: keyof typeof speeds;
  color?: keyof typeof colors;
};

export function BackgroundBoxes({
  variant = "skewed",
  rows = 12,
  cols = 18,
  size = 40,
  speed = "normal",
  color = "primary",
  className,
  style,
  children,
  ...props
}: BackgroundBoxesProps) {
  const vars = {
    "--markas-boxes-cols": side(cols),
    "--markas-boxes-size": `${size}px`,
    "--markas-boxes-duration": speeds[speed],
  } as CSSProperties;

  return (
    <div
      {...props}
      className={cx(base, className)}
      style={{ ...vars, ...style }}
    >
      <style>{keyframes}</style>
      <div
        aria-hidden="true"
        className={cx(layer, variants[variant].layer, colors[color])}
      >
        <div className={cx(grid, variants[variant].grid)}>
          {makeCells(rows, cols).map((item, index) => (
            <span
              key={index}
              className={cx(cell, item.lit && ember)}
              style={item.style}
            />
          ))}
        </div>
      </div>
      <div className={content}>{children}</div>
    </div>
  );
}
```

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

<BackgroundBoxes
  variant="skewed"
  rows={12}
  cols={18}
  className="rounded-xl bg-surface px-6 py-16"
>
  <h2 className="text-center text-3xl font-bold text-ink">Desain per blok</h2>
</BackgroundBoxes>;
```

### Vue

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

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

const base = "relative isolate overflow-hidden";
const layer = "absolute inset-0 overflow-hidden";
const grid =
  "absolute top-1/2 left-1/2 grid grid-cols-[repeat(var(--markas-boxes-cols),var(--markas-boxes-size))] auto-rows-[var(--markas-boxes-size)] border-t border-l border-line";
const cell =
  "border-r border-b border-line transition-colors duration-1000 ease-out hover:bg-current/40 hover:duration-0 motion-reduce:transition-none";
const ember =
  "animate-[markas-boxes-glow_var(--markas-boxes-duration)_ease-in-out_infinite] motion-reduce:animate-none";
const content =
  "pointer-events-none relative [&_:is(a,button,input,select,textarea,label,summary)]:pointer-events-auto";
const variants = {
  skewed: {
    layer:
      "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
    grid: "[transform:translate(-50%,-50%)_skewX(-48deg)_skewY(14deg)]",
  },
  flat: {
    layer:
      "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
    grid: "-translate-x-1/2 -translate-y-1/2",
  },
  perspective: {
    layer:
      "[perspective:520px] [mask-image:linear-gradient(to_bottom,transparent_25%,black_60%)]",
    grid: "[transform:translate(-50%,-38%)_rotateX(58deg)]",
  },
};
const colors = {
  muted: "text-muted",
  primary: "text-primary",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
};
const speeds = { slow: "9s", normal: "6s", fast: "3.5s" };
const keyframes =
  "@keyframes markas-boxes-glow{0%,100%{box-shadow:none}50%{box-shadow:inset 0 0 0 999px color-mix(in oklab,currentColor 30%,transparent)}}";
const MAX_CELLS = 600;
const MAX_SIDE = 40;
const EMBER_TARGET = 6;
const EMBER_DELAY_MS = 700;

const side = (value: number) =>
  Math.max(1, Math.min(Math.trunc(value), MAX_SIDE));

/** Sel ke-i. Sekitar 6% sel (dua modulus berbeda supaya tidak membentuk garis) menyala pelan supaya latar terasa hidup tanpa hover. */
function makeCells(rows: number, cols: number) {
  const columns = side(cols);
  const total = Math.min(side(rows) * columns, MAX_CELLS);
  return Array.from({ length: total }, (_, index) => {
    const lit = ((index * 7) % 13) + ((index * index) % 11) === EMBER_TARGET;
    return lit
      ? { lit, style: { animationDelay: `${(index % 9) * -EMBER_DELAY_MS}ms` } }
      : { lit, style: undefined };
  });
}

const props = withDefaults(
  defineProps<{
    variant?: keyof typeof variants;
    rows?: number;
    cols?: number;
    size?: number;
    speed?: keyof typeof speeds;
    color?: keyof typeof colors;
  }>(),
  {
    variant: "skewed",
    rows: 12,
    cols: 18,
    size: 40,
    speed: "normal",
    color: "primary",
  },
);

const vars = computed(() => ({
  "--markas-boxes-cols": side(props.cols),
  "--markas-boxes-size": `${props.size}px`,
  "--markas-boxes-duration": speeds[props.speed],
}));
const cells = computed(() => makeCells(props.rows, props.cols));
</script>

<template>
  <div :class="base" :style="vars">
    <component :is="'style'">{{ keyframes }}</component>
    <div
      aria-hidden="true"
      :class="[layer, variants[props.variant].layer, colors[props.color]]"
    >
      <div :class="[grid, variants[props.variant].grid]">
        <span
          v-for="(item, index) in cells"
          :key="index"
          :class="[cell, item.lit && ember]"
          :style="item.style"
        />
      </div>
    </div>
    <div :class="content"><slot /></div>
  </div>
</template>
```

```vue
<template>
  <BackgroundBoxes
    variant="skewed"
    :rows="12"
    :cols="18"
    class="rounded-xl bg-surface px-6 py-16"
  >
    <h2 class="text-center text-3xl font-bold text-ink">Desain per blok</h2>
  </BackgroundBoxes>
</template>
```

### Svelte

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

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

  const base = "relative isolate overflow-hidden";
  const layer = "absolute inset-0 overflow-hidden";
  const grid =
    "absolute top-1/2 left-1/2 grid grid-cols-[repeat(var(--markas-boxes-cols),var(--markas-boxes-size))] auto-rows-[var(--markas-boxes-size)] border-t border-l border-line";
  const cell =
    "border-r border-b border-line transition-colors duration-1000 ease-out hover:bg-current/40 hover:duration-0 motion-reduce:transition-none";
  const ember =
    "animate-[markas-boxes-glow_var(--markas-boxes-duration)_ease-in-out_infinite] motion-reduce:animate-none";
  const content =
    "pointer-events-none relative [&_:is(a,button,input,select,textarea,label,summary)]:pointer-events-auto";
  const variants = {
    skewed: {
      layer:
        "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
      grid: "[transform:translate(-50%,-50%)_skewX(-48deg)_skewY(14deg)]",
    },
    flat: {
      layer:
        "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
      grid: "-translate-x-1/2 -translate-y-1/2",
    },
    perspective: {
      layer:
        "[perspective:520px] [mask-image:linear-gradient(to_bottom,transparent_25%,black_60%)]",
      grid: "[transform:translate(-50%,-38%)_rotateX(58deg)]",
    },
  };
  const colors = {
    muted: "text-muted",
    primary: "text-primary",
    success: "text-success",
    warning: "text-warning",
    danger: "text-danger",
  };
  const speeds = { slow: "9s", normal: "6s", fast: "3.5s" };
  const keyframes =
    "@keyframes markas-boxes-glow{0%,100%{box-shadow:none}50%{box-shadow:inset 0 0 0 999px color-mix(in oklab,currentColor 30%,transparent)}}";
  const MAX_CELLS = 600;
  const MAX_SIDE = 40;
  const EMBER_TARGET = 6;
  const EMBER_DELAY_MS = 700;

  const side = (value: number) =>
    Math.max(1, Math.min(Math.trunc(value), MAX_SIDE));

  /** Sel ke-i. Sekitar 6% sel (dua modulus berbeda supaya tidak membentuk garis) menyala pelan supaya latar terasa hidup tanpa hover. */
  function makeCells(rows: number, cols: number) {
    const columns = side(cols);
    const total = Math.min(side(rows) * columns, MAX_CELLS);
    return Array.from({ length: total }, (_, index) => {
      const lit = ((index * 7) % 13) + ((index * index) % 11) === EMBER_TARGET;
      return lit
        ? { lit, style: `animation-delay:${(index % 9) * -EMBER_DELAY_MS}ms` }
        : { lit, style: undefined };
    });
  }

  type Props = HTMLAttributes<HTMLDivElement> & {
    variant?: keyof typeof variants;
    rows?: number;
    cols?: number;
    size?: number;
    speed?: keyof typeof speeds;
    color?: keyof typeof colors;
    children?: Snippet;
  };

  let {
    variant = "skewed",
    rows = 12,
    cols = 18,
    size = 40,
    speed = "normal",
    color = "primary",
    class: className,
    style,
    children,
    ...rest
  }: Props = $props();

  const vars = $derived(
    `--markas-boxes-cols:${side(cols)};--markas-boxes-size:${size}px;--markas-boxes-duration:${speeds[speed]}`,
  );
  const cells = $derived(makeCells(rows, cols));
</script>

<div {...rest} class={[base, className]} style="{vars};{style ?? ''}">
  {@html `<style>${keyframes}</style>`}
  <div
    aria-hidden="true"
    class={[layer, variants[variant].layer, colors[color]]}
  >
    <div class={[grid, variants[variant].grid]}>
      {#each cells as item, index (index)}
        <span class={[cell, item.lit && ember]} style={item.style}></span>
      {/each}
    </div>
  </div>
  <div class={content}>{@render children?.()}</div>
</div>
```

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

<BackgroundBoxes
  variant="skewed"
  rows={12}
  cols={18}
  class="rounded-xl bg-surface px-6 py-16"
>
  <h2 class="text-center text-3xl font-bold text-ink">Desain per blok</h2>
</BackgroundBoxes>
```

### Astro

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

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

const base = "relative isolate overflow-hidden";
const layer = "absolute inset-0 overflow-hidden";
const grid =
  "absolute top-1/2 left-1/2 grid grid-cols-[repeat(var(--markas-boxes-cols),var(--markas-boxes-size))] auto-rows-[var(--markas-boxes-size)] border-t border-l border-line";
const cell =
  "border-r border-b border-line transition-colors duration-1000 ease-out hover:bg-current/40 hover:duration-0 motion-reduce:transition-none";
const ember =
  "animate-[markas-boxes-glow_var(--markas-boxes-duration)_ease-in-out_infinite] motion-reduce:animate-none";
const content =
  "pointer-events-none relative [&_:is(a,button,input,select,textarea,label,summary)]:pointer-events-auto";
const variants = {
  skewed: {
    layer:
      "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
    grid: "[transform:translate(-50%,-50%)_skewX(-48deg)_skewY(14deg)]",
  },
  flat: {
    layer:
      "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
    grid: "-translate-x-1/2 -translate-y-1/2",
  },
  perspective: {
    layer:
      "[perspective:520px] [mask-image:linear-gradient(to_bottom,transparent_25%,black_60%)]",
    grid: "[transform:translate(-50%,-38%)_rotateX(58deg)]",
  },
};
const colors = {
  muted: "text-muted",
  primary: "text-primary",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
};
const speeds = { slow: "9s", normal: "6s", fast: "3.5s" };
const keyframes =
  "@keyframes markas-boxes-glow{0%,100%{box-shadow:none}50%{box-shadow:inset 0 0 0 999px color-mix(in oklab,currentColor 30%,transparent)}}";
const MAX_CELLS = 600;
const MAX_SIDE = 40;
const EMBER_TARGET = 6;
const EMBER_DELAY_MS = 700;

const side = (value: number) =>
  Math.max(1, Math.min(Math.trunc(value), MAX_SIDE));

/** Sel ke-i. Sekitar 6% sel (dua modulus berbeda supaya tidak membentuk garis) menyala pelan supaya latar terasa hidup tanpa hover. */
function makeCells(rows: number, cols: number) {
  const columns = side(cols);
  const total = Math.min(side(rows) * columns, MAX_CELLS);
  return Array.from({ length: total }, (_, index) => {
    const lit = ((index * 7) % 13) + ((index * index) % 11) === EMBER_TARGET;
    return lit
      ? { lit, style: `animation-delay:${(index % 9) * -EMBER_DELAY_MS}ms` }
      : { lit, style: undefined };
  });
}

type Props = Omit<HTMLAttributes<"div">, "style"> & {
  variant?: keyof typeof variants;
  rows?: number;
  cols?: number;
  size?: number;
  speed?: keyof typeof speeds;
  color?: keyof typeof colors;
  style?: string;
};

const {
  variant = "skewed",
  rows = 12,
  cols = 18,
  size = 40,
  speed = "normal",
  color = "primary",
  class: className,
  style,
  ...rest
} = Astro.props;
const vars = `--markas-boxes-cols:${side(cols)};--markas-boxes-size:${size}px;--markas-boxes-duration:${speeds[speed]}`;
---

<div {...rest} class:list={[base, className]} style={`${vars};${style ?? ""}`}>
  <style is:inline set:html={keyframes} />
  <div
    aria-hidden="true"
    class:list={[layer, variants[variant].layer, colors[color]]}
  >
    <div class:list={[grid, variants[variant].grid]}>
      {
        makeCells(rows, cols).map((item) => (
          <span class:list={[cell, item.lit && ember]} style={item.style} />
        ))
      }
    </div>
  </div>
  <div class={content}><slot /></div>
</div>
```

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

<BackgroundBoxes
  variant="skewed"
  rows={12}
  cols={18}
  class="rounded-xl bg-surface px-6 py-16"
>
  <h2 class="text-center text-3xl font-bold text-ink">Desain per blok</h2>
</BackgroundBoxes>
```

Tanpa JavaScript di browser; markup dirender di server.

### Angular

Simpan di `src/app/markas/background-boxes.component.ts`. Tanpa dependensi tambahan.

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

const base = "relative isolate overflow-hidden";
const layer = "absolute inset-0 overflow-hidden";
const grid =
  "absolute top-1/2 left-1/2 grid grid-cols-[repeat(var(--markas-boxes-cols),var(--markas-boxes-size))] auto-rows-[var(--markas-boxes-size)] border-t border-l border-line";
const cell =
  "border-r border-b border-line transition-colors duration-1000 ease-out hover:bg-current/40 hover:duration-0 motion-reduce:transition-none";
const ember =
  "animate-[markas-boxes-glow_var(--markas-boxes-duration)_ease-in-out_infinite] motion-reduce:animate-none";
const content =
  "pointer-events-none relative [&_:is(a,button,input,select,textarea,label,summary)]:pointer-events-auto";
const variants = {
  skewed: {
    layer:
      "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
    grid: "[transform:translate(-50%,-50%)_skewX(-48deg)_skewY(14deg)]",
  },
  flat: {
    layer:
      "[mask-image:radial-gradient(ellipse_at_center,black_40%,transparent_85%)]",
    grid: "-translate-x-1/2 -translate-y-1/2",
  },
  perspective: {
    layer:
      "[perspective:520px] [mask-image:linear-gradient(to_bottom,transparent_25%,black_60%)]",
    grid: "[transform:translate(-50%,-38%)_rotateX(58deg)]",
  },
};
const colors = {
  muted: "text-muted",
  primary: "text-primary",
  success: "text-success",
  warning: "text-warning",
  danger: "text-danger",
};
const speeds = { slow: "9s", normal: "6s", fast: "3.5s" };
const keyframes =
  "@keyframes markas-boxes-glow{0%,100%{box-shadow:none}50%{box-shadow:inset 0 0 0 999px color-mix(in oklab,currentColor 30%,transparent)}}";
const MAX_CELLS = 600;
const MAX_SIDE = 40;
const EMBER_TARGET = 6;
const EMBER_DELAY_MS = 700;

export type BackgroundBoxesVariant = keyof typeof variants;
export type BackgroundBoxesSpeed = keyof typeof speeds;
export type BackgroundBoxesColor = keyof typeof colors;

const side = (value: number) =>
  Math.max(1, Math.min(Math.trunc(value), MAX_SIDE));

/** Sel ke-i. Sekitar 6% sel (dua modulus berbeda supaya tidak membentuk garis) menyala pelan supaya latar terasa hidup tanpa hover. */
function makeCells(rows: number, cols: number) {
  const columns = side(cols);
  const total = Math.min(side(rows) * columns, MAX_CELLS);
  return Array.from({ length: total }, (_, index) => {
    const lit = ((index * 7) % 13) + ((index * index) % 11) === EMBER_TARGET;
    return lit
      ? {
          className: `${cell} ${ember}`,
          style: `animation-delay:${(index % 9) * -EMBER_DELAY_MS}ms`,
        }
      : { className: cell, style: null };
  });
}

/**
 * Attribute selector on a native div: the user's class, style, and attributes stay on the
 * root. The keyframes are a global style, so encapsulation is off.
 */
@Component({
  selector: "div[markas-background-boxes]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  encapsulation: ViewEncapsulation.None,
  styles: [keyframes],
  host: {
    "[class]": "base",
    "[style.--markas-boxes-cols]": "columns()",
    "[style.--markas-boxes-size]": "size() + 'px'",
    "[style.--markas-boxes-duration]": "duration()",
  },
  template: `
    <div aria-hidden="true" [class]="layerClasses()">
      <div [class]="gridClasses()">
        @for (item of cells(); track $index) {
          <span [class]="item.className" [style]="item.style"></span>
        }
      </div>
    </div>
    <div [class]="content"><ng-content /></div>
  `,
})
export class BackgroundBoxes {
  readonly variant = input<BackgroundBoxesVariant>("skewed");
  readonly rows = input(12, { transform: numberAttribute });
  readonly cols = input(18, { transform: numberAttribute });
  readonly size = input(40, { transform: numberAttribute });
  readonly speed = input<BackgroundBoxesSpeed>("normal");
  readonly color = input<BackgroundBoxesColor>("primary");

  protected readonly base = base;
  protected readonly content = content;
  protected readonly columns = computed(() => side(this.cols()));
  protected readonly duration = computed(() => speeds[this.speed()]);
  protected readonly layerClasses = computed(
    () => `${layer} ${variants[this.variant()].layer} ${colors[this.color()]}`,
  );
  protected readonly gridClasses = computed(
    () => `${grid} ${variants[this.variant()].grid}`,
  );
  protected readonly cells = computed(() =>
    makeCells(this.rows(), this.cols()),
  );
}
```

```ts
import { Component } from "@angular/core";
import { BackgroundBoxes } from "./markas/background-boxes.component";

@Component({
  selector: "app-hero",
  imports: [BackgroundBoxes],
  template: `
    <div
      markas-background-boxes
      variant="skewed"
      [rows]="12"
      [cols]="18"
      class="rounded-xl bg-surface px-6 py-16"
    >
      <h2 class="text-center text-3xl font-bold text-ink">Desain per blok</h2>
    </div>
  `,
})
export class Hero {}
```

Komponen ini berupa atribut pada div, jadi class, style, dan atribut lain tetap di elemen akar.

## Props

| Nama | Tipe | Default | Keterangan |
| --- | --- | --- | --- |
| `variant` | `skewed \| flat \| perspective` | skewed | skewed memiringkan grid seperti lantai isometrik, flat membuatnya lurus, perspective merebahkannya ke arah cakrawala. |
| `rows` | `number` | 12 | Jumlah baris, maksimal 40. Grid berhenti di 600 kotak. |
| `cols` | `number` | 18 | Jumlah kolom, maksimal 40. |
| `size` | `number` | 40 | Ukuran kotak dalam piksel. |
| `speed` | `slow \| normal \| fast` | normal | Siklus beberapa kotak yang menyala sendiri: 9s, 6s, atau 3.5s. |
| `color` | `muted \| primary \| success \| warning \| danger` | primary | Warna token saat kotak menyala. |
| `children / slot` | `slot / ReactNode / snippet` | - | Konten yang tampil di atas grid. |

## Aksesibilitas

- Grid memakai aria-hidden dan kotaknya tidak bisa difokus.
- Konten meneruskan pointer ke grid, kecuali tautan, tombol, label, dan kolom form yang tetap bisa diklik.
- Dengan reduced motion, kotak berhenti menyala sendiri dan berganti warna tanpa transisi.

## Catatan

- Kotak menyala dengan CSS `:hover` biasa dan memudar dalam satu detik. Di layar sentuh, beberapa kotak tetap menyala sendiri.
- Pembungkus konten meneruskan pointer ke grid. Tautan, tombol, label, dan kolom form tetap bisa diklik; teks di dalam konten tidak bisa dipilih dengan mouse.
- Jaga rows kali cols tetap kecil: grid berhenti di 600 kotak.
- Komponen ini tidak punya teks bawaan, jadi tidak ada prop label yang perlu diterjemahkan.
