MaplibreX.Components.ControlButton (MaplibreX v0.1.0)

Copy Markdown View Source

Reusable button control for MapLibre maps.

This component provides a standardized button control with optional icons, tooltips, and active state styling. It can display either a text icon name or custom HTML/SVG content via slot.

Examples

Button with text icon:

<.control_button
  id="layers-btn"
  map_id="my-map"
  icon="layers"
  tooltip="Toggle Layers"
  phx-click="toggle_layers"
/>

Button with custom SVG icon:

<.control_button
  id="custom-btn"
  map_id="my-map"
  tooltip="Custom Action"
  phx-click="custom_action"
>
  <svg width="20" height="20" viewBox="0 0 20 20">
    <path d="M10 0 L20 10 L10 20 L0 10 Z" fill="currentColor"/>
  </svg>
</.control_button>

Button with active state:

<.control_button
  id="filter-btn"
  map_id="my-map"
  icon="filter"
  tooltip="Filter"
  active={@filter_active}
  phx-click="toggle_filter"
/>

Multiple buttons at different positions:

def render(assigns) do
  ~H"""
  <.map id="my-map" center={[-74.5, 40]} zoom={9} style={@style} class="h-96" />

  <.control_button
    id="zoom-in"
    map_id="my-map"
    position="top-right"
    icon="+"
    tooltip="Zoom In"
    phx-click="zoom_in"
  />

  <.control_button
    id="zoom-out"
    map_id="my-map"
    position="top-right"
    icon="-"
    tooltip="Zoom Out"
    phx-click="zoom_out"
  />

  <.control_button
    id="my-location"
    map_id="my-map"
    position="bottom-right"
    icon="📍"
    tooltip="My Location"
    phx-click="go_to_location"
  />
  """
end

Button with Heroicons (if using heroicons library):

<.control_button
  id="settings"
  map_id="my-map"
  tooltip="Settings"
  phx-click="open_settings"
>
  <.icon name="hero-cog" class="w-5 h-5" />
</.control_button>

Summary

Functions

Renders a button control on the map.

Functions

control_button(assigns)

Renders a button control on the map.

Attributes

  • id (required) - Unique identifier for the control button
  • map_id (required) - ID of the map to add the control to
  • position - Control position: "top-left", "top-right", "bottom-left", "bottom-right". Defaults to "top-right"
  • icon - Text icon or emoji to display (if no slot provided)
  • tooltip - Tooltip text shown on hover
  • active - Whether the button is in active state (for styling). Defaults to false
  • class - Additional CSS classes to apply to the button

Slots

  • inner_block - Custom HTML/SVG icon content (alternative to icon attribute)

Events

This component does not emit custom events. Use standard Phoenix event bindings (phx-click, etc.) on the component itself.

Notes

  • Either icon attribute OR slot content should be provided (not both)
  • If both are provided, slot content takes precedence
  • The button integrates with MapLibre's control system
  • Active state adds visual styling to indicate button state
  • Compatible with any icon library (Heroicons, FontAwesome, etc.)

Attributes

  • id (:string) (required)
  • map_id (:string) (required)
  • position (:string) - Defaults to "top-right".
  • icon (:string) - Defaults to nil.
  • tooltip (:string) - Defaults to nil.
  • active (:boolean) - Defaults to false.
  • class (:string) - Defaults to "".
  • Global attributes are accepted.

Slots

  • inner_block