Skip to content

Commit

Permalink
fix: prefers media variables not work without directives.
Browse files Browse the repository at this point in the history
  • Loading branch information
riccox committed Aug 5, 2023
1 parent 7737e54 commit e4d3912
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
9 changes: 3 additions & 6 deletions packages/tailwind/src/plugin/index.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -78,15 +78,12 @@ const config = plugin.withOptions<PluginConfig>(
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
Expand Down
35 changes: 25 additions & 10 deletions packages/tailwind/src/plugin/utils/theme.ts
Expand Up @@ -6,14 +6,15 @@ 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) => {
return !removeThemes.includes(theme.name || '');
});
};

export const createThemeVariables = (theme: Theme) => {
export const processThemeVariables = (addBase: PluginAPI['addBase'], theme: Theme) => {
const colorsCssVariableMap = {
// make sure bw color available
...createBWCssVariableMap(theme.colorScheme),
Expand Down Expand Up @@ -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`]: {
Expand All @@ -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);
};

1 comment on commit e4d3912

@vercel
Copy link

@vercel vercel bot commented on e4d3912 Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.