UNPKG

7.68 kBMarkdownView Raw
1# Frequently Asked Questions
2
3- [How do I use pseudo-selectors like `:checked`, `:last`, `:before`, or `:after`?](#how-do-i-use-pseudo-selectors-like-checked-last-before-or-after)
4- [How can I use `ReactCSSTransitionGroup` without any classes?](#how-can-i-use-reactcsstransitiongroup-without-any-classes)
5- [How can I use Radium with jsbin?](#how-can-i-use-radium-with-jsbin)
6- [Can I use my favourite CSS/LESS/SASS syntax?](#can-i-use-my-favourite-csslesssass-syntax)
7- [Can I use Radium with Bootstrap?](#can-i-use-radium-with-bootstrap)
8- [Why doesn't Radium work on react-router's Link, or react-bootstrap's Button, or SomeOtherComponent?](#why-doesnt-radium-work-on-react-routers-link-or-react-bootstraps-button-or-someothercomponent)
9- [How can I get rid of `userAgent` warnings in tests?](#how-can-i-get-rid-of-useragent-warnings-in-tests)
10- [Why do React warnings have the wrong component name?](#why-do-react-warnings-have-the-wrong-component-name)
11
12## How do I use pseudo-selectors like `:checked`, `:last`, `:before`, or `:after`?
13
14Radium only provides the interactivity pseudo-selectors `:hover`, `:active`, and `:focus`. You need to use JavaScript logic to implement the others. To implement `:checked` for example:
15
16```jsx
17class CheckForBold extends React.Component {
18 constructor() {
19 super();
20 this.state = {isChecked: false};
21 }
22
23 _onChange = () => {
24 this.setState({isChecked: !this.state.isChecked});
25 };
26
27 render() {
28 return (
29 <label style={{fontWeight: this.state.isChecked ? 'bold' : 'normal'}}>
30 <input
31 checked={this.state.isChecked}
32 onChange={this._onChange}
33 type="checkbox"
34 />
35 {' '}Check for bold
36 </label>
37 );
38 }
39}
40```
41
42Instead of `:first` and `:last`, change behavior during array iteration. Note that the border property is broken down into parts to avoid complications as in issue [#95](https://github.com/FormidableLabs/radium/issues/95).
43
44```jsx
45var droids = [
46 'R2-D2',
47 'C-3PO',
48 'Huyang',
49 'Droideka',
50 'Probe Droid'
51];
52
53@Radium
54class DroidList extends React.Component {
55 render() {
56 return (
57 <ul style={{padding: 0}}>
58 {droids.map((droid, index, arr) =>
59 <li key={index} style={{
60 borderColor: 'black',
61 borderRadius: index === 0 ? '12px 12px 0 0' :
62 index === (arr.length - 1) ? '0 0 12px 12px' : '',
63 borderStyle: 'solid',
64 borderWidth: index === (arr.length - 1) ? '1px' : '1px 1px 0 1px',
65 cursor: 'pointer',
66 listStyle: 'none',
67 padding: 12,
68 ':hover': {
69 background: '#eee'
70 }
71 }}>
72 {droid}
73 </li>
74 )}
75 </ul>
76 );
77 }
78}
79```
80
81Instead of `:before` and `:after`, add extra elements when rendering your HTML.
82
83## How can I use `ReactCSSTransitionGroup` without any classes?
84
85Try out the experimental [`ReactStyleTransitionGroup`](https://github.com/adambbecker/react-style-transition-group) instead.
86
87## How can I use Radium with jsbin?
88
89To get the latest version, drop this into the HTML:
90
91```html
92<script src="https://npmcdn.com/radium/dist/radium.js"></script>
93```
94
95We also recommend changing the JavaScript language to ES6/Babel.
96
97## Can I use my favourite CSS/LESS/SASS syntax?
98
99Yes, with the help of the [react-styling](https://github.com/halt-hammerzeit/react-styling) module, which requires [template strings](https://babeljs.io/docs/learn-es2015/#template-strings). Using react-styling you can write your styles in any syntax you like (curly braces or tabs, CSS, LESS/SASS, anything will do).
100
101The example from the main Readme (using regular CSS syntax)
102
103```jsx
104<Button kind="primary">Radium Button</Button>
105```
106
107```jsx
108import styler from 'react-styling/flat'
109
110@Radium
111class Button extends React.Component {
112 static propTypes = {
113 kind: React.PropTypes.oneOf(['primary', 'warning']).isRequired
114 }
115
116 render() {
117 return (
118 <button style={style[`button_${this.props.kind}`]}>
119 {this.props.children}
120 </button>
121 )
122 }
123}
124
125const style = styler`
126 .button {
127 color: #fff;
128
129 :hover {
130 background: ${color('#0074d9').lighten(0.2).hexString()};
131 }
132
133 &.primary {
134 background: #0074D9;
135 }
136
137 &.warning {
138 background: #FF4136;
139 }
140 }
141`
142```
143
144You can find a more advanced example in the [react-styling readme](https://github.com/halt-hammerzeit/react-styling#radium).
145
146## Can I use Radium with Bootstrap?
147
148See issue [#323](https://github.com/FormidableLabs/radium/issues/323) for discussion.
149
150## Why doesn't Radium work on react-router's Link, or react-bootstrap's Button, or SomeOtherComponent?
151
152Radium doesn't mess with the `style` prop of non-DOM elements. This includes thin wrappers like `react-router`'s `Link` component. We can't assume that a custom component will use `style` the same way DOM elements do. For instance, it could be a string enum to select a specific style. In order for resolving `style` on a custom element to work, that element needs to actually pass that `style` prop to the DOM element underneath, in addition to passing down all the event handlers (`onMouseEnter`, etc). Since Radium has no control over the implementation of other components, resolving styles on them is not safe.
153
154A workaround is to wrap your custom component in Radium, even if you do not have the source, like this:
155
156```jsx
157var Link = require('react-router').Link;
158Link = Radium(Link);
159```
160
161Huge thanks to [@mairh](https://github.com/mairh) for coming up with this idea in issue [#324](https://github.com/FormidableLabs/radium/issues/324).
162
163We are also exploring adding a mechanism to bypass Radium's check, see issue [#258](https://github.com/FormidableLabs/radium/issues/258).
164
165## How can I get rid of `userAgent` warnings in tests?
166
167You might see warnings like this when testing React components that use Radium:
168
169```
170Radium: userAgent should be supplied for server-side rendering. See https://github.com/FormidableLabs/radium/tree/master
171/docs/api#radium for more information.
172Either the global navigator was undefined or an invalid userAgent was provided. Using a valid userAgent? Please let us k
173now and create an issue at https://github.com/rofrischmann/inline-style-prefixer/issues
174```
175
176This isn't an issue if you run your tests in a browser-like environment such as jsdom or PhantomJS, but if you just run them in Node, there will be no userAgent defined. In your test setup, you can define one:
177
178```jsx
179global.navigator = {userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2454.85 Safari/537.36'};
180```
181
182Make sure it is a real user agent that `inline-style-prefixer` recognizes, or you'll still get the second error. The above UA is [Chrome 49 from the `inline-style-prefixer` tests](https://github.com/rofrischmann/inline-style-prefixer/blob/master/test/prefixer-test.js).
183
184## Why do React warnings have the wrong component name?
185
186You may see the name "Constructor" instead of your component name, for example: "Warning: Failed propType: Invalid prop `onClick` of type `function` supplied to `Constructor`, expected `string`." or "Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `Constructor`."
187
188Your transpiler is probably not able to set the `displayName` property of the component correctly, which can happen if you wrap `React.createClass` immediately with `Radium`, e.g. `var Button = Radium(React.createClass({ ... }));`. Instead, wrap your component afterward, ex. `Button = Radium(Button);`, or when exporting, ex. `module.exports = Radium(Button);`, or set `displayName` manually.