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.6KB

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