Reference
Button
A button: its label or block is what it shows, on click what it does.
Button(_ label: String, tone: Enum, size: Enum, shape: Enum, …) { … }
Before 5.0
label
tone
size
shape
width
Saved 0 times
Example
state saved = 0
Row(gap: .sm, justify: .end) {
Button("Cancel").outlined
Button("Save draft") { on click { saved = saved + 1 } }
Button("Publish").primary { on click { toast("Published", tone: .success) } }
}
Text("Saved {saved} times").muted
Tones
Row(gap: .sm) {
Button("Primary").primary
Button("Secondary").secondary
Button("Success").success
Button("Danger").danger
Button("Warning").warning
Button("Info").info
}
Sizes, shapes and widths
Stack(gap: .sm) {
Row(gap: .sm, align: .center) {
Button("Small").sm
Button("Default")
Button("Large").lg
}
Row(gap: .sm) { Button("Rounded").rounded Button("Pill").pill.primary Button("Outlined").outlined.danger }
Button("Continue to checkout").primary.full
}
Busy and disabled
state agreed = false
Checkbox(bind: agreed, label: "I agree to the terms")
Row(gap: .sm) {
Button("Create account", disabled: !agreed).primary
Button("Saving", loading: true)
}
A link and a submit
state email = ""
Form {
on submit { toast("Subscribed") }
Input(bind: email, label: "Email").email
Row(gap: .sm) {
Button("Subscribe", type: .submit).primary
Button("Read the guide", to: "/learn").outlined
}
}
Props
| Prop | Type | Unset | Meaning |
|---|---|---|---|
label | String (positional) | none | Its words; a block may hold more (an icon, |
tone: | one of its cases | none: the surface colour with a border | The colour it is filled with, from the theme's tones. Written as a flag,
|
size: | one of its cases | md | Its padding and text size. Written as a flag,
|
shape: | one of its cases | none: | Its corners; unset, the theme's
|
width: | one of its cases | none: as wide as its words | How wide it is. Written as a flag,
|
outlined: | Bool | false | A border and no fill; with |
type: | one of its cases | button | What it does inside a form; any button not
|
to: | Path | none | A route to go to when it is pressed, through the router as a |
disabled: | Bool | false | Whether it is inert: it cannot be pressed or focused. Written as a flag, |
loading: | Bool | false | Busy while it holds — |
Attribute families: global, aria, data. Plus the 22 universal props every element takes: animate:, exit:, speed:, duration:, delay:, easing:, stagger:, on:, count:, shared:, class:, ref:, drag:, dragGroup:, dropValue:, dropAxis:, dropInside:, roving:, trapFocus:, mount:, update:, cleanup:.
Flags and cases
A flag is written after the parentheses with a dot: a Bool prop, or a case of one of the element's enum props. A case the enum lacks is an error with a hint.
.primary
.secondary
.success
.danger
.warning
.info
.sm
.md
.lg
.rounded
.pill
.full
.outlined
.button
.submit
.reset
.disabled
.loading
Events
The DOM events it is drawn to answer. Any other DOM event works too, in an on … { } handler.
| Event | Sends | Meaning |
|---|---|---|
on click | DOM | The element was clicked or activated |
Every element also takes the props, the events and the attribute families listed on every element.
Slots
children
Renders with the class wf-btn, stable across releases, so a stylesheet may target it.