ChatGPT UI Button - Horizon AI Template

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, ButtonGroup } from "@chakra-ui/react"
  • Button: The button component with support for custom icons, spinners, etc.
  • ButtonGroup: Used to group buttons whose actions are related, with an option to flush them together.

Usage#

<Button borderRadius="12px" colorScheme="brand">
Button
</Button>
<Button variant="brand">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={4} direction="row" align="center">
<Button borderRadius="12px" colorScheme="brand" size="xs">
Button
</Button>
<Button borderRadius="12px" colorScheme="brand" size="sm">
Button
</Button>
<Button borderRadius="12px" colorScheme="brand" size="md">
Button
</Button>
<Button borderRadius="12px" colorScheme="brand" size="lg">
Button
</Button>
</Stack>
<Stack spacing={4} direction="row" align="center">
<Button variant="brand" size="xs">
Button
</Button>
<Button variant="brand" size="sm">
Button
</Button>
<Button variant="brand" size="md">
Button
</Button>
<Button variant="brand" size="lg">
Button
</Button>
</Stack>

Button variants#

Use the variant prop to change the visual style of the Button. You can set the value to solid, ghost, outline, or link.

<Stack direction="row" spacing={4} align="center">
<Button borderRadius="12px" colorScheme="brand" variant="solid">
Button
</Button>
<Button borderRadius="12px" colorScheme="brand" variant="outline">
Button
</Button>
<Button borderRadius="12px" colorScheme="brand" variant="ghost">
Button
</Button>
<Button borderRadius="12px" colorScheme="brand" variant="link">
Button
</Button>
</Stack>
<Stack direction="row" spacing={4} align="center">
<Button variant="brand">Button</Button>
<Button variant="outline">Button</Button>
<Button variant="ghost">Button</Button>
<Button variant="link">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 and rightIcon prop values should be react elements NOT strings.

<Stack direction="row" spacing={4}>
<Button
borderRadius="12px"
leftIcon={<EmailIcon />}
colorScheme="brand"
variant="solid"
>
Email
</Button>
<Button
borderRadius="12px"
rightIcon={<ArrowForwardIcon />}
colorScheme="brand"
variant="outline"
>
Call us
</Button>
</Stack>
<Stack direction="row" spacing={4}>
<Button leftIcon={<EmailIcon />} variant="brand">
Email
</Button>
<Button rightIcon={<ArrowForwardIcon />} variant="outline">
Call us
</Button>
</Stack>

You can also use icons from popular libraries like react-icons and pass it into the button.

// import { MdBuild , MdCall } from "react-icons/md"
<Stack direction="row" spacing={4}>
<Button
borderRadius="12px"
leftIcon={<MdBuild />}
colorScheme="brand"
variant="brand"
>
Settings
</Button>
<Button
borderRadius="12px"
rightIcon={<MdCall />}
colorScheme="brand"
variant="outline"
>
Call us
</Button>
</Stack>
// import { MdBuild , MdCall } from "react-icons/md"
<Stack direction="row" spacing={4}>
<Button leftIcon={<MdBuild />} variant="brand">
Settings
</Button>
<Button rightIcon={<MdCall />} variant="outline">
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 direction="row" spacing={4}>
<Button borderRadius="12px" isLoading colorScheme="brand" variant="solid">
Email
</Button>
<Button
borderRadius="12px"
isLoading
loadingText="Submitting"
colorScheme="brand"
variant="outline"
>
Submit
</Button>
</Stack>
<Stack direction="row" spacing={4}>
<Button isLoading variant="brand">
Email
</Button>
<Button isLoading loadingText="Submitting" variant="outline">
Submit
</Button>
</Stack>

You can change the spinner element to use custom loaders as per your design requirements. Pass the spinner prop and set it to a custom react element.

<Button
borderRadius="12px"
isLoading
colorScheme="brand"
spinner={<BeatLoader size={8} color="white" />}
>
Click me
</Button>
<Button isLoading spinner={<BeatLoader size={8} color="white" />}>
Click me
</Button>

When a loadingText is present, you can change the placement of the spinner element to either start or end. It is start by default.

<Stack direction="row" spacing={4} align="center">
<Button
borderRadius="12px"
isLoading
loadingText="Loading"
colorScheme="red"
variant="outline"
spinnerPlacement="start"
>
Submit
</Button>
<Button
borderRadius="12px"
isLoading
loadingText="Loading"
colorScheme="brand"
variant="outline"
spinnerPlacement="end"
>
Continue
</Button>
</Stack>
<Stack direction="row" spacing={4} align="center">
<Button
isLoading
loadingText="Loading"
colorScheme="red"
variant="outline"
spinnerPlacement="start"
>
Submit
</Button>
<Button
isLoading
loadingText="Loading"
variant="outline"
spinnerPlacement="end"
>
Continue
</Button>
</Stack>

