Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

leaflet-sidebar-v2

nickpeihl11kMIT3.2.3TypeScript support: included

A responsive sidebar just for Leaflet

gis, leaflet, control, map

readme

leaflet-sidebar-v2

A responsive sidebar for Leaflet. A fork of sidebar-v2 that only contains the necessary ingredients for use in Leaflet and provides enhanced functionality.

Demo

Hint: There's a Angular wrapper available at https://github.com/runette/ngx-leaflet-sidebar

Why the Fork?

  • JS API for panel modification
  • "autopan" feature, moving the map content next to the sidebar content
  • only supports leaflet (0.x and 1.x) to maintain a smaller codebase and support more features
  • compatibility with bootstrap (no generic .sidebar class)
  • provide a npm package leaflet-sidebar-v2 with main and style fields in package.json

Examples

in examples folder, available live at https://noerw.github.io/leaflet-sidebar-v2/examples

Usage

API

leaflet-sidebar-v2 provides a simple API to dynamically modify the sidebar. All functions may be chained.

creation

The parameters object is fully optional. The default values are shown:

var sidebar = L.control.sidebar({
    autopan: false,       // whether to maintain the centered map point when opening the sidebar
    closeButton: true,    // whether t add a close button to the panes
    container: 'sidebar', // the DOM container or #ID of a predefined sidebar container that should be used
    position: 'left',     // left or right
}).addTo(map);

modification

/* add a new panel */
var panelContent = {
    id: 'userinfo',                     // UID, used to access the panel
    tab: '<i class="fa fa-gear"></i>',  // content can be passed as HTML string,
    pane: someDomNode.innerHTML,        // DOM elements can be passed, too
    title: 'Your Profile',              // an optional pane header
    position: 'bottom'                  // optional vertical alignment, defaults to 'top'
};
sidebar.addPanel(panelContent);

/* add an external link */
sidebar.addPanel({
    id: 'ghlink',
    tab: '<i class="fa fa-github"></i>',
    button: 'https://github.com/noerw/leaflet-sidebar-v2',
});

/* add a button with click listener */
sidebar.addPanel({
    id: 'click',
    tab: '<i class="fa fa-info"></i>',
    button: function (event) { console.log(event); }
});

/* remove a panel */
sidebar.removePanel('userinfo');

/* en- / disable a panel */
sidebar.disablePanel('userinfo');
sidebar.enablePanel('userinfo');

open / close / show content

/* open a panel */
sidebar.open('userinfo');

/* close the sidebar */
sidebar.close();

remove sidebar

/* remove the sidebar (keeping the sidebar container) */
sidebar.remove();
sidebar.removeFrom(map); // leaflet 0.x

/* to clear the sidebar state, remove the container reference */
sidebar._container = null

markup

If you use the sidebar with static content only, you can predefine content in HTML:

<div id="sidebar" class="leaflet-sidebar collapsed">
    <!-- Nav tabs -->
    <div class="leaflet-sidebar-tabs">
        <ul role="tablist"> <!-- top aligned tabs -->
            <li><a href="#home" role="tab"><i class="fa fa-bars"></i></a></li>
            <li class="disabled"><a href="#messages" role="tab"><i class="fa fa-envelope"></i></a></li>
            <li><a href="#profile" role="tab"><i class="fa fa-user"></i></a></li>
        </ul>

        <ul role="tablist"> <!-- bottom aligned tabs -->
            <li><a href="#settings" role="tab"><i class="fa fa-gear"></i></a></li>
        </ul>
    </div>

    <!-- Tab panes -->
    <div class="leaflet-sidebar-content">
        <div class="leaflet-sidebar-pane" id="home">
            <h1 class="leaflet-sidebar-header">
                sidebar-v2
                <div class="leaflet-sidebar-close"><i class="fa fa-caret-left"></i></div>
            </h1>
            <p>A responsive sidebar for mapping libraries</p>
        </div>

        <div class="leaflet-sidebar-pane" id="messages">
            <h1 class="leaflet-sidebar-header">Messages<div class="leaflet-sidebar-close"><i class="fa fa-caret-left"></i></div></h1>
        </div>

        <div class="leaflet-sidebar-pane" id="profile">
            <h1 class="leaflet-sidebar-header">Profile<div class="leaflet-sidebar-close"><i class="fa fa-caret-left"></i></div></h1>
        </div>
    </div>
