Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Created November 14, 2022 12:11
Show Gist options
  • Save IgorDePaula/d6c572271be9d40181251a1722c488d1 to your computer and use it in GitHub Desktop.
Save IgorDePaula/d6c572271be9d40181251a1722c488d1 to your computer and use it in GitHub Desktop.
import React, {useEffect, useState} from "react";
import {Switch} from '@headlessui/react'
export type SwitcherType = {
label?: string
state: boolean,
getState?: (param: boolean | undefined) => void
}
const Switcher: React.FunctionComponent<SwitcherType> = (props: SwitcherType) => {
const {label, state = false, getState = null} = props
const [enabled, setEnabled] = useState<boolean | undefined>(state)
useEffect(() => {
getState && getState(enabled)
}, [enabled])
return (
<Switch.Group>
<div className="flex items-center">
{label && <Switch.Label className="mr-4">{label}</Switch.Label>}
<Switch
checked={enabled}
onChange={(event: any) => {
setEnabled(event)
}}
className={`${
enabled ? 'bg-blue-600' : 'bg-gray-200'
} relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2`}
>
<span
className={`${
enabled ? 'translate-x-6' : 'translate-x-1'
} inline-block h-4 w-4 transform rounded-full bg-white transition-transform`}
/>
</Switch>
</div>
</Switch.Group>
)
}
export default Switcher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment