control_button
StableA map control button with an icon or your own content, and an active state.
<div id="control-button-example">
<.example_frame error={@map_error} error_id="example-control-button-map-error">
<.map
id="example-control-button-map"
center={[2.1734, 41.3851]}
zoom={15}
pitch={@pitch}
bearing={-20}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- phx-click is passed on to the button on the map; active tints it,
in the colour this app gives --maplibrex-control-active-background. --%>
<.control_button
id="example-control-button-tilt"
map_id="example-control-button-map"
position="top-left"
tooltip={tooltip(@tilted)}
active={@tilted}
class="grid! place-items-center"
phx-click="tilt"
>
<.icon name="hero-cube-transparent" class="size-5" />
</.control_button>
<div class="border-t border-line px-4 py-4">
<.example_note id="example-control-button-hint" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Press the button in the map's top-left corner: each press reaches handle_event/3, which tilts or flattens the map."
)}
</.example_note>
</div>
<%!-- map:moved keeps pitch current when the map is tilted by hand. --%>
<.example_readout values={[tilted: @tilted, pitch: @pitch, presses: @presses]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="control-button-example">
<.example_frame error={@map_error} error_id="example-control-button-map-error">
<.map
id="example-control-button-map"
center={[2.1734, 41.3851]}
zoom={15}
pitch={@pitch}
bearing={-20}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- phx-click is passed on to the button on the map; active tints it,
in the colour this app gives --maplibrex-control-active-background. --%>
<.control_button
id="example-control-button-tilt"
map_id="example-control-button-map"
position="top-left"
tooltip={tooltip(@tilted)}
active={@tilted}
class="grid! place-items-center"
phx-click="tilt"
>
<.icon name="hero-cube-transparent" class="size-5" />
</.control_button>
<div class="border-t border-line px-4 py-4">
<.example_note id="example-control-button-hint" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Press the button in the map's top-left corner: each press reaches handle_event/3, which tilts or flattens the map."
)}
</.example_note>
</div>
<%!-- map:moved keeps pitch current when the map is tilted by hand. --%>
<.example_readout values={[tilted: @tilted, pitch: @pitch, presses: @presses]} />
</div>
defmodule MaplibrexDemoWeb.Examples.ControlButtonExample do
@moduledoc """
The example of `<.control_button>`: a map button whose `phx-click` reaches
this LiveView. Each press tilts or flattens the map from the server, and
the button's `active` state and tooltip follow the assigns.
"""
use MaplibrexDemoWeb, :live_view
import MaplibreX.Components
import MaplibrexDemoWeb.Examples.ExampleComponents
# Demo plumbing, not needed in an application: applies the page's locale,
# copies each map event to the page's Events tab and keeps @theme on the
# page's light or dark theme.
on_mount MaplibrexDemoWeb.Examples.Plumbing
# OpenFreeMap's basemap for the page's theme: the map's style follows @theme.
defp basemap("dark"), do: "https://tiles.openfreemap.org/styles/dark"
defp basemap("light"), do: "https://tiles.openfreemap.org/styles/liberty"
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, tilted: false, pitch: 0, presses: 0, map_error: nil)}
end
# From the button on the map.
@impl true
def handle_event("tilt", _params, socket) do
tilted = not socket.assigns.tilted
{:noreply,
socket
|> assign(tilted: tilted, pitch: if(tilted, do: 60, else: 0))
|> update(:presses, &(&1 + 1))}
end
# The map can also be tilted by hand (right-drag or two fingers); map:moved
# keeps the pitch assign in step with it.
def handle_event("map:moved", %{"pitch" => pitch}, socket) do
{:noreply, assign(socket, :pitch, pitch)}
end
def handle_event("map:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
defp tooltip(true), do: gettext("Flatten the map")
defp tooltip(false), do: gettext("Tilt the map")
end