Interactions · Angular, React and OpenLayers
Select features on hover
Highlight Polish voivodeship polygons dynamically on pointer hover in OpenLayers by configuring Select interaction condition as pointerMove.

What this example shows
Across a categorical map of Poland’s 16 voivodeships coloured by TERYT code, gliding the mouse across provincial borders highlights whichever voivodeship sits beneath the cursor with a crisp white outline while preserving its base fill colour and displaying its Polish name below the map.
When you need it
Choropleth dashboards, statistical atlases and region pickers use instant hover highlighting to confirm which polygon the cursor is over before the user clicks to drill down.
Configuring an OpenLayers Select interaction with pointerMove delegates hit-testing, previous-feature deselection and highlight rendering entirely to OpenLayers.
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.
select-hover-features.tsimport { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerVectorComponent,
MapComponent,
SelectInteractionComponent,
SourceGeoJSONComponent,
ViewComponent,
} from 'cc-openlayers';
import { pointerMove } from 'ol/events/condition';
import type { FeatureLike } from 'ol/Feature';
import type { SelectEvent } from 'ol/interaction/Select';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
/** The colour of each voivodeship, by its position in code order. */
const PALETTE = [
'#4e79a7',
'#f28e2b',
'#e15759',
'#76b7b2',
'#59a14f',
'#edc948',
'#b07aa1',
'#ff9da7',
'#9c755f',
'#bab0ac',
'#86bcb6',
'#d37295',
'#fabfd2',
'#b6992d',
'#499894',
'#79706e',
];
const fillOf = (feature: FeatureLike) => new Fill({ color: PALETTE[Number(feature.get('code')) / 2 - 1] ?? '#eeeeee' });
/** Voivodeships selected by hovering: the one under the pointer gets a white outline and is named below the map. */
@Component({
selector: 'app-select-hover-features',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerVectorComponent,
MapComponent,
SelectInteractionComponent,
SourceGeoJSONComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-interaction-select [condition]="pointerMove" [style]="hoverStyle" (olSelect)="select($event)" />
<aol-view [zoom]="6"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
<aol-layer-vector background="white" [style]="colorStyle">
<aol-source-geojson url="assets/poland/voivodeships.geojson" featureProjection="EPSG:3857" />
</aol-layer-vector>
</aol-map>
<div class="example-controls">
<span>
Voivodeship <output>{{ name() || '–' }}</output>
</span>
</div>
`,
})
export class SelectHoverFeatures {
readonly name = signal('');
protected readonly pointerMove = pointerMove;
protected readonly colorStyle = (feature: FeatureLike) => new Style({ fill: fillOf(feature) });
protected readonly hoverStyle = (feature: FeatureLike) =>
new Style({
fill: fillOf(feature),
stroke: new Stroke({ color: 'rgba(255, 255, 255, 0.7)', width: 2 }),
});
protected select(event: SelectEvent): void {
this.name.set(event.selected[0]?.get('name') ?? '');
}
} 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.
select-hover-features.react.tsximport { useState } from 'react';
import { pointerMove } from 'ol/events/condition.js';
import type { FeatureLike } from 'ol/Feature.js';
import type { SelectEvent } from 'ol/interaction/Select.js';
import Fill from 'ol/style/Fill.js';
import Stroke from 'ol/style/Stroke.js';
import Style from 'ol/style/Style.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
GeoJSONSource,
Map,
SelectInteraction,
VectorLayer,
View,
} from 'cc-openlayers/react';
/** The colour of each voivodeship, by its position in code order. */
const PALETTE = [
'#4e79a7',
'#f28e2b',
'#e15759',
'#76b7b2',
'#59a14f',
'#edc948',
'#b07aa1',
'#ff9da7',
'#9c755f',
'#bab0ac',
'#86bcb6',
'#d37295',
'#fabfd2',
'#b6992d',
'#499894',
'#79706e',
];
const fillOf = (feature: FeatureLike) => new Fill({ color: PALETTE[Number(feature.get('code')) / 2 - 1] ?? '#eeeeee' });
const colorStyle = (feature: FeatureLike) => new Style({ fill: fillOf(feature) });
const hoverStyle = (feature: FeatureLike) =>
new Style({
fill: fillOf(feature),
stroke: new Stroke({ color: 'rgba(255, 255, 255, 0.7)', width: 2 }),
});
/** Voivodeships selected by hovering: the one under the pointer gets a white outline and is named below the map. */
export function SelectHoverFeatures() {
const [name, setName] = useState('');
const select = (event: SelectEvent) => setName(event.selected[0]?.get('name') ?? '');
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<SelectInteraction condition={pointerMove} style={hoverStyle} onSelect={select} />
<View zoom={6}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<VectorLayer background="white" style={colorStyle}>
<GeoJSONSource url="assets/poland/voivodeships.geojson" featureProjection="EPSG:3857" />
</VectorLayer>
</Map>
<div className="example-controls">
<span>
Voivodeship <output>{name || '–'}</output>
</span>
</div>
</>
);
}How the React version works
Voivodeships selected by hovering: the one under the pointer gets a white outline and is named below the map; each is coloured by its code.
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.
From Angular to React
| Angular | React |
|---|---|
<aol-source-geojson> with the voivodeships | <GeoJSONSource> |
<aol-interaction-select [condition]="pointerMove" [style]> | <SelectInteraction condition={pointerMove} style> with the style function in the module |
(olSelect) and a signal of the name | onSelect and useState |
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.
Architecture and OpenLayers mechanics
Passing OpenLayers’ pointerMove event predicate into [condition] switches the Select interaction from click-driven to continuous cursor tracking—selecting the polygon under the pointer and deselecting the previous one automatically.
A custom hoverStyle function preserves each voivodeship’s TERYT palette fill while adding a contrasting white border stroke, and olSelect writes the active province’s name into the name signal (clearing it when moving onto empty background).
How it works
- Setting
[condition]topointerMovemakes Select follow cursor motion automatically instead of waiting for clicks. hoverStyleretains each polygon’s TERYT-derived fill colour while adding a high-contrast white outline.olSelectupdates thenamesignal with the hovered voivodeship’s name and clears it when leaving the country.- Deterministic TERYT codes (02, 04, ...) map each province to its consistent categorical colour.
Pitfalls
- Touch screens emit no passive hover events without touching and dragging the screen—always pair hover previews with a click/tap interaction when supporting mobile and tablet users.
- Hover selection clears the instant the cursor moves away: mount a second click-conditioned Select interaction whenever users also need to pin a chosen region while interacting with a side panel.
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.