# Kosong, memuat, dan error

> Yang tampil di halaman saat belum ada isi, saat sedang memuat, dan saat gagal memuat.

Selain "terisi", setiap list dan kartu punya tiga state lagi. Tampilkan state itu di tempat isinya seharusnya muncul, dengan ukuran yang sama, supaya halaman tidak melompat.

## Kapan dipakai

- **Kosong:** kumpulan datanya belum ada isinya, atau tidak ada baris yang cocok dengan filter.
- **Memuat:** data sedang diambil. Tampilkan skeleton yang bentuknya mengikuti isi.
- **Error:** data gagal dimuat. Sebutkan apa yang gagal dan beri cara untuk mencoba lagi.

## Kosong

_Pratinjau langsung versi React dan Vue: https://design.sosialoka.tech/id/patterns/states/_

Isinya judul, satu kalimat tentang apa yang nanti muncul, dan satu aksi untuk mengisinya. Kalau filter menyembunyikan semua baris, aksinya adalah menghapus filter.

## Memuat

_Pratinjau langsung versi React dan Vue: https://design.sosialoka.tech/id/patterns/states/_

Isinya skeleton yang bentuknya mengikuti baris yang akan muncul, `aria-busy="true"` di wadahnya, dan satu kalimat untuk screen reader. Kalau proses memuat lebih cepat dari sekitar 300 ms, skeleton tidak perlu ditampilkan.

**React**
```tsx
import { Skeleton } from "@/components/ui/skeleton"

/** Loading: skeletons in the shape of the rows that are coming, and a sentence for screen readers. */
export default function LoadingStateDemo() {
  return (
    <div aria-busy="true" className="grid w-full max-w-md gap-4 rounded-xl border border-border bg-card p-4">
      <span className="sr-only">Memuat rilis…</span>
      {[0, 1, 2].map((row) => (
        <div key={row} className="flex items-center gap-3" aria-hidden="true">
          <Skeleton className="size-10 rounded-md" />
          <div className="grid flex-1 gap-2">
            <Skeleton className="h-4 w-2/3" />
            <Skeleton className="h-3 w-1/3" />
          </div>
        </div>
      ))}
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { Skeleton } from "@/components/ui/skeleton"

/** Loading: skeletons in the shape of the rows that are coming, and a sentence for screen readers. */
</script>

<template>
  <div aria-busy="true" class="grid w-full max-w-md gap-4 rounded-xl border border-border bg-card p-4">
    <span class="sr-only">Memuat rilis…</span>
    <div v-for="row in 3" :key="row" class="flex items-center gap-3" aria-hidden="true">
      <Skeleton class="size-10 rounded-md" />
      <div class="grid flex-1 gap-2">
        <Skeleton class="h-4 w-2/3" />
        <Skeleton class="h-3 w-1/3" />
      </div>
    </div>
  </div>
</template>
```

**Flutter**
```dart
// Loading: placeholder bars in the shape of the rows that are coming, and a label for screen readers.
// shadcn_ui has no skeleton widget; muted boxes with the theme radius do the job.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class LoadingStateDemo extends StatelessWidget {
  const LoadingStateDemo({super.key});

  @override
  Widget build(BuildContext context) {
    final muted = ShadTheme.of(context).colorScheme.muted;
    Widget bar(double width, double height) => Container(
      width: width,
      height: height,
      decoration: BoxDecoration(color: muted, borderRadius: BorderRadius.circular(6)),
    );
    return Semantics(
      label: 'Memuat rilis…',
      child: ExcludeSemantics(
        child: ShadCard(
          width: 448,
          child: Column(
            children: [
              for (var row = 0; row < 3; row++)
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 8),
                  child: Row(
                    children: [
                      bar(40, 40),
                      const SizedBox(width: 12),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [bar(240, 14), const SizedBox(height: 8), bar(120, 12)],
                      ),
                    ],
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}
```

## Error

_Pratinjau langsung versi React dan Vue: https://design.sosialoka.tech/id/patterns/states/_

Isinya `role="alert"`, apa yang gagal, apa yang perlu dilakukan, dan tombol "Coba lagi". Kalau backend mengirim pesan, pakai pesan itu.

**React**
```tsx
import { RefreshCwIcon, TriangleAlertIcon } from "lucide-react"
import { Button } from "@/components/ui/button"

/** Loading failed: what failed, what to do next, and a way to try again. */
export default function ErrorStateDemo() {
  return (
    <div
      role="alert"
      className="flex w-full max-w-md flex-col items-center gap-4 rounded-xl border border-border bg-card px-6 py-10 text-center"
    >
      <span className="grid size-12 place-items-center rounded-lg bg-destructive-soft text-destructive">
        <TriangleAlertIcon className="size-6" />
      </span>
      <div className="grid gap-1">
        <h2 className="text-lg font-semibold">Rilis belum bisa dimuat</h2>
        <p className="text-sm text-muted-foreground">Cek koneksimu, lalu coba lagi.</p>
      </div>
      <Button variant="outline">
        <RefreshCwIcon />
        Coba lagi
      </Button>
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { RefreshCw, TriangleAlert } from "@lucide/vue"
import { Button } from "@/components/ui/button"

/** Loading failed: what failed, what to do next, and a way to try again. */
</script>

<template>
  <div role="alert" class="flex w-full max-w-md flex-col items-center gap-4 rounded-xl border border-border bg-card px-6 py-10 text-center">
    <span class="grid size-12 place-items-center rounded-lg bg-destructive-soft text-destructive">
      <TriangleAlert class="size-6" />
    </span>
    <div class="grid gap-1">
      <h2 class="text-lg font-semibold">Rilis belum bisa dimuat</h2>
      <p class="text-sm text-muted-foreground">Cek koneksimu, lalu coba lagi.</p>
    </div>
    <Button variant="outline"><RefreshCw />Coba lagi</Button>
  </div>
</template>
```

**Flutter**
```dart
// Loading failed: what failed, what to do next, and a way to try again. Announced as a live region.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:sosialoka_ui/sosialoka_ui.dart';

class ErrorStateDemo extends StatelessWidget {
  const ErrorStateDemo({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    final c = context.sosialoka;
    return Semantics(
      liveRegion: true,
      child: ConstrainedBox(
        constraints: const BoxConstraints(maxWidth: 448),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              width: 48,
              height: 48,
              decoration: BoxDecoration(color: c.destructiveSoft, borderRadius: BorderRadius.circular(8)),
              child: Icon(LucideIcons.triangleAlert, color: c.destructive),
            ),
            const SizedBox(height: 16),
            Text('Rilis belum bisa dimuat', style: theme.textTheme.large),
            const SizedBox(height: 4),
            Text('Cek koneksimu, lalu coba lagi.', style: theme.textTheme.muted),
            const SizedBox(height: 16),
            ShadButton.outline(
              leading: const Icon(LucideIcons.refreshCw, size: 16),
              onPressed: () {},
              child: const Text('Coba lagi'),
            ),
          ],
        ),
      ),
    );
  }
}
```

## Kode state kosong

**React**
```tsx
import { DiscIcon, PlusIcon } from "lucide-react"
import { Button } from "@/components/ui/button"

/** Nothing here yet: what will appear, in one sentence, and the one action that fills it. */
export default function EmptyStateDemo() {
  return (
    <div className="flex w-full max-w-md flex-col items-center gap-4 rounded-xl border border-dashed border-border px-6 py-10 text-center">
      <span className="grid size-12 place-items-center rounded-lg bg-muted text-muted-foreground">
        <DiscIcon className="size-6" />
      </span>
      <div className="grid gap-1">
        <h2 className="text-lg font-semibold">Belum ada rilis</h2>
        <p className="text-sm text-muted-foreground">Rilis pertamamu akan muncul di sini.</p>
      </div>
      <Button>
        <PlusIcon />
        Rilis baru
      </Button>
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { Disc, Plus } from "@lucide/vue"
import { Button } from "@/components/ui/button"

/** Nothing here yet: what will appear, in one sentence, and the one action that fills it. */
</script>

<template>
  <div class="flex w-full max-w-md flex-col items-center gap-4 rounded-xl border border-dashed border-border px-6 py-10 text-center">
    <span class="grid size-12 place-items-center rounded-lg bg-muted text-muted-foreground">
      <Disc class="size-6" />
    </span>
    <div class="grid gap-1">
      <h2 class="text-lg font-semibold">Belum ada rilis</h2>
      <p class="text-sm text-muted-foreground">Rilis pertamamu akan muncul di sini.</p>
    </div>
    <Button><Plus />Rilis baru</Button>
  </div>
</template>
```

**Flutter**
```dart
// Nothing here yet: what will appear, in one sentence, and the one action that fills it.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class EmptyStateDemo extends StatelessWidget {
  const EmptyStateDemo({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    return ConstrainedBox(
      constraints: const BoxConstraints(maxWidth: 448),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 48,
            height: 48,
            decoration: BoxDecoration(color: theme.colorScheme.muted, borderRadius: BorderRadius.circular(8)),
            child: Icon(LucideIcons.disc, color: theme.colorScheme.mutedForeground),
          ),
          const SizedBox(height: 16),
          Text('Belum ada rilis', style: theme.textTheme.large),
          const SizedBox(height: 4),
          Text('Rilis pertamamu akan muncul di sini.', style: theme.textTheme.muted),
          const SizedBox(height: 16),
          ShadButton(
            leading: const Icon(LucideIcons.plus, size: 16),
            onPressed: () {},
            child: const Text('Rilis baru'),
          ),
        ],
      ),
    );
  }
}
```

## Dibangun dari

[Button](https://design.sosialoka.tech/id/components/button/) dan Skeleton, yang ikut terpasang bersama [Sidebar](https://design.sosialoka.tech/id/components/sidebar/).

## Flutter

shadcn_ui tidak punya widget skeleton. Kotak berwarna muted dengan radius tema sudah cukup. Bungkus state error dengan `Semantics(liveRegion: true)`.
