Skip to content

control

Stable

Places your own HEEx content in a corner of the map as a control.

The example's template and LiveView, read from the files that run in the preview.

lib/maplibrex_demo_web/examples/control_example.html.heex
<div id="control-example">
  <.example_frame error={@map_error} error_id="example-control-map-error">
    <.map
      id="example-control-map"
      center={[-0.1276, 51.5072]}
      zoom={12}
      style={style_url(@chosen[@theme])}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <%!-- Anything in the slot becomes the control: here a form with radio
        buttons. Its bindings work as anywhere else in the LiveView, and its
        content follows the assigns: the page's theme picks the choices. A
        new position moves the same control. --%>
  <.control id="example-control-basemaps" map_id="example-control-map" position={@position}>
    <form id="example-control-basemap-form" phx-change="basemap" class="px-3 py-2 font-sans">
      <fieldset>
        <legend class="mb-1 text-label font-bold">{gettext("Basemap")}</legend>
        <label
          :for={basemap <- basemaps(@theme)}
          for={"example-control-basemap-#{basemap}"}
          class="flex cursor-pointer items-center gap-2 py-0.5 text-ui"
        >
          <input
            type="radio"
            id={"example-control-basemap-#{basemap}"}
            name="basemap"
            value={basemap}
            checked={@chosen[@theme] == basemap}
            class="accent-accent"
          />
          {basemap_name(basemap)}
        </label>
      </fieldset>
    </form>
  </.control>

  <div class="space-y-4 border-t border-line px-4 py-4">
    <.example_group id="example-control-positions" label="position" code>
      <.example_button
        :for={position <- positions()}
        id={"example-control-position-#{position}"}
        code
        pressed={@position == position}
        phx-click="position"
        phx-value-position={position}
      >
        {position}
      </.example_button>
    </.example_group>

    <.example_note id="example-control-hint" icon="hero-cursor-arrow-rays-micro">
      {gettext(
        "Choose a basemap in the control on the map: the form's phx-change sets the assign."
      )}
    </.example_note>
  </div>

  <.example_readout values={[
    basemap: @chosen[@theme],
    style: style_url(@chosen[@theme]),
    position: @position
  ]} />
</div>
lib/maplibrex_demo_web/examples/control_example.ex
defmodule MaplibrexDemoWeb.Examples.ControlExample do
  @moduledoc """
  The example of `<.control>`: a form of your own, written in HEEx, placed
  on the map as a control. Its `phx-change` reaches this LiveView like any
  other form's, its content follows the assigns, and a new position moves
  it, form and all.
  """
  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 public styles: those the control offers in the page's
  # light theme, and those it offers in the dark one.
  @basemaps %{"light" => ~w(liberty positron bright), "dark" => ~w(dark fiord)}

  @positions ~w(top-left top-right bottom-left bottom-right)

  @impl true
  def mount(_params, _session, socket) do
    # The basemap chosen in each theme: switching the theme switches to the
    # other theme's choice, and back.
    {:ok,
     assign(socket,
       chosen: %{"light" => "liberty", "dark" => "dark"},
       position: "top-left",
       map_error: nil
     )}
  end

  @impl true
  def handle_event("position", %{"position" => position}, socket)
      when position in @positions do
    {:noreply, assign(socket, :position, position)}
  end

  def handle_event("position", _params, socket), do: {:noreply, socket}

  # From the form inside the control: a new basemap is a new style for <.map>.
  def handle_event("basemap", %{"basemap" => basemap}, socket) do
    theme = socket.assigns.theme

    if basemap in @basemaps[theme] do
      {:noreply, update(socket, :chosen, &Map.put(&1, theme, basemap))}
    else
      {:noreply, socket}
    end
  end

  def handle_event("basemap", _params, socket), do: {:noreply, socket}

  def handle_event("map:error", %{"error" => message}, socket) do
    {:noreply, assign(socket, :map_error, message)}
  end

  defp basemaps(theme), do: @basemaps[theme]
  defp positions, do: @positions

  defp style_url(basemap), do: "https://tiles.openfreemap.org/styles/#{basemap}"

  defp basemap_name("liberty"), do: "Liberty"
  defp basemap_name("positron"), do: "Positron"
  defp basemap_name("bright"), do: "Bright"
  defp basemap_name("dark"), do: "Dark"
  defp basemap_name("fiord"), do: "Fiord"
end