Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tooltip component #738

Merged
merged 12 commits into from
May 1, 2020
89 changes: 89 additions & 0 deletions src/components/Tooltip/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { storiesOf } from '@storybook/react'
import * as React from 'react'
import styled from 'styled-components'

import { Icon } from '../Icon'
import { Tooltip } from './Tooltip'

storiesOf('Tooltip', module).add('all', () => (
<List>
<li>
<Tooltip
message="Pablo Diego José Francisco de Paula Juan Nepomuceno Cipriano de la Santísima Trinidad Ruiz Picasso"
balloonType="dark"
>
Default: Pablo Diego José Francisco de Paula Juan Nepomuceno Cipriano de la Santísima
Trinidad Ruiz Picasso
</Tooltip>
</li>
<li>
<Tooltip message="invisible message" ellipsisOnly={true}>
Ellipsis Only
</Tooltip>
<Tooltip
message="Pablo Diego José Francisco de Paula Juan Nepomuceno Cipriano de la Santísima Trinidad Ruiz Picasso"
ellipsisOnly={true}
>
<Text>
Ellipsis Only: Pablo Diego José Francisco de Paula Juan Nepomuceno Cipriano de la
Santísima Trinidad Ruiz Picasso
</Text>
</Tooltip>
</li>
<li>
<Tooltip
message="Pablo Diego José Francisco de Paula Juan Nepomuceno Cipriano de la Santísima Trinidad Ruiz Picasso"
multiLine={true}
>
MultiLineMessage: Pablo Diego José Francisco de Paula Juan Nepomuceno Cipriano de la
Santísima Trinidad Ruiz Picasso
</Tooltip>
</li>
<li>
<Tooltip
message={
<>
MultiLineMessage
<br />
MultiLine 1<br />
MultiLine 2<br />
MultiLine 3<br />
...
</>
}
multiLine={true}
>
<Text>
MultiLineMessage: Pablo Diego José Francisco de Paula Juan Nepomuceno Cipriano de la
Santísima Trinidad Ruiz Picasso
</Text>
</Tooltip>
</li>
<li>
<Tooltip message="Icon Message" triggerType="icon">
<Icon name="fa-question-circle" />
</Tooltip>
</li>
</List>
))

const List = styled.ul`
width: 200px;
margin-top: 100px;
padding: 0 24px;
list-style: none;

& > li {
&:not(:first-child) {
margin-top: 16px;
}
}
`

const Text = styled.span`
display: inline-block;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
`
103 changes: 103 additions & 0 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import React, { useState } from 'react'
import styled, { css } from 'styled-components'
import { LightBalloon, DarkBalloon } from '../Balloon'
import { useTheme, Theme } from '../../hooks/useTheme'

interface Prop {
message: React.ReactNode
children: React.ReactNode
triggerType?: 'icon' | 'text'
multiLine?: boolean
ellipsisOnly?: boolean
balloonType?: 'light' | 'dark'
}

export const Tooltip: React.FC<Prop> = ({
message,
children,
triggerType,
multiLine,
ellipsisOnly = false,
balloonType = 'light',
}) => {
const theme = useTheme()
const [isVisible, setIsVisible] = useState(false)

const className = [triggerType === 'icon' ? 'icon-tooltip' : '', multiLine ? 'multi-line' : '']
.filter(c => !!c)
.join(' ')
const ref = React.createRef<HTMLSpanElement>()

const overAction = () => {
if (!ellipsisOnly) {
setIsVisible(true)

return
}

const wrapper = ref.current!
const parentWidth = parseInt(
window.getComputedStyle(wrapper.parentNode! as HTMLElement, null).width.match(/\d+/)![0],
10,
)

if (parentWidth <= wrapper.clientWidth) {
setIsVisible(true)
}
}
const outAction = () => {
setIsVisible(false)
}
const StyledBalloon = balloonType === 'light' ? StyledLightBalloon : StyledDarkBallon
AtsushiM marked this conversation as resolved.
Show resolved Hide resolved

return (
<Wrapper
ref={ref}
onMouseOver={overAction}
onTouchStart={overAction}
onMouseOut={outAction}
onTouchEnd={outAction}
AtsushiM marked this conversation as resolved.
Show resolved Hide resolved
>
{isVisible && (
<StyledBalloon horizontal="left" vertical="bottom" className={className}>
<StyledBalloonText themes={theme}>{message}</StyledBalloonText>
</StyledBalloon>
)}
{children}
</Wrapper>
)
}

const Wrapper = styled.span`
position: relative;
display: inline-block;
max-width: 100%;
`

const StyledLightBalloon = styled(LightBalloon)`
position: absolute;
left: 0;
bottom: calc(100% + 10px);

&.multi-line {
width: 100%;
white-space: normal;
}

&.icon-tooltip {
left: calc(-100% - 5px);
}
`
const StyledDarkBallon = StyledLightBalloon.withComponent(DarkBalloon)
AtsushiM marked this conversation as resolved.
Show resolved Hide resolved

const StyledBalloonText = styled.p<{ themes: Theme }>`
margin: 0;
${props => {
const { themes } = props
const { size } = themes

return css`
padding: ${size.pxToRem(size.space.XXS)} ${size.pxToRem(size.space.XS)};
`
}}
`
1 change: 1 addition & 0 deletions src/components/Tooltip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Tooltip'