In this section we will see how to use Headless UI in NextJS. Before we start we need to install & setup tailwind css with nextjs then we will install headless ui. You can read below article or https://tailwindcss.com/docs/guides/nextjs .
Install Tailwind CSS In NextJS 13
Install Headless UI in NextJS
To get started, install Headless UI via npm:
npm install @headlessui/react
import { useState } from 'react'
import { Switch } from '@headlessui/react'
export default function ToggleExample() {
const [enabled, setEnabled] = useState(false)
return (
<div className="flex items-center justify-center py-16">
<Switch
checked={enabled}
onChange={setEnabled}
className={`${enabled ? 'bg-teal-900' : 'bg-teal-700'}
relative inline-flex h-[38px] w-[74px] shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75`}
>
<span className="sr-only">Use setting</span>
<span
aria-hidden="true"
className={`${enabled ? 'translate-x-9' : 'translate-x-0'}
pointer-events-none inline-block h-[34px] w-[34px] transform rounded-full bg-white shadow-lg ring-0 transition duration-200 ease-in-out`}
/>
</Switch>
</div>
)
}
Run the server
npm run dev