Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

149 lines
4.7KB

  1. #' Bulma Columns
  2. #'
  3. #' @param breakpoint By default, columns are only activated from **tablet**
  4. #' onwards, meaning that columns are stacked on top of each other on
  5. #' **mobile**. Use `breakpoint` to set the point at which columns are spread
  6. #' out. See `viewport` option in [bulma_responsive()] for valid options.
  7. #' @param gap Gap between colums, integer from 0 to 8
  8. #' @param multiline Should columns wrap to multiple lines (allows columns whose
  9. #' width or size add up to more than the screen width).
  10. #' @param centered Should columns be centered on the screen?
  11. #' @param class Extra classes that should be applied to the `columns` container.
  12. #' @param style CSS styles applied to the `columns` container.
  13. #' @param column_options Column options to be applied to each column. Use
  14. #' [bulma_column_options()] to format.
  15. #' @export
  16. bulma_columns <- function(
  17. ...,
  18. breakpoint = "tablet",
  19. gap = NULL,
  20. multiline = TRUE,
  21. centered = FALSE,
  22. class = NULL,
  23. style = NULL,
  24. column_options = NULL
  25. ) {
  26. items <- dots2list(...)
  27. if (is_bulma_column(items)) items <- list(items)
  28. if (!is.null(column_options) && !inherits(column_options, "bulma_column_options")) {
  29. rlang::abort("Please use bulma_column_options() to specify `column_options`")
  30. }
  31. breakpoint <- validate_viewport(breakpoint, "breakpoint")
  32. breakpoint <- c_is(breakpoint)
  33. gap <- c_is(validate_value(paste(gap), paste(0:8), FALSE, "gap"))
  34. class <- paste(
  35. "columns",
  36. paste(unlist(class), collapse = " "),
  37. breakpoint,
  38. gap, if (length(gap)) "is-variable",
  39. if (multiline) "is-multiline",
  40. if (centered) "is-centered"
  41. )
  42. ret <- tag("div", list(class = str_trim(class), style = style,
  43. map_arg(items, .f = bulma_column, .args = column_options)))
  44. ret <- tagList(ret)
  45. class(ret) <- c("bulma_columns", class(ret))
  46. ret
  47. }
  48. is_bulma_columns <- function(x) inherits(x, "bulma_columns")
  49. #' Bulma Column
  50. #'
  51. #' @param size One of "full", "four-fifths", "three-quarters", "two-thirds",
  52. #' "three-fifths", "half", "two-fifths", "one-third", "one-quarter",
  53. #' "one-fifth"
  54. #' @param width Column width in terms of the 12 grid units, i.e. 1-12.
  55. #' @param offset Column offset, one of `size` or `width`
  56. #' @param narrow Use [bulma_column_narrow()] to construct options
  57. #' @inheritParams bulma_columns
  58. #' @export
  59. bulma_column <- function(
  60. ...,
  61. size = NULL,
  62. width = NULL,
  63. offset = NULL,
  64. narrow = NULL,
  65. class = NULL,
  66. style = NULL
  67. ) {
  68. content <- dots2list(...)
  69. if (is_bulma_column(content)) return(content)
  70. class <- str_trim(paste(
  71. "column",
  72. paste(unlist(class), collapse = " "),
  73. bulma_column_options_format(size, width, offset, narrow)
  74. ))
  75. ret <- tag("div", list(class = class, style = style, content))
  76. class(ret) <- c("bulma_column", class(ret))
  77. ret
  78. }
  79. is_bulma_column <- function(x) inherits(x, "bulma_column")
  80. #' Bulma Column Options
  81. #'
  82. #' @inheritParams bulma_column
  83. #' @examples
  84. #' bulma_column_options(size = "half", offset = "one-quarter")
  85. #'
  86. #' @export
  87. bulma_column_options <- function(size = NULL, width = NULL, offset = NULL, narrow = NULL,
  88. class = NULL, style = NULL) {
  89. structure(
  90. list(
  91. size = validate_col_size(size),
  92. width = validate_value(paste(width), paste(1:12), FALSE, "width"),
  93. offset = validate_value(offset, c(bulma_constants("column sizes"), paste(1:12)), FALSE, "offset"),
  94. narrow = use_bulma_column_narrow(narrow, "narrow"),
  95. class = class,
  96. style = style
  97. ),
  98. class = "bulma_column_options"
  99. )
  100. }
  101. bulma_column_options_format <- function(size = NULL, width = NULL, offset = NULL, narrow = NULL) {
  102. size <- c_is(validate_col_size(size))
  103. width <- c_is(validate_value(paste(width), paste(1:12), FALSE, "width"))
  104. offset <- validate_value(offset, c(bulma_constants("column sizes"), paste(1:12)), FALSE, "offset")
  105. offset <- c_is(c_prefix(offset, "offset-"))
  106. narrow <- use_bulma_column_narrow(narrow, "narrow")
  107. str_trim(paste(size, width, offset, narrow))
  108. }
  109. validate_col_size <- function(size, var_name = "size") {
  110. validate_value(size, bulma_constants("column sizes"), FALSE, var_name)
  111. }
  112. #' Bulma Narrow Column Helper
  113. #'
  114. #' @inheritParams bulma_responsive
  115. #' @export
  116. bulma_column_narrow <- function(viewport = NULL) {
  117. ret <- if (is.null(viewport)) {
  118. "is-narrow"
  119. } else if (is.logical(viewport)) {
  120. if (isTRUE(viewport)) "is-narrow" else return(NULL)
  121. } else {
  122. viewport <- validate_viewport(viewport)
  123. c_is(c_prefix(viewport, "narrow-"))
  124. }
  125. class(ret) <- c("bulma_column_narrow", "character")
  126. ret
  127. }
  128. use_bulma_column_narrow <- function(x, varname) {
  129. if (!is.null(x)) {
  130. if (!inherits(x, glue("bulma_column_narrow"))) {
  131. rlang::abort(glue("Please use bulma_column_narrow() to prepare `{varname}`."))
  132. }
  133. x
  134. }
  135. }