Things that do not work, and why. Most trace back to Hologram being a young framework with hooks a map library would like and does not have.
Slot content is cloned
HoloMap.Marker and HoloMap.Popup accept slot content, and MapLibre receives a
clone of it rather than the node Hologram rendered.
MapLibre moves a marker's element into its own overlay container. Handing it the real node would take that node out from under the virtual DOM and corrupt the next patch, so the markup is cloned instead.
The consequence: Hologram event bindings inside the slot do not fire.
<HoloMap.Marker id="office" lng_lat={{-70.66, 19.45}}>
<button $click={:clicked}>Details</button> <!-- will not fire -->
</HoloMap.Marker>Use the marker's own on_click prop instead:
<HoloMap.Marker id="office" lng_lat={{-70.66, 19.45}} on_click={:clicked}>
<div class="chip">Head office</div>
</HoloMap.Marker>The markup still renders and restyles normally on re-render. Only the bindings are inert. For genuinely interactive content, put it outside the map and drive it from state.
Untrusted content in popups
A popup shows one of three things, and only one of them keeps markup out whatever the value holds:
| Content | Reaches MapLibre through | A value the application does not control |
|---|---|---|
HoloMap.Popup's text, HoloMap.Marker's popup_text | setText, which puts it in a text node | Shows as it is, markup and all, as characters |
HoloMap.Popup's html, HoloMap.Marker's popup_html | setHTML, which inserts it as markup | Must be escaped before it is interpolated |
HoloMap.Popup's slot content | setHTML, with the markup Hologram rendered in the slot | Safe where Hologram rendered it as text, such as {@name} |
For a value that only needs showing, such as a place name, a review or a
feature property, reach for text:
<HoloMap.Popup id="review" lng_lat={@coords} text={@review.body} />
<HoloMap.Marker id="shop" lng_lat={@coords} popup_text={@shop.name} />A @review.body of <img src=x onerror=alert(1)> shows those characters,
and no img element is created.
text wins over html, and html over slot content. A popup given both
text and html, or a marker given both popup_text and popup_html, shows
the text and logs a console warning naming the two props: showing the html
instead would silently drop the choice of text, the one that keeps markup out
of the page. Each updates the popup in place when it changes.
html and popup_html are inserted as markup
HoloMap.Popup's html and HoloMap.Marker's popup_html reach MapLibre's
setHTML, which inserts them as markup. Hologram escapes none of the three ways
to write them:
<HoloMap.Popup id="p" lng_lat={@coords} html="<b>hi</b>" />
<HoloMap.Popup id="p" lng_lat={@coords} html="<b>{@name}</b>" />
<HoloMap.Popup id="p" lng_lat={@coords} html={@html} />- A plain literal arrives with its character references decoded, as a
browser decodes them, so
html="<b>"is the markup<b>. - A literal with an inserted value arrives as the literal text and the value
joined, neither of them escaped. A
@nameof<img src=x onerror=alert(1)>is inserted as animgelement with anonerrorhandler. - An expression arrives as it is. An escaped string passed this way, such as
html={"<b>"}, stays escaped and shows the characters<b>.
The application must therefore escape any value it does not control, such as
user input or a field from an external service, 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, where the value arrives:
def init(_params, component, _server) do
place = MyApp.Places.get!(1)
name = Plug.HTML.html_escape(place.name)
put_state(component, coords: place.coords, html: "<b>#{name}</b>")
endtext and popup_text arrive the same three ways, literal text decoded and
inserted values joined unescaped, and whatever arrives shows as text.
Hologram 0.11 escaped both the literal text and the inserted value, so there
html="<b>hi</b>" rendered the characters <b>hi</b>.
Slot content
Slot content goes through setHTML as well: the runtime reads the markup
Hologram rendered in the slot and hands MapLibre that string. The tags written
in the template are markup, which is what a slot is for, and a value Hologram
rendered as text arrives escaped, so it shows as text:
<HoloMap.Popup id="p" lng_lat={@coords}><b>{@name}</b></HoloMap.Popup>A value in an attribute is as safe as it is anywhere else on the page: an
href={@url} holding a javascript: URL runs when the link is clicked, in a
popup or out of one. HoloMap.Marker's slot content is cloned the same way
into the marker's element.
Bare boolean attributes
In a HOLO template a bare attribute is an empty string, not true. HoloMap
coerces this for every boolean prop it knows about, so <... cluster /> works.
A boolean prop it has missed would reach MapLibre as "" and fail style
validation with boolean expected, string found. Pass ={true} explicitly if
you hit one, and it is worth reporting.
Controls are replaced, not updated
Changing any prop on a control removes and re-adds it, in its place in its corner. That is invisible for the built-ins, but it means a control is not the place to put a rapidly changing value.
The definition key includes the control's position, because MapLibre cannot
move a control between corners in place.
Structural source changes tear down layers
A GeoJSON source's data, cluster, cluster_radius and cluster_max_zoom,
a vector, raster or raster-dem source's tiles and url, and an image
source's url/coordinates update in place. Any other source key (zoom
ranges, bounds, tile_size, cluster_min_points, cluster_properties) is
structural, and so is taking tiles, url, cluster_radius or
cluster_max_zoom away, which MapLibre's setters cannot do. MapLibre refuses
to remove a source that still has layers attached, so the reconciler detaches
the layers it owns, rebuilds the source, and re-adds them where they were in
the stack.
It works, and it is not free. Put frequently-changing data in data.
An update in place keeps what MapLibre does with it. A new url keeps the
bounds and zoom range the previous TileJSON gave where the new one leaves them
out, where a rebuilt source would start from MapLibre's defaults. After a
cluster change, MapLibre's getStyle still reports the cluster options the
source was added with.
MapLibre is bundled once per page
Hologram builds one JavaScript bundle per page, and a page that uses HoloMap
carries its own copy of MapLibre, around 1.1 MB minified and 0.28 MB gzipped.
Five map pages are five distinct downloads.
This is Hologram's bundling model, not something the library can change. It is also not as bad as it sounds: a visitor only downloads the pages they actually visit, and Hologram serves each one compressed and cached for good, since every bundle is named by its content hash. See Installation.
No HoloMap styling of its own
The library ships no CSS. MapLibre's own stylesheet is a required install step, and everything else (sizing, borders, marker chrome) is yours. That is deliberate: a component library that imposed a look would be fought rather than used.
One map per cid
The registry is keyed by cid, which is also the DOM container's id. Two
HoloMap.Map components with the same cid on one page is undefined behaviour
though Hologram itself already requires cids to be unique.
Hologram gaps this library works around
These are upstream, not HoloMap's, and are worth knowing if you write your own
interop. This section was last checked against Hologram 0.12.11.
Float.round/2 rejects integers. MapLibre reports a whole-number coordinate
as an integer, and Float.round/2 only accepts floats. Widen with * 1.0
first. This is standard Elixir behaviour, but it bites more often here because
the values come from JavaScript, where there is no distinction.
The interop reads an atom as the global it names. An atom handed to
Hologram.JS.call/2 becomes the JavaScript global of that name when there is
one, or a binding the module imports with js_import, and a string
otherwise. So :top, :name and :status arrive as window.top,
window.name and window.status. HoloMap.Runtime sends every call and
request as one JSON string encoded with HoloMap.JSON, in which an atom is
its name; do the same in your own interop with values you do not control.
Not all of the standard library is ported. Hologram's client runtime reference tracks what is available. Anything in a template expression or an action runs in the browser and is subject to it.
Hologram 0.11.0's client-side port of :maps.find/2 returned the bare value
instead of {:ok, value}, which broke Map.fetch/2 in the browser. Hologram
0.12 returns {:ok, value}, checked in a browser on 0.12.11, and HoloMap no
longer works around it.
Things Hologram does not offer yet
As of Hologram 0.12.11, a component has init, action, command and
template, and nothing else.
- No "props changed" hook. This is the whole reason for the DOM definition protocol described in Architecture.
- No unmount hook. Disposal is inferred from the container going stale.
Hologram's roadmap mentions a
setup(pre-init) hook; an unmount counterpart would letHoloMapdrop a good deal of machinery. - Interop only runs in actions. A map cannot be constructed during SSR, so the first paint is always a blank container.
A sprite that changes while it loads
MapLibre loads a sprite in the background, and nothing cancels the load when
the sprite is removed (_loadSprite in its src/style/style.ts registers
whatever arrives). A HoloMap.Sprite taken out of the template before its
icons have arrived leaves them on the map once they do, until a style is
built from scratch, and one whose url changes in that time can end up with
the icons of the URL whose load finished last. A sprite that stays as it is
long enough for its icons to arrive, about one round trip, is not affected.
A light MapLibre refuses reaches only the console
MapLibre validates the light HoloMap.Light sets, and leaves the light as
it was for a value it refuses, such as an anchor that is neither "map"
nor "viewport". Its light has no evented parent (_load in its
src/style/style.ts), so the error reaches no listener of the map:
MapLibre logs it to the browser console, and on_error does not hear it.
Not implemented yet
- Nested
HoloMap.Popupinside aHoloMap.Markerslot; use the marker'spopup_htmlprop - Server-side rendering of a static map image