Skip to content

conditional styles #1184

Answered by hasparus
marhalpert asked this question in General
Discussion options

You must be logged in to vote

If you want a visually different kind of component, I'd use Variants.
If you need a small change of style resulting from small change of props/state, I'd use sx prop.

/** @jsx jsx */
import { jsx } from 'theme-ui';

export const Foo = () => {
  const [state, setState] = useState(0);
  
  return (
    <button
      sx={{ background: state % 2 === 0 ? 'lightsalmon' : 'darksalmon' }}
      onClick={() => setState(s => s + 1)}
    >
      +
    </button>
  )
}

Remember that sx prop generates a new classname, so when there's a lot of possible states (e.g. slider), help yourself with inline style prop.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by marhalpert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants