lustre/ui/button
The button
element is a clickable control that receives keyboard
and pointer events that you would use to dispatch messages to your update
function when interacted with.
Common uses for buttons include:
-
Submitting forms or triggering the submission of data.
-
As navigation elements when using the stylised
link
variant. -
Opening and closing dialogs, menus, and other interactive components.
Anatomy
A button is made up of different parts:
-
The main
button
container used to control the button’s styles and layout. (required) -
One or more items of content like text or icons that provide visual and semantic information about the button’s purpose. (required)
Recipes
Below are some recipes that show common uses of the button
element.
A basic button with an icon and text:
import lustre/element/html
import lustre/event
import lustre/ui/button.{button}
import lustre/ui/icon
pub fn save(handle_click) {
button([event.on_click(handle_click)], [
icon.save([]),
html.text("Save"),
])
}
A command palette button with a keyboard shortcut badge:
import lustre/element/html
import lustre/event
import lustre/ui/button.{button}
pub fn command_palette(handle_click) {
button([button.solid(), event.on_click(handle_click)], [
html.text("Open"),
button.shortcut_badge([], ["⌘", "k"])
])
}
Customisation
The style or look of a button can be controlled by using one of the following attributes:
The size of a button can be controlled by using one of the following attributes:
The border radius of a button can be controlled while still using your theme’s configuration by using one of the following attributes:
It is possible to control some aspects of a button’s styling through CSS
variables. You may want to do this in cases where you are integrating lustre/ui
into an existing design system and you want the button
element to match
elements outside of this package.
The following CSS variables can set in your own stylesheets or by using the corresponding attribute functions in this module:
--background
--background-hover
--border
--border-hover
--border-width
--height
--min-height
--padding-x
--radius
--text
Functions
pub fn background(value: String) -> Attribute(a)
pub fn background_hover(value: String) -> Attribute(a)
pub fn badge(
attributes: List(Attribute(a)),
children: List(Element(a)),
) -> Element(a)
Renders a small badge inside a button. Badges are used to display additional information such as a keyboard shortcut or a count of items.
pub fn border_hover(value: String) -> Attribute(a)
pub fn border_width(value: String) -> Attribute(a)
pub fn button(
attributes: List(Attribute(a)),
children: List(Element(a)),
) -> Element(a)
The button
element is a clickable control that receives keyboard and pointer
events that you would use to dispatch messages to your update
function when
interacted with.
Note: in Safari, buttons aren’t part of the tab order by default. To make
them focusable, buttons in Lustre UI have their tabindex
attribute set to
0
for consistent behaviour across browsers.
pub fn count_badge(
attributes: List(Attribute(a)),
count: Int,
) -> Element(a)
Renders a small numerical count as a badge inside a button. This could be used to represent the number of items in a list or the number of unread notifications.
If the count is greater than 99, the badge will display "99+"
.
pub fn link(
attributes: List(Attribute(a)),
children: List(Element(a)),
) -> Element(a)
Styles a HTML <a>
element as a button. You should use this element when
you want to create a button that navigates to a different page or location.
pub fn min_height(value: String) -> Attribute(a)
pub fn of(
element: fn(List(Attribute(a)), List(Element(a))) -> Element(a),
attributes: List(Attribute(a)),
children: List(Element(a)),
) -> Element(a)
In the uncommon case that you need to render a button as a different HTML element, you can use this function to supply the element you want to use. This can be useful if you have an element that implements the functionality of a button but and you to add Lustre UI’s button styles to it.
Note: If you are styling a different element as a button, it’s crucial
to ensure that the element is accessible and behaves correctly. In most cases,
this means setting the role
attribute to "button"
and ensuring that the
element handles both click and keyboard events.
pub fn shortcut_badge(
attributes: List(Attribute(a)),
chord: List(String),
) -> Element(a)
Renders a small badge inside a button that represents a keyboard shortcut that would trigger the button’s action.
You can add a separator between keys by setting the --separator
css variable:
import lustre/attribute
import lustre/ui/button
pub fn cmd_k_badge() {
let chord = ["⌘", "k"]
let styles = attribute.style([#("--separator", "'+'")])
button.shortcut_badge([styles], chord)
}