Current section
Files
Jump to
Current section
Files
guides/imperative-api.md
# Imperative API
Most of a map belongs in props. Some of it does not.
## When to reach for it
"Fly to the result the user just picked" is an **event**, not a value.
Re-rendering the same centre twice should not re-animate, and animating is the
whole point. That is the category `HoloMap.API` exists for.
| Situation | Use |
| --------- | --- |
| The camera reflects application state | `HoloMap.Map`'s `center` / `zoom` props |
| The camera should animate in response to an event | `fly_to/2`, `ease_to/2` |
| A layer's paint reflects state | `HoloMap.Layer.*`'s `paint` prop |
| A one-off highlight your state does not track | `set_paint_property/4` |
| Hover and selection styling | `set_feature_state/3` |
An imperative change lasts until the reconciler sets the same thing from the
props. A paint or layout property is set again when that property's declared
value changes, a filter when the declared `filter` changes, and the camera when
a declared `center`, `zoom`, `bearing` or `pitch` changes, in which case only
the values that changed are set. A change to any other prop leaves the
imperative value in place, so an imperative `fill-color` survives a new
`fill-opacity` or `filter`. A layer that is added again, because its `type`,
`source` or `source_layer` changed or its source was rebuilt, comes back as
declared, and so does every layer when the style changes. The reconciler
treats rendered props as the source of truth.
## Calling it
Always from an action, always with the map's `cid` first:
```elixir
def action(:show_parcel, params, component) do
HoloMap.API.fly_to("explorer", center: {params.lng, params.lat}, zoom: 17)
put_state(component, :selected, params.id)
end
```
> #### Actions only
>
> These calls compile to no-ops on the server. They work in `action/3` and in
> functions an action reaches; they do nothing in `init/3` or `command/3`.
>
> To move the camera at mount time, chain an action from `init`:
>
> ```elixir
> def init(_params, component, _server) do
> component
> |> put_state(:style, @style_url)
> |> put_action(:frame_data)
> end
> ```
Calls made before the map has booted, or before its style finishes loading, are
queued and replayed in order, so an action scheduled from `init` is not lost,
even when it runs before the map's own boot action. Calls for a map that has not
booted within five seconds are dropped with a console warning. A call for a map
torn down with `HoloMap.Runtime.destroy/1` while its container stays on the page
is dropped at once, with a warning, until the map boots again. So is a call for
a map MapLibre could not build, such as one on a browser without WebGL2, while
its container stays on the page: the warning names MapLibre's error, and the
calls queued for the map are dropped with it as soon as the map fails.
## Camera
```elixir
HoloMap.API.fly_to("explorer", center: {-70.66, 19.45}, zoom: 14, duration: 1_200)
HoloMap.API.ease_to("explorer", bearing: 45, pitch: 60)
HoloMap.API.jump_to("explorer", center: {-70.66, 19.45})
HoloMap.API.fit_bounds("explorer", [-71.0, 19.0, -70.0, 20.0], padding: 48)
HoloMap.API.resize("explorer")
```
`fly_to/2` arcs out and back in, which reads well over long distances.
`ease_to/2` travels in a straight line, which reads better over short ones.
`jump_to/2` does not animate, which is what a `center` prop change does.
`:easing` shapes an animated move's progress over its duration. MapLibre takes
it as a JavaScript function, which Elixir cannot send, so it takes the name of
one of CSS's timing curves, `:linear`, `:ease_in`, `:ease_out` or
`:ease_in_out`, or a curve of your own as `{:cubic_bezier, x1, y1, x2, y2}`,
and the browser runtime builds the function. Anything else raises
`ArgumentError`. `HoloMap.API.fly_to/2` documents each curve.
```elixir
HoloMap.API.ease_to("explorer", bearing: 90, duration: 900, easing: :ease_in_out)
HoloMap.API.fly_to("explorer", center: {-70.66, 19.45}, easing: {:cubic_bezier, 0.3, 0, 0.2, 1})
```
`fit_bounds/3` accepts any shape `HoloMap.Spec.bounds/1` understands: a pair of
corners, or a flat `[west, south, east, north]` list.
`resize/1` is almost never needed. MapLibre observes its own container, so a map
whose container changes size follows along by itself and this call does nothing.
The one case where it is required is a map built with `track_resize={false}`,
which switches that observer off; the demo's camera page has one set up that way
so the difference is visible.
## Layers
```elixir
HoloMap.API.set_filter("explorer", "parcels-fill", ["==", ["get", "zone"], "R1"])
HoloMap.API.set_paint_property("explorer", "parcels-fill", :fill_color, "#ef4444")
HoloMap.API.set_layout_property("explorer", "parcels-fill", :visibility, "none")
```
Property names may be atoms in Elixir style or MapLibre specification names;
both reach MapLibre as the latter.
These duplicate what the `paint`, `layout` and `filter` props already do, and in
almost every case the props are the better choice: a style change, or a
structural source change that adds a layer again, gives the layer its props
back, and loses whatever was set here. Reach here for something genuinely
transient.
## Feature state
Feature state is the mechanism behind hover and selection styling, and it has no
declarative equivalent: it belongs to a *feature*, not to a component.
```elixir
HoloMap.API.set_feature_state("explorer", %{source: "parcels", id: 12}, %{hover: true})
HoloMap.API.remove_feature_state("explorer", %{source: "parcels", id: 12}, "hover")
HoloMap.API.remove_feature_state("explorer", %{source: "parcels", id: 12})
```
Add `:source_layer` for a vector source. Every feature needs an id, which GeoJSON
features often lack. Set `generate_id` on the source, or point `promote_id` at
a property that is already unique. There is a worked example in
[Events](events.html).
Feature state lasts as long as the source does. A style change MapLibre diffs
keeps it on the sources the components declared. One it builds from scratch
clears it, as a structural change to the source does.
## Anything else
`HoloMap.Runtime.call/3` reaches any method on the underlying `maplibregl.Map`:
```elixir
HoloMap.Runtime.call("explorer", "setRenderWorldCopies", [false])
```
It queues against style readiness like everything else, but it does not
normalise its arguments. They are passed to MapLibre as given.
## From JavaScript
A MapLibre plugin (a draw control, a geocoder, a deck.gl overlay) needs the
raw map object, and Hologram's interop cannot hand one to a third-party script.
Every booted map is published on `window`:
```javascript
window.HoloMap.map("explorer") // the maplibregl.Map, or null
window.HoloMap.cids() // every live map's cid
window.HoloMap.destroy("explorer") // release its WebGL context
```
Prefer `HoloMap.API` from Elixir: it queues correctly against the style's
readiness, which a raw handle does not.