Current section
Files
Jump to
Current section
Files
guides/limitations.md
# Limitations
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.**
```elixir
<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:
```elixir
<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`:
```elixir
<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:
```elixir
<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 `@name` of `<img src=x onerror=alert(1)>`
is inserted as an `img` element with an `onerror` handler.
- **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:
```elixir
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>")
end
```
`text` 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:
```elixir
<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
Only a GeoJSON source's `data` and an image source's `url`/`coordinates` update
in place. Any other source key (cluster settings, tile URLs, zoom ranges) is
structural: 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`.
## 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](installation.html#serving-the-bundles).
## 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.
**Not all of the standard library is ported.** Hologram's
[client runtime reference](https://hologram.page/reference/client-runtime)
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](architecture.html).
- **No unmount hook.** Disposal is inferred from the container going stale.
Hologram's roadmap mentions a `setup` (pre-init) hook; an unmount counterpart
would let `HoloMap` drop 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.
## Not implemented yet
- Nested `HoloMap.Popup` inside a `HoloMap.Marker` slot; use the marker's
`popup_html` prop
- Server-side rendering of a static map image