Skip to content

Commit 3756fbb

Browse files
committed
fix: correctly remove controls onUnmounted
closes #246
1 parent c8c5689 commit 3756fbb

File tree

3 files changed

+53
-67
lines changed

3 files changed

+53
-67
lines changed

docs/componentsguide/mapcontrols/zoom/index.md

+33-40
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,49 @@ See [Demo page for all Map Controls](../index.md)
88

99
## Properties
1010

11-
### className
11+
### Props from OpenLayers
1212

13-
- **Type**: `String`
14-
- **Default**: `ol-zoom`
13+
Properties are passed-trough from OpenLayers directly.
14+
Their types and default values can be checked-out [in the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_source_ImageStatic-Static.html).
15+
Only some properties deviate caused by reserved keywords from Vue / HTML.
16+
This deviating props are described in the section below.
1517

16-
### duration
18+
### Deviating Properties
1719

18-
- **Type**: `Number`
19-
- **Default**: `250`
20+
None.
2021

21-
### zoomInClassName
22+
## Events
2223

23-
- **Type**: `String`
24-
- **Default**: `ol-zoom-in`
24+
You have access to all Events from the underlying source.
25+
Check out [the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_source_ImageStatic-Static.html) to see the available events tht will be fired.
2526

26-
### zoomOutClassName
27+
```html
28+
<ol-zoom-control @error="handleEvent" />
29+
```
2730

28-
- **Type**: `String`
29-
- **Default**: `ol-zoom-out`
31+
## Methods
3032

31-
### zoomInLabel
33+
You have access to all Methods from the underlying source.
34+
Check out [the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_source_ImageStatic-Static.html) to see the available methods.
3235

33-
- **Type**: `String`
34-
- **Default**: `+`
36+
To access the source, you can use a `ref()` as shown below:
3537

36-
### zoomOutLabel
38+
```vue
39+
<template>
40+
<!-- ... -->
41+
<ol-zoom-control ref="sourceRef" />
42+
<!-- ... -->
43+
</template>
3744
38-
- **Type**: `String`
39-
- **Default**: `-`
45+
<script setup lang="ts">
46+
import { ref, onMounted } from "vue";
47+
import type Zoom from "ol/control/Zoom";
4048
41-
### zoomInTipLabel
49+
const sourceRef = ref<{ source: Zoom }>(null);
4250
43-
- **Type**: `String`
44-
- **Default**: `Zoom in`
45-
46-
### zoomInTipLabel
47-
48-
- **Type**: `String`
49-
- **Default**: `Zoom in`
50-
51-
### zoomOutTipLabel
52-
53-
- **Type**: `String`
54-
- **Default**: `Zoom Out`
55-
56-
### delta
57-
58-
- **Type**: `Number`
59-
- **Default**: `1`
60-
61-
### target
62-
63-
- **Type**: `HTMLElement`
51+
onMounted(() => {
52+
const zoom: Zoom = sourceRef.value?.control;
53+
// call your method on `control`
54+
});
55+
</script>
56+
```

src/components/mapControls/OlZoomControl.vue

+12-26
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,22 @@
22
<div v-if="false"></div>
33
</template>
44
<script setup lang="ts">
5-
import Zoom from "ol/control/Zoom";
5+
import Zoom, { type Options } from "ol/control/Zoom";
66
import { useAttrs } from "vue";
77
import useControl from "@/composables/useControl";
88
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
99
10-
const props = withDefaults(
11-
defineProps<{
12-
duration?: number;
13-
className?: string;
14-
zoomInClassName?: string;
15-
zoomOutClassName?: string;
16-
zoomInLabel?: string;
17-
zoomOutLabel?: string;
18-
zoomInTipLabel?: string;
19-
zoomOutTipLabel?: string;
20-
delta?: number;
21-
target?: HTMLElement;
22-
}>(),
23-
{
24-
duration: 250,
25-
className: "ol-zoom",
26-
zoomInClassName: "ol-zoom-in",
27-
zoomOutClassName: "ol-zoom-out",
28-
zoomInLabel: "+",
29-
zoomOutLabel: "-",
30-
zoomInTipLabel: "Zoom in",
31-
zoomOutTipLabel: "Zoom Out",
32-
delta: 1,
33-
}
34-
);
10+
const props = withDefaults(defineProps<Options>(), {
11+
duration: 250,
12+
className: "ol-zoom",
13+
zoomInClassName: "ol-zoom-in",
14+
zoomOutClassName: "ol-zoom-out",
15+
zoomInLabel: "+",
16+
zoomOutLabel: "-",
17+
zoomInTipLabel: "Zoom in",
18+
zoomOutTipLabel: "Zoom Out",
19+
delta: 1,
20+
});
3521
3622
const attrs = useAttrs();
3723
const { properties } = usePropsAsObjectProperties(props);

src/composables/useControl.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,14 @@ export default function useControl<T extends InnerControlType>(
137137

138138
onUnmounted(() => {
139139
if (parent && parent instanceof Map) {
140-
parent.removeControl(control.value);
140+
parent
141+
?.getControls()
142+
.getArray()
143+
.forEach((c) => {
144+
if (c instanceof ControlType) {
145+
parent.removeControl(c);
146+
}
147+
});
141148
} else {
142149
// ol-ext controls
143150
if (parent?.controls_) {

0 commit comments

Comments
 (0)