Maplace.js

Styling

Restyled venue map

A dark frame, pins coloured by category, a legend that filters them and a card of the venues, or of the one picked.

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

Styled by whats-on.css

<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- A look all your own, on OpenFreeMap's liberty style: a dark frame, pins of your markup
     coloured by category, a count over the map's top left and a legend on its bottom left whose
     chips show one category; Maplace.js's panel on the map made nothing. -->
<!-- This example's look lives in the examples' styles/whats-on.css, which every version links. -->
<link rel="stylesheet" href="../../styles/whats-on.css">
<div id="whats-on-maplibre-frame" class="whats-on">
  <div id="whats-on-maplibre-status" class="event-status over-map">
    <output id="whats-on-maplibre-count" class="event-count"></output>
    <span id="whats-on-maplibre-selected" class="event-selected" hidden></span>
  </div>
  <maplace-map id="whats-on-maplibre-map" class="whats-on-map" map-style="https://tiles.openfreemap.org/styles/liberty"
    list="maplibre-whats-on-legend" list-position="bottom-left">
    <maplace-location location-id="national-theatre" lat="51.507" lng="-0.1145" title="National Theatre" group="Theatre"
      data-address="Upper Ground, London SE1 9PX" data-since="1976" data-known-for="Three stages on the South Bank">
      <template data-icon><span class="pin" data-category="Theatre"></span></template>
    </maplace-location>
    <maplace-location location-id="globe" lat="51.5081" lng="-0.0972" title="Shakespeare's Globe" group="Theatre"
      data-address="21 New Globe Walk, London SE1 9DT" data-since="1997" data-known-for="An open-air playhouse rebuilt after the 1599 original">
      <template data-icon><span class="pin" data-category="Theatre"></span></template>
    </maplace-location>
    <maplace-location location-id="donmar" lat="51.5136" lng="-0.1254" title="Donmar Warehouse" group="Theatre"
      data-address="41 Earlham Street, London WC2H 9LX" data-since="1977" data-known-for="A small studio theatre in Covent Garden">
      <template data-icon><span class="pin" data-category="Theatre"></span></template>
    </maplace-location>
    <maplace-location location-id="royal-albert-hall" lat="51.501" lng="-0.1774" title="Royal Albert Hall" group="Concerts"
      data-address="Kensington Gore, London SW7 2AP" data-since="1871" data-known-for="The BBC Proms, every summer">
      <template data-icon><span class="pin" data-category="Concerts"></span></template>
    </maplace-location>
    <maplace-location location-id="wigmore-hall" lat="51.5165" lng="-0.149" title="Wigmore Hall" group="Concerts"
      data-address="36 Wigmore Street, London W1U 2BP" data-since="1901" data-known-for="Chamber music and song recitals">
      <template data-icon><span class="pin" data-category="Concerts"></span></template>
    </maplace-location>
    <maplace-location location-id="barbican" lat="51.5202" lng="-0.0938" title="Barbican Hall" group="Concerts"
      data-address="Silk Street, London EC2Y 8DS" data-since="1982" data-known-for="Home of the London Symphony Orchestra">
      <template data-icon><span class="pin" data-category="Concerts"></span></template>
    </maplace-location>
    <maplace-location location-id="borough-market" lat="51.5055" lng="-0.091" title="Borough Market" group="Markets"
      data-address="8 Southwark Street, London SE1 1TL" data-since="1756" data-known-for="One of London's oldest food markets">
      <template data-icon><span class="pin" data-category="Markets"></span></template>
    </maplace-location>
    <maplace-location location-id="camden-market" lat="51.5413" lng="-0.146" title="Camden Market" group="Markets"
      data-address="Camden Lock Place, London NW1 8AF" data-since="1974" data-known-for="Stalls along the Regent's Canal">
      <template data-icon><span class="pin" data-category="Markets"></span></template>
    </maplace-location>
    <maplace-location location-id="spitalfields" lat="51.5197" lng="-0.0754" title="Old Spitalfields Market" group="Markets"
      data-address="16 Horner Square, London E1 6EW" data-since="1638" data-known-for="A Victorian market hall in the East End">
      <template data-icon><span class="pin" data-category="Markets"></span></template>
    </maplace-location>
    <maplace-location location-id="british-library" lat="51.5299" lng="-0.1277" title="British Library" group="Talks"
      data-address="96 Euston Road, London NW1 2DB" data-since="1973" data-known-for="The UK's national library, at St Pancras">
      <template data-icon><span class="pin" data-category="Talks"></span></template>
    </maplace-location>
    <maplace-location location-id="conway-hall" lat="51.5203" lng="-0.1188" title="Conway Hall" group="Talks"
      data-address="25 Red Lion Square, London WC1R 4RL" data-since="1929" data-known-for="Talks and debates, home of the Conway Hall Ethical Society">
      <template data-icon><span class="pin" data-category="Talks"></span></template>
    </maplace-location>
    <maplace-location location-id="royal-institution" lat="51.5098" lng="-0.1425" title="Royal Institution" group="Talks"
      data-address="21 Albemarle Street, London W1S 4BS" data-since="1799" data-known-for="The Christmas Lectures, given since 1825">
      <template data-icon><span class="pin" data-category="Talks"></span></template>
    </maplace-location>
  </maplace-map>
  <aside id="whats-on-maplibre-card" class="venue-card" aria-label="Venue">
    <div class="venue-picker">
      <p class="venue-list-category" hidden></p>
      <p class="venue-empty">Pick a venue on the map, or here.</p>
      <ul class="venue-list" aria-label="Venues"></ul>
    </div>
    <template>
      <div class="venue">
        <button type="button" class="venue-close" aria-label="Close"></button>
        <p class="venue-category"></p>
        <h3 class="venue-name">Venue</h3>
        <dl>
          <div><dt>Address</dt><dd class="venue-address"></dd></div>
          <div><dt>Since</dt><dd class="venue-since"></dd></div>
          <div><dt>Known for</dt><dd class="venue-known-for"></dd></div>
        </dl>
      </div>
    </template>
  </aside>
