popup
StableA popup with text or HTML content, placed at a coordinate.
Set assigns
anchor
open
Run commands
- @place
- colosseum
- @anchor
- auto
- @open
- true
- @last_event
- —
- @likes
- 0
- @note
- —
<div id="popup-example">
<.example_frame error={@map_error} error_id="example-popup-map-error">
<%!-- The map centres on the chosen place, so the popup above it is in view. --%>
<.map
id="example-popup-map"
center={@place.lng_lat}
zoom={15}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The popup sits next to <.map> and names it with map_id. Where it is,
whether it is open and its anchor are assigns; its content is the
slot. Every one of them follows its assign after mount. --%>
<.popup
id="example-popup"
map_id="example-popup-map"
lng_lat={@place.lng_lat}
open={@open}
anchor={@anchor}
events={["popup:opened", "popup:closed"]}
>
<p class="font-bold">{name(@place.id)}</p>
<p class="font-mono">{lng_lat(@place.lng_lat)}</p>
<%!-- The popup shows the slot's own elements, so its ids are unique and
its bindings work: a click, a form and a label's for. --%>
<div class="mt-2 flex items-center gap-2">
<.example_popup_button id="example-popup-like" phx-click="like">
<.icon name="hero-heart-micro" class="size-4" />
{gettext("Like")}
</.example_popup_button>
<span id="example-popup-likes" class="tabular-nums">
{ngettext("1 like", "%{count} likes", @likes)}
</span>
</div>
<.form
for={@note_form}
id="example-popup-note"
phx-change="note"
phx-submit="save_note"
class="mt-2"
>
<div class="flex justify-between text-label">
<label for="example-popup-note-text" class="font-bold">{gettext("Note")}</label>
<span id="example-popup-note-length" class="font-mono tabular-nums">
{String.length(@note_form[:text].value)}/40
</span>
</div>
<div class="mt-1 flex gap-1.5">
<input
type="text"
id="example-popup-note-text"
name={@note_form[:text].name}
value={@note_form[:text].value}
maxlength="40"
autocomplete="off"
class="h-7 min-w-0 flex-1 rounded-ui border border-map-ink/25 bg-transparent px-2"
/>
<.example_popup_button id="example-popup-note-save" type="submit">
{gettext("Save")}
</.example_popup_button>
</div>
</.form>
</.popup>
<div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[1fr_auto]">
<div class="space-y-4">
<%!-- Assigns: a place moves the popup and opens it. --%>
<.example_group id="example-popup-places" label={gettext("Set assigns")}>
<.example_button
:for={place <- places()}
id={"example-popup-place-#{place.id}"}
pressed={@place.id == place.id}
phx-click="place"
phx-value-place={place.id}
>
{name(place.id)}
</.example_button>
</.example_group>
<div class="flex flex-wrap gap-x-8 gap-y-4">
<.example_group id="example-popup-anchor" label="anchor" code>
<.example_button
:for={anchor <- anchors()}
id={"example-popup-anchor-#{anchor}"}
code
pressed={@anchor == anchor}
phx-click="anchor"
phx-value-anchor={anchor}
>
{anchor}
</.example_button>
</.example_group>
<.example_group id="example-popup-toggle" label="open" code>
<.form for={@options} id="example-popup-options" phx-change="options" class="py-1.5">
<.example_checkbox field={@options[:open]} label="open" />
</.form>
</.example_group>
</div>
</div>
<%!-- Commands: JS structs that open and close the popup in the browser,
with no round trip; popup:opened and popup:closed then tell the
server. --%>
<.example_group id="example-popup-commands" label={gettext("Run commands")}>
<.example_button
id="example-popup-cmd-open"
code
phx-click={PopupCmd.open("example-popup", @place.lng_lat)}
>
open
</.example_button>
<.example_button
id="example-popup-cmd-close"
code
phx-click={PopupCmd.close("example-popup")}
>
close
</.example_button>
<.example_button
id="example-popup-cmd-toggle"
code
phx-click={PopupCmd.toggle("example-popup")}
>
toggle
</.example_button>
</.example_group>
</div>
<%!-- popup:opened and popup:closed keep open current. --%>
<.example_readout values={[
place: @place.id,
anchor: @anchor,
open: @open,
last_event: @last_event || "—",
likes: @likes,
note: @note || "—"
]} />
</div>
Generated from the component's definition in the library, so it always matches the version this demo runs.
| Attribute | Type | Default | Description |
|---|---|---|---|
| id required | string | — | Unique identifier for the popup |
| map_id required | string | — | ID of the map to add the popup to |
| lng_lat | list |
nil
|
Popup position as [longitude, latitude]. If not provided, popup will be created but not displayed until positioned
|
| max_width | string |
"240px"
|
Maximum width of the popup. Defaults to "240px"
|
| close_button | boolean |
true
|
Show close button. Defaults to true
|
| close_on_click | boolean |
true
|
Close popup when clicking on the map. Defaults to true
|
| close_on_move | boolean |
false
|
Close popup when moving the map. Defaults to false
|
| anchor | string |
"auto"
|
Popup anchor point. One of: "center", "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right", "auto". Defaults to "auto", which, like nil, lets MapLibre choose the anchor that keeps the popup in view
|
| offset | any |
nil
|
Pixel offset as [x, y] or single number for all sides
|
| class_name | string |
nil
|
CSS class name for the popup container |
| focus_after_open | boolean |
true
|
Whether the popup moves the keyboard focus to its first focusable element (a link, a button, a field; else the close button) as it opens, MapLibre's focusAfterOpen. Defaults to true. false leaves the focus where it is: for a popup the server opens while the visitor may be typing in a field elsewhere on the page, or one a page control opens, whose next Space or Enter should stay with that control
|
| open | boolean |
nil
|
Whether the popup should be open. Defaults to true when lng_lat is provided. A popup without a position stays closed even with open={true}, and the browser console warns; it opens once it gets a position
|
| global attributes | global | — | Any other HTML attribute, passed on to the component's element. |
| events | any |
[]
|
a list of event names, or :all
|
Slots
- inner_block Required.