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.

A street map of central Europe around southern Poland at zoom 6, centred on Kraków, with the zoom buttons in the top left corner and the whole OpenStreetMap credit, without its button, in the bottom right.
A screenshot of the example. Map data © OpenStreetMap contributors.

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.ts
import { 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);
  }
}

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-rotate and aol-control-attribution mount the three standard controls individually so attribution options can be customised.
  • [collapsible]="false" starts the attribution fully open without a toggle button, while (olChangeSize) invokes checkSize() 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 setCollapsed alongside setCollapsible guarantees 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-attribution explicitly, omit the default control bundle (or disable its built-in attribution) so the map does not render two duplicate copyright bars.

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.