# ActionBar
Package: @mantine/core
Import: import { ActionBar } from '@mantine/core';
Description: A fixed-position bottom bar for bulk selection actions

## Usage

`ActionBar` is a bottom bar with bulk actions that appears when items are selected.
Use `ActionBar.Divider` to separate groups of actions and `ActionBar.CloseButton`
to add a close button that calls `onClose`:

```tsx
import { useState } from 'react';
import { ActionBar, Button, Checkbox, Table, Text } from '@mantine/core';

const elements = [
  { position: 6, mass: 12.011, symbol: 'C', name: 'Carbon' },
  { position: 7, mass: 14.007, symbol: 'N', name: 'Nitrogen' },
  { position: 39, mass: 88.906, symbol: 'Y', name: 'Yttrium' },
  { position: 56, mass: 137.33, symbol: 'Ba', name: 'Barium' },
  { position: 58, mass: 140.12, symbol: 'Ce', name: 'Cerium' },
];

function Demo() {
  const [selection, setSelection] = useState<number[]>([]);

  const toggleRow = (position: number) =>
    setSelection((current) =>
      current.includes(position)
        ? current.filter((item) => item !== position)
        : [...current, position]
    );

  const toggleAll = () =>
    setSelection((current) =>
      current.length === elements.length ? [] : elements.map((element) => element.position)
    );

  const rows = elements.map((element) => (
    <Table.Tr
      key={element.position}
      bg={selection.includes(element.position) ? 'var(--mantine-color-blue-light)' : undefined}
    >
      <Table.Td>
        <Checkbox
          aria-label="Select row"
          checked={selection.includes(element.position)}
          onChange={() => toggleRow(element.position)}
        />
      </Table.Td>
      <Table.Td>{element.position}</Table.Td>
      <Table.Td>{element.name}</Table.Td>
      <Table.Td>{element.symbol}</Table.Td>
      <Table.Td>{element.mass}</Table.Td>
    </Table.Tr>
  ));

  return (
    <>
      <Table>
        <Table.Thead>
          <Table.Tr>
            <Table.Th>
              <Checkbox
                aria-label="Select all"
                checked={selection.length === elements.length}
                indeterminate={selection.length > 0 && selection.length !== elements.length}
                onChange={toggleAll}
              />
            </Table.Th>
            <Table.Th>Element position</Table.Th>
            <Table.Th>Element name</Table.Th>
            <Table.Th>Symbol</Table.Th>
            <Table.Th>Atomic mass</Table.Th>
          </Table.Tr>
        </Table.Thead>
        <Table.Tbody>{rows}</Table.Tbody>
      </Table>

      <ActionBar
        opened={selection.length > 0}
        onClose={() => setSelection([])}
        shadow="md"
        aria-label="Selected elements actions"
      >
        <Text size="sm">{selection.length} selected</Text>
        <ActionBar.Divider />
        <Button variant="default" size="compact-sm">
          Delete
        </Button>
        <Button variant="default" size="compact-sm">
          Move
        </Button>
        <Button variant="default" size="compact-sm">
          Archive
        </Button>
        <ActionBar.CloseButton aria-label="Clear selection" />
      </ActionBar>
    </>
  );
}
```


## With ActionIcon buttons

```tsx
import { useState } from 'react';
import { ArchiveIcon, FolderIcon, TrashIcon } from '@phosphor-icons/react';
import { ActionBar, ActionIcon, Checkbox, Group, Table, Text, Tooltip } from '@mantine/core';

const elements = [
  { position: 6, mass: 12.011, symbol: 'C', name: 'Carbon' },
  { position: 7, mass: 14.007, symbol: 'N', name: 'Nitrogen' },
  { position: 39, mass: 88.906, symbol: 'Y', name: 'Yttrium' },
  { position: 56, mass: 137.33, symbol: 'Ba', name: 'Barium' },
  { position: 58, mass: 140.12, symbol: 'Ce', name: 'Cerium' },
];

function Demo() {
  const [selection, setSelection] = useState<number[]>([]);

  const toggleRow = (position: number) =>
    setSelection((current) =>
      current.includes(position)
        ? current.filter((item) => item !== position)
        : [...current, position]
    );

  const toggleAll = () =>
    setSelection((current) =>
      current.length === elements.length ? [] : elements.map((element) => element.position)
    );

  const rows = elements.map((element) => (
    <Table.Tr
      key={element.position}
      bg={selection.includes(element.position) ? 'var(--mantine-color-blue-light)' : undefined}
    >
      <Table.Td>
        <Checkbox
          aria-label="Select row"
          checked={selection.includes(element.position)}
          onChange={() => toggleRow(element.position)}
        />
      </Table.Td>
      <Table.Td>{element.position}</Table.Td>
      <Table.Td>{element.name}</Table.Td>
      <Table.Td>{element.symbol}</Table.Td>
      <Table.Td>{element.mass}</Table.Td>
    </Table.Tr>
  ));

  return (
    <>
      <Table>
        <Table.Thead>
          <Table.Tr>
            <Table.Th>
              <Checkbox
                aria-label="Select all"
                checked={selection.length === elements.length}
                indeterminate={selection.length > 0 && selection.length !== elements.length}
                onChange={toggleAll}
              />
            </Table.Th>
            <Table.Th>Element position</Table.Th>
            <Table.Th>Element name</Table.Th>
            <Table.Th>Symbol</Table.Th>
            <Table.Th>Atomic mass</Table.Th>
          </Table.Tr>
        </Table.Thead>
        <Table.Tbody>{rows}</Table.Tbody>
      </Table>

      <ActionBar
        opened={selection.length > 0}
        onClose={() => setSelection([])}
        shadow="md"
        aria-label="Selected elements actions"
      >
        <Text size="sm" fw={500}>{selection.length} rows selected</Text>
        <ActionBar.Divider />
        <Tooltip.Group openDelay={300} closeDelay={100}>
          <Group gap="xs">
            <Tooltip label="Delete">
              <ActionIcon variant="default" color="red" size="lg">
                <TrashIcon size={16} />
              </ActionIcon>
            </Tooltip>
            <Tooltip label="Move to folder">
              <ActionIcon variant="default" size="lg">
                <FolderIcon size={16} />
              </ActionIcon>
            </Tooltip>
            <Tooltip label="Archive">
              <ActionIcon variant="default" size="lg">
                <ArchiveIcon size={16} />
              </ActionIcon>
            </Tooltip>
          </Group>
        </Tooltip.Group>
        <ActionBar.Divider />
        <ActionBar.CloseButton aria-label="Clear selection" />
      </ActionBar>
    </>
  );
}
```


## Placement

