I want to use the color prop I get from Button component to use it for background color using styled component theme.
import React from "react"; import styled from "styled-components"; const StyledButton = styled("button")<{ color: string; padding: string; }>` background: ${(props) => props.theme.colors.primary}; outline: none; box-shadow: none; border: none; padding: ${(props) => props.padding}; border-radius: 5px; color: white; font-size: ${(props) => props.theme.fontSizes.small}; margin: 0 10px; `; interface Props extends React.ComponentPropsWithoutRef<"button"> { color?: string; padding?: string; } const Button = ({ children, color, padding }: Props) => { return ( <StyledButton color={color!} padding={padding!}> {children} </StyledButton> ); }; export default Button; Theme:
import { DefaultTheme } from "styled-components"; const theme: DefaultTheme = { colors: { primary: "#5A8DEE", boldPrimary: "#274d8c", lightPrimary: "#dae3f5", green: "#5CCf4C", gray: "#F2F4F4", white: "#FFFFFF", text: "#5f5f63", lightText: "#878791", }, fontSizes: { extraSmall: "0.75rem", small: "1rem", medium: "1.25rem", large: "1.50rem", }, }; export default theme; Like when I get primary from Button props, I want it to get the color codes from the theme context I made.
background: ${(props) => props.theme.colors.primary};andfont-size: ${(props) => props.theme.fontSizes.small};works pretty fine.themeis working what is the question/issue then? Sorry, I'm just not understanding what you are asking for.${(props) => props.theme.colors.primary};. I can do conditional but doing it for all +10 colors is messy.primary? Theprops.theme.colors.primary? It's passed in thethemeprop which you said is working. Are you saying your theme has multipletheme.colorproperties and you want to specify which from a passed prop?