Skip to content

Commit 510ba9f

Browse files
feat(docs): Add CSS Variables page (#3301)
1 parent ec74f7d commit 510ba9f

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

site/data/sidebar-getting-started.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
- title: Color palette
3939
- title: Color modes
4040
- title: CSS variables
41-
draft: true
4241
- title: Reboot
4342
- title: Typography
4443
- title: Images

site/src/components/shortcodes/Code.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ let codeToDisplay = filePath
5757
: code
5858
5959
if (filePath && fileMatch && codeToDisplay) {
60-
const match = codeToDisplay.match(new RegExp(fileMatch))
60+
const match = codeToDisplay.match(new RegExp(fileMatch, 'g'))
6161
6262
if (!match || !match[0]) {
63-
throw new Error(`The file at ${filePath} does not contains a match for the regex '${fileMatch}'.`)
63+
throw new Error(`The file at ${filePath} does not contain a match for the regex '${fileMatch}'.`)
6464
}
6565
66-
codeToDisplay = match[0]
66+
codeToDisplay = match.join('\n\n')
6767
}
6868
---
6969

site/src/content/docs/foundation/css-variables.mdx

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,84 @@ aliases:
77
toc: true
88
---
99

10-
<CalloutSoon type="page" />
10+
import { getConfig } from '@libs/config'
11+
12+
OUDS Web includes many [CSS custom properties (variables)](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties) in its compiled CSS for real-time customization without the need to recompile Sass. These provide easy access to commonly used values like our theme colors, breakpoints, spacings, and primary font stacks when working in your browser’s inspector, a code sandbox, or general prototyping.
13+
14+
**All our custom properties are prefixed with `bs-`** to avoid conflicts with third party CSS.
15+
16+
## Root variables
17+
18+
Here are the variables we include (note that the `:root` is required) that can be accessed anywhere OUDS Web’s CSS is loaded. They’re located in our `_root.scss` file in our common package and included in our compiled dist files in the theme package. But we also provide many semantic tokens in this form (mainly colors) in our `_semantic-colors-custom-props.scss` in the theme package and included in our compiled dist files.
19+
20+
### Default
21+
22+
These CSS variables are available everywhere, regardless of color mode.
23+
24+
<Code lang="css" filePath={`packages/${getConfig().brand}/dist/css/ouds-web.css`} fileMatch="(:root,\n\[data-bs-theme=light\],\n:root\[data-bs-theme=light\] \[data-bs-theme=root\],\n:root\[data-bs-theme=dark\] \[data-bs-theme=root-inverted\] {[^}]*})" />
25+
26+
### Dark mode
27+
28+
These variables are scoped to our built-in dark mode.
29+
30+
<Code lang="css" filePath={`packages/${getConfig().brand}/dist/css/ouds-web.css`} fileMatch="(\[data-bs-theme=dark\],\n:root\[data-bs-theme=dark\],\n:root\[data-bs-theme=dark\] \[data-bs-theme=root\],\n:root\[data-bs-theme=light\] \[data-bs-theme=root-inverted\] {[^}]*})" />
31+
32+
## Component variables
33+
34+
OUDS Web is increasingly making use of custom properties as local variables for various components. This way we reduce our compiled CSS, ensure styles aren’t inherited in places like nested tables, and allow some basic restyling and extending of OUDS Web components after Sass compilation.
35+
36+
{/* Have a look at our table documentation for some [insight into how we’re using CSS variables]([[docsref:/content/tables#how-do-the-variants-and-accented-tables-work]]). Our [navbars also use CSS variables]([[docsref:/components/navbar#css]]) as of v5.2.0. */}
37+
We’re using CSS variables across our grids—primarily for gutters in the [new opt-in CSS grid]([[docsref:/layout/css-grid]])—with more component usage coming in the future.
38+
39+
Whenever possible, we’ll assign CSS variables at the base component level (e.g., `.breadcrumb` for breadcrumb and its sub-components). This reduces guessing on where and how to customize, and allows for easy modifications by our team in future updates.
40+
41+
## Prefix
42+
43+
Most CSS variables use a prefix to avoid collisions with your own codebase. This prefix is in addition to the `--` that’s required on every CSS variable.
44+
45+
Customize the prefix via the `$prefix` Sass variable. By default, it’s set to `bs-` (note the trailing dash).
46+
47+
{/* OUDS Web mod */}
48+
## Deduping embedded SVGs
49+
50+
OUDS Web uses embedded SVGs as data URIs in the wild, which means extremely long strings in CSS. When one of them is used several times in the stylesheet, CSS custom properties allow to factorize its string—thus decreasing the output file size.
51+
52+
```css
53+
:root {
54+
--bs-chevron-icon: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 14'%3e%3cpath d='M9 2L7 0 0 7l7 7 2-2-5-5 5-5z'/%3e%3c/svg%3e");
55+
}
56+
57+
.back-to-top-link::after {
58+
background-image: var(--bs-chevron-icon);
59+
}
60+
61+
.pagination-item:first-child .page-link::before {
62+
background-image: var(--bs-chevron-icon);
63+
}
64+
```
65+
{/* End mod */}
66+
67+
## Examples
68+
69+
CSS variables offer similar flexibility to Sass’s variables, but without the need for compilation before being served to the browser. For example, here we’re resetting our page’s font and link styles with CSS variables.
70+
71+
```css
72+
body {
73+
font: 1rem/1.5 var(--bs-font-body-family);
74+
}
75+
a {
76+
color: var(--bs-color-content-brand-primary);
77+
}
78+
```
79+
80+
## `:focus-visible` variables
81+
82+
OUDS Web provides custom `:focus-visible` (which is supported by OUDS Web) styles using a combination of Sass and CSS variables that can be optionally added to specific components and elements. This focus is removed for form elements.
83+
84+
<ScssDocs name="focus-visible" file="scss/mixins/_focus.scss" />
85+
86+
To completely remove this style, add `outline: 0;` and `box-shadow: none;`.
87+
88+
## Grid breakpoints
89+
90+
While we include our grid breakpoints as CSS variables (except for `2xs`), be aware that **CSS variables do not work in media queries**. This is by design in the CSS spec for variables, but may change in coming years with support for `env()` variables. Check out [this Stack Overflow answer](https://stackoverflow.com/a/47212942) for some helpful links. In the meantime, you can use these variables in other CSS situations, as well as in your JavaScript.

0 commit comments

Comments
 (0)