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

261 line
9.4KB

  1. c_is <- function(x) {
  2. str_trim(paste("is-", x, sep = "", collapse = " "))
  3. }
  4. c_has <- function(x) {
  5. str_trim(paste("has-", x, sep = "", collapse = " "))
  6. }
  7. c_prefix <- function(x = NULL, prefix = NULL) {
  8. if (is.null(x)) return(NULL)
  9. paste0(prefix, x)
  10. }
  11. str_trim <- function(x) {
  12. gsub("^\\s*|\\s*$", "", x)
  13. }
  14. # Modifiers :: Syntax -----------------------------------------------------
  15. #' Bulma Modifier Classes
  16. #'
  17. #' @param color "primary", "link", "info", "success", "warning", "danger"
  18. #' @param size "small", "medium", "large"
  19. #' @param state "outlined", "loading"
  20. #'
  21. #' @return String of modifer classes
  22. #' @examples
  23. #' bulma_modifier(color = "primary", size = "large", state = "outlined")
  24. #' @references https://bulma.io/documentation/modifiers/syntax/
  25. #' @export
  26. bulma_modifier <- function(color = NULL, size = NULL, state = NULL) {
  27. color <- validate_value(color,
  28. c("primary", "link", "info", "success", "warning", "danger"), FALSE, "color")
  29. size <- validate_value(size, c("small", "medium", "large"), FALSE, "size")
  30. state <- validate_value(state, c("outlined", "loading"), TRUE, "state")
  31. c_is(c(color, size, state))
  32. }
  33. # Modifiers :: Helpers ----------------------------------------------------
  34. #' Bulma Helper Classes
  35. #'
  36. #' @param float `clearfix` Fixes an element's floating children
  37. #'
  38. #' `pulled-left` Moves an element to the left
  39. #'
  40. #' `pulled-right` Moves an element to the right
  41. #' @param spacing `marginless` Removes any margin
  42. #'
  43. #' `paddingless` Removes any padding
  44. #' @param other `overlay` Completely covers the first positioned parent
  45. #' `clipped` Adds overflow hidden
  46. #'
  47. #' `radiusless` Removes any radius
  48. #'
  49. #' `shadowless` Removes any shadow
  50. #'
  51. #' `unselectable` Prevents the text from being selectable
  52. #'
  53. #' `invisible` Adds visibility hidden
  54. #'
  55. #' `sr-only` Hide elements visually but keep the element available to be announced by a screen reader
  56. #' @return String of modifer classes
  57. #' @examples
  58. #' bulma_helper(float = "pulled-left")
  59. #' bulma_helper(spacing = c("marginless", "paddingless"))
  60. #' bulma_helper(other = "clipped", float = "pulled-right")
  61. #' @references https://bulma.io/documentation/modifiers/helpers
  62. #' @export
  63. bulma_helper <- function(float = NULL, spacing = NULL, other = NULL) {
  64. float <- validate_value(float, c("clearfix", "pulled-left", "pulled-right"), FALSE, "float")
  65. spacing <- validate_value(spacing, c("marginless", "paddingless"), TRUE, "spacing")
  66. other <- validate_value(other,
  67. c("overlay", "clipped", "radiusless", "shadowless", "unselectable", "invisible", "sr-only"),
  68. TRUE, "other")
  69. c_is(c(float, spacing, other))
  70. }
  71. # Modifiers :: Responsive Helpers -----------------------------------------
  72. #' Bulma Responsive Helper Classes
  73. #'
  74. #' @param display One of "block", "flex", "inline", "inline-block", "inline-flex"
  75. #' @param viewport One of "mobile", "tablet", "touch", "desktop", "widescreen",
  76. #' "fullhd". See <https://bulma.io/documentation/modifiers/responsive-helpers/>
  77. #' for more information.
  78. #'
  79. #' @examples
  80. #' bulma_responsive("flex", "tablet")
  81. #' bulma_responsive("flex", "widescreen", only = TRUE)
  82. #' @references https://bulma.io/documentation/modifiers/responsive-helpers/
  83. #' @return A string of helper classes
  84. #' @export
  85. bulma_responsive <- function(display = NULL, viewport = NULL, only = FALSE) {
  86. display <- validate_value(display, c(
  87. "block", "flex", "inline", "inline-block", "inline-flex"
  88. ), FALSE, "display")
  89. viewport <- validate_value(viewport, c(
  90. "mobile", "tablet", "touch", "desktop", "widescreen", "fullhd"
  91. ), FALSE, "viewport")
  92. c_is(paste(display, viewport, if (only) "only", sep = "-"))
  93. }
  94. # Modifiers :: Color ------------------------------------------------------
  95. #' Bulma Color Helper Classes
  96. #'
  97. #' @param text One of "white", "black", "light", "dark", "primary", "info",
  98. #' "link", "success", "warning", "danger", "black-bis", "black-ter",
  99. #' "grey-darker", "grey-dark", "grey", "gre-light", "grey-lighter",
  100. #' "white-ter", "white-bis"
  101. #' @param background One of "white", "black", "light", "dark", "primary",
  102. #' "info", "link", "success", "warning", "danger", "black-bis", "black-ter",
  103. #' "grey-darker", "grey-dark", "grey", "gre-light", "grey-lighter",
  104. #' "white-ter", "white-bis"
  105. #'
  106. #' @examples
  107. #' bulma_color("white", "black")
  108. #' bulma_color("white", "primary")
  109. #'
  110. #' @references https://bulma.io/documentation/modifiers/color-helpers
  111. #' @return String of modifer classes
  112. #' @export
  113. bulma_color <- function(text = NULL, background = NULL) {
  114. text <- validate_value(text, bulma::.bulma_colors, FALSE, "text")
  115. text <- c_prefix(text, "text-")
  116. background <- validate_value(background, bulma::.bulma_colors, FALSE, "background")
  117. background <- c_prefix(background, prefix = "background-")
  118. c_has(c(text, background))
  119. }
  120. .bulma_colors <- c("white", "black", "light", "dark", "primary", "info", "link",
  121. "success", "warning", "danger", "black-bis", "black-ter",
  122. "grey-darker", "grey-dark", "grey", "gre-light", "grey-lighter",
  123. "white-ter", "white-bis")
  124. # Modifiers :: Typography -------------------------------------------------
  125. #' Bulma Typography Helper Classes
  126. #'
  127. #' @param size One of 1 through 7 (large to small)
  128. #' @param text One of "white", "black", "light", "dark", "primary", "info",
  129. #' "link", "success", "warning", "danger", "black-bis", "black-ter",
  130. #' "grey-darker", "grey-dark", "grey", "gre-light", "grey-lighter",
  131. #' "white-ter", "white-bis"
  132. #' @param responsive_size See [bulma_responsive_size()].
  133. #' @param alignment One of "centered", "justified", "left", "right"
  134. #' @param transformation One (or more) of "capitalized", "lowercase", "uppercase", "italic"
  135. #' @param weight One of "light", "normal", "semibold", "bold"
  136. #' @param font_family One of "sans-serif", "monospace", "primary", "secondary", "code".
  137. #' (Coming in version 0.7.3.)
  138. #'
  139. #' @examples
  140. #' bulma_color("white", "black")
  141. #' bulma_color("white", "primary")
  142. #'
  143. #' @references https://bulma.io/documentation/modifiers/typography-helpers/
  144. #' @return String of modifer classes
  145. #' @export
  146. bulma_typography <- function(
  147. size = NULL,
  148. color = NULL,
  149. responsive_size = NULL,
  150. alignment = NULL,
  151. responsive_alignment = NULL,
  152. transformation = NULL,
  153. weight = NULL,
  154. font_family = NULL
  155. ) {
  156. size <- validate_value(paste(size), paste(1:7), FALSE, "size")
  157. size <- c_prefix(size, prefix = "size-")
  158. responsive_size <- use_bulma_responsive(responsive_size, "size", "responsive_size")
  159. color <- validate_value(color, bulma::.bulma_colors, FALSE, "text")
  160. color <- c_prefix(color, prefix = "text-")
  161. alignment <- validate_value(alignment, c("centered", "justified", "left", "right"),
  162. FALSE, "alignment")
  163. alignment <- c_prefix(alignment, "text-")
  164. responsive_alignment <- use_bulma_responsive(responsive_alignment, "alignment", "responsive_alignment")
  165. transformation <- validate_value(transformation,
  166. c("capitalized", "lowercase", "uppercase", "italic"), TRUE, "transformation")
  167. weight <- validate_value(weight, c("light", "normal", "semibold", "bold"), FALSE, "weight")
  168. weight <- c_prefix(weight, "text-weight-")
  169. font_family <- validate_value(font_family, c(
  170. "sans-serif", "monospace", "primary", "secondary", "code"
  171. ), TRUE, "font_family")
  172. font_family <- c_prefix(font_family, "family-")
  173. x <- paste(
  174. c_is(c(size, transformation, font_family)),
  175. c_has(c(color, alignment, weight)),
  176. responsive_size,
  177. responsive_alignment
  178. )
  179. str_trim(x)
  180. }
  181. #' Bulma Responsive Size Helper Classes
  182. #'
  183. #' @inheritParams bulma_typography
  184. #' @inheritParams bulma_responsive
  185. #' @examples
  186. #' bulma_responsive_size()
  187. #' bulma_responsive_size(2, "tablet")
  188. #' bulma_responsive_size(viewport = "widescreen")
  189. #' @return String of modifier classes
  190. #' @export
  191. bulma_responsive_size <- function(size = "1", viewport = "desktop") {
  192. # TODO: Needs to be vectorized, matching size/viewport arguments
  193. size <- validate_value(paste(size), paste(1:7), FALSE, "size")
  194. size <- c_prefix(size, "size-")
  195. viewport <- validate_value(viewport,
  196. c("mobile", "tablet", "touch", "desktop", "widescreen", "fullhd"),
  197. FALSE, "viewport")
  198. x <- c_is(paste0(size, "-", viewport))
  199. class(x) <- c("responsive_size", "bulma", "character")
  200. x
  201. }
  202. use_bulma_responsive <- function(x, var, varname) {
  203. if (!is.null(x)) {
  204. if (!inherits(x, glue("responsive_{var}"))) {
  205. rlang::abort(glue("Please use bulma_responsive_{var}() to prepare `{varname}`."))
  206. }
  207. x
  208. }
  209. }
  210. #' Bulma Responsive Alignemnt Helper Classes
  211. #'
  212. #' @inheritParams bulma_typography
  213. #' @inheritParams bulma_responsive_size
  214. #' @examples
  215. #' bulma_responsive_alignment()
  216. #' bulma_responsive_alignment("centered", "tablet")
  217. #' bulma_responsive_alignment(viewport = "widescreen")
  218. #' @return String of modifier classes
  219. #' @references https://bulma.io/documentation/modifiers/typography-helpers/#responsive-alignment
  220. #' @export
  221. bulma_responsive_alignment <- function(alignment = "left", viewport = "desktop") {
  222. # TODO: Needs to be vectorized, matching alignment/viewport arguments
  223. alignment <- validate_value(alignment, c("centered", "justified", "left", "right"),
  224. FALSE, "alignment")
  225. alignment <- c_prefix(alignment, "text-")
  226. viewport <- validate_value(viewport,
  227. c("mobile", "tablet", "touch", "desktop", "widescreen", "fullhd"),
  228. FALSE, "viewport")
  229. x <- c_has(paste0(alignment, "-", viewport))
  230. class(x) <- c("responsive_alignment", "bulma", "character")
  231. x
  232. }