A tooltip anchored to a coordinate.
<HoloMap.Popup id="details" lng_lat={@selected_coords}>
<h3>{@selected.name}</h3>
<p>{@selected.address}</p>
</HoloMap.Popup>A popup declared here is open. Closing it means not rendering it, which is ordinary conditional rendering:
{%if @selected}
<HoloMap.Popup id="details" lng_lat={@selected.coords}>
<h3>{@selected.name}</h3>
</HoloMap.Popup>
{/if}That is the whole state model. There is no imperative open/close, because "which popup is open" is application state and belongs in your component.
Props
| Prop | Type | Notes |
|---|---|---|
id | string | Required. Identity across re-renders |
lng_lat | {lng, lat} | Required, unless track_pointer is set. Where the popup points |
track_pointer | boolean | Follow the pointer over the map in place of lng_lat. See Following the pointer |
html | string | Content as an HTML string, inserted as markup, instead of slot content |
text | string | Content as plain text, never read as markup. Wins over html and slot content |
close_button | boolean | Show MapLibre's own close button |
close_on_click | boolean | Close when the map is clicked |
close_on_move | boolean | Close when the camera moves |
anchor | string | Which side of the popup touches the coordinate |
offset | number, [x, y] or map | Pixel offset from the anchor |
class_name | string | Extra classes on the popup element |
max_width | string | CSS max-width, default "240px" |
focus_after_open | boolean | Move keyboard focus into the popup |
subpixel_positioning | boolean | Position on fractional pixels |
padding | map | Pixels to keep between the popup and the map's edges as MapLibre picks the side it opens on: any of :top, :bottom, :left and :right. Applies when anchor is not set |
location_occluded_opacity | number or string | CSS opacity of the popup while its coordinate is behind the globe. Left out, the popup keeps its opacity |
html is inserted as markup
MapLibre inserts html as markup, and Hologram escapes none of the three
ways to write it:
- A plain literal,
html="<b>hi</b>", arrives with its character references decoded, sohtml="<b>"is the markup<b>. - A literal with an inserted value,
html="<b>{@name}</b>", arrives as the text and the value joined, neither escaped. A@nameof<img src=x onerror=alert(1)>is inserted as animgelement with anonerrorhandler. - An expression,
html={@html}, arrives as it is, so an escaped string such ashtml={"<b>"}stays escaped and shows the characters.
The application must escape an untrusted value before it interpolates
it. Plug.HTML.html_escape/1, which comes with Phoenix, does that. Call
it on the server, in init/3 or a command:
name = Plug.HTML.html_escape(place.name)
put_state(component, :html, "<b>#{name}</b>")A value that only needs showing, such as a place name, is safer in
text. See the Limitations guide.
Following the pointer
track_pointer makes the popup follow the pointer over the map, as
MapLibre's trackPointer() does, and lng_lat is then not needed. It suits
a hover readout:
{%if @hovered}
<HoloMap.Popup id="hover" track_pointer close_button={false}
close_on_click={false} focus_after_open={false}>
{@hovered.name}
</HoloMap.Popup>
{/if}MapLibre shows such a popup only while the pointer is over the map, from
its first move there, and hides it on a touch screen, which has no pointer
to follow. It closes the popup on a map click unless close_on_click is
false, and moves keyboard focus into it as it opens unless
focus_after_open is false, which is why both are set above. The popup
keeps following the pointer through changes to its other props. Set back
to false or nil, track_pointer anchors the popup to lng_lat again,
in place, and set again it follows the pointer from its next move.
Text, HTML or slot content
A popup shows one of three things:
text, through MapLibre'ssetText, which puts it in a text node. Nothing in it is markup:text={@name}with a@nameof<img src=x onerror=alert(1)>shows those characters. Written as a literal,textarrives as any literal attribute does in Hologram 0.12, with its character references decoded and any inserted value joined unescaped, and the result shows as text all the same.html, through MapLibre'ssetHTML, which inserts it as markup.- Slot content, through
setHTMLtoo: MapLibre receives the markup Hologram rendered in the slot. The tags written in the template are markup, and a value Hologram rendered as text, such as{@name}, arrives escaped and shows as text. A value in an attribute is as safe as anywhere else on the page, sohref={@url}with ajavascript:URL runs when clicked, inside the popup or out of it.
text wins over html, and html over slot content. A popup given both
text and html shows the text and logs a console warning: showing the
html instead would silently drop the choice of text, the one that keeps
markup out of the page. Each updates in place when it changes.
<HoloMap.Popup id="review" lng_lat={@coords} text={@review.body} />Closing with the built-in button
MapLibre closes a popup on its own for three reasons: the close button, a
click on the map (close_on_click, on by default) and a camera move
(close_on_move). None of them go through your state, so without help the
component that rendered the popup still believes it is open, and clicking the
same thing again changes nothing, re-renders nothing, and the popup never
comes back.
on_close is what closes that gap. Handle it by clearing whatever state
opened the popup, and the declaration matches reality again:
{%if @selected}
<HoloMap.Popup id="details" lng_lat={@selected.coords} on_close={:selection_cleared}>
<h3>{@selected.name}</h3>
</HoloMap.Popup>
{/if}
def action(:selection_cleared, _params, component) do
put_state(component, :selected, nil)
endIf you would rather not handle it, set close_button={false} and
close_on_click={false} so the only way to close the popup is through your
own state.
A changed declaration always re-asserts itself: if the props change while MapLibre has the popup closed, it reopens. Selecting a different feature therefore works whether or not the previous popup was dismissed.
Events
on_open fires when the popup is added to the map: when it first renders,
and when a changed declaration reopens a popup MapLibre had closed.
on_close fires for every close MapLibre performs on its own. Neither fires
when the application stops rendering the popup, since that close already
comes from its own state, nor when a change to anchor, close_button,
close_on_click, close_on_move, focus_after_open or
location_occluded_opacity replaces an open popup with one built with the
new options: the popup stays open throughout. MapLibre has no setter for
any of them. The other props change the popup in place.
Neither carries a payload.
Slot content is cloned
Like HoloMap.Marker, popup content reaches MapLibre as an HTML clone, so
Hologram event bindings inside the slot do not fire. Put interactive
controls outside the map and drive them from state instead. See the
Limitations guide.
Keyboard focus
With focus_after_open, which is on unless the popup turns it off,
MapLibre moves keyboard focus into the popup when it opens: when it first
renders, and when a changed declaration reopens a popup MapLibre had
closed. A popup replaced while open by one built with new options opens
without taking focus, since nothing asked for an open, and its next open
takes focus as focus_after_open says.
Summary
Functions
Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).
Returns the list of property definitions for the compiled component.
Functions
@spec __is_hologram_component__() :: boolean()
Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).
Examples
iex> __is_hologram_component__()
true
Returns the list of property definitions for the compiled component.