Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/popover.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Popover } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What a `Popover`'s `view` closure receives. */
    export type PopoverParts = Component.Headless.Parts<typeof Popover>
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    const sizes = {
      sm: "w-56",
      default: "w-72",
      auto: "w-auto"
    } as const
    
    export type PopoverSize = keyof typeof sizes
    
    /** The box the kit places next to the trigger. The popover's `sideOffset` is its padding. */
    export const PopoverPositioner = <Msg>(h: Html.H<Msg>, popover: PopoverParts, options: ClassOption = {}) =>
      popover.positioner(h.div.class(cn("z-50 outline-none", options.class)))
    
    export const PopoverPopup = <Msg>(
      h: Html.H<Msg>,
      popover: PopoverParts,
      options: ClassOption & { readonly size?: PopoverSize | undefined } = {}
    ) =>
      popover.popup(
        h.div.class(cn(
          "relative flex origin-[var(--transform-origin)] flex-col gap-2 rounded-lg border bg-popover p-4 text-[13px]",
          "text-foreground shadow-float outline-none",
          "h-[var(--popup-height,auto)] transition-[opacity,scale,height] duration-150 ease-out",
          "data-[ending-style]:scale-95 data-[ending-style]:opacity-0",
          "data-[starting-style]:scale-95 data-[starting-style]:opacity-0",
          "data-[instant]:duration-0",
          sizes[options.size ?? "default"],
          options.class
        ))
      )
    
    /** A small diamond on the popup's edge. The kit points it at the trigger: leave room with `sideOffset: 12`. */
    export const PopoverArrow = <Msg>(h: Html.H<Msg>, popover: PopoverParts, options: ClassOption = {}) =>
      popover.arrow(
        h.div.class(cn(
          "size-[10px] rotate-45 bg-popover",
          "data-[side=bottom]:-top-[5.5px] data-[side=bottom]:border-t data-[side=bottom]:border-l",
          "data-[side=top]:-bottom-[5.5px] data-[side=top]:border-r data-[side=top]:border-b",
          "data-[side=right]:-left-[5.5px] data-[side=right]:border-b data-[side=right]:border-l",
          "data-[side=left]:-right-[5.5px] data-[side=left]:border-t data-[side=left]:border-r",
          options.class
        ))
      )
    
    /**
     * Around content that changes with the trigger, in a popover with several triggers:
     * the content of the last trigger fades out as the new one fades in.
     */
    export const PopoverViewport = <Msg>(h: Html.H<Msg>, popover: PopoverParts, options: ClassOption = {}) =>
      popover.viewport(
        h.div.class(cn(
          "relative",
          "[&>[data-current]]:flex [&>[data-current]]:flex-col [&>[data-current]]:gap-2",
          "[&>[data-previous]]:flex [&>[data-previous]]:flex-col [&>[data-previous]]:gap-2",
          "[&>[data-current]]:transition-opacity [&>[data-current]]:duration-200",
          "[&>[data-previous]]:transition-opacity [&>[data-previous]]:duration-200",
          "[&>[data-current][data-starting-style]]:opacity-0 [&>[data-previous][data-ending-style]]:opacity-0",
          options.class
        ))
      )
    
    export const PopoverTitle = <Msg>(h: Html.H<Msg>, popover: PopoverParts, options: ClassOption = {}) =>
      popover.title(h.h2.class(cn("m-0 border-0 p-0 text-sm leading-snug font-semibold", options.class)))
    
    export const PopoverDescription = <Msg>(h: Html.H<Msg>, popover: PopoverParts, options: ClassOption = {}) =>
      popover.description(h.p.class(cn("m-0 text-[13px] leading-normal text-muted-foreground", options.class)))
    
    export const PopoverFooter = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.div.class(cn("mt-1 flex flex-wrap items-center justify-end gap-2", options.class))
    
  3. Import it

    import { PopoverPositioner, PopoverPopup, PopoverArrow, PopoverViewport, PopoverTitle, PopoverDescription, PopoverFooter } from "#ui/popover"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Popover } from "@effect/ui-kit"
import { Schema } from "effect"
import { buttonVariants } from "#ui/button"
import { PopoverDescription, PopoverFooter, PopoverPopup, PopoverPositioner, PopoverTitle } from "#ui/popover"

