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

fix(docs): fix theme switch on SSG #3435

Merged
merged 7 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"serve": "yarn workspace vuestic-ui serve",
"serve:book": "yarn workspace vuestic-ui serve",
"start:docs:ci": "yarn workspace docs start:ci",
"serve:production": "yarn workspace vuestic-ui serve:production",
"lint": "yarn workspace vuestic-ui lint",
"build": "yarn workspace vuestic-ui build",
Expand Down
27 changes: 22 additions & 5 deletions packages/docs/layouts/default.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<div class="docs-layout">
<div class="docs-layout" :key="currentPresetName + isMounted">
<div v-if="!isMounted" class="docs-layout__loader" />
<div
ref="header"
class="docs-layout__header"
Expand Down Expand Up @@ -31,16 +32,14 @@
<script setup lang="ts">
import { useColors } from 'vuestic-ui'
import { useDocsScroll } from '../composables/useDocsScroll';
import { useIsMounted } from 'vuestic-ui/src/composables/useIsMounted'

const cookie = useCookie('vuestic-theme')
m0ksem marked this conversation as resolved.
Show resolved Hide resolved
const { applyPreset } = useColors()
const { currentPresetName } = useColors()
const breakpoints = useBreakpoint()

const isSidebarVisible = ref(!breakpoints.smDown)
const isOptionsVisible = ref(false)

applyPreset(cookie.value || 'light')

watch(() => breakpoints.smDown, (newValue, oldValue) => {
if (newValue && !oldValue) {
isSidebarVisible.value = false
Expand Down Expand Up @@ -75,6 +74,8 @@ useHead({
{ src: 'https://unpkg.com/[email protected]/dist/ionicons/ionicons.esm.js', type: 'module' },
],
})

const isMounted = useIsMounted()
</script>

<style lang="scss">
Expand All @@ -97,6 +98,22 @@ html {
overflow: hidden;
font-family: var(--va-font-family);

&__loader {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 9999999;
background:
linear-gradient(
90deg,
var(--va-background-primary) 0%,
var(--va-background-secondary) 50%,
var(--va-background-primary) 100%
);
}

&__header {
width: 100%;
background: var(--va-white);
Expand Down
8 changes: 0 additions & 8 deletions packages/docs/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
// TEST Config
routeRules: {
'/': {static: true},
'/en/**': {static: true},
'/ru/**': {static: true},
'/_nuxt/**': { headers: { 'cache-control': 's-maxage=0' } },
},
app: {
head: {
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
{ rel: 'icon', type: 'image/png', href: '/favicon.png' },
],

meta: [
Expand Down
5 changes: 3 additions & 2 deletions packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"name": "docs",
"version": "0.0.1",
"scripts": {
"build": "yarn build:analysis && nuxt build --max_old_space_size=4096",
"build:ci": "yarn build:analysis && nuxt build --max_old_space_size=4096",
"build": "yarn build:analysis && nuxt generate --max_old_space_size=4096",
"build:ci": "yarn build:analysis && nuxt generate --max_old_space_size=4096",
"start:ci": "yarn build:analysis && yarn preview",
"build:analysis": "yarn workspace sandbox build:analysis ../docs/page-config/getting-started/tree-shaking",
"serve": "yarn build:analysis --use-cache && nuxt dev",
"generate": "yarn build:analysis && nuxt generate --max_old_space_size=4096",
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
const configWithColors: PartialGlobalConfig = {
...userConfig,
colors: {
currentPresetName: themeCookie.value || userConfig.colors?.currentPresetName,
currentPresetName: themeCookie.value || userConfig.colors?.currentPresetName || 'light',
...userConfig.colors,
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/ui/src/composables/useColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export const useColors = () => {
const { setGlobalConfig, globalConfig } = gc

const colors = useReactiveComputed<ColorVariables>({
get: () => globalConfig.value.colors!.variables,
get: () => globalConfig.value.colors!.presets[globalConfig.value.colors!.currentPresetName],
set: (v: ColorVariables) => { setColors(v) },
})

const setColors = (colors: Partial<ColorVariables>): void => {
globalConfig.value.colors!.variables = {
globalConfig.value.colors!.presets[globalConfig.value.colors!.currentPresetName] = {
...globalConfig.value.colors.variables,
...colors,
} as ColorVariables
Expand Down Expand Up @@ -153,7 +153,6 @@ export const useColors = () => {
if (!globalConfig.value.colors!.presets[presetName]) {
return warn(`Preset ${presetName} does not exist`)
}
globalConfig.value.colors!.variables = { ...globalConfig.value.colors!.presets[presetName] }
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ export const createColorConfigPlugin = (app: App, config?: PartialGlobalConfig)
updateColors(newValue)
}, { immediate: true, deep: true })

if (config?.colors?.currentPresetName) {
applyPreset(config.colors.currentPresetName)
}

return {
renderCSSVariables,
updateColors,
Expand Down
32 changes: 17 additions & 15 deletions packages/ui/src/services/global-config/global-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ import { getColorsClassesDefaultConfig } from '../colors-classes/config/default'

export const GLOBAL_CONFIG = Symbol('GLOBAL_CONFIG')

export const createGlobalConfig = () => {
const globalConfig = ref<GlobalConfig>({
colors: getColorDefaultConfig(),
icons: getIconDefaultConfig(),
components: getComponentsDefaultConfig(),
breakpoint: getBreakpointDefaultConfig(),
i18n: getI18nConfigDefaults(),
colorsClasses: getColorsClassesDefaultConfig(),
/**
* global config variable to pass nuxt-link component to vuestic-ui via @vuestic/nuxt
* TODO: give a try to integrate inertia js router components via this option
* TODO: if this try won't be success, may be remake to provide/inject
*/
routerComponent: undefined,
}) as Ref<GlobalConfig>
const getDefaultConfig = () => ({
colors: getColorDefaultConfig(),
icons: getIconDefaultConfig(),
components: getComponentsDefaultConfig(),
breakpoint: getBreakpointDefaultConfig(),
i18n: getI18nConfigDefaults(),
colorsClasses: getColorsClassesDefaultConfig(),
/**
* global config variable to pass nuxt-link component to vuestic-ui via @vuestic/nuxt
* TODO: give a try to integrate inertia js router components via this option
* TODO: if this try won't be success, may be remake to provide/inject
*/
routerComponent: undefined,
})

export const createGlobalConfig = (defaultConfig: PartialGlobalConfig = {}) => {
const globalConfig = ref<GlobalConfig>(mergeDeep(getDefaultConfig(), defaultConfig)) as Ref<GlobalConfig>

const getGlobalConfig = (): GlobalConfig => globalConfig.value
const setGlobalConfig = (updater: GlobalConfig | GlobalConfigUpdater<GlobalConfig>) => {
Expand Down
6 changes: 2 additions & 4 deletions packages/ui/src/services/global-config/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import { PartialGlobalConfig } from '../types'
import { defineGlobalProperty, defineVuesticPlugin } from '../../vue-plugin/utils'

/** Provides global configuration to Vuestic components */
export const GlobalConfigPlugin = defineVuesticPlugin((config: PartialGlobalConfig | undefined) => ({
export const GlobalConfigPlugin = defineVuesticPlugin((config: PartialGlobalConfig | undefined = {}) => ({
m0ksem marked this conversation as resolved.
Show resolved Hide resolved
install (app: App) {
const globalConfig = createGlobalConfig()

if (config) { globalConfig.mergeGlobalConfig(config) }
const globalConfig = createGlobalConfig(config)

// @ts-ignore
if (config?.componentsAll) {
Expand Down