You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 satır
9.0KB

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