HeadlessUI: Popover
Part 4: Adding overlay
To add an overlay when open the popover, we can add
Summary
The webpage content describes how to add an overlay to a popover component in a web application using HeadlessUI, a set of completely unstyled, fully accessible UI components for React, Vue, and Svelte.
Abstract
The article provides a step-by-step guide on enhancing the user interface of a web application by implementing a popover overlay using HeadlessUI's Popover component. It explains the process of adding the <Popover.overlay> element, which serves as a modal backdrop when the popover is open. The overlay is styled with a fixed position, full coverage of the viewport, a black background, and an opacity of 30%. The article includes code snippets demonstrating the implementation of the popover button, overlay, and panel, along with the use of state management to handle the popover's open and close states. It also shows how to fetch and display data from an API within the popover. The author encourages readers to support writers by subscribing to Medium and offers a membership link. Additionally, the article promotes the PlainEnglish.io website and its newsletter, and it recommends an AI service called ZAI.chat as a cost-effective alternative to ChatGPT Plus.
Opinions
To add an overlay when open the popover, we can add
<Popover.ButtonclassName="group inline-flex items-center rounded-md bg-orange-700 px-3 py-2 text-base font-medium text-white hover:text-opacity-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75"><Popover.Overlay className="fixed inset-0 bg-black opacity-30" /><span>Solutions</span>
import React, { useState } from 'react'import { Popover } from '@headlessui/react'import { ChevronRightIcon } from '@heroicons/react/solid'import MyLink from './MyLink'const MyPopover = () => {const [todos, setTodos] = useState([])return (<div className="fixed top-16 w-full max-w-sm px-4"><Popover className="relative">{({ open, close }) => (<><Popover.ButtonclassName="group inline-flex items-center rounded-md bg-orange-700 px-3 py-2 text-base font-medium text-white hover:text-opacity-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75"><Popover.Overlay className="fixed inset-0 bg-black opacity-30" /><span>Solutions</span><ChevronRightIcon
className={`${open ? 'rotate-90 transform' : ''}ml-2 h-5 w-5 text-orange-300 transition duration-150 ease-in-out group-hover:text-opacity-80`}
/></Popover.Button><buttononClick={async () => {const data = await fetch('https://jsonplaceholder.typicode.com/todos')const todos = await data.json()setTodos(todos)close()}}
>Read and accept</button><Popover.Panel className="absolute z-10 mt-3"><div className="overflow-hidden rounded-lg shadow-lg ring-1 ring-black ring-opacity-5"><div className="relative grid gap-8 bg-white p-7 "><Popover.Button as={MyLink} href="/insights">Insights</Popover.Button><a href="/analytics">Analytics</a><a href="/engagement">Engagement</a><a href="/security">Security</a><a href="/integrations">Integrations</a></div></div></Popover.Panel></>)}
</Popover>{todos.length > 0 && todos.map(todo => <li key={todo.id}>{todo.title}</li>)}</div>)
}
export default MyPopoverIf you liked this story, you might also like a Medium membership. It’s only $5 a month (a price of a cup of coffee!) but it will give you unlimited access to stories while supporting your favorite writers. If you sign up using this link, I’ll earn a small commission. Thanks!
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.
Digvijay MahapatraHow this HTML API is replacing hundreds of lines of UI Component code
Minko GechevIn the past two years we doubled down on our investment in developer experience and performance — in every single release we’ve been…
Arnold GunterI don’t wanna say anything but just see yourself how mind-blowing this is…
Tracking your fitness progress can be incredibly motivating, and having an app that helps you log workouts and calories burned is a great…
Tari IbabaSay goodbye to try-catch