# Flex

A [flexbox](https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox)-based layout container that utilizes dimension values and supports the gap property for consistent spacing between items.

## Example

```python
from deephaven import ui


@ui.component
def ui_flex():
    return ui.flex(
        ui.view(1, background_color="red", height="size-800", width="size-800"),
        ui.view(2, background_color="green", height="size-800", width="size-800"),
        ui.view(3, background_color="blue", height="size-800", width="size-800"),
    )


my_flex = ui_flex()
```

## Direction

The `direction` prop determines the direction in which the flex items are laid out.

Options:

- `row` (default): the flex items are arranged horizontally from left to right.
- `column`: the flex items are arranged vertically from top to bottom.
- `row-reverse`: the flex items are arranged horizontally from right to left.
- `column-reverse`: the flex items are arranged vertically from bottom to top.

```python
from deephaven import ui


@ui.component
def ui_flex_direction():
    return [
        'direction="row"',
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-800"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-800"),
        ),
        'direction="row-reverse"',
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-800"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-800"),
            direction="row-reverse",
        ),
        'direction="column"',
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-800"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-800"),
            direction="column",
        ),
        'direction="column-reverse"',
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-800"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-800"),
            direction="column-reverse",
        ),
    ]


my_flex_direction = ui_flex_direction()
```

## Nesting

Flexboxes can be nested to create more complicated layouts. By using the `flex` prop on the children, the flexbox can expand to fill the remaining space.

```python
from deephaven import ui


@ui.component
def ui_flex_nesting():
    return [
        ui.flex(
            ui.view(1, background_color="red", height="size-800"),
            ui.flex(
                ui.view(
                    2, background_color="green", height="size-800", width="size-800"
                ),
                ui.view(
                    3, background_color="blue", height="size-800", width="size-800"
                ),
            ),
            direction="column",
        ),
    ]


my_flex_nesting = ui_flex_nesting()
```

## Wrapping

When enabled, items that overflow wrap into the next row. Resize your browser window to see the items reflow.

```python
from deephaven import ui


@ui.component
def ui_flex_wrap():
    return ui.flex(
        ui.view(1, background_color="red", height="size-800", width="size-800"),
        ui.view(2, background_color="green", height="size-800", width="size-800"),
        ui.view(3, background_color="yellow", height="size-800", width="size-800"),
        ui.view(4, background_color="blue", height="size-800", width="size-800"),
        ui.view(5, background_color="orange", height="size-800", width="size-800"),
        wrap=True,
        width="200px",
        align_content="start",
    )


my_flex_wrap = ui_flex_wrap()
```

## Justification

The `justify_content` prop is used to align items along the main axis. When the direction is set to “column”, it controls the vertical alignment, and when the direction is set to “row”, it controls the horizontal alignment.

Options:

- `stretch` (default): the flex items are stretched to fill the container along the cross-axis.
- `start`: the flex items are aligned at the start of the cross-axis.
- `end`: the flex items are aligned at the end of the cross-axis.
- `center`: the flex items are centered along the cross-axis.
- `left`: the flex items are packed toward the left edge of the container.
- `right`: the flex items are packed toward the right edge of the container.
- `space-between`: the flex items are evenly distributed with the first item at the start and the last item at the end.
- `space-around`: the flex items are evenly distributed with equal space around them.
- `space-evenly`: the flex items are evenly distributed with equal space between them.
- `baseline`: the flex items are aligned based on their baselines.
- `first baseline`: the flex items are aligned based on the first baseline of the container.
- `last baseline`: the flex items are aligned based on the last baseline of the container.
- `safe center`: the flex items are centered along the cross-axis, ensuring they remain within the safe area.
- `unsafe center`: the flex items are centered along the cross-axis, without considering the safe area.

```python
from deephaven import ui


@ui.component
def ui_flex_justify():
    start = ui.flex(
        ui.view(1, background_color="red", height="size-800", width="size-400"),
        ui.view(2, background_color="green", height="size-800", width="size-800"),
        ui.view(3, background_color="blue", height="size-800", width="size-200"),
        justify_content="start",
    )
    center = ui.flex(
        ui.view(1, background_color="red", height="size-800", width="size-400"),
        ui.view(2, background_color="green", height="size-800", width="size-800"),
        ui.view(3, background_color="blue", height="size-800", width="size-200"),
        justify_content="center",
    )
    end = ui.flex(
        ui.view(1, background_color="red", height="size-800", width="size-400"),
        ui.view(2, background_color="green", height="size-800", width="size-800"),
        ui.view(3, background_color="blue", height="size-800", width="size-200"),
        justify_content="end",
    )
    space_between = ui.flex(
        ui.view(1, background_color="red", height="size-800", width="size-400"),
        ui.view(2, background_color="green", height="size-800", width="size-800"),
        ui.view(3, background_color="blue", height="size-800", width="size-200"),
        justify_content="space-between",
    )
    space_around = ui.flex(
        ui.view(1, background_color="red", height="size-800", width="size-400"),
        ui.view(2, background_color="green", height="size-800", width="size-800"),
        ui.view(3, background_color="blue", height="size-800", width="size-200"),
        justify_content="space-around",
    )

    return ui.flex(
        'justify_content="start"',
        start,
        'justify_content="center"',
        center,
        'justify_content="end"',
        end,
        'justify_content="space-between"',
        space_between,
        'justify_content="space-around"',
        space_around,
        direction="column",
    )


my_flex_justify = ui_flex_justify()
```

## Alignment

The `align_items` prop aligns items along the cross-axis. When the direction is set to “column”, it controls horizontal alignment, and when it is set to “row”, it controls vertical alignment.

