Trigger actions such as saving, submitting or deleting.
Basic usage
Use button text that clearly describes the action and handle clicks through onClick.
import { Button } from '@ifhover/leaf-ui' ;
export function ButtonBasic () {
return < Button >Save</ Button >;
}
type defaults to button, which does not submit a form. Set type="submit" for form submission.
Examples
Appearance
Use variant for outline (the default), solid, text or inline link appearance. Inline links use the theme color by default, brighten on hover and darken when pressed, with a default color transition of 200ms.
import { Button , Flex , Grid , Text } from '@ifhover/leaf-ui' ;
import { Fragment } from 'react' ;
const options = [
{ value : 'outline' , label : 'Outline' } ,
{ value : 'solid' , label : 'Solid' } ,
{ value : 'text' , label : 'Text' } ,
{ value : 'link' , label : 'Link' } ,
] as const ;
export function ButtonVariants () {
return (
< Grid columns = {{ xs : 1 , sm : 2 }} gap = { 16 } align = "center" >
{ options .map (({ value , label }) => (
< Fragment key = {value}>
< Text tone = "muted" >{label}</ Text >
< Flex align = "center" wrap >
< Button variant = {value}>Save</ Button >
</ Flex >
</ Fragment >
))}
</ Grid >
);
}
Sizes
Set the size with size; the default is md.
import { Button , Flex , Grid , Text } from '@ifhover/leaf-ui' ;
import { Fragment } from 'react' ;
const options = [
{ value : 'sm' , label : 'Small' } ,
{ value : 'md' , label : 'Medium' } ,
{ value : 'lg' , label : 'Large' } ,
] as const ;
export function ButtonSizes () {
return (
< Grid gap = { 16 } align = "center" style = {{ gridTemplateColumns : 'max-content minmax(0, 1fr)' }}>
{ options .map (({ value , label }) => (
< Fragment key = {value}>
< Text tone = "muted" >{label}</ Text >
< Flex align = "center" wrap >
< Button size = {value}>Save</ Button >
</ Flex >
</ Fragment >
))}
</ Grid >
);
}
Danger actions
Set danger for destructive actions such as deleting or clearing. It works with every appearance.
import { Button , Flex , Grid , Text } from '@ifhover/leaf-ui' ;
import { Fragment } from 'react' ;
const options = [
{ value : 'outline' , label : 'Outline' } ,
{ value : 'solid' , label : 'Solid' } ,
{ value : 'text' , label : 'Text' } ,
{ value : 'link' , label : 'Link' } ,
] as const ;
export function ButtonDanger () {
return (
< Grid columns = {{ xs : 1 , sm : 2 }} gap = { 16 } align = "center" >
{ options .map (({ value , label }) => (
< Fragment key = {value}>
< Text tone = "muted" >{label}</ Text >
< Flex align = "center" wrap >
< Button danger variant = {value}>
Delete
</ Button >
</ Flex >
</ Fragment >
))}
</ Grid >
);
}
Disabled and loading
disabled disables the button. loading shows a spinner and prevents repeated clicks.
import { Button , Flex , Grid , Text } from '@ifhover/leaf-ui' ;
export function ButtonStates () {
return (
< Grid columns = {{ xs : 1 , sm : 2 }} gap = { 16 } align = "center" >
< Text tone = "muted" >Disabled</ Text >
< Flex align = "center" wrap >
< Button disabled >Save</ Button >
</ Flex >
< Text tone = "muted" >Loading</ Text >
< Flex align = "center" wrap >
< Button loading >Save</ Button >
</ Flex >
</ Grid >
);
}
Icons
Add icons with startIcon and endIcon. An icon-only button needs an aria-label.
import { Button , Flex , Grid , Text } from '@ifhover/leaf-ui' ;
import { ArrowRight , Plus , Trash2 } from 'lucide-react' ;
import { Fragment } from 'react' ;
const options = [
{ value : 'sm' , label : 'Small' } ,
{ value : 'md' , label : 'Medium' } ,
{ value : 'lg' , label : 'Large' } ,
] as const ;
export function ButtonIcons () {
return (
< Grid gap = { 16 } align = "center" style = {{ gridTemplateColumns : 'max-content minmax(0, 1fr)' }}>
{ options .map (({ value , label }) => (
< Fragment key = {value}>
< Text tone = "muted" >{label}</ Text >
< Flex align = "center" gap = { 8 } wrap >
< Button size = {value} startIcon = {< Plus />}>
New project
</ Button >
< Button size = {value} variant = "outline" endIcon = {< ArrowRight />}>
Continue
</ Button >
< Button size = {value} variant = "outline" aria-label = "Add project" startIcon = {< Plus />} />
< Button
size = {value}
danger
variant = "outline"
aria-label = "Delete project"
startIcon = {< Trash2 />}
/>
</ Flex >
</ Fragment >
))}
</ Grid >
);
}
Full width
Set fullWidth to fill the parent container.
import { Button } from '@ifhover/leaf-ui' ;
export function ButtonFullWidth () {
return < Button fullWidth >Continue</ Button >;
}
Async saving
Control loading with application state. The timer simulates a request; replace it with your async operation.
Save changes Not saved yet import { Button , Flex , Text } from '@ifhover/leaf-ui' ;
import { useEffect , useRef , useState } from 'react' ;
export function ButtonLoading () {
const [ status , setStatus ] = useState < 'idle' | 'loading' | 'saved' >( 'idle' );
const timer = useRef < ReturnType < typeof setTimeout> | undefined >( undefined );
useEffect (() => () => clearTimeout ( timer .current) , []);
function save () {
clearTimeout ( timer .current);
setStatus ( 'loading' );
// This example simulates a request; replace it with your application request.
timer .current = setTimeout (() => setStatus ( 'saved' ) , 1200 );
}
return (
< Flex direction = "column" gap = "md" align = "start" >
< Button loading = {status === 'loading' } onClick = {save}>
{status === 'saved' ? 'Save again' : 'Save changes' }
</ Button >
< Text tone = "muted" role = "status" >
{status === 'saved' ? 'Changes saved' : 'Not saved yet' }
</ Text >
</ Flex >
);
}
Use ButtonGroup to join adjacent actions and share a size or disabled state.
import { Button , ButtonGroup } from '@ifhover/leaf-ui' ;
export function ButtonGroupDemo () {
return (
< ButtonGroup aria-label = "Step actions" >
< Button >Previous</ Button >
< Button >Next</ Button >
</ ButtonGroup >
);
}
SplitButton pairs a primary action with related menu actions. Use onClick for the primary button and an action for each menu item.
import { Flex , SplitButton , Text } from '@ifhover/leaf-ui' ;
import { useState } from 'react' ;
export function SplitButtonDemo () {
const [ result , setResult ] = useState ( '' );
return (
< Flex direction = "column" gap = "md" align = "start" >
< SplitButton
variant = "solid"
onClick = {() => setResult ( 'Saved' )}
menuLabel = "More save options"
items = {[
{ key : 'copy' , label : 'Save a copy' , onClick : () => setResult ( 'Copy saved' ) } ,
{ key : 'draft' , label : 'Save draft' , onClick : () => setResult ( 'Draft saved' ) } ,
]}
>
Save
</ SplitButton >
< Text tone = "muted" role = "status" >
{result}
</ Text >
</ Flex >
);
}
API
Supports native button attributes such as onClick, name, form, aria-* and data-*.
Props for grouping related buttons.
Props for a button combining a primary action with dropdown actions.