Skip to main content

alphaThis is a new frontend component. Help us improve it and give your feedback on Slack.

Map Key Plugin

Shows a key of the symbols on the map in a panel. Its entries come from the Datasets plugin's datasets and sublayers with showInKey: true, and from addSymbol.

Today, the Datasets plugin is wired up to it via a dedicated hook — datasets and sublayers configured with showInKey: true feed their styled symbols into the key panel automatically. Other plugins may hook into the key panel in future, but that isn't built yet; Datasets is currently the only integration.

The plugin holds no dataset-specific config itself — when paired with the Datasets plugin, it reads dataset styling from that plugin's registry at runtime via that hook. There's nothing to configure to make that connection; both plugins just need to be present in plugins.

NOTE

Buttons render in the shared top-left slot in the order their plugins appear in the plugins array — list datasetsPlugin before mapKeyPlugin to get Layers before Key.

ESM usage

import createDatasetsPlugin from '@defra/interactive-map/plugins/datasets'
import createMapKeyPlugin from '@defra/interactive-map/plugins/map-key'
const datasetsPlugin = createDatasetsPlugin({
datasets: [
{
id: 'my-parcels',
label: 'My parcels',
geojson: 'https://example.com/api/parcels',
showInKey: true,
showInMenu: true,
style: {
stroke: '#d4351c',
strokeWidth: 2,
fill: 'transparent'
}
}
]
})
const mapKeyPlugin = createMapKeyPlugin()
const interactiveMap = new InteractiveMap({
plugins: [datasetsPlugin, mapKeyPlugin]
})

UMD usage

Copy the entire plugins/map-key/dist/umd/ directory to /your-assets-path/plugins/map-key/umd/. The plugin uses dynamic imports, so all files in the directory must be served from the same location. Then add the script tag alongside the Datasets plugin's:

<script defer src="/your-assets-path/plugins/datasets/umd/index.js"></script>
<script defer src="/your-assets-path/plugins/map-key/umd/index.js"></script>
const datasetsPlugin = defra.datasetsPlugin({
datasets: [
{
id: 'my-parcels',
label: 'My parcels',
geojson: 'https://example.com/api/parcels',
showInKey: true,
showInMenu: true,
style: {
stroke: '#d4351c',
strokeWidth: 2,
fill: 'transparent'
}
}
]
})
const mapKeyPlugin = defra.mapKeyPlugin()
const interactiveMap = new defra.InteractiveMap('map', {
plugins: [datasetsPlugin, mapKeyPlugin]
})

NOTE

GOV.UK Prototype Kit — skip the copy step. All files are served automatically. Use this path instead:

<script defer src="/plugin-assets/%40defra%2Finteractive-map/plugins/map-key/dist/umd/index.js"></script>

Options

Options are passed to the factory function when creating the plugin.


noKeyItemText

Type:
string
Default:
'No features displayed'

Text shown in the key panel when it has no visible entries to display — for example, no datasets or sublayers configured with showInKey: true currently have visible features on the map.

createMapKeyPlugin({ noKeyItemText: 'No layers to show' })

groups

Type:
Record<string, MapKeyGroupConfig>

Display options for key groups, keyed by group id.

A dataset's group id is its groupId, or otherwise its group label in lower case with spaces replaced by hyphens. Sublayers are grouped under their parent dataset, so a dataset labelled 'Land covers' with sublayers has the group id 'land-covers'.

createMapKeyPlugin({
groups: {
'land-covers': { groupStyle: 'ramp' }
}
})

groupLabel

Type:
string

Heading shown above the group. Replaces the heading the group's entries provide.

groupStyle

Type:
'ramp'

How the group's entries are laid out. Without a groupStyle, each entry shows its symbol beside its label.

'ramp' shows the entries as touching bands, in order. Each band is filled with the entry's fill or fill pattern, and outlined in its stroke colour:

  • Horizontal — bands of equal width in a row, with each label centred below its band. Used when every label fits on one line under its band.
  • Vertical — bands stacked with each label beside its band. Used when any label doesn't fit.

The layout is chosen again when the key panel resizes, or when the text size changes.

When any entry in the group has a stroke, the bands are separated by a small gap.

A ramp suits polygon entries. If any entry in the group has a symbol or symbolSvgContent, the group is shown without a groupStyle.


manifest

Type:
Partial<PluginManifest>

Overrides for the plugin's buttons, panels and controls, matched by id. See PluginDescriptor.manifest.


Key display properties

These properties control how an entry looks in the key panel — they have no effect on how a feature renders on the map itself. Today the only way to set them is via a dataset's style object, since Datasets is the only plugin feeding this key panel.


Key symbol shape

A polygon/line entry's key symbol is inferred from its style:

  • a stroke with no fill shows as a line
  • any fill shows as a shape. For an outline-only shape, set fill: 'transparent' — it draws nothing on the map

Symbols and fill patterns show as themselves, whatever the stroke and fill.


symbolDescription

Type:
string | Record<string, string>

Accessible description of the symbol shown alongside its key entry.


Methods

Methods are called on the plugin instance after the map is ready. Entries added with them are shown alongside the entries from the Datasets plugin, and only while it's in use.


addSymbol(symbolDefinition)

Add an entry to the key, shown alongside the entries from the datasets plugin. Entries are only shown while the datasets plugin is in use.

ArgumentTypeDescription
symbolDefinition.idstringUnique identifier for the entry. Used to remove it
symbolDefinition.labelstringOptional. Text shown beside the entry's symbol
symbolDefinition.groupLabelstringOptional. Heading of the group to show the entry in. Entries with the same group are shown together
symbolDefinition.groupIdstringOptional. Id of the group to show the entry in. Defaults to groupLabel in lower case, with spaces replaced by hyphens, so the entry joins a datasets group with the same id
symbolDefinition.styleDatasetStyleOptional. How the entry's symbol looks, using the same properties as a dataset's style: for example stroke, strokeWidth, fill, fillPattern, symbol and symbolDescription
mapKeyPlugin.addSymbol({
id: 'site-boundary',
label: 'Site boundary',
style: { stroke: { outdoor: '#d4351c', dark: '#ffffff' }, strokeWidth: 2 }
})

removeSymbol(symbolDefinition)

Remove an entry added with addSymbol, matched by its id.

ArgumentTypeDescription
symbolDefinition.idstringID of the entry to remove. Its other properties are ignored
mapKeyPlugin.removeSymbol({ id: 'site-boundary' })

Events

This plugin does not emit any custom events.