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

docs(global-theme): clarify that styles won't be applied automatically #15609

Merged
merged 2 commits into from Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 36 additions & 7 deletions packages/react/src/components/Theme/Theme.mdx
Expand Up @@ -42,19 +42,48 @@ You also get this file when you import `@carbon/react` directly in your Sass.

## Setting the global theme

Use the `GlobalTheme` component to set the theme for your entire project. By
default, the global theme will be set to the `white` theme. You can change the
global theme value by using the `theme` prop on `GlobalTheme`:
To the set the theme for your entire project, the `GlobalTheme` component can be used.

Please note that in contrast to `Theme` this does not apply any styles on its own but rather sets the context's theme according to the value you pass to its `theme` prop.

This is due to the various options of applying global css custom properties which differ from application to application. Depending on your architecture you may want to apply a class to the `<body>` or add a custom data attribute to your `<html>` element:

```jsx
import { useEffect } from 'react';
import { GlobalTheme } from '@carbon/react';

<GlobalTheme theme="g100">
<YourApp />
</GlobalTheme>;
function App() {
const theme = 'g100'; // ← your implementation, e.g. fetching user settings

useEffect(() => {
document.documentElement.dataset.carbonTheme = theme;
}, [theme]);

return (
<GlobalTheme theme={theme}>
<YourApp />
</GlobalTheme>;
);
}
```

Note: this component should be used at the root of your app.
```scss
@use '@carbon/styles/scss/theme';
@use '@carbon/styles/scss/themes';

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

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

This way, the `GlobalTheme` component is used to "synchronize" the state of the application's context and your scss, so that other components and hooks like useTheme can work properly.
For this reason, the component should be used as a wrapper of the root of your application.

By default, the global theme is set to `white`.

## Setting an inline theme

Expand Down