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

Add support for custom light/dark schemes #23

Merged
merged 2 commits into from
Sep 18, 2024

Conversation

sisp
Copy link
Contributor

@sisp sisp commented Mar 25, 2024

I've added support for custom light/dark schemes when using Material for MkDocs.

Material for MkDocs' default schemes are default (light) and slate (dark). But it is possible to define custom schemes with different names/identifiers. For this case, I've added options to configure these identifiers:

scheme: custom-light
scheme_dark: custom-dark

Their defaults are default and slate respectively. As before, the fallback for when Material for MkDocs is not used (or especially data-md-color-scheme is not set) is the (configured) light Vega theme.

WDYT, @timvink?

@timvink
Copy link
Owner

timvink commented Apr 13, 2024

Hi @sisp , thanks for the contribution! I was on holiday, hence the late reply.

I don't understand this PR, and I think the current options document is confusing for users.

We are now mixing the concept of vegalite themes and mkdocs material themes. I would expect the theme to be a vegalite theme (https://github.com/vega/vega-themes).

The way this plugin currently works, is that it automatically detects if you're using mkdocs-material with the dark mode, and then select the vegalite theme vega_theme_dark. The problem you've identified is that you can define custom schemes in mkdocs-material.

An alternative solution would be for this plugin to have the theme option be set to either vega_theme, vega_theme_dark or autodetect (default). The autodetect option could then infer, when using mkdocs-material, which theme to use.

Then we still need an option to specify which mkdocs-material schemes are dark, and which are light. Perhaps something like:

charts:
    scheme: autodetect
    mkdocs_material_light_themes:
        - default
    mkdocs_material_dark_themes:
        - slate

That way, it's clearer to users what's happening & we're not mixing vegalite themes with mkdocs-material themes. Thoughts?

@sisp
Copy link
Contributor Author

sisp commented Apr 17, 2024

I was implicitly mapping scheme -> vega_theme and scheme_dark -> vega_theme_dark where scheme/scheme_dark are Material for MkDocs theme names, whereas you're suggesting an explicit Vega theme selection (or auto-detection) via theme (or scheme?) and a decoupled Material for MkDocs themes config via mkdocs_material_{light,dark}_themes. I recognize the advantage of your design.

Two questions though:

  1. theme: autodetect|vega_theme|vega_theme_dark feels not so intuitive to me. How about theme: autodetect|light|dark where light means the theme set by vega_theme and dark means the theme set by vega_theme_dark?

  2. The mkdocs_material_{light,dark}_themes settings feel verbose to me. Also, your design would be extensible to more MkDocs themes other than Material for MkDocs. I realize that the current config is flat, but how about structuring it more? E.g.:

    charts:
      theme: autodetect
      integrations:
        mkdocs_material:
          themes_light:
            - default
            - light
          themes_dark:
            - slate
            - dark

    Or do you think that's too much structure for only a single scenario at the moment?

@timvink
Copy link
Owner

timvink commented May 8, 2024

I like the decoupling of the vega theme and the mkdocs material, and the nested config structure makes it clear, great idea! You are right about 'autodetect' option not feeling intuitive.

This plugin is using vega-embed, which has a theme parameter that we use, that supports themes from vega-themes. Currently we're passing either default or dark:

("vega_theme", config_options.Type(str, default="default")),
("vega_theme_dark", config_options.Type(str, default="dark")),

# Config as javascript dictionary
add_variables = f"""
<script>
var mkdocs_chart_plugin = {plugin_config}
</script>
"""

And then only overwriting it if mkdocs-material theme is present and has a different color scheme:

// mkdocs-material has a dark mode
// detect which one is being used
var theme = (document.querySelector('body').getAttribute('data-md-color-scheme') == 'slate') ? mkdocs_chart_plugin['vega_theme_dark'] : mkdocs_chart_plugin['vega_theme'];
// Render the chart
vegaEmbed(block, schema, {
actions: false,
"theme": theme,
"renderer": mkdocs_chart_plugin['vega_renderer']
});
}

So we can combine the ideas and remain backwards compatible:

charts:
  vega_theme: default # <-- existing default
  vega_theme_dark: dark # <-- existing default. We can explain we will attempt to use this theme if dark mode is preferred/detected.
  integrations:
    mkdocs_material:
      themes_light:
        - default
        - light
      themes_dark:
        - slate
        - dark

Then, we can also add a javascript detection if the users prefers dark mode on the browser or OS (https://stackoverflow.com/questions/56393880/how-do-i-detect-dark-mode-using-javascript)

@timvink
Copy link
Owner

timvink commented Aug 26, 2024

Any chance we can finish this one? :)

@sisp
Copy link
Contributor Author

sisp commented Aug 26, 2024

Absolutely! Sorry, I had forgotten about it. I'll finish it tomorrow evening.

@sisp sisp force-pushed the feat/custom-schemes branch from 82b4b03 to af21c74 Compare August 27, 2024 18:42
@sisp
Copy link
Contributor Author

sisp commented Aug 27, 2024

I've updated the PR to add explicit mkdocs-material integration and also user's preferred color scheme detection as discussed above. I split the two changes into separate commits to separate concerns.

The current implementation tries to derive the theme in the following order:

  1. Try to detect mkdocs-material's active color scheme.
  2. Try to detect the user's preferred color scheme on the browser or OS.
  3. Fall back to the light theme.

When trying to detect mkdocs-material's active color scheme, the light names are actually ignored. Should we rethink the design? 🤔

@timvink
Copy link
Owner

timvink commented Sep 18, 2024

This is epic! Really well done!

When trying to detect mkdocs-material's active color scheme, the light names are actually ignored. Should we rethink the design?

I noticed. Actually vega_theme was implicitly used as vega_theme_light. I renamed it, and deprecated vega_theme. Works perfectly for me now.

@timvink timvink merged commit 971cb27 into timvink:main Sep 18, 2024
12 checks passed
@timvink
Copy link
Owner

timvink commented Sep 18, 2024

I did that here btw: #25

Will release soon

@sisp sisp deleted the feat/custom-schemes branch September 18, 2024 14:24
@sisp
Copy link
Contributor Author

sisp commented Sep 18, 2024

Fantastic! Thanks for the amazing feedback and guidance! 🙏

@timvink
Copy link
Owner

timvink commented Oct 22, 2024

Sorry it took a while. Also updated the README ! Now released to pypi

@sisp
Copy link
Contributor Author

sisp commented Oct 22, 2024

No problem. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants