# Shareable link to a place

A shareable link to the place picked, shown under the map, that opens the map on it.

## How it works

The page's address names the place picked, and the line under the map gives its link: following it opens the map on that place, and the back button steps through what was picked. "View all", the list's first entry, takes it out again.

- **Note**: each version has its own parameter, as a parameter belongs to one map, and this site opens on the HTML version, so only its link works here. On your own page, with one map, the link just works.
- **HTML**: `url-state="place"` on `<maplace-map>`, `url-state="place-maplibre"` on MapLibre, does all of it, a place no location has being ignored ([URL state](/element/#url-state)); `fit-all` and `fit-all-label` add the first entry ([Map attributes](/element/#map-attributes)). The line under the map is the page's own script, on `maplace-selectionchange`: a link needs only the parameter, and the map's id as its hash so a long page lands on it.
- **Script** and **Bundler**: by hand, in a few lines: `selectedId` from the address, checked against your locations as an unknown id throws ([Options](/store/#options)); a `selectionChange` listener calling `history.pushState`; and a `popstate` listener calling [`select`](/store/#methods). `list: { fitAll: true, fitAllLabel }` adds the first entry ([List view options](/store/#list-view-options)), and the same listener writes the line under the map.
- **React**: `defaultSelectedId` and `onSelectionChange` on `<MaplaceProvider>` ([Controlled selection](/react/#controlled-selection)), and small components through [`useMaplace()`](/react/#usemaplace): one selecting on `popstate`, one writing the line under the map.

## Code

### HTML on MapLibre

`packages/maplace/examples/link-to-place/maplibre/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- The address says which place is selected: a link opens on it, and back steps through what was picked; "View all" takes it out. The line under the map gives the link to what is picked. -->
<maplace-map id="link-to-place-maplibre-map" list fit-all fit-all-label="View all" url-state="place-maplibre">
  <maplace-location location-id="rome" lat="41.9028" lng="12.4964" title="Rome"></maplace-location>
  <maplace-location location-id="milan" lat="45.4642" lng="9.19" title="Milan"></maplace-location>
  <maplace-location location-id="naples" lat="40.8518" lng="14.2681" title="Naples"></maplace-location>
  <maplace-location location-id="turin" lat="45.0703" lng="7.6869" title="Turin"></maplace-location>
</maplace-map>
<p id="link-to-place-maplibre-link"></p>
<script>
  const linkToPlaceMapLibreMap = document.getElementById('link-to-place-maplibre-map');

  /** The line under the map: a link opening on the place `id`, on this map, or a prompt for one. */
  function linkToPlaceMapLibreShowLink(id) {
    const line = document.getElementById('link-to-place-maplibre-link');
    if (id === undefined) {
      line.textContent = 'Pick a place for a link to it.';
      return;
    }
    const address = new URL(location.href);
    address.searchParams.set('place-maplibre', id);
    address.hash = 'link-to-place-maplibre-map';
    const link = document.createElement('a');
    link.href = address.href;
    link.textContent = address.search;
    line.replaceChildren('Link to this place: ', link);
  }

  linkToPlaceMapLibreShowLink(linkToPlaceMapLibreMap.maplace.getSnapshot().selectedId);
  linkToPlaceMapLibreMap.addEventListener('maplace-selectionchange', (event) => linkToPlaceMapLibreShowLink(event.detail.id));
</script>
```

### HTML on Google

`packages/maplace/examples/link-to-place/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<!-- The address says which place is selected: a link opens on it, and back steps through what was picked; "View all" takes it out. The line under the map gives the link to what is picked. -->
<maplace-map id="link-to-place-map" api-key="YOUR_KEY" list fit-all fit-all-label="View all" url-state="place">
  <maplace-location location-id="rome" lat="41.9028" lng="12.4964" title="Rome"></maplace-location>
  <maplace-location location-id="milan" lat="45.4642" lng="9.19" title="Milan"></maplace-location>
  <maplace-location location-id="naples" lat="40.8518" lng="14.2681" title="Naples"></maplace-location>
  <maplace-location location-id="turin" lat="45.0703" lng="7.6869" title="Turin"></maplace-location>
</maplace-map>
<p id="link-to-place-link"></p>
<script>
  const linkToPlaceMap = document.getElementById('link-to-place-map');

  /** The line under the map: a link opening on the place `id`, on this map, or a prompt for one. */
  function linkToPlaceShowLink(id) {
    const line = document.getElementById('link-to-place-link');
    if (id === undefined) {
      line.textContent = 'Pick a place for a link to it.';
      return;
    }
    const address = new URL(location.href);
    address.searchParams.set('place', id);
    address.hash = 'link-to-place-map';
    const link = document.createElement('a');
    link.href = address.href;
    link.textContent = address.search;
    line.replaceChildren('Link to this place: ', link);
  }

  linkToPlaceShowLink(linkToPlaceMap.maplace.getSnapshot().selectedId);
  linkToPlaceMap.addEventListener('maplace-selectionchange', (event) => linkToPlaceShowLink(event.detail.id));
</script>
```

### Script on MapLibre

`packages/maplace/examples/link-to-place/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 map and its list 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>
  #link-to-place-maplibre-script-map-and-list { display: flex; height: 24rem; }
  #link-to-place-maplibre-script-map { flex: 1 1 auto; min-width: 0; }
  #link-to-place-maplibre-script-list { display: contents; }
  #link-to-place-maplibre-script-list [role="listbox"] { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, as the element's does. */
  @media (width < 40rem) {
    #link-to-place-maplibre-script-map-and-list { flex-direction: column; }
    #link-to-place-maplibre-script-list [role="listbox"] { flex: none; max-height: 40%; }
  }
</style>
<div id="link-to-place-maplibre-script-map-and-list">
  <div id="link-to-place-maplibre-script-map"></div>
  <div id="link-to-place-maplibre-script-list"></div>
</div>
<p id="link-to-place-maplibre-script-link"></p>
<script type="module">
  const PLACE = "place-maplibre-script";
  const locations = [
    { id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
    { id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
    { id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
    { id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin" },
  ];

  /** The place the address names, if there is one: a link can name anything. */
  function placeInAddress() {
    const id = new URLSearchParams(location.search).get(PLACE);
    return locations.find((place) => place.id === id)?.id;
  }

  /** A new history entry for a place picked, unless the address says so already. */
  function writeAddress({ id }) {
    if (id === placeInAddress()) return;
    const address = new URL(location.href);
    if (id === undefined) address.searchParams.delete(PLACE);
    else address.searchParams.set(PLACE, id);
    history.pushState(null, "", address);
  }

  /** The line under the map: a link opening on the place `id`, on this map, or a prompt for one. */
  function showLink(id) {
    const line = document.getElementById("link-to-place-maplibre-script-link");
    if (id === undefined) {
      line.textContent = "Pick a place for a link to it.";
      return;
    }
    const address = new URL(location.href);
    address.searchParams.set(PLACE, id);
    address.hash = "link-to-place-maplibre-script-map";
    const link = document.createElement("a");
    link.href = address.href;
    link.textContent = address.search;
    line.replaceChildren("Link to this place: ", link);
  }

  const maplace = Maplace.createMapLibreMap("#link-to-place-maplibre-script-map", {
    locations,
    selectedId: placeInAddress(),
    list: { container: "#link-to-place-maplibre-script-list", fitAll: true, fitAllLabel: "View all" },
  });
  showLink(maplace.getSnapshot().selectedId);
  maplace.on("selectionChange", (change) => {
    writeAddress(change);
    showLink(change.id);
  });
  // Only on a change: selecting the selected place again would pan back to it.
  addEventListener("popstate", () => {
    const place = placeInAddress();
    if (place !== maplace.getSnapshot().selectedId) maplace.select(place);
  });
</script>
```

### Script on Google

`packages/maplace/examples/link-to-place/script.html`

```html
<!doctype html>
<!-- A page of your own script: the CDN script, what the map and its list 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>
  #link-to-place-script-map-and-list { display: flex; height: 24rem; }
  #link-to-place-script-map { flex: 1 1 auto; min-width: 0; }
  #link-to-place-script-list { display: contents; }
  #link-to-place-script-list [role="listbox"] { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, as the element's does. */
  @media (width < 40rem) {
    #link-to-place-script-map-and-list { flex-direction: column; }
    #link-to-place-script-list [role="listbox"] { flex: none; max-height: 40%; }
  }
</style>
<div id="link-to-place-script-map-and-list">
  <div id="link-to-place-script-map"></div>
  <div id="link-to-place-script-list"></div>
</div>
<p id="link-to-place-script-link"></p>
<script type="module">
  const PLACE = "place-script";
  const locations = [
    { id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
    { id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
    { id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
    { id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin" },
  ];

  /** The place the address names, if there is one: a link can name anything. */
  function placeInAddress() {
    const id = new URLSearchParams(location.search).get(PLACE);
    return locations.find((place) => place.id === id)?.id;
  }

  /** A new history entry for a place picked, unless the address says so already. */
  function writeAddress({ id }) {
    if (id === placeInAddress()) return;
    const address = new URL(location.href);
    if (id === undefined) address.searchParams.delete(PLACE);
    else address.searchParams.set(PLACE, id);
    history.pushState(null, "", address);
  }

  /** The line under the map: a link opening on the place `id`, on this map, or a prompt for one. */
  function showLink(id) {
    const line = document.getElementById("link-to-place-script-link");
    if (id === undefined) {
      line.textContent = "Pick a place for a link to it.";
      return;
    }
    const address = new URL(location.href);
    address.searchParams.set(PLACE, id);
    address.hash = "link-to-place-script-map";
    const link = document.createElement("a");
    link.href = address.href;
    link.textContent = address.search;
    line.replaceChildren("Link to this place: ", link);
  }

  const maplace = Maplace.createGoogleMap("#link-to-place-script-map", {
    apiKey: "YOUR_KEY",
    locations,
    selectedId: placeInAddress(),
    list: { container: "#link-to-place-script-list", fitAll: true, fitAllLabel: "View all" },
  });
  showLink(maplace.getSnapshot().selectedId);
  maplace.on("selectionChange", (change) => {
    writeAddress(change);
    showLink(change.id);
  });
  // Only on a change: selecting the selected place again would pan back to it.
  addEventListener("popstate", () => {
    const place = placeInAddress();
    if (place !== maplace.getSnapshot().selectedId) maplace.select(place);
  });
</script>
```

### Bundler on MapLibre

`packages/maplace/examples/link-to-place/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>
  #link-to-place-maplibre-vanilla-map-and-list { display: flex; height: 24rem; }
  #link-to-place-maplibre-vanilla-map { flex: 1 1 auto; min-width: 0; }
  #link-to-place-maplibre-vanilla-list { display: contents; }
  #link-to-place-maplibre-vanilla-list [role="listbox"] { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, as the element's does. */
  @media (width < 40rem) {
    #link-to-place-maplibre-vanilla-map-and-list { flex-direction: column; }
    #link-to-place-maplibre-vanilla-list [role="listbox"] { flex: none; max-height: 40%; }
  }
</style>
<div id="link-to-place-maplibre-vanilla-map-and-list">
  <div id="link-to-place-maplibre-vanilla-map"></div>
  <div id="link-to-place-maplibre-vanilla-list"></div>
</div>
<p id="link-to-place-maplibre-vanilla-link"></p>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/link-to-place/maplibre/vanilla.ts`

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

const PLACE = "place-maplibre-vanilla";
const locations: Location[] = [
  { id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
  { id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
  { id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
  { id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin" },
];

/** The place the address names, if there is one: a link can name anything. */
function placeInAddress(): string | undefined {
  const id = new URLSearchParams(location.search).get(PLACE);
  return locations.find((place) => place.id === id)?.id;
}

/** A new history entry for a place picked, unless the address says so already. */
function writeAddress({ id }: { readonly id: string | undefined }): void {
  if (id === placeInAddress()) return;
  const address = new URL(location.href);
  if (id === undefined) address.searchParams.delete(PLACE);
  else address.searchParams.set(PLACE, id);
  history.pushState(null, "", address);
}

/** The line under the map: a link opening on the place `id`, on this map, or a prompt for one. */
function showLink(id: string | undefined): void {
  const line = document.getElementById("link-to-place-maplibre-vanilla-link") as HTMLElement;
  if (id === undefined) {
    line.textContent = "Pick a place for a link to it.";
    return;
  }
  const address = new URL(location.href);
  address.searchParams.set(PLACE, id);
  address.hash = "link-to-place-maplibre-vanilla-map";
  const link = document.createElement("a");
  link.href = address.href;
  link.textContent = address.search;
  line.replaceChildren("Link to this place: ", link);
}

const maplace = createMapLibreMap("#link-to-place-maplibre-vanilla-map", {
  locations,
  selectedId: placeInAddress(),
  list: {
    container: "#link-to-place-maplibre-vanilla-list",
    fitAll: true,
    fitAllLabel: "View all",
  },
});
showLink(maplace.getSnapshot().selectedId);
maplace.on("selectionChange", (change) => {
  writeAddress(change);
  showLink(change.id);
});
// Only on a change: selecting the selected place again would pan back to it.
addEventListener("popstate", () => {
  const place = placeInAddress();
  if (place !== maplace.getSnapshot().selectedId) maplace.select(place);
});
```

### Bundler on Google

`packages/maplace/examples/link-to-place/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>
  #link-to-place-vanilla-map-and-list { display: flex; height: 24rem; }
  #link-to-place-vanilla-map { flex: 1 1 auto; min-width: 0; }
  #link-to-place-vanilla-list { display: contents; }
  #link-to-place-vanilla-list [role="listbox"] { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, as the element's does. */
  @media (width < 40rem) {
    #link-to-place-vanilla-map-and-list { flex-direction: column; }
    #link-to-place-vanilla-list [role="listbox"] { flex: none; max-height: 40%; }
  }
</style>
<div id="link-to-place-vanilla-map-and-list">
  <div id="link-to-place-vanilla-map"></div>
  <div id="link-to-place-vanilla-list"></div>
</div>
<p id="link-to-place-vanilla-link"></p>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/link-to-place/vanilla.ts`

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

const PLACE = "place-vanilla";
const locations: Location[] = [
  { id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
  { id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
  { id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
  { id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin" },
];

/** The place the address names, if there is one: a link can name anything. */
function placeInAddress(): string | undefined {
  const id = new URLSearchParams(location.search).get(PLACE);
  return locations.find((place) => place.id === id)?.id;
}

/** A new history entry for a place picked, unless the address says so already. */
function writeAddress({ id }: { readonly id: string | undefined }): void {
  if (id === placeInAddress()) return;
  const address = new URL(location.href);
  if (id === undefined) address.searchParams.delete(PLACE);
  else address.searchParams.set(PLACE, id);
  history.pushState(null, "", address);
}

/** The line under the map: a link opening on the place `id`, on this map, or a prompt for one. */
function showLink(id: string | undefined): void {
  const line = document.getElementById("link-to-place-vanilla-link") as HTMLElement;
  if (id === undefined) {
    line.textContent = "Pick a place for a link to it.";
    return;
  }
  const address = new URL(location.href);
  address.searchParams.set(PLACE, id);
  address.hash = "link-to-place-vanilla-map";
  const link = document.createElement("a");
  link.href = address.href;
  link.textContent = address.search;
  line.replaceChildren("Link to this place: ", link);
}

const maplace = createGoogleMap("#link-to-place-vanilla-map", {
  locations,
  selectedId: placeInAddress(),
  list: { container: "#link-to-place-vanilla-list", fitAll: true, fitAllLabel: "View all" },
});
showLink(maplace.getSnapshot().selectedId);
maplace.on("selectionChange", (change) => {
  writeAddress(change);
  showLink(change.id);
});
// Only on a change: selecting the selected place again would pan back to it.
addEventListener("popstate", () => {
  const place = placeInAddress();
  if (place !== maplace.getSnapshot().selectedId) maplace.select(place);
});
```

### React on MapLibre

`packages/maplace/examples/link-to-place/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>
  #link-to-place-maplibre-react-root .map-and-list { display: flex; height: 24rem; }
  #link-to-place-maplibre-react-root .map { flex: 1 1 auto; min-width: 0; }
  #link-to-place-maplibre-react-root [role="listbox"] { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, as the element's does. */
  @media (width < 40rem) {
    #link-to-place-maplibre-react-root .map-and-list { flex-direction: column; }
    #link-to-place-maplibre-react-root [role="listbox"] { flex: none; max-height: 40%; }
  }
</style>
<div id="link-to-place-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/link-to-place/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, useEffect, useSyncExternalStore } from "react";
import { createRoot } from "react-dom/client";

const PLACE = "place-maplibre-react";
const locations: Location[] = [
  { id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
  { id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
  { id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
  { id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin" },
];

/** The place the address names, if there is one: a link can name anything. */
function placeInAddress(): string | undefined {
  const id = new URLSearchParams(location.search).get(PLACE);
  return locations.find((place) => place.id === id)?.id;
}

/** A new history entry for a place picked, unless the address says so already. */
function writeAddress({ id }: { readonly id: string | undefined }): void {
  if (id === placeInAddress()) return;
  const address = new URL(location.href);
  if (id === undefined) address.searchParams.delete(PLACE);
  else address.searchParams.set(PLACE, id);
  history.pushState(null, "", address);
}

/** Back and forward select what the address names then. */
function FollowAddress() {
  const maplace = useMaplace();
  useEffect(() => {
    // Only on a change: selecting the selected place again would pan back to it.
    const follow = () => {
      const place = placeInAddress();
      if (place !== maplace.getSnapshot().selectedId) maplace.select(place);
    };
    addEventListener("popstate", follow);
    return () => removeEventListener("popstate", follow);
  }, [maplace]);
  return null;
}

/** The line under the map: a link opening on the place picked, on this map, or a prompt for one. */
function PlaceLink() {
  const maplace = useMaplace();
  const id = useSyncExternalStore(maplace.subscribe, () => maplace.getSnapshot().selectedId);
  if (id === undefined) return <p>Pick a place for a link to it.</p>;
  const address = new URL(location.href);
  address.searchParams.set(PLACE, id);
  address.hash = "link-to-place-maplibre-react-root";
  return (
    <p>
      Link to this place: <a href={address.href}>{address.search}</a>
    </p>
  );
}

function LinkToPlace() {
  return (
    <MaplaceProvider
      locations={locations}
      defaultSelectedId={placeInAddress()}
      onSelectionChange={writeAddress}
    >
      <div className="map-and-list">
        <MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
        <LocationList fitAll fitAllLabel="View all" />
      </div>
      <PlaceLink />
      <FollowAddress />
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("link-to-place-maplibre-react-root") as HTMLElement).render(
  <StrictMode>
    <LinkToPlace />
  </StrictMode>,
);
```

### React on Google

`packages/maplace/examples/link-to-place/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>
  #link-to-place-react-root .map-and-list { display: flex; height: 24rem; }
  #link-to-place-react-root .map { flex: 1 1 auto; min-width: 0; }
  #link-to-place-react-root [role="listbox"] { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, as the element's does. */
  @media (width < 40rem) {
    #link-to-place-react-root .map-and-list { flex-direction: column; }
    #link-to-place-react-root [role="listbox"] { flex: none; max-height: 40%; }
  }
</style>
<div id="link-to-place-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/link-to-place/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, useEffect, useSyncExternalStore } from "react";
import { createRoot } from "react-dom/client";

const PLACE = "place-react";
const locations: Location[] = [
  { id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
  { id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
  { id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
  { id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin" },
];

/** The place the address names, if there is one: a link can name anything. */
function placeInAddress(): string | undefined {
  const id = new URLSearchParams(location.search).get(PLACE);
  return locations.find((place) => place.id === id)?.id;
}

/** A new history entry for a place picked, unless the address says so already. */
function writeAddress({ id }: { readonly id: string | undefined }): void {
  if (id === placeInAddress()) return;
  const address = new URL(location.href);
  if (id === undefined) address.searchParams.delete(PLACE);
  else address.searchParams.set(PLACE, id);
  history.pushState(null, "", address);
}

/** Back and forward select what the address names then. */
function FollowAddress() {
  const maplace = useMaplace();
  useEffect(() => {
    // Only on a change: selecting the selected place again would pan back to it.
    const follow = () => {
      const place = placeInAddress();
      if (place !== maplace.getSnapshot().selectedId) maplace.select(place);
    };
    addEventListener("popstate", follow);
    return () => removeEventListener("popstate", follow);
  }, [maplace]);
  return null;
}

/** The line under the map: a link opening on the place picked, on this map, or a prompt for one. */
function PlaceLink() {
  const maplace = useMaplace();
  const id = useSyncExternalStore(maplace.subscribe, () => maplace.getSnapshot().selectedId);
  if (id === undefined) return <p>Pick a place for a link to it.</p>;
  const address = new URL(location.href);
  address.searchParams.set(PLACE, id);
  address.hash = "link-to-place-react-root";
  return (
    <p>
      Link to this place: <a href={address.href}>{address.search}</a>
    </p>
  );
}

function LinkToPlace() {
  return (
    <MaplaceProvider
      locations={locations}
      defaultSelectedId={placeInAddress()}
      onSelectionChange={writeAddress}
    >
      <div className="map-and-list">
        <MaplaceMap createAdapter={createGoogleAdapter} className="map" />
        <LocationList fitAll fitAllLabel="View all" />
      </div>
      <PlaceLink />
      <FollowAddress />
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("link-to-place-react-root") as HTMLElement).render(
  <StrictMode>
    <LinkToPlace />
  </StrictMode>,
);
```
