Coder Social home page Coder Social logo

Comments (3)

el avatar el commented on July 19, 2024

Hello,

the active state is just a css class ".active".

You can just set it to whatever you like with css.

from style-switcher.

mmnoo avatar mmnoo commented on July 19, 2024

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.

mmnoo avatar mmnoo commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.