question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Installing a light or dark theme from CSS

See original GitHub issue

Problem to solve

Now the only way to indicate a light or dark theme is to transfer a special attribute when compiling the template. This makes it difficult to mimic the theme’s settings from the user’s system settings.

Even if at the stage of compilation of the template to determine the system configuration and set the appropriate attribute, when changing the system settings need to reload the page. The theme of a webpage can not be changed dynamically.

Proposed solution

Now the class is used like a theme–dark and a theme–light. I suggest adding another class theme–system. Here is an example:

.theme--dark {
  color: #fff
}

.theme--light {
  color: #000
}

.theme--system {
  @media (prefers-color-scheme: dark) {
    @extend .theme--dark
  }
  
  @media (prefers-color-scheme: light) {
    @extend .theme--light
  }
}

// I'm not familiar with the stylus. So please forgive if the syntax is not very correct. I just want to demonstrate the idea.

This way the theme will automatically follow the user’s system settings and change without the need to reload the page.

Of course, this solution is not very well supported by browsers. However, I am sure that this will be improved in the future.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
jellehakcommented, Jan 10, 2020

vuetify.theme.dark => vuetify.framework.theme.dark

const mq = window.matchMedia('(prefers-color-scheme: dark)')

export const vuetify = new Vuetify({
  theme: { dark: mq.matches }
})

mq.addEventListener('change', (e) => {
  vuetify.framework.theme.dark = e.matches
})
6reactions
andrew-lyashenkocommented, Feb 11, 2021

@jellehak Thank you!!!

And if suddenly someone is looking for a solution for nuxt, then here it is:

// plugins/vuetify.ts

export default function ({ $vuetify }) {
  const mq = window.matchMedia('(prefers-color-scheme: dark)');
  $vuetify.theme.dark = mq.matches;

  mq.addEventListener('change', (e) => {
    $vuetify.theme.dark = e.matches;
  });
}
// nuxt.config.js

plugins: [
  { src: '~/plugins/vuetify.ts', ssr: false }
],
Read more comments on GitHub >

github_iconTop Results From Across the Web

A Complete Guide to Dark Mode on the Web - CSS-Tricks
Dark mode” is defined as a color scheme that uses light-colored text and other UI elements on a dark-colored background. Dark mode, dark...
Read more >
prefers-color-scheme - CSS: Cascading Style Sheets | MDN
The prefers-color-scheme CSS media feature is used to detect if a user has requested light or dark color themes.
Read more >
How to Implement Dark Mode with CSS and JavaScript
A simple guide for creating a dark mode theme and button for toggling between light mode and dark mode using a few lines...
Read more >
How to Enable Dark Mode on Your Website with Pure CSS?
The prefers-color-scheme CSS media feature is used to detect if the user has requested the system to use a light or dark color...
Read more >
prefers-color-scheme: Hello darkness, my old friend - web.dev
The prefers-color-scheme media feature is used to detect if the user has requested the page to use a light or dark color theme....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found