Skip to content

Make order of variant prop irrelevant #723

@cour64

Description

@cour64

Say I have the variant for buttons defined as follows:

buttons: {
  primary: {
    bg: 'primary'
  }
}

Currently the order of the props matters. e.g.

sx={{
  variant: 'buttons.primary',
  bg: 'secondary'
}}

Will correctly overwrite the color set with the bg prop to secondary but

sx={{
  bg: 'secondary',
  variant: 'buttons.primary'
}}

will not. The bg prop set in the button.primary variant will overwrite the bg prop set specifically on the sx prop.

I'm not sure if there is any use for this overwriting behaviour, but it makes sense to me that styles defined specifically should overwrite the styles defined in variant regardless of the order they are set.

A potential solution is to perhaps swap the order of styles when they are spread in the @theme-ui/css package so from:

if (key === 'variant') {
const variant = css(get(theme, val))(theme)
result = { ...result, ...variant }
continue
}

to

if (key === 'variant') {
  const variant = css(get(theme, val))(theme)
  result = { ...variant, ...result }
  continue
}

Not sure if this will work exhaustively, but it does pass all the tests in my fork

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions