jjimenezshaw / Leaflet.Control.Layers.Tree

a Tree Layers Control for Leaflet
https://jjimenezshaw.github.io/Leaflet.Control.Layers.Tree/examples/basic.html
BSD 3-Clause "New" or "Revised" License
146 stars 36 forks source link

Request - use click instead of hover to open/close the control #50

Open fisharebest opened 3 years ago

fisharebest commented 3 years ago

At present, the control is opened/closed using mouseover/mouseout events.

This makes it difficult to operate by users with some disabilities and/or assistive technology.

Can you provide an option to open/close this control using mouse-click and/or keyboard?

jjimenezshaw commented 3 years ago

This control tries to behave like "Control.Layers" control (https://leafletjs.com/reference-1.7.1.html#control-layers). As far as I know, there is no such a feature in Leaflet.

However you can capture the events in the DOM, and call the expand() and collapse() methods (probably using also the option collapsed in the control creation)

albfan commented 1 year ago

Basically:

     var layerControlTree = L.control.layers(base, tree, {collapsed: false}).addTo(map);

     var collapsed = false
      $('.leaflet-control-layers').on('click', () => {
          if (collapsed)
            layerControlTree.expand();
          else
            layerControlTree.collapse();
          collapsed = !collapsed
      });