avatarCkmobile

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

  • The author believes that adding an overlay to a popover enhances the user experience by focusing attention on the popover content.
  • The use of HeadlessUI is advocated for its accessibility and customizability, allowing developers to create their own UI components without styling constraints.
  • The article suggests that readers might appreciate similar content available on PlainEnglish.io, indicating a positive opinion of the platform.
  • By providing a membership link, the author expresses support for the Medium Partner Program and the value of compensating writers.
  • The recommendation of ZAI.chat implies that the author finds it to be a valuable and economical tool compared to other AI services like ChatGPT Plus.

HeadlessUI: Popover

Part 4: Adding overlay

Photo by Linus Mimietz on Unsplash

To add an overlay when open the popover, we can add component.

<Popover.Button
className="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>

full code

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.Button
className="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>
<button
onClick={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 MyPopover

If 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!

Follow us: YouTube, Medium, Udemy, Linkedin, Twitter, Instagram, Gumroad, Quora, Telegram

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.

CSS
React
JavaScript
Web Development
Software Development
Recommended from ReadMedium