From e4d3912368beb997da028263da24e3693e2d6934 Mon Sep 17 00:00:00 2001 From: Ricco Xie Date: Sat, 5 Aug 2023 23:05:18 +0800 Subject: [PATCH] fix: prefers media variables not work without directives. --- packages/tailwind/src/plugin/index.ts | 9 ++---- packages/tailwind/src/plugin/utils/theme.ts | 35 +++++++++++++++------ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/tailwind/src/plugin/index.ts b/packages/tailwind/src/plugin/index.ts index b02104c..21e4297 100644 --- a/packages/tailwind/src/plugin/index.ts +++ b/packages/tailwind/src/plugin/index.ts @@ -9,7 +9,7 @@ import postcssJs from 'postcss-js'; import path from 'path'; import { PluginConfig, PartialTheme } from './types/config.types'; import { Theme } from './types/theme.types'; -import { createThemeVariables, excludeThemesByName } from './utils/theme'; +import { processThemeVariables, excludeThemesByName } from './utils/theme'; import _ from 'lodash'; import { getSelectorsWithPrefix } from './utils/prefix'; @@ -78,15 +78,12 @@ const config = plugin.withOptions( mergedTheme = _.merge(lightTheme, theme); } - addBase( - // inject some basic depended css variables - createThemeVariables(mergedTheme) - ); + processThemeVariables(addBase, mergedTheme); }); } } - // add base styles + // add preset base styles addBase(baseCSSObj); // add component styles diff --git a/packages/tailwind/src/plugin/utils/theme.ts b/packages/tailwind/src/plugin/utils/theme.ts index f10f9a9..8b66acc 100644 --- a/packages/tailwind/src/plugin/utils/theme.ts +++ b/packages/tailwind/src/plugin/utils/theme.ts @@ -6,6 +6,7 @@ import { createColorCssVariableMap, } from './variables'; import { PartialTheme } from '../types/config.types'; +import { type PluginAPI } from 'tailwindcss/types/config'; export const excludeThemesByName = (removeThemes: string[], themes: PartialTheme[]) => { return themes.filter((theme) => { @@ -13,7 +14,7 @@ export const excludeThemesByName = (removeThemes: string[], themes: PartialTheme }); }; -export const createThemeVariables = (theme: Theme) => { +export const processThemeVariables = (addBase: PluginAPI['addBase'], theme: Theme) => { const colorsCssVariableMap = { // make sure bw color available ...createBWCssVariableMap(theme.colorScheme), @@ -48,22 +49,24 @@ export const createThemeVariables = (theme: Theme) => { }; } - // return theme all relative styles - return { - ...(theme.name === 'light' && { + // add theme all relative styles into base layer. + if (theme.name === 'light') { + addBase({ [':root']: { colorScheme: 'light', ...colorsCssVariableMap, }, ...colorVariablesClassesMap, - }), + }); + } + addBase({ [`[data-theme=${theme.name}]`]: { colorScheme: theme.colorScheme || 'light', ...colorsCssVariableMap, }, - ...themeColorVariablesClassesMap, - // if theme enabled prefersColorScheme param - ...(theme.prefersColorScheme && { + }); + if (theme.prefersColorScheme) { + addBase({ [`@media (prefers-color-scheme:${theme.colorScheme || 'light'})`]: { ...(theme.name === 'dark' && { [`:root`]: { @@ -77,6 +80,18 @@ export const createThemeVariables = (theme: Theme) => { }, ...colorVariablesClassesMap, }, - }), - }; + }); + + if (theme.name === 'dark') { + addBase({ + [`@media (prefers-color-scheme:${theme.colorScheme || 'light'})`]: { + [`:root`]: { + colorScheme: 'dark', + ...colorsCssVariableMap, + }, + }, + }); + } + } + addBase(themeColorVariablesClassesMap); };