# WButton

`WButton` is an interactive component that combines `WAnchor` state management with Tailwind-like utility styling and built-in loading states.

- [Basic Usage](#basic-usage)
- [Constructor](#constructor)
- [Props](#props)
- [Layout Modes](#layout-modes)
- [Event Handling](#event-handling)
- [State Variants](#state-variants)
- [Styling Examples](#styling-examples)
- [All Supported Classes](#all-supported-classes)
- [Customizing Theme](#customizing-theme)
- [Related Documentation](#related-documentation)

```dart
WButton(
  onTap: () => print('Button tapped'),
  className: 'bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg transition-colors',
  child: WText('Click Me'),
)
```

## Basic Usage

The `WButton` widget handles common interactive states automatically. It provides a simple way to create buttons that react to hover, focus, and disabled states without manual state management.

```dart
WButton(
  onTap: _submit,
  className: 'bg-indigo-500 text-white p-3 rounded shadow hover:shadow-lg',
  child: WText('Submit Form'),
)
```

## Constructor

```dart
const WButton({
  Key? key,
  required Widget child,
  VoidCallback? onTap,
  VoidCallback? onLongPress,
  VoidCallback? onDoubleTap,
  bool isLoading = false,
  bool disabled = false,
  String? className,
  String? loadingText,
  Widget? loadingWidget,
  double loadingSize = 16,
  Color? loadingColor,
  Set<String>? states,
  String? semanticLabel,
  FocusNode? focusNode,
  bool autofocus = false,
})
```

## Props

| Prop | Type | Default | Description |
|:-----|:-----|:--------|:------------|
| `child` | `Widget` | **Required** | The button content (usually `WText` or `WIcon`). |
| `className` | `String?` | `null` | Wind utility classes for styling. |
| `onTap` | `VoidCallback?` | `null` | Callback when the button is tapped. Disabled if `isLoading` or `disabled` is true. |
| `onLongPress` | `VoidCallback?` | `null` | Callback for long press gestures. |
| `onDoubleTap` | `VoidCallback?` | `null` | Callback for double tap gestures. |
| `isLoading` | `bool` | `false` | Activates loading state: disables interaction and paints a spinner instead of the child. The button keeps its accessible name (see [Loading States](#loading-states)). |
| `disabled` | `bool` | `false` | Activates disabled state: disables interaction and changes cursor. |
| `loadingText` | `String?` | `null` | Text to display next to the loading spinner. A blank one is not rendered. |
| `loadingWidget` | `Widget?` | `null` | Custom widget to replace the default spinner. `loadingText` is not rendered beside it. |
| `loadingSize` | `double` | `16` | Size of the default loading spinner. |
| `loadingColor` | `Color?` | `null` | Colour of the spinner. Falls back to text colour, then auto-computes contrast via W3C luminance when no colour is resolvable. |
| `states` | `Set<String>?` | `null` | Custom state prefixes (e.g., `{'error'}` for `error:` classes). |
| `semanticLabel` | `String?` | `null` | Accessible name for icon-only controls. When set, excludes the child subtree from semantics so the label overrides any child text; prefer it for icon-only controls rather than buttons that already expose readable text. |
| `focusNode` | `FocusNode?` | `null` | A focus node you own, to move focus to the button or read its focus. The button never disposes it, and swapping it between rebuilds moves the `focus:` styling to the new node. A disabled or loading button cannot take focus, and the node keeps the `canRequestFocus` the button last gave it. Forwarded to `WAnchor`. |
| `autofocus` | `bool` | `false` | Asks for focus when the button first mounts, only when nothing in the focus scope is focused already. A button that is disabled or loading at mount never takes focus, and does not ask again when loading ends. Forwarded to `WAnchor`. |

## Layout Modes

You can control content alignment using standard utility classes.

### Centered Content

By default, using `justify-center` in the `className` will centre the child within the button's constraints.

```dart
WButton(
  className: 'w-full bg-blue-500 justify-center py-3 text-white',
  child: WText('Centered Full Width'),
)
```

## Event Handling

`WButton` exposes standard gesture callbacks. All interaction is automatically suppressed when `isLoading` or `disabled` is set to `true`. A disabled or loading button still appears to assistive technology as a button, marked not enabled, so a screen reader announces it as "dimmed" instead of skipping it. The same holds for a button with no callback at all (`onTap: null`): it is a button that is not enabled, not plain text.

```dart
WButton(
  onTap: () => print('Tapped'),
  onLongPress: () => print('Long Pressed'),
  className: 'bg-gray-200 p-4',
  child: WText('Press Me'),
)
```

## State Variants

`WButton` supports multiple state-based prefixes. These classes are applied only when the corresponding state is active.

```dart
WButton(
  onTap: _submit,
  className: 'bg-blue-600 hover:bg-blue-700 focus:ring-2 disabled:bg-gray-400 loading:opacity-50',
  child: WText('Stateful Button'),
)
```

| Prefix | Trigger |
|:-------|:--------|
| `hover:` | When the mouse pointer is over the button. |
| `focus:` | When the button has keyboard focus. |
| `disabled:` | When the `disabled` prop is `true`. |
| `loading:` | When the `isLoading` prop is `true`. |

## Styling Examples

### Focus Control

Pass a `focusNode` to move focus to a button from code, or `autofocus: true` to take focus on mount. You create and dispose the node; `focus:` classes follow it.

```dart
final FocusNode _saveFocus = FocusNode();

WButton(
  focusNode: _saveFocus,
  onTap: _save,
  className: 'bg-blue-600 focus:ring-2 focus:ring-blue-300 dark:bg-blue-500 dark:focus:ring-blue-700 text-white px-4 py-2 rounded-lg',
  child: WText('Save'),
)

// Elsewhere: _saveFocus.requestFocus();
```

### Loading States

When `isLoading` is true, `WButton` paints a spinner instead of the `child`. You can add text or a custom widget.

```dart
// Basic spinner
WButton(
  isLoading: true,
  className: 'bg-blue-500 text-white p-2',
  child: WText('Login'),
)

// Spinner with text
WButton(
  isLoading: true,
  loadingText: 'Processing...',
  className: 'bg-green-600 text-white px-4 py-2',
  child: WText('Submit'),
)
```

A loading button stays a button for assistive technology, marked not enabled, and keeps an accessible name. The name is, in order:

1. `semanticLabel`, when given.
2. The rendered `loadingText`, when `loadingText` is not blank and `loadingWidget` is null. A blank `loadingText` (empty or whitespace only) is not rendered.
3. The child's own text. The child stays mounted but is not painted (it sits at zero opacity in a `Stack` beside the spinner), so the loading layout is unchanged. It sits under `ExcludeFocus`, so a focusable widget inside it cannot take keyboard focus while hidden.

With a `loadingWidget`, `loadingText` is never rendered, so the child names the button; a `loadingWidget` that carries its own text joins that name, so pass a `semanticLabel` when the two would read badly together. Because the child remains in the tree, a test finder such as `find.text('Save')` still matches it while loading.

> [!NOTE]
> A loading button disables its anchor, so it cannot hold focus: a keyboard or D-pad user who has focus on the button when loading starts loses it.

> [!NOTE]
> When no explicit `loadingColor` is set and no text colour is available from the className, the spinner automatically picks a contrasting colour (light or dark) based on the button's background luminance using the W3C relative luminance algorithm.

### Icon Buttons

Combine `WButton` with `WIcon` for utility-styled icon buttons.

```dart
WButton(
  className: 'bg-red-500 hover:bg-red-600 p-3 rounded-full text-white shadow-lg',
  child: WIcon(Icons.delete),
)
```

## All Supported Classes

`WButton` reads sizing, padding, background, border, radius, shadow, ring, text colour, size and weight, opacity and `justify-center` / alignment. Layout (`flex`, `grid`, `hidden`, `gap`), margin and text transforms are not applied; put them on a wrapping `WDiv` or the child `WText`.

| Category | Classes |
|:---------|:--------|
| Layout | `justify-center` |
| Spacing | `p-{n}`, `px-{n}`, `py-{n}` |
| Sizing | `w-{size}`, `h-{size}`, `min-w-{size}`, `max-w-{size}` |
| Typography | `text-{size}`, `font-{weight}`, `text-{color}` |
| Background | `bg-{color}`, `bg-gradient-to-{dir}` |
| Borders | `border`, `border-{n}`, `rounded`, `rounded-{size}`, `ring` |
| Effects | `shadow`, `opacity-{n}`, `duration-{n}`, `ease-{type}` |

## Customizing Theme

You can customise the default behaviour and appearance of buttons by modifying `WindThemeData`.

```dart
WindThemeData(
  // Customize the base spacing unit used for p-4, m-2, etc.
  baseSpacingUnit: 4.0,
  // Add custom colors for specific button brands
  colors: {
    'brand': Colors.deepPurple,
  },
)
```

## Related Documentation

- [WAnchor](https://anilcancakir.com/wind_kit/docs/v1.13/widgets/w-anchor.md) - The base interactive wrapper
- [WText](https://anilcancakir.com/wind_kit/docs/v1.13/widgets/w-text.md) - For styling button labels
- [Background Colour](https://anilcancakir.com/wind_kit/docs/v1.13/styling/background-color.md) - Colour utilities
- [Text Colour](https://anilcancakir.com/wind_kit/docs/v1.13/typography/text-color.md) - Typography colour utilities
- [Spacing](https://anilcancakir.com/wind_kit/docs/v1.13/layout/spacing.md) - Padding and margin utilities
