HoloMap.Popup (HoloMap v0.2.0)

Copy Markdown View Source

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

PropTypeNotes
idstringRequired. Identity across re-renders
lng_lat{lng, lat}Required. Where the popup points
htmlstringContent as an HTML string, inserted as markup, instead of slot content
textstringContent as plain text, never read as markup. Wins over html and slot content
close_buttonbooleanShow MapLibre's own close button
close_on_clickbooleanClose when the map is clicked
close_on_movebooleanClose when the camera moves
anchorstringWhich side of the popup touches the coordinate
offsetnumber, [x, y] or mapPixel offset from the anchor
class_namestringExtra classes on the popup element
max_widthstringCSS max-width, default "240px"
focus_after_openbooleanMove keyboard focus into the popup
subpixel_positioningbooleanPosition on fractional pixels

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, so html="&lt;b&gt;" 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 @name of <img src=x onerror=alert(1)> is inserted as an img element with an onerror handler.
  • An expression, html={@html}, arrives as it is, so an escaped string such as html={"&lt;b&gt;"} 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.

Text, HTML or slot content

A popup shows one of three things:

  • text, through MapLibre's setText, which puts it in a text node. Nothing in it is markup: text={@name} with a @name of <img src=x onerror=alert(1)> shows those characters. Written as a literal, text arrives 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's setHTML, which inserts it as markup.
  • Slot content, through setHTML too: 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, so href={@url} with a javascript: 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)
end

If 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 or focus_after_open replaces an open popup with one built with the new options: the popup stays open throughout. 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.

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

__is_hologram_component__()

@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

__props__()

@spec __props__() :: [{atom(), atom(), keyword()}]

Returns the list of property definitions for the compiled component.