Skip to content

Commit

Permalink
fix: built-in dark theme var lost if set modifyVars option (#1492)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript authored Feb 21, 2023
1 parent 53868ab commit 96040d6
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/features/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,30 @@ export default (api: IApi) => {
path.resolve(__dirname, '../../client/theme-api'),
);

// set dark mode selector as less variable
memo.theme ??= {};
memo.theme['dark-selector'] = `~'[${PREFERS_COLOR_ATTR}="dark"]'`;
return memo;
});

// set dark mode selector as less variable
// why not use `theme` or `modifyVars`?
// because `theme` will be override by `modifyVars` in umi
// and `modifyVar` will override `theme` from user
api.chainWebpack((memo) => {
const lessRule = memo.module.rule('less');

['css', 'css-modules'].forEach((rule) => {
Object.values(lessRule.oneOf(rule).uses.entries()).forEach((loader) => {
if (loader.get('loader').includes('less-loader')) {
loader.tap((opts) => {
opts.lessOptions.modifyVars ??= {};
opts.lessOptions.modifyVars[
'dark-selector'
] = `~'[${PREFERS_COLOR_ATTR}="dark"]'`;

return opts;
});
}
});
});

return memo;
});
Expand Down

0 comments on commit 96040d6

Please sign in to comment.