Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion __tests__/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import postcss from 'postcss';
import nested from 'postcss-nested';

import plugin, { PostcssThemeOptions } from '../src/index';
import plugin from '../src/index';
import { PostcssThemeOptions } from '../src/types';

export function normalizeResult(input: string) {
return input
Expand Down
43 changes: 43 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import path from 'path';
import { PostcssThemeConfig, PostcssStrictThemeConfig, Theme } from "../types";

/** Get the theme variable name from a string */
export const parseThemeKey = (value: string) => {
const key = value.match(/@theme ([a-zA-Z-_0-9]+)/);
return key ? key[1] : '';
}

/** Replace a theme variable reference with a value */
export const replaceTheme = (value: string, replace: string) => {
return value.replace(/@theme ([a-zA-Z-_0-9]+)/, replace);
}

/** Get the location of the theme file */
export function getThemeFilename(cssFile: string) {
return path.join(path.dirname(cssFile), 'theme');
}

/** Remove :theme-root usage from a selector */
export const replaceThemeRoot = (selector: string) =>
selector.replace(/:theme-root\((\S+)\)/g, '$1').replace(/:theme-root/g, '');

/** Make a SimpleTheme into a LightDarkTheme */
export const normalizeTheme = (config: PostcssThemeConfig | {}): PostcssStrictThemeConfig => {
return Object.assign(
{},
...Object.entries(config).map(([theme, themeConfig]) => {
if ('light' in themeConfig && 'dark' in themeConfig) {
return { [theme]: themeConfig };
}

return { [theme]: { light: themeConfig, dark: {} } };
})
);
};

/** Determine if a theme has dark mode enabled */
export const hasDarkMode = (theme: Theme) =>
Boolean(
Object.keys(theme.dark).length > 0 && Object.keys(theme.light).length > 0
);

Loading