# Notice

> An inline message with its icon, a title and the words, in one of five tones.

- Import: `import { Notice } from '@nyxon/nasim';`
- Server Component: no, it needs a client component
- Since: 0.5.0; status: stable
- Source: libs/nasim/src/composites/Notice.tsx
- Describes: @nyxon/nasim unreleased, after 0.12.0
- Page: https://nasim.nyxon.tech/docs/components/feedback/notice

<Example name="notice/tones">
  ```tsx title="notice/tones.tsx"
  import { Notice, type NoticeTone } from '@nyxon/nasim';

  // What a settings page says about the shop's payment gateway, in each tone.
  const notices: { tone: NoticeTone; title: string; body: string }[] = [
    {
      tone: 'info',
      title: 'درگاه در حالت آزمایشی است',
      body: 'پرداخت‌ها ثبت می‌شوند، اما پولی جابه‌جا نمی‌شود.',
    },
    {
      tone: 'success',
      title: 'درگاه وصل شد',
      body: 'از این لحظه مشتری‌ها می‌توانند آنلاین پرداخت کنند.',
    },
    {
      tone: 'warning',
      title: 'گواهی درگاه تا ۵ روز دیگر تمام می‌شود',
      body: 'پیش از آن گواهی تازه را بارگذاری کنید.',
    },
    {
      tone: 'danger',
      title: 'اتصال به درگاه قطع است',
      body: 'سفارش‌های تازه تا وصل شدن دوباره منتظر می‌مانند.',
    },
    {
      tone: 'neutral',
      title: 'درگاه دوم تعریف نشده است',
      body: 'اگر درگاه اول قطع شود، پرداختی ممکن نیست.',
    },
  ];

  export default function Tones() {
    return (
      <div className="flex flex-col gap-3">
        {notices.map(({ tone, title, body }) => (
          <Notice key={tone} tone={tone} title={title}>
            {body}
          </Notice>
        ))}
      </div>
    );
  }
  ```
</Example>

## Import [#import]

```tsx
import { Notice } from '@nyxon/nasim';
```

## Usage [#usage]

HeroUI's alert with its icon, a `title` and the words as children. `tone` is `"info"` (the default),
`"success"`, `"warning"`, `"danger"` or `"neutral"`. It is a client component; a Server Component
renders it with plain props.

```tsx
<Notice tone="warning" title={t.limits.title}>
  {t.limits.body}
</Notice>
```

## Examples [#examples]

### Tones [#tones]

Shown above: what a settings page says about a payment gateway, in each tone.

### After an action [#after-an-action]

A failure that appears in answer to a press gets `role="alert"`, so it is read out the moment it appears.
Good news that appears that way gets `role="status"`.

<Example name="notice/after-action">
  ```tsx title="notice/after-action.tsx"
  'use client';

  import { Button, Notice } from '@nyxon/nasim';
  import { useState } from 'react';

  const copy = {
    save: 'ذخیرهٔ شماره کارت',
    failed: 'ذخیره نشد',
    why: 'شماره کارت ۱۶ رقم است؛ این شماره ۱۵ رقم دارد.',
  };

  // A failure that appears in answer to a press is role="alert": it is read out the moment it appears.
  export default function AfterAction() {
    const [failed, setFailed] = useState(false);
    return (
      <div className="flex flex-col items-start gap-3">
        <Button onPress={() => setFailed(true)}>{copy.save}</Button>
        {failed ? (
          <Notice tone="danger" title={copy.failed} role="alert">
            {copy.why}
          </Notice>
        ) : null}
      </div>
    );
  }
  ```
</Example>

## Rules [#rules]

* The title says what happened, the body what to do next. Never colour alone: the icon and the words
  carry the tone.
* A notice that is on the page from the start takes no `role`: it is read in order with the page.

## Accessibility [#accessibility]

Without `role` it is plain content. `role="alert"` is announced at once and interrupts; keep it for a
failure the person caused just now. `role="status"` is announced politely, after what is being read.

## API [#api]

| Prop        | Type                                                        | Default | Description                                                                        |
| ----------- | ----------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------- |
| `tone`      | `"danger" \| "info" \| "neutral" \| "success" \| "warning"` | —       |                                                                                    |
| `title`     | `ReactNode`                                                 | —       |                                                                                    |
| `children`  | `ReactNode`                                                 | —       |                                                                                    |
| `role`      | `"alert" \| "status"`                                       | —       | "alert" for a failure that appears in answer to an action, "status" for good news. |
| `className` | `string`                                                    | —       |                                                                                    |

`NoticeTone` is exported too.

## Related [#related]

`StatusChip` (a status in a word or two), `MessageBar` and `ErrorState` (a whole view that failed to
load).