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

Set CSS value in HTML to reflect current theme #192

Open
vsajip opened this issue Jul 1, 2024 · 8 comments
Open

Set CSS value in HTML to reflect current theme #192

vsajip opened this issue Jul 1, 2024 · 8 comments

Comments

@vsajip
Copy link
Member

vsajip commented Jul 1, 2024

See this page, where the anchor points to an SVG. If you ensure that there's no theme in local storage for this page, then if the browser settings are used to switch between dark and light modes (I'm using Firefox), the SVG is styled appropriately to display correctly. However, if I use the theme selector on the page to select dark mode when the browser is set to light mode, the SVG does not display correctly.

The reason is that I style for light mode and use a media query to override some styles, using @media (prefers-color-scheme: dark). However, when the theme selector is used to select dark mode, then some dark-mode CSS is pulled in using the activateTheme() JS function, but from what I can tell this leaves no trace in the markup that we're now in dark mode. If there were e.g. a dark-theme class added to the <body> tag when the page is in dark mode, then I could duplicate the styling using an appropriate selector. My complete styling of the SVG is:

    svg {
      background-color: transparent !important;
    }
    line {
      stroke: #000000;
      fill: none;
      stroke-opacity: 1;
    }
    polygon, rect {
      fill: none;
      stroke: #000000;
      fill-opacity: 1;
      stroke-opacity: 1;
    }
    polygon.filled {
      fill: #ff0000;
    }
    polyline {
      fill: none;
      stroke-opacity: 1;
      stroke: #000000;
    }
    text {
      fill: #000000;
      fill-opacity: 1;
      stroke: none;
      font-family: sans-serif;
      font-style: normal;
      font-weight: normal;
      text-anchor: start;
    }
    @media (prefers-color-scheme: dark) { /* could duplicate this styling if body.dark-theme */
      polygon, rect, polyline, line {
        stroke: #ffffff;
      }
      text {
        fill: #ffffff;
      }
    }

If there is another approach I could use to achieve the desired result (proper styling of the SVG in all cases), please let me know. Otherwise, would you consider adding dark-theme class to the body or other container tag so that I could try and achieve the desired result? Or have I missed somewhere where such a class has been added?

@vsajip vsajip added the question label Jul 1, 2024
@vsajip
Copy link
Member Author

vsajip commented Jul 3, 2024

I got this working by overwriting activateTheme with my own function, see here. The result can be previewed here. It would still be good to have the function of setting/unsetting body.{light,dark}-theme in this theme.

@hugovk hugovk changed the title SVG created to work in dark and light mode works as expected when using the browser to set the mode, but not when using the theme selector Set CSS value in HTML to reflect current theme Jul 21, 2024
@hugovk
Copy link
Member

hugovk commented Jul 21, 2024

Yeah, https://peps.python.org/ has a data-colour_scheme attribute on the html element that changes between dark, light and auto based on the theme selection.

I'd welcome a PR to do something like this here too.

@vsajip
Copy link
Member Author

vsajip commented Jul 22, 2024

Do you want it to match the peps.python.org behaviour exactly? Or is what I've implemented (using the body tag, and the attribute has a -theme suffix) OK?

@hugovk
Copy link
Member

hugovk commented Jul 22, 2024

We don't need to copy peps.python.org exactly.

Checking a couple of others:

Furo has a <body data-theme="dark"> which toggles the three states.

PyData Sphinx Theme has the same thing, but it only togges light and dark. It also has a data-mode that toggles the thee states.

Hmm, shall we follow Furo?

@pradyunsg
Copy link
Member

pradyunsg commented Jul 22, 2024

I'd suggest following PyData Sphinx Theme honestly -- Furo's approach works because of the weird stacking it has due to the legacy of being a light-only theme that evolved a dark mode and requires... shenanigans (see https://github.com/pradyunsg/furo/blob/main/src/furo/assets/styles/base/_theme.sass which includes dark colors twice).

PyData's approach allows content to only care about the current display theme, with keeping relevant "what mode is selected" state separately, which would work better for the usecases like showing the dark mode SVG when content is dark.

@ONKARBH
Copy link

ONKARBH commented Jan 21, 2025

"Hi, team! I’ve reviewed the discussion, and it seems using a data-attribute for tracking theme modes is a more robust solution. Can you provide a small code snippet or example for implementing dynamic dark mode functionality with data-color_scheme or similar attributes? This will help me ensure a consistent and standards-compliant integration in my contributions. Thank you!"

Here as the sample code:

<html lang="en" data-color_scheme="light">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dark Mode Example</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      transition: background-color 0.3s, color 0.3s;
    }
    [data-color_scheme="light"] body {
      background-color: #ffffff;
      color: #000000;
    }
    [data-color_scheme="dark"] body {
      background-color: #000000;
      color: #ffffff;
    }
  </style>
</head>
<body>
  <h1>Dark Mode Example</h1>
  <button onclick="toggleTheme()">Toggle Dark Mode</button>
  <script>
    function toggleTheme() {
      const html = document.documentElement;
      const currentTheme = html.getAttribute('data-color_scheme');
      const newTheme = currentTheme === 'light' ? 'dark' : 'light';
      html.setAttribute('data-color_scheme', newTheme);
    }
  </script>
</body>
</html>

@Wulian233
Copy link

This account is definitely a bit suspicious🤔💀

@ONKARBH
Copy link

ONKARBH commented Jan 21, 2025

@Wulian233
Sir, I am new to this field, and I am slowly learning all these technologies. I don’t have much knowledge about them, and there’s no one to guide me, especially like you do. Most importantly, I don’t have a strong command of the English language. I found your projects very approachable, and they are extremely helpful for beginners like me.

I didn’t know earlier how to push the changes made locally to the previous pull request, but I learned it under your guidance. You had assigned me work related to the sidebar issue, but I couldn’t complete it as per your expectations. However, I have now improved the sidebar in my local project.

Your guidance has been immensely helpful to beginners like me. I apologize for taking up your time.

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

No branches or pull requests

5 participants