Button
The Button component is used to trigger an action or event, such as submitting a form, opening a dialog, canceling an action, or performing a delete operation.
Import#
import { Button } from "@nature-ui/core";
Usage#
<Button className="bg-blue-500">Button</Button>
Button Sizes#
Use the size
prop to change the size of the button. You can set the value to
xs
, sm
, md
, or lg
.
<Stack spacing="1rem" row className="items-center"><Button className="bg-teal-500" size="xs">Button</Button><Button className="bg-teal-500" size="sm">Button</Button><Button className="bg-teal-500" size="md">Button</Button><Button className="bg-teal-500" size="lg">Button</Button></Stack>
Button variants#
<Stack row spacing="1rem" className="items-center">{/* Solid (Default) variant */}<Button className="bg-purple-500">Button</Button>{/* Outline variant */}<Button className="text-purple-500 border border-purple-500">Button</Button>{/* Ghost variant */}<Button className="text-purple-500">Button</Button></Stack>
Button with icon#
You can add left and right icons to the Button component using the leftIcon
and RightIcon
props respectively.
Note: The
leftIcon
andrightIcon
prop values should be react elements NOT strings.
<Stack row spacing="1rem"><Button leftIcon={<EmailIcon />} className="bg-teal-500"></Button><ButtonrightIcon={<ArrowForwardIcon />}className="text-teal-500 border border-teal-500">Call us</Button></Stack>
You can also use icons from popular libraries like react-icons and pass it into the button.
Note:
@nature-ui/icons
exports each of these icon packs
// import { MdBuild , MdCall } from "react-icons/md"<Stack row spacing="1rem"><Button leftIcon={<MdBuild />} className="bg-pink-500">Settings</Button><ButtonrightIcon={<MdCall />}className="text-blue-500 border-blue-500 border">Call us</Button></Stack>
Button loading state#
Pass the isLoading
prop to show its loading state. By default, the button will
show a spinner and leave the button's width unchanged.
You can also pass the loadingText
prop to show a spinner and the loading text.
<Stack row spacing="1rem"><Button isLoading className="bg-purple-500"></Button><ButtonisLoadingloadingText="Submitting"className="text-purple-500 border border-purple-500">Submit</Button></Stack>
Accessibility#
- Button has
role
ofbutton
. - When Button has focus, Space or Enter activates it.
Composition#
All props you pass (size
, color
, etc.) are converted to style props. This
means you can override any style of the Button with custom tailwindcss
classes
or the emotion css
prop.
// The size prop affects the height of the button// but I can still override it by passing a custom height<Buttonsize="md"css={{height: "48px",width: "200px",}}className="bg-gray-300 border-2 border-green-500">Button</Button>
Props#
Button composes the Box
component so you can pass all props for Box
. These
are props specific to the Button component.
Name | Type | Default | Description |
---|---|---|---|
aria-label | string | An accessible label for the button, useful when you render only an icon in the button | |
color | string | blue | The color scheme of the button. Use the colors in theme.colors that have 100 - 900 values |
leftIcon | ReactElement | The left icon element to use in the button | |
rightIcon | ReactElement | The right icon element to use in the button | |
spinner | ReactElement | The spinner element to use when isLoading is set to true | |
isDisabled | boolean | false | If true , the button will be disabled. |
isLoading | boolean | false | If true , the button will show a spinner. |
loadingText | string | The label to show in the button when isLoading is true. If no text is passed, it only shows the spinner | |
size | sm , md , lg | md | The size of the button. |