Saltar al contenido

popup

Estable

Un popup con contenido de texto o HTML, situado en una coordenada.

lib/maplibrex_demo_web/examples/popup_example.html.heex
<div id="popup-example">
  <.example_frame error={@map_error} error_id="example-popup-map-error">
    <%!-- The map centres on the chosen place, so the popup above it is in view. --%>
    <.map
      id="example-popup-map"
      center={@place.lng_lat}
      zoom={15}
      style={basemap(@theme)}
      events={["map:error"]}
      class="h-full w-full"
    />
  </.example_frame>

  <%!-- The popup sits next to <.map> and names it with map_id. Where it is,
        whether it is open and its anchor are assigns; its content is the
        slot. Every one of them follows its assign after mount. --%>
  <.popup
    id="example-popup"
    map_id="example-popup-map"
    lng_lat={@place.lng_lat}
    open={@open}
    anchor={@anchor}
    events={["popup:opened", "popup:closed"]}
  >
    <p class="font-bold">{name(@place.id)}</p>
    <p class="font-mono">{lng_lat(@place.lng_lat)}</p>

    <%!-- The popup shows the slot's own elements, so its ids are unique and
          its bindings work: a click, a form and a label's for. --%>
    <div class="mt-2 flex items-center gap-2">
      <.example_popup_button id="example-popup-like" phx-click="like">
        <.icon name="hero-heart-micro" class="size-4" />
        {gettext("Like")}
      </.example_popup_button>
      <span id="example-popup-likes" class="tabular-nums">
        {ngettext("1 like", "%{count} likes", @likes)}
      </span>
    </div>

    <.form
      for={@note_form}
      id="example-popup-note"
      phx-change="note"
      phx-submit="save_note"
      class="mt-2"
    >
      <div class="flex justify-between text-label">
        <label for="example-popup-note-text" class="font-bold">{gettext("Note")}</label>
        <span id="example-popup-note-length" class="font-mono tabular-nums">
          {String.length(@note_form[:text].value)}/40
        </span>
      </div>
      <div class="mt-1 flex gap-1.5">
        <input
          type="text"
          id="example-popup-note-text"
          name={@note_form[:text].name}
          value={@note_form[:text].value}
          maxlength="40"
          autocomplete="off"
          class="h-7 min-w-0 flex-1 rounded-ui border border-map-ink/25 bg-transparent px-2"
        />
        <.example_popup_button id="example-popup-note-save" type="submit">
          {gettext("Save")}
        </.example_popup_button>
      </div>
    </.form>
  </.popup>

  <div class="grid gap-x-8 gap-y-4 border-t border-line px-4 py-4 lg:grid-cols-[1fr_auto]">
    <div class="space-y-4">
      <%!-- Assigns: a place moves the popup and opens it. --%>
      <.example_group id="example-popup-places" label={gettext("Set assigns")}>
        <.example_button
          :for={place <- places()}
          id={"example-popup-place-#{place.id}"}
          pressed={@place.id == place.id}
          phx-click="place"
          phx-value-place={place.id}
        >
          {name(place.id)}
        </.example_button>
      </.example_group>

      <div class="flex flex-wrap gap-x-8 gap-y-4">
        <.example_group id="example-popup-anchor" label="anchor" code>
          <.example_button
            :for={anchor <- anchors()}
            id={"example-popup-anchor-#{anchor}"}
            code
            pressed={@anchor == anchor}
            phx-click="anchor"
            phx-value-anchor={anchor}
          >
            {anchor}
          </.example_button>
        </.example_group>

        <.example_group id="example-popup-toggle" label="open" code>
          <.form for={@options} id="example-popup-options" phx-change="options" class="py-1.5">
            <.example_checkbox field={@options[:open]} label="open" />
          </.form>
        </.example_group>
      </div>
    </div>

    <%!-- Commands: JS structs that open and close the popup in the browser,
          with no round trip; popup:opened and popup:closed then tell the
          server. --%>
    <.example_group id="example-popup-commands" label={gettext("Run commands")}>
      <.example_button
        id="example-popup-cmd-open"
        code
        phx-click={PopupCmd.open("example-popup", @place.lng_lat)}
      >
        open
      </.example_button>
      <.example_button
        id="example-popup-cmd-close"
        code
        phx-click={PopupCmd.close("example-popup")}
      >
        close
      </.example_button>
      <.example_button
        id="example-popup-cmd-toggle"
        code
        phx-click={PopupCmd.toggle("example-popup")}
      >
        toggle
      </.example_button>
    </.example_group>
  </div>

  <%!-- popup:opened and popup:closed keep open current. --%>
  <.example_readout values={[
    place: @place.id,
    anchor: @anchor,
    open: @open,
    last_event: @last_event || "—",
    likes: @likes,
    note: @note || "—"
  ]} />
</div>