The Button component provides a versatile and customizable button for various use cases. It supports different sizes, styles, and types, and can be used as a regular button or a link.
About
The Button
component is a flexible and customizable element designed to meet various needs. It supports multiple sizes, styles, and types, making it suitable for diverse use cases. You can use it as a standard button or as a link, depending on your requirements. The component allows for additional customization through props, enabling you to style and animate it to match your application's theme.
Example
Import the Button
component into your Svelte file. The following example shows how to use the button component!
<script>
import { Button } from "theui-svelte";
</script>
<Button>Button</Button>
Link Button
You can turn the Button
into a link by adding the href prop. This makes the button behave like a regular link. This will navigate to the home page when clicked.
<Button href="/">Link button</Button>
New Tab Icon
Sometimes, you may want to show an arrow icon to indicate that a link opens in a new tab. You can do this by setting the newTabIcon
prop to true
. By default, it's false
, so the icon is hidden.
Important: The link must have a target
attribute (like target="_blank"
) for the icon to appear. If there's no target
, the icon won't show - even if newTabIcon
is true
.
<Button href="/" target="_blank" newTabIcon={true}>...</Button>
Button Content
The Button
component offers beforeLabel
and afterLabel
snippet blocks that let you add custom content before or after the button’s label. This is useful for icons, badges, or other elements, giving you more control over the button’s look and behavior. Although you can add elements manually with HTML and CSS, using these snippets keeps the structure cleaner and easier to maintain. It also simplifies layout and styling.
For better structure, flexibility, and consistency, beforeLabel
and afterLabel
are great options.
Example Using Snippets
<Button>
{#snippet beforeLabel()}
<Svg size={1.5}>
<path d="M8 8a3 3 0 ... />
</Svg>
{/snippet}
Button Text
{#snippet afterLabel()}
<Svg size={1.5}>
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 ... />
</Svg>
{/snippet}
</Button>
Example Without Snippets
<Button>
<div class="CUSTOM CLASSES">
<Svg size={1.5}>
<path d="M8 8a3 3 0 ... />
</Svg>
Button Text
<Svg size={1.5}>
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 ... />
</Svg>
</div>
</Button>
Button Color
The color
prop lets you style the button using predefined themes like "brand"
, "error"
, "info"
, "success"
, and "warning"
. These match common statuses or actions to keep your app’s design consistent. The default is "brand"
.
<Button color="brand">Brand</Button>
<Button color="error">Error</Button>
<Button color="info">Info</Button>
<Button color="success">Success</Button>
<Button color="warning">Warning</Button>
Each color serves a purpose, making design intuitive and user-friendly. While you can customize colors with the class attribute, the color
prop offers a quick and consistent solution.
Theme
The theme
prop controls the button’s overall style and background. It supports three values: 'default'
, 'soft'
, and 'gradient'
. The default theme gives the standard button look, the soft theme uses softer colors, and the gradient theme adds a vibrant background with gradient styling.
Soft Theme
The soft theme is a softer variation of the default button colors, making it suitable for more subtle designs.
<Button theme="soft" color="brand">Brand</Button>
<Button theme="soft" color="error">Error</Button>
<Button theme="soft" color="info">Info</Button>
<Button theme="soft" color="success">Success</Button>
<Button theme="soft" color="warning">Warning</Button>
Gradient Theme
The gradient theme applies vibrant gradient colors to the button background, providing a more modern and eye-catching look.
<Button theme="gradient" gradientColor="brand">Brand</Button>
<Button theme="gradient" gradientColor="error">Error</Button>
<Button theme="gradient" gradientColor="info">Info</Button>
<Button theme="gradient" gradientColor="success">Success</Button>
<Button theme="gradient" gradientColor="warning">Warning</Button>
Outline
The outline
prop lets you create a button with a modern outline style. It gives the button a transparent background and a border, making it great for secondary actions or a minimalist look.
To use an outline button, set the outline
prop to true
like this: outline={true}
<Button color="brand" outline={true}>Brand</Button>
<Button color="error" outline={true}>Error</Button>
<Button color="info" outline={true}>Info</Button>
<Button color="success" outline={true}>Success</Button>
<Button color="warning" outline={true}>Warning</Button>
Size
The size
prop allows you to adjust the button's size to fit various design needs. This prop supports a range of predefined sizes, ensuring consistency and flexibility across your UI components.
Available values are "xs"
, "sm"
, "md"
(default), "lg"
, "xl"
, and "auto"
, allowing flexibility while maintaining consistency.
<Button size="xs">Extra small</Button>
<Button size="sm">Small</Button>
<Button size="md">Medium</Button>
<Button size="lg">Large</Button>
<Button size="xl">Extra large</Button>
<Button size="auto">Auto</Button>
Each size option adjusts the button's padding and font size, providing a cohesive look across different screen sizes and use cases. The auto
size adapts to the content, has no padding, making it flexible for various layouts.
Square Button
The square
prop allows you to create perfectly square buttons, ideal for icons or small content. By setting square
to true
, the button's height and width are equal, ensuring a neat, compact appearance. This prop is particularly useful for minimalist designs or icon buttons.
<Button square={true}>m</Button>
You can combine the square
prop with the size
prop to create square buttons of varying dimensions. This flexibility lets you choose the appropriate size for different UI elements while maintaining the square shape.
<Button square={true} size="xs"> ... </Button>
<Button square={true} size="sm"> ... </Button>
<Button square={true} size="md"> ... </Button>
<Button square={true} size="lg"> ... </Button>
<Button square={true} size="xl"> ... </Button>
This feature ensures that your square buttons can fit seamlessly into different parts of your design, from small icons to larger clickable elements.
Rounded Corner Button
The rounded
prop in the Button component allows you to easily control the border radius, ensuring consistent design throughout your application. This prop supports several predefined values, providing flexibility while maintaining a uniform look. Available values are: none
, sm
, md
, lg
, xl
, and full
. Default value md
.
<Button rounded="none">None</Button>
<Button rounded="sm">Small</Button>
<Button rounded="md">Medium</Button>
<Button rounded="lg">Large</Button>
<Button rounded="xl">Extra large</Button>
<Button rounded="full">Full</Button>
Using the rounded
prop ensures your buttons adhere to a consistent design pattern, enhancing the visual coherence of your UI.
Button Animation Speed
The animationSpeed
determines the speed of the button's animation, with options including "none"
, "slower"
, "slow"
, "normal"
, "fast"
, and "faster"
.
<Button animationSpeed="slower">Slower animation</Button>
<Button animationSpeed="slow">Slow animation</Button>
<Button animationSpeed="normal">Normal animation</Button>
<Button animationSpeed="fast">Fast animation</Button>
<Button animationSpeed="faster">Faster animation</Button>
<Button animationSpeed="none">No Animation animation</Button>
Each value adjusts the animation timing to fit your design needs, ensuring a seamless user experience.
Accessibility
The Button component is designed with accessibility in mind, ensuring it can be used by everyone, including users relying on assistive technologies. By default, it includes the necessary ARIA attributes and supports keyboard navigation.
- Keyboard Navigation: Use tab to navigate between headers.
- ARIA Attributes: The
aria-label
attribute used to provide a descriptive label when the button’s text or icon alone may not be clear (e.g., icon-only buttons). You can use theariaLabel
prop allows you to provide an accessible name for the button, ensuring screen readers can describe its function clearly.Svelte<Button ariaLabel="Submit the form">Submit</Button>
- Focus Management: Focus styles are provided using focus-visible or focus-within to ensure a clear visual indicator when the button is focused.
- State Indications: Visual cues (hover, focus, active, and disabled states) are provided to communicate the button's interactive state clearly.