</div>
<script>
  // The category the map shows alone, if any: the legend presses its chip, the card names it.
  const whatsOnMaplibreShownAlone = (maplace) => {
    const groups = maplace.getSnapshot().filter?.groups;
    return groups?.length === 1 ? groups[0] : undefined;
  };

  // The legend, a list view of your own: a chip per category, in the venues' order, showing that
  // category alone, or every one again once pressed. The chips are made once, so a chip pressed
  // from the keyboard keeps its focus; a store change only moves the press.
  Maplace.defineListView('maplibre-whats-on-legend', (maplace) => {
    const legend = document.createElement('fieldset');
    legend.className = 'event-legend';
    legend.setAttribute('aria-label', 'Categories');
    const categories = new Set(maplace.getSnapshot().locations.flatMap(({ group }) => group ?? []));
    const chips = [...categories].map((category) => {
      const chip = document.createElement('button');
      chip.type = 'button';
      chip.className = 'category-chip';
      chip.dataset.category = category;
      chip.setAttribute('aria-pressed', 'false');
      chip.textContent = category;
      chip.addEventListener('click', () => maplace.setFilter(whatsOnMaplibreShownAlone(maplace) === category ? undefined : [category]));
      return chip;
    });
    legend.append(...chips);
    let pressed;
    const pressShownAlone = () => {
      const alone = whatsOnMaplibreShownAlone(maplace);
      if (alone === pressed) return;
      pressed = alone;
      for (const chip of chips) chip.setAttribute('aria-pressed', String(chip.dataset.category === alone));
    };
    pressShownAlone();
    const unsubscribe = maplace.subscribe(pressShownAlone);
    return { element: legend, destroy: () => { unsubscribe(); legend.remove(); } };
  });

  // Every venue fitted, with room at the edges for the count and the legend over the map, as the
  // map opens; then the count over the map: how many venues are shown, of all of them, said again
  // when that changes.
  {
    const maplace = document.getElementById('whats-on-maplibre-map').maplace;
    maplace.fitAll({ padding: 80 });
    const count = document.getElementById('whats-on-maplibre-count');
    const writeCount = () => {
      const snapshot = maplace.getSnapshot();
      const shown = Maplace.listedLocations(snapshot).length;
      const total = snapshot.locations.length;
      const text = shown === total ? `${total} venues` : `${shown} of ${total} venues`;
      if (count.textContent !== text) count.textContent = text;
    };
    writeCount();
    maplace.subscribe(writeCount);

    // Beside it, the name of the venue selected, marked with its category for its colour; hidden
    // while none is. Not a live region: Maplace.js says each selection to a screen reader already.
    const name = document.getElementById('whats-on-maplibre-selected');
    const writeName = (id) => {
      const selected = maplace.getSnapshot().locations.find((location) => location.id === id);
      name.hidden = selected === undefined;
      name.textContent = selected?.title ?? '';
      name.dataset.category = selected?.group ?? '';
    };
    writeName(maplace.getSnapshot().selectedId);
    maplace.on('selectionChange', ({ id }) => writeName(id));

    // The card beside the map: the venue selected, its details from its location's data-
    // attributes (its data), its category marked for its colour, made from the card's template and in the card
    // only while a venue is selected, with a × closing it; or, while none is, the venues shown,
    // each a button selecting it, under the category shown alone, if any, written again only when
    // the venues shown change, as a hover changes the store too. Not a live region either.
    const card = document.getElementById('whats-on-maplibre-card');
    const picker = card.querySelector('.venue-picker');
    const list = card.querySelector('.venue-list');
    const categoryLine = card.querySelector('.venue-list-category');
    const venuePart = card.querySelector('template').content.firstElementChild.cloneNode(true);
    const close = venuePart.querySelector('.venue-close');
    close.addEventListener('click', () => {
      const closed = maplace.getSnapshot().selectedId ?? '';
      maplace.select(undefined);
      picker.querySelector(`.venue-pick[data-id="${CSS.escape(closed)}"]`)?.focus();
    });
    const venueEntry = ({ id, title, group }) => {
      const pick = document.createElement('button');
      pick.type = 'button';
      pick.className = 'venue-pick';
      pick.dataset.category = group ?? '';
      pick.textContent = title ?? id;
      pick.dataset.id = id;
      pick.addEventListener('click', () => {
        maplace.select(id);
        close.focus();
      });
      const entry = document.createElement('li');
      entry.append(pick);
      return entry;
    };
    let listedIds;
    const writeList = () => {
      const listed = Maplace.listedLocations(maplace.getSnapshot());
      const ids = listed.map(({ id }) => id).join();
      if (ids === listedIds) return;
      listedIds = ids;
      const alone = whatsOnMaplibreShownAlone(maplace);
      categoryLine.hidden = alone === undefined;
      categoryLine.textContent = alone ?? '';
      categoryLine.dataset.category = alone ?? '';
      list.replaceChildren(...listed.map(venueEntry));
    };
    writeList();
    maplace.subscribe(writeList);
    const write = (selector, text) => { venuePart.querySelector(selector).textContent = text; };
    const writeCard = (id) => {
      const selected = maplace.getSnapshot().locations.find((location) => location.id === id);
      picker.hidden = selected !== undefined;
      if (selected === undefined) {
        venuePart.remove();
        return;
      }
      venuePart.dataset.category = selected.group;
      write('.venue-category', selected.group);
      write('.venue-name', selected.title);
      write('.venue-address', selected.data.address);
      write('.venue-since', selected.data.since);
      write('.venue-known-for', selected.data.knownFor);
      if (!venuePart.isConnected) card.append(venuePart);
    };
    writeCard(maplace.getSnapshot().selectedId);
    maplace.on('selectionChange', ({ id }) => writeCard(id));
  }
