Skip to content

Commit e014ea6

Browse files
authored
make reach menu look good with global styles (codesandbox#3592)
1 parent 3b3af83 commit e014ea6

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

packages/components/src/components/Menu/index.tsx

+48-38
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,74 @@
11
import React from 'react';
22
import deepmerge from 'deepmerge';
3+
import css from '@styled-system/css';
34
import * as ReachMenu from '@reach/menu-button';
5+
import { createGlobalStyle } from 'styled-components';
46
import { Element } from '../Element';
57
import { Button } from '../Button';
68
import { List } from '../List';
79

8-
const Menu = ({ ...props }) => (
9-
<Element as={ReachMenu.Menu} {...props}>
10-
{props.children}
11-
</Element>
12-
);
10+
const Menu = ({ ...props }) => {
11+
const PortalStyles = createGlobalStyle(
12+
css({
13+
'[data-reach-menu]': {
14+
zIndex: 2,
15+
},
16+
'[data-reach-menu-list][data-component=MenuList]': {
17+
minWidth: 100,
18+
backgroundColor: 'menuList.background',
19+
borderRadius: 3,
20+
boxShadow: 2,
21+
overflow: 'hidden',
22+
border: '1px solid',
23+
borderColor: 'menuList.border',
24+
':focus': { outline: 'none' },
25+
// override reach ui styles
26+
padding: 0,
27+
},
28+
'[data-reach-menu-item][data-component=MenuItem]': {
29+
fontSize: 2,
30+
paddingY: 2,
31+
paddingX: 3,
32+
cursor: 'pointer',
33+
outline: 'none',
34+
color: 'menuList.foreground',
35+
'&[data-selected]': {
36+
outline: 'none',
37+
backgroundColor: 'menuList.hoverBackground',
38+
},
39+
// override reach ui styles
40+
font: 'ineherit',
41+
},
42+
})
43+
);
1344

14-
const MenuButton = ({ css = {}, ...props }) => (
45+
return (
46+
<Element as={ReachMenu.Menu} data-component="Menu" {...props}>
47+
<PortalStyles />
48+
{props.children}
49+
</Element>
50+
);
51+
};
52+
53+
const MenuButton = props => (
1554
<Button
16-
// @ts-ignore
1755
as={ReachMenu.MenuButton}
1856
variant="link"
19-
css={deepmerge(css, { width: 'auto' })}
2057
{...props}
58+
css={deepmerge({ width: 'auto' }, props.css || {})}
2159
>
2260
{props.children}
2361
</Button>
2462
);
2563

2664
const MenuList = props => (
27-
<List
28-
as={ReachMenu.MenuList}
29-
css={{
30-
minWidth: 100,
31-
backgroundColor: 'menuList.background',
32-
borderRadius: 3,
33-
boxShadow: 2,
34-
overflow: 'hidden',
35-
border: '1px solid',
36-
borderColor: 'menuList.border',
37-
':focus': { outline: 'none' },
38-
}}
39-
{...props}
40-
>
65+
<List as={ReachMenu.MenuList} data-component="MenuList" {...props}>
4166
{props.children}
4267
</List>
4368
);
4469

4570
const MenuItem = props => (
46-
<Element
47-
as={ReachMenu.MenuItem}
48-
css={{
49-
fontSize: 2,
50-
paddingY: 2,
51-
paddingX: 3,
52-
cursor: 'pointer',
53-
outline: 'none',
54-
color: 'menuList.foreground',
55-
'&[data-selected]': {
56-
outline: 'none',
57-
backgroundColor: 'menuList.hoverBackground',
58-
},
59-
}}
60-
{...props}
61-
/>
71+
<Element as={ReachMenu.MenuItem} data-component="MenuItem" {...props} />
6272
);
6373

6474
Menu.Button = MenuButton;

packages/components/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export * from './components/FormField';
2424
export * from './components/Tags';
2525
export * from './components/Tags/Tag';
2626
export * from './components/TagInput';
27+
export * from './components/Menu';
2728

2829
// layout
2930
export * from './components/Grid';

0 commit comments

Comments
 (0)