Skip to content

control

Stable

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

position

Choose a basemap in the control on the map: the form's phx-change sets the assign.

@basemap
liberty
@style
https://tiles.openfreemap.org/styles/liberty
@position
top-left
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>