Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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