# Button

Buttons allow users to trigger an action or to navigate to another page. They are available in multiple styles for various purposes. Buttons are ideal for calling attention to actions users need to perform in order to move forward in a flow.

## Example

```python
from deephaven import ui

btn = ui.button("Press me", on_press=lambda: print("Button clicked"))
```

## UI recommendations

Recommendations for creating clear and effective buttons:

1. Button text should be clear and concise. No more than 4 words or 20 characters is recommended.
2. Use verbs for button text to indicate the action that will be taken when the button is clicked. For example, “Save”, “Delete”, or “Add friend”, rather than “Ok” or “Cancel”. Nouns or adjectives tend to be less clear.
3. Use sentence case for button text with more than one word. For example, “Add friend” instead of “Add Friend” or “ADD FRIEND”.
4. No punctuation is needed at the end of the button text.
5. Use icons only when necessary, and not as a replacement for text or decoration. Icons should be used to provide additional context to the button’s action.
6. When presenting choices, use a single filled accent button to suggest to users the recommended choice, paired with outlined primary or secondary buttons for the other options. This helps to visually distinguish the primary action from the secondary actions.
7. Use negative buttons sparingly, as they can be visually distracting. They should be used for actions that are destructive or irreversible.

Consider using [`action_button`](action_button.md) for task-based actions, or in cases where buttons aren’t meant to draw attention to themselves. To represent a binary choice, use a [`toggle_button`](toggle_button.md) instead. If you have a collection of related buttons, you can group them using a [`button_group`](button_group.md).

## Events

Buttons accept a value to display and can trigger actions based on events such as setting state when pressed. See the [API Reference]() for a full list of available events.

```python
from deephaven import ui


@ui.component
def counter():
    count, set_count = use_state(0)
    return ui.button(
        f"Pressed {count} times",
        on_press=lambda: set_count(count + 1),
    )


counter_example = counter()
```

## Variants

Buttons can have different styles to indicate their purpose.

```python
from deephaven import ui


@ui.component
def button_variants():
    return [
        ui.button("Accent fill", variant="accent", style="fill"),
        ui.button("Accent outline", variant="accent", style="outline"),
        ui.button("Primary fill", variant="primary", style="fill"),
        ui.button("Primary outline", variant="primary", style="outline"),
        ui.button("Secondary fill", variant="secondary", style="fill"),
        ui.button("Secondary outline", variant="secondary", style="outline"),
        ui.button("Negative fill", variant="negative", style="fill"),
        ui.button("Negative outline", variant="negative", style="outline"),
    ]


button_variants_example = button_variants()
```

Static-color buttons are available in white and black. They don’t dynamically change in response to the user’s theme. They should only be used over fixed-color backgrounds, not over theme colors that may change.

```python
from deephaven import ui


@ui.component
def static_buttons():
    return [
        ui.view(
            ui.button_group(
                ui.button("White fill", static_color="white", style="fill"),
                ui.button(
                    "White outline",
                    static_color="white",
                    style="outline",
                ),
            ),
            background_color="#000066",
            padding="size-300",
        ),
        ui.view(
            ui.button_group(
                ui.button("Black fill", static_color="black", style="fill"),
                ui.button(
                    "Black outline",
                    static_color="black",
                    style="outline",
                ),
            ),
            background_color="#FFFF00",
            padding="size-300",
        ),
    ]


static_buttons_example = static_buttons()
```

## Icon buttons

Buttons can have icons when necessary to provide additional context. If no visible label is provided (e.g., an icon-only button), an alternative text label must be provided to identify the control for accessibility using the `aria-label` prop. See [icon](icon.md) for a list of available icons.

```python
from deephaven import ui


@ui.component
def icon_buttons():
    return [
        ui.button(ui.icon("squirrel"), "Squirrel"),
        ui.button(ui.icon("squirrel"), aria_label="Squirrel"),
    ]


icon_buttons_example = icon_buttons()
```

## Pending state

Buttons can be in a pending state to indicate that an action is in progress (such as an asynchronous server request). After a one-second delay, an indeterminate spinner will be displayed in place of the button label and icon. You can trigger this behavior by setting the `is_pending` prop. Button events are disabled while `is_pending` is true.

```python
from deephaven import ui
from threading import Timer


@ui.component
def pending_button():
    [pending, set_pending] = ui.use_state(False)

    def handle_on_press():
        # start an asynchronous thing
        timeout = Timer(3, callback_finshed)  # use a timer to wait 3 seconds
        timeout.start()

        # turn on loading spinner
        set_pending(True)

    def callback_finshed():
        # turn of loading spinner
        set_pending(False)

    return ui.button(
        "Pending request",
        on_press=handle_on_press,
        is_pending=pending,
        variant="accent",
    )


pending_example = pending_button()
```

## Disabled state

Buttons can be disabled to prevent user interaction. This is useful when the button is not available for interaction, but should still be visible.

