cristianbote / goober

🥜 goober, a less than 1KB 🎉 css-in-js alternative with a familiar API
https://goober.rocks
MIT License
3.11k stars 118 forks source link

Duplicated styles within dynamic classes #546

Open RainArroDev opened 1 year ago

RainArroDev commented 1 year ago

Can this type of syntax be written differently so the outputted classes would not have duplicated content? In the example font-size: 16px; is duplicated between classes.

Input

const ButtonTheme = (theme: Colors) => {
  const _theme = useTheme();

  return {
    primary: `
      background: ${_theme.colors.primary};
      &:hover {
        color: red;
      }
    `,
    secondary: `
      background: ${_theme.colors.secondary};
    `,
  }[theme];
};

export const ButtonBase = (props: { theme?: Colors }) => {
  return css`
    font-size: 16px;
    ${ButtonTheme(props.theme || 'primary')}
  `;
};

Output

<style id="_goober">
            .go1767598017 {
                font-size: 16px;
                background: #0066ff;
            }

            .go1767598017:hover {
                color: red;
            }

            .go1138492251 {
                font-size: 16px;
                background: #43aa8b;
            }
 </style>