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] Any way to get current theme (dark/light) in a (customed) component?

See original GitHub issue

First, thanks for this awesome Vue plugin.

I drew some glyph icons (.ttf; surely for those which cannot found in FontAwesome or MaterialDesign) for my project. So I’m trying to write a Vue component to wrap my icon, which can aware currently activated theme (dark/light) and change color for it.

Basically, for consistence, I wish it can works just like v-icon, with same API, maybe…

However, after hours of research, I didn’t find any reliable way/API/mechanism to get the activated theme of a specific level of a HTML/Component hierarchy, for coloring the glyph icons I made.

I guess the possible way can be (may be silly, I’m not familiar the internal implementation of Vuetify):

  1. event from parent (e.g. v-app ?) to notify all children (oops, this.$broadcast seems only available in Vue1.x; not available in Vue2.x)
  2. Accessible in this.$vuetify?
  3. A CSS class which Vuetify will automatically change its color ?
  4. A HTML attribute directive which Vuetify will automatically add CSS classes such as .theme--dark to the HTML element, then user can decide the color in CSS (e.g. .my-glyphicon.theme--dark { color: #fff; })?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
emjayesscommented, Jan 25, 2019

I was just trying to understand how to programmatically manipulate the theme, and themeable components, first as an exercise to make a theme switcher for the user…

Old habits die hard, and so first crack at it was this ah, rather jquery-esque (walk the dom, manipulate/toggle classes) and not so vue-esque (or reactive) approach:

<template>
  <v-switch @change="switchTheme"
    hint="switch theme"
    append-icon="palette"
  ></v-switch>
</template>

<script>
export default {
  inject: ['theme'],

  /**
   * use either this.theme.isDark or this.$vuetify.dark
   */

  methods: {
    switchTheme() {
      const switchOff = this.$vuetify.dark ? 'theme--dark' : 'theme--light'
        , switchOn = this.$vuetify.dark ? 'theme--light' : 'theme--dark'
        , themed = document.getElementsByClassName( switchOff )

      Array.from(themed).forEach( (el) => {
        el.classList.remove( switchOff )
        el.classList.add( switchOn )
      })

      this.$vuetify.dark = ! this.$vuetify.dark
      this.theme.isDark = ! this.theme.isDark
    }
  }
}
</script>

theme-switcher

Aaaand right about when I got that working, I found this theme-switching codepen, maybe shed a tear, and went to bed. 😂


👍 Point stands tho, that I had trouble finding documentation or examples of how to work with the active and available themes programmatically… at runtime.

7reactions
KaelWDcommented, Oct 5, 2018

VIcon uses this: https://github.com/vuetifyjs/vuetify/blob/dev/src/mixins/themeable.ts#L70

If you just want to consume the current theme, simply use inject: ['theme'] and this.theme.isDark. We could probably expose a renderless component to make this easier though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing Dark Mode In React Apps Using styled ...
To implement dark mode, we need to create four different components. Theme This contains the color properties of our light and dark themes....
Read more >
A Dark Mode Toggle with React and ThemeProvider
This media feature is used to detect if the user has requested the page to use a light or dark color theme based...
Read more >
Dark theme - Android Developers
Your themes and styles should avoid hard-coded colors or icons intended for use under a light theme. You should use theme attributes (preferred)...
Read more >
Automatic Dark Mode Detection in Angular Material - Philipp Kief
This article showed how to detect the Dark Mode in an Angular application and how the color scheme of all components can be...
Read more >
Is there an API to detect which theme the OS is using - dark or ...
In order to manage the dark theme, you must first use the latest version of the Material Components library: "com.google.android.material: ...
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