Actions
Button
A button in five variants and three sizes, with a loading state. With href it renders a link styled as a button.
JxButton
import 'package:jx_cli/ui.dart'
Available since 0.0.1
Preview
When to use it
- Submitting a form or confirming an action.
-
A call to action that navigates (
href). -
A destructive action, with the
dangervariant.
States
Minimal example
import 'package:jx_cli/ui.dart';
JxButton(onClick: save, [.text('Save')])
Complete example
A full Jaspr component you can paste into a project.
import 'package:jaspr/jaspr.dart';
import 'package:jx_cli/ui.dart';
/// Saves a draft and shows a spinner while the request runs.
class SaveButton extends StatefulComponent {
const SaveButton({required this.onSave, super.key});
final Future<void> Function() onSave;
@override
State<SaveButton> createState() => _SaveButtonState();
}
class _SaveButtonState extends State<SaveButton> {
bool saving = false;
Future<void> _save() async {
setState(() => saving = true);
try {
await component.onSave();
} finally {
setState(() => saving = false);
}
}
@override
Component build(BuildContext context) {
return JxButton(
loading: saving,
onClick: _save,
[.text(saving ? 'Saving' : 'Save draft')],
);
}
}
Properties
| Property | Type | Default | Description |
|---|---|---|---|
children |
List<Component> |
required |
Content: text, icon, or both. |
variant |
JxButtonVariant |
primary |
primary
,
secondary
,
outline
,
ghost
or
danger
.
|
size |
JxButtonSize |
md |
sm
(32 px),
md
(40 px) or
lg
(48 px) minimum height.
|
href |
String? |
null |
Renders an
<a>
instead of a
<button>
.
|
type |
ButtonType |
button |
submit
to submit the enclosing form.
|
disabled |
bool |
false |
Disables the button. A link loses its
href
and gets
aria-disabled
.
|
loading |
bool |
false |
Shows a spinner, disables the button and sets
aria-busy
.
|
fullWidth |
bool |
false |
Stretches the button to the width of its container. |
classes |
String? |
null |
Extra JX classes, appended after the component classes. |
attributes |
Map<String, String>? |
null |
Extra HTML attributes, such as
aria-label
for an icon-only button.
|
Related types
| Name | Role |
|---|---|
JxButtonVariant
|
primary, secondary, outline, ghost, danger. |
JxButtonSize |
sm, md, lg. |
Events
| Callback | Type | Fires when |
|---|---|---|
onClick |
VoidCallback? |
The button or link is activated (click, Enter or Space). |
Customizing
-
Pass
classesto add or override utilities: they come last, soclasses: 'w:full'wins over the default width. -
Colors come from the theme families
blue,gray,red,greenandyellow. Redefine them intheme.extend.colorsto restyle every component at once.
Responsive
-
Buttons keep their minimum height on touch screens;
fullWidthis handy on narrow screens, for exampleclasses: 'w:full md:w:auto'.
Accessibility
-
A native
<button type="button">: focusable, activated with Enter and Space. -
A visible focus ring appears on keyboard focus only (
focus-visible). -
While loading,
aria-busy="true"is set and the spinner is hidden from screen readers. -
A disabled link has no
hrefand carriesrole="link"andaria-disabled="true". -
For an icon-only button, give it a name:
attributes: {'aria-label': 'Close'}.
Limitations
-
No built-in icon slot: put an
svginchildren, before or after the text.