npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@russss/maplibregl-layer-switcher

v2.1.0

Published

Layer switcher for Maplibre GL JS based on layer prefixes

Downloads

261

Readme

MapLibre GL Layer Switcher

This is a layer switcher component for MapLibre GL JS, which I use on OpenInfraMap. Layers are selected by the string prefix of the layer name.

This package also includes a modified URL hash component which allows a compact representation of the enabled layers to be added to the URL hash.

This package was formerly called mapboxgl-layer-switcher but after renaming it does not support Mapbox GL.

Use

These steps assume you're using Webpack with css-loader. First, import the layer switcher component:

import { LayerSwitcher } from '@russss/maplibregl-layer-switcher'

Assuming you are using a build system which supports CSS imports, the CSS should be imported automatically.

Now define your layers. Each layer has a short identifier which will be used in the URL hash, a prefix for the style layers to match, and a boolean for whether it's enabled by default:

const layer_switcher = new LayerSwitcher([
  new LayerGroup('Artificial', [
    new Layer('b', 'Borders', 'boundary'),
    new Layer('l', 'Landuse', 'landuse_', true),
    new Layer('r', 'Roads', 'road_', true),
    new Layer('B', 'Buildings', 'building', true)
  ]),
  new LayerGroup('Natural', [new Layer('w', 'Water', 'water', true)]),
  new Layer('L', 'Labels', 'label', true)
])

It is also possible to define groups of radio buttons. If you fill in a group id after the prefix parameter this layer will be part of that radio button group. Set the last parameter as enabled for one layer in a radio button group.

const layer_switcher = new LayerSwitcher([
  new Layer('str', 'Streets', 'streets', 'backgrounds'), // Radio button 'backgrounds' group
  new LayerGroup('Satellite', [ // Radio button 'backgrounds' group
    new Layer('srgb', 'RGB', 'sat-rgb', 'backgrounds')
    new Layer('sir', 'IR', 'sat-ir', 'backgrounds', true)
  ]),
  new LayerGroup('Artificial', [ // Checkbox group
    new Layer('b', 'Borders', 'boundary'),
    new Layer('l', 'Landuse', 'landuse_', true),
    new Layer('r', 'Roads', 'road_', true),
    new Layer('B', 'Buildings', 'building', true)
  ]),
  new LayerGroup('Natural', [ // Radio button 'natural' group
    new Layer('w', 'Water', 'water', 'natural')
    new Layer('t', 'Trees', 'trees', 'natural', true)
  ]),
])

Adding the layer switcher to the map after it's been initialised will result in the hidden layers being briefly shown. To avoid this, we need to load the style into JS first (I usually bundle it with the rest of my JS), and call the setInitialVisibility method on the loaded style object.

const style = layer_switcher.setInitialVisibility(style) //...load map style JSON into an object

Now we can instantiate our map and add the layer switcher to it:

const map = new maplibregl.Map({ style: style, container: 'map' })
map.addControl(layer_switcher)

URL Hash

To include layers in the URL hash, first import and create the URLHash object, passing in your LayerSwitcher:

import { URLHash } from '@russss/maplibregl-layer-switcher'
// ...
const url_hash = new URLHash(layer_switcher)

When creating the map, you need to call url_hash.init, passing in your default map options. The URLHash class will update the zoom and center options if they're provided in the URL hash. Then call url_hash.enable on the map:

var map = new maplibregl.Map(
  url_hash.init({
    container: 'map',
    style: style,
    minZoom: 2,
    maxZoom: 17.9,
    center: [12, 26]
  })
)

url_hash.enable(map)

The URLHash object doesn't currently support tilt and rotation parameters.

Development

Vite can be used to test the library (using index.html and index.ts in the root directory) but isn't used to build the published version.