Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a button as a map control #344

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions site/src/components/examples/CustomControl.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Map from '../../../../lib/Map.js';
import OSM from '../../../../lib/source/OSM.js';
import React, {useCallback, useState} from 'react';
import TileLayer from '../../../../lib/layer/WebGLTile.js';
import View from '../../../../lib/View.js';

function MapButton(props) {
return <button {...props}>⛶ reset view</button>;
}

const initialViewState = {center: [0, 0], zoom: 1};

function CustomControl() {
const [viewState, setViewState] = useState(initialViewState);

const onViewChange = useCallback(event => {
const view = event.target;
setViewState({
center: view.getCenter(),
zoom: view.getZoom(),
});
}, []);

const onButtonClick = useCallback(() => {
setViewState(initialViewState);
}, []);

return (
<div style={{position: 'relative', height: '100%'}}>
<Map>
<View
center={viewState.center}
zoom={viewState.zoom}
onChange={onViewChange}
/>
<TileLayer>
<OSM />
</TileLayer>
</Map>
<MapButton
style={{position: 'absolute', top: '10px', right: '10px'}}
onClick={onButtonClick}
/>
</div>
);
}

export default CustomControl;
12 changes: 12 additions & 0 deletions site/src/content/examples/custom-control.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: 'Custom Control'
level: 1
description: |
This example demonstrates how a regular React component can be used as a map control.
The `<Map>` and `<MapButton>` components are rendered to the same parent element,
and the map button is styled to render at the top right of the map.
---

import CustomControl from '../../components/examples/CustomControl.jsx';

<CustomControl client:only="react" />