```python
from deephaven import ui

btn = ui.button("Disabled button", is_disabled=True)
```

## Button links

Buttons can be used as links to navigate to another page if the `href` attribute is provided.

```python
from deephaven import ui

btn = ui.button("Go to deephaven.io", href="https://deephaven.io")
```

## API reference

Buttons allow users to perform an action or to navigate to another page. They have multiple styles for various needs, and are ideal for calling attention to where a user needs to do something in order to move forward in a flow.
Python implementation for the Adobe React Spectrum Button component: https://react-spectrum.adobe.com/react-spectrum/Button.html

**Returns:** `Element` The rendered button component.

<ParamTable param={{"module_name": "deephaven.ui.", "name": "button", "parameters": [{"name": "*children", "type": "Any", "description": "The contents to display inside the button."}, {"name": "variant", "type": "Literal['accent', 'primary', 'secondary', 'negative'] | None", "description": "The visual style of the button.", "default": "'accent'"}, {"name": "style", "type": "Literal['fill', 'outline'] | None", "description": "The background style of the button.", "default": "'fill'"}, {"name": "static_color", "type": "Literal['white', 'black'] | None", "description": "The static color style to apply. Useful when the button appears over a color background.", "default": "None"}, {"name": "is_pending", "type": "bool | None", "description": "Whether to disable events immediately and display a loading spinner after a 1 second delay.", "default": "None"}, {"name": "type", "type": "Literal['button', 'submit', 'reset']", "description": "The behavior of the button when used in an HTML form.", "default": "'button'"}, {"name": "is_disabled", "type": "bool | None", "description": "Whether the button is disabled.", "default": "None"}, {"name": "auto_focus", "type": "bool | None", "description": "Whether the button should automatically receive focus when the page loads.", "default": "None"}, {"name": "href", "type": "str | None", "description": "A URL to link to when the button is pressed.", "default": "None"}, {"name": "target", "type": "str | None", "description": "The target window or tab to open the linked URL in.", "default": "None"}, {"name": "rel", "type": "str | None", "description": "The relationship between the current document and the linked URL.", "default": "None"}, {"name": "on_press", "type": "Callable[[PressEvent], None] | None", "description": "Function called when the button is pressed.", "default": "None"}, {"name": "on_press_start", "type": "Callable[[PressEvent], None] | None", "description": "Function called when the button is pressed and held.", "default": "None"}, {"name": "on_press_end", "type": "Callable[[PressEvent], None] | None", "description": "Function called when the button is released after being pressed.", "default": "None"}, {"name": "on_press_up", "type": "Callable[[PressEvent], None] | None", "description": "Function called when the button is released.", "default": "None"}, {"name": "on_press_change", "type": "Callable[[bool], None] | None", "description": "Function called when the pressed state changes.", "default": "None"}, {"name": "on_focus", "type": "Callable[[FocusEvent], None] | None", "description": "Function called when the button receives focus.", "default": "None"}, {"name": "on_blur", "type": "Callable[[FocusEvent], None] | None", "description": "Function called when the button loses focus.", "default": "None"}, {"name": "on_focus_change", "type": "Callable[[bool], None] | None", "description": "Function called when the focus state changes.", "default": "None"}, {"name": "on_key_down", "type": "Callable[[KeyboardEvent], None] | None", "description": "Function called when a key is pressed down.", "default": "None"}, {"name": "on_key_up", "type": "Callable[[KeyboardEvent], None] | None", "description": "Function called when a key is released.", "default": "None"}, {"name": "flex", "type": "str | float | bool | None", "description": "When used in a flex layout, specifies how the element will grow or shrink to fit the space available.", "default": "None"}, {"name": "flex_grow", "type": "float | None", "description": "When used in a flex layout, specifies how much the element will grow to fit the space available.", "default": "None"}, {"name": "flex_shrink", "type": "float | None", "description": "When used in a flex layout, specifies how much the element will shrink to fit the space available.", "default": "None"}, {"name": "flex_basis", "type": "str | float | None", "description": "When used in a flex layout, specifies the initial size of the element.", "default": "None"}, {"name": "align_self", "type": "Literal['auto', 'normal', 'start', 'end', 'center', 'flex-start', 'flex-end', 'self-start', 'self-end', 'stretch'] | None", "description": "Overrides the alignItems property of a flex or grid container.", "default": "None"}, {"name": "justify_self", "type": "Literal['auto', 'normal', 'start', 'end', 'flex-start', 'flex-end', 'self-start', 'self-end', 'center', 'left', 'right', 'stretch'] | None", "description": "Specifies how the element is justified inside a flex or grid container.", "default": "None"}, {"name": "order", "type": "int | None", "description": "The layout for the element within a flex or grid container.", "default": "None"}, {"name": "grid_area", "type": "str | None", "description": "The name of grid area to place the element in.", "default": "None"}, {"name": "grid_column", "type": "str | None", "description": "The name of grid column to place the element in.", "default": "None"}, {"name": "grid_row", "type": "str | None", "description": "The name of grid row to place the element in.", "default": "None"}, {"name": "grid_column_start", "type": "str | None", "description": "The name of grid column to start the element in.", "default": "None"}, {"name": "grid_column_end", "type": "str | None", "description": "The name of grid column to end the element in.", "default": "None"}, {"name": "grid_row_start", "type": "str | None", "description": "The name of grid row to start the element in.", "default": "None"}, {"name": "grid_row_end", "type": "str | None", "description": "The name of grid row to end the element in.", "default": "None"}, {"name": "margin", "type": "str | float | None", "description": "The margin around the element.", "default": "None"}, {"name": "margin_top", "type": "str | float | None", "description": "The margin above the element.", "default": "None"}, {"name": "margin_bottom", "type": "str | float | None", "description": "The margin below the element.", "default": "None"}, {"name": "margin_start", "type": "str | float | None", "description": "The margin for the logical start side of the element, depending on layout direction.", "default": "None"}, {"name": "margin_end", "type": "str | float | None", "description": "The margin for the logical end side of the element, depending on layout direction.", "default": "None"}, {"name": "margin_x", "type": "str | float | None", "description": "The margin for the horizontal sides of the element.", "default": "None"}, {"name": "margin_y", "type": "str | float | None", "description": "The margin for the vertical sides of the element.", "default": "None"}, {"name": "width", "type": "str | float | None", "description": "The width of the element.", "default": "None"}, {"name": "min_width", "type": "str | float | None", "description": "The minimum width of the element.", "default": "None"}, {"name": "max_width", "type": "str | float | None", "description": "The maximum width of the element.", "default": "None"}, {"name": "height", "type": "str | float | None", "description": "The height of the element.", "default": "None"}, {"name": "min_height", "type": "str | float | None", "description": "The minimum height of the element.", "default": "None"}, {"name": "max_height", "type": "str | float | None", "description": "The maximum height of the element.", "default": "None"}, {"name": "position", "type": "Literal['static', 'relative', 'absolute', 'fixed', 'sticky'] | None", "description": "The position of the element.", "default": "None"}, {"name": "top", "type": "str | float | None", "description": "The distance from the top of the containing element.", "default": "None"}, {"name": "bottom", "type": "str | float | None", "description": "The distance from the bottom of the containing element.", "default": "None"}, {"name": "left", "type": "str | float | None", "description": "The distance from the left of the containing element.", "default": "None"}, {"name": "right", "type": "str | float | None", "description": "The distance from the right of the containing element.", "default": "None"}, {"name": "start", "type": "str | float | None", "description": "The distance from the start of the containing element, depending on layout direction.", "default": "None"}, {"name": "end", "type": "str | float | None", "description": "The distance from the end of the containing element, depending on layout direction.", "default": "None"}, {"name": "z_index", "type": "int | None", "description": "The stack order of the element.", "default": "None"}, {"name": "is_hidden", "type": "bool | None", "description": "Whether the element is hidden.", "default": "None"}, {"name": "id", "type": "str | None", "description": "The unique identifier of the element.", "default": "None"}, {"name": "exclude_from_tab_order", "type": "bool | None", "description": "Whether the element should be excluded from the tab order.", "default": "None"}, {"name": "aria_expanded", "type": "Literal['true', 'false'] | bool | None", "description": "Whether the element is expanded.", "default": "None"}, {"name": "aria_has_popup", "type": "Literal['true', 'false'] | bool | Literal['menu', 'listbox', 'tree', 'grid', 'dialog'] | None", "description": "Whether the element has a popup.", "default": "None"}, {"name": "aria_controls", "type": "str | None", "description": "The id of the element controlled by the current element.", "default": "None"}, {"name": "aria_pressed", "type": "Literal['true', 'false'] | bool | Literal['mixed'] | None", "description": "Whether the element is pressed.", "default": "None"}, {"name": "aria_label", "type": "str | None", "description": "The label for the element.", "default": "None"}, {"name": "aria_labelled_by", "type": "str | None", "description": "The id of the element that labels the current element.", "default": "None"}, {"name": "aria_described_by", "type": "str | None", "description": "The id of the element that describes the current element.", "default": "None"}, {"name": "aria_details", "type": "str | None", "description": "The details of the current element.", "default": "None"}, {"name": "UNSAFE_class_name", "type": "str | None", "description": "A CSS class to apply to the element.", "default": "None"}, {"name": "UNSAFE_style", "type": "Dict[str, Any] | None", "description": "A CSS style to apply to the element.", "default": "None"}, {"name": "key", "type": "str | None", "description": "A unique identifier used by React to render elements in a list.", "default": "None"}]}} />