const PopoverBasicMessage = UiSchema.TaggedEnum({
  RequestedOpenPopover: {},
  RequestedClosePopover: {}
})

export const PopoverBasic = Html.component({
  name: "PopoverBasic",
  model: Schema.Struct({ open: Schema.Boolean }),
  message: PopoverBasicMessage
}).make({
  init: () => ({ open: false }),
  update: {
    RequestedOpenPopover: ($) => $.evolve({ open: true }),
    RequestedClosePopover: ($) => $.evolve({ open: false })
  },
  view: (h, model) =>
    Popover(h, {
      props: { open: model.open, sideOffset: 8 },
      onEmit: {
        RequestedOpen: () => PopoverBasicMessage.RequestedOpenPopover(),
        RequestedClose: () => PopoverBasicMessage.RequestedClosePopover(),
        Settled: () => undefined
      },
      view: (popover) =>
        h.div(
          popover.trigger({ element: h.button.class(buttonVariants({ variant: "outline" })) }).children(
            "Notifications"
          ),
          popover.portal(h.dialog)?.children(
            PopoverPositioner(h, popover).children(
              PopoverPopup(h, popover).children(
                PopoverTitle(h, popover).children("You're all caught up"),
                PopoverDescription(h, popover).children(
                  "New mentions and review requests show up here as they arrive."
                ),
                PopoverFooter(h).children(
                  popover.close(h.button.class(buttonVariants({ variant: "outline", size: "sm" })))
                    .children("Close")
                )
              )
            )
          )
        )
    })
})

Examples

With a form

Fields in a popover write straight to the Model.

100% × 25px, at most 300px

Sides and alignment

The side and align props place the popup. It flips when it doesn't fit.

With an arrow

The kit points the arrow at the trigger. The styles give it a size and a side.

Opened by hover

openOnHover on the trigger, with its own delay and closeDelay.

Several triggers

One popover moves between three triggers, and its viewport cross-fades the content.

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
triggerA <button> that opens the popover. Takes { element, openOnHover, delay, closeDelay, disabled }.
portalThe <dialog> in the top layer, around the parts below.
backdropA <div> that covers the page. Optional.
positionerA <div> placed next to the trigger. sideOffset is its padding.
popupA <div> with the popover's content.
viewportA <div> around content that changes with the trigger. Optional.
arrowA <div> that points at the trigger. Optional.
titleA heading that labels the popover.
descriptionA <p> that describes the popover.
closeA <button> that asks the popover to close.

API reference

Props

PropTypeDescription
openbooleanWhether the popover shows. Required: the consumer owns it.
modalboolean | "trap-focus"false by default.
triggerIdstring | nullThe id of the trigger the popover is open for and placed against.
side"top" | "bottom" | "left" | "right" | "inline-start" | "inline-end"The side of the trigger the popup shows on.
align"start" | "center" | "end"How the popup lines up with the trigger along that side. "center" by default.
sideOffsetnumberThe gap between the trigger and the popup, in pixels.
alignOffsetnumberPixels the popup moves along the side.
collisionPaddingnumberPixels kept from the viewport's edges, 5 by default.
positionMethod"absolute" | "fixed""absolute" by default, which scrolls with the page.
keepMountedbooleanThe portal stays in the page, hidden, while the popover is closed.

Events

EventDescription
RequestedOpenCarries the trigger's id and the reason: trigger-press or trigger-hover. Also emitted while open, when another trigger asks.
RequestedCloseCarries the reason: a Dialog's reasons, or trigger-hover.
SettledThe transitions of an open or a close finished.

Data attributes

AttributeDescription
data-popup-openOn the trigger while the popover is open for it.
data-pressedOn the trigger while the popover is open for it by a press.
data-open / data-closedOn the backdrop, positioner, popup and arrow.
data-starting-style / data-ending-styleOn the backdrop and popup, to transition from and to.
data-side / data-alignOn the positioner, popup and arrow: where the popup ended up, after it flipped or shifted to fit.
data-instantOn the popup: a change that should skip its transition.
data-uncenteredOn the arrow while it can't point at the trigger's middle.
--transform-originOn the positioner: the point on the trigger the popup grows from.
--anchor-width / --available-heightOn the positioner: the trigger's size and the room left, for a width or max-height.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.