mirror of
https://github.com/jbranchaud/til
synced 2026-01-09 18:18:02 +00:00
Add Inactive And Active Component Styles With Radium as a react til
This commit is contained in:
45
react/inactive-and-active-component-styles-with-radium.md
Normal file
45
react/inactive-and-active-component-styles-with-radium.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# Inactive And Active Component Styles With Radium
|
||||
|
||||
[Radium](https://github.com/FormidableLabs/radium) is "toolchain for React
|
||||
component styling" allowing you to do comprehensive inline styling with CSS.
|
||||
|
||||
Often times, especially in the case of a series of nav elements, there is a
|
||||
need to style one element as _active_ while styling the rest as _inactive_.
|
||||
This can be achieved with Radium by defining two groups of styles (`base`
|
||||
and `active`) and then relying on props to conditionally apply the active
|
||||
style.
|
||||
|
||||
```javascript
|
||||
import React from 'react';
|
||||
import Radium from 'radium';
|
||||
|
||||
const styles = {
|
||||
base: {
|
||||
textDecoration: "none",
|
||||
color: "gray",
|
||||
},
|
||||
active: {
|
||||
color: "black",
|
||||
backgroundColor: "lightgray",
|
||||
},
|
||||
};
|
||||
|
||||
let NavItem = ({ label, path, active }) => {
|
||||
return (
|
||||
<a
|
||||
href={path}
|
||||
style={[
|
||||
styles.base,
|
||||
styles[active && 'active'],
|
||||
]}
|
||||
>{label}</a>
|
||||
);
|
||||
};
|
||||
|
||||
NavItem = Radium(NavItem);
|
||||
```
|
||||
|
||||
With Radium, our `base` (_inactive_) styles always get applied. Then, the
|
||||
`active` styles only get applied when the `active` prop is true. We produce
|
||||
a Radium-ified version of our `NavItem` on the last line so that Radium can
|
||||
handle all of the styling of the component.
|
||||
Reference in New Issue
Block a user