Coding Guidelines
Please ensure you are familiar with these guidelines when developing in this repository.
Table of contents
Styled Components Guidelines
Styles are not (usually) shared
For the most part, styled components should be considered **owned by a single component**. The whole point of using styled-components is to encapsulate the css, avoiding the nightmare that occurs when trying to modify css that is shared across the app. These styled components contain a lot of css. We separate out these styled components to make the other component files easier to read.
Therefore, most of the time styled components should be placed into a file named xxx.styles.ts, where xxx.tsx is the component consuming the styled components.
What this means:
- You should not export
xxx.styles.tsinindex.tsfiles. - Styled components should be named basically. Use basic terms like
HeaderContainerorNameLabel. Name these much like you would name css classNames. Their usage is isolated, so there is no need to worry about name clashes with other styled components in other*.style.tsfiles. - A component should not import styled-components that it does not owned. E.g.
foo.tsxshould not import items frombar.styles.ts. - If two components need a styled component that is styled the same, just copy the styled component. It is okay to have duplicate css.
- If you find there is a legitimate styled component that is reusable across the application, consider putting it in
common/styles.ts.- Note: Please do not make
common/styles.tsa dumping ground for components that aren’t actually reused much.
- Note: Please do not make
Use transient props for style options
Styled components can have props, which must be filtered to prevent those props from becoming attributes on the DOM element.
You should prefix all props with $ to ensure they are auto-filtered.
export interface FooProps {
$good: number; // Prefix property with $ to make it transient
bad: number; // No prefix, which means this will end up as an attribute
}
export const Foo = styled.div<FooProps>`
width: ${props => props.$good};
height: ${props => props.bad};
`;
// This:
<Foo $good={42} bad={66}>Testing</Foo>
// Produces this:
<div class="xyz" bad="66">Testing</div>