> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thehuxdesign.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Button

> Customizable button component with multiple variants, sizes, and loading states

## Overview

`HuxButton` is a versatile button component that provides multiple visual variants, sizes, loading states, and automatic WCAG AA contrast compliance. It adapts seamlessly to light and dark themes.

## Basic Usage

```dart theme={null}
HuxButton(
  onPressed: () => print('Button pressed!'),
  child: Text('Primary Button'),
)
```

## Variants

HuxButton supports four visual variants through the `HuxButtonVariant` enum:

### Primary

The default variant with a filled background using the primary color.

<img src="https://mintcdn.com/thehuxdesign/JcF8YCJAtmofbFq4/images/button-primary.png?fit=max&auto=format&n=JcF8YCJAtmofbFq4&q=85&s=fd502ba06f587901208eed44bdd6bd3b" alt="Primary Button" width="1400" height="300" data-path="images/button-primary.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Primary'),
  variant: HuxButtonVariant.primary, // Default
)
```

### Secondary

A subtle variant with a light background and border.

<img src="https://mintcdn.com/thehuxdesign/JcF8YCJAtmofbFq4/images/button-secondary.png?fit=max&auto=format&n=JcF8YCJAtmofbFq4&q=85&s=e6fcd62175fd77c3d168b237726d9421" alt="Secondary Button" width="1400" height="300" data-path="images/button-secondary.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Secondary'),
  variant: HuxButtonVariant.secondary,
)
```

### Outline

A transparent button with only a border outline.

<img src="https://mintcdn.com/thehuxdesign/JcF8YCJAtmofbFq4/images/button-outline.png?fit=max&auto=format&n=JcF8YCJAtmofbFq4&q=85&s=461bb7c851b080f2044bc3cedee5ac7d" alt="Outline Button" width="1400" height="300" data-path="images/button-outline.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Outline'),
  variant: HuxButtonVariant.outline,
)
```

### Ghost

A minimal transparent button without borders.

<img src="https://mintcdn.com/thehuxdesign/JcF8YCJAtmofbFq4/images/button-ghost.png?fit=max&auto=format&n=JcF8YCJAtmofbFq4&q=85&s=543e603df54f9299c38ee432d3fd5c8f" alt="Ghost Button" width="1400" height="300" data-path="images/button-ghost.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Ghost'),
  variant: HuxButtonVariant.ghost,
)
```

## Sizes

Control button dimensions with three size options:

### Small

Compact size for tight spaces (32px height).

<img src="https://mintcdn.com/thehuxdesign/81AtyNkpXR7HVLTv/images/button-small.png?fit=max&auto=format&n=81AtyNkpXR7HVLTv&q=85&s=e65128f8336f8aa76b53c9a085d0ea51" alt="Small Button" width="1400" height="300" data-path="images/button-small.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Small'),
  size: HuxButtonSize.small,
)
```

### Medium

Default size for most use cases (40px height).

<img src="https://mintcdn.com/thehuxdesign/81AtyNkpXR7HVLTv/images/button-medium.png?fit=max&auto=format&n=81AtyNkpXR7HVLTv&q=85&s=b478900f0bf62a489dbe22a58b83cf59" alt="Medium Button" width="1400" height="300" data-path="images/button-medium.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Medium'),
  size: HuxButtonSize.medium, // Default
)
```

### Large

Prominent size for important actions (48px height).

<img src="https://mintcdn.com/thehuxdesign/81AtyNkpXR7HVLTv/images/button-large.png?fit=max&auto=format&n=81AtyNkpXR7HVLTv&q=85&s=26f4ce2b2d93963874878766faca6a06" alt="Large Button" width="1400" height="300" data-path="images/button-large.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Large'),
  size: HuxButtonSize.large,
)
```

## States

### Loading State

Display a loading indicator while processing.

```dart theme={null}
class LoadingButtonExample extends StatefulWidget {
  @override
  _LoadingButtonExampleState createState() => _LoadingButtonExampleState();
}

class _LoadingButtonExampleState extends State<LoadingButtonExample> {
  bool _isLoading = false;

