theme-ui icon indicating copy to clipboard operation
theme-ui copied to clipboard

Hover/Conditional Styles

Open suyang581 opened this issue 6 years ago • 5 comments

Is there an easy way to define hover styles or general styles that are applied conditionally to a particular variant? I couldn't find any information in the docs.

suyang581 avatar May 06 '20 12:05 suyang581

There are a few examples for some components such as Button

const theme = {
  // ...
  buttons: {
    primary: {
      '&:hover': {
        bg: 'text'
      }
    }
  }
}

styles that are applied conditionally to a particuliar variant

Not sure what you mean by that, themes are static (and luckily: a dynamic theme could result in unexpected UI behaviours)

But if you are after general style variants, there is nothing preventing you from composing your theme more in-depth:

const hoverStyle = {
  '&:hover,&:active': {
    textDecoration: 'underline'
  }
};

const disabledStyle = {
  '&:disabled': {
    opacity: 0.65,
    cursor: 'not-allowed'
  }
};

const theme = {
  // ...
  buttons: {
    primary: {
      bg: 'primary',
      color: 'text',
      ...hoverStyle,
      ...disabledStyle
    }
  }
}

flo-sch avatar May 06 '20 16:05 flo-sch

Thank you!

suyang581 avatar May 06 '20 16:05 suyang581

I came here looking for a solution to conditionally styling an element one way or another, based on a boolean variable.

For future readers, this is what I ended up with:

sx={{
  color: selected ? 'accent' : 'primary'
}}

phivk avatar Jun 29 '20 15:06 phivk

@suaurial @flo-sch I am taking a slightly different approach which some might prefer:

buttons: {
  base: {
    [ shared button styles ]
    '&:disabled': {
      variant: 'buttons.disabled'
    }
  },
  disabled: {  
   [ disabled styles ]
  }
  primary: { 
    variant: 'buttons.base',
    backgroundColor: 'primary',
    [other more specific styles]
  }
}

rgreenhalge avatar Aug 13 '21 23:08 rgreenhalge

That works too, was there a specific reason to define disabled as a variant itself though?

(Perhaps to apply it on a <a /> which do not have css :disabled selectors?)

flo-sch avatar Aug 14 '21 07:08 flo-sch