Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import * as React from 'react'
  2. import PropTypes from 'prop-types'
  3. import styled, { css } from 'styled-components'
  4. import { font } from 'styled-theme'
  5. import { prop, withProp, ifProp } from 'styled-tools'
  6. import { color, isiOS } from '../../../utils/theme-utils'
  7.  
  8. const FormPlace = css`
  9. position: absolute;
  10. right: 0px;
  11. bottom: 20px;
  12. `
  13.  
  14. const Wrapper = styled.div`
  15. position: relative;
  16. width: 40px;
  17. display: inline;
  18. ${ifProp('inInput', FormPlace, '')}
  19. `
  20.  
  21. const QuestionIcon = styled.div`
  22. font-size: 26px;
  23. padding: 4px 16px;
  24. font-family: ${font('primary')};
  25. color: ${withProp('shade', shade => color(prop('palette', 'greyscale'), shade))};
  26. transition: all .2s ease-in-out;
  27. cursor: pointer;
  28.  
  29. &:hover {
  30. color: ${color('greyscale', 800)};
  31. }
  32. `
  33. const TooltipBody = styled.div`
  34. position: absolute;
  35. box-sizing: border-box;
  36. font-family: ${font('primary')};
  37. background-color: ${color('greyscale', 800)};
  38. width: 281px;
  39. box-shadow: 0 3px 6px 0 ${color('greyscale', 900, 0.16)};
  40. line-height: 18px;
  41. text-align: left;
  42. padding: 15px;
  43. z-index: 1;
  44. left: -236px;
  45. top: 50px;
  46. border-radius: 4px;
  47.  
  48. &:after {
  49. content: '';
  50. top: -9px;
  51. right: 14px;
  52. position: absolute;
  53. border-left: 10px solid transparent;
  54. border-right: 10px solid transparent;
  55. border-bottom: 10px solid ${color('greyscale', 800)};
  56. }
  57. `
  58. function doNothing() {}
  59. export class Tooltip extends React.Component {
  60. constructor(props) {
  61. super(props)
  62. this.state = {
  63. openedTooltip: false,
  64. }
  65. this.openTooltip = this.openTooltip.bind(this)
  66. this.closeTooltip = this.closeTooltip.bind(this)
  67. if (isiOS) {
  68. document.getElementById('app').addEventListener('click', doNothing)
  69. }
  70. }
  71. componentWillUnmount() {
  72. if (isiOS) {
  73. document.getElementById('app').removeEventListener('click', doNothing)
  74. }
  75. }
  76.  
  77. openTooltip() {
  78. this.setState({ openedTooltip: true })
  79. }
  80.  
  81. closeTooltip() {
  82. if (this.state.openedTooltip) {
  83. this.setState({
  84. openedTooltip: false,
  85. })
  86. }
  87. }
  88.  
  89. render() {
  90. return (
  91. <Wrapper inInput={this.props.inInput}>
  92. <QuestionIcon
  93. shade={this.props.shade}
  94. palette={this.props.palette}
  95. onMouseOver={this.openTooltip}
  96. onFocus={this.openTooltip}
  97. onMouseLeave={this.closeTooltip}
  98. onBlur={this.closeTooltip}
  99. >?
  100. </QuestionIcon>
  101. {this.state.openedTooltip && (
  102. <TooltipBody>
  103. {this.props.children}
  104. </TooltipBody>
  105. )}
  106. </Wrapper>
  107. )
  108. }
  109. }
  110.  
  111. Tooltip.propTypes = {
  112. children: PropTypes.node,
  113. inInput: PropTypes.bool,
  114. palette: PropTypes.string,
  115. shade: PropTypes.number,
  116. }
  117.  
  118. Tooltip.defaultProps = {
  119. palette: 'greyscale',
  120. shade: 600,
  121. inInput: false,
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement