| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/layers.R |
| 3 |
\name{sass_layer} |
| 4 |
\alias{sass_layer} |
| 5 |
\alias{sass_layer_file} |
| 6 |
\alias{sass_bundle} |
| 7 |
\alias{sass_bundle_remove} |
| 8 |
\alias{is_sass_bundle} |
| 9 |
\title{Bundling Sass layers} |
| 10 |
\usage{ |
| 11 |
sass_layer( |
| 12 |
functions = NULL, |
| 13 |
defaults = NULL, |
| 14 |
mixins = NULL, |
| 15 |
rules = NULL, |
| 16 |
html_deps = NULL, |
| 17 |
file_attachments = character(0), |
| 18 |
declarations = NULL, |
| 19 |
tags = NULL |
| 20 |
) |
| 21 |
|
| 22 |
sass_layer_file(file) |
| 23 |
|
| 24 |
sass_bundle(...) |
| 25 |
|
| 26 |
sass_bundle_remove(bundle, name) |
| 27 |
|
| 28 |
is_sass_bundle(x) |
| 29 |
} |
| 30 |
\arguments{ |
| 31 |
\item{functions}{\code{\link[=as_sass]{as_sass()}} \code{input} intended for \href{https://rstudio.github.io/sass/articles/sass.html#functions-1}{Sass functions}. |
| 32 |
Functions are placed before \code{defaults} so that variable definitions may make |
| 33 |
use of functions.} |
| 34 |
|
| 35 |
\item{defaults}{\code{\link[=as_sass]{as_sass()}} \code{input} intended for \href{https://rstudio.github.io/sass/articles/sass.html#variables-1}{variable defaults}. |
| 36 |
These variable defaults after placed after \code{functions} but before \code{mixins}. |
| 37 |
When multiple layers are combined in a \code{sass_bundle()}, defaults are merged |
| 38 |
in reverse order; that is, \code{sass_bundle(layer1, layer2)} will include |
| 39 |
\code{layer2$defaults} before \code{layer1$defaults}.} |
| 40 |
|
| 41 |
\item{mixins}{\code{\link[=as_sass]{as_sass()}} \code{input} intended for \href{https://rstudio.github.io/sass/articles/sass.html#mixins-1}{Sass mixins}. Mixins |
| 42 |
are placed after \code{defaults}, but before \code{rules}.} |
| 43 |
|
| 44 |
\item{rules}{\code{\link[=as_sass]{as_sass()}} \code{input} intended for \href{https://sass-lang.com/documentation/style-rules}{Sass rules}. Rules are placed last |
| 45 |
(i.e., after \code{functions}, \code{defaults}, and \code{mixins}).} |
| 46 |
|
| 47 |
\item{html_deps}{An HTML dependency (or a list of them). This dependency |
| 48 |
gets attached to the return value of \code{\link[=sass]{sass()}}/\code{\link[=as_sass]{as_sass()}}.} |
| 49 |
|
| 50 |
\item{file_attachments}{A named character vector, representing file assets |
| 51 |
that are referenced (using relative paths) from the sass in this layer. The |
| 52 |
vector names should be a relative path, and the corresponding vector values |
| 53 |
should be absolute paths to files or directories that exist; at render |
| 54 |
time, each value will be copied to the relative path indicated by its name. |
| 55 |
(For directories, the \emph{contents} of the source directory will be copied |
| 56 |
into the destination directory; the directory itself will not be copied.) |
| 57 |
You can also omit the name, in which case that file or directory will be |
| 58 |
copied directly into the output directory.} |
| 59 |
|
| 60 |
\item{declarations}{Deprecated, use \code{functions} or \code{mixins} instead.} |
| 61 |
|
| 62 |
\item{tags}{Deprecated. Preserve meta information using a key in \code{sass_bundle(KEY = val)}. |
| 63 |
preserve simple metadata as layers are merged.} |
| 64 |
|
| 65 |
\item{file}{file path to a \code{.scss} file.} |
| 66 |
|
| 67 |
\item{...}{A collection of \code{sass_layer()}s and/or objects that \code{\link[=as_sass]{as_sass()}} |
| 68 |
understands. Arguments should be provided in reverse priority order: |
| 69 |
defaults, declarations, and rules in later layers will take precedence over |
| 70 |
those of previous layers. Non-layer values will be converted to layers by |
| 71 |
calling \code{sass_layer(rules = ...)}.} |
| 72 |
|
| 73 |
\item{bundle}{Output value from \code{sass_layer()} or \code{sass_bundle()}} |
| 74 |
|
| 75 |
\item{name}{If a Sass layer name is contained in \code{name}, the matching Sass layer will be removed from the \code{bundle}} |
| 76 |
|
| 77 |
\item{x}{object to inspect} |
| 78 |
} |
| 79 |
\description{ |
| 80 |
Sass layers provide a way to package Sass variables, rules, functions, and |
| 81 |
mixins in a structured and composable way that follows best Sass practices. |
| 82 |
Most importantly, when multiple \code{sass_layer()} are combined into a |
| 83 |
\code{sass_bundle()}, variable \code{defaults} for later layers are placed \emph{before} |
| 84 |
earlier layers, effectively 'new' defaults through all the 'old' defaults. |
| 85 |
} |
| 86 |
\section{Functions}{ |
| 87 |
\itemize{ |
| 88 |
\item \code{sass_layer()}: Compose the parts of a single Sass layer. Object returned is a \code{sass_bundle()} with a single Sass layer |
| 89 |
|
| 90 |
\item \code{sass_layer_file()}: Read in a \code{.scss} file with parse special \verb{/*-- scss:(functions|defaults|rules|mixins) --*/} comments as relevant sections of a \code{sass_layer()}. |
| 91 |
|
| 92 |
\item \code{sass_bundle()}: Collect \code{sass_bundle()} and/or \code{sass_layer()} objects. Unnamed Sass bundles will be concatenated together, preserving their internal name structures. Named Sass bundles will be condensed into a single Sass layer for easier removal from the returned Sass bundle. |
| 93 |
|
| 94 |
\item \code{sass_bundle_remove()}: Remove a whole \code{sass_layer()} from a \code{sass_bundle()} object. |
| 95 |
|
| 96 |
\item \code{is_sass_bundle()}: Check if \code{x} is a Sass bundle object |
| 97 |
|
| 98 |
}} |
| 99 |
\examples{ |
| 100 |
blue <- list(color = "blue !default") |
| 101 |
red <- list(color = "red !default") |
| 102 |
green <- list(color = "green !default") |
| 103 |
|
| 104 |
# a sass_layer() by itself is not very useful, it just defines some |
| 105 |
# SASS to place before (defaults) and after (rules) |
| 106 |
core <- sass_layer(defaults = blue, rules = "body { color: $color; }") |
| 107 |
core |
| 108 |
sass(core) |
| 109 |
|
| 110 |
# However, by stacking sass_layer()s, we have ability to place |
| 111 |
# SASS both before and after some other sass (e.g., core) |
| 112 |
# Here we place a red default _before_ the blue default and export the |
| 113 |
# color SASS variable as a CSS variable _after_ the core |
| 114 |
red_layer <- sass_layer(red, rules = ":root{ --color: #{$color}; }") |
| 115 |
sass(sass_bundle(core, red_layer)) |
| 116 |
sass(sass_bundle(core, red_layer, sass_layer(green))) |
| 117 |
|
| 118 |
# Example of merging layers and removing a layer |
| 119 |
# Remember to name the layers that are removable |
| 120 |
core_layers <- sass_bundle(core, red = red_layer, green = sass_layer(green)) |
| 121 |
core_layers # pretty printed for console |
| 122 |
core_slim <- sass_bundle_remove(core_layers, "red") |
| 123 |
sass(core_slim) |
| 124 |
|
| 125 |
|
| 126 |
# File attachment example: Create a checkboard pattern .png, then |
| 127 |
# use it from a sass layer |
| 128 |
|
| 129 |
tmp_png <- tempfile(fileext = ".png") |
| 130 |
grDevices::png(filename = tmp_png, width = 20, height = 20, |
| 131 |
bg = "transparent", antialias = "none") |
| 132 |
par(mar = rep_len(0,4), xaxs = "i", yaxs = "i") |
| 133 |
plot.new() |
| 134 |
rect(c(0,0.5), c(0,0.5), c(0.5,1), c(0.5,1), col = "#00000044", border=NA) |
| 135 |
dev.off() |
| 136 |
|
| 137 |
layer <- sass_layer( |
| 138 |
rules = ".bg-check { background-image: url(images/demo_checkboard_bg.png) }", |
| 139 |
file_attachments = c("images/demo_checkboard_bg.png" = tmp_png) |
| 140 |
) |
| 141 |
|
| 142 |
output_path <- tempfile(fileext = ".css") |
| 143 |
sass(layer, output = output_path, write_attachments = TRUE) |
| 144 |
} |