Controls · Angular, React and OpenLayers
An attribution that folds on narrow maps
Keep OpenLayers attributions expanded on wide maps and collapse them automatically into a compact button below 600 pixels in Angular and React.

What this example shows
Centred on Kraków across central Europe at zoom 6, this map replaces the bundled control defaults with explicit zoom, rotate and attribution controls so the data credit responds dynamically to container width.
On viewports 600 pixels wide or wider, the full OpenStreetMap copyright notice stays permanently open without a toggle button; narrowing the map below 600 pixels switches the attribution into a collapsible info button and folds it away automatically.
When you need it
Map providers legally require visible data attribution, yet an unfolded multi-provider copyright bar on a 360-pixel smartphone screen can obscure a third of the bottom map edge.
Basing collapse behaviour on the map element’s own pixel width—rather than a coarse CSS media query on the browser window—ensures maps inside narrow dashboard columns fold cleanly even on desktop monitors.
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.
attributions.tsimport { ChangeDetectionStrategy, Component, viewChild } from '@angular/core';
import {
ControlAttributionComponent,
ControlRotateComponent,
ControlZoomComponent,
CoordinateComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
/** The attribution stays open on a wide map and collapses to its button when the map is narrower than 600 pixels. */
@Component({
selector: 'app-attributions',
imports: [
ControlAttributionComponent,
ControlRotateComponent,
ControlZoomComponent,
CoordinateComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px" (olChangeSize)="checkSize()">
<aol-interaction-default />
<aol-control-zoom />
<aol-control-rotate />
<aol-control-attribution [collapsible]="false" />
<aol-view [zoom]="6">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
</aol-map>
`,
})
export class Attributions {
private readonly map = viewChild.required(MapComponent);
private readonly attribution = viewChild.required(ControlAttributionComponent);
protected checkSize(): void {
const size = this.map().instance.getSize();
if (!size) return;
const small = size[0] < 600;
this.attribution().instance.setCollapsible(small);
this.attribution().instance.setCollapsed(small);
}
} 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.
attributions.react.tsximport { useCallback, useRef } from 'react';
import type OLAttribution from 'ol/control/Attribution.js';
import type OLMap from 'ol/Map.js';
import {
AttributionControl,
Coordinate,
DefaultInteractions,
Map,
OSMSource,
RotateControl,
TileLayer,
View,
ZoomControl,
} from 'cc-openlayers/react';
/** The attribution stays open on a wide map and collapses to its button when the map is narrower than 600 pixels. */
export function Attributions() {
const map = useRef<OLMap>(null);
const attribution = useRef<OLAttribution>(null);
const checkSize = useCallback(() => {
const size = map.current?.getSize();
if (!size) return;
const small = size[0] < 600;
attribution.current?.setCollapsible(small);
attribution.current?.setCollapsed(small);
}, []);
return (
<Map ref={map} height="480px" onChangeSize={checkSize}>
<DefaultInteractions />
<ZoomControl />
<RotateControl />
<AttributionControl ref={attribution} collapsible={false} />
<View zoom={6}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
);
}How the React version works
A non-collapsible control replaces the default attribution, and on a map narrower than 600 pixels the example collapses it itself and expands it again when the map widens. It shows a reaction to a change of the map size.
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-control-attribution>, <aol-control-zoom>, <aol-control-rotate> instead of the defaults | <AttributionControl>, <ZoomControl>, <RotateControl> without DefaultControls |
(olChangeSize)="checkSize()" | onChangeSize={checkSize} on Map |
viewChild of the attribution control | useRef passed to AttributionControl |
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 control is a child of Map, as in the Angular template; the adapter adds it to the map and removes it on unmount.
Architecture and OpenLayers mechanics
Declaring aol-control-zoom, aol-control-rotate and aol-control-attribution individually lets the attribution control start expanded without a button via [collapsible]="false". Listening to (olChangeSize) on the map runs checkSize() whenever the map viewport resizes.
Inside checkSize(), maps narrower than 600 pixels call both setCollapsible (true) and setCollapsed (true), while wider maps revert both to false. Calling setCollapsed explicitly after setCollapsible is essential because OpenLayers otherwise restores whichever collapsed state a user last clicked manually.
How it works
aol-control-zoom,aol-control-rotateandaol-control-attributionmount the three standard controls individually so attribution options can be customised.[collapsible]="false"starts the attribution fully open without a toggle button, while(olChangeSize)invokescheckSize()on every map resize.checkSize()folds the credit into a collapsible button below 600 pixels of map width and expands it from 600 pixels upward.- Calling
setCollapsedalongsidesetCollapsibleguarantees deterministic folding even if the user previously expanded the credit manually.
Pitfalls
- OpenStreetMap’s attribution guidelines—reflected in OpenLayers marking its OSM source non-collapsible by default—only permit collapsing credits on narrow screens after user interaction or a brief initial visibility window; check your tile provider’s terms before folding credits immediately on first paint.
- When mounting
aol-control-attributionexplicitly, omit the default control bundle (or disable its built-in attribution) so the map does not render two duplicate copyright bars.
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.