score:0

when dealing with styled component variants here is what i like to do to keep things organised and scalable.

if the variants are stored within the same file i am using the inheritance properties:

const defaultbutton = styled.button`
    color: ${(props) => props.theme.primary};
`;

const buttonflashy = styled(defaultbutton)`
    color: fuchsia;
`;

const buttondisabled = styled(defaultbutton)`
    color: ${(props) => props.theme.grey};
`;

if if we are talking about a reusable components i would use this technique:

import styled from 'styled-components';

// note that having a default class is important
const styledcta = ({ classname = 'default', children }) => {
    return <wrapper classname={classname}>{children}</wrapper>;
};

/*
 * default button styles
 */
const wrapper = styled.button`
    color: #000;
`;

/*
 * custom button variant 1
 */
export const styledctafushia = styled(styledcta)`
    && {
        color: fuchsia;
    }
`;

/*
 * custom button variant 2
 */
export const styledctadisabled = styled(styledcta)`
    && {
        color: ${(props) => props.theme.colors.grey.light};
    }
`;

export default styledcta;

usage:

import styledcta, { styledctadisabled, styledctafushia } from 'components/styledcta';

const page = () => {
    return (
        <>
            <styledcta>default cta</styledcta>
            <styledctadisabled>disable cta</styledctadisabled>
            <styledctafushia>fuchsia cta</styledctafushia>
        </>
    )
};

read more about this in the blog posts i created on the subject here and there.

score:0

there are many ways to do this. one simple way is to use the package called styled-components-modifiers. documentation is simple and straightforward.

https://www.npmjs.com/package/styled-components-modifiers

simple usage example:

import { applystylemodifiers } from 'styled-components-modifiers';

export const text_modifiers = {
  success: () => `
  color: #118d4e;
 `,
 warning: () => `
 color: #dbc72a;
 `,

 error: () => `
 color: #db2a30;
 `,
};

export const heading = styled.h2`
color: #28293d;
font-weight: 600;
${applystylemodifiers(text_modifiers)};
`;

in the component - import heading and use modifier prop to select the variants.

 <heading modifiers='success'>
    hello buddy!!
 </heading>

score:0

styled components are usually used with styled system that supports variants and other nice features that enhance styled components. in the example below button prop variant automatically is mapped to keys of variants object:

const buttonvariant = ({ theme }) =>
  variant({
    variants: {
      header: {
        backgroundcolor: theme.colors.lightblue,
        color: theme.colors.white,
        active: theme.colors.blue,
      },
      white: {
        backgroundcolor: 'white',
        color: theme.colors.lightblue,
        active: theme.colors.blue,
      },
    },
  })

const button = styled.button`
  ${(props) => buttonvariant(props)}
`

styled system variants: https://styled-system.com/variants

score:0

use the variant api to apply styles to a component based on a single prop. this can be a handy way to support slight stylistic variations in button or typography components.

import the variant function and pass variant style objects in your component definition. when defining variants inline, you can use styled system like syntax to pick up values from your theme.

// example button with variants
import styled from 'styled-components'
import { variant } from 'styled-system'

const button = styled('button')(
  {
    appearance: 'none',
    fontfamily: 'inherit',
  },
  variant({
    variants: {
      primary: {
        color: 'white',
        bg: 'primary',
      },
      secondary: {
        color: 'white',
        bg: 'secondary',
      },
    }
  })
)

score:2

this is just my opinion:

i don't think we can do anything very different from what you did.

a different way that i thought, would be to create an options object to map the possibilities of the variant, like this:

const variantoptions = {
  header: {
    backgroundcolor: theme.colors.lightblue,
    color: theme.colors.white,
    active: theme.colors.blue,
  },
  white: {
    backgroundcolor: "white",
    color: theme.colors.lightblue,
    active: theme.colors.blue,
  },
};

and use it in your style component like this:

const buttonstyle = styled.button`
  padding: 8px 20px;
  border: none;
  outline: none;
  font-weight: ${(props) => props.theme.font.headerfontweight};
  font-size: ${(props) => props.theme.font.headerfontsize};
  display: block;
  &:hover {
    cursor: pointer;
  }

  ${({ variant }) =>
    variant &&
    variantoptions[variant] &&
    css`
       background-color: ${variantoptions[variant].backgroundcolor};
       color: ${variantoptions[variant].color};
       &:active {
          color: ${variantoptions[variant].active};
       }
   `}
`;

and all of this buttons will work:

<buttonstyle variant="*wrong*">button</buttonstyle>
<buttonstyle variant="header">button</buttonstyle>
<buttonstyle variant="white">button</buttonstyle>
<buttonstyle>button</buttonstyle>

score:3

inspired by previous solutions, i want to share what i came up with:

import styled, { css, defaulttheme } from 'styled-components';

const variantstyles = (theme: defaulttheme, variant = 'primary') =>
  ({
    primary: css`
      color: ${theme.colors.light};
      background: ${theme.colors.primary};
      border: 1px solid ${theme.colors.primary};
    `,
  }[variant]);

const button = styled.button<{ variant: string }>`
  padding: 1rem;
  font-size: 0.875rem;
  transition: all 0.3s;
  cursor: pointer;

  ${({ theme, variant }) => variantstyles(theme, variant)}

  &:active {
    transform: translatey(1.5px);
  }
`;

export default button;

for now it contains only primary and its the default one, by you can add more variants by adding new object to variantstyles object

then you can use it by passing the variant as a prop or keep the default by not passing any variant.

import { button } from './herosection.styles';

<button variant="primary">start learning</button>

Related Query

More Query from same tag