control_group
StableGroups several controls into one map control.
orientation
Use the three buttons in the map's top-left corner, or zoom the map yourself: map:moved keeps the zoom assign current.
- @zoom
- 11
- @orientation
- vertical
<div id="control-group-example">
<.example_frame error={@map_error} error_id="example-control-group-map-error">
<.map
id="example-control-group-map"
center={[-99.1332, 19.4326]}
zoom={@zoom}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- Each <.control_button> in the group is shown inside it, in template
order; active tints the current level. A new orientation applies to
the same group. --%>
<.control_group
id="example-control-group"
map_id="example-control-group-map"
position="top-left"
orientation={@orientation}
>
<.control_button
:for={level <- levels()}
id={"example-control-group-#{level.id}"}
map_id="example-control-group-map"
tooltip={label(level.id)}
active={@zoom == level.zoom}
class="grid! place-items-center"
phx-click="zoom"
phx-value-level={level.id}
>
<.icon name={level.icon} class="size-5" />
</.control_button>
</.control_group>
<div class="space-y-4 border-t border-line px-4 py-4">
<.example_group id="example-control-group-orientations" label="orientation" code>
<.example_button
:for={orientation <- orientations()}
id={"example-control-group-orientation-#{orientation}"}
code
pressed={@orientation == orientation}
phx-click="orientation"
phx-value-orientation={orientation}
>
{orientation}
</.example_button>
</.example_group>
<.example_note id="example-control-group-hint" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Use the three buttons in the map's top-left corner, or zoom the map yourself: map:moved keeps the zoom assign current."
)}
</.example_note>
</div>
<.example_readout values={[zoom: @zoom, orientation: @orientation]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="control-group-example">
<.example_frame error={@map_error} error_id="example-control-group-map-error">
<.map
id="example-control-group-map"
center={[-99.1332, 19.4326]}
zoom={@zoom}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- Each <.control_button> in the group is shown inside it, in template
order; active tints the current level. A new orientation applies to
the same group. --%>
<.control_group
id="example-control-group"
map_id="example-control-group-map"
position="top-left"
orientation={@orientation}
>
<.control_button
:for={level <- levels()}
id={"example-control-group-#{level.id}"}
map_id="example-control-group-map"
tooltip={label(level.id)}
active={@zoom == level.zoom}
class="grid! place-items-center"
phx-click="zoom"
phx-value-level={level.id}
>
<.icon name={level.icon} class="size-5" />
</.control_button>
</.control_group>
<div class="space-y-4 border-t border-line px-4 py-4">
<.example_group id="example-control-group-orientations" label="orientation" code>
<.example_button
:for={orientation <- orientations()}
id={"example-control-group-orientation-#{orientation}"}
code
pressed={@orientation == orientation}
phx-click="orientation"
phx-value-orientation={orientation}
>
{orientation}
</.example_button>
</.example_group>
<.example_note id="example-control-group-hint" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Use the three buttons in the map's top-left corner, or zoom the map yourself: map:moved keeps the zoom assign current."
)}
</.example_note>
</div>
<.example_readout values={[zoom: @zoom, orientation: @orientation]} />
</div>
defmodule MaplibrexDemoWeb.Examples.ControlGroupExample do
@moduledoc """
The example of `<.control_group>`: three `<.control_button>` components
shown as one map control, each setting the map's zoom from the server,
laid out in the orientation the assigns choose.
"""
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"
@levels [
%{id: "country", zoom: 5, icon: "hero-globe-americas"},
%{id: "city", zoom: 11, icon: "hero-building-office-2"},
%{id: "street", zoom: 16, icon: "hero-map-pin"}
]
@orientations ~w(vertical horizontal)
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, zoom: 11, orientation: "vertical", map_error: nil)}
end
# From the buttons in the group.
@impl true
def handle_event("zoom", %{"level" => id}, socket) do
case Enum.find(@levels, &(&1.id == id)) do
nil -> {:noreply, socket}
level -> {:noreply, assign(socket, :zoom, level.zoom)}
end
end
def handle_event("orientation", %{"orientation" => orientation}, socket)
when orientation in @orientations do
{:noreply, assign(socket, :orientation, orientation)}
end
def handle_event("orientation", _params, socket), do: {:noreply, socket}
# The visitor can zoom the map too; map:moved keeps the zoom assign in
# step, so a button always has a new zoom to set.
def handle_event("map:moved", %{"zoom" => zoom}, socket) do
{:noreply, assign(socket, :zoom, zoom)}
end
def handle_event("map:error", %{"error" => message}, socket) do
{:noreply, assign(socket, :map_error, message)}
end
defp levels, do: @levels
defp orientations, do: @orientations
defp label("country"), do: gettext("Zoom to the country")
defp label("city"), do: gettext("Zoom to the city")
defp label("street"), do: gettext("Zoom to street level")
end