# List with a details panel

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

## 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](/element/#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](/element/#events)).
- **Script** and **Bundler**: `selectedId` in the map's options and `list: { container }` to mount the list on a node of yours ([Options](/store/#options), [List view options](/store/#list-view-options)), and a `selectionChange` listener ([Events](/store/#events)).
- **React**: `defaultSelectedId` on `<MaplaceProvider>`, `<LocationList />` outside the `<MaplaceMap>`, and a component reading the selection with [`useMaplace()`](/react/#usemaplace).

## Code

### HTML on MapLibre

`packages/maplace/examples/tabs/maplibre/page.html`

```html
<!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>
```

### HTML on Google

`packages/maplace/examples/tabs/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.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-map { flex-direction: column; }
  #tabs-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-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-map > [role="listbox"] > [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
  #tabs-map > #tabs-details { order: -1; padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<maplace-map id="tabs-map" api-key="YOUR_KEY" 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-details"></aside>
</maplace-map>
<script>
  const tabsNotes = { london: 'Capital of the United Kingdom.', edinburgh: 'Capital of Scotland, home of the Fringe arts festival.' };
  const tabsMap = document.getElementById('tabs-map');
  function showTabsNote(id) {
    document.getElementById('tabs-details').textContent = tabsNotes[id] ?? '';
  }
  showTabsNote(tabsMap.maplace.getSnapshot().selectedId);
  tabsMap.addEventListener('maplace-selectionchange', (event) => showTabsNote(event.detail.id));
</script>
```

### Script on MapLibre

`packages/maplace/examples/tabs/maplibre/script.html`

```html
<!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet, what the tabs, their details and the map mount on, and the map made with Maplace. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
  #tabs-maplibre-script-panel { display: flex; flex-direction: column; height: 24rem; }
  #tabs-maplibre-script-map { flex: 1 1 auto; min-height: 0; }
  #tabs-maplibre-script-list [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
  #tabs-maplibre-script-list [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
  #tabs-maplibre-script-list [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
  #tabs-maplibre-script-details { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-maplibre-script-panel">
  <div id="tabs-maplibre-script-list"></div>
  <aside id="tabs-maplibre-script-details"></aside>
  <div id="tabs-maplibre-script-map"></div>
</div>
<script type="module">
  const notes = {
    london: "Capital of the United Kingdom.",
    edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
  };
  const maplace = Maplace.createMapLibreMap("#tabs-maplibre-script-map", {
    selectedId: "london",
    locations: [
      { id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
      { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
    ],
    list: { container: "#tabs-maplibre-script-list" },
  });

  const details = document.querySelector("#tabs-maplibre-script-details");
  showNote(maplace.getSnapshot().selectedId);
  maplace.on("selectionChange", ({ id }) => showNote(id));

  /** The chosen tab's details, in the panel below the tabs. */
  function showNote(id) {
    if (details) details.textContent = id === undefined ? "" : (notes[id] ?? "");
  }
</script>
```

### Script on Google

`packages/maplace/examples/tabs/script.html`

```html
<!doctype html>
<!-- A page of your own script: the CDN script, what the tabs, their details and the map mount on, and the map made with Maplace, which adds Maps JS with your key. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
  #tabs-script-panel { display: flex; flex-direction: column; height: 24rem; }
  #tabs-script-map { flex: 1 1 auto; min-height: 0; }
  #tabs-script-list [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
  #tabs-script-list [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
  #tabs-script-list [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
  #tabs-script-details { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-script-panel">
  <div id="tabs-script-list"></div>
  <aside id="tabs-script-details"></aside>
  <div id="tabs-script-map"></div>
</div>
<script type="module">
  const notes = {
    london: "Capital of the United Kingdom.",
    edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
  };
  const maplace = Maplace.createGoogleMap("#tabs-script-map", {
    apiKey: "YOUR_KEY",
    selectedId: "london",
    locations: [
      { id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
      { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
    ],
    list: { container: "#tabs-script-list" },
  });

  const details = document.querySelector("#tabs-script-details");
  showNote(maplace.getSnapshot().selectedId);
  maplace.on("selectionChange", ({ id }) => showNote(id));

  /** The chosen tab's details, in the panel below the tabs. */
  function showNote(id) {
    if (details) details.textContent = id === undefined ? "" : (notes[id] ?? "");
  }
</script>
```

### Bundler on MapLibre

`packages/maplace/examples/tabs/maplibre/vanilla.html`

```html
<!doctype html>
<!-- The page vanilla.ts runs on: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
  #tabs-maplibre-vanilla-panel { display: flex; flex-direction: column; height: 24rem; }
  #tabs-maplibre-vanilla-map { flex: 1 1 auto; min-height: 0; }
  #tabs-maplibre-vanilla-list [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
  #tabs-maplibre-vanilla-list [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
  #tabs-maplibre-vanilla-list [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
  #tabs-maplibre-vanilla-details { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-maplibre-vanilla-panel">
  <div id="tabs-maplibre-vanilla-list"></div>
  <aside id="tabs-maplibre-vanilla-details"></aside>
  <div id="tabs-maplibre-vanilla-map"></div>
</div>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/tabs/maplibre/vanilla.ts`

```ts
import "maplibre-gl/dist/maplibre-gl.css";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";

const notes: Readonly<Record<string, string>> = {
  london: "Capital of the United Kingdom.",
  edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};
const maplace = createMapLibreMap("#tabs-maplibre-vanilla-map", {
  selectedId: "london",
  locations: [
    { id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
    { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
  ],
  list: { container: "#tabs-maplibre-vanilla-list" },
});

const details = document.querySelector("#tabs-maplibre-vanilla-details");
showNote(maplace.getSnapshot().selectedId);
maplace.on("selectionChange", ({ id }) => showNote(id));

/** The chosen tab's details, in the panel below the tabs. */
function showNote(id: string | undefined): void {
  if (details) details.textContent = id === undefined ? "" : (notes[id] ?? "");
}
```

### Bundler on Google

`packages/maplace/examples/tabs/vanilla.html`

```html
<!doctype html>
<!-- The page vanilla.ts runs on: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->
<script>
  (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
    key: "YOUR_KEY",
    v: "weekly",
  });
</script>
<style>
  #tabs-vanilla-panel { display: flex; flex-direction: column; height: 24rem; }
  #tabs-vanilla-map { flex: 1 1 auto; min-height: 0; }
  #tabs-vanilla-list [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
  #tabs-vanilla-list [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
  #tabs-vanilla-list [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
  #tabs-vanilla-details { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-vanilla-panel">
  <div id="tabs-vanilla-list"></div>
  <aside id="tabs-vanilla-details"></aside>
  <div id="tabs-vanilla-map"></div>
</div>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/tabs/vanilla.ts`

```ts
import { createGoogleMap } from "@maplacejs/maplace/google";

const notes: Readonly<Record<string, string>> = {
  london: "Capital of the United Kingdom.",
  edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};
const maplace = createGoogleMap("#tabs-vanilla-map", {
  selectedId: "london",
  locations: [
    { id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
    { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
  ],
  list: { container: "#tabs-vanilla-list" },
});

const details = document.querySelector("#tabs-vanilla-details");
showNote(maplace.getSnapshot().selectedId);
maplace.on("selectionChange", ({ id }) => showNote(id));

/** The chosen tab's details, in the panel below the tabs. */
function showNote(id: string | undefined): void {
  if (details) details.textContent = id === undefined ? "" : (notes[id] ?? "");
}
```

### React on MapLibre

`packages/maplace/examples/tabs/maplibre/react.html`

```html
<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
  #tabs-maplibre-react-root .panel { display: flex; flex-direction: column; height: 24rem; }
  #tabs-maplibre-react-root .map { flex: 1 1 auto; min-height: 0; }
  #tabs-maplibre-react-root [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
  #tabs-maplibre-react-root [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
  #tabs-maplibre-react-root [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
  #tabs-maplibre-react-root aside { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/tabs/maplibre/react.tsx`

```tsx
import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { LocationList, MaplaceMap, MaplaceProvider, useMaplace } from "@maplacejs/maplace/react";
import { StrictMode, useSyncExternalStore } from "react";
import { createRoot } from "react-dom/client";

const cities: Location[] = [
  { id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
  { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
];
const notes: Readonly<Record<string, string>> = {
  london: "Capital of the United Kingdom.",
  edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};

const noteOf = (id: string | undefined) => (id === undefined ? "" : (notes[id] ?? ""));

/** The chosen place's note, read from the store: the selection it starts on, and every one after. */
function TabDetails() {
  const maplace = useMaplace();
  const selectedId = useSyncExternalStore(
    maplace.subscribe,
    () => maplace.getSnapshot().selectedId,
  );
  return <aside>{noteOf(selectedId)}</aside>;
}

function Tabs() {
  return (
    <MaplaceProvider locations={cities} defaultSelectedId="london">
      <div className="panel">
        <LocationList />
        <TabDetails />
        <MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
      </div>
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("tabs-maplibre-react-root") as HTMLElement).render(
  <StrictMode>
    <Tabs />
  </StrictMode>,
);
```

### React on Google

`packages/maplace/examples/tabs/react.html`

```html
<!doctype html>
<!-- The page react.tsx renders into: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->
<script>
  (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
    key: "YOUR_KEY",
    v: "weekly",
  });
</script>
<style>
  #tabs-react-root .panel { display: flex; flex-direction: column; height: 24rem; }
  #tabs-react-root .map { flex: 1 1 auto; min-height: 0; }
  #tabs-react-root [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
  #tabs-react-root [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
  #tabs-react-root [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
  #tabs-react-root aside { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/tabs/react.tsx`

```tsx
import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, MaplaceMap, MaplaceProvider, useMaplace } from "@maplacejs/maplace/react";
import { StrictMode, useSyncExternalStore } from "react";
import { createRoot } from "react-dom/client";

const cities: Location[] = [
  { id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
  { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
];
const notes: Readonly<Record<string, string>> = {
  london: "Capital of the United Kingdom.",
  edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};

const noteOf = (id: string | undefined) => (id === undefined ? "" : (notes[id] ?? ""));

/** The chosen place's note, read from the store: the selection it starts on, and every one after. */
function TabDetails() {
  const maplace = useMaplace();
  const selectedId = useSyncExternalStore(
    maplace.subscribe,
    () => maplace.getSnapshot().selectedId,
  );
  return <aside>{noteOf(selectedId)}</aside>;
}

function Tabs() {
  return (
    <MaplaceProvider locations={cities} defaultSelectedId="london">
      <div className="panel">
        <LocationList />
        <TabDetails />
        <MaplaceMap createAdapter={createGoogleAdapter} className="map" />
      </div>
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("tabs-react-root") as HTMLElement).render(
  <StrictMode>
    <Tabs />
  </StrictMode>,
);
```
