# Button

> HeroUI's button, re-exported through Nasim with its variants, sizes and pending state.

- Import: `import { Button } from '@nyxon/nasim';`
- Server Component: no, it needs a client component
- Since: 0.1.0; status: stable
- Source: libs/nasim/src/primitives/index.ts
- Describes: @nyxon/nasim unreleased, after 0.12.0
- Page: https://nasim.nyxon.tech/docs/components/actions/button

<Example name="button/variants">
  ```tsx title="button/variants.tsx"
  import { Button } from '@nyxon/nasim';

  // One screen's actions, from the main one to the one that destroys something.
  const copy = {
    order: 'ثبت سفارش',
    preview: 'پیش‌نمایش فاکتور',
    cancel: 'انصراف',
    export: 'خروجی اکسل',
    more: 'گزینه‌های بیشتر',
    remove: 'حذف سرویس',
  };

  export default function Variants() {
    return (
      <div className="flex flex-wrap gap-2">
        <Button variant="primary">{copy.order}</Button>
        <Button variant="secondary">{copy.preview}</Button>
        <Button variant="tertiary">{copy.cancel}</Button>
        <Button variant="outline">{copy.export}</Button>
        <Button variant="ghost">{copy.more}</Button>
        <Button variant="danger">{copy.remove}</Button>
      </div>
    );
  }
  ```
</Example>

## Import [#import]

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

## Usage [#usage]

`Button` is HeroUI's, re-exported unchanged; HeroUI's documentation for 3.2.8 applies. It is a client
component: a Server Component may render it with plain props, and a handler such as `onPress` is passed
from a client component. Its words come from the screen's strings module.

```tsx
<Button onPress={save}>{t.settings.save}</Button>
```

## Examples [#examples]

### Variants [#variants]

`primary` for the one main action of a screen, `secondary` and `tertiary` beside it, `outline` and
`ghost` for quieter ones, `danger` for an action that destroys something (shown above).

### Sizes [#sizes]

`lg` is the main action of a phone screen: 44px tall on a phone, 40px on a desktop.

<Example name="button/sizes">
  ```tsx title="button/sizes.tsx"
  import { Button } from '@nyxon/nasim';

  const copy = { small: 'کوچک', medium: 'معمولی', large: 'پرداخت ۴۵۰٬۰۰۰ تومان' };

  // `lg` is the main action of a phone screen: 44px tall on a phone, 40px on a desktop.
  export default function Sizes() {
    return (
      <div className="flex flex-wrap items-center gap-2">
        <Button size="sm" variant="secondary">
          {copy.small}
        </Button>
        <Button size="md" variant="secondary">
          {copy.medium}
        </Button>
        <Button size="lg">{copy.large}</Button>
      </div>
    );
  }
  ```
</Example>

### Pending [#pending]

While a request runs, `isPending` keeps the button from a second press. Pass a function as the child to
show a `Spinner` in it: `<Spinner color="current" size="sm" />`.

<Example name="button/pending">
  ```tsx title="button/pending.tsx"
  'use client';

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

  const copy = { save: 'ذخیرهٔ تغییرات' };

  // While a request runs the button shows a spinner and takes no second press.
  export default function Pending() {
    const [isPending, setPending] = useState(false);
    const save = () => {
      setPending(true);
      setTimeout(() => setPending(false), 2000);
    };
    return (
      <Button isPending={isPending} onPress={save}>
        {({ isPending: busy }) => (
          <>
            {busy ? <Spinner color="current" size="sm" /> : null}
            {copy.save}
          </>
        )}
      </Button>
    );
  }
  ```
</Example>

## Rules [#rules]

* Name the action («ذخیرهٔ تغییرات», «حذف سرویس»), not just «تأیید».
* One `primary` per screen or dialog; in a footer the cancel comes first and the main action last.
* A link that looks like a button is a `LinkButton`, not a `Button` that navigates.
* In a form, the submit is `SubmitButton`, which blocks a second submit itself.

## Accessibility [#accessibility]

It renders a `<button>` with React Aria's press handling: Enter and Space press it, and a pending button
takes no press. An icon-only button (`isIconOnly`) has no words, so it needs an `aria-label` from the
screen's strings. On a touch screen an icon-only button keeps its look but gets a 44px hit area
(`tokens.css`).

## API [#api]

| Prop         | Type                                                                                          | Default               | Description                                                                                                                                                                           |
| ------------ | --------------------------------------------------------------------------------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `variant`    | `"danger" \| "danger-soft" \| "ghost" \| "outline" \| "primary" \| "secondary" \| "tertiary"` | —                     |                                                                                                                                                                                       |
| `size`       | `"lg" \| "md" \| "sm"`                                                                        | —                     |                                                                                                                                                                                       |
| `fullWidth`  | `boolean`                                                                                     | —                     |                                                                                                                                                                                       |
| `isIconOnly` | `boolean`                                                                                     | —                     |                                                                                                                                                                                       |
| `isPending`  | `boolean`                                                                                     | —                     | Whether the button is in a pending state. This disables press and hover events while retaining focusability, and announces the pending state to screen readers.                       |
| `isDisabled` | `boolean`                                                                                     | —                     | Whether the button is disabled.                                                                                                                                                       |
| `onPress`    | `((e: PressEvent) => void)`                                                                   | —                     | Handler that is called when the press is released over the target.                                                                                                                    |
| `type`       | `"button" \| "reset" \| "submit"`                                                             | `'button'`            | The behavior of the button when used in an HTML form.                                                                                                                                 |
| `children`   | `ChildrenOrFunction<ButtonRenderProps>`                                                       | —                     | The children of the component. A function may be provided to alter the children based on component state.                                                                             |
| `className`  | `ClassNameOrFunction<ButtonRenderProps>`                                                      | `'react-aria-Button'` | The CSS \[className]\(https\://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state. |

These are the props this page uses. `Button` takes the rest of React Aria's button props too (`aria-*`,
`form`, the press and focus events); HeroUI's documentation lists them.

## Related [#related]

`LinkButton` (a link styled as a button), `SubmitButton` (a form's submit) and `Spinner` (the pending
mark, labelled in Persian).