Button
The Button
component allows users to trigger an action or event, such as
submitting a form, opening a dialog, canceling an action, or performing a delete
operation.
Usage
Basic usage
import { Button } from '@snipshot/particles'; export default function ButtonDefault() { return <Button>Confirm</Button>; }
Variants
The Button
component comes with five different variants: filled
, outlined
,
text
, soft
, and elevated
.
Colors
You can choose from one of the predefined color options by passing a color
props value: primary
, secondary
, tertiary
, info
, success
, warning
,
error
.
If you need an unstyled button, pass the uncolored
value.
Radiuses
You can customize the border radius by providing a radius
prop value.
Sizes
To make a button smaller or larger use the size
props with the following
values: xs
, sm
, md
, lg
, xl
.
You can also use a dense
property to reduce paddings:
Icons
You can add icons to the Button
component using leftIcon
and rightIcon
props.
Disabled state
The component supports accessible disabled state with the disabled
prop.
Loading state
The Button component supports a loading state with a loading spinner (or a
custom loading element) and optional loading text using isLoading
,
loadingText
, and loadingElement
props.
Polymorphic component
The Button
is a polymorphic component, which means that you can use the as
prop to specify any valid HTML element as a string or a React component. This
chosen element will serve as the base for rendering the Button component,
replacing the default <button>
element.
Accessibility
The Button component follows
WAI-ARIA (opens in a new tab) best practices to
ensure accessibility for all users. It includes semantic <button>
element and
appropriate ARIA attributes to enhance compatibility with screen readers and
assistive technologies.
API Reference
Prop | Type | Default | Description |
---|---|---|---|
as | React.ElementType | 'button' | HTML or React element that will be rendered by the component |
color | 'primary' | 'secondary' | 'tertiary' |'success' | 'info' | 'warning' | 'error' | 'primary' | Buttons's color scheme |
dense | boolean | false | Shrinks the button reducing paddings |
disabled | boolean | false | Button's disabled state |
isLoading | boolean | false | Applies loading state styling and behaviour |
loadingElement | React.ReactElement | undefined | Element that will be displayed in the loading state instead of a default spinner |
loadingText | string | '' | Button's content that will be displayed in loading state |
loadingSpinner | boolean | true | Disable a loading spinner in the loading state |
leftIcon | React.ReactElement | undefined | Icon element on the left |
radius | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'md' | Border radius value |
rightIcon | React.ReactElement | undefined | Icon element on the right |
shadowOnPress | boolean | false | Adds inset box-shadow imitating button press. Only for filled variant. |
shrinkOnPress | boolean | false | Makes the button a bit smaller on press |
size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'md' | Button's size value |
variant | 'filled' | 'outlined' | 'text' | 'soft' | 'elevated' | 'filled' | Styling variant |
CSS custom properties
The Button
component provides several CSS variables that can be used for
styling via CSS modules or using a style
attribute of the component.
Property | Description |
---|---|
--pui-btn-font-size | font-size value |
--pui-btn-font-weight | font-weight value |
--pui-btn-line-height | line-height value |
--pui-btn-letter-spacing | letter-spacing value |
--pui-btn-icon-size | Size of the .btn-icon and .btn-load-spinner elements |
--pui-btn-color | Main button's color. If used, --pui-btn-color-channels has to be provided either |
--pui-btn-color-channels | RGB channels of the main color (--pui-btn-color ) represented as an rray of three numbers like 255 0 0 |
--pui-btn-on-color | Color that is used on the main color (e.g. font color). |
--pui-btn-color-darker | Darker version of the main (--pui-btn-color ) color. In some cases necessary for hover effects. If used, pui-btn-color-darker-channels has to be provided either |
--pui-btn-color-darker-channels | RGB channels of the darker main color (--pui-btn-color-darker ) represented as an array of three numbers like 255 0 0 |
--pui-btn-color-container-low | Lighter version of the main (--pui-btn-color ) color, used for background-color in the soft variant |
--pui-btn-on-color-container | Color that is used on the --pui-btn-color-container-low color. |