Comments (3)
Hello,
the active state is just a css class ".active".
You can just set it to whatever you like with css.
from style-switcher.
I ended up doing that, and it blew up the style switcher. I cant rule out the complicated code base I am working in though (within the constraints of my time box anyway).
Instead of manually setting the style on the map instance and then setting css classes to make the switcher have the right style in bold, my current approach is to hack into the private properties of the style switcher instance to grab the target style button and click it with JavaScript. Very not ideal, and definitely not durable against any internal changes you guys may make, but it seems to be working without bugs currently!
from style-switcher.
Not exactly code I take pride in, but sometimes you need to do gross things quickly :)
const clickOnStyleSwitcherTargetStyleHack = ({ mapInstance, styleToSelect }: {
styleToSelect: string;
mapInstance: IMapInstance;
}) => {
// Instead of manually changing the map instance style to match another map instance, we click on the other instance's style switcher target style
// This ensures the style is bolded in both map instances, and also that subsequent styles render property (when we have a style switcher on both map sides)
// this hack may break if the internals of the style switcher or mapbox change. Use with caution.
// @ts-expect-error - this is definitely a dirty hack, so we are using private properties
const styleSwitcherControl = mapInstance._controls.find((control) => control instanceof MapboxStyleSwitcherControl);
const styleButtons = styleSwitcherControl.mapStyleContainer.children;
for (let index = 0; index < styleButtons.length; index++) {
const button = styleButtons[index];
const isButtonForTheTargetStyle = button.getAttribute('data-uri') === `"${styleToSelect}"`; // getAttribute returns a string including quotes
if (isButtonForTheTargetStyle) {
button.click();
}
}
};
from style-switcher.
Related Issues (20)
- Can't use spaces in layer titles
- Allow to choose default style HOT 1
- Unable to use mapbox-gl-controls with node/gulp. HOT 6
- Events HOT 4
- Add MapLibre support HOT 4
- Use without NPM? HOT 2
- change icon style-switcher HOT 1
- Projection switching HOT 1
- Hide list when clicken selected style option HOT 3
- Current Map Layers sources (polygons) disappears when an map style is selected HOT 5
- Support locally-defined styles
- closeModal immediately executed in shadow DOM context
- Question.. Switch styles and maintain state? HOT 13
- Creating a projection switcher without duplicating all this code
- Source map problem with npm 8.12.0 HOT 2
- If the defaultStyle is one of the available styles options, it should get the active status. HOT 3
- No code for onRemove method HOT 1
- support layer toggle method of switching styles HOT 2
- Preserve existing layers when switching, or add beforeChange and onChange callback options HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from style-switcher.