Maplace.js

Lists and menus

List with a details panel

The list outside the map; a details panel follows the selection.

Markdown

Map and code

Run the examples on

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

These maps come from Google Maps and OpenFreeMap, which see your IP address. They stay off until you say so, once for the whole site. What loading the maps sends

<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- The list sits outside the map, styled as tabs above it, starting on the first place; the chosen tab's details sit right below the tabs, above the map, and follow the selection. -->
<style>
  #tabs-maplibre-map { flex-direction: column; }
  #tabs-maplibre-map > [role="listbox"] { order: -2; display: flex; flex: none; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
  #tabs-maplibre-map > [role="listbox"] > [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
  #tabs-maplibre-map > [role="listbox"] > [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
  #tabs-maplibre-map > #tabs-maplibre-details { order: -1; padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<maplace-map id="tabs-maplibre-map" list selected-id="london">
  <maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London" zoom="6"></maplace-location>
  <maplace-location location-id="edinburgh" lat="55.9533" lng="-3.1883" title="Edinburgh" zoom="6"></maplace-location>
  <aside id="tabs-maplibre-details"></aside>
</maplace-map>
<script>
  const tabsMapLibreNotes = { london: 'Capital of the United Kingdom.', edinburgh: 'Capital of Scotland, home of the Fringe arts festival.' };
  const tabsMapLibreMap = document.getElementById('tabs-maplibre-map');
  function showTabsMapLibreNote(id) {
    document.getElementById('tabs-maplibre-details').textContent = tabsMapLibreNotes[id] ?? '';
  }
  showTabsMapLibreNote(tabsMapLibreMap.maplace.getSnapshot().selectedId);
  tabsMapLibreMap.addEventListener('maplace-selectionchange', (event) => showTabsMapLibreNote(event.detail.id));
</script>

How it works

The list stays outside the map, as a list does unless it is given a corner, and becomes a row of tabs above it, with a panel under them that follows the selection.

  • HTML: list and selected-id on <maplace-map> (Map attributes), the page’s CSS drawing the list as tabs through its role="listbox" and role="option", and a maplace-selectionchange listener filling the panel (Events).
  • Script and Bundler: selectedId in the map’s options and list: { container } to mount the list on a node of yours (Options, List view options), and a selectionChange listener (Events).
  • React: defaultSelectedId on <MaplaceProvider>, <LocationList /> outside the <MaplaceMap>, and a component reading the selection with useMaplace().