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

maplibre-gl-style-flipper

v1.0.9

Published

A custom control to switch between different map styles in MapLibre GL JS

Readme

MapLibre GL Style Flipper npm version

A custom control to switch between different map styles in MapLibre GL JS.

DEMO

Features

  • Easy Integration: Add a style switcher control to your MapLibre GL JS map with just a few lines of code.
  • Customizable Styles: Define your own map styles with names, images, and URLs.
  • Responsive Design: Buttons adapt to different screen sizes.
  • Callback Support: Execute custom logic when the map style changes.

Installation

Install the package via npm:

npm install maplibre-gl-style-flipper

Or include it directly via a CDN:

<script src="https://unpkg.com/maplibre-gl-style-flipper@latest/dist/style-flipper-control.js"></script>

Usage

Basic Example

Here’s how to use the StyleFlipperControl in a simple HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MapLibre GL Style Flipper</title>
    <script src="https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.js"></script>
    <link href="https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.css" rel="stylesheet" />
    <style>
        body, html {
            margin: 0;
            padding: 0;
            height: 100%;
        }
        #map {
            height: 100%;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="map"></div>
    <script type="module">
        import StyleFlipperControl from "maplibre-gl-style-flipper";

        // Define map styles
        const mapStyles = {
            "carto-positron": {
                code: "carto-positron",
                url: "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
                image: "https://carto.com/help/images/building-maps/basemaps/positron_labels.png",
            },
            "carto-dark": {
                code: "carto-dark",
                url: "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json",
                image: "https://carto.com/help/images/building-maps/basemaps/dark_labels.png",
            },
            "carto-voyager": {
                code: "carto-voyager",
                url: "https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json",
                image: "https://carto.com/help/images/building-maps/basemaps/voyager_labels.png",
            },
        };

        // Initialize the map
        const map = new maplibregl.Map({
            container: "map",
            style: mapStyles["carto-positron"].url, // Default style
            center: [0, 0], // Initial center
            zoom: 1, // Initial zoom
        });

        // Add navigation control
        map.addControl(new maplibregl.NavigationControl());

        // Create an instance of StyleFlipperControl
        const styleFlipperControl = new StyleFlipperControl(mapStyles);

        // Set the initial style code
        styleFlipperControl.setCurrentStyleCode("carto-positron");

        // Add the control to the map
        map.addControl(styleFlipperControl, "bottom-left");
    </script>
</body>
</html>

Explanation

  1. Map Styles:

    • The mapStyles object contains the styles you want to switch between. Each style has:
      • code: A unique identifier for the style.
      • url: The URL of the MapLibre GL style.
      • image: The path to an image that represents the style (used in the control buttons).
  2. Map Initialization:

    • The map is initialized with a default style (carto-positron in this case).
  3. Style Flipper Control:

    • The StyleFlipperControl is created with the mapStyles object and an optional callback function that triggers when the style changes.
    • The control is added to the map using map.addControl().
  4. Customization:

    • You can customize the position of the control by changing the second argument of map.addControl() (e.g., "top-right", "bottom-left").

API Reference

StyleFlipperControl

Constructor

new StyleFlipperControl(styles, onStyleChange);

  • styles: An object containing map styles. Each key is a style name, and the value is an object with:
    • code: A unique identifier for the style.
    • url: The URL of the MapLibre GL style.
    • image: The path to an image that represents the style.
  • onStyleChange (optional): A callback function that is triggered when the style changes. It receives two arguments:
    • styleClass: The name of the selected style.
    • styleCode: The code of the selected style.

Methods

  • setCurrentStyleCode(code):

    • Sets the current style code and highlights the corresponding button.
    • code: The code of the style to set as active.
  • saveCustomSourcesAndLayers():

    • Saves the current custom sources and layers.
  • restoreCustomSourcesAndLayers():

    • Restores the saved custom sources and layers.

Customization

CSS Styling

You can customize the appearance of the control by overriding the default CSS. Here’s an example:

<style>
.style-flipper-control {
    background: rgba(255, 255, 255, 0.8);
    border-radius: 8px;
    padding: 8px;
}

.style-flipper-control .map-style {
    width: 40px;
    height: 40px;
    border-radius: 4px;
}

.style-flipper-control .map-style.active {
    border: 2px solid #ff6b6b;
}
</style>

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue.


License

This project is licensed under the MIT License. See the LICENSE file for details.