Skip to content

Commit d0605b7

Browse files
committed
feat: add onLayerChangeVisible to vector layer
1 parent f8289f2 commit d0605b7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/layers/vector-layer.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import OLVectorLayer from "ol/layer/Vector";
44
import { Options } from "ol/layer/BaseVector";
55
import { BaseLayerProps } from "./base-layer-props";
66

7-
type Props = Options<any> & BaseLayerProps;
7+
interface VectorLayerProps {
8+
onLayerChangeVisible?: (visible: boolean) => void;
9+
}
10+
11+
type Props = Options<any> & BaseLayerProps & VectorLayerProps;
812
const VectorLayer: FunctionComponent<Props> = ({
913
source,
1014
style,
1115
zIndex = 0,
1216
name,
17+
onLayerChangeVisible,
1318
}) => {
1419
const map = useContext(MapContext);
1520

@@ -21,6 +26,14 @@ const VectorLayer: FunctionComponent<Props> = ({
2126
style,
2227
});
2328
vectorLayer.setProperties({ name });
29+
30+
// If function is not null
31+
if (onLayerChangeVisible) {
32+
vectorLayer.on("change:visible", (evt) =>
33+
onLayerChangeVisible(evt.target)
34+
);
35+
}
36+
2437
map.addLayer(vectorLayer);
2538
vectorLayer.setZIndex(zIndex);
2639

0 commit comments

Comments
 (0)