Social Buttons#

We've included the colors for common social media platforms, and you can simply use their buttons via the colorScheme prop.

<HStack>
<Button borderRadius="12px" colorScheme="facebook" leftIcon={<FaFacebook />}>
Facebook
</Button>
<Button borderRadius="12px" colorScheme="twitter" leftIcon={<FaTwitter />}>
Twitter
</Button>
</HStack>
<HStack>
<Button colorScheme="facebook" leftIcon={<FaFacebook />}>
Facebook
</Button>
<Button colorScheme="twitter" leftIcon={<FaTwitter />}>
Twitter
</Button>
</HStack>

The Facebook and Twitter icons in the above example are available from react-icons as FaFacebook and FaTwitter, found in the react-icons/fa import.

Grouping Buttons#

You can use the Stack or ButtonGroup component to group buttons. When you use the ButtonGroup component, it allows you to:

  • Set the size and variant of all buttons within it.
  • Add spacing between the buttons.
  • Flush the buttons together by removing the border radius of the its children as needed.
<ButtonGroup borderRadius="15px" variant="outline" spacing="6">
<Button borderRadius="15px" colorScheme="brand">
Save
</Button>
<Button borderRadius="15px">Cancel</Button>
</ButtonGroup>
<ButtonGroup variant="outline" spacing="6">
<Button>Save</Button>
<Button>Cancel</Button>
</ButtonGroup>

To flush the buttons, pass the isAttached prop.

<ButtonGroup borderRadius="15px" size="sm" isAttached variant="outline">
<Button borderRadius="15px" mr="-px">
Save
</Button>
<IconButton
borderRadius="15px"
aria-label="Add to friends"
icon={<AddIcon />}
/>
</ButtonGroup>
<ButtonGroup size="sm" isAttached variant="outline">
<Button mr="-px">Save</Button>
<IconButton aria-label="Add to friends" icon={<AddIcon />} />
</ButtonGroup>

Variants#

You can use the Stack or ButtonGroup component to group buttons. When you use the ButtonGroup component, it allows you to:

  • Set the size and variant of all buttons within it.
  • Add spacing between the buttons.
  • Flush the buttons together by removing the border radius of the its children as needed.
<ButtonGroup borderRadius="15px" variant="outline" spacing="6">
<Button borderRadius="15px" colorScheme="brand">
Save
</Button>
<Button borderRadius="15px">Cancel</Button>
</ButtonGroup>
<ButtonGroup variant="outline" spacing="6">
<Button>Save</Button>
<Button>Cancel</Button>
</ButtonGroup>

Accessibility#

  • Button has role of button.
  • When Button has focus, Space or Enter activates it.

Composition#

All props you pass (variant, colorScheme, etc.) are converted to style props. This means you can override any style of the Button via props.

Custom variants:#

"transparent-with-icon"

function Example() {
let mainTeal = useColorModeValue("brand.300", "brand.300")
let inputBg = useColorModeValue("white", "gray.800")
let mainText = useColorModeValue("gray.700", "gray.200")
let navbarIcon = useColorModeValue("gray.500", "gray.200")
let searchIcon = useColorModeValue("gray.700", "gray.200")
return (
<Button
bg="transparent"
fontWeight="bold"
borderRadius="inherit"
cursor="pointer"
_hover="none"
_active={{
bg: "transparent",
transform: "none",
borderColor: "transparent",
}}
_focus={{ boxShadow: "none" }}
_hover={{ boxShadow: "none" }}
borderRadius="15px"
colorScheme="brand"
ms="0px"
px="0px"
me={{ sm: "2px", md: "16px" }}
color={navbarIcon}
variant="transparent-with-icon"
leftIcon={<EmailIcon color={navbarIcon} w="22px" h="22px" me="0px" />}
>
Email
</Button>
)
}
<Button variant="transparent-with-icon">Email</Button>

"no-hover"