Options:

- `stretch` (default): the flex items are stretched to fill the container along the cross-axis.
- `start`: the flex items are aligned at the start of the cross-axis.
- `end`: the flex items are aligned at the end of the cross-axis.
- `center`: the flex items are centered along the cross-axis.
- `self-start`: the flex items are aligned at the start of their container.
- `self-end`: the flex items are aligned at the end of their container.
- `baseline`: the flex items are aligned based on their baselines.
- `first baseline`: the flex items are aligned based on the first baseline of the container.
- `last baseline`: the flex items are aligned based on the last baseline of the container.
- `safe center`: the flex items are centered along the cross-axis, ensuring they remain within the safe area.
- `unsafe center`: the flex items are centered along the cross-axis, without considering the safe area.

```python
from deephaven import ui


@ui.component
def ui_flex_align_vertical():
    vertical = ui.flex(
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-400"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-200"),
            direction="column",
            align_items="start",
        ),
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-400"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-200"),
            direction="column",
            align_items="center",
        ),
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-400"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-200"),
            direction="column",
            align_items="end",
        ),
    )

    return ui.flex(vertical)


my_flex_align_vertical = ui_flex_align_vertical()
```

```python
from deephaven import ui


@ui.component
def ui_flex_align_horizontal():
    horizontal = ui.flex(
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-400"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-200"),
            align_items="start",
        ),
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-400"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-200"),
            align_items="center",
        ),
        ui.flex(
            ui.view(1, background_color="red", height="size-800", width="size-400"),
            ui.view(2, background_color="green", height="size-800", width="size-800"),
            ui.view(3, background_color="blue", height="size-800", width="size-200"),
            align_items="end",
        ),
        direction="column",
    )

    return ui.flex(horizontal)


my_flex_align_horizontal = ui_flex_align_horizontal()
```

## API reference

Base Flex component for laying out children in a flexbox.

**Returns:** `Element` The rendered flex box.

<ParamTable param={{"module_name": "deephaven.ui.", "name": "flex", "parameters": [{"name": "*children", "type": "Any", "description": "Elements to render in the flexbox."}, {"name": "direction", "type": "Literal['row', 'column', 'row-reverse', 'column-reverse'] | None", "description": "The direction in which to layout children.", "default": "None"}, {"name": "wrap", "type": "Literal['wrap', 'nowrap', 'wrap-reverse'] | None", "description": "Whether children should wrap when they exceed the panel's width.", "default": "None"}, {"name": "justify_content", "type": "Literal['start', 'end', 'center', 'left', 'right', 'space-between', 'space-around', 'space-evenly', 'stretch', 'baseline', 'first baseline', 'last baseline', 'safe center', 'unsafe center'] | None", "description": "The distribution of space around items along the main axis.", "default": "None"}, {"name": "align_content", "type": "Literal['start', 'end', 'center', 'space-between', 'space-around', 'space-evenly', 'stretch', 'baseline', 'first baseline', 'last baseline', 'safe center', 'unsafe center'] | None", "description": "The distribution of space between and around items along the cross axis.", "default": "None"}, {"name": "align_items", "type": "Literal['start', 'end', 'center', 'stretch', 'self-start', 'self-end', 'baseline', 'first baseline', 'last baseline', 'safe center', 'unsafe center'] | None", "description": "The alignment of children within their container.", "default": "None"}, {"name": "gap", "type": "str | float | None", "description": "The space to display between both rows and columns of children.", "default": "'size-100'"}, {"name": "column_gap", "type": "str | float | None", "description": "The space to display between columns of children.", "default": "None"}, {"name": "row_gap", "type": "str | float | None", "description": "The space to display between rows of children.", "default": "None"}, {"name": "flex", "type": "str | float | bool", "description": "When used in a flex layout, specifies how the element will grow or shrink to fit the space available.", "default": "'auto'"}, {"name": "flex_grow", "type": "float | None", "description": "When used in a flex layout, specifies how 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 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 main 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": "Species how the element is justified inside a flex or grid container.", "default": "None"}, {"name": "order", "type": "int | None", "description": "The layout order for the element within a flex or grid container.", "default": "None"}, {"name": "grid_area", "type": "str | None", "description": "When used in a grid layout specifies, specifies the named grid area that the element should be placed in within the grid.", "default": "None"}, {"name": "grid_row", "type": "str | None", "description": "When used in a grid layout, specifies the row the element should be placed in within the grid.", "default": "None"}, {"name": "grid_column", "type": "str | None", "description": "When used in a grid layout, specifies the column the element should be placed in within the grid.", "default": "None"}, {"name": "grid_row_start", "type": "str | None", "description": "When used in a grid layout, specifies the starting row to span within the grid.", "default": "None"}, {"name": "grid_row_end", "type": "str | None", "description": "When used in a grid layout, specifies the ending row to span within the grid.", "default": "None"}, {"name": "grid_column_start", "type": "str | None", "description": "When used in a grid layout, specifies the starting column to span within the grid.", "default": "None"}, {"name": "grid_column_end", "type": "str | None", "description": "When used in a grid layout, specifies the ending column to span within the grid.", "default": "None"}, {"name": "margin", "type": "str | float | None", "description": "The margin for all four sides of the element.", "default": "None"}, {"name": "margin_top", "type": "str | float | None", "description": "The margin for the top side of the element.", "default": "None"}, {"name": "margin_bottom", "type": "str | float | None", "description": "The margin for the bottom side of 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 left and right sides of the element.", "default": "None"}, {"name": "margin_y", "type": "str | float | None", "description": "The margin for the top and bottom 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": "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"}]}} />
