Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion site/data/sidebar-getting-started.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
- title: Color palette
- title: Color modes
- title: CSS variables
draft: true
- title: Reboot
- title: Typography
- title: Images
Expand Down
6 changes: 3 additions & 3 deletions site/src/components/shortcodes/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ let codeToDisplay = filePath
: code

if (filePath && fileMatch && codeToDisplay) {
const match = codeToDisplay.match(new RegExp(fileMatch))
const match = codeToDisplay.match(new RegExp(fileMatch, 'g'))

if (!match || !match[0]) {
throw new Error(`The file at ${filePath} does not contains a match for the regex '${fileMatch}'.`)
throw new Error(`The file at ${filePath} does not contain a match for the regex '${fileMatch}'.`)
}

codeToDisplay = match[0]
codeToDisplay = match.join('\n\n')
}
---

Expand Down
82 changes: 81 additions & 1 deletion site/src/content/docs/foundation/css-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,84 @@ aliases:
toc: true
---

<CalloutSoon type="page" />
import { getConfig } from '@libs/config'

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.

**All our custom properties are prefixed with `bs-`** to avoid conflicts with third party CSS.

## Root variables

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.

### Default

These CSS variables are available everywhere, regardless of color mode.

<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\] {[^}]*})" />

### Dark mode

These variables are scoped to our built-in dark mode.

<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\] {[^}]*})" />

## Component variables

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.

{/* 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. */}
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.

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.

## Prefix

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.

Customize the prefix via the `$prefix` Sass variable. By default, it’s set to `bs-` (note the trailing dash).

{/* OUDS Web mod */}
## Deduping embedded SVGs

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.

```css
:root {
--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");
}

.back-to-top-link::after {
background-image: var(--bs-chevron-icon);
}

.pagination-item:first-child .page-link::before {
background-image: var(--bs-chevron-icon);
}
```
{/* End mod */}

## Examples

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.

```css
body {
font: 1rem/1.5 var(--bs-font-body-family);
}
a {
color: var(--bs-color-content-brand-primary);
}
```

## `:focus-visible` variables

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.

<ScssDocs name="focus-visible" file="scss/mixins/_focus.scss" />

To completely remove this style, add `outline: 0;` and `box-shadow: none;`.

## Grid breakpoints

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.
Loading