Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions components/tooltip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { trimList, getOtherProps, $, SVG } from '../util'

import { StyledToolTip, StyledToolTipSpan } from './styled'

const EVENT_NAME_LIST = ['hover', 'click']
const EVENT_NAME_LIST = [ 'hover', 'click' ]

const TIP_ROOT_ID = 'IBOT_TOOLTIP_ROOT'

Expand All @@ -30,7 +30,7 @@ function parseContent (content, eventName = 'hover') {
isString(content) || isArray(content) || isValidElement(content)
? content
: EVENT_NAME_LIST.includes(eventName) && isObject(content)
? content[eventName] || content.hover
? content[ eventName ] || content.hover
: null
)
}
Expand All @@ -39,12 +39,12 @@ export default class Tooltip extends PureComponent {
state = {
isOpen: false,
isClicked: false,
$text: null,
$text: null
}

static propTypes = {
theme: PropTypes.oneOf(['core', 'plain']),
position: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).isRequired,
theme: PropTypes.oneOf([ 'core', 'plain' ]),
position: PropTypes.oneOf([ 'top', 'right', 'bottom', 'left' ]).isRequired,
arrowed: PropTypes.bool,
inflexible: PropTypes.bool,

Expand All @@ -55,23 +55,23 @@ export default class Tooltip extends PureComponent {
PropTypes.node,
PropTypes.shape(
EVENT_NAME_LIST.reduce(
(res, n) => Object.assign(res, { [n]: PropTypes.node }),
{},
),
),
(res, n) => Object.assign(res, { [ n ]: PropTypes.node }),
{}
)
)
]),

onMouseEnter: PropTypes.func,
onClick: PropTypes.func,
onMouseLeave: PropTypes.func,

delay: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
duration: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
delay: PropTypes.oneOfType([ PropTypes.number, PropTypes.string ]),
duration: PropTypes.oneOfType([ PropTypes.number, PropTypes.string ]),

children: PropTypes.node,
html: PropTypes.string,

setRef: PropTypes.func,
setRef: PropTypes.func
}

static defaultProps = {
Expand All @@ -86,7 +86,7 @@ export default class Tooltip extends PureComponent {
tipClassName: '',

setRef: () => null,
onClick: () => null,
onClick: () => null
}

ref = React.createRef()
Expand All @@ -97,6 +97,8 @@ export default class Tooltip extends PureComponent {

componentDidUpdate (_, { isOpen: wasOpen }) {
const { duration } = this.props
if (!duration) return null

const { isOpen } = this.state

if (duration > 0 && !wasOpen && isOpen) {
Expand All @@ -112,7 +114,7 @@ export default class Tooltip extends PureComponent {
onClick = e => {
this.setState({
isOpen: !!parseContent(this.props.content, 'click'),
isClicked: true,
isClicked: true
})

e.persist()
Expand All @@ -123,18 +125,18 @@ export default class Tooltip extends PureComponent {
hoverTimeout: setTimeout(
() => this.setState(
{ isOpen: !!parseContent(this.props.content, 'hover') },
this.props.onMouseEnter,
this.props.onMouseEnter
),
this.props.delay,
),
this.props.delay
)
})

onMouseLeave = () => {
clearTimeout(this.hoverTimeout)

this.setState(
{ isOpen: false, isClicked: false },
this.props.onMouseLeave,
this.props.onMouseLeave
)
}

Expand All @@ -146,7 +148,7 @@ export default class Tooltip extends PureComponent {
content,

html,
children,
children
} = this.props

const { isOpen, isClicked } = this.state
Expand All @@ -155,7 +157,7 @@ export default class Tooltip extends PureComponent {
'Tooltip',
className,
isOpen ? 'is-open' : '',
isClicked ? 'is-clicked' : '',
isClicked ? 'is-clicked' : ''
])

const eventName = isClicked ? 'click' : 'hover'
Expand All @@ -172,9 +174,9 @@ export default class Tooltip extends PureComponent {
<>
{ html ? <span dangerouslySetInnerHTML={{ __html: html }} /> : children }

<Tip
{isOpen && <Tip
$text={this.ref.current}
isOpen={isOpen}
isOpen={true}
className={tipClassName}
eventName={eventName}

Expand All @@ -185,7 +187,7 @@ export default class Tooltip extends PureComponent {
>
{parseContent(content, eventName)}
</Tip>
</>
} </>
</StyledToolTipSpan>
)
}
Expand All @@ -196,7 +198,7 @@ class Tip extends PureComponent {
prevProps: this.props,

isOpen: this.props.isOpen,
position: this.props.position,
position: this.props.position
}

static propTypes = {
Expand All @@ -205,20 +207,20 @@ class Tip extends PureComponent {
eventName: PropTypes.oneOf(EVENT_NAME_LIST),
$text: PropTypes.instanceOf(Element),

theme: PropTypes.oneOf(['plain', 'core']),
position: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
theme: PropTypes.oneOf([ 'plain', 'core' ]),
position: PropTypes.oneOf([ 'top', 'right', 'bottom', 'left' ]),
inflexible: PropTypes.bool,
arrowed: PropTypes.bool,

children: PropTypes.node,
children: PropTypes.node
}

static getDerivedStateFromProps (props, { prevProps }) {
if (!isEqual(prevProps, props)) {
return {
prevProps: props,
isOpen: props.isOpen,
position: props.position,
position: props.position
}
}
return null
Expand Down Expand Up @@ -253,8 +255,8 @@ class Tip extends PureComponent {
if (!$text || !$tipBase || !$tip) return

const flexible = !inflexible
const [minX, minY] = [10, 10]
const [maxX, maxY] = [window.innerWidth - 10, window.innerHeight - 10]
const [ minX, minY ] = [ 10, 10 ]
const [ maxX, maxY ] = [ window.innerWidth - 10, window.innerHeight - 10 ]

const { top, right, bottom, left } = $text.getBoundingClientRect()
const { offsetWidth: wOf$text, offsetHeight: hOf$text } = $text
Expand All @@ -272,7 +274,7 @@ class Tip extends PureComponent {
top: `${top}px`,
left: `${left}px`,
width: `${wOf$text}px`,
height: `${hOf$text}px`,
height: `${hOf$text}px`
})

// Main-axis position adjustment:
Expand Down Expand Up @@ -359,7 +361,7 @@ class Tip extends PureComponent {
className,
`on-${position}`,
inflexible && 'inflexible',
arrowed && 'arrowed',
arrowed && 'arrowed'
])

return isOpen && (
Expand Down