Radium: Updating Button Styles via Props

Phil Holden
InstructorPhil Holden
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

In a CSS library like Bootstrap we can set a button's style to be "primary" or "secondary" by appending classes. For React components we want to be able to do this via props. Radium enables this by composing styles via an array. This mimicks the cascade of CSS.

[00:00] Let's say we want to make our button blue, only if it's a primary button. Otherwise, it will be gray. First, we rename our style object to styles and nest our existing rules into the base property. Then, update the style prop.

[00:13] Now, we can add a primary property and set the background color to blue and the base color to gray. Radium allows us to compose styles using array. It does this by doing a deep merge of properties. Because primary comes after base and the array, its properties will have precedence over those in base.

[00:30] In this case, our button turns from gray to blue. Now, we want to make the color conditional on the kind of button. Add in the kind prop, define the condition. Now, we can toggle the button's color via the props.

egghead
egghead
~ 15 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today