nb-theme
This is a theme manager plugin for Vue.js 3+.
Installation
yarn add @vlalg-nimbus/nb-theme
Usage
Attention to the imported theme css file
import { createApp } from 'vue'
import App from './App.vue'
import ThemePlugin from '@vlalg-nimbus/nb-theme'
// Your css files
import './style/style.css';
import "./style/themes.css"; // CSS file
const app = createApp(App)
app.use(ThemePlugin, {
theme: 'dark', // start default theme
dev: true, // Console theme changes
})
app.mount('#app')
The only required option when creating is theme, the options (dev and texts) are optional.
To use it, simply call the gets and sets in your files using Vue inject.
Below I provide the names of the gets and sets.
<template>
<button @click="updateTheme('dark')">Dark theme</button>
<button @click="updateTheme('test')">Test theme</button>
<button @click="updateTheme('default')">Default theme</button>
<button @click="updateTheme('other')">Others theme</button>
<p>getTheme (theme name): {{ getTheme }}</p>
<p>getThemeVariableValue: {{ getThemeVariableValue('--bg-color') }}</p>
<p>getThemeText: {{ getThemeText('subtitle') }}</p>
</template>
<script setup>
import { inject } from 'vue'
const getTheme = inject('$getTheme')
const changeTheme = inject('$changeTheme')
const getThemeVariableValue = inject('$getThemeVariableValue')
const getThemeText = inject('$getThemeText')
const updateTheme = (newTheme) => {
if (changeTheme) changeTheme(newTheme)
}
</script>
css
First of all, it is required to define :root with the names of the variables and their values.
For each theme that will be used, create a group in this css file defining the values of the variables. Pay attention to the names of the variables, if they do not exist in a theme, they will end up taking the value of :root.
Example:
:root {
--bg-color: #ffffff;
--text-color: #000000;
}
[data-theme='default'] {
--bg-color: #04eeff;
--text-color: #000000;
}
[data-theme='dark'] {
--bg-color: #000000;
--text-color: #ffffff;
}
[data-theme='other'] {
--bg-color: tomato;
--text-color: #ffffff;
}
If you do not follow the above pattern it will not work as expected.
Typescript
The lib provides automatic typing
Vue
import { createApp } from 'vue'
import App from './App.vue'
import ThemePlugin from '@vlalg-nimbus/nb-theme'
import type { ThemeTextMap, ThemeOptions } from '@vlalg-nimbus/nb-theme'
const texts: ThemeTextMap = {
light: {
welcome: 'Bem-vindo',
goodbye: 'Até logo'
},
dark: {
welcome: 'Welcome',
goodbye: 'See you'
}
}
const themeOptions: ThemeOptions = {
theme: 'dark',
dev: true,
texts
}
const app = createApp(App)
app.use(ThemePlugin, themeOptions)
app.mount('#app');
Nuxt
import { createApp } from 'vue'
import App from './App.vue'
import ThemePlugin from '@vlalg-nimbus/nb-theme'
import type { ThemeTextMap, ThemeOptions } from '@vlalg-nimbus/nb-theme'
const texts: ThemeTextMap = {
light: {
welcome: 'Bem-vindo',
goodbye: 'Até logo'
},
dark: {
welcome: 'Welcome',
goodbye: 'See you'
}
}
const themeOptions: ThemeOptions = {
theme: 'dark',
dev: true,
texts
}
const app = createApp(App)
app.use(ThemePlugin, themeOptions)
app.mount('#app');
Preview & Playground
Select the component you want to edit/test
Options
Items with an (*) mean they are required
| name | Value type | Default | Description |
|---|---|---|---|
| theme (*) | String | Sets default thema name | |
| dev | Boolean | false | Defines whether to print to the console when a theme changes |
| texts | Object | {} | Defines texts that should change when changing the theme, as if it were an I18N, but for a theme. |
The texts option accepts a object in this format:
{
themeNameOne: {
welcome: 'Bem-vindo',
goodbye: 'Até logo'
},
themeNameTwo: {
welcome: 'Welcome',
goodbye: 'See you'
},
...
}
Gets
| name | Description |
|---|---|
$getTheme | Get the current theme name |
$getThemeVariableValue | Gets the value of a variable in css by theme. If not found, returns an empty value. |
$getThemeText | Gets the value of a variable defined in the theme texts. If not found, returns an empty value. |
Sets
| name | Description |
|---|---|
$changeTheme | Change the theme that is active in the system |
