Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. // @flow
  2. import React, { memo } from "react";
  3. import Downshift from "downshift";
  4. import styled from "@emotion/styled";
  5. import { css } from "@emotion/core";
  6. import theme from "../../../theme";
  7.  
  8. type ListItemsprops = {
  9. label: string,
  10. value: any,
  11. props: any
  12. }
  13. type Props = {
  14. bg: string,
  15. listItems: Array<ListItemsprops>,
  16. icon?: Node,
  17. clsNameButton: string,
  18. clsNameMenu: string,
  19. clsNameMenuItem: string,
  20. onChange: function
  21. };
  22.  
  23. export const DropdownMenu = ({
  24. icon,
  25. listItems = [],
  26. bg = "rgba(255,255,255,0)",
  27. clsNameButton = "",
  28. clsNameMenu = "",
  29. clsNameMenuItem = "",
  30. onChange = fn => fn
  31. }: Props) => {
  32. return (
  33. <Downshift onChange={onChange}>
  34. {({
  35. getItemProps,
  36. getMenuProps,
  37. isOpen,
  38. highlightedIndex,
  39. selectedItem,
  40. getToggleButtonProps,
  41. getRootProps
  42. }) => {
  43. return (
  44. <Box {...getRootProps()}>
  45. <MenuToggle bg={bg} {...getToggleButtonProps()} className={clsNameButton}>
  46. {icon ? icon : (<><i /><i /><i /></>)}
  47. </MenuToggle>
  48. {isOpen
  49. ? (
  50. <Menu {...getMenuProps()} className={clsNameMenu}>
  51. {
  52. listItems
  53. .map((item, index) => (
  54. <MenuItem
  55. {...getItemProps({
  56. key: item.label,
  57. index,
  58. item,
  59. style: {
  60. color: highlightedIndex === index ? theme.colors.links : 'inherit',
  61. fontWeight: selectedItem === item ? '500' : '400',
  62. },
  63. })}
  64. className={clsNameMenuItem}
  65. {...item.props}
  66. >
  67. {item.value}
  68. </MenuItem>
  69. ))
  70. }
  71. </Menu>
  72. ) : null}
  73. </Box>
  74. )
  75. }}
  76. </Downshift>
  77. );
  78. };
  79.  
  80. const Box = styled.div`
  81. position: relative;
  82. width: 1em;
  83. height: 1em;
  84. z-index: 2;
  85. `;
  86. const MenuToggle = styled.div`
  87. width: 3rem;
  88. height: 3rem;
  89. border-radius: 3rem;
  90. cursor: pointer;
  91. position: absolute;
  92. left: 50%;
  93. top: 50%;
  94. transform: translate(-50%, -50%);
  95. display: flex;
  96. flex-direction: column;
  97. align-items: center;
  98. justify-content: center;
  99. background: ${({ bg }) => bg};
  100.  
  101. i {
  102. display: block;
  103. width: .4rem;
  104. height: .4rem;
  105. border-radius: .3rem;
  106. margin: 0.2rem auto;
  107. background: ${theme.colors.primary};
  108. opacity: 0.5;
  109. }
  110.  
  111. &:hover i {
  112. opacity: 0.75;
  113. }
  114. `;
  115. const Menu = styled.ul`
  116. position: absolute;
  117. top: 100%;
  118. right: 0;
  119. background: #fff;
  120. border-radius: 0.5rem;
  121. box-shadow: 0.5rem 0.5rem 0.5rem rgba(0, 0, 0, 0.15);
  122. padding: 1.2rem 0;
  123. margin: 1rem 0 0;
  124. min-width: 20rem;
  125. list-style: none;
  126. `;
  127. const MenuItem = styled.li`
  128. padding: .6rem 2.4rem;
  129. color: inherit;
  130.  
  131. &:hover {
  132. color: ${theme.colors.links}
  133. }
  134. `;
  135.  
  136. export default memo(DropdownMenu);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement