-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tooltip): add tooltip component
- Loading branch information
1 parent
35deca0
commit 26b2962
Showing
5 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
name: Tooltip | ||
menu: Components | ||
route: /tooltip | ||
--- | ||
|
||
import { Playground, Props } from 'docz'; | ||
import { Tooltip, Menu, Button, Trash } from '../../index.ts'; | ||
|
||
# Popover | ||
|
||
A Popover is used to display extra information or actions in a compact way. | ||
|
||
## Best practices | ||
|
||
- Make sure the correct popover events are covered, depending on the event. (Click, hover, focus, etc...). | ||
|
||
## Examples | ||
|
||
### Basic usage | ||
|
||
<Playground> | ||
<Tooltip content="More info here"> | ||
<Button>Hover here</Button> | ||
</Tooltip> | ||
</Playground> | ||
|
||
### Bottom placement | ||
|
||
<Playground> | ||
<Tooltip placement="bottom" content="This is placed on the bottom"> | ||
<Button>Hover here</Button> | ||
</Tooltip> | ||
</Playground> | ||
|
||
## API | ||
|
||
<Props of={Tooltip} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.content { | ||
border-radius: var(--border-radius-small); | ||
background-color: var(--color-neutral-8); | ||
color: var(--color-neutral-1); | ||
padding: 8px 14px; | ||
box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.1); | ||
z-index: 100; | ||
transition: opacity 0.24s; | ||
opacity: 1; | ||
} | ||
|
||
.content.enter, | ||
.content.exit { | ||
opacity: 0; | ||
} | ||
|
||
.triggerContainer { | ||
display: inline-block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { fireEvent, render, waitFor } from '@testing-library/react'; | ||
|
||
import { Button } from '../../index'; | ||
import Tooltip from './Tooltip'; | ||
|
||
describe('Tooltip', () => { | ||
const component = ( | ||
<Tooltip content="This is more info"> | ||
<Button>Hover over me</Button> | ||
</Tooltip> | ||
); | ||
|
||
test('show on hover', async () => { | ||
const { queryByText, getByText } = render(component); | ||
const button = getByText('Hover over me'); | ||
|
||
expect(queryByText('This is more info')).toBe(null); | ||
|
||
await waitFor(() => { | ||
fireEvent.mouseEnter(button); | ||
}); | ||
|
||
expect(queryByText('This is more info')).not.toBe(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useRef, useState } from 'react'; | ||
import { usePopper } from 'react-popper'; | ||
import { CSSTransition } from 'react-transition-group'; | ||
import { Placement } from '@popperjs/core'; | ||
import classnames from 'classnames'; | ||
|
||
import styles from './Tooltip.module.css'; | ||
|
||
interface Props { | ||
content?: React.ReactNode; | ||
children?: React.ReactNode; | ||
placement?: Placement; | ||
className?: string; | ||
} | ||
|
||
const Popover: React.FC<Props> = ({ content, children, placement = 'top', className }: Props) => { | ||
const [isShown, setIsShown] = useState(false); | ||
const divRef = useRef<HTMLDivElement>(null); | ||
const contentRef = useRef<HTMLDivElement>(null); | ||
const [arrowRef, setArrowRef] = useState<HTMLDivElement | null>(null); | ||
const contentClassNames = classnames(styles.content, className); | ||
|
||
const hide = () => setIsShown(false); | ||
const show = () => setIsShown(true); | ||
|
||
const { styles: popperStyles, attributes } = usePopper(divRef.current, contentRef.current, { | ||
modifiers: [ | ||
{ | ||
name: 'offset', | ||
options: { | ||
offset: [0, 10], | ||
}, | ||
}, | ||
{ | ||
name: 'arrow', | ||
options: { | ||
element: arrowRef, | ||
}, | ||
}, | ||
], | ||
placement, | ||
}); | ||
|
||
return ( | ||
<> | ||
<div className={styles.triggerContainer} ref={divRef} onMouseEnter={show} onMouseLeave={hide}> | ||
{children} | ||
</div> | ||
|
||
<CSSTransition | ||
unmountOnExit | ||
mountOnEnter | ||
timeout={240} | ||
in={isShown} | ||
classNames={{ enter: styles.enter, exit: styles.exit }} | ||
> | ||
<div | ||
ref={contentRef} | ||
style={popperStyles.popper} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...attributes.popper} | ||
className={contentClassNames} | ||
> | ||
<div ref={setArrowRef} style={popperStyles.arrow} className="arrow" /> | ||
{content} | ||
</div> | ||
</CSSTransition> | ||
</> | ||
); | ||
}; | ||
|
||
export default Popover; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters