EmptyState
Nothing to show yet, said in words, with the next step as a button or a link.
import { EmptyState } from '@nyxon/nasim';
const copy = {
title: 'هنوز سرویسی نخریدهاید',
hint: 'سرویسهایی که بخرید اینجا با زمان و حجم باقیماندهشان دیده میشوند.',
browse: 'دیدن پلنها',
};
// An empty list always offers the next step. With an `href` action it renders on the server.
export default function LinkAction() {
return (
<EmptyState
title={copy.title}
description={copy.hint}
action={{ label: copy.browse, href: '#plans' }}
/>
);
}Import
import { EmptyState } from '@nyxon/nasim';Usage
A title, an optional description (what the list is for, or what the person can do about it being
empty; it may be a node) and an action. action is { label, href } (a LinkButton) or
{ label, onPress } (a Button), or your own element when the next step needs more than one button.
icon replaces the default one.
With an href action, or none, it renders in a Server Component. An onPress action is a function, so
that one is rendered from a client component.
<EmptyState
title={t.services.emptyTitle}
description={t.services.emptyHint}
action={{ label: t.shop.browse, href: '/shop' }}
/>Examples
A link to the next step
Shown above: an empty list of services, and the way to buy one.
Nothing matches the filters
The next step is to clear them: a handler, in a client component.
'use client';
import { EmptyState } from '@nyxon/nasim';
import { useState } from 'react';
const copy = {
title: 'سفارشی با این فیلترها نیست',
hint: 'فیلتر «پرداختنشده» و بازهٔ «این هفته» روشناند.',
clear: 'پاک کردن فیلترها',
cleared: 'فیلترها پاک شدند.',
};
// Nothing matches the filters: the action is a handler, so it needs a client component.
export default function Filtered() {
const [cleared, setCleared] = useState(false);
if (cleared) return <p className="text-center text-muted">{copy.cleared}</p>;
return (
<EmptyState
title={copy.title}
description={copy.hint}
action={{ label: copy.clear, onPress: () => setCleared(true) }}
/>
);
}Rules
- An empty screen always offers the next step.
- Say why it is empty in the screen's words: «هنوز سرویسی نخریدهاید» is not «فیلتر نتیجهای نداشت».
Accessibility
The title is an h2; pass as="h1" when the state is the whole page. When a screen swaps this state in
(a wait ended with nothing), pass titleRef and move the focus to it: the title becomes focusable, so
the keyboard is where the news is and a screen reader hears it. The icon is decorative and hidden.
API
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | required | |
description | ReactNode | — | What this list is for, or what the person can do about it being empty. |
icon | ReactNode | — | |
as | "h1" | "h2" | — | Heading level: "h1" when the state is the whole page, "h2" (default) inside one. |
titleRef | Ref<HTMLHeadingElement> | — | A ref to the title, which takes focus when a screen swaps this state in. |
action | ReactElement<unknown, string | React.JSXElementConstructor<any>> | ({ label: string; } & ({ href: string; } | { onPress: () => void; })) | — | The next step: a link into the app, a handler, or your own element when it needs more than a button. |
Related
ErrorState (the request failed) and LoadingState (still waiting).