Skip to content

Commit 6bb6b0e

Browse files
1.0.0! and some code cleanup
1 parent b34fa3c commit 6bb6b0e

File tree

3 files changed

+62
-44
lines changed

3 files changed

+62
-44
lines changed

demo/compiled.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20867,6 +20867,18 @@
2086720867

2086820868
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
2086920869

20870+
var componentDefaults = {
20871+
width: 100,
20872+
buttonColor: '#FFFFFF',
20873+
passiveColor: '#FFFFFF',
20874+
activeColor: '#13BF11'
20875+
};
20876+
20877+
var stopEvent = function stopEvent(e) {
20878+
e.preventDefault();
20879+
e.stopPropagation();
20880+
};
20881+
2087020882
var OnOff = function (_React$Component) {
2087120883
_inherits(OnOff, _React$Component);
2087220884

@@ -20876,15 +20888,11 @@
2087620888
var _this = _possibleConstructorReturn(this, (OnOff.__proto__ || Object.getPrototypeOf(OnOff)).call(this, props));
2087720889

2087820890
var active = !!props.initialValue;
20879-
_this.state = {
20891+
_this.state = _extends({}, componentDefaults, props, {
2088020892
on: active, // false if not set
20881-
width: props.with || 100,
2088220893
activeColorWidth: active ? 1 : 0.6,
20883-
buttonPosition: active ? 0.4 : 0,
20884-
buttonColor: props.buttonColor || '#FFFFFF',
20885-
passiveColor: props.passiveColor || '#FFFFFF',
20886-
activeColor: props.activeColor || '#13BF11'
20887-
};
20894+
buttonPosition: active ? 0.4 : 0
20895+
});
2088820896

2088920897
_this.handleChange = _this.handleChange.bind(_this);
2089020898
_this.onChange = props.onChange || function () {};
@@ -20912,29 +20920,29 @@
2091220920
}, {
2091320921
key: 'getPointerCoords',
2091420922
value: function getPointerCoords(e) {
20915-
return {
20916-
x: e.pageX || e.touches[0].pageX,
20917-
y: e.pageY || e.touches[0].pageY
20918-
};
20923+
return e.pageX || e.touches[0].pageX;
2091920924
}
2092020925
}, {
2092120926
key: 'onPointerDown',
2092220927
value: function onPointerDown(e) {
20923-
this.touchDown = this.getPointerCoords(e.nativeEvent).x - parseInt(e.target.style.left, 10);
20928+
stopEvent(e);
20929+
this.touchDown = this.getPointerCoords(e.nativeEvent) - parseInt(e.target.style.left, 10);
2092420930
window.addEventListener('ontouchend' in window ? 'touchend' : 'mouseup', this.onDragEnd);
20925-
if ('onmouseout' in window) e.target.addEventListener('mouseout', this.onDragEnd);
2092620931
}
2092720932
}, {
2092820933
key: 'onDrag',
2092920934
value: function onDrag(e) {
20930-
e.preventDefault();
2093120935
if (!this.touchDown) return;else this.dragged = true;
20936+
stopEvent(e);
20937+
20938+
// 0.4 and 0.6 are related to proportions where 1 is the width
20939+
// 0.4 is the most left point of the buttonPosition
20940+
// 0.6 is the most right point, so the active color fills the whole background behind the button
2093220941

20933-
var positionNow = this.getPointerCoords(e.nativeEvent).x;
20942+
var positionNow = this.getPointerCoords(e.nativeEvent);
2093420943
var diff = (positionNow - this.touchDown) / this.state.width;
20935-
// o.4 and 0.6 are related to proportions where 1 is the width
20936-
var max = 0.4;
20937-
if (diff < 0) diff = 0;else if (diff > max) diff = max;
20944+
var maxDragDistance = 0.4;
20945+
if (diff < 0) diff = 0;else if (diff > maxDragDistance) diff = maxDragDistance;
2093820946
var pos = 0.6 + diff;
2093920947

2094020948
this.setState({
@@ -20945,8 +20953,10 @@
2094520953
}, {
2094620954
key: 'onDragEnd',
2094720955
value: function onDragEnd(e) {
20956+
stopEvent(e);
20957+
if (!this.touchDown) return;
20958+
2094820959
window.removeEventListener('ontouchend' in window ? 'touchend' : 'mouseup', this.onDragEnd);
20949-
if ('onmouseout' in window) e.target.removeEventListener('mouseout', this.onDragEnd);
2095020960
var newState = this.dragged ? this.state.buttonPosition > 0.2 : !this.state.on;
2095120961
this.handleChange(newState);
2095220962
this.touchDown = this.dragged = null;
@@ -20973,17 +20983,15 @@
2097320983

2097420984
return _react2.default.createElement(
2097520985
'div',
20976-
{
20977-
/*onClick={this.handleChange}*/
20978-
style: { position: 'relative', height: 0.6 * this.state.width }
20979-
},
20986+
{ style: { position: 'relative', height: 0.6 * this.state.width } },
2098020987
_react2.default.createElement('div', { style: passive }),
2098120988
_react2.default.createElement('div', { style: active }),
2098220989
_react2.default.createElement('div', {
2098320990
onMouseMove: this.onDrag,
2098420991
onTouchMove: this.onDrag,
2098520992
onTouchStart: this.onPointerDown,
2098620993
onMouseDown: this.onPointerDown,
20994+
onMouseOut: this.onDragEnd,
2098720995
style: button })
2098820996
);
2098920997
}

index.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22
import React from 'react';
33
import {grey, offBackground, onBackground, buttonStyle, setStyles} from './styles.js';
44

5+
const componentDefaults = {
6+
width: 100,
7+
buttonColor: '#FFFFFF',
8+
passiveColor: '#FFFFFF',
9+
activeColor: '#13BF11'
10+
}
11+
12+
const stopEvent = e => {
13+
e.preventDefault();
14+
e.stopPropagation();
15+
}
16+
517
export default class OnOff extends React.Component {
618
constructor(props) {
719
super(props);
820
const active = !!props.initialValue;
921
this.state = {
22+
...componentDefaults,
23+
...props,
1024
on: active, // false if not set
11-
width: props.with || 100,
1225
activeColorWidth: active ? 1 : 0.6,
1326
buttonPosition: active ? 0.4 : 0,
14-
buttonColor: props.buttonColor || '#FFFFFF',
15-
passiveColor: props.passiveColor || '#FFFFFF',
16-
activeColor: props.activeColor || '#13BF11'
1727
};
1828

1929
this.handleChange = this.handleChange.bind(this);
@@ -35,29 +45,29 @@ export default class OnOff extends React.Component {
3545
}
3646

3747
getPointerCoords(e){
38-
return {
39-
x: e.pageX || e.touches[0].pageX,
40-
y: e.pageY || e.touches[0].pageY
41-
};
48+
return e.pageX || e.touches[0].pageX;
4249
}
4350

4451
onPointerDown(e){
45-
this.touchDown = this.getPointerCoords(e.nativeEvent).x - parseInt(e.target.style.left, 10);
52+
stopEvent(e);
53+
this.touchDown = this.getPointerCoords(e.nativeEvent) - parseInt(e.target.style.left, 10);
4654
window.addEventListener('ontouchend' in window ? 'touchend' : 'mouseup', this.onDragEnd);
47-
if ('onmouseout' in window) e.target.addEventListener('mouseout', this.onDragEnd);
4855
}
4956

5057
onDrag(e){
51-
e.preventDefault();
5258
if (!this.touchDown) return;
5359
else this.dragged = true;
60+
stopEvent(e);
61+
62+
// 0.4 and 0.6 are related to proportions where 1 is the width
63+
// 0.4 is the most left point of the buttonPosition
64+
// 0.6 is the most right point, so the active color fills the whole background behind the button
5465

55-
const positionNow = this.getPointerCoords(e.nativeEvent).x;
66+
const positionNow = this.getPointerCoords(e.nativeEvent);
5667
let diff = (positionNow - this.touchDown) / this.state.width;
57-
// o.4 and 0.6 are related to proportions where 1 is the width
58-
const max = 0.4;
68+
const maxDragDistance = 0.4;
5969
if (diff < 0) diff = 0;
60-
else if (diff > max) diff = max;
70+
else if (diff > maxDragDistance) diff = maxDragDistance;
6171
const pos = 0.6 + diff;
6272

6373
this.setState({
@@ -67,8 +77,10 @@ export default class OnOff extends React.Component {
6777
}
6878

6979
onDragEnd(e){
80+
stopEvent(e);
81+
if (!this.touchDown) return;
82+
7083
window.removeEventListener('ontouchend' in window ? 'touchend' : 'mouseup', this.onDragEnd);
71-
if ('onmouseout' in window) e.target.removeEventListener('mouseout', this.onDragEnd);
7284
const newState = this.dragged ? this.state.buttonPosition > 0.2 : !this.state.on;
7385
this.handleChange(newState);
7486
this.touchDown = this.dragged = null;
@@ -96,17 +108,15 @@ export default class OnOff extends React.Component {
96108
}, this.state.width);
97109

98110
return (
99-
<div
100-
/*onClick={this.handleChange}*/
101-
style={{position: 'relative', height: 0.6 * this.state.width}}
102-
>
111+
<div style={{position: 'relative', height: 0.6 * this.state.width}}>
103112
<div style={passive}/>
104113
<div style={active}/>
105114
<div
106115
onMouseMove={this.onDrag}
107116
onTouchMove={this.onDrag}
108117
onTouchStart={this.onPointerDown}
109118
onMouseDown={this.onPointerDown}
119+
onMouseOut={this.onDragEnd}
110120
style={button}/
111121
>
112122
</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-onoff-switch",
3-
"version": "0.0.4",
3+
"version": "1.0.0",
44
"description": "React on-off switch component",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)