CSS Components logo

Example Component

A full example of a component using CSS Components.

Below is a full example of a basic component put together using CSS Components.

1src
2└── components
3    └── Panel
4        ├── index.tsx # The main component file
5        ├── styles.module.css # The CSS file
6        └── styles.ts # The CSS Components file
7    ├── SomeOtherComponent
8    └── SomeOtherComponent
9

Each component folder can be split into 3 files:

Let's create a simple Panel component with a title and some content. The component will have a wide variant that will make the panel fill the width of its container.

Here it is in action:

Hello World

This is some example content. It's working great! 😄

1import Panel from "@/components/Panel";
2
3() => (
4  <Panel title="Hello World" wide>
5    <p>This is some example content. It's working great! 😄</p>
6  </Panel>
7);
8

index.tsx

This is the main component file. It's a simple component with a title and some content.

If you've used React in any capacity this format should be very familiar to you, with the exception of PanelWrapper, Title and Content. These are all CSS Components that are imported from styles.ts.

Note how this component is free of ugly CSS class names. Everything is semantic.

1import { PanelWrapper, Title, Content } from "./styles";
2
3interface Props {
4  title: string;
5  children: React.ReactNode;
6  wide?: boolean;
7}
8
9const Panel = ({ title, children, wide }: Props) => (
10  <PanelWrapper wide={wide}>
11    <Title>{title}</Title>
12    <Content>{children}</Content>
13  </PanelWrapper>
14);
15
16export default Panel;
17

styles.module.css

Next is your bog standard CSS file. It contains all the CSS classes used by CSS Components. You're free to name them how you want, but we do have a helpful naming convention.

While this file uses CSS, you can use whatever styling solution you want. We'd recommend using SCSS; it works really well with CSS components!

1div.PanelWrapper {
2  display: flex;
3  flex-direction: column;
4  text-align: center;
5  margin: auto;
6  width: 240px;
7  border-radius: 0.5rem;
8  overflow: hidden;
9  background-color: #e6e6e6;
10  color: black;
11}
12
13div.PanelWrapper_wide {
14  width: 100%;
15}
16
17h2.Title {
18  background-color: #d6d6d6;
19  margin: 0;
20  padding: 1rem;
21}
22
23section.Content {
24  padding: 1rem;
25}
26

styles.ts ✨

The styles file is where the magic happens! It contains all of the CSS Components that are imported into the main React component.

As you can see, CSS Components are nothing more than a way to map CSS classes to React components.

You can either create this file yourself, or use our helpful CLI tool.

1import { styled } from "@phantomstudios/css-components";
2
3import css from "./styles.module.css";
4
5export const PanelWrapper = styled("div", {
6  css: css.PanelWrapper,
7  variants: {
8    wide: {
9      true: css.PanelWrapper_wide,
10    },
11  },
12});
13
14export const Title = styled("h2", {
15  css: css.Title,
16});
17
18export const Content = styled("section", {
19  css: css.Content,
20});
21

And that's it!

As you can see, the only thing that's really different from your standard, CSS-Component-less workflow is the styles.ts file. Pop that in and you're good to go.

Happy styling!