# Floating action button

Keeps one task reachable over scrolling content, as a button or a link, collapsing to its icon on phones.

- Family: action
- Import: `import { Fab } from "@senvenli/react/action";`
- Page: https://design.senvenli.com/docs/components/fab
- JSON: https://design.senvenli.com/docs/components/fab.json

## Example: A task kept in reach

Fixed to the bottom corner by default; `placement="inline"` keeps it in the flow, as here.

```tsx
import { Fab } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

export default function Example() {
  const [orders, setOrders] = useState(0);
  return (
    <Stack direction="horizontal" space="space-200">
      {/* In an app, leave `placement` out: the button floats above the page's bottom corner. */}
      <Fab iconBefore="plus" placement="inline" onPress={() => setOrders(orders + 1)}>New order</Fab>
      <Text tone="subtle">Orders started: {orders}</Text>
    </Stack>
  );
}
```

## Examples

### A link

With `href` it opens a contact or chat page, in a new tab unless `target` says otherwise.

```tsx
import { Fab } from "@senvenli/react/action";

export default function Example() {
  return <Fab iconBefore="message-circle" href="https://example.com/contact" placement="inline">Contact us</Fab>;
}
```

### Icon only

`iconOnly`: a round button at every width; `label` stays its accessible name.

```tsx
import { Fab } from "@senvenli/react/action";

export default function Example() {
  // Round and icon-only at every width; the label is still its name for screen readers and its tooltip.
  return <Fab iconBefore="plus" iconOnly placement="inline" onPress={() => {}}>New order</Fab>;
}
```

## Guidelines

### When to use it

- One main task must stay reachable while people scroll: contact, new item.

### Use something else when

- [button](https://design.senvenli.com/docs/components/button.md): The action fits in the page header or a toolbar and need not float.
- [bottom-nav](https://design.senvenli.com/docs/components/bottom-nav.md): People switch between top destinations rather than start a task.

### Do and avoid

- At most one per screen; never over a dialog or sheet.

## Accessibility

Built in:

- Keeps its label as the accessible name when collapsed to an icon.
- Links that open a new tab say so.

You provide:

- A verb label for the task.

## API reference

### Fab

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `string` |  | yes | The task, as a verb ("Contact us", "New order"), as Button's label; also the name when icon-only. |
| iconBefore | `IconName` |  | yes | Lucide icon name before the label ("message-circle", "calendar-plus"), as Button's `iconBefore`. |
| onPress | `(event: PressEvent) => void` |  | no |  |
| href | `string` |  | no | A link (a chat or contact URL) instead of an action; it opens in a new tab unless `target` says otherwise. |
| target | `HTMLAttributeAnchorTarget` | `"_blank"` | no | Where `href` opens. `_blank` (default) follows Link's external convention: `noopener noreferrer` and the new-tab words for screen readers. `_self` keeps an in-app link in the page, through the host router. |
| placement | `"inline" \| "fixed"` | `"fixed"` | no | `fixed` (default): bottom-end above the safe area. `inline`: in the flow (previews, docs). |
| collapseOnScroll | `boolean` | `true` | no | Collapse to icon-only on phones after the first scroll (default true). |
| iconOnly | `boolean` | `false` | no | Icon-only at every width, as Button's `iconOnly`: a round button; `label` stays its accessible name. |

### Tokens it reads

`--ds-background-brand-bold`, `--ds-background-brand-bold-hovered`, `--ds-background-brand-bold-pressed`, `--ds-easing-standard`, `--ds-radius-full`, `--ds-shadow-overlay`, `--ds-space-100`, `--ds-space-150`, `--ds-space-250`, `--ds-space-300`, `--ds-text-on-brand`

## Related

- [button](https://design.senvenli.com/docs/components/button.md)