By default, `ActionBar` is centered at the bottom of the screen. Change it with the `position` prop.
For non-centered positions, set `styles={{ root: { marginInline: 0 } }}` to remove the auto centering.
Use `affixProps` to pass props down to the underlying [Affix](https://alpha.mantine.dev/llms/core-affix.md):

```tsx
import { useDisclosure } from '@mantine/hooks';
import { ActionBar, Button, Group, Text } from '@mantine/core';

function Demo() {
  const [center, centerHandlers] = useDisclosure(false);
  const [left, leftHandlers] = useDisclosure(false);
  const [right, rightHandlers] = useDisclosure(false);

  return (
    <>
      <Group justify="center">
        <Button onClick={centerHandlers.toggle}>Center (default)</Button>
        <Button onClick={leftHandlers.toggle}>Left</Button>
        <Button onClick={rightHandlers.toggle}>Right</Button>
      </Group>

      <ActionBar opened={center} onClose={centerHandlers.close} shadow="md" aria-label="Actions">
        <Text size="sm">Center position (default)</Text>
        <ActionBar.CloseButton aria-label="Close" />
      </ActionBar>

      <ActionBar
        opened={left}
        onClose={leftHandlers.close}
        shadow="md"
        aria-label="Actions"
        position={{ bottom: 30, left: 30 }}
        styles={{ root: { marginInline: 0 } }}
      >
        <Text size="sm">Left position</Text>
        <ActionBar.CloseButton aria-label="Close" />
      </ActionBar>

      <ActionBar
        opened={right}
        onClose={rightHandlers.close}
        shadow="md"
        aria-label="Actions"
        position={{ bottom: 30, right: 30 }}
        styles={{ root: { marginInline: 0 } }}
      >
        <Text size="sm">Right position</Text>
        <ActionBar.CloseButton aria-label="Close" />
      </ActionBar>
    </>
  );
}
```


## Close on Escape

Set `closeOnEscape` to close the bar with the `Escape` key. `Escape` pressed inside
a modal or an opened dropdown does not close the bar:

```tsx
import { useDisclosure } from '@mantine/hooks';
import { ActionBar, Button, Group, Text } from '@mantine/core';

function Demo() {
  const [opened, { toggle, close }] = useDisclosure(false);

  return (
    <>
      <Group justify="center">
        <Button onClick={toggle}>Toggle action bar</Button>
      </Group>

      <ActionBar opened={opened} onClose={close} closeOnEscape shadow="md" aria-label="Actions">
        <Text size="sm">Press Escape to close this bar</Text>
        <ActionBar.Divider />
        <Button variant="default" size="compact-sm" onClick={close}>
          Close
        </Button>
      </ActionBar>
    </>
  );
}
```


## Transitions

Use `transitionProps` to change the [transition](https://alpha.mantine.dev/llms/core-transition.md), its duration and timing function:

```tsx
import { ActionBar, Button } from '@mantine/core';

function Demo() {
  return (
    <ActionBar opened transitionProps={{ transition: 'slide-up', duration: 300 }}>
      <Button>Delete</Button>
    </ActionBar>
  );
}
```

## keepMounted

By default, the action bar is removed from the DOM when it is closed. Set `keepMounted`
to keep it mounted, for example to preserve form state inside the bar:

```tsx
import { ActionBar, Button } from '@mantine/core';

function Demo() {
  return (
    <ActionBar opened={false} keepMounted>
      <Button>Delete</Button>
    </ActionBar>
  );
}
```

## Rendering in a portal

`ActionBar` is rendered inside [Portal](https://alpha.mantine.dev/llms/core-portal.md) by default. Set `withinPortal={false}`
to render it in place:

```tsx
import { ActionBar, Button } from '@mantine/core';

function Demo({ selectedCount }: { selectedCount: number }) {
  return (
    <ActionBar
      opened={selectedCount > 0}
      withinPortal={false}
      aria-label="Selected items actions"
    >
      <Button>Delete {selectedCount} items</Button>
    </ActionBar>
  );
}
```

## Accessibility

`ActionBar` does not set any labels by default, set:

* `aria-label` on `ActionBar` to describe what the actions apply to
* `aria-label` on `ActionBar.CloseButton`, it renders only an icon

Screen readers are not notified when the bar appears. Set `announcement` to announce it
every time the bar opens or its content changes:

```tsx
import { ActionBar, Button } from '@mantine/core';

function Demo({ selection }: { selection: string[] }) {
  return (
    <ActionBar
      opened={selection.length > 0}
      aria-label="Selected rows actions"
      announcement={`${selection.length} rows selected, bulk actions available`}
    >
      <Button>Delete</Button>
      <ActionBar.CloseButton aria-label="Clear selection" />
    </ActionBar>
  );
}
```


#### Props

**ActionBar props**

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| affixProps | AffixProps & DataAttributes | - | Props passed down to the `Affix` component |
| announcement | React.ReactNode | - | Content announced by screen readers when the action bar opens or when the content changes while it is opened, for example `'3 rows selected'` |
| aria-label | string | - | `aria-label` of the actions group, describes what the actions apply to |
| closeOnEscape | boolean | false | Determines whether the action bar should be closed when `Escape` key is pressed |
| keepMounted | boolean | false | If set, the action bar is kept in the DOM when closed. React `Activity` is used to preserve its state while it is hidden |
| onClose | () => void | - | Called when close button is clicked or Escape is pressed |
| opened | boolean | required | Controls visibility |
| portalProps | BasePortalProps | - | Props passed down to the `Portal` component. Ignored when `withinPortal` is `false`. |
| position | AffixPosition | { bottom: 30, left: 0, right: 0 } | Action bar position on screen |
| radius | MantineRadius \| number | theme.defaultRadius | Key of `theme.radius` or any valid CSS value to set `border-radius` |
| shadow | MantineShadow | - | Key of `theme.shadows` or any valid CSS `box-shadow` value |
| transitionProps | TransitionProps | { transition: 'pop', duration: 200 } | Props passed down to the `Transition` component |
| withBorder | boolean | - | Adds border to the root element |
| withinPortal | boolean | true | Determines whether the component is rendered within `Portal` |
| zIndex | React.CSSProperties["zIndex"] | 200 | Root element `z-index` property |


#### Styles API

ActionBar component supports Styles API. With Styles API, you can customize styles of any inner element. Follow the documentation to learn how to use CSS modules, CSS variables and inline styles to get full control over component styles.

**ActionBar selectors**

| Selector | Static selector | Description |
|----------|----------------|-------------|
| root | .mantine-ActionBar-root | Root element (Paper) |
| divider | .mantine-ActionBar-divider | ActionBar.Divider element |
| closeButton | .mantine-ActionBar-closeButton | ActionBar.CloseButton element |
