Skip to content

Commit 34e8d0e

Browse files
committed
Add RGB css var generation on client side
1 parent 86f4005 commit 34e8d0e

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"jsx-a11y/click-events-have-key-events": "off",
3636
"react-hooks/rules-of-hooks": "error",
3737
"react-hooks/exhaustive-deps": "warn",
38-
"import/no-extraneous-dependencies": "off"
38+
"import/no-extraneous-dependencies": "off",
39+
"jsdoc/valid-types": "warn"
3940
},
4041
"env": {
4142
"jest/globals": true,

plugin/assets/src/customizer/customize-preview.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ import {
3838
/**
3939
* Internal dependencies
4040
*/
41-
import { STYLES } from '../customizer/components/google-fonts-control/styles';
41+
import { STYLES } from './components/google-fonts-control/styles';
4242
import { setConfig } from '../block-editor/utils/get-config';
43+
import { applyRgbValue } from '../../../../theme/assets/src/helper/apply-rgb-value';
4344

4445
const getIconFontName = iconStyle => {
4546
return iconStyle === 'filled'
@@ -100,6 +101,11 @@ export const COLOR_MODES = {
100101
target: document.body,
101102
dark: isDarkMode,
102103
} );
104+
105+
applyRgbValue( colorPallete, {
106+
target: document.body,
107+
dark: isDarkMode,
108+
} );
103109
}
104110
);
105111
} );

theme/assets/src/front-end/components/dark-mode-switch.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
themeFromSourceColor,
2424
applyTheme,
2525
} from '@material/material-color-utilities';
26+
import { applyRgbValue } from '../../helper/apply-rgb-value';
2627

2728
const body = document.body;
2829
export const ICONS = {
@@ -54,14 +55,21 @@ const maybeToggleDarkMode = event => {
5455
target: document.body,
5556
dark: true,
5657
} );
58+
applyRgbValue( colorPallete, {
59+
target: document.body,
60+
dark: true,
61+
} );
5762
body.setAttribute( 'data-color-scheme', 'dark' );
5863
switcherIcon.textContent = ICONS.LIGHT_MODE;
5964
} else {
6065
applyTheme( colorPallete, {
6166
target: document.body,
6267
dark: false,
6368
} );
64-
69+
applyRgbValue( colorPallete, {
70+
target: document.body,
71+
dark: false,
72+
} );
6573
body.setAttribute( 'data-color-scheme', 'light' );
6674
switcherIcon.textContent = ICONS.DARK_MODE;
6775
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {
18+
blueFromArgb,
19+
greenFromArgb,
20+
redFromArgb,
21+
} from '@material/material-color-utilities';
22+
23+
/**
24+
* @typedef {Object} Option
25+
* @property {boolean?} dark Dark mode.
26+
* @property {HTMLElement?} target HTML target element.
27+
*/
28+
29+
/** @typedef {import('@material/material-color-utilities').Theme} Theme Theme. */
30+
31+
/**
32+
* Apply RGB variable to given element.
33+
*
34+
* @param {Theme} theme generated from material library.
35+
* @param {Option} options theme options
36+
*/
37+
export const applyRgbValue = ( theme, options ) => {
38+
let _temp;
39+
const target =
40+
( options === null || options === void 0 ? void 0 : options.target ) ||
41+
document.body;
42+
const isDark =
43+
( _temp =
44+
options === null || options === void 0 ? void 0 : options.dark ) !==
45+
null && _temp !== void 0
46+
? _temp
47+
: false;
48+
const scheme = isDark ? theme.schemes.dark : theme.schemes.light;
49+
for ( const [ key, value ] of Object.entries( scheme.toJSON() ) ) {
50+
const token = key.replace( /([a-z])([A-Z])/g, '$1-$2' ).toLowerCase();
51+
const color = `${ redFromArgb( value ) },${ greenFromArgb(
52+
value
53+
) },${ blueFromArgb( value ) }`;
54+
target.style.setProperty( `--md-sys-color-${ token }-rgb`, color );
55+
}
56+
};

0 commit comments

Comments
 (0)