Skip to content

Commit af1bc8e

Browse files
committed
Change double render console color based on browser theme
1 parent 8b42cd5 commit af1bc8e

File tree

8 files changed

+43
-9
lines changed

8 files changed

+43
-9
lines changed

packages/react-devtools-extensions/src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ function syncSavedPreferences() {
4646
)};
4747
window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = ${JSON.stringify(
4848
getHideConsoleLogsInStrictMode(),
49+
)};
50+
window.__REACT_DEVTOOLS_BROWSER_THEME__ = ${JSON.stringify(
51+
getBrowserTheme(),
4952
)};`,
5053
);
5154
}

packages/react-devtools-extensions/src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* global chrome */
22

3+
import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
4+
35
const IS_CHROME = navigator.userAgent.indexOf('Firefox') < 0;
46

57
export type BrowserName = 'Chrome' | 'Firefox';
@@ -8,8 +10,6 @@ export function getBrowserName(): BrowserName {
810
return IS_CHROME ? 'Chrome' : 'Firefox';
911
}
1012

11-
export type BrowserTheme = 'dark' | 'light';
12-
1313
export function getBrowserTheme(): BrowserTheme {
1414
if (IS_CHROME) {
1515
// chrome.devtools.panels added in Chrome 18.

packages/react-devtools-shared/src/backend/agent.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import type {
4040
} from './types';
4141
import type {ComponentFilter} from '../types';
4242
import {isSynchronousXHRSupported} from './utils';
43+
import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
4344

4445
const debug = (methodName, ...args) => {
4546
if (__DEBUG__) {
@@ -636,11 +637,13 @@ export default class Agent extends EventEmitter<{|
636637
breakOnConsoleErrors,
637638
showInlineWarningsAndErrors,
638639
hideConsoleLogsInStrictMode,
640+
browserTheme,
639641
}: {|
640642
appendComponentStack: boolean,
641643
breakOnConsoleErrors: boolean,
642644
showInlineWarningsAndErrors: boolean,
643645
hideConsoleLogsInStrictMode: boolean,
646+
browserTheme: BrowserTheme,
644647
|}) => {
645648
// If the frontend preference has change,
646649
// or in the case of React Native- if the backend is just finding out the preference-
@@ -651,6 +654,7 @@ export default class Agent extends EventEmitter<{|
651654
breakOnConsoleErrors,
652655
showInlineWarningsAndErrors,
653656
hideConsoleLogsInStrictMode,
657+
browserTheme,
654658
});
655659
};
656660

packages/react-devtools-shared/src/backend/console.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
1111
import type {CurrentDispatcherRef, ReactRenderer, WorkTagMap} from './types';
12+
import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
1213
import {format} from './utils';
1314

1415
import {getInternalReactConstants} from './renderer';
@@ -17,9 +18,12 @@ import {getStackByFiberInDevAndProd} from './DevToolsFiberComponentStack';
1718
// NOTE: KEEP IN SYNC with src/hook.js
1819
const OVERRIDE_CONSOLE_METHODS = ['error', 'trace', 'warn', 'log'];
1920
const DIMMED_NODE_CONSOLE_COLOR = '\x1b[2m%s\x1b[0m';
20-
const DIMMED_WARNING_COLOR = 'rgba(250, 180, 50, .5)';
21-
const DIMMED_ERROR_COLOR = 'rgba(250, 123, 130, .5)';
22-
const DIMMED_LOG_COLOR = 'rgba(125, 125, 125, 0.5)';
21+
const DARK_MODE_DIMMED_WARNING_COLOR = 'rgba(250, 180, 50, 0.5)';
22+
const DARK_MODE_DIMMED_ERROR_COLOR = 'rgba(250, 123, 130, 0.5)';
23+
const DARK_MODE_DIMMED_LOG_COLOR = 'rgba(125, 125, 125, 0.5)';
24+
const LIGHT_MODE_DIMMED_WARNING_COLOR = 'rgba(250, 180, 50, 0.75)';
25+
const LIGHT_MODE_DIMMED_ERROR_COLOR = 'rgba(250, 123, 130, 0.75)';
26+
const LIGHT_MODE_DIMMED_LOG_COLOR = 'rgba(125, 125, 125, 0.75)';
2327

2428
// React's custom built component stack strings match "\s{4}in"
2529
// Chrome's prefix matches "\s{4}at"
@@ -124,11 +128,13 @@ export function patch({
124128
breakOnConsoleErrors,
125129
showInlineWarningsAndErrors,
126130
hideConsoleLogsInStrictMode,
131+
browserTheme,
127132
}: {
128133
appendComponentStack: boolean,
129134
breakOnConsoleErrors: boolean,
130135
showInlineWarningsAndErrors: boolean,
131136
hideConsoleLogsInStrictMode: boolean,
137+
browserTheme: BrowserTheme,
132138
}): void {
133139
// Settings may change after we've patched the console.
134140
// Using a shared ref allows the patch function to read the latest values.
@@ -248,14 +254,23 @@ export function patch({
248254
let color;
249255
switch (method) {
250256
case 'warn':
251-
color = DIMMED_WARNING_COLOR;
257+
color =
258+
browserTheme === 'light'
259+
? LIGHT_MODE_DIMMED_WARNING_COLOR
260+
: DARK_MODE_DIMMED_WARNING_COLOR;
252261
break;
253262
case 'error':
254-
color = DIMMED_ERROR_COLOR;
263+
color =
264+
browserTheme === 'light'
265+
? LIGHT_MODE_DIMMED_ERROR_COLOR
266+
: DARK_MODE_DIMMED_ERROR_COLOR;
255267
break;
256268
case 'log':
257269
default:
258-
color = DIMMED_LOG_COLOR;
270+
color =
271+
browserTheme === 'light'
272+
? LIGHT_MODE_DIMMED_LOG_COLOR
273+
: DARK_MODE_DIMMED_LOG_COLOR;
259274
break;
260275
}
261276

packages/react-devtools-shared/src/backend/renderer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,12 +742,14 @@ export function attach(
742742
window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ !== false;
743743
const hideConsoleLogsInStrictMode =
744744
window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ === true;
745+
const browserTheme = window.__REACT_DEVTOOLS_BROWSER_THEME__;
745746

746747
patchConsole({
747748
appendComponentStack,
748749
breakOnConsoleErrors,
749750
showInlineWarningsAndErrors,
750751
hideConsoleLogsInStrictMode,
752+
browserTheme,
751753
});
752754
}
753755

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type UpdateConsolePatchSettingsParams = {|
165165
breakOnConsoleErrors: boolean,
166166
showInlineWarningsAndErrors: boolean,
167167
hideConsoleLogsInStrictMode: boolean,
168+
browserTheme: 'dark' | 'light',
168169
|};
169170

170171
export type BackendEvents = {|

packages/react-devtools-shared/src/devtools/views/Settings/SettingsContext.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,15 @@ function SettingsContextController({
181181
breakOnConsoleErrors,
182182
showInlineWarningsAndErrors,
183183
hideConsoleLogsInStrictMode,
184+
browserTheme,
184185
});
185186
}, [
186187
bridge,
187188
appendComponentStack,
188189
breakOnConsoleErrors,
189190
showInlineWarningsAndErrors,
190191
hideConsoleLogsInStrictMode,
192+
browserTheme,
191193
]);
192194

193195
useEffect(() => {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
1212
import type {ReactRenderer} from './backend/types';
13+
import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
1314

1415
import {
1516
patch as patchConsole,
@@ -224,7 +225,10 @@ export function installHook(target: any): DevToolsHook | null {
224225
// NOTE: KEEP IN SYNC with src/backend/console.js:patch
225226
function patchConsoleForInitialRenderInExtension(
226227
renderer: ReactRenderer,
227-
{hideConsoleLogsInStrictMode}: {hideConsoleLogsInStrictMode: boolean},
228+
{
229+
hideConsoleLogsInStrictMode,
230+
browserTheme,
231+
}: {hideConsoleLogsInStrictMode: boolean, browserTheme: BrowserTheme},
228232
): void {
229233
const overrideConsoleMethods = ['error', 'trace', 'warn', 'log'];
230234

@@ -346,6 +350,7 @@ export function installHook(target: any): DevToolsHook | null {
346350
window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ !== false;
347351
const hideConsoleLogsInStrictMode =
348352
window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ === true;
353+
const browserTheme = window.__REACT_DEVTOOLS_BROWSER_THEME__;
349354

350355
// The installHook() function is injected by being stringified in the browser,
351356
// so imports outside of this function do not get included.
@@ -361,10 +366,12 @@ export function installHook(target: any): DevToolsHook | null {
361366
breakOnConsoleErrors,
362367
showInlineWarningsAndErrors,
363368
hideConsoleLogsInStrictMode,
369+
browserTheme,
364370
});
365371
} else {
366372
patchConsoleForInitialRenderInExtension(renderer, {
367373
hideConsoleLogsInStrictMode,
374+
browserTheme,
368375
});
369376
}
370377
} catch (error) {}

0 commit comments

Comments
 (0)