A button that makes the map fill the screen, with a fallback for browsers that cannot.
<script lang="ts">
import {
Map,
MapControlContainer,
MapFullscreenControl,
MapZoomControl
} from '$lib/components/ui/map/index.js';
let fullscreen = $state(false);
</script>
<div class="relative size-full">
<Map class="size-full" viewport={{ center: [12.4964, 41.9028], zoom: 12, bearing: 0, pitch: 0 }}>
<MapControlContainer position="top-right">
<MapZoomControl />
<MapFullscreenControl onchange={(next) => (fullscreen = next)} />
</MapControlContainer>
</Map>
<p
class="absolute top-3 left-3 rounded-md bg-background/90 px-2 py-1 text-xs shadow"
aria-live="polite"
>
{fullscreen ? 'Fullscreen' : 'In page'}
</p>
</div>
Press the button and the map fills the screen. Press it again, or press Escape, to go back. The icon and the button’s label switch between “Enter fullscreen” and “Exit fullscreen” so everyone can tell which state the map is in.
Only the map goes fullscreen, not the page. Other controls, popups and menus inside the map stay visible.
Usage
Place the control inside a Control Container.
<script lang="ts">
import { Map, MapControlContainer, MapFullscreenControl } from '$lib/components/ui/map/index.js';
</script>
<Map class="h-96">
<MapControlContainer position="top-right">
<MapFullscreenControl />
</MapControlContainer>
</Map>Every browser
The control uses the browser’s Fullscreen API when it can. Some browsers cannot put a single element in fullscreen, such as Safari on iPhone and many in-app webviews. There, the control falls back to CSS: the map is pinned over the whole viewport and the page behind it stops scrolling.
The control picks the fallback when:
- the browser has no Fullscreen API for elements,
- the browser refuses the request, for example inside an iframe without
allowfullscreen, - the request gets no answer within one second.
Escape leaves both kinds of fullscreen.
Knowing the state
onchange is called with true when the map enters fullscreen and false when it leaves. It fires the same way for the API and the fallback.
<MapFullscreenControl onchange={(fullscreen) => (isFullscreen = fullscreen)} />To tell the two kinds apart, read the button’s data-mode attribute. It holds a FullscreenMode: off, native for the Fullscreen API, or fallback for the CSS version. While the fallback is on, the map element also has data-fullscreen="fallback", which you can target in your own CSS.
If the control is removed while the map is fullscreen, the map leaves fullscreen.
API Reference
MapFullscreenControl
Events
| Name | Type | Description |
|---|---|---|
onchange | (fullscreen: boolean) => void | Called with `true` on entering fullscreen and `false` on leaving it. |