"use client"
import { Button } from "@chakra-ui/react"
import {
ActionBarContent,
ActionBarRoot,
ActionBarSelectionTrigger,
ActionBarSeparator,
} from "@/components/ui/action-bar"
import { Checkbox } from "@/components/ui/checkbox"
import { useState } from "react"
import { LuShare, LuTrash2 } from "react-icons/lu"
const Demo = () => {
const [checked, setChecked] = useState(false)
return (
<>
<Checkbox onCheckedChange={(e) => setChecked(!!e.checked)}>
Show Action bar
</Checkbox>
<ActionBarRoot open={checked}>
<ActionBarContent>
<ActionBarSelectionTrigger>2 selected</ActionBarSelectionTrigger>
<ActionBarSeparator />
<Button variant="outline" size="sm">
<LuTrash2 />
Delete
</Button>
<Button variant="outline" size="sm">
<LuShare />
Share
</Button>
</ActionBarContent>
</ActionBarRoot>
</>
)
}
Setup
If you don't already have the snippet, run the following command to add the
action-bar
snippet
npx @chakra-ui/cli snippet add action-bar
The snippet includes a closed component composition based on the Popover
component.
Usage
The action bar is designed to be controlled by table or checkbox selections. It provides a set of actions that can be performed on the selected items.
import {
ActionBarCloseTrigger,
ActionBarContent,
ActionBarRoot,
ActionBarSelectionTrigger,
ActionBarSeparator,
} from "@/components/ui/action-bar"
<ActionBarRoot>
<ActionBarContent>
<ActionBarCloseTrigger />
<ActionBarSelectionTrigger />
<ActionBarSeparator />
<Button />
</ActionBarContent>
</ActionBarRoot>
Examples
Close Trigger
Render the ActionBarCloseTrigger
to close the action bar, and pass the
onOpenChange
handler to control the visibility of the action bar.
The open
and onOpenChange
props control the visibility of the action bar.
"use client"
import { Button } from "@chakra-ui/react"
import {
ActionBarCloseTrigger,
ActionBarContent,
ActionBarRoot,
ActionBarSelectionTrigger,
ActionBarSeparator,
} from "@/components/ui/action-bar"
import { Checkbox } from "@/components/ui/checkbox"
import { useState } from "react"
import { LuShare, LuTrash2 } from "react-icons/lu"
const Demo = () => {
const [checked, setChecked] = useState(false)
return (
<>
<Checkbox
checked={checked}
onCheckedChange={(e) => setChecked(!!e.checked)}
>
Show Action bar
</Checkbox>
<ActionBarRoot
open={checked}
onOpenChange={(e) => setChecked(e.open)}
closeOnInteractOutside={false}
>
<ActionBarContent>
<ActionBarSelectionTrigger>2 selected</ActionBarSelectionTrigger>
<ActionBarSeparator />
<Button variant="outline" size="sm">
<LuTrash2 />
Delete
</Button>
<Button variant="outline" size="sm">
<LuShare />
Share
</Button>
<ActionBarCloseTrigger />
</ActionBarContent>
</ActionBarRoot>
</>
)
}
Within Dialog
Here's an example of composing the ActionBar
and the Dialog
to perform a
delete action on a set of selected items.
Press the Delete projects
button to open the dialog.
"use client"
import { DialogHeader } from "@chakra-ui/react"
import { Button } from "@chakra-ui/react"
import {
ActionBarContent,
ActionBarRoot,
ActionBarSelectionTrigger,
ActionBarSeparator,
} from "@/components/ui/action-bar"
import { Checkbox } from "@/components/ui/checkbox"
import {
DialogBody,
DialogContent,
DialogDescription,
DialogFooter,
DialogRoot,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import { useState } from "react"
import { LuSquarePlus, LuTrash2 } from "react-icons/lu"
const Demo = () => {
const [checked, setChecked] = useState(false)
return (
<>
<Checkbox onCheckedChange={(e) => setChecked(!!e.checked)}>
Check to select projects
</Checkbox>
<ActionBarRoot open={checked}>
<ActionBarContent>
<ActionBarSelectionTrigger>4 selected</ActionBarSelectionTrigger>
<ActionBarSeparator />
<Button variant="outline" size="sm">
<LuSquarePlus />
Add to collection
</Button>
<DialogRoot placement="center">
<DialogTrigger asChild>
<Button variant="surface" colorPalette="red" size="sm">
<LuTrash2 />
Delete projects
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Delete projects</DialogTitle>
</DialogHeader>
<DialogBody>
<DialogDescription>
Are you sure you want to delete 4 projects?
</DialogDescription>
</DialogBody>
<DialogFooter>
<Button variant="outline">Cancel</Button>
<Button colorPalette="red">Delete</Button>
</DialogFooter>
</DialogContent>
</DialogRoot>
</ActionBarContent>
</ActionBarRoot>
</>
)
}
Without Snippet
If you don't want to use the snippet, you can use the ActionBar
component from
the @chakra-ui/react
package.
"use client"
import { ActionBar, Button, Checkbox, Portal } from "@chakra-ui/react"
import { useState } from "react"
import { LuShare, LuTrash2 } from "react-icons/lu"
const Demo = () => {
const [checked, setChecked] = useState(false)
return (
<>
<Checkbox.Root onCheckedChange={(e) => setChecked(!!e.checked)}>
<Checkbox.HiddenInput />
<Checkbox.Control />
<Checkbox.Label>Show Action bar</Checkbox.Label>
</Checkbox.Root>
<ActionBar.Root open={checked}>
<Portal>
<ActionBar.Positioner>
<ActionBar.Content>
<ActionBar.SelectionTrigger>
2 selected
</ActionBar.SelectionTrigger>
<ActionBar.Separator />
<Button variant="outline" size="sm">
<LuTrash2 />
Delete
</Button>
<Button variant="outline" size="sm">
<LuShare />
Share
</Button>
</ActionBar.Content>
</ActionBar.Positioner>
</Portal>
</ActionBar.Root>
</>
)
}
Props
Root
Prop | Default | Type |
---|---|---|
No props to display |