Button
The Button
component in Pearl UI is a versatile and customizable element used to trigger various actions or events within your application. These actions could range from submitting a form, opening a dialog, canceling an action, or performing a delete operation. By default, the Button
component renders the Pressable component.
#
ImportPearl UI provides two button-related components for different use cases:
Button
: This is a single button that can be clicked to trigger an action or event.ButtonGroup
: This is a container for multipleButton
components. It manages the state of its child buttons and allows you to apply shared styles to them.
import { Button, ButtonGroup } from "pearl-ui";
#
Usage<Button>I am a button</Button>
#
Button sizesThe size
prop allows you to adjust the size of the button. The value for this prop should be one of the keys available in
<Button size="xs">Tiny Button</Button>
<Button size="s">Small Button</Button>
<Button size="m">Regular Button</Button>
<Button size="l">Large Button</Button>
#
Button variantsThe variant
prop allows you to change the visual style of the button. The value for this prop should be one of the keys available in
<Button variant="solid">Solid Button</Button>
<Button variant="outline">Outline Button</Button>
<Button variant="ghost">Ghost Button</Button>
#
Button with IconThe Button
component supports the addition of icons on either side of the button text. This can be achieved using the leftIcon
and rightIcon
props for left and right icons respectively.
info
The leftIcon
and rightIcon
prop values should be React elements, NOT strings.
import { Icon } from "pearl-ui";
<Button leftIcon={<Icon iconFamily="AntDesign" iconName="heart" />}> Button with left icon</Button>
<Button variant="outline" rightIcon={<Icon iconFamily="AntDesign" iconName="cloudupload" />}> Button with right icon</Button>
#
Loading State of ButtonThe isLoading
prop can be used to display a loading state on the button. By default, Button
will show a spinner and maintain the button's width. The loadingText
prop can be used to display a spinner and the loading text. When a loadingText
is present, the placement of the spinner element can be adjusted to either spinnerPlacement
prop. It is
<Button isLoading>Basic loading button</Button>
<Button isLoading loadingText="Loading">Button with loading text</Button>
<Button isLoading loadingText="Loading" spinnerPlacement="end"> Button with spinner at the end</Button>
#
Customizing Button Color SchemeThe colorScheme
prop allows you to change the color palette of the button. This is a powerful feature as it changes all the use color values in a palette through a single prop. The value for this prop should be one of the keys available in
<Button colorScheme="primary">Primary Button</Button>
<Button colorScheme="secondary">Secondary Button</Button>
#
Grouping ButtonsYou can use the Stack or ButtonGroup
component to group multiple buttons. When you use the ButtonGroup
component, it allows you to:
- Set the
size
,variant
, andcolorScheme
of all buttons within it. - Add
spacing
between the buttons. - Flush the buttons together by removing the border radius of their children as needed.
<ButtonGroup variant="outline" spacing="6"> <Button>Save</Button> <Button colorScheme="danger">Cancel</Button></ButtonGroup>
To flush the buttons, pass the isAttached
prop.
<ButtonGroup size="sm" isAttached variant="outline"> <Button>Save</Button> <Button colorScheme="danger" rightIcon={<Icon iconFamily="Ionicons" iconName="md-lock-closed" />} > Lock </Button></ButtonGroup>
#
Overriding StylesThe Button
component supports a variety of style props which can be used to override the pre-defined variant style in the theme. Manual style props passed into the component have a higher precedence than the default component configuration.
// passing style prop m="8" overrides the default component config value of m="1"// passing style prop boxShadow="xl" adds a box shadow to the button<Button m="8" boxShadow="3xl" variant="outline" />
#
Example#
AccessibilityButton
has therole
ofbutton
.Button
has the default accessibility label set as the text value passed into it. When the button is in a loading state, the accessibility label is set as"Loading" . A custom label can be specified using theaccessibilityLabel
prop.- When the
Button
is disabled or loading, it is reflected asdisabled
andbusy
respectively in screen readers. - Similar to Pressable, Button expects an
actionDescription
prop to specify the intented outcome of interacting with the component eg. 'Closes modal', 'Go to the homepage', etc.
#
Button Component Properties#
Supported Style PropertiesThe Button
component is a composition of the Pressable component. Therefore, all properties related to the Pressable component can be passed to the Button
component, in addition to the properties listed below.
#
Additional PropertiesProperty Name | Required | Data Type | Default | Description |
---|---|---|---|---|
size | No | "m" | Defines the size of the button. | |
variant | No | "filled" | Specifies the variant of the button. | |
isLoading | No | false | Indicates whether the button is in a loading state. | |
isFullWidth | No | false | Determines whether the button should span the entire width of the parent container. | |
loadingText | No | null | Specifies the text to display when the button is in a loading state. | |
colorScheme | No | "primary" | Defines the active color palette of the button. The expected value is the key of a palette object eg primary , secondary , neutral , etc. | |
spinnerPlacement | No | "start" | Specifies the position of the loading spinner relative to the loadingText . | |
leftIcon | No | null | Defines the icon to display on the left side of the main text. | |
rightIcon | No | null | Defines the icon to display on the right side of the main text. |
#
ButtonGroup Component Properties#
Supported Style PropertiesThe ButtonGroup
component is composed of the Stack component, which means you can pass all Stack properties to it.
#
Additional PropertiesHere is a list of additional properties that the ButtonGroup
component supports:
Name | Required | Type | Default | Description |
---|---|---|---|---|
size | No | "m" | Determines the size of all the children buttons in the group. | |
variant | No | "filled" | Determines the variant of all the children buttons in the group. | |
spacing | No | 2 | Sets the spacing between the children buttons in the group. | |
isDisabled | No | false | If true, the button group is disabled. | |
isAttached | No | false | If true, the children buttons are flushed together. | |
colorScheme | No | "primary" | Sets the active color palette of all the children buttons in the group. |