Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change theme after map init #176

Open
jonnitto opened this issue Nov 7, 2024 · 3 comments
Open

Change theme after map init #176

jonnitto opened this issue Nov 7, 2024 · 3 comments

Comments

@jonnitto
Copy link

jonnitto commented Nov 7, 2024

I've build a map where the theme changes based on the interface settings (dark and light mode). Before, I had the function setDefaultStyle. But after v1 the function was removed. I've found nothing in the change log on how to achieve this functionality, on how to update the layer options afterwards. How can I do this?

@jonnitto jonnitto changed the title Change style after map init Change theme after map init Nov 7, 2024
@jonnitto
Copy link
Author

jonnitto commented Nov 7, 2024

I tried some thing like this (style is one of light, dark, white, black, grayscale

layer.options.theme = style;
layer.clearLayout();
layer.rerenderTiles();

But this doesn't work

@jonnitto
Copy link
Author

jonnitto commented Nov 7, 2024

My current workaround is following: (leafletLayer is used somewhere else to generate the layer variable)

import { leafletLayer } from "../../../node_modules/protomaps-leaflet/src/frontends/leaflet";
import { paintRules, labelRules } from "./node_modules/protomaps-leaflet/src/default_style/style";
import { themes } from ./node_modules/protomaps-leaflet/src/default_style/themes";

function setLayerStyle(layer, style) {
    const theme = themes[style];
    if (!theme) {
        return;
    }
    layer.options.theme = style;
    layer.paintRules = paintRules(theme);
    layer.labelRules = labelRules(theme);
    layer.backgroundColor = theme.background;
    layer.clearLayout();
    layer.rerenderTiles();
}

@bdon
Copy link
Member

bdon commented Nov 9, 2024

I think that is a good workaround for now. We should provide a convenience method later for setting a style from a custom object or one of the preset theme names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants