Popover

A flexible and customizable UI element that displays additional content when triggered by user interaction. It is useful for providing contextual information, tooltips, or action menus without cluttering the interface.

About

The Popover component allows developers to display a small floating panel next to a specified trigger element. It supports various customization options, including positioning, animation speed, rounded corners, shadow effects, and trigger events (click or hover). The popover can contain a title and body content, both of which can be styled individually using provided class props.

By default, the popover appears above (top) the trigger element with a configurable gap. It can be dismissed automatically when clicking outside or manually by interacting with the trigger. The component is designed to be lightweight, accessible, and easy to integrate with different UI designs.

Example

The Popover component can be easily added to your project by wrapping the content you want to display inside it. The trigger prop connects the Popover component to a specific element by matching its id. When the user interacts with this element, the popover appears next to it. This ensures precise control over where and when the popover is displayed.

Svelte
<script>
  import { Popover } from "theui-svelte";
</script>

<Popover trigger="my-popover"> 
  Popover - am amazing component for the displaying additional content.
</Popover>

<Button id="my-popover">Test popover</Button>

Popover Title

The Popover component allows adding a title to provide a heading or short description before the main content. You can add a title in two ways:

Using the title Prop

You can use the title prop to define the title of the Popover. Just pass the text of the title in the prop.

Svelte
<Popover trigger="my-popover" title="Popover Title">
  Popover - am amazing component for the displaying additional content.
</Popover>

Using Snippet

Alternatively, for custom title you can use the title snippet.

Svelte
<Popover trigger="my-popover">
  {#snippet title()}
    Popover <span class="text-brand-primary-500">Title</span>
  {/snippet}
  ...
</Popover>

Position

The Popover component allows you to control where it appears relative to the trigger element using the position prop. By default, the popover appears at the top of the trigger, but you can set it to different positions like top, bottom, left, or right. Additionally, you can use more specific placements such as top-start, top-end, bottom-start, bottom-end, left-start, left-end, right-start, and right-end to fine-tune alignment.

Svelte
<Popover trigger="my-popover" position="right">
  Popover - am amazing component for the<br>displaying additional content.
</Popover>

Trigger Event

The Popover component supports different interaction types through the triggerEvent prop, allowing you to control how the popover is activated. By default, the popover opens when the trigger element is clicked, but you can also set it to open on hover.

Available Options

  • click (Default) – The popover appears when the user clicks the trigger element and closes when clicking outside.
  • hover – The popover appears when the user hovers over the trigger element and disappears when moving away.
Svelte
<Popover trigger="my-popover5" triggerEvent="hover">
  Popover - am amazing component for the<br>displaying additional content.
</Popover>

Popover Gap

The gap prop controls the spacing between the Popover and its trigger element. It accepts a number value, representing the offset in pixels. By default, the gap is set to 8, ensuring a small but noticeable space between the popover and the trigger.

Svelte
<Popover trigger="my-popover" gap={16}>
  This popover has a larger gap from the trigger.
</Popover>

Close on Clicking Popover

The closeOnClick prop determines whether the Popover should close when clicking inside it. By default, this is set to true, meaning the popover will close when the user interacts with its content.

Svelte
<Popover trigger="my-popover7" closeOnClick={true}>
  Clicking inside this popover won't close it.
</Popover>

Animation Speed

The animate prop controls the speed of the animation when the Popover appears or disappears. It accepts the following values none, slower, slow, normal, fast and faster - which can be used to define how quickly the popover fades in and out.

Svelte
<Popover trigger="my-popover" animate="slower">
  This popover has a larger gap from the trigger.
</Popover>

Rounded Corners

The rounded prop controls the corner rounding of the Popover. You can adjust it using predefined values to give the popover a smooth, rounded appearance. Available values are sm, md, lg, xl, full and none.

Svelte
<Popover trigger="my-popover" rounded="none">
  This popover has a larger gap from the trigger.
</Popover>

Shadow

The shadow prop adds a shadow effect to the Popover, helping it stand out against the background. Available values are xs, sm, md, lg, xl, 2xl, inner, none.

Svelte
<Popover trigger="my-popover" shadow="xl">
  This popover has a larger gap from the trigger.
</Popover>

Customization

The Popover component offers multiple ways to customize its appearance using the titleClasses, bodyClasses, and class attributes. These allow you to apply custom styles to the title, body and the container of the popover.

  • titleClasses prop lets you add custom styles to the Popover title, such as font size, color, or spacing.
  • bodyClasses prop allows you to style the Popover body, including padding, background, border, and typography.
  • class attribute allows you to styles the Popover container, modifying its overall look, size, or layout.
Svelte
<Popover
  trigger="my-popover"
  title="POPOVER TITLE"
  class="bg-blue-100 text-center"
  titleClasses="font-bold text-blue-500"
  bodyClasses="flex flex-col gap-4"
>
  This popover has a styled title and body.
  <p>
    <Button class="bg-blue-500 hover:bg-blue-600" size="sm">Button</Button>
  </p>
</Popover>

Accessibility

The Popover component is designed with accessibility in mind, ensuring it is usable for keyboard and screen reader users. It follows best practices for focus management, keyboard navigation, and ARIA attributes.

  1. Keyboard Navigation
    • Users can open and close the popover using the Space or Enter keys when triggerEvent="click".
    • Pressing Escape closes the popover when it is open when triggerEvent="click".
    • The popovers triggered by hover are also accessible via focus for keyboard users.
  2. ARIA Attributes

    To enhance screen reader compatibility, the popover automatically includes essential ARIA attributes:

    • The aria-controls links the trigger element to the popover container.
    • The aria-haspopup on the trigger element to indicate a popover is available.
    • The aria-expanded attribute will be managed by the component itself for proper accessibility.
  3. Focus Management
    • When opened, focus remains on the trigger or shifts inside the popover for better navigation.
    • When closed, focus returns to the trigger element to maintain a logical navigation flow.
    • While using triggerEvent="hover", keyboard users to focus on the trigger and navigate inside the popover.

Configuration