flex: Flex layout

Description Usage Arguments See Also Examples

View source: R/flex.R

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

1
2
3
4
5
6
7
8
flex(
  tag,
  direction = NULL,
  justify = NULL,
  align = NULL,
  wrap = NULL,
  reverse = NULL
)

Arguments

tag

A tag element.

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".

See Also

Other layout functions: column(), fieldset(), navbar(), responsive, webpage()

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
### Different `direction`s

# 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(
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border(),
  div("A flex item") %>%
    padding(3) %>%
    border()
) %>%
  display("flex") %>%
  flex(
    direction = list(xs = "column", md = "row")  # <-
  ) %>%
  background("grey") %>%
  border()

# *Resize the browser for this example.*

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

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

### 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(
  replicate(
    div("A flex item") %>%
      padding(3) %>%
      border(),
    n = 5,
    simplify = FALSE
  )
) %>%
  display("flex") %>%
  flex(justify = "start")  # <-

# We can also push items to the **end**.

div(
  replicate(
    div("A flex item") %>%
      padding(3) %>%
      border(),
    n = 5,
    simplify = FALSE
  )
) %>%
  display("flex") %>%
  flex(justify = "end")  # <-

# Without using a table layout we can **center** items.

div(
  replicate(
    div("A flex item") %>%
      padding(3) %>%
      border(),
    n = 5,
    simplify = FALSE
  )
) %>%
  display("flex") %>%
  flex(justify = "center")  # <-

# You can also put space **between** items

div(
  replicate(
    div("A flex item") %>%
      padding(3) %>%
      border(),
    n = 5,
    simplify = FALSE
  )
) %>%
  display("flex") %>%
  flex(justify = "between")  # <-

# ... or put space **around** items.

div(
  replicate(
    div("A flex item") %>%
      padding(3) %>%
      border(),
    n = 5,
    simplify = FALSE
  )
) %>%
  display("flex") %>%
  flex(justify = "around")  # <-

# *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(
  replicate(
    div("A flex item") %>%
      padding(3) %>%
      border(),
    n = 5,
    simplify = FALSE
  )
) %>%
  display("flex") %>%
  flex(wrap = TRUE)

yonder documentation built on Jan. 11, 2020, 9:35 a.m.