2019-09-20 04:27:57 -05:00
# Theming Grafana
2019-07-03 02:50:42 -05:00
2019-09-20 04:27:57 -05:00
## Overview
2019-07-03 02:50:42 -05:00
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-24 16:05:56 -06:00
**Themes are implemented in Typescript.** That's because our goal is to share variables between Grafana TypeScript and [Sass ](https://sass-lang.com/ ) code. Theme definitions are located in the following files:
2021-07-10 03:24:14 -05: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 02:50:42 -05:00
2019-09-20 04:27:57 -05: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-24 16:05:56 -06:00
This section provides usage guidelines.
2019-09-20 04:27:57 -05:00
### Using themes in React components
2019-07-03 02:50:42 -05:00
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-24 16:05:56 -06:00
Here's how to use Grafana themes in React components.
2021-07-10 03:24:14 -05:00
#### useStyles2 hook
2020-07-01 04:39:46 -05:00
2021-07-10 03:24:14 -05:00
`useStyles2` memoizes the function and provides access to the theme.
2019-07-03 02:50:42 -05:00
2019-09-20 04:27:57 -05:00
```tsx
2020-07-01 04:39:46 -05:00
import React, { FC } from 'react';
2021-07-10 03:24:14 -05:00
import { GrafanaTheme2 } from '@grafana/data';
import { useStyles2 } from '@grafana/ui';
2021-04-23 03:06:42 -05:00
import { css } from '@emotion/css';
2019-07-03 02:50:42 -05:00
2021-07-10 03:24:14 -05:00
const getComponentStyles = (theme: GrafanaTheme2) => css`
2020-07-01 04:39:46 -05:00
padding: ${theme.spacing.md};
`;
const Foo: FC< FooProps > = () => {
2021-07-10 03:24:14 -05:00
const styles = useStyles2(getComponentsStyles);
2020-07-01 04:39:46 -05:00
// Use styles with className
};
2019-07-03 02:50:42 -05:00
```
2020-07-01 04:39:46 -05:00
#### Get the theme object
2019-07-03 02:50:42 -05:00
2019-09-20 04:27:57 -05:00
```tsx
2020-07-01 04:39:46 -05:00
import React, { FC } from 'react';
2021-07-10 03:24:14 -05:00
import { useTheme2 } from '@grafana/ui';
2019-07-03 02:50:42 -05:00
2020-07-01 04:39:46 -05:00
const Foo: FC< FooProps > = () => {
2021-07-10 03:24:14 -05:00
const theme = useTheme2();
2019-09-09 01:18:38 -05:00
// Your component has access to the theme variables now
2020-07-01 04:39:46 -05:00
};
```
2021-07-10 03:24:14 -05:00
## Picking the right variable
### The rich color object and the state colors
The theme.colors object has 6 rich color objects for `primary` , `secondary` , `info` , `success` , `warning` and `error` . These all
have the same sub colors that have different use cases.
| 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:
- 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` then use `theme.colors.error.contrastText` .
### 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 |
| theme.colors.text.disabled | Text color for disabled / faint things |
| 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
| Property | When to use |
| --------------------------------- | ------------------------------------------------------------------------------------------------- |
| theme.colors.background.canvas | Think 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 |
### 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 |
| theme.colors.border.strong | For even stronger border like hover highighted border |
### 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
2021-08-20 08:53:54 -05:00
For font family, font sizes and line heights use the variables under `theme.typography` .
2021-07-10 03:24:14 -05:00
2020-07-01 04:39:46 -05:00
#### Using `ThemeContext` directly
```tsx
import { ThemeContext } from '@grafana/ui';
2021-04-23 03:06:42 -05:00
< ThemeContext.Consumer > {(theme) => < Foo theme = {theme} / > }< / ThemeContext.Consumer > ;
2019-07-03 02:50:42 -05:00
```
2019-11-21 09:52:57 -06:00
#### Using `withTheme` higher-order component (HOC)
2019-07-03 02:50:42 -05:00
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-24 16:05:56 -06:00
With this method your component will be automatically wrapped in `ThemeContext.Consumer` and provided with current theme via `theme` prop. Components used with `withTheme` must implement the `Themeable` interface.
2019-07-03 02:50:42 -05:00
```ts
import { ThemeContext, Themeable } from '@grafana/ui';
2021-07-10 03:24:14 -05:00
interface FooProps extends Themeable2 {}
2019-07-03 02:50:42 -05:00
const Foo: React.FunctionComponent< FooProps > = () => ...
2021-07-10 03:24:14 -05:00
export default withTheme2(Foo);
2019-11-21 09:52:57 -06:00
```
2021-07-10 03:24:14 -05:00
### Using theme in tests
2019-11-21 09:52:57 -06:00
2021-07-10 03:24:14 -05:00
If you need to pass a theme object to a function under test just import `createTheme` and call it without
any arguments.
2019-11-21 09:52:57 -06:00
```tsx
2021-07-10 03:24:14 -05:00
import { createTheme } from '@grafana/data';
2019-11-21 09:52:57 -06:00
describe('MyComponent', () => {
2021-07-10 03:24:14 -05:00
it('should work', () => {
result = functionThatNeedsTheme(createTheme());
expect(result).toBe(true);
2019-11-21 09:52:57 -06:00
});
});
2019-07-03 02:50:42 -05:00
```
2019-09-20 04:27:57 -05:00
## FAQ
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-24 16:05:56 -06:00
This section provides insight into frequently-asked questions.
### How can I modify Sass variable files?
2020-07-01 04:39:46 -05:00
**If possible, migrate styles to Emotion**
2019-09-20 04:27:57 -05:00
> For the following to apply you need to run `yarn dev` task.
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-24 16:05:56 -06:00
`[_variables|_variables.dark|_variables.light].generated.scss` files are the ones that are referenced in the main Sass files for Sass variables to be available. **These files are automatically generated and should never be modified by hand!**
2019-09-20 04:27:57 -05:00
2021-07-10 03:24:14 -05: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.