Skip to main content

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

Menu Plugin

Shows groups of radios and checkboxes that control which datasets are shown, and keeps the choices in the URL.

The groups are shown in a Layers panel. Datasets are shown or hidden by the choices through their own visibleWhen. Keeping the choices in the URL means they survive a reload or a shared link.

NOTE

This plugin is in early development. Its options and behaviour may change.

Usage

import createDatasetsPlugin from '@defra/interactive-map/plugins/datasets'
import createMenuPlugin from '@defra/interactive-map/plugins/menu'
const menuPlugin = createMenuPlugin({
menu: [
{
id: 'period',
label: 'Period',
type: 'radio',
value: 'roman',
items: [
{ id: 'roman', label: 'Roman' },
{ id: 'medieval', label: 'Medieval' }
]
}
]
})
const datasetsPlugin = createDatasetsPlugin({
datasets: [
{
id: 'roman-sites',
geojson: romanSites,
visibleWhen: { menu: { period: ['roman'] } }
}
]
})
const interactiveMap = new InteractiveMap('map', {
plugins: [menuPlugin, datasetsPlugin]
})

For UMD, add plugins/menu/dist/umd/index.js and call defra.menuPlugin().

Options

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


Type:
MenuGroup[] (required)

The menu's groups of options. Datasets can be shown or hidden by the selected options with their own visibleWhen.

PropertyTypeDescription
idstringRequired. Unique identifier for the group, also used as its URL parameter
labelstringThe group's legend
type'radio' | 'checkbox'Required. Whether one option (radio) or any number of options (checkbox) can be selected
valuestringRadio groups only. The selected option's id, when the URL doesn't set it
itemsMenuItem[]Required. The group's options
visibleWhenboolean | { menu?: Record<string, string[]> }When the group is shown. A hidden group's URL parameter is removed
PropertyTypeDescription
idstringRequired. The option's value: a radio group's value, or a checkbox's ID
labelstringRequired. Text shown beside the option
checkedbooleanCheckbox groups only. Whether the checkbox starts checked, when the URL doesn't set the group
visibleWhenboolean | { menu?: Record<string, string[]> }When the option is shown
Type:
boolean | { menu?: Record<string, string[]> }

true or false, or { menu: { groupId: values } } to show it only while every listed group's selected value is one of its values.


manifest

Type:
Partial<PluginManifest>

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


The menu's state maps:

  • each radio group's id to the selected option's id
  • each checkbox's id to true or false

It's read from the URL when the map loads, falling back to each group's value or each checkbox's checked. Each change updates the URL without reloading the page: a radio group's parameter holds its selected id, and a checkbox group's holds the checked ids, comma separated.

?period=medieval&features=walls,ditches

A dataset's visibleWhen menu conditions are checked against this state.

Methods

This plugin does not expose any public methods.

Events

Subscribe to events using interactiveMap.on().


Emitted whenever the menu state changes: when it's first read from the URL as the map loads, and when the user changes a choice.

Payload:
the menu state, as described in Menu state and the URL
interactiveMap.on('menu:changed', (menuState) => {
console.log('Period:', menuState.period)
})