2024-08-23 10:08:04 -05:00
# Frontend style guide
Grafana Labs follows the [Airbnb React/JSX Style Guide ](https://github.com/airbnb/javascript/tree/master/react ) in matters pertaining to React. This guide provides highlights of the style rules we follow.
2019-01-23 04:37:46 -06:00
## Basic rules
2019-09-20 11:45:06 -05:00
- Try to keep files small and focused.
- Break large components up into sub-components.
2020-02-21 00:35:54 -06:00
- Use spaces for indentation.
2019-01-23 04:37:46 -06:00
2024-08-23 10:08:04 -05:00
## Naming conventions
Follow these guidelines when naming elements of your code.
2019-01-23 04:37:46 -06:00
2024-08-23 10:08:04 -05:00
### Class names
2019-01-23 04:37:46 -06:00
2024-08-23 10:08:04 -05:00
Use PascalCase. For example:
2019-01-23 04:37:46 -06:00
2020-02-21 00:35:54 -06:00
```typescript
// bad
class dataLink {
//...
}
2019-01-23 04:37:46 -06:00
// good
2020-02-21 00:35:54 -06:00
class DataLink {
//...
}
```
2024-08-23 10:08:04 -05:00
### Constants
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
Use ALL CAPS for constants. For example:
```typescript
2020-02-21 00:35:54 -06:00
// bad
2024-08-23 10:08:04 -05:00
const constantValue = "This string won't change";
2020-02-21 00:35:54 -06:00
// bad
2024-08-23 10:08:04 -05:00
const constant_value = "This string won't change";
2020-02-21 00:35:54 -06:00
// good
2024-08-23 10:08:04 -05:00
const CONSTANT_VALUE = "This string won't change";
```
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
### Emotion class names
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
Use camelCase. For example:
```typescript
const getStyles = (theme: GrafanaTheme2) => ({
// bad
ElementWrapper: css`...`,
// bad
['element-wrapper']: css`...`,
// good
elementWrapper: css({
padding: theme.spacing(1, 2),
background: theme.colors.background.secondary,
}),
});
2020-02-21 00:35:54 -06:00
```
2024-08-23 10:08:04 -05:00
Use hook useStyles2(getStyles) to memoize the styles generation and try to avoid passing props to the getStyles function and instead compose classes using Emotion CX function.
### Enums
Use PascalCase. For example:
2020-02-21 00:35:54 -06:00
```
// bad
enum buttonVariant {
//...
}
// good
enum ButtonVariant {
//...
}
```
2024-08-23 10:08:04 -05:00
### Files and directories
Name files according to the primary export:
- When the primary export is a class or React component, use PascalCase.
- When the primary export is a function, use camelCase.
For files that export multiple utility functions, use the name that describes the responsibility of grouped utilities. For example, a file that exports math utilities should be named `math.ts` .
- Use `constants.ts` for files that export constants.
- Use `actions.ts` for files that export Redux actions.
- Use `reducers.ts` for Redux reducers.
- Use `*.test.ts(x)` for test files.
For directory names, use dash-case (sometimes called kebab-case).
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
- Use `features/new-important-feature/utils.ts`
### Functions
Use PascalCase. For example:
Use camelCase.
2020-02-21 00:35:54 -06:00
```typescript
// bad
const CalculatePercentage = () => { ... }
// bad
const calculate_percentage = () => { ... }
// good
const calculatePercentage = () => { ... }
```
2024-08-23 10:08:04 -05:00
### Interfaces
Use PascalCase. For example:
```
// bad
interface buttonProps {
//...
}
// bad
interface button_props {
//...
}
// bad
interface IButtonProps {
//...
}
// good
interface ButtonProps {
//...
}
// bad
type requestInfo = ...
// bad
type request_info = ...
// good
type RequestInfo = ...
```
### Methods
Use PascalCase. For example:
Use camelCase.
2020-02-21 00:35:54 -06:00
```typescript
class DateCalculator {
// bad
CalculateTimeRange () {...}
}
class DateCalculator {
// bad
2020-06-30 16:42:50 -05:00
calculate_time_range () {...}
2020-02-21 00:35:54 -06:00
}
class DateCalculator {
// good
calculateTimeRange () {...}
}
```
2024-08-23 10:08:04 -05:00
### React components
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
Follow these guidelines for naming React components.
#### React callback props and handlers
Name callback props and handlers with an _on_ prefix. For example:
```tsx
2020-02-21 00:35:54 -06:00
// bad
2024-08-23 10:08:04 -05:00
handleChange = () => {
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
};
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
render() {
return (
< MyComponent changed = {this.handleChange} / >
);
}
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
// good
onChange = () => {
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
};
render() {
return (
< MyComponent onChange = {this.onChange} / >
);
2020-02-21 00:35:54 -06:00
}
2024-08-23 10:08:04 -05:00
2020-02-21 00:35:54 -06:00
```
2024-08-23 10:08:04 -05:00
#### React component constructor
Use the following convention when implementing these React components:
2020-02-21 00:35:54 -06:00
```typescript
2024-08-23 10:08:04 -05:00
// bad
constructor(props) {...}
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
// good
constructor(props: Props) {...}
2020-02-21 00:35:54 -06:00
```
2024-08-23 10:08:04 -05:00
#### React component defaultProps
2022-09-23 01:22:16 -05:00
2024-08-23 10:08:04 -05:00
Use the following convention when implementing these React components:
2020-02-21 00:35:54 -06:00
```typescript
// bad
2024-08-23 10:08:04 -05:00
static defaultProps = { ... }
// good
static defaultProps: Partial< Props > = { ... }
```
#### React component definitions
Use the following convention when implementing these React components:
```jsx
2020-02-21 00:35:54 -06:00
// bad
2024-08-23 10:08:04 -05:00
export class YourClass extends PureComponent { ... }
2020-02-21 00:35:54 -06:00
// good
2024-08-23 10:08:04 -05:00
export class YourClass extends PureComponent< {},{}> { ... }
```
#### React state and properties
Use camelCase. For example:
```typescript
interface ModalState {
// bad
IsActive: boolean;
// bad
is_active: boolean;
// good
isActive: boolean;
}
2020-02-21 00:35:54 -06:00
```
2024-08-23 10:08:04 -05:00
### SASS
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
SASS styles are deprecated. You should migrate to Emotion whenever you need to modify SASS styles.
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
### Types
2020-05-05 10:36:28 -05:00
2024-08-23 10:08:04 -05:00
In general, you should let TypeScript infer the types so that there's no need to explicitly define the type for each variable.
2020-05-05 10:36:28 -05:00
There are some exceptions to this:
```typescript
2024-08-23 10:08:04 -05:00
// TypeScript needs to know the type of arrays or objects; otherwise, it infers type as an array of any
2020-05-05 10:36:28 -05:00
// bad
const stringArray = [];
// good
const stringArray: string[] = [];
```
2021-08-31 05:55:05 -05:00
Specify function return types explicitly in new code. This improves readability by being able to tell what a function returns just by looking at the signature. It also prevents errors when a function's return type is broader than expected by the author.
2020-05-05 10:36:28 -05:00
2024-08-23 10:08:04 -05:00
> **Note:** Linting is not enabled for this issue because there is old code that needs to be fixed first.
2020-05-05 10:36:28 -05:00
```typescript
// bad
function transform(value?: string) {
if (!value) {
2021-08-31 05:55:05 -05:00
return undefined;
2020-05-05 10:36:28 -05:00
}
2021-08-31 05:55:05 -05:00
return applyTransform(value);
}
2020-05-05 10:36:28 -05:00
// good
function transform(value?: string): TransformedValue | undefined {
if (!value) {
2021-08-31 05:55:05 -05:00
return undefined;
2020-05-05 10:36:28 -05:00
}
2021-08-31 05:55:05 -05:00
return applyTransform(value);
}
2020-05-05 10:36:28 -05:00
```
2024-08-23 10:08:04 -05:00
### Variables
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
Use PascalCase. For example:
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
Use camelCase.
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
```typescript
// bad
const QueryTargets = [];
// bad
const query_targets = [];
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
// good
const queryTargets = [];
```
2021-08-27 01:27:56 -05:00
2024-08-23 10:08:04 -05:00
## Code organization
2020-02-21 00:35:54 -06:00
Organize your code in a directory that encloses feature code:
2024-08-23 10:08:04 -05:00
- Put Redux state and domain logic code in the `state` directory (for example, `features/my-feature/state/actions.ts` ).
- Put React components in the `components` directory (for example, `features/my-feature/components/ButtonPeopleDreamOf.tsx` ).
2020-02-21 00:35:54 -06:00
- Put test files next to the test subject.
2024-08-23 10:08:04 -05:00
- Put containers (pages) in the feature root (for example, `features/my-feature/DashboardPage.tsx` ).
- Put API function calls that aren't a Redux thunk in an `api.ts` file within the same directory.
- Subcomponents should live in the component folders. Small components don't need their own folder.
2020-02-21 00:35:54 -06:00
- Component SASS styles should live in the same folder as component code.
2024-08-23 10:08:04 -05:00
For code that needs to be used by an external plugin:
2020-02-21 00:35:54 -06:00
- Put components and types in `@grafana/ui` .
- Put data models and data utilities in `@grafana/data` .
- Put runtime services interfaces in `@grafana/runtime` .
2024-08-23 10:08:04 -05:00
### Exports
2020-02-21 00:35:54 -06:00
- Use named exports for all code you want to export from a file.
2024-08-23 10:08:04 -05:00
- Use declaration exports (that is, `export const foo = ...` ).
2021-10-19 01:30:43 -05:00
- Avoid using default exports (for example, `export default foo` ).
2020-02-21 00:35:54 -06:00
- Export only the code that is meant to be used outside the module.
2024-08-23 10:08:04 -05:00
### Code comments
2020-02-21 00:35:54 -06:00
- Use [TSDoc ](https://github.com/microsoft/tsdoc ) comments to document your code.
- Use [react-docgen ](https://github.com/reactjs/react-docgen ) comments (`/** ... */`) for props documentation.
2024-08-23 10:08:04 -05:00
- Use inline comments for comments inside functions, classes, etc.
2020-02-25 06:59:11 -06:00
- Please try to follow the [code comment guidelines ](./code-comments.md ) when adding comments.
2020-02-21 00:35:54 -06:00
2024-08-23 10:08:04 -05:00
## Linting
2020-02-21 00:35:54 -06:00
Linting is performed using [@grafana/eslint-config ](https://github.com/grafana/eslint-config-grafana ).
2024-08-23 10:08:04 -05:00
## Functional components
2019-01-23 04:37:46 -06:00
2024-08-23 10:08:04 -05:00
Use function declarations instead of function expressions when creating a new React functional component. For example:
2021-10-19 01:30:43 -05:00
```typescript
2023-03-16 03:57:29 -05:00
// bad
export const Component = (props: Props) => { ... }
// bad
export const Component: React.FC< Props > = (props) => { ... }
// good
2022-07-19 12:12:11 -05:00
export function Component(props: Props) { ... }
2021-10-19 01:30:43 -05:00
```
2019-10-03 07:13:58 -05:00
## State management
2019-08-09 06:15:52 -05:00
- Don't mutate state in reducers or thunks.
2020-06-05 06:54:27 -05:00
- Use `createSlice` . See [Redux Toolkit ](https://redux-toolkit.js.org/ ) for more details.
2019-10-03 07:13:58 -05:00
- Use `reducerTester` to test reducers. See [Redux framework ](redux.md ) for more details.
- Use state selectors to access state instead of accessing state directly.