</script>

How it works

A map that looks nothing like Maplace.js’s: a dark frame, pins of your own coloured by category, a count over the map, a legend on it whose chips show one category at a time, and beside it a card listing the venues shown, or the one picked. Each piece is one of Maplace.js’s hooks; together they are one idea, the whole look the page’s own.

  • HTML: a <template data-icon> in each <maplace-location>, with its group (Location attributes); the legend a list view of your own, defineListView, at list-position="bottom-left". The element puts one view of yours on the map, so the count is your own element over the map’s top left, where a control would sit, taking no clicks. The card’s details are each venue’s own data- attributes, its data, read by the page’s script on the store’s selectionChange into the card’s <template>, added to the card while a venue is picked (Events); while none is, the card lists the venues shown, from listedLocations, each a button calling select.
  • Script and Bundler: an icon node per location (Icons), and the count and the legend put on the map with the adapter’s mountControl, the count rewritten from listedLocations, the name beside it from the snapshot’s selectedId, the press moved with the store’s filter as the store changes, and the card written from the venue’s data (Location fields) on selectionChange, its markup a <template> in the page, and while no venue is, a button per venue shown, rewritten from listedLocations when they change; a chip calls setFilter with its category, or with nothing to show them all.
  • React: markerIcon on <MaplaceMap>, and the count and the legend in two <MapControl>s, reading the store with useMaplace(), as the card does beside <MaplaceMap>, inside <MaplaceProvider>, its list read as the ids listed so a hover renders nothing, and flushSync rendering it at once so a press can move the focus into it.
  • On Google: its map-type, Street View and camera controls are off (map-options in HTML, mapOptions elsewhere), for the clean look, as the MapLibre map shows none either.
  • In every version: the look is one stylesheet, whats-on.css, in the examples’ styles/ folder, which each version’s HTML links, so the page, the script, the bundled and the React versions share it, scoped by the frame’s class, .whats-on, and the map’s, .whats-on-map. Every venue fitted with 80px of padding, clear of the count and the legend: the store’s view, { kind: "fit-all", padding: 80 } (The first view), or in HTML, which has no attribute for it, fitAll before the map opens. Maplace.js’s panel around what goes on the map is made nothing with .maplace-control { padding: 0; background: none; box-shadow: none; }, as its rules weigh nothing (Styling); each category’s colour is one custom property, --category, which its pins and its chip take; the pins’ hovered and selected looks are rules on data-hovered and data-selected, as in HTML markers with CSS states. A venue picked from the card’s list moves the focus to the card’s ×, named “Close”, which takes it back to that venue in the list, so a keyboard never loses its place. The name of the venue picked is a plain element, not a live region: Maplace.js already says each selection to a screen reader (On every map). The pins differ by colour alone, so the chips, named, are how a reader picks a category out.