Skip to content

Commit b8f41cc

Browse files
feat(docs): Add CSS Variables page
1 parent 26efa28 commit b8f41cc

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

site/data/sidebar.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
- title: Components
4343
draft: true
4444
- title: CSS variables
45-
draft: true
4645
- title: Optimize
4746
draft: true
4847

site/src/components/shortcodes/Code.astro

Lines changed: 2 additions & 2 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]) {
6363
throw new Error(`The file at ${filePath} does not contains 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/customize/css-variables.mdx

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

9-
<CalloutSoon type="page" />
9+
import { getConfig } from '@libs/config'
10+
11+
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.
12+
13+
**All our custom properties are prefixed with `bs-`** to avoid conflicts with third party CSS.
14+
15+
## Root variables
16+
17+
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.
18+
19+
### Default
20+
21+
These CSS variables are available everywhere, regardless of color mode.
22+
23+
<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\] {[^}]*})" />
24+
25+
### Dark mode
26+
27+
These variables are scoped to our built-in dark mode.
28+
29+
<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\] {[^}]*})" />
30+
31+
## Component variables
32+
33+
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.
34+
35+
{/* 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. */}
36+
We’re using CSS variables across our grids—primarily for gutters the [new opt-in CSS grid]([[docsref:/layout/css-grid]])—with more component usage coming in the future.
37+
38+
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.
39+
40+
## Prefix
41+
42+
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.
43+
44+
Customize the prefix via the `$prefix` Sass variable. By default, it’s set to `bs-` (note the trailing dash).
45+
46+
{/* OUDS Web mod */}
47+
## Deduping embedded SVGs
48+
49+
OUDS Web uses [embedded SVGs as data URIs]([[docsref:/customize/overview#csps-and-embedded-svgs]]) in the wild, which means extremely long strings in CSS. When one of them is used several times in the stylesheet, CSS custom properties allows to factorize its string— thus to decrease output file size.
50+
51+
```css
52+
:root {
53+
--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");
54+
}
55+
56+
.back-to-top-link::after {
57+
background-image: var(--bs-chevron-icon);
58+
}
59+
60+
.pagination-item:first-child .page-link::before {
61+
background-image: var(--bs-chevron-icon);
62+
}
63+
```
64+
{/* End mod */}
65+
66+
## Examples
67+
68+
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.
69+
70+
```css
71+
body {
72+
font: 1rem/1.5 var(--bs-font-body-family);
73+
}
74+
a {
75+
color: var(--bs-color-content-brand-primary);
76+
}
77+
```
78+
79+
## `:focus-visible` variables
80+
81+
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.
82+
83+
<ScssDocs name="focus-visible" file="scss/mixins/_focus.scss" />
84+
85+
To completely remove this style, add `outline: 0;` and `box-shadow: none;`.
86+
87+
## Grid breakpoints
88+
89+
While we include our grid breakpoints as CSS variables (except for `xs`), 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)