DocsHelper.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React from "react";
  2. import { ClickAwayListener } from "@material-ui/core";
  3. import styled from "styled-components";
  4. type Props = {
  5. tooltipText: string;
  6. link?: string;
  7. placement?: TooltipPlacement;
  8. disableMargin?: boolean;
  9. };
  10. const DocsHelper: React.FC<Props> = ({
  11. tooltipText,
  12. link,
  13. placement,
  14. disableMargin,
  15. }) => {
  16. const [open, setOpen] = React.useState(false);
  17. const handleTooltipClose = () => {
  18. setOpen(false);
  19. };
  20. const handleTooltipOpen = () => {
  21. setOpen(true);
  22. };
  23. const handleTooltipToggle = () => {
  24. setOpen(!open);
  25. };
  26. return (
  27. <DocsHelperContainer disableMargin={disableMargin}>
  28. <ClickAwayListener
  29. onClickAway={() => {
  30. handleTooltipClose();
  31. }}
  32. >
  33. <div>
  34. <HelperButton onClick={handleTooltipToggle}>
  35. <Icon className="material-icons">help_outline</Icon>
  36. </HelperButton>
  37. {open && (
  38. <Tooltip placement={placement}>
  39. <StyledContent onClick={handleTooltipOpen}>
  40. {tooltipText}
  41. {link && (
  42. <A target="_blank" href={link}>
  43. Documentation {">"}
  44. </A>
  45. )}
  46. </StyledContent>
  47. </Tooltip>
  48. )}
  49. </div>
  50. </ClickAwayListener>
  51. </DocsHelperContainer>
  52. );
  53. };
  54. export default DocsHelper;
  55. type TooltipPlacement = "top-end" | "bottom-end" | "top-start" | "bottom-start";
  56. const Tooltip = styled.div<{ placement: TooltipPlacement }>`
  57. position: absolute;
  58. ${({ placement }) => {
  59. switch (placement) {
  60. case "top-start":
  61. return `
  62. bottom: 25px;
  63. left: 0px;
  64. `;
  65. case "bottom-end":
  66. return `
  67. top: 25px;
  68. right: 0px;
  69. `;
  70. case "bottom-start":
  71. return `
  72. top: 25px;
  73. left: 0px;
  74. `;
  75. case "top-end":
  76. default:
  77. return `
  78. bottom: 25px;
  79. right: 0px;
  80. `;
  81. }
  82. }}
  83. word-wrap: break-word;
  84. min-height: 18px;
  85. width: fit-content;
  86. padding: 5px 7px;
  87. z-index: 999;
  88. display: flex;
  89. flex-direction: column;
  90. justify-content: center;
  91. flex: 1;
  92. color: white;
  93. text-transform: none;
  94. opacity: 0;
  95. animation: faded-in 0.2s 0.15s;
  96. animation-fill-mode: forwards;
  97. @keyframes faded-in {
  98. from {
  99. opacity: 0;
  100. }
  101. to {
  102. opacity: 1;
  103. }
  104. }
  105. `;
  106. const StyledContent = styled.div`
  107. font-size: 12px;
  108. font-weight: normal;
  109. padding: 12px 14px;
  110. line-height: 1.5em;
  111. user-select: text;
  112. width: max-content;
  113. max-width: 300px;
  114. height: calc(100% + 10px);
  115. margin-left: -7px;
  116. height: 100%;
  117. background: #2e3135;
  118. border: 1px solid #aaaabb;
  119. border-radius: 5px;
  120. `;
  121. const HelperButton = styled.div`
  122. cursor: pointer;
  123. display: flex;
  124. align-items: center;
  125. margin-left: 10px;
  126. justify-content: center;
  127. > i {
  128. color: #aaaabb;
  129. width: 24px;
  130. height: 24px;
  131. font-size: 20px;
  132. border-radius: 20px;
  133. }
  134. `;
  135. const A = styled.a`
  136. display: inline-block;
  137. height: 20px;
  138. color: #8590ff;
  139. text-decoration: underline;
  140. cursor: pointer;
  141. margin-top: 10px;
  142. width: 100%;
  143. text-align: right;
  144. user-select: none;
  145. `;
  146. const DocsHelperContainer = styled.div<{ disableMargin: boolean }>`
  147. ${(props) => {
  148. if (props.disableMargin) {
  149. return "";
  150. }
  151. return `margin-left: auto;`;
  152. }}
  153. position: relative;
  154. `;
  155. const Icon = styled.i`
  156. display: flex;
  157. align-items: center;
  158. justify-content: center;
  159. `;