-
Notifications
You must be signed in to change notification settings - Fork 2.9k
docs(react-components): Add theming docs #23080
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
Merged
miroslavstastny
merged 5 commits into
microsoft:master
from
miroslavstastny:docs/theming
Jun 6, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91bedfc
docs(react-components): Add theming docs
miroslavstastny a1fc10a
Apply suggestions from code review
miroslavstastny 41727aa
Apply suggestions from code review
miroslavstastny 588a0b4
address PR comments
miroslavstastny 69f4a53
remove broken import
miroslavstastny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
apps/public-docsite-v9/src/Concepts/Theming.stories.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { Meta, Source } from '@storybook/addon-docs'; | ||
|
|
||
| <Meta title="Concepts/Developer/Theming" /> | ||
|
|
||
| # Theming | ||
|
|
||
| The Fluent UI Theme is represented by a set of tokens. Each token resolves to a single value which can be assigned to a CSS property. | ||
|
|
||
| ```tsx | ||
| const exampleTheme = { | ||
| borderRadiusSmall: '2px', | ||
| //... | ||
| colorNeutralForeground2: '#424242', | ||
| }; | ||
| ``` | ||
|
|
||
| You can browse all the available tokens in **[Theme](/docs/theme-colors--colors)** section of the docs. | ||
|
|
||
| ## How theme is applied | ||
|
|
||
| No matter what theme is used, the component styles are always the same. The only way to change the component styling is through theme tokens which can be used in style values. | ||
|
|
||
| Those tokens are resolved to CSS variables. The `FluentProvider` component is responsible for setting the values of the CSS variables in DOM and changing them when the theme changes. When the theme is switched, only the variables are changed, all styles remain the same. | ||
|
|
||
| Place a `<FluentProvider />` at the root of your app and pass a theme to the `theme` prop. The provider will render a `div` and set all tokens as CSS variables on that element. The provider also propagates CCS variables to React portals created with [Portal component](?path=/docs/components-portal--default). | ||
|
|
||
| ```jsx | ||
| import { FluentProvider, teamsLightTheme } from '@fluentui/react-components'; | ||
|
|
||
| export const AppRoot = ({children}) => ( | ||
| <FluentProvider theme={teamsLightTheme}> | ||
| {children} | ||
| </FluentProvider>, | ||
| ); | ||
| ``` | ||
|
|
||
| Import `tokens` to style a component using `makeStyles` | ||
|
|
||
| ```jsx | ||
| import { tokens } from '@fluentui/react-theme'; | ||
|
|
||
| const useStyles = makeStyles({ | ||
| root: { display: 'flex' }, | ||
| rootPrimary: { color: tokens.colorNeutralForeground3 }, | ||
| }); | ||
|
|
||
| export Component = props => { | ||
| const classes = useStyles(); | ||
|
|
||
| return <div className={mergeClasses('ui-component', classes.root, props.primary && classes.rootPrimary)} />; | ||
| } | ||
| ``` | ||
|
|
||
| For more details, see [Styling components](?path=/docs/concepts-developer-styling-components--page). | ||
|
|
||
| ### Do not use CSS variables directly | ||
|
|
||
| **⚠ Never use theme CSS variables directly!** The CSS variables implementation of the theme is internal to the library. We might eventually decide to change the variable names, hash them or even use direct values instead of some variables. Always use the `tokens` to access the theme. | ||
|
|
||
| ## Available themes | ||
|
|
||
| Fluent UI currently exports following themes: | ||
|
|
||
| - Web Light (`webLightTheme`) | ||
| - Web Dark (`webDarkTheme`) | ||
| - Teams Light (`teamsLightTheme`) | ||
| - Teams Dark (`teamsDarkTheme`) | ||
| - Teams High Contrast (`teamsHighContrastTheme`) | ||
|
|
||
| ### High contrast themes | ||
|
|
||
| ⚠ Do not use High Contrast themes! All Fluent UI components support Windows High Contrast mode automatically regardless of the active theme. | ||
|
miroslavstastny marked this conversation as resolved.
|
||
| Windows high contrast mode is the recommended high contrast platform for all customers using Fluent UI. | ||
|
|
||
| Hardcoded High Contrast themes are considered legacy, to be used only in applications which explicitly support those. | ||
|
|
||
| ## Customizing the theme | ||
|
|
||
| Applications can customize a theme in multiple ways. | ||
|
|
||
| ### Custom Brand ramp | ||
|
|
||
| The brand ramp is a color ramp going from dark to light colors: | ||
|
|
||
| <img src={require('../../public/brand-ramp-example.png')} alt="Example of a brand ramp" /> | ||
|
miroslavstastny marked this conversation as resolved.
|
||
|
|
||
| A theme is derived from a brand ramp. To use a theme with a custom brand ramp, instead of importing a predefined theme, you can use theme factory functions. | ||
|
|
||
| The following factory functions are available: | ||
|
|
||
| - `createLightTheme()` | ||
| - `createDarkTheme()` | ||
| - `createTeamsDarkTheme()` | ||
| - `createHighContrastTheme()` | ||
|
|
||
| ```tsx | ||
| import { BrandVariants, createLightTheme, createDarkTheme } from '@fluentui/react-components'; | ||
|
|
||
| const customBrandRamp: BrandVariants = { | ||
| 10: '#008', | ||
| //... | ||
| 160: '#88F', | ||
| }; | ||
|
|
||
| export const customLightTheme = createLightTheme(customBrandRamp); | ||
| export const customDarkTheme = createDarkTheme(customBrandRamp); | ||
| ``` | ||
|
|
||
| ### Overriding existing tokens | ||
|
|
||
| ⚠️ If the existing tokens do not fulfill your needs, you should talk to your designer instead of overriding tokens. | ||
|
|
||
| A theme is a flat object containing `{ [token name]: CSS value }` pairs. You can copy the object and overwrite any tokens you wish. | ||
|
|
||
| ```tsx | ||
| import { webLightTheme, Theme } from '@fluentui/react-components'; | ||
|
|
||
| export const customLightTheme: Theme = { | ||
| ...webLightTheme, | ||
| colorNeutralForeground1: '#555', // overriden token | ||
| }; | ||
| ``` | ||
|
|
||
| ### Extending theme with new tokens | ||
|
|
||
| Similarly to overriding existing tokens, you can add custom tokens as well. | ||
|
|
||
| ⚠ Components which use custom tokens cannot be shared between applications. Keep in mind that any application which uses a component with custom tokens must also add the custom tokens to its own themes. Instead of adding custom tokens inside potentially reusable components, you should talk to design. | ||
|
|
||
| ```tsx | ||
| import { webLightTheme, Theme } from '@fluentui/react-components'; | ||
|
|
||
| export const customLightTheme: Theme & { customSpacingVerticalHuge: string } = { | ||
| ...webLightTheme, | ||
| customSpacingVerticalHuge: '128px', | ||
| }; | ||
| ``` | ||
|
|
||
| To use the tokens in styles, one is supposed to import `tokens`. Obviously that object would not contain any custom tokens. For that reason you can use `themeToTokensObject()` utility which will create the tokens object with the custom tokens. | ||
|
|
||
| ⚠ Keep in mind that the object generated by the `themeToTokensObject()` will contain all the tokens and will not be tree-shakeable. | ||
|
|
||
| ```tsx | ||
| import { themeToTokensObject } from '@fluentui/react-components'; | ||
|
|
||
| export const customTokens = themeToTokensObject(customLightTheme); | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it be helpful to show what it actually means?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but the theme object is semantic name -> raw value, so I added an example of that. The later sections explain how that is added to DOM and consumed in styles.