Skip to content

Commit e3b76a8

Browse files
houssemchebebBrian Vaughn
andauthored
Devtools: Refactor imperative theme code (#21950)
Co-authored-by: Brian Vaughn <[email protected]>
1 parent d3f8747 commit e3b76a8

File tree

8 files changed

+375
-635
lines changed

8 files changed

+375
-635
lines changed

packages/react-devtools-scheduling-profiler/src/SchedulingProfiler.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
useContext,
1616
useDeferredValue,
1717
useLayoutEffect,
18+
useRef,
1819
useState,
1920
} from 'react';
2021
import {SettingsContext} from 'react-devtools-shared/src/devtools/views/Settings/SettingsContext';
@@ -30,6 +31,8 @@ export function SchedulingProfiler(_: {||}) {
3031
SchedulingProfilerContext,
3132
);
3233

34+
const ref = useRef(null);
35+
3336
// HACK: Canvas rendering uses an imperative API,
3437
// but DevTools colors are stored in CSS variables (see root.css and SettingsContext).
3538
// When the theme changes, we need to trigger update the imperative colors and re-draw the Canvas.
@@ -42,7 +45,7 @@ export function SchedulingProfiler(_: {||}) {
4245
const [key, setKey] = useState<string>(theme);
4346
useLayoutEffect(() => {
4447
const pollForTheme = () => {
45-
if (updateColorsToMatchTheme()) {
48+
if (updateColorsToMatchTheme(((ref.current: any): HTMLDivElement))) {
4649
clearInterval(intervalID);
4750
setKey(deferredTheme);
4851
}
@@ -56,7 +59,7 @@ export function SchedulingProfiler(_: {||}) {
5659
}, [deferredTheme]);
5760

5861
return (
59-
<div className={styles.Content}>
62+
<div className={styles.Content} ref={ref}>
6063
{schedulingProfilerData ? (
6164
<Suspense fallback={<ProcessingData />}>
6265
<DataResourceComponent

packages/react-devtools-scheduling-profiler/src/content-views/constants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export let COLORS = {
8282
WARNING_TEXT_INVERED: '',
8383
};
8484

85-
export function updateColorsToMatchTheme(): boolean {
86-
const computedStyle = getComputedStyle((document.body: any));
85+
export function updateColorsToMatchTheme(element: Element): boolean {
86+
const computedStyle = getComputedStyle(element);
8787

8888
// Check to see if styles have been initialized...
8989
if (computedStyle.getPropertyValue('--color-background') == null) {

packages/react-devtools-shared/src/constants.js

Lines changed: 287 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,296 @@ export const UNSUPPORTED_VERSION_URL =
5555
export const REACT_DEVTOOLS_WORKPLACE_URL =
5656
'https://fburl.com/react-devtools-workplace-group';
5757

58+
import type {
59+
Theme,
60+
DisplayDensity,
61+
} from './devtools/views/Settings/SettingsContext';
62+
63+
export const THEME_STYLES: {[style: Theme | DisplayDensity]: any} = {
64+
light: {
65+
'--color-attribute-name': '#ef6632',
66+
'--color-attribute-name-not-editable': '#23272f',
67+
'--color-attribute-name-inverted': 'rgba(255, 255, 255, 0.7)',
68+
'--color-attribute-value': '#1a1aa6',
69+
'--color-attribute-value-inverted': '#ffffff',
70+
'--color-attribute-editable-value': '#1a1aa6',
71+
'--color-background': '#ffffff',
72+
'--color-background-hover': 'rgba(0, 136, 250, 0.1)',
73+
'--color-background-inactive': '#e5e5e5',
74+
'--color-background-invalid': '#fff0f0',
75+
'--color-background-selected': '#0088fa',
76+
'--color-button-background': '#ffffff',
77+
'--color-button-background-focus': '#ededed',
78+
'--color-button': '#5f6673',
79+
'--color-button-disabled': '#cfd1d5',
80+
'--color-button-active': '#0088fa',
81+
'--color-button-focus': '#23272f',
82+
'--color-button-hover': '#23272f',
83+
'--color-border': '#eeeeee',
84+
'--color-commit-did-not-render-fill': '#cfd1d5',
85+
'--color-commit-did-not-render-fill-text': '#000000',
86+
'--color-commit-did-not-render-pattern': '#cfd1d5',
87+
'--color-commit-did-not-render-pattern-text': '#333333',
88+
'--color-commit-gradient-0': '#37afa9',
89+
'--color-commit-gradient-1': '#63b19e',
90+
'--color-commit-gradient-2': '#80b393',
91+
'--color-commit-gradient-3': '#97b488',
92+
'--color-commit-gradient-4': '#abb67d',
93+
'--color-commit-gradient-5': '#beb771',
94+
'--color-commit-gradient-6': '#cfb965',
95+
'--color-commit-gradient-7': '#dfba57',
96+
'--color-commit-gradient-8': '#efbb49',
97+
'--color-commit-gradient-9': '#febc38',
98+
'--color-commit-gradient-text': '#000000',
99+
'--color-component-name': '#6a51b2',
100+
'--color-component-name-inverted': '#ffffff',
101+
'--color-component-badge-background': 'rgba(0, 0, 0, 0.1)',
102+
'--color-component-badge-background-inverted': 'rgba(255, 255, 255, 0.25)',
103+
'--color-component-badge-count': '#777d88',
104+
'--color-component-badge-count-inverted': 'rgba(255, 255, 255, 0.7)',
105+
'--color-console-error-badge-text': '#ffffff',
106+
'--color-console-error-background': '#fff0f0',
107+
'--color-console-error-border': '#ffd6d6',
108+
'--color-console-error-icon': '#eb3941',
109+
'--color-console-error-text': '#fe2e31',
110+
'--color-console-warning-badge-text': '#000000',
111+
'--color-console-warning-background': '#fffbe5',
112+
'--color-console-warning-border': '#fff5c1',
113+
'--color-console-warning-icon': '#f4bd00',
114+
'--color-console-warning-text': '#64460c',
115+
'--color-context-background': 'rgba(0,0,0,.9)',
116+
'--color-context-background-hover': 'rgba(255, 255, 255, 0.1)',
117+
'--color-context-background-selected': '#178fb9',
118+
'--color-context-border': '#3d424a',
119+
'--color-context-text': '#ffffff',
120+
'--color-context-text-selected': '#ffffff',
121+
'--color-dim': '#777d88',
122+
'--color-dimmer': '#cfd1d5',
123+
'--color-dimmest': '#eff0f1',
124+
'--color-error-background': 'hsl(0, 100%, 97%)',
125+
'--color-error-border': 'hsl(0, 100%, 92%)',
126+
'--color-error-text': '#ff0000',
127+
'--color-expand-collapse-toggle': '#777d88',
128+
'--color-link': '#0000ff',
129+
'--color-modal-background': 'rgba(255, 255, 255, 0.75)',
130+
'--color-bridge-version-npm-background': '#eff0f1',
131+
'--color-bridge-version-npm-text': '#000000',
132+
'--color-bridge-version-number': '#0088fa',
133+
'--color-primitive-hook-badge-background': '#e5e5e5',
134+
'--color-primitive-hook-badge-text': '#5f6673',
135+
'--color-record-active': '#fc3a4b',
136+
'--color-record-hover': '#3578e5',
137+
'--color-record-inactive': '#0088fa',
138+
'--color-resize-bar': '#eeeeee',
139+
'--color-resize-bar-active': '#dcdcdc',
140+
'--color-resize-bar-border': '#d1d1d1',
141+
'--color-resize-bar-dot': '#333333',
142+
'--color-scheduling-profiler-native-event': '#ccc',
143+
'--color-scheduling-profiler-native-event-hover': '#aaa',
144+
'--color-scheduling-profiler-priority-background': '#f6f6f6',
145+
'--color-scheduling-profiler-priority-border': '#eeeeee',
146+
'--color-scheduling-profiler-user-timing': '#c9cacd',
147+
'--color-scheduling-profiler-user-timing-hover': '#93959a',
148+
'--color-scheduling-profiler-react-idle': '#d3e5f6',
149+
'--color-scheduling-profiler-react-idle-hover': '#c3d9ef',
150+
'--color-scheduling-profiler-react-render': '#9fc3f3',
151+
'--color-scheduling-profiler-react-render-hover': '#83afe9',
152+
'--color-scheduling-profiler-react-commit': '#c88ff0',
153+
'--color-scheduling-profiler-react-commit-hover': '#b281d6',
154+
'--color-scheduling-profiler-react-layout-effects': '#b281d6',
155+
'--color-scheduling-profiler-react-layout-effects-hover': '#9d71bd',
156+
'--color-scheduling-profiler-react-passive-effects': '#b281d6',
157+
'--color-scheduling-profiler-react-passive-effects-hover': '#9d71bd',
158+
'--color-scheduling-profiler-react-schedule': '#9fc3f3',
159+
'--color-scheduling-profiler-react-schedule-hover': '#2683E2',
160+
'--color-scheduling-profiler-react-suspense-rejected': '#f1cc14',
161+
'--color-scheduling-profiler-react-suspense-rejected-hover': '#ffdf37',
162+
'--color-scheduling-profiler-react-suspense-resolved': '#a6e59f',
163+
'--color-scheduling-profiler-react-suspense-resolved-hover': '#89d281',
164+
'--color-scheduling-profiler-react-suspense-unresolved': '#c9cacd',
165+
'--color-scheduling-profiler-react-suspense-unresolved-hover': '#93959a',
166+
'--color-scheduling-profiler-text-color': '#000000',
167+
'--color-scheduling-profiler-react-work-border': '#ffffff',
168+
'--color-scroll-thumb': '#c2c2c2',
169+
'--color-scroll-track': '#fafafa',
170+
'--color-search-match': 'yellow',
171+
'--color-search-match-current': '#f7923b',
172+
'--color-selected-tree-highlight-active': 'rgba(0, 136, 250, 0.1)',
173+
'--color-selected-tree-highlight-inactive': 'rgba(0, 0, 0, 0.05)',
174+
'--color-scroll-caret': 'rgba(150, 150, 150, 0.5)',
175+
'--color-tab-selected-border': '#0088fa',
176+
'--color-text': '#000000',
177+
'--color-text-invalid': '#ff0000',
178+
'--color-text-selected': '#ffffff',
179+
'--color-toggle-background-invalid': '#fc3a4b',
180+
'--color-toggle-background-on': '#0088fa',
181+
'--color-toggle-background-off': '#cfd1d5',
182+
'--color-toggle-text': '#ffffff',
183+
'--color-tooltip-background': 'rgba(0, 0, 0, 0.9)',
184+
'--color-tooltip-text': '#ffffff',
185+
'--color-warning-background': '#fb3655',
186+
'--color-warning-background-hover': '#f82042',
187+
'--color-warning-text-color': '#ffffff',
188+
'--color-warning-text-color-inverted': '#fd4d69',
189+
},
190+
dark: {
191+
'--color-attribute-name': '#9d87d2',
192+
'--color-attribute-name-not-editable': '#ededed',
193+
'--color-attribute-name-inverted': '#282828',
194+
'--color-attribute-value': '#cedae0',
195+
'--color-attribute-value-inverted': '#ffffff',
196+
'--color-attribute-editable-value': 'yellow',
197+
'--color-background': '#282c34',
198+
'--color-background-hover': 'rgba(255, 255, 255, 0.1)',
199+
'--color-background-inactive': '#3d424a',
200+
'--color-background-invalid': '#5c0000',
201+
'--color-background-selected': '#178fb9',
202+
'--color-button-background': '#282c34',
203+
'--color-button-background-focus': '#3d424a',
204+
'--color-button': '#afb3b9',
205+
'--color-button-active': '#61dafb',
206+
'--color-button-disabled': '#4f5766',
207+
'--color-button-focus': '#a2e9fc',
208+
'--color-button-hover': '#ededed',
209+
'--color-border': '#3d424a',
210+
'--color-commit-did-not-render-fill': '#777d88',
211+
'--color-commit-did-not-render-fill-text': '#000000',
212+
'--color-commit-did-not-render-pattern': '#666c77',
213+
'--color-commit-did-not-render-pattern-text': '#ffffff',
214+
'--color-commit-gradient-0': '#37afa9',
215+
'--color-commit-gradient-1': '#63b19e',
216+
'--color-commit-gradient-2': '#80b393',
217+
'--color-commit-gradient-3': '#97b488',
218+
'--color-commit-gradient-4': '#abb67d',
219+
'--color-commit-gradient-5': '#beb771',
220+
'--color-commit-gradient-6': '#cfb965',
221+
'--color-commit-gradient-7': '#dfba57',
222+
'--color-commit-gradient-8': '#efbb49',
223+
'--color-commit-gradient-9': '#febc38',
224+
'--color-commit-gradient-text': '#000000',
225+
'--color-component-name': '#61dafb',
226+
'--color-component-name-inverted': '#282828',
227+
'--color-component-badge-background': 'rgba(255, 255, 255, 0.25)',
228+
'--color-component-badge-background-inverted': 'rgba(0, 0, 0, 0.25)',
229+
'--color-component-badge-count': '#8f949d',
230+
'--color-component-badge-count-inverted': 'rgba(255, 255, 255, 0.7)',
231+
'--color-console-error-badge-text': '#000000',
232+
'--color-console-error-background': '#290000',
233+
'--color-console-error-border': '#5c0000',
234+
'--color-console-error-icon': '#eb3941',
235+
'--color-console-error-text': '#fc7f7f',
236+
'--color-console-warning-badge-text': '#000000',
237+
'--color-console-warning-background': '#332b00',
238+
'--color-console-warning-border': '#665500',
239+
'--color-console-warning-icon': '#f4bd00',
240+
'--color-console-warning-text': '#f5f2ed',
241+
'--color-context-background': 'rgba(255,255,255,.95)',
242+
'--color-context-background-hover': 'rgba(0, 136, 250, 0.1)',
243+
'--color-context-background-selected': '#0088fa',
244+
'--color-context-border': '#eeeeee',
245+
'--color-context-text': '#000000',
246+
'--color-context-text-selected': '#ffffff',
247+
'--color-dim': '#8f949d',
248+
'--color-dimmer': '#777d88',
249+
'--color-dimmest': '#4f5766',
250+
'--color-error-background': '#200',
251+
'--color-error-border': '#900',
252+
'--color-error-text': '#f55',
253+
'--color-expand-collapse-toggle': '#8f949d',
254+
'--color-link': '#61dafb',
255+
'--color-modal-background': 'rgba(0, 0, 0, 0.75)',
256+
'--color-bridge-version-npm-background': 'rgba(0, 0, 0, 0.25)',
257+
'--color-bridge-version-npm-text': '#ffffff',
258+
'--color-bridge-version-number': 'yellow',
259+
'--color-primitive-hook-badge-background': 'rgba(0, 0, 0, 0.25)',
260+
'--color-primitive-hook-badge-text': 'rgba(255, 255, 255, 0.7)',
261+
'--color-record-active': '#fc3a4b',
262+
'--color-record-hover': '#a2e9fc',
263+
'--color-record-inactive': '#61dafb',
264+
'--color-resize-bar': '#282c34',
265+
'--color-resize-bar-active': '#31363f',
266+
'--color-resize-bar-border': '#3d424a',
267+
'--color-resize-bar-dot': '#cfd1d5',
268+
'--color-scheduling-profiler-native-event': '#b2b2b2',
269+
'--color-scheduling-profiler-native-event-hover': '#949494',
270+
'--color-scheduling-profiler-priority-background': '#1d2129',
271+
'--color-scheduling-profiler-priority-border': '#282c34',
272+
'--color-scheduling-profiler-user-timing': '#c9cacd',
273+
'--color-scheduling-profiler-user-timing-hover': '#93959a',
274+
'--color-scheduling-profiler-react-idle': '#3d485b',
275+
'--color-scheduling-profiler-react-idle-hover': '#465269',
276+
'--color-scheduling-profiler-react-render': '#2683E2',
277+
'--color-scheduling-profiler-react-render-hover': '#1a76d4',
278+
'--color-scheduling-profiler-react-commit': '#731fad',
279+
'--color-scheduling-profiler-react-commit-hover': '#611b94',
280+
'--color-scheduling-profiler-react-layout-effects': '#611b94',
281+
'--color-scheduling-profiler-react-layout-effects-hover': '#51167a',
282+
'--color-scheduling-profiler-react-passive-effects': '#611b94',
283+
'--color-scheduling-profiler-react-passive-effects-hover': '#51167a',
284+
'--color-scheduling-profiler-react-schedule': '#2683E2',
285+
'--color-scheduling-profiler-react-schedule-hover': '#1a76d4',
286+
'--color-scheduling-profiler-react-suspense-rejected': '#f1cc14',
287+
'--color-scheduling-profiler-react-suspense-rejected-hover': '#e4c00f',
288+
'--color-scheduling-profiler-react-suspense-resolved': '#a6e59f',
289+
'--color-scheduling-profiler-react-suspense-resolved-hover': '#89d281',
290+
'--color-scheduling-profiler-react-suspense-unresolved': '#c9cacd',
291+
'--color-scheduling-profiler-react-suspense-unresolved-hover': '#93959a',
292+
'--color-scheduling-profiler-text-color': '#000000',
293+
'--color-scheduling-profiler-react-work-border': '#ffffff',
294+
'--color-scroll-thumb': '#afb3b9',
295+
'--color-scroll-track': '#313640',
296+
'--color-search-match': 'yellow',
297+
'--color-search-match-current': '#f7923b',
298+
'--color-selected-tree-highlight-active': 'rgba(23, 143, 185, 0.15)',
299+
'--color-selected-tree-highlight-inactive': 'rgba(255, 255, 255, 0.05)',
300+
'--color-scroll-caret': '#4f5766',
301+
'--color-shadow': 'rgba(0, 0, 0, 0.5)',
302+
'--color-tab-selected-border': '#178fb9',
303+
'--color-text': '#ffffff',
304+
'--color-text-invalid': '#ff8080',
305+
'--color-text-selected': '#ffffff',
306+
'--color-toggle-background-invalid': '#fc3a4b',
307+
'--color-toggle-background-on': '#178fb9',
308+
'--color-toggle-background-off': '#777d88',
309+
'--color-toggle-text': '#ffffff',
310+
'--color-tooltip-background': 'rgba(255, 255, 255, 0.95)',
311+
'--color-tooltip-text': '#000000',
312+
'--color-warning-background': '#ee1638',
313+
'--color-warning-background-hover': '#da1030',
314+
'--color-warning-text-color': '#ffffff',
315+
'--color-warning-text-color-inverted': '#ee1638',
316+
},
317+
compact: {
318+
'--font-size-monospace-small': '9px',
319+
'--font-size-monospace-normal': '11px',
320+
'--font-size-monospace-large': '15px',
321+
'--font-size-sans-small': '10px',
322+
'--font-size-sans-normal': '12px',
323+
'--font-size-sans-large': '14px',
324+
'--line-height-data': '18px',
325+
},
326+
comfortable: {
327+
'--font-size-monospace-small': '10px',
328+
'--font-size-monospace-normal': '13px',
329+
'--font-size-monospace-large': '17px',
330+
'--font-size-sans-small': '12px',
331+
'--font-size-sans-normal': '14px',
332+
'--font-size-sans-large': '16px',
333+
'--line-height-data': '22px',
334+
},
335+
};
336+
58337
// HACK
59338
//
60-
// Extracting during build time avoids a temporarily invalid state for the inline target.
61339
// Sometimes the inline target is rendered before root styles are applied,
62340
// which would result in e.g. NaN itemSize being passed to react-window list.
63-
//
64-
let COMFORTABLE_LINE_HEIGHT;
65-
let COMPACT_LINE_HEIGHT;
66-
67-
try {
68-
// $FlowFixMe
69-
const rawStyleString = require('!!raw-loader!react-devtools-shared/src/devtools/views/root.css')
70-
.default;
71-
72-
const extractVar = varName => {
73-
const regExp = new RegExp(`${varName}: ([0-9]+)`);
74-
const match = rawStyleString.match(regExp);
75-
return parseInt(match[1], 10);
76-
};
77-
78-
COMFORTABLE_LINE_HEIGHT = extractVar('comfortable-line-height-data');
79-
COMPACT_LINE_HEIGHT = extractVar('compact-line-height-data');
80-
} catch (error) {
81-
// We can't use the Webpack loader syntax in the context of Jest,
82-
// so tests need some reasonably meaningful fallback value.
83-
COMFORTABLE_LINE_HEIGHT = 15;
84-
COMPACT_LINE_HEIGHT = 10;
85-
}
341+
const COMFORTABLE_LINE_HEIGHT = parseInt(
342+
THEME_STYLES.comfortable['--line-height-data'],
343+
10,
344+
);
345+
const COMPACT_LINE_HEIGHT = parseInt(
346+
THEME_STYLES.compact['--line-height-data'],
347+
10,
348+
);
86349

87350
export {COMFORTABLE_LINE_HEIGHT, COMPACT_LINE_HEIGHT};

0 commit comments

Comments
 (0)