MaplibreX
MapLibre GL JS as declarative Phoenix LiveView components. The map is declared in HEEx and driven from the server: assigns change the map, map events arrive in handle_event/3, and the application writes no JavaScript.
This demo runs MaplibreX 0.8.0. It lists all 38 components with their status, and shows each working example next to the code that produces it.
Install
Requires Elixir 1.15 or later and Phoenix LiveView 1.0 or later.
The steps start from an application generated by mix phx.new (Phoenix 1.8). For a path: dependency, deck.gl and the reasons behind each step, see
the installation in the library's README.
-
Add the dependency to mix.exs
mix.exs def deps do [ {:maplibrex, "~> 0.8.0"} ] endThe demo itself runs MaplibreX 0.8.0, the latest release on Hex.
-
Install MapLibre GL JS
MaplibreX does not bundle MapLibre GL JS; your application owns its version. MaplibreX supports maplibre-gl 5 and 6; on version 6, deckgl_layer needs 6.9.1 or later.
Shell npm install --prefix assets maplibre-gl -
Register the hooks
In
assets/js/app.js, add the hooks to the ones the generatedLiveSocketalready has:assets/js/app.js import { MapHooks } from "maplibrex" const liveSocket = new LiveSocket("/live", Socket, { longPollFallbackMs: 2500, params: {_csrf_token: csrfToken}, hooks: {...colocatedHooks, ...MapHooks}, })The bare
"maplibrex"import resolves throughNODE_PATH, which the generated esbuild profile points atdeps/. The bundle found there importsmaplibre-gl, which npm installed inassets/node_modules. Inconfig/config.exs, add that directory to theNODE_PATHof the esbuild profile, after the two entries already there:config/config.exs env: %{ "NODE_PATH" => [ Path.expand("../deps", __DIR__), Mix.Project.build_path(), Path.expand("../assets/node_modules", __DIR__) ] }Without that entry the build stops with
Could not resolve "maplibre-gl". If you did not install the deck.gl packages, which onlydeckgl_layerneeds, also add--external:@deck.gl/*at the end of the profile'sargs. -
maplibre-gl 6 only: serve the worker
maplibre-gl 6 starts its worker from separate files that esbuild does not copy into your build, and without them the map draws no tile data; maplibre-gl 5 needs none of this. MaplibreX's
mix maplibrex.copy_workercopies them next toapp.js: add it to the asset aliases inmix.exs, after esbuild:mix.exs "assets.build": ["compile", "tailwind my_app", "esbuild my_app", "maplibrex.copy_worker"], "assets.deploy": [ "tailwind my_app --minify", "esbuild my_app --minify", "maplibrex.copy_worker", "phx.digest" ],Then point MapLibre at the copy, near the top of
assets/js/app.js:assets/js/app.js import { getVersion, setWorkerUrl } from "maplibre-gl" setWorkerUrl(`/assets/js/maplibre-gl-worker.mjs?v=${getVersion()}`)Run
mix assets.buildafter every install or upgrade of maplibre-gl:mix phx.serverdoes not copy the files. With maplibre-gl 5 the task copies nothing and says so, so the aliases can keep it. -
Import the stylesheets
assets/css/app.css @import "maplibre-gl/dist/maplibre-gl.css"; @import "../../deps/maplibrex/assets/css/maplibrex.css"; -
Import the components
lib/my_app_web/live/map_live.ex defmodule MyAppWeb.MapLive do use MyAppWeb, :live_view import MaplibreX.Components # ... endA component pushes an event to your LiveView only when its
eventsattribute names it, so a plain<.map>pushes none. Give each component the events you handle, such asevents={["map:moved"]}, and write ahandle_event/3clause for each: an event with no matching clause crashes your LiveView.
A minimal map
A map, a navigation control and one marker per city. When the visitor pans or zooms, map:moved updates the assigns.
def render(assigns) do
~H"""
<.map id="map" center={@center} zoom={@zoom} class="h-96 w-full" events={["map:moved"]} />
<.navigation_control id="nav" map_id="map" position="top-left" />
<.marker :for={city <- @cities} id={city.id} map_id="map"
lng_lat={city.coords} color="#22d3ee" popup_text={city.name} />
"""
end
# The map pushes only the events its `events` attribute names.
def handle_event("map:moved", %{"center" => center, "zoom" => zoom}, socket) do
{:noreply, assign(socket, center: center, zoom: zoom)}
end
Components by family
The roadmap explains each status that is not stable, with its library issue.
| Family | Components | Stable | Limited | Planned |
|---|---|---|---|---|
| Map Stable 1 Limited 0 Planned 0 | 1 | 1 | 0 | 0 |
| Markers & popups Stable 2 Limited 0 Planned 0 | 2 | 2 | 0 | 0 |
| Layers Stable 11 Limited 0 Planned 0 | 11 | 11 | 0 | 0 |
| Sources Stable 5 Limited 0 Planned 0 | 5 | 5 | 0 | 0 |
| Images Stable 1 Limited 0 Planned 0 | 1 | 1 | 0 | 0 |
| Controls Stable 12 Limited 0 Planned 0 | 12 | 12 | 0 | 0 |
| 3D Stable 2 Limited 0 Planned 0 | 2 | 2 | 0 | 0 |
| Drawing Stable 1 Limited 0 Planned 0 | 1 | 1 | 0 | 0 |
| Advanced Stable 3 Limited 0 Planned 0 | 3 | 3 | 0 | 0 |
| All families Stable 38 Limited 0 Planned 0 | 38 | 38 | 0 | 0 |
Where to go next
- Components Every component by family, with its attributes, events and status.
- The map example A map whose camera is driven from the server, with client-side commands and its events.
- Scenarios Several components combined into one working map.
- Roadmap What is limited or planned, and what MaplibreX 0.8.0 fixes.