Skip to content

fullscreen_control

Stable

A button that shows the map in full screen.

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

lib/maplibrex_demo_web/examples/fullscreen_control_example.html.heex
<div id="fullscreen-example" class="bg-canvas">
  <.example_frame error={@map_error} error_id="example-fullscreen-map-error">
    <.map
      id="example-fullscreen-map"
      center={[18.4232, -33.9188]}
      zoom={12}
      style={basemap(@theme)}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <%!-- The control follows its assigns: a new position moves it, a new
        container_id replaces it in the same place. --%>
  <.fullscreen_control
    id="example-fullscreen"
    map_id="example-fullscreen-map"
    position={@position}
    container_id={@container_id}
    events={["fullscreen:entered", "fullscreen:exited"]}
  />

  <div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[auto_1fr]">
    <.example_group id="example-fullscreen-positions" label="position" code>
      <.example_button
        :for={position <- positions()}
        id={"example-fullscreen-position-#{position}"}
        code
        pressed={@position == position}
        phx-click="position"
        phx-value-position={position}
      >
        {position}
      </.example_button>
    </.example_group>

    <.example_group id="example-fullscreen-containers" label="container_id" code>
      <.example_button
        :for={{name, id} <- containers()}
        id={"example-fullscreen-container-#{name}"}
        code
        pressed={@container_id == id}
        phx-click="container"
        phx-value-container={name}
      >
        {inspect(id)}
      </.example_button>
    </.example_group>
  </div>

  <%!-- fullscreen:entered and fullscreen:exited keep fullscreen current. --%>
  <.example_readout values={[
    position: @position,
    container_id: inspect(@container_id),
    fullscreen: @fullscreen
  ]} />
</div>
lib/maplibrex_demo_web/examples/fullscreen_control_example.ex
defmodule MaplibrexDemoWeb.Examples.FullscreenControlExample do
  @moduledoc """
  The example of `<.fullscreen_control>`: a button that shows the map, or
  the element `container_id` names, in full screen, and reports it with
  `fullscreen:entered` and `fullscreen:exited`.
  """
  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"

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

  # What goes full screen: the map alone (no container_id), or this whole
  # example, controls included.
  @containers %{"map" => nil, "example" => "fullscreen-example"}

  @impl true
  def mount(_params, _session, socket) do
    {:ok,
     assign(socket, position: "top-right", container_id: nil, fullscreen: false, 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("container", %{"container" => container}, socket)
      when is_map_key(@containers, container) do
    {:noreply, assign(socket, :container_id, @containers[container])}
  end

  def handle_event(event, _params, socket) when event in ~w(position container) do
    {:noreply, socket}
  end

  # Events from the control: the browser's full screen was entered or left,
  # by the button or by Escape.

  def handle_event("fullscreen:entered", _payload, socket) do
    {:noreply, assign(socket, :fullscreen, true)}
  end

  def handle_event("fullscreen:exited", _payload, socket) do
    {:noreply, assign(socket, :fullscreen, false)}
  end

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

  defp positions, do: @positions
  defp containers, do: Enum.sort(@containers)
end