Skip to content

Commit 8ea2c32

Browse files
jyash97zhengjitf
authored andcommitted
fix(devtools): expose css vars to reach-ui portal components (facebook#22716)
1 parent e619ae2 commit 8ea2c32

File tree

11 files changed

+112
-85
lines changed

11 files changed

+112
-85
lines changed

packages/react-devtools-shared/src/devtools/views/Button.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
*/
99

1010
import * as React from 'react';
11-
import Tooltip from '@reach/tooltip';
1211

1312
import styles from './Button.css';
14-
import tooltipStyles from './Tooltip.css';
13+
import Tooltip from './Components/reach-ui/tooltip';
1514

1615
type Props = {
1716
children: React$Node,
@@ -35,11 +34,7 @@ export default function Button({
3534
);
3635

3736
if (title) {
38-
button = (
39-
<Tooltip className={tooltipStyles.Tooltip} label={title}>
40-
{button}
41-
</Tooltip>
42-
);
37+
button = <Tooltip label={title}>{button}</Tooltip>;
4338
}
4439

4540
return button;

packages/react-devtools-shared/src/devtools/views/Components/OwnersStack.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import {
1616
useRef,
1717
useState,
1818
} from 'react';
19-
import Tooltip from '@reach/tooltip';
20-
import {Menu, MenuList, MenuButton, MenuItem} from '@reach/menu-button';
2119
import Button from '../Button';
2220
import ButtonIcon from '../ButtonIcon';
2321
import Toggle from '../Toggle';
@@ -26,6 +24,13 @@ import {OwnersListContext} from './OwnersListContext';
2624
import {TreeDispatcherContext, TreeStateContext} from './TreeContext';
2725
import {useIsOverflowing} from '../hooks';
2826
import {StoreContext} from '../context';
27+
import Tooltip from '../Components/reach-ui/tooltip';
28+
import {
29+
Menu,
30+
MenuList,
31+
MenuButton,
32+
MenuItem,
33+
} from '../Components/reach-ui/menu-button';
2934

3035
import type {SerializedElement} from './types';
3136

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import * as React from 'react';
11+
import {
12+
Menu,
13+
MenuList as ReachMenuList,
14+
MenuButton,
15+
MenuItem,
16+
} from '@reach/menu-button';
17+
import useThemeStyles from '../../useThemeStyles';
18+
19+
const MenuList = ({children, ...props}: {children: React$Node, ...}) => {
20+
const style = useThemeStyles();
21+
return (
22+
<ReachMenuList style={style} {...props}>
23+
{children}
24+
</ReachMenuList>
25+
);
26+
};
27+
28+
export {MenuItem, MenuButton, MenuList, Menu};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import * as React from 'react';
11+
import ReachTooltip from '@reach/tooltip';
12+
import tooltipStyles from './Tooltip.css';
13+
import useThemeStyles from '../../useThemeStyles';
14+
15+
const Tooltip = ({
16+
children,
17+
className = '',
18+
...props
19+
}: {
20+
children: React$Node,
21+
className: string,
22+
...
23+
}) => {
24+
const style = useThemeStyles();
25+
return (
26+
<ReachTooltip
27+
className={`${tooltipStyles.Tooltip} ${className}`}
28+
style={style}
29+
{...props}>
30+
{children}
31+
</ReachTooltip>
32+
);
33+
};
34+
35+
export default Tooltip;

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -248,28 +248,6 @@ function SettingsContextController({
248248
);
249249
}
250250

251-
function setStyleVariable(
252-
name: string,
253-
value: string,
254-
documentElements: DocumentElements,
255-
) {
256-
documentElements.forEach(documentElement =>
257-
documentElement.style.setProperty(name, value),
258-
);
259-
}
260-
261-
function updateStyleHelper(
262-
themeKey: string,
263-
style: string,
264-
documentElements: DocumentElements,
265-
) {
266-
setStyleVariable(
267-
`--${style}`,
268-
`var(--${themeKey}-${style})`,
269-
documentElements,
270-
);
271-
}
272-
273251
export function updateDisplayDensity(
274252
displayDensity: DisplayDensity,
275253
documentElements: DocumentElements,
@@ -296,12 +274,6 @@ export function updateThemeVariables(
296274
// $FlowFixMe scrollbarColor is missing in CSSStyleDeclaration
297275
documentElement.style.scrollbarColor = `var(${`--${theme}-color-scroll-thumb`}) var(${`--${theme}-color-scroll-track`})`;
298276
});
299-
300-
// The ThemeProvider works by writing DOM style variables to an HTMLDivElement.
301-
// Because Portals render in a different DOM subtree, these variables don't propagate.
302-
// So we need to also set @reach/tooltip specific styles on the root.
303-
updateStyleHelper(theme, 'color-tooltip-background', documentElements);
304-
updateStyleHelper(theme, 'color-tooltip-text', documentElements);
305277
}
306278

307279
export {SettingsContext, SettingsContextController};

packages/react-devtools-shared/src/devtools/views/TabBar.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99

1010
import * as React from 'react';
1111
import {Fragment, useCallback} from 'react';
12-
import Tooltip from '@reach/tooltip';
1312
import Icon from './Icon';
1413

1514
import styles from './TabBar.css';
16-
import tooltipStyles from './Tooltip.css';
15+
import Tooltip from './Components/reach-ui/tooltip';
1716

1817
import type {IconType} from './Icon';
1918

@@ -127,7 +126,7 @@ export default function TabBar({
127126

128127
if (title) {
129128
button = (
130-
<Tooltip key={id} className={tooltipStyles.Tooltip} label={title}>
129+
<Tooltip key={id} label={title}>
131130
{button}
132131
</Tooltip>
133132
);

packages/react-devtools-shared/src/devtools/views/ThemeProvider.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,18 @@
88
*/
99

1010
import * as React from 'react';
11-
import {useContext, useMemo} from 'react';
12-
import {SettingsContext} from './Settings/SettingsContext';
13-
import {THEME_STYLES} from '../../constants';
11+
import useThemeStyles from './useThemeStyles';
1412

1513
export default function ThemeProvider({children}: {|children: React$Node|}) {
16-
const {theme, displayDensity, browserTheme} = useContext(SettingsContext);
14+
const themeStyle = useThemeStyles();
1715

18-
const style = useMemo(
19-
() => ({
16+
const style = React.useMemo(() => {
17+
return {
18+
...themeStyle,
2019
width: '100%',
2120
height: '100%',
22-
...THEME_STYLES[displayDensity],
23-
...THEME_STYLES[theme === 'auto' ? browserTheme : theme],
24-
}),
25-
[theme, browserTheme, displayDensity],
26-
);
21+
};
22+
}, [themeStyle]);
2723

2824
return <div style={style}>{children}</div>;
2925
}

packages/react-devtools-shared/src/devtools/views/Toggle.js

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

1010
import * as React from 'react';
1111
import {useCallback} from 'react';
12-
import Tooltip from '@reach/tooltip';
1312

1413
import styles from './Toggle.css';
15-
import tooltipStyles from './Tooltip.css';
14+
import Tooltip from './Components/reach-ui/tooltip';
1615

1716
type Props = {
1817
children: React$Node,
@@ -58,11 +57,7 @@ export default function Toggle({
5857
);
5958

6059
if (title) {
61-
toggle = (
62-
<Tooltip className={tooltipStyles.Tooltip} label={title}>
63-
{toggle}
64-
</Tooltip>
65-
);
60+
toggle = <Tooltip label={title}>{toggle}</Tooltip>;
6661
}
6762

6863
return toggle;
Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,10 @@
11
:root {
2-
/**
3-
* The light and dark theme styles below should be kept in sync with 'react-devtools-shared/src/constants'
4-
* They are repeated here because they're used by e.g. tooltips or context menus
5-
* which get rendered outside of the DOM subtree (where normal theme/styles are written).
6-
*/
7-
8-
/* Light theme */
9-
--light-color-scroll-thumb: #c2c2c2;
10-
--light-color-scroll-track: #fafafa;
11-
--light-color-tooltip-background: rgba(0, 0, 0, 0.9);
12-
--light-color-tooltip-text: #ffffff;
13-
14-
/* Dark theme */
15-
--dark-color-scroll-thumb: #afb3b9;
16-
--dark-color-scroll-track: #313640;
17-
--dark-color-tooltip-background: rgba(255, 255, 255, 0.95);
18-
--dark-color-tooltip-text: #000000;
19-
202
/* Font smoothing */
213
--font-smoothing: auto;
224

23-
/* Compact density */
24-
--compact-line-height-data: 18px;
25-
--compact-root-font-size: 16px;
26-
27-
/* Comfortable density */
28-
--comfortable-line-height-data: 22px;
29-
--comfortable-root-font-size: 20px;
30-
315
/* GitHub.com system fonts */
326
--font-family-monospace: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo,
337
Courier, monospace;
348
--font-family-sans: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica,
359
Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;
36-
}
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import {useContext, useMemo} from 'react';
11+
import {SettingsContext} from './Settings/SettingsContext';
12+
import {THEME_STYLES} from '../../constants';
13+
14+
const useThemeStyles = () => {
15+
const {theme, displayDensity, browserTheme} = useContext(SettingsContext);
16+
17+
const style = useMemo(
18+
() => ({
19+
...THEME_STYLES[displayDensity],
20+
...THEME_STYLES[theme === 'auto' ? browserTheme : theme],
21+
}),
22+
[theme, browserTheme, displayDensity],
23+
);
24+
25+
return style;
26+
};
27+
28+
export default useThemeStyles;

0 commit comments

Comments
 (0)