0
votes

I have a demo here

Its a simple react app where I'm using Styled Components with a theme

The theme is like

import { DefaultTheme } from "styled-components";

export const theme: DefaultTheme = {
  sizes: {
    height: "50px",
    width: "50px"
  }
};

Is it possible to use the theme values with arithmetic operations in the styled component

So something like

height: ${({ theme }) => theme.sizes.height}+
    ${({ theme }) => theme.sizes.height};
    

This doesn't work in my demo but I didn't know if the syntax was incorrect or its just not possible.

1
"50px" + "50px" = "50px50px". - Brian Thompson

1 Answers

2
votes

To apply this your theme should look this way, props will be numbers, instead of strings.

export const theme: DefaultTheme = {
  sizes: {
    height: 50,
    width: 50
  }
};

So you can set your height now.

height: ${({ theme }) => theme.sizes.height + theme.sizes.height};