flex: Flex layout

View source: R/design-flex.R

flexR Documentation

Flex layout

Description

Use flex() to control how a flex container tag element places its flex items or child tag elements. For more on turning a tag element into a flex container see display(). By default tag elements within a flex container are treated as flex items.

Usage

flex(
  x,
  direction = NULL,
  justify = NULL,
  align = NULL,
  wrap = NULL,
  reverse = NULL
)

Arguments

x

A tag element or .style pronoun.

direction

A responsive argument. One of "row" or "column" specifying the placement of flex items, defaults to NULL. If "row" items are placed vertically, if "column" items are placed horizontally. Browsers place items vertically by default.

justify

A responsive argument. One of "start", "end", "center", "between", or "around" specifying how items are horizontally aligned, defaults to NULL. See the justify section below for more on how the different values affect horizontal spacing.

align

A responsive argument. One of "start", "end", "center", "baseline", or "stretch" specifying how items are vertically aligned, defaults to NULL. See the align section below for more on how the different values affect vertical spacing.

wrap

A responsive argument. One of TRUE or FALSE specifying whether to wrap flex items inside the flex container, tag, defaults to NULL. If TRUE items wrap inside the container, if FALSE items will not wrap. See the wrap section below for more.

reverse

A responsive argument. One of TRUE or FALSE specifying if flex items are placed in reverse order, defaults to NULL. If TRUE items are placed from right to left when direction is "row" or bottom to top when direction is "column".

Details

Getting started

This section needs pretty specific examples of how to use flex. I don’t know that people will want a tutorial on flex.

flex(

)

Different directions

Many of flex()’s arguments are viewport responsive and below we will see how useful this can be. On small screens the flex items are placed vertically and can occupy the full width of the mobile device. On medium or larger screens the items are placed horizontally once again.

div(
  .style %>%
    display("flex") %>%
    flex(
      direction = list(xs = "column", md = "row")  # <-
    ) %>%
    background("secondary") %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
)
A flex item
A flex item
A flex item

Resize the browser for this example.

You can keep items as a column by specifying only "column".

div(
  .style %>%
    display("flex") %>%
    flex("column"),  # <-
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
)
A flex item
A flex item
A flex item

Spacing items with justify

Below is a series of examples showing how to change the horizontal alignment of your flex items. Let’s start by pushing items to the beginning of their parent container.

div(
  .style %>%
    display("flex") %>%
    flex(justify = "start"),  # <-
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
)
A flex item
A flex item
A flex item
A flex item

We can also push items to the end.

div(
  .style %>%
    display("flex") %>%
    flex(justify = "end"),  # <-
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
)
A flex item
A flex item
A flex item
A flex item

Without using a table layout we can center items.

div(
  .style %>%
    display("flex") %>%
    flex(justify = "center"),  # <-
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
)
A flex item
A flex item
A flex item
A flex item

You can also put space between items

div(
  .style %>%
    display("flex") %>%
    flex(justify = "between"),  # <-
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
)
A flex item
A flex item
A flex item
A flex item

… or put space around items.

div(
  .style %>%
    display("flex") %>%
    flex(justify = "around"),  # <-
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
)
A flex item
A flex item
A flex item
A flex item

The "between" and "around" values come from the original CSS values "space-between" and "space-around".

Wrap onto new lines

Using flexbox we can also control how items wrap onto new lines.

div(
  .style %>%
    display("flex") %>%
    flex(wrap = TRUE),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
)
A flex item
A flex item
A flex item
A flex item

See Also

Other design utilities: active(), affix(), background(), border(), display(), float(), font(), height(), margin(), padding(), scroll(), shadow(), width()


nteetor/yonder documentation built on June 8, 2022, 1:36 p.m.