Skip to content

Commit e46d4a5

Browse files
committed
feat: add RotationButton
1 parent 92f8e44 commit e46d4a5

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
This demonstrates the use of the RotationButton
2+
3+
```jsx
4+
import { useEffect, useState } from 'react';
5+
6+
import OlMap from 'ol/Map';
7+
import OlView from 'ol/View';
8+
import OlLayerTile from 'ol/layer/Tile';
9+
import OlSourceOsm from 'ol/source/OSM';
10+
import OlVectorLayer from 'ol/layer/Vector';
11+
import OlVectorSource from 'ol/source/Vector';
12+
import OlFormatGeoJSON from 'ol/format/GeoJSON';
13+
import { fromLonLat } from 'ol/proj';
14+
15+
import MapContext from '@terrestris/react-geo/dist/Context/MapContext/MapContext'
16+
import MapComponent from '@terrestris/react-geo/dist/Map/MapComponent/MapComponent';
17+
import RotationButton from '@terrestris/react-geo/dist/Button/RotationButton/RotationButton';
18+
19+
const RotationButtonExample = () => {
20+
21+
const [map, setMap] = useState();
22+
23+
useEffect(() => {
24+
25+
setMap(new OlMap({
26+
layers: [
27+
new OlLayerTile({
28+
name: 'OSM',
29+
source: new OlSourceOsm()
30+
})
31+
],
32+
view: new OlView({
33+
center: fromLonLat([8, 50]),
34+
zoom: 4
35+
})
36+
}));
37+
},[] );
38+
39+
if (!map) {
40+
return null;
41+
}
42+
43+
return (
44+
<div>
45+
<MapContext.Provider value={map}>
46+
<MapComponent
47+
map={map}
48+
style={{
49+
height: '400px'
50+
}}
51+
/>
52+
<RotationButton/>
53+
</MapContext.Provider>
54+
</div>
55+
);
56+
}
57+
58+
<RotationButtonExample />
59+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as React from 'react';
2+
import { within } from '@testing-library/react';
3+
4+
import OlMap from 'ol/Map';
5+
import OlView from 'ol/View';
6+
import OlSourceOsm from 'ol/source/OSM';
7+
import OlLayerTile from 'ol/layer/Tile';
8+
9+
import { renderInMapContext } from '../../Util/rtlTestUtils';
10+
import RotationButton from './RotationButton';
11+
12+
describe('<RotationButton />', () => {
13+
14+
const coord = [829729, 6708850];
15+
let map: OlMap;
16+
let layer: OlLayerTile<OlSourceOsm>;
17+
18+
beforeEach(() => {
19+
20+
layer = new OlLayerTile({
21+
source: new OlSourceOsm()
22+
});
23+
24+
map = new OlMap({
25+
view: new OlView({
26+
center: coord,
27+
zoom: 10
28+
}),
29+
controls: [],
30+
layers: [
31+
layer
32+
]
33+
});
34+
});
35+
36+
describe('#Basics', () => {
37+
38+
it('is defined', () => {
39+
expect(RotationButton).not.toBeUndefined();
40+
});
41+
42+
it('can be rendered', () => {
43+
const { container } = renderInMapContext(map, <RotationButton />);
44+
45+
const button = within(container).getByRole('button');
46+
expect(button).toBeVisible();
47+
});
48+
});
49+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
3+
import { faArrowsRotate } from '@fortawesome/free-solid-svg-icons';
4+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
5+
6+
import { DragRotateAndZoom } from 'ol/interaction.js';
7+
8+
import { useMap } from '../../Hook/useMap';
9+
import ToggleButton, {
10+
ToggleButtonProps
11+
} from '../ToggleButton/ToggleButton';
12+
13+
14+
export type RotationButtonProps = Partial<ToggleButtonProps>;
15+
16+
export const RotationButton: React.FC<RotationButtonProps> = ({
17+
tooltip = 'Shift + Drag to rotate and zoom the map around its center',
18+
pressedIcon = <FontAwesomeIcon icon={faArrowsRotate} />,
19+
tooltipProps,
20+
...passThroughProps}
21+
) => {
22+
const map = useMap();
23+
24+
if (!map) {
25+
return <></>;
26+
}
27+
let action = new DragRotateAndZoom();
28+
const onToggle = (pressed: boolean) => {
29+
if (pressed) {
30+
map.addInteraction(action);
31+
} else {
32+
map.removeInteraction(action);
33+
}
34+
};
35+
36+
return (
37+
<ToggleButton
38+
tooltip={'Shift + Drag to rotate and zoom the map around its center'}
39+
icon={<FontAwesomeIcon icon={faArrowsRotate} />}
40+
pressedIcon={<FontAwesomeIcon icon={faArrowsRotate} />}
41+
onToggle={onToggle}
42+
tooltipProps={tooltipProps}
43+
{...passThroughProps}
44+
/>
45+
);
46+
};
47+
export default RotationButton;

0 commit comments

Comments
 (0)