Skip to content

Commit 8e8f171

Browse files
committed
perf: refactor to enable tree-shaking transition code
1 parent 3fccf39 commit 8e8f171

File tree

2 files changed

+3
-7
lines changed

2 files changed

+3
-7
lines changed

src/runtime/plugin.client.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { computed, reactive, watch } from 'vue'
22

33
import type { ColorModeInstance } from './types'
44
import { defineNuxtPlugin, isVue2, isVue3, useRouter, useHead, useState } from '#imports'
5-
import { globalName, storageKey, dataValue } from '#color-mode-options'
5+
import { globalName, storageKey, dataValue, disableTransition } from '#color-mode-options'
66

77
// Initialise to empty object to avoid hard error when hydrating app in test mode
88
const helper = (window[globalName] || {}) as unknown as {
99
preference: string
1010
value: string
11-
disableTransition: boolean
1211
getColorScheme: () => string
1312
addColorScheme: (className: string) => void
1413
removeColorScheme: (className: string) => void
@@ -90,14 +89,14 @@ export default defineNuxtPlugin((nuxtApp) => {
9089

9190
watch(() => colorMode.value, (newValue, oldValue) => {
9291
let style: HTMLStyleElement | undefined
93-
if (helper.disableTransition) {
92+
if (disableTransition) {
9493
style = window!.document.createElement('style')
9594
style.appendChild(document.createTextNode('*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}'))
9695
window!.document.head.appendChild(style)
9796
}
9897
helper.removeColorScheme(oldValue)
9998
helper.addColorScheme(newValue)
100-
if (helper.disableTransition) {
99+
if (disableTransition) {
101100
// Calling getComputedStyle forces the browser to redraw
102101
// eslint-disable-next-line @typescript-eslint/no-unused-vars
103102
const _ = window!.getComputedStyle(style!).opacity

src/script.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
const preference = (ls && ls.getItem && ls.getItem('<%= options.storageKey %>')) || '<%= options.preference %>'
1313
let value = preference === 'system' ? getColorScheme() : preference
14-
const disableTransition = '<%= options.disableTransition %>'
1514
// Applied forced color mode
1615
const forcedColorMode = de.getAttribute('data-color-mode-forced')
1716
if (forcedColorMode) {
@@ -23,8 +22,6 @@
2322
w['<%= options.globalName %>'] = {
2423
preference,
2524
value,
26-
// @ts-ignore
27-
disableTransition: disableTransition === 'true',
2825
getColorScheme,
2926
addColorScheme,
3027
removeColorScheme

0 commit comments

Comments
 (0)