2024-07-26 02:14:19 +08:00
# Work with Grafana themes
2019-07-03 15:50:42 +08:00
2019-09-20 17:27:57 +08:00
## Overview
2019-07-03 15:50:42 +08:00
2024-07-26 02:14:19 +08:00
Themes in Grafana are implemented in TypeScript. We chose the TypeScript language in part because it shares variables between Grafana TypeScript and [Sass ](https://sass-lang.com/ ) code.
Theme definitions are located in the following files:
Docs: Add links, fix grammar, formatting, wording (#22381)
* Docs: Add links, fix grammar, formatting, wording
- Add links to theme files and technology references
- Adjust formatting to be consistent, e.g., syntax highlighting, section spacing
- Fix misc grammar, punctuation
- Fix SASS -> Sass
Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>
Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
2020-02-25 06:05:56 +08:00
2021-07-10 16:24:14 +08:00
- [packages/grafana-data/src/themes/createTheme.ts ](../../packages/grafana-data/src/themes/createTheme.ts )
- [packages/grafana-data/src/themes/createColors.ts ](../../packages/grafana-data/src/themes/createColors.ts )
2019-07-03 15:50:42 +08:00
2019-09-20 17:27:57 +08:00
## Usage
Docs: Add links, fix grammar, formatting, wording (#22381)
* Docs: Add links, fix grammar, formatting, wording
- Add links to theme files and technology references
- Adjust formatting to be consistent, e.g., syntax highlighting, section spacing
- Fix misc grammar, punctuation
- Fix SASS -> Sass
Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>
Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
2020-02-25 06:05:56 +08:00
2024-07-26 02:14:19 +08:00
This section provides usage guidelines for themes.
Docs: Add links, fix grammar, formatting, wording (#22381)
* Docs: Add links, fix grammar, formatting, wording
- Add links to theme files and technology references
- Adjust formatting to be consistent, e.g., syntax highlighting, section spacing
- Fix misc grammar, punctuation
- Fix SASS -> Sass
Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>
Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
2020-02-25 06:05:56 +08:00
2024-07-26 02:14:19 +08:00
### Use themes in React components
2019-07-03 15:50:42 +08:00
2024-07-26 02:14:19 +08:00
The following section describes how to use Grafana themes in React components.
Docs: Add links, fix grammar, formatting, wording (#22381)
* Docs: Add links, fix grammar, formatting, wording
- Add links to theme files and technology references
- Adjust formatting to be consistent, e.g., syntax highlighting, section spacing
- Fix misc grammar, punctuation
- Fix SASS -> Sass
Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>
Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
2020-02-25 06:05:56 +08:00
2024-07-26 02:14:19 +08:00
#### The useStyles2 hook
2020-07-01 17:39:46 +08:00
2024-07-26 02:14:19 +08:00
The `useStyles2` hook memoizes the function and provides access to the theme.
2019-07-03 15:50:42 +08:00
2019-09-20 17:27:57 +08:00
```tsx
2024-06-25 19:43:47 +08:00
import { FC } from 'react';
2021-07-10 16:24:14 +08:00
import { GrafanaTheme2 } from '@grafana/data';
import { useStyles2 } from '@grafana/ui';
2021-04-23 16:06:42 +08:00
import { css } from '@emotion/css';
2019-07-03 15:50:42 +08:00
2022-10-05 14:50:39 +08:00
function Foo(props: FooProps) {
const styles = useStyles2(getStyles);
2020-07-01 17:39:46 +08:00
// Use styles with className
2022-10-05 14:50:39 +08:00
}
2019-07-03 15:50:42 +08:00
2023-03-28 19:18:50 +08:00
const getStyles = (theme: GrafanaTheme2) =>
css({
padding: theme.spacing(1, 2),
});
```
2022-10-05 14:50:39 +08:00
2020-07-01 17:39:46 +08:00
#### Get the theme object
2019-07-03 15:50:42 +08:00
2024-07-26 02:14:19 +08:00
Use code similar to the following to give your component access to the theme variables:
2019-09-20 17:27:57 +08:00
```tsx
2024-06-25 19:43:47 +08:00
import { FC } from 'react';
2021-07-10 16:24:14 +08:00
import { useTheme2 } from '@grafana/ui';
2019-07-03 15:50:42 +08:00
2020-07-01 17:39:46 +08:00
const Foo: FC< FooProps > = () => {
2021-07-10 16:24:14 +08:00
const theme = useTheme2();
2019-09-09 14:18:38 +08:00
// Your component has access to the theme variables now
2020-07-01 17:39:46 +08:00
};
```
2024-07-26 02:14:19 +08:00
## Select a variable
This section explains how to select the correct variables in your theme.
2021-07-10 16:24:14 +08:00
### The rich color object and the state colors
2024-07-26 02:14:19 +08:00
The `theme.colors` object has six rich color objects:
- `primary`
- `secondary`
- `info`
- `success`
- `warning`
- `error`
All these objects use the same secondary colors which are associated with different use cases.
2021-07-10 16:24:14 +08:00
| Property | When to use |
| ------------ | ---------------------------------------------------------- |
| main | For backgrounds |
| shade | For hover highlight |
| text | For text color |
| border | For borders, currently always the same as text color |
| contrastText | Text color to use for text placed on top of the main color |
Example use cases:
2024-07-26 02:14:19 +08:00
- Want a red background? Use `theme.colors.error.main` .
- Want green text? Use `theme.colors.success.text` .
- Want text to be visible when placed inside a background that uses `theme.colors.error.main` ? Use `theme.colors.error.contrastText` .
2021-07-10 16:24:14 +08:00
### Text colors
| Property | When to use |
| ----------------------------- | ------------------------------------------------------------------------------ |
| theme.colors.text.primary | The default text color |
| theme.colors.text.secondary | Text color for things that should be a bit less prominent |
2024-07-26 02:14:19 +08:00
| theme.colors.text.disabled | Text color for disabled or faint things |
2021-07-10 16:24:14 +08:00
| theme.colors.text.link | Text link color |
| theme.colors.text.maxContrast | Maximum contrast (absolute white in dark theme, absolute black in white theme) |
### Background colors
2025-02-11 19:27:04 +08:00
| Property | When to use |
| --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| theme.colors.background.canvas | Dashboard background. A background surface for panels and panes that use primary background |
| theme.colors.background.primary | The default content background for content panes and panels |
| theme.colors.background.secondary | For cards and other surfaces that need to stand out when placed on top of the primary background |
| theme.colors.background.elevated | For popovers and menu backgrounds. This is the same color as primary in most light themes but in dark themes it has a brighter shade to help give it contrast against the primary background |
2021-07-10 16:24:14 +08:00
### Borders
| Property | When to use |
| -------------------------- | ------------------------------------------------------------ |
| theme.colors.border.weak | Primary border for panels and panes and other subtle borders |
| theme.colors.border.medium | For stronger borders like inputs |
2022-10-05 07:43:49 +08:00
| theme.colors.border.strong | For even stronger border like hover highlighted border |
2021-07-10 16:24:14 +08:00
### Actions
| Property | When to use |
| ---------------------------- | ----------------------------------------------------- |
| theme.colors.action.hover | Background color for hover on card, menu or list item |
| theme.colors.action.focus | Background color for focused card, menu or list item |
| theme.colors.action.selected | Background color for selected card, menu or list item |
### Paddings and margins
| Example | Result |
| --------------------------- | ----------------- |
| theme.spacing(1) | 8px |
| theme.spacing(1, 2) | 8px 16px |
| theme.spacing(1, 2, 0.5, 4) | 8px 16px 4px 32px |
### Border radius
| Example | Result |
| --------------------------- | ------ |
| theme.shape.borderRadius(1) | 2px |
| theme.shape.borderRadius(2) | 4px |
### Typography
2024-07-26 02:14:19 +08:00
To customize font family, font sizes, and line heights, use the variables under `theme.typography` .
#### Set the context directly
2021-07-10 16:24:14 +08:00
2024-07-26 02:14:19 +08:00
Use `ThemeContext` like this:
2020-07-01 17:39:46 +08:00
```tsx
2023-10-13 20:11:41 +08:00
import { ThemeContext } from '@grafana/data';
2020-07-01 17:39:46 +08:00
2021-04-23 16:06:42 +08:00
< ThemeContext.Consumer > {(theme) => < Foo theme = {theme} / > }< / ThemeContext.Consumer > ;
2019-07-03 15:50:42 +08:00
```
2024-07-26 02:14:19 +08:00
#### Use `withTheme` higher-order component
2019-07-03 15:50:42 +08:00
2024-07-26 02:14:19 +08:00
With this method your component will be automatically wrapped in `ThemeContext.Consumer` and provided with current theme via the `theme` prop. Components used with `withTheme` must implement the `Themeable` interface.
2019-07-03 15:50:42 +08:00
```ts
import { ThemeContext, Themeable } from '@grafana/ui';
2021-07-10 16:24:14 +08:00
interface FooProps extends Themeable2 {}
2019-07-03 15:50:42 +08:00
const Foo: React.FunctionComponent< FooProps > = () => ...
2021-07-10 16:24:14 +08:00
export default withTheme2(Foo);
2019-11-21 23:52:57 +08:00
```
2024-07-26 02:14:19 +08:00
### Use a theme in tests
2019-11-21 23:52:57 +08:00
2024-07-26 02:14:19 +08:00
If you need to pass a theme object to a function that you are testing, then import `createTheme` and call it without any arguments. For example:
2019-11-21 23:52:57 +08:00
```tsx
2021-07-10 16:24:14 +08:00
import { createTheme } from '@grafana/data';
2019-11-21 23:52:57 +08:00
describe('MyComponent', () => {
2021-07-10 16:24:14 +08:00
it('should work', () => {
result = functionThatNeedsTheme(createTheme());
expect(result).toBe(true);
2019-11-21 23:52:57 +08:00
});
});
2019-07-03 15:50:42 +08:00
```
2024-07-26 02:14:19 +08:00
### Modify Sass variables
Docs: Add links, fix grammar, formatting, wording (#22381)
* Docs: Add links, fix grammar, formatting, wording
- Add links to theme files and technology references
- Adjust formatting to be consistent, e.g., syntax highlighting, section spacing
- Fix misc grammar, punctuation
- Fix SASS -> Sass
Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>
Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
2020-02-25 06:05:56 +08:00
2024-07-26 02:14:19 +08:00
If you need to modify the Sass variable files, we recommend that you migrate the styles to [Emotion ](https://emotion.sh/docs/introduction ).
Docs: Add links, fix grammar, formatting, wording (#22381)
* Docs: Add links, fix grammar, formatting, wording
- Add links to theme files and technology references
- Adjust formatting to be consistent, e.g., syntax highlighting, section spacing
- Fix misc grammar, punctuation
- Fix SASS -> Sass
Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>
Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
2020-02-25 06:05:56 +08:00
2024-07-26 02:14:19 +08:00
For the following variables to apply, you need to run this `yarn dev` task:
2020-07-01 17:39:46 +08:00
2024-07-26 02:14:19 +08:00
- `[_variables|_variables.dark|_variables.light].generated.scss` : These files must be referenced in the main Sass files for Sass variables to be available.
2019-09-20 17:27:57 +08:00
2024-07-26 02:14:19 +08:00
If you need to modify the Sass variable files, be sure to update the files that end with `.tmpl.ts` and not the `.generated.scss` files.
2019-09-20 17:27:57 +08:00
2024-07-26 02:14:19 +08:00
> **Important:** These variable files are automatically generated and should never be modified by hand.