  void _handlePress() async {
    setState(() {
      _isLoading = true;
    });

    // Simulate async operation
    await Future.delayed(Duration(seconds: 2));

    setState(() {
      _isLoading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return HuxButton(
      onPressed: _isLoading ? null : _handlePress,
      isLoading: _isLoading,
      child: Text('Submit'),
    );
  }
}
```

### Disabled State

Disable button interaction.

```dart theme={null}
HuxButton(
  onPressed: null, // or use isDisabled: true
  child: Text('Disabled Button'),
  isDisabled: true,
)
```

## Icons

Add icons to enhance button meaning:

### Icon with Text

<img src="https://mintcdn.com/thehuxdesign/81AtyNkpXR7HVLTv/images/button-icon.png?fit=max&auto=format&n=81AtyNkpXR7HVLTv&q=85&s=bc1ff218b3556563b6c6d3d66f18b622" alt="Icon with text Button" width="1400" height="300" data-path="images/button-icon.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Save'),
  icon: FeatherIcons.save,
)
```

### Icon-only Button

<img src="https://mintcdn.com/thehuxdesign/81AtyNkpXR7HVLTv/images/button-icon-only.png?fit=max&auto=format&n=81AtyNkpXR7HVLTv&q=85&s=2ee3cdc00090f817484122e444d43bd5" alt="Icon-only Button" width="1400" height="300" data-path="images/button-icon-only.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Icon(FeatherIcons.heart),
  size: HuxButtonSize.small,
)
```

### Square Icon-only Button

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: SizedBox(width: 0), // Icon-only with no text
  icon: FeatherIcons.heart,
  size: HuxButtonSize.medium,
  width: HuxButtonWidth.fixed,
  widthValue: 40, // Square button: 40x40
)
```

## Width Control

Control button width behavior with the `HuxButtonWidth` enum:

### Hug Content (Default)

Button width matches its content.

<img src="https://mintcdn.com/thehuxdesign/81AtyNkpXR7HVLTv/images/button-hug.png?fit=max&auto=format&n=81AtyNkpXR7HVLTv&q=85&s=a168441212370bd37a6f249882158f1e" alt="Hug Content Button" width="1400" height="300" data-path="images/button-hug.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Short Text'),
  // width: HuxButtonWidth.hug (default)
)
```

### Expand to Fill

Button takes full available width.

<img src="https://mintcdn.com/thehuxdesign/81AtyNkpXR7HVLTv/images/button-full.png?fit=max&auto=format&n=81AtyNkpXR7HVLTv&q=85&s=94fdda29c1fda6d8c9d487783514d239" alt="Full Width Button" width="1400" height="300" data-path="images/button-full.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Full Width'),
  width: HuxButtonWidth.expand,
)
```

### Fixed Width

Button has a specific width.

<img src="https://mintcdn.com/thehuxdesign/81AtyNkpXR7HVLTv/images/button-fixed.png?fit=max&auto=format&n=81AtyNkpXR7HVLTv&q=85&s=75a816d83c86aafe056f787489b47921" alt="Fixed Width Button" width="1400" height="300" data-path="images/button-fixed.png" />

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Fixed Width'),
  width: HuxButtonWidth.fixed,
  widthValue: 200, // 200px width
)
```

## Custom Colors

### Using Preset Colors

```dart theme={null}
// Indigo theme
HuxButton(
  onPressed: () {},
  child: Text('Indigo Button'),
  primaryColor: HuxColors.getPresetColor('indigo'),
)

// Green theme
HuxButton(
  onPressed: () {},
  child: Text('Green Button'),
  primaryColor: HuxColors.getPresetColor('green'),
)

// Pink theme
HuxButton(
  onPressed: () {},
  child: Text('Pink Button'),
  primaryColor: HuxColors.getPresetColor('pink'),
)
```

### Custom Colors

```dart theme={null}
HuxButton(
  onPressed: () {},
  child: Text('Custom Purple'),
  primaryColor: Color(0xFF6366F1),
)