function Example() {
let navbarIcon = useColorModeValue("gray.700", "gray.200")
let mainText = useColorModeValue("gray.700", "gray.200")
let navbarBg = useColorModeValue(
"linear-gradient(112.83deg, rgba(255, 255, 255, 0.82) 0%, rgba(255, 255, 255, 0.8) 110.84%)",
"linear-gradient(112.83deg, rgba(255, 255, 255, 0.21) 0%, rgba(255, 255, 255, 0) 110.84%)",
)
let navbarBorder = useColorModeValue(
"1.5px solid #FFFFFF",
"1.5px solid rgba(255, 255, 255, 0.31)",
)
let navbarShadow = useColorModeValue(
"0px 7px 23px rgba(0, 0, 0, 0.05)",
"none",
)
let navbarFilter = useColorModeValue(
"none",
"drop-shadow(0px 7px 23px rgba(0, 0, 0, 0.05))",
)
let navbarBackdrop = "blur(21px)"
let bgButton = useColorModeValue(
"linear-gradient(135deg, #868CFF 0%, #4318FF 100%)",
"linear-gradient(135deg, #868CFF 0%, #4318FF 100%)",
)
let navbarPosition = "fixed"
let colorButton = "white"
return (
<Button
_hover={{ boxShadow: "none" }}
bg={bgButton}
color={colorButton}
fontSize="sm"
borderRadius="14px"
px="30px"
_active="none"
display={{
sm: "none",
lg: "flex",
}}
>
Free Download
</Button>
)
}
let bgButton = useColorModeValue(
"linear-gradient(135deg, #868CFF 0%, #4318FF 100%)",
"linear-gradient(135deg, #868CFF 0%, #4318FF 100%)",
)
let navbarPosition = "fixed"
let colorButton = "white"
return (
<Button
_hover={{ boxShadow: "none" }}
bg={bgButton}
color={colorButton}
fontSize="sm"
borderRadius="14px"
px="30px"
_active="none"
display={{
sm: "none",
lg: "flex",
}}
>
Free Download
</Button>
)

Custom Button#

In the event that you need to make your own custom button, you can leverage the Box component. It provides the hover, focus, active and disabled style props to style the button.

// Button from facebook.com
<Box
as="button"
height="24px"
lineHeight="1.2"
transition="all 0.2s cubic-bezier(.08,.52,.52,1)"
border="1px"
px="8px"
borderRadius="2px"
fontSize="14px"
fontWeight="semibold"
bg="#f5f6f7"
borderColor="#ccd0d5"
color="#4b4f56"
_hover={{ bg: "#ebedf0" }}
_active={{
bg: "#dddfe2",
transform: "scale(0.98)",
borderColor: "#bec3c9",
}}
_focus={{
boxShadow:
"0 0 1px 2px rgba(88, 144, 255, .75), 0 1px 1px rgba(0, 0, 0, .15)",
}}
>
Join Group
</Box>
// Button from facebook.com
<Box
as="button"
height="24px"
lineHeight="1.2"
transition="all 0.2s cubic-bezier(.08,.52,.52,1)"
border="1px"
px="8px"
borderRadius="2px"
fontSize="14px"
fontWeight="semibold"
bg="#f5f6f7"
borderColor="#ccd0d5"
color="#4b4f56"
_hover={{ bg: "#ebedf0" }}
_active={{
bg: "#dddfe2",
transform: "scale(0.98)",
borderColor: "#bec3c9",
}}
_focus={{
boxShadow:
"0 0 1px 2px rgba(88, 144, 255, .75), 0 1px 1px rgba(0, 0, 0, .15)",
}}
>
Join Group
</Box>

Props#

Button Props#

Button composes the Box component, so you can pass all its props. These are props specific to the Button component:

colorScheme

Type
"brand" | "whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | ... 4 more ...
Default
"gray"

iconSpacing

Description

The space between the button icon and label.

Type
SystemProps["marginRight"]

isActive

Description

If true, the button will be styled in its active state.

Type
boolean

isDisabled

Description

If true, the button will be disabled.

Type
boolean

isFullWidth

Description

If true, the button will take up the full width of its container.

Type
boolean

isLoading

Description

If true, the button will show a spinner.

Type
boolean

leftIcon

Description

If added, the button will show an icon before the button's label.

Type
React.ReactElement

loadingText

Description

The label to show in the button when isLoading is true If no text is passed, it only shows the spinner

Type
string

rightIcon

Description

If added, the button will show an icon after the button's label.

Type
React.ReactElement

size

Type
"sm" | "md" | "lg" | "xs"
Default
"md"

spinner

Description

Replace the spinner component when isLoading is set to true

Type
React.ReactElement

spinnerPlacement

Description

It determines the placement of the spinner when isLoading is true

Type
"end" | "start"
Default
"start"

variant

Type
"link" | "outline" | "solid" | "ghost" | "unstyled"
Default
"solid"

Button Group Props#

ButtonGroup composes the Box component, so you can pass all its props. These are props specific to the ButtonGroup component:

colorScheme

Description

Color Schemes for ButtonGroup are not implemented in the default theme. You can extend the themeto implement them.

Type
"brand" | "whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | ... 4 more ...

isAttached

Description

If true, the borderRadius of button that are direct children will be altered to look flushed together

Type
boolean

isDisabled

Description

If true, all wrapped button will be disabled

Type
boolean

size

Type
"sm" | "md" | "lg" | "xs"

spacing

Description

The spacing between the buttons

Type
SystemProps["marginRight"]
Default
'0.5rem'

variant

Type
"link" | "outline" | "solid" | "ghost" | "unstyled"

Horizon AI Template © 2021-2023 Copyright. All Rights Reserved.

  • Blog
  • License