Popover source

Inspect and copy the reviewed source included with this Free component.

Free source · 5 files

no_js.rs

// SPDX-FileCopyrightText: 2026 Proa Labs, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Declarative popovers: HTML owns disclosure and CSS owns placement.

use super::{PopoverAlign, PopoverSide, POPOVER_ANATOMY, POPOVER_RECIPE};
use crate::OverlayIds;
use proa::html_sync;
use proa::{WebContext, WebRenderSync, WriteBuf, WriteError};

/// A non-modal popover that works without the Proa JavaScript runtime.
///
/// Import `runtime/proa-free.css` (or `proa.css`) into your Tailwind entry
/// point and scan these component sources. Browsers with CSS anchor positioning
/// place the panel beside its trigger and flip it to avoid viewport overflow.
/// Without anchor positioning, native popovers open centered in the viewport.
/// Without the HTML Popover API, content remains visible inline.
///
/// `id` must be unique in the document. `trigger` supplies the button's contents,
/// not a nested button. The browser owns open state, Escape, light dismissal,
/// and the trigger's implicit expanded state. This is not a modal dialog.
pub struct NoJsPopover<Trigger = (), Content = ()> {
    pub id: &'static str,
    pub side: PopoverSide,
    pub align: PopoverAlign,
    pub trigger_class: Option<&'static str>,
    pub content_class: Option<&'static str>,
    pub trigger: Option<Trigger>,
    pub content: Option<Content>,
}

impl<Trigger, Content> NoJsPopover<Trigger, Content> {
    pub const fn new(id: &'static str) -> Self {
        Self {
            id,
            side: PopoverSide::Bottom,
            align: PopoverAlign::Center,
            trigger_class: None,
            content_class: None,
            trigger: None,
            content: None,
        }
    }
}

impl<
        B: WriteBuf,
        L: proa::DataLoader,
        Trigger: WebRenderSync<L, B>,
        Content: WebRenderSync<L, B>,
    > WebRenderSync<L, B> for NoJsPopover<Trigger, Content>
{
    fn render(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
        let ids = OverlayIds::new(Some(self.id));
        html_sync! {
            <div
                id={self.id}
                data-no-js-popover-root
                data-scope={POPOVER_ANATOMY.scope()}
                data-part="root"
                data-mode="no-js"
                class={POPOVER_RECIPE.root}
            >
                <button
                    id={ids.trigger()}
                    type="button"
                    data-no-js-popover-trigger
                    data-scope={POPOVER_ANATOMY.scope()}
                    data-part="trigger"
                    popovertarget={ids.content()}
                    aria-controls={ids.content()}
                    aria-haspopup="dialog"
                    class={proa::text!("{} {}", POPOVER_RECIPE.trigger, self.trigger_class)}
                >
                    {self.trigger}
                </button>
                <div
                    id={ids.content()}
                    popover="auto"
                    data-no-js-popover-content
                    data-scope={POPOVER_ANATOMY.scope()}
                    data-part="content"
                    data-side={self.side.as_str()}
                    data-align={self.align.as_str()}
                    role="dialog"
                    tabindex="-1"
                    aria-labelledby={ids.trigger()}
                    class={proa::text!("{} {}", super::recipe::NO_JS_POPOVER_CONTENT, self.content_class)}
                >
                    {self.content}
                </div>
            </div>
        }
        .render(cx)
    }
}

/// Declarative close button. `popover_id` is the owning [`NoJsPopover::id`].
pub struct NoJsPopoverClose<C = ()> {
    pub popover_id: &'static str,
    pub class: Option<&'static str>,
    pub children: Option<C>,
}

impl<B: WriteBuf, L: proa::DataLoader, C: WebRenderSync<L, B>> WebRenderSync<L, B>
    for NoJsPopoverClose<C>
{
    fn render(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
        let ids = OverlayIds::new(Some(self.popover_id));
        html_sync! {
            <button
                type="button"
                data-no-js-popover-close
                data-scope={POPOVER_ANATOMY.scope()}
                data-part="close"
                popovertarget={ids.content()}
                popovertargetaction="hide"
                class={self.class}
            >
                {self.children}
            </button>
        }
        .render(cx)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn no_js_contract_is_complete_before_javascript() {
        let mut out = Vec::new();
        let mut cx = WebContext::with_buffer(&mut out);
        NoJsPopover {
            side: PopoverSide::Top,
            align: PopoverAlign::End,
            trigger_class: Some("custom-trigger"),
            content_class: Some("custom-panel"),
            trigger: Some("Settings"),
            content: Some(NoJsPopoverClose {
                popover_id: "settings",
                class: Some("custom-close"),
                children: Some("Done"),
            }),
            ..NoJsPopover::new("settings")
        }
        .render(&mut cx)
        .unwrap();
        let html = String::from_utf8(out).unwrap();
        assert!(html.contains(r#"id="settings--trigger""#));
        assert!(html.contains(r#"id="settings--content""#));
        assert_eq!(
            html.matches(r#"popovertarget="settings--content""#).count(),
            2
        );
        assert!(html.contains(r#"aria-controls="settings--content""#));
        assert!(html.contains(r#"aria-labelledby="settings--trigger""#));
        assert!(html.contains(r#"popover="auto""#));
        assert!(html.contains(r#"popovertargetaction="hide""#));
        assert!(html.contains(r#"data-side="top""#));
        assert!(html.contains(r#"data-align="end""#));
        for class in ["custom-trigger", "custom-panel", "custom-close"] {
            assert!(html.contains(class));
        }
        // Native disclosure must never be captured by delegated JS or frozen
        // behind SSR-only hidden/expanded state.
        for legacy in [
            "data-popover-root",
            "data-popover-trigger",
            "data-popover-content",
            "data-state=",
            "aria-hidden=",
            "aria-expanded=",
            "style=",
        ] {
            assert!(!html.contains(legacy), "native markup contains {legacy}");
        }
    }
}

Copy this source with the Free Proa UI components it imports; those component files remain available from their own source pages.

Licensed under MIT OR Apache-2.0. Third-party notices remain attached to the installed payload.

Open Markdown
FreePopover

Anchored floating content.

Install with the Proa CLI
proa ui add popover

Installs the reviewed component source, dependencies, shared support files, and required legal notices.

CLI and registry setup
recipe.rs
// SPDX-FileCopyrightText: 2026 Proa Labs, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Popover recipe — semantic root placement and named slot classes.

use crate::theme::rounded;
use crate::tw_join;
use proa::shared::attr_value::AttrValue;
use proa::{DataLoader, WebContext, WriteBuf, WriteError};

/// Popover placement options.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, proa::ProaVariant)]
#[variant(name = "side")]
pub enum PopoverSide {
    Top,
    #[default]
    Bottom,
    Left,
    Right,
}

impl PopoverSide {

Search

Type at least 2 characters

Customize

Make it yours

Palette

Radius

Overview
Components
Settings

Build your own

Themed Proa UI surface

Preview
Components52
RecipesTyped
customer-portal