|
1 | 1 | import React from 'react';
|
2 | 2 | import deepmerge from 'deepmerge';
|
| 3 | +import css from '@styled-system/css'; |
3 | 4 | import * as ReachMenu from '@reach/menu-button';
|
| 5 | +import { createGlobalStyle } from 'styled-components'; |
4 | 6 | import { Element } from '../Element';
|
5 | 7 | import { Button } from '../Button';
|
6 | 8 | import { List } from '../List';
|
7 | 9 |
|
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 | + ); |
13 | 44 |
|
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 => ( |
15 | 54 | <Button
|
16 |
| - // @ts-ignore |
17 | 55 | as={ReachMenu.MenuButton}
|
18 | 56 | variant="link"
|
19 |
| - css={deepmerge(css, { width: 'auto' })} |
20 | 57 | {...props}
|
| 58 | + css={deepmerge({ width: 'auto' }, props.css || {})} |
21 | 59 | >
|
22 | 60 | {props.children}
|
23 | 61 | </Button>
|
24 | 62 | );
|
25 | 63 |
|
26 | 64 | 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}> |
41 | 66 | {props.children}
|
42 | 67 | </List>
|
43 | 68 | );
|
44 | 69 |
|
45 | 70 | 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} /> |
62 | 72 | );
|
63 | 73 |
|
64 | 74 | Menu.Button = MenuButton;
|
|
0 commit comments