Skip to content

Commit 0ce750f

Browse files
OnOff hello world
0 parents  commit 0ce750f

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.DS_Store

index.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
import React from 'react';
3+
4+
const grey = '#CCC';
5+
const offBackground = {
6+
top: 0,
7+
boxShadow: `inset 0 0 0px 1px ${grey}`,
8+
height: 0.6,
9+
width: 1
10+
}
11+
12+
const onBackground = {
13+
top: 0,
14+
height: 0.6
15+
}
16+
17+
const buttonStyle = {
18+
height: 0.6,
19+
width: 0.6,
20+
top: 0
21+
}
22+
23+
const commonStyles = {
24+
position: 'absolute',
25+
borderRadius: 0.3,
26+
transition: '0.1s ease-in-out'
27+
}
28+
29+
export default class OnOff extends React.Component {
30+
constructor(props) {
31+
super(props);
32+
this.state = {
33+
on: !!props.on, // false if not set
34+
width: props.with || 100,
35+
buttonColor: props.buttonColor || '#FFFFFF',
36+
passiveColor: props.passiveColor || '#FFFFFF',
37+
activeColor: props.activeColor || '#13BF11'
38+
};
39+
this.handleChange = this.handleChange.bind(this);
40+
this.onChange = props.onChange || (() => {});
41+
}
42+
43+
handleChange() {
44+
this.setState(
45+
{on: !this.state.on},
46+
() => this.onChange(this.state.on) // callback to parent Component
47+
);
48+
}
49+
50+
setStyles(obj){
51+
const styles = {
52+
...obj,
53+
...commonStyles
54+
}
55+
for (var key in styles) {
56+
if (typeof styles[key] == 'number') styles[key] = this.state.width * styles[key] + 'px';
57+
}
58+
return styles;
59+
}
60+
61+
render() {
62+
const on = this.state.on;
63+
64+
const active = this.setStyles({
65+
...onBackground,
66+
width: on ? 1 : 0.6,
67+
background: this.state.activeColor
68+
});
69+
const passive = this.setStyles({
70+
...offBackground,
71+
width: on ? 0.6 : 1,
72+
left: on ? 0.4 : 0,
73+
background: this.state.passiveColor
74+
});
75+
const button = this.setStyles({
76+
...buttonStyle,
77+
background: this.state.buttonColor,
78+
left: on ? 0.4 : 0,
79+
boxShadow: `inset 0 0 0 1px ${on ? this.state.activeColor : grey}, 0 2px 4px ${grey}`
80+
});
81+
82+
return (
83+
<div onClick={this.handleChange} style={{position: 'relative', height: 0.6 * this.state.width}}>
84+
<div style={active}/>
85+
<div style={passive} />
86+
<div style={button}/>
87+
</div>
88+
)
89+
}
90+
}

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "react-onoff-switch",
3+
"version": "0.0.1",
4+
"description": "React on-off switch component",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"react",
11+
"onoff",
12+
"switch"
13+
],
14+
"author": "Sergio Crisostomo",
15+
"license": "ISC",
16+
"devDependencies": {
17+
"babel-core": "^6.18.2",
18+
"babel-loader": "^6.2.7",
19+
"babel-preset-es2015": "^6.18.0",
20+
"babel-preset-react": "^6.16.0",
21+
"babel-preset-stage-0": "^6.16.0",
22+
"react-dom": "^15.3.2",
23+
"webpack": "^1.13.3"
24+
},
25+
"dependencies": {
26+
"react": "^15.3.2"
27+
}
28+
}

webpack.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var path = require('path');
2+
var webpack = require('webpack');
3+
4+
module.exports = {
5+
entry: './demo.js',
6+
output: { path: __dirname, filename: 'compiled.js' },
7+
module: {
8+
loaders: [
9+
{
10+
test: /.jsx?$/,
11+
loader: 'babel-loader',
12+
exclude: /node_modules/,
13+
query: {
14+
presets: ['react', 'es2015', 'stage-0']
15+
}
16+
}
17+
]
18+
}
19+
}

0 commit comments

Comments
 (0)