zoom_range
StableShows its content only between a minimum and a maximum zoom level.
Set assigns
Run commands
- @zoom
- 10
- @min
- 12
- @max
- nil
- @shown
- false
<div id="zoom-range-example">
<.example_frame error={@map_error} error_id="example-zoom-range-map-error">
<.map
id="example-zoom-range-map"
center={[-46.6333, -23.5505]}
zoom={10}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
<%!-- The content stays where it is written, here over the map; MaplibreX
hides it outside the range. A new min or max applies at once and
to every zoom after it. --%>
<.zoom_range
id="example-zoom-range"
map_id="example-zoom-range-map"
min={@range.min}
max={@range.max}
>
<p
id="example-zoom-range-badge"
class="pointer-events-none absolute bottom-3 left-3 z-10 flex items-center gap-1.5 rounded-ui border border-line bg-canvas px-2.5 py-1.5 text-ui font-bold shadow-overlay"
>
<.icon name="hero-eye-micro" class="size-4 text-accent-ink" />
{describe(@range)}
</p>
</.zoom_range>
</.example_frame>
<div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[1fr_auto]">
<.example_group id="example-zoom-range-ranges" label={gettext("Set assigns")}>
<.example_button
:for={range <- ranges()}
id={"example-zoom-range-preset-#{range.id}"}
code
pressed={@range.id == range.id}
phx-click="range"
phx-value-range={range.id}
>
<span :if={range.min}>min={range.min}</span>
<span :if={range.max}>max={range.max}</span>
</.example_button>
</.example_group>
<%!-- Commands: zoom the map in the browser, across the range's ends. --%>
<.example_group id="example-zoom-range-commands" label={gettext("Run commands")}>
<.example_button
id="example-zoom-range-zoom-out"
code
phx-click={MapCmd.zoom_out("example-zoom-range-map")}
>
zoom_out
</.example_button>
<.example_button
id="example-zoom-range-zoom-in"
code
phx-click={MapCmd.zoom_in("example-zoom-range-map")}
>
zoom_in
</.example_button>
</.example_group>
</div>
<%!-- map:moved keeps zoom current; shown is the same test the hook makes. --%>
<.example_readout values={[
zoom: @zoom,
min: bound(@range.min),
max: bound(@range.max),
shown: shown?(@range, @zoom)
]} />
</div>
The example's template and LiveView, read from the files that run in the preview.
<div id="zoom-range-example">
<.example_frame error={@map_error} error_id="example-zoom-range-map-error">
<.map
id="example-zoom-range-map"
center={[-46.6333, -23.5505]}
zoom={10}
style={basemap(@theme)}
events={["map:moved", "map:error"]}
class="h-full w-full"
/>
<%!-- The content stays where it is written, here over the map; MaplibreX
hides it outside the range. A new min or max applies at once and
to every zoom after it. --%>
<.zoom_range
id="example-zoom-range"
map_id="example-zoom-range-map"
min={@range.min}
max={@range.max}
>
<p
id="example-zoom-range-badge"
class="pointer-events-none absolute bottom-3 left-3 z-10 flex items-center gap-1.5 rounded-ui border border-line bg-canvas px-2.5 py-1.5 text-ui font-bold shadow-overlay"
>
<.icon name="hero-eye-micro" class="size-4 text-accent-ink" />
{describe(@range)}
</p>
</.zoom_range>
</.example_frame>
<div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[1fr_auto]">
<.example_group id="example-zoom-range-ranges" label={gettext("Set assigns")}>
<.example_button
:for={range <- ranges()}
id={"example-zoom-range-preset-#{range.id}"}
code
pressed={@range.id == range.id}
phx-click="range"
phx-value-range={range.id}
>
<span :if={range.min}>min={range.min}</span>
<span :if={range.max}>max={range.max}</span>
</.example_button>
</.example_group>
<%!-- Commands: zoom the map in the browser, across the range's ends. --%>
<.example_group id="example-zoom-range-commands" label={gettext("Run commands")}>
<.example_button
id="example-zoom-range-zoom-out"
code
phx-click={MapCmd.zoom_out("example-zoom-range-map")}
>
zoom_out
</.example_button>
<.example_button
id="example-zoom-range-zoom-in"
code
phx-click={MapCmd.zoom_in("example-zoom-range-map")}
>
zoom_in
</.example_button>
</.example_group>
</div>
<%!-- map:moved keeps zoom current; shown is the same test the hook makes. --%>
<.example_readout values={[
zoom: @zoom,
min: bound(@range.min),
max: bound(@range.max),
shown: shown?(@range, @zoom)
]} />
</div>
defmodule MaplibrexDemoWeb.Examples.ZoomRangeExample do
@moduledoc """
The example of `<.zoom_range>`: a note over the map that MaplibreX shows
only while the zoom is within the range the assigns set. The server
follows the zoom with `map:moved`, so it knows whether the note is shown.
"""
use MaplibrexDemoWeb, :live_view
import MaplibreX.Components
import MaplibrexDemoWeb.Examples.ExampleComponents
alias MaplibreX.Components.Map, as: MapCmd
# 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/positron"
@ranges [
%{id: "from", min: 12, max: nil},
%{id: "between", min: 8, max: 12},
%{id: "up_to", min: nil, max: 8}
]
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, range: hd(@ranges), zoom: 10, map_error: nil)}
end
@impl true
def handle_event("range", %{"range" => id}, socket) do
case Enum.find(@ranges, &(&1.id == id)) do
nil -> {:noreply, socket}
range -> {:noreply, assign(socket, :range, range)}
end
end
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 ranges, do: @ranges
# The same test the zoom_range hook makes in the browser.
defp shown?(%{min: min, max: max}, zoom) do
(is_nil(min) or zoom >= min) and (is_nil(max) or zoom <= max)
end
defp describe(%{min: min, max: nil}), do: gettext("Shown from zoom %{min}", min: min)
defp describe(%{min: nil, max: max}), do: gettext("Shown up to zoom %{max}", max: max)
defp describe(%{min: min, max: max}),
do: gettext("Shown between zoom %{min} and %{max}", min: min, max: max)
defp bound(nil), do: "nil"
defp bound(value), do: value
end