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 danger variant.

States

Small
Large
Disabled
Loading
Link, disabled
Full width

Minimal example

Dart
import 'package:jx_cli/ui.dart';

JxButton(onClick: save, [.text('Save')])

Complete example

A full Jaspr component you can paste into a project.

Dart
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 classes to add or override utilities: they come last, so classes: 'w:full' wins over the default width.
  • Colors come from the theme families blue, gray, red, green and yellow. Redefine them in theme.extend.colors to restyle every component at once.

Responsive

  • Buttons keep their minimum height on touch screens; fullWidth is handy on narrow screens, for example classes: '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 href and carries role="link" and aria-disabled="true".
  • For an icon-only button, give it a name: attributes: {'aria-label': 'Close'}.

Limitations

  • No built-in icon slot: put an svg in children, before or after the text.