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
In some cases, a new tab arrow icon may be needed to indicate that a link opens in a new window or tab. The newTabIcon
Boolean prop enables this functionality. By default, it is false
, so the icon is hidden. When set to true
, the icon appears if the link includes a target
attribute.
The target
attribute is required for the newTabIcon
to appear. Without it, the icon will not be displayed, even if newTabIcon
is set to true.
<Button href="/" target="_blank" newTabIcon={true}>...</Button>
Button Content
The Button
component provides beforeLabel
and afterLabel
snippet blocks, allowing you to add custom content before and after the button's label. This structure is especially useful for adding icons, badges, or any other elements, giving you more control over the button's appearance and behavior.
While you can manually add custom elements directly inside the Button
element using standard HTML and CSS, the snippet approach ensures a cleaner and more maintainable structure. It also simplifies the integration of custom elements, reducing the need for additional styling and layout adjustments.
For cleaner structure, flexibility and consistency the beforeLabel
and afterLabel
snippet blocks are perfect for you.
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 customizes the button's appearance by applying predefined color themes. These themes correspond to various statuses or actions, ensuring consistent visual design throughout your application. The available color options are "brand"
, "error"
, "info"
, "success"
, and "warning"
. The default value 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 determines the overall style and background of the button. It accepts three values: 'default'
, 'light'
, and 'gradient'
. The default theme applies the standard button styles, while the light theme uses lighter versions of the default colors for a softer look. The gradient theme gives a more vibrant feel by applying gradient backgrounds to the button.
Light Theme
The light theme is a softer variation of the default button colors, making it suitable for more subtle designs.
<Button theme="light" color="brand">Brand</Button>
<Button theme="light" color="error">Error</Button>
<Button theme="light" color="info">Info</Button>
<Button theme="light" color="success">Success</Button>
<Button theme="light" 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 animate
determines the speed of the button's animation, with options including "none"
, "slower"
, "slow"
, "normal"
, "fast"
, and "faster"
.
<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>
<Button rounded="none">None</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.