Styling and labels · Angular, React and OpenLayers
Hover and selection styles
Combine reactive pointer-hover feature styles and click selection styles on OpenLayers vector polygons in Angular and React without conflicts.

What this example shows
Four dark-red rectangular zones sit across districts of Kraków over OpenStreetMap. Moving the mouse over any rectangle lightens its fill immediately as hover feedback, while clicking selects a rectangle in crisp blue via OpenLayers’ Select interaction.
When you need it
Interactive parcel maps, district pickers and building explorers need distinct visual states for “currently hovered under cursor” versus “actively clicked/selected for inspection”.
Keeping hover state declarative in the template and click selection in aol-interaction-select avoids imperative style mutations that fall out of sync.
The code
The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.
color-select-hover.tsimport { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import {
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPolygonComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SelectInteractionComponent,
SourceOsmComponent,
SourceVectorComponent,
StyleComponent,
StyleFillComponent,
StyleStrokeComponent,
ViewComponent,
} from 'cc-openlayers';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
/** Polygons that change colour under the pointer and when selected by a click. */
@Component({
selector: 'app-color-select-hover',
imports: [
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPolygonComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SelectInteractionComponent,
SourceOsmComponent,
SourceVectorComponent,
StyleComponent,
StyleFillComponent,
StyleStrokeComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px" (pointerMove)="hover($event)">
<aol-interaction-default />
<aol-control-defaults />
<aol-interaction-select [style]="selectedStyle" />
<aol-view [zoom]="12"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
<aol-layer-vector>
<aol-style>
<aol-style-stroke color="rgba(90, 17, 26)" [width]="3" />
<aol-style-fill color="rgba(90, 17, 26, 0.5)" />
</aol-style>
<aol-source-vector>
@for (polygon of polygons; track polygon.id) {
<aol-feature [id]="polygon.id">
<aol-geometry-polygon>
<aol-collection-coordinates [coordinates]="polygon.coordinates" srid="EPSG:4326" />
</aol-geometry-polygon>
@if (polygon.id === hovered()) {
<aol-style>
<aol-style-stroke color="white" [width]="3" />
<aol-style-fill color="rgba(90, 17, 26, 0.3)" />
</aol-style>
}
</aol-feature>
}
</aol-source-vector>
</aol-layer-vector>
</aol-map>
`,
})
export class ColorSelectHover {
readonly hovered = signal<string | number | undefined>(undefined);
readonly selectedStyle = new Style({
fill: new Fill({ color: 'rgba(0, 153, 255, 0.1)' }),
stroke: new Stroke({ color: 'rgba(0, 153, 255)', width: 3 }),
});
readonly polygons = [
{ id: 1, west: 19.86, south: 50.075, east: 19.905, north: 50.095 },
{ id: 2, west: 19.93, south: 50.085, east: 19.975, north: 50.105 },
{ id: 3, west: 19.94, south: 50.025, east: 19.985, north: 50.045 },
{ id: 4, west: 20.01, south: 50.06, east: 20.06, north: 50.085 },
].map(({ id, west, south, east, north }) => ({
id,
coordinates: [
[
[west, south],
[east, south],
[east, north],
[west, north],
[west, south],
],
],
}));
private readonly map = viewChild.required(MapComponent);
private readonly layer = viewChild.required(LayerVectorComponent);
protected hover(event: MapBrowserEvent) {
const hit = this.map().instance.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
layerFilter: (layer) => layer === this.layer().instance,
hitTolerance: 10,
});
this.hovered.set(hit?.getId());
}
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
color-select-hover.react.tsximport { useRef, useState } from 'react';
import type OLMap from 'ol/Map.js';
import type MapBrowserEvent from 'ol/MapBrowserEvent.js';
import type OLVectorLayer from 'ol/layer/Vector.js';
import OLFill from 'ol/style/Fill.js';
import OLStroke from 'ol/style/Stroke.js';
import OLStyle from 'ol/style/Style.js';
import {
CollectionCoordinates,
Coordinate,
DefaultControls,
DefaultInteractions,
Feature,
Fill,
Map,
OSMSource,
PolygonGeometry,
SelectInteraction,
Stroke,
Style,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
const selectedStyle = new OLStyle({
fill: new OLFill({ color: 'rgba(0, 153, 255, 0.1)' }),
stroke: new OLStroke({ color: 'rgba(0, 153, 255)', width: 3 }),
});
const polygons = [
{ id: 1, west: 19.86, south: 50.075, east: 19.905, north: 50.095 },
{ id: 2, west: 19.93, south: 50.085, east: 19.975, north: 50.105 },
{ id: 3, west: 19.94, south: 50.025, east: 19.985, north: 50.045 },
{ id: 4, west: 20.01, south: 50.06, east: 20.06, north: 50.085 },
].map(({ id, west, south, east, north }) => ({
id,
coordinates: [
[
[west, south],
[east, south],
[east, north],
[west, north],
[west, south],
],
],
}));
/** Polygons that change colour under the pointer and when selected by a click. */
export function ColorSelectHover() {
const [hovered, setHovered] = useState<string | number | undefined>(undefined);
const map = useRef<OLMap>(null);
const layer = useRef<OLVectorLayer>(null);
const hover = (event: MapBrowserEvent) => {
const hit = map.current?.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
layerFilter: (candidate) => candidate === layer.current,
hitTolerance: 10,
});
setHovered(hit?.getId());
};
return (
<Map ref={map} height="480px" onPointerMove={hover}>
<DefaultInteractions />
<DefaultControls />
<SelectInteraction style={selectedStyle} />
<View zoom={12}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer ref={layer}>
<Style>
<Stroke color="rgba(90, 17, 26)" width={3} />
<Fill color="rgba(90, 17, 26, 0.5)" />
</Style>
<VectorSource>
{polygons.map((polygon) => (
<Feature key={polygon.id} id={polygon.id}>
<PolygonGeometry>
<CollectionCoordinates coordinates={polygon.coordinates} srid="EPSG:4326" />
</PolygonGeometry>
{polygon.id === hovered ? (
<Style>
<Stroke color="white" width={3} />
<Fill color="rgba(90, 17, 26, 0.3)" />
</Style>
) : null}
</Feature>
))}
</VectorSource>
</VectorLayer>
</Map>
);
}How the React version works
Four polygons over Kraków: the one under the pointer changes its colour, and a click selects a polygon and draws it blue.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection. The hit test looks only at the polygon layer, within ten pixels of the pointer.
From Angular to React
| Angular | React |
|---|---|
<aol-interaction-select [style]> | <SelectInteraction style> with the style constant in the module |
(pointerMove) and viewChild of the map and the layer | onPointerMove on Map with useRef<OLMap> and useRef<OLVectorLayer> |
a signal of the hovered id and @if adding the feature's own <aol-style> | useState of the hovered id and a conditional <Style> inside the hovered <Feature> |
@for over the polygons | polygons.map(…) with <Feature key={polygon.id}> |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular. The hovered polygon's own style is a child component: it is set on the feature when it mounts and removed when the pointer moves on.
Architecture and OpenLayers mechanics
On (pointerMove) of aol-map, forEachFeatureAtPixel (scoped strictly to the polygon layer) writes the hovered feature’s ID into a reactive signal. Inside the template, @if attaches a temporary aol-style child only to the matching feature—reverting cleanly to the layer’s base red style as soon as the cursor leaves.
Separately, aol-interaction-select manages clicked selection state and renders the chosen feature with its own dedicated [style].
How it works
(pointerMove)onaol-mapqueriesforEachFeatureAtPixelwith a layer filter to identify the polygon beneath the cursor.- Storing the hovered ID in a signal lets
@ifmount a highlightaol-stylestrictly on the hovered feature. - Removing the conditional
aol-styleautomatically restores the vector layer’s default dark-red polygon style. aol-interaction-selectapplies its own blue[style]to whichever polygon the user clicks.
Pitfalls
- Mutating feature.setStyle(...) imperatively in event handlers conflicts with declarative template bindings—drive conditional
aol-styleblocks from signals instead. - Always pass a layer filter (and optional pixel hit tolerance for thin strokes) to
forEachFeatureAtPixelso background vector layers do not steal hover hits.
Related examples
The same feature in the OpenLayers examples .
Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.