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.

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