Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

248 lines
8.8KB

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