</div>

You still need to initialize the sidebar (see API.creation)

Events

The sidebar fires 3 types of events: opening, closing, and content. The latter has a payload including the id of the activated content div.

You can listen for them like this:

sidebar.on('content', function(e) {
    // e.id contains the id of the opened panel
})

License

leaflet-sidebar-v2 is free software, and may be redistributed under the MIT license.

changelog

Changelog

This project adheres to semantic versioning.

v3.2.3 (2020-05-18)

  • fix IE & android 4 compatibility (#19, #54)

v3.2.2 (2019-12-17)

  • fix angular builds (#45)

v3.2.1 (2019-08-09)

  • zoom panel will be not hidden when mobile (#39)
  • fix border for right-aligned (#39)
  • simplify scss (#39)

v3.2.0 (2019-07-10)

  • leaflet v1.5.0 compatibility
  • throw if tab with given ID is not found
  • fix click event removal

v3.1.1 (2019-03-10)

  • update minified version

v3.1.0 (2019-03-10)

  • restore container state in onAdd() when added previously
  • don't pass right-click events to map container (#33)

v3.0.7 (2019-02-19)

  • fix in removePanel() (#32)

v3.0.6 (2019-01-11)

  • fix regression from v3.0.5

v3.0.5 (2019-01-11)

  • apply container ID if specified in constructor options and not existing (#27)
  • fix wrongly initialized caret for position: right (#28)

v3.0.4 (2018-12-10)

  • fix type definition (#24)

v3.0.3 (2018-11-25)

  • fix repeated removal of panels (#24)

v3.0.2 (2018-01-20)

  • support click listeners in html-markup-only variant (#17)

v3.0.1 (2018-01-19)

  • add typescript type definitions (#12)

v3.0.0 (2017-12-13)

  • rename sidebar options.id to options.container, accepting a DOM Node or ID
  • fix sidebar remove()

breaking changes

  • prefixed all CSS classes with leaflet- to avoid conflicts with bootstrap (#13)
  • remove() now destroys the sidebar container for consistency with other leaflet controls

v2.0.0 (2017-11-13)

  • added title option for addPanel(): autocreates a header with close button
  • added closeButton option for sidebar constructor: defaults to true

breaking changes

  • sidebar div is now moved into the map container, may break your layout (#8)
  • sidebar constructor only accepts option object, id param moved to opts.id

v1.1.1 (2017-11-13)

  • shrink package size

v1.1.0 (2017-11-13)

  • add autopan feature
  • allow to add buttons with URLs or click handlers as tabs
  • restructure examples & readme

v1.0.2 (2017-11-13)

  • fix build files in package

v1.0.1 (2017-11-13)

  • hide scrollbar when collapsed
  • add support for L.Evented for leaflet 1.2.0+
  • fix chrome css bug
  • all URLs in tab href

v1.0.0 (2016-10-31)

Added

  • New API functions, addPanel, removePanel, enablePanel, disablePanel

Changed

  • Only compatible with Leaflet

Removed

  • Remove OpenLayers support
  • Remove Google Maps support

v0.3.0 (2016-01-19)

  • ol2: move scale line control too when sidebar opens/closes
  • hide scrollbars when collapsed (#21)
  • fix tab clicking on devices with touch screen and mouse (#34)
  • new .sidebar-header CSS class for styled headings
  • new .sidebar-close CSS class for close buttons in headings
  • fix broken Google Maps code (until Google changes things again...)
  • allow .disabled on <li> elements in .sidebar-tabs element
  • allow second tabbar at the bottom
  • new position: 'right' option

v0.2.1 (2014-09-29)

  • ol2, ol3: fixed sidebar content scrolling

v0.2.0 (2014-09-29)

  • jQuery API and events

v0.1.0 (2014-09-12)

  • first beta release