HuxButton(
  onPressed: () {},
  child: Text('Custom Orange'),
  primaryColor: Colors.deepOrange,
)
```

## API Reference

### HuxButton Properties

| Property       | Type               | Default      | Description                                   |
| -------------- | ------------------ | ------------ | --------------------------------------------- |
| `onPressed`    | `VoidCallback?`    | **required** | Callback triggered when button is pressed     |
| `child`        | `Widget`           | **required** | The child widget to display inside the button |
| `variant`      | `HuxButtonVariant` | `primary`    | Visual variant of the button                  |
| `size`         | `HuxButtonSize`    | `medium`     | Size variant of the button                    |
| `isLoading`    | `bool`             | `false`      | Whether to show loading indicator             |
| `isDisabled`   | `bool`             | `false`      | Whether the button is disabled                |
| `icon`         | `IconData?`        | `null`       | Optional icon to display before text          |
| `primaryColor` | `Color?`           | `null`       | Custom primary color (overrides theme)        |

### HuxButtonVariant Enum

* `HuxButtonVariant.primary` - Filled background with primary color
* `HuxButtonVariant.secondary` - Light background with border
* `HuxButtonVariant.outline` - Transparent with border only
* `HuxButtonVariant.ghost` - Transparent without border

### HuxButtonSize Enum

* `HuxButtonSize.small` - 32px height, compact padding
* `HuxButtonSize.medium` - 40px height, standard padding
* `HuxButtonSize.large` - 48px height, generous padding

## Common Patterns

### Form Buttons

```dart theme={null}
Row(
  children: [
    Expanded(
      child: HuxButton(
        onPressed: () {},
        child: Text('Cancel'),
        variant: HuxButtonVariant.outline,
      ),
    ),
    SizedBox(width: 12),
    Expanded(
      child: HuxButton(
        onPressed: () {},
        child: Text('Submit'),
        variant: HuxButtonVariant.primary,
      ),
    ),
  ],
)
```

### Action Buttons

```dart theme={null}
Column(
  children: [
    HuxButton(
      onPressed: () {},
      child: Text('Primary Action'),
      variant: HuxButtonVariant.primary,
      size: HuxButtonSize.large,
    ),
    SizedBox(height: 12),
    HuxButton(
      onPressed: () {},
      child: Text('Secondary Action'),
      variant: HuxButtonVariant.secondary,
    ),
    SizedBox(height: 8),
    HuxButton(
      onPressed: () {},
      child: Text('Cancel'),
      variant: HuxButtonVariant.ghost,
    ),
  ],
)
```

### Button Grid

```dart theme={null}
GridView.count(
  crossAxisCount: 2,
  shrinkWrap: true,
  children: [
    HuxButton(
      onPressed: () {},
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(FeatherIcons.camera),
          SizedBox(height: 8),
          Text('Camera'),
        ],
      ),
      variant: HuxButtonVariant.outline,
    ),
    HuxButton(
      onPressed: () {},
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(FeatherIcons.image),
          SizedBox(height: 8),
          Text('Gallery'),
        ],
      ),
      variant: HuxButtonVariant.outline,
    ),
  ],
)
```

## Accessibility

HuxButton includes several accessibility features:

### Automatic Contrast

Text color is automatically calculated to ensure WCAG AA compliance (4.5:1 contrast ratio) against any background color.

### Semantic Roles

Buttons include proper semantic roles for screen readers.

### Focus States

Keyboard navigation support with visible focus indicators.

### Disabled States

Proper disabled state handling for assistive technologies.

## Best Practices

<AccordionGroup>
  <Accordion title="Choose the Right Variant" icon="eye">
    * Use **Primary** for the main action on a screen
    * Use **Secondary** for important but not primary actions
    * Use **Outline** for secondary actions that need visual weight
    * Use **Ghost** for subtle actions like "Cancel" or "Skip"
  </Accordion>

  <Accordion title="Size Appropriately" icon="ruler">
    * Use **Large** for important call-to-action buttons
    * Use **Medium** for most standard interactions
    * Use **Small** for compact interfaces or secondary actions
  </Accordion>

  <Accordion title="Loading States" icon="loader">
    ```dart theme={null}
    // Always disable the button during loading
    HuxButton(
      onPressed: _isLoading ? null : _handlePress,
      isLoading: _isLoading,
      child: Text('Submit'),
    )
    ```
  </Accordion>

  <Accordion title="Clear Labels" icon="type">
    * Use action-oriented labels: "Save", "Delete", "Continue"
    * Avoid generic labels: "OK", "Submit", "Button"
    * Keep labels concise but descriptive
  </Accordion>
</AccordionGroup>

## Examples

<CardGroup cols={2}>
  <Card title="Form Building" icon="edit" href="/examples/form-building">
    See buttons in action within forms
  </Card>

  <Card title="Loading States" icon="loader" href="/examples/loading-states">
    Learn how to handle async operations
  </Card>

  <Card title="Custom Theming" icon="palette" href="/examples/custom-theming">
    Apply custom colors and themes
  </Card>

  <Card title="Accessibility" icon="universal-access" href="/advanced/accessibility">
    Ensure your buttons are accessible
  </Card>
</CardGroup>
