This is a basic example, with no customization. It's what you get out of the box, with the default color scheme and no text indicator. The state in this example is set to active, so the component is turned on.
const reactToggleComponent = require('react-toggle-component');
const domElement = document.getElementById('react-toggle-component');
const isActive = true;
const httpRequests = {
onState: {
url: 'https://randomuser.me/api',
postData: {}
},
offState: {
url: 'https://randomuser.me/api',
postData: {}
}
};
function callback(data) {
console.log(data);
};
reactToggleComponent.init(domElement, isActive, callback, httpRequests);
This is a basic example, with no customization. It's what you get out of the box, with the default color scheme and no text indicator. The state in this example is set to inactive, so the component is turned off.
const reactToggleComponent = require('react-toggle-component');
const domElement = document.getElementById('react-toggle-component');
const isActive = false;
const httpRequests = {
onState: {
url: 'https://randomuser.me/api',
postData: {}
},
offState: {
url: 'https://randomuser.me/api',
postData: {}
}
};
function callback(data) {
console.log(data);
};
reactToggleComponent.init(domElement, isActive, callback, httpRequests);
Here you see how to pass an options argument containing an object for the on and off state text label. Try to toggle the button to see both labels.
const reactToggleComponent = require('react-toggle-component');
const domElement = document.getElementById('react-toggle-component');
const isActive = false;
const httpRequests = {
onState: {
url: 'https://randomuser.me/api',
postData: {}
},
offState: {
url: 'https://randomuser.me/api',
postData: {}
}
};
const opts = {
onState: {
text: 'On'
},
offState: {
text: 'Off'
}
};
function callback(data) {
console.log(data);
};
reactToggleComponent.init(domElement, isActive, callback, httpRequests, opts);
Here is an example of how you can change the styles of each individual component of the toggle button. You can alter the text, slider and button individually, for both on and off states.
const reactToggleComponent = require('react-toggle-component');
const domElement = document.getElementById('react-toggle-component');
const isActive = false;
const httpRequests = {
onState: {
url: 'https://randomuser.me/api',
postData: {}
},
offState: {
url: 'https://randomuser.me/api',
postData: {}
}
};
const opts = {
common: {
styles: {
textComponent: {
color: '#fff',
fontSize: '14'
},
sliderComponent: {
backgroundColor: '#fff'
}
}
},
onState: {
text: 'In',
styles: {
textComponent: {
left: '30'
},
toggleComponent: {
backgroundColor: '#d2112e'
}
}
},
offState: {
text: 'Out',
styles: {
textComponent: {
right: '20'
},
toggleComponent: {
backgroundColor: '#000'
}
}
}
};
function callback(data) {
console.log(data);
};
reactToggleComponent.init(domElement, isActive, callback, httpRequests, opts);
This example shows a different style, color scheme, and faster, snappier transition between the on and off states.
const reactToggleComponent = require('react-toggle-component');
const domElement = document.getElementById('react-toggle-component');
const isActive = false;
const httpRequests = {
onState: {
url: 'https://randomuser.me/api',
postData: {}
},
offState: {
url: 'https://randomuser.me/api',
postData: {}
}
};
const opts = {
common: {
styles: {
toggleComponent: {
width: '80'
},
sliderComponent: {
height: '30',
top: '4',
msTransition: 'transform 0.1s ease',
MozTransition: 'transform 0.1s ease',
WebkitTransition: '-webkit-transform 0.1s ease',
width: '30'
}
}
},
onState: {
styles: {
toggleComponent: {
backgroundColor: '#38677e'
},
sliderComponent: {
left: '5',
msTransform: 'translate(40px, 0px)',
MozTransform: 'translate(40px, 0px)',
WebkitTransform: 'translate(40px, 0px)'
}
}
},
offState: {
styles: {
toggleComponent: {
backgroundColor: '#bbb'
},
sliderComponent: {
left: '3'
}
}
}
};
function callback(data) {
console.log(data);
};
reactToggleComponent.init(domElement, isActive, callback, httpRequests, opts);