Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

tailwind css toggle switch react

import { useState, useEffect } from 'react';
import { PropTypes } from 'prop-types';
function Switch(props) {
    const [toggle , setToggle ] = useState(false)
    const toggleClass = 'transform translate-x-12';

    const _handleClick = e => {
        props.cb()
        setToggle(!toggle);
    }

    useEffect(() => {
        setToggle(props.toggled)
    }, [props.toggled])
    return (
        //   Switch Container
        <div
            className="md:w-20 md:h-8 w-12 h-6 flex items-center bg-gray-300 rounded-full p-1 cursor-pointer"
            onClick={() => {
                _handleClick();
            }}
        >
            {/* Switch */}
            <div
                className =  {"bg-white md:w-6 md:h-6 h-5 w-5 rounded-full shadow-md transform " +  (toggle ? null : toggleClass)}
            ></div>
           
            {!toggle && (
                <span className="text-xs -translate-x-5"> {props.offValue}</span>
            )}
            {toggle  && (
                <span className='text-xs translate-x-2'>{props.onValue}</span>
            )}
            
        </div>
    );
}
Switch.PropTypes = {
    cb: PropTypes.func.isRequired,
    toggled: PropTypes.bool.isRequired,
    offValue: PropTypes.string.isRequired,
    onValue: PropTypes.string.isRequired
}
export default Switch;
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript add to html 
Javascript :: how to detect if an video is over js html 
Javascript :: react enzyme 
Javascript :: datatable set row id 
Javascript :: get user input node js console 
Javascript :: js html input limit to 5 words 
Javascript :: formik seterrors 
Javascript :: java script zip function 
Javascript :: convert string to object javascript 
Javascript :: dynamically add/remove rows in html table using javascript 
Javascript :: javascript trigger function when element is in viewport 
Javascript :: check browser 
Javascript :: window scroll up 
Javascript :: how to comment out code in react js 
Javascript :: array destructuring in react 
Javascript :: check url if it has trailing slash 
Javascript :: jquery timer countdown 
Javascript :: date filter 
Javascript :: vue dynamic routes with parameters 
Javascript :: javascript render jsx element x many times 
Javascript :: make a bot send a welcome message discordjs 
Javascript :: multiple if statements js es6 inline 
Javascript :: jquery onclick click 
Javascript :: how to get java model attributes from javascript 
Javascript :: setImmediate() nodejs 
Javascript :: reactstrap dropdown 
Javascript :: javascript perform click 
Javascript :: first letter string uppercase javascript 
Javascript :: Make Floating label TextInput in react native 
Javascript :: DevDependencies and dependencies syntax in Node package.json 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =