FormsPopover
Searchastronomic.dev# Popover
To keep a set of choices one click away, like the home types or a price range in a filter bar: a button that opens a small panel over the page.
Interactive: inline script.
## Usage
```astro
import Popover from "@astronomic-dev/ui/components/Popover.astro";
```
## Examples
### Several choices
Real checkboxes in the panel. Check two: the trigger says "2 types", and the panel stays open.
```astro
---
import Popover from "@astronomic-dev/ui/components/Popover.astro";
import PopoverGroup from "@astronomic-dev/ui/components/PopoverGroup.astro";
import PopoverOption from "@astronomic-dev/ui/components/PopoverOption.astro";
import { fields } from "../_data/vega.ts";
const type = fields.find((field) => field.key === "type")!;
---
<div class="grid max-w-xs">
<Popover label={type.label} summary="choices" anyText="Any type" countText="{n} types">
<PopoverGroup legend={type.label} legendHidden>
{type.options!.map((option) => <PopoverOption type="checkbox" name={type.key} value={option.value}>{option.label}</PopoverOption>)}
</PopoverGroup>
</Popover>
</div>
```
### A range
Two radio lists, Min and Max. Pick a Min: every Max at or below it is ruled out, and the trigger says what you picked.
```astro
---
import Popover from "@astronomic-dev/ui/components/Popover.astro";
import PopoverGroup from "@astronomic-dev/ui/components/PopoverGroup.astro";
import PopoverOption from "@astronomic-dev/ui/components/PopoverOption.astro";
import { rangeOptions } from "@astronomic-dev/ui/lib/filters.ts";
import { fields } from "../_data/vega.ts";
const price = fields.find((field) => field.key === "price")!;
const { min, max } = rangeOptions(price, "buy");
---
<div class="grid max-w-xs">
<Popover label={price.label} summary="range" anyText="Any price" cols={2}>
<PopoverGroup legend="Min" end="min">
{min.map((option) => <PopoverOption type="radio" name="price-min" value={option.value} checked={option.value === ""}>{option.label}</PopoverOption>)}
</PopoverGroup>
<PopoverGroup legend="Max" end="max">
{max!.map((option) => <PopoverOption type="radio" name="price-max" value={option.value} checked={option.value === ""}>{option.label}</PopoverOption>)}
</PopoverGroup>
</Popover>
</div>
```
### Any content
The panel holds what you put in it. With no summary, the trigger's words stay as written.
```astro
---
import Popover from "@astronomic-dev/ui/components/Popover.astro";
---
<div class="grid max-w-xs">
<Popover label="Open house" triggerText="Saturday, November 7">
<div class="grid max-w-xs gap-1 p-2.5">
<p class="m-0 text-body font-medium text-pretty">11 am to 1 pm</p>
<p class="m-0 text-small text-pretty text-muted">4127 Tennyson St, Denver. Street parking on 41st Ave.</p>
<a class="mt-1 text-small font-medium text-fg underline" href="#directions">Get directions</a>
</div>
</Popover>
</div>
```
## Props
### Popover
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | None | Required. What the panel is about ("Home type"): the panel's name, and the first words of the trigger's name. |
| `triggerText` | `string` | `anyText` | The trigger's words as the page first draws them. Pass the right words when the page opens with something picked. |
| `summary` | `"choices" \| "range"` | None | What the script keeps the trigger's words in step with: the checked options, or the two end groups of a range (it also rules out a Max at or below the Min). Unset: the words stay as you wrote them. |
| `anyText` | `string` | `"Any"` | The words for nothing picked. |
| `countText` | `string` | `"{n} selected"` | The words for two or more choices; {n} is the count. One choice shows its own words. |
| `rangeText` | `string` | `"{min} to {max}"` | The words for both ends of a range; {min} and {max} are the options' words. |
| `fromText` | `string` | `"{min}+"` | The words for a minimum only. |
| `upToText` | `string` | `"Up to {max}"` | The words for a maximum only. |
| `cols` | `1 \| 2` | `1` | The panel's columns: 2 sets two PopoverGroups side by side, with a line between. |
| `disabled` | `boolean` | `false` | The trigger can't be used, and the panel's controls do not submit. |
| `id` | `string` | `a page-unique id` | The trigger's id. The panel's id adds "-panel". |
| `class` | `string` | None | More classes for the outer box, merged with cn(), so yours win on conflicts. |
| `triggerClass` | `string` | None | More classes for the trigger. |
| `panelClass` | `string` | None | More classes for the panel. |
| Slot | Description |
| --- | --- |
| `default` | The panel's content: PopoverGroups of PopoverOptions, or anything else. |
| `trigger` | Replaces the trigger's words, so a field can show its label above its value. The words are then the trigger's whole name, so write the label in them, and mark the part that changes with data-popover-text. It is content inside the button, so it must not hold a link or a button. |
Any other attribute goes on the outer box. In a range, syncPopover(root) from lib/popover.ts brings the trigger and the limits in step after your own script sets the controls. setPopoverWords(root, { label, anyText }) changes the trigger's name, the panel's name and the words for nothing picked, for a mode switch that makes Price into Rent.
## Accessibility
| Key | Action |
| --- | --- |
| Enter or Space | On the trigger: opens and closes the panel |
| Tab and Shift+Tab | Move through the panel in page order, then on past it. Safari's default settings leave checkboxes out of Tab: there the arrows reach them |
| Space | On a checkbox: checks and unchecks it |
| Up and Down arrows | On a checkbox: move to the next or previous enabled checkbox in the panel, wrapping at the ends, and check nothing. In a two-column panel Down runs through one column, then the next |
| Up and Down arrows | In a radio list: move and pick, as native radios do |
| Enter | On a checkbox or a radio in the panel: picks it, and does not submit the form |
| Escape | Closes the panel and returns focus to the trigger |
- It is a native popover: the browser opens it, closes it on Escape and on a click outside, and keeps it above everything. Without JavaScript all of that still works, and the controls still submit; only the trigger's words stay as the server drew them.
- The trigger is a <button> named by label and its words ("Home type: 2 types"), with aria-haspopup, aria-controls and aria-expanded. Do not point a Field's for at it: a label replaces the button's own words, and the value is lost.
- The panel is a non-modal dialog named by label. Focus moves to the picked control, else to the first one, when it opens; a panel with nothing to focus takes focus itself. The page behind stays usable.
- When the panel closes, focus goes back to the trigger, unless the press that closed it landed on another control.
- A panel that does not fit flips, then scrolls inside. On a phone it is full width with a small inset. Rows are at least 44px on a touch screen.
- Under reduced motion or the theme's calm setting it opens at once.
Popover
FreeTo keep a set of choices one click away, like the home types or a price range in a filter bar: a button that opens a small panel over the page.
- File
- Popover.astro
- Group
- Forms
- Kind
- Interactive: inline script
- Examples
- 3
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
Several choices
Real checkboxes in the panel. Check two: the trigger says "2 types", and the panel stays open.
This preview did not load.
main.astro
examples/popover/main.astro
---
import Popover from "@astronomic-dev/ui/components/Popover.astro";
import PopoverGroup from "@astronomic-dev/ui/components/PopoverGroup.astro";
import PopoverOption from "@astronomic-dev/ui/components/PopoverOption.astro";
import { fields } from "../_data/vega.ts";
const type = fields.find((field) => field.key === "type")!;
---
<div class="grid max-w-xs">
<Popover label={type.label} summary="choices" anyText="Any type" countText="{n} types">
<PopoverGroup legend={type.label} legendHidden>
{type.options!.map((option) => <PopoverOption type="checkbox" name={type.key} value={option.value}>{option.label}</PopoverOption>)}
</PopoverGroup>
</Popover>
</div>A range
Two radio lists, Min and Max. Pick a Min: every Max at or below it is ruled out, and the trigger says what you picked.
This preview did not load.
range.astro
examples/popover/range.astro
---
import Popover from "@astronomic-dev/ui/components/Popover.astro";
import PopoverGroup from "@astronomic-dev/ui/components/PopoverGroup.astro";
import PopoverOption from "@astronomic-dev/ui/components/PopoverOption.astro";
import { rangeOptions } from "@astronomic-dev/ui/lib/filters.ts";
import { fields } from "../_data/vega.ts";
const price = fields.find((field) => field.key === "price")!;
const { min, max } = rangeOptions(price, "buy");
---
<div class="grid max-w-xs">
<Popover label={price.label} summary="range" anyText="Any price" cols={2}>
<PopoverGroup legend="Min" end="min">
{min.map((option) => <PopoverOption type="radio" name="price-min" value={option.value} checked={option.value === ""}>{option.label}</PopoverOption>)}
</PopoverGroup>
<PopoverGroup legend="Max" end="max">
{max!.map((option) => <PopoverOption type="radio" name="price-max" value={option.value} checked={option.value === ""}>{option.label}</PopoverOption>)}
</PopoverGroup>
</Popover>
</div>Any content
The panel holds what you put in it. With no summary, the trigger's words stay as written.
This preview did not load.
content.astro
examples/popover/content.astro
---
import Popover from "@astronomic-dev/ui/components/Popover.astro";
---
<div class="grid max-w-xs">
<Popover label="Open house" triggerText="Saturday, November 7">
<div class="grid max-w-xs gap-1 p-2.5">
<p class="m-0 text-body font-medium text-pretty">11 am to 1 pm</p>
<p class="m-0 text-small text-pretty text-muted">4127 Tennyson St, Denver. Street parking on 41st Ave.</p>
<a class="mt-1 text-small font-medium text-fg underline" href="#directions">Get directions</a>
</div>
</Popover>
</div>Minimal · Light