Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Show light and dark theme applied using system preferences #4022

Merged
merged 3 commits into from
Jun 10, 2024
Merged
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
89 changes: 89 additions & 0 deletions src/pages/elements/themes/code.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,95 @@ Themes can also be extended with your own custom tokens:
);
```

#### System preferences

Modern browsers and systems have adopted the idea of dark and light themes
applied at a system level. Where no user preference has been specified their
system level theme should be matched.

For example applying `g10` or `g100` automaticcally based on system preferences.

```scss
@use '@carbon/themes/scss/themes' as *;
@use '@carbon/themes';

/* system preference theme by default */
:root {
@include themes.theme($g10);
}

@media (prefers-color-scheme: dark) {
:root {
@include themes.theme($g100);
}
}

/* user has configured a theme preference for the current page/app */
[data-carbon-theme='white'] {
@include themes.theme($white);
}

[data-carbon-theme='g10'] {
@include themes.theme($g10);
}

[data-carbon-theme='g90'] {
@include themes.theme($g90);
}

[data-carbon-theme='g100'] {
@include themes.theme($g100);
}
```

Some designs include a sections in the opposite theme.

```scss
@mixin theme-scheme($default, $compliment) {
/* apply default theme */
@include theme($default, true);

/* apply to the compliment theme */
[data-carbon-theme--compliment] {
@include theme($compliment, true);
}

/* apply the default theme */
/* to switch back from within a compliment */
[data-carbon-theme] {
@include theme($default, true);
}
}

/* system preference theme by default */
:root {
@include theme-scheme(themes.$g10, themes.$g100);
}

@media (prefers-color-scheme: dark) {
:root {
@include theme-scheme(themes.$g100, themes.$g10);
}
}

/* user has configured a theme preference for the current page/app */
[data-carbon-theme='white'] {
@include theme-scheme(themes.$white, themes.$g90);
}

[data-carbon-theme='g10'] {
@include theme-scheme(themes.$g10, themes.$g100);
}

[data-carbon-theme='g90'] {
@include theme-scheme(themes.$g90, themes.$white);
}

[data-carbon-theme='g100'] {
@include theme-scheme(themes.$g100, themes.$g10);
}
```

### JavaScript

If you're looking to use these themes in JavaScript, we export a variety of
Expand Down
Loading