-
Notifications
You must be signed in to change notification settings - Fork 90
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!: merge old Tooltip with ActionTooltip and introduce new Tooltip #1189
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f9a1be3
feat: merged old Tooltip with ActionTooltip; introduced new Tooltip c…
DenisVershkov 405d271
fix: returned qa and disable portal props
DenisVershkov 9066ccc
feat: added README files; updated tests; changed code strucure
DenisVershkov e604fe7
chore: removed unused classes; updated content type; updated storyboo…
DenisVershkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,44 +1,89 @@ | ||
import React from 'react'; | ||
|
||
import {Hotkey} from '../Hotkey'; | ||
import type {HotkeyProps} from '../Hotkey'; | ||
import {Tooltip} from '../Tooltip'; | ||
import type {TooltipProps} from '../Tooltip'; | ||
import {useForkRef} from '../../hooks'; | ||
import {type TooltipDelayProps, useTooltipVisible} from '../../hooks/private'; | ||
import {Hotkey, type HotkeyProps} from '../Hotkey'; | ||
import {Popup, type PopupPlacement} from '../Popup'; | ||
import type {DOMProps, QAProps} from '../types'; | ||
import {block} from '../utils/cn'; | ||
|
||
import './ActionTooltip.scss'; | ||
|
||
const b = block('action-tooltip'); | ||
|
||
export interface ActionTooltipProps | ||
extends Pick< | ||
TooltipProps, | ||
'children' | 'disabled' | 'placement' | 'openDelay' | 'closeDelay' | 'className' | 'qa' | ||
> { | ||
export interface ActionTooltipProps extends QAProps, DOMProps, TooltipDelayProps { | ||
id?: string; | ||
disablePortal?: boolean; | ||
contentClassName?: string; | ||
disabled?: boolean; | ||
placement?: PopupPlacement; | ||
children: React.ReactElement; | ||
title: string; | ||
hotkey?: HotkeyProps['value']; | ||
description?: React.ReactNode; | ||
} | ||
|
||
const DEFAULT_PLACEMENT: PopupPlacement = ['bottom', 'top']; | ||
const b = block('action-tooltip'); | ||
|
||
export function ActionTooltip(props: ActionTooltipProps) { | ||
const {title, hotkey, description, children, ...tooltipProps} = props; | ||
const { | ||
placement = DEFAULT_PLACEMENT, | ||
title, | ||
hotkey, | ||
children, | ||
className, | ||
contentClassName, | ||
description, | ||
disabled = false, | ||
style, | ||
qa, | ||
id, | ||
disablePortal, | ||
...delayProps | ||
} = props; | ||
|
||
const [anchorElement, setAnchorElement] = React.useState<HTMLElement | null>(null); | ||
const tooltipVisible = useTooltipVisible(anchorElement, delayProps); | ||
|
||
const renderPopup = () => { | ||
return ( | ||
<Popup | ||
id={id} | ||
disablePortal={disablePortal} | ||
role="tooltip" | ||
className={b(null, className)} | ||
style={style} | ||
open={tooltipVisible && !disabled} | ||
placement={placement} | ||
anchorRef={{current: anchorElement}} | ||
disableEscapeKeyDown | ||
disableOutsideClick | ||
disableLayer | ||
qa={qa} | ||
> | ||
<div className={b('content', contentClassName)}> | ||
<React.Fragment> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fragment is not necessary here |
||
<div className={b('heading')}> | ||
<div className={b('title')}>{title}</div> | ||
{hotkey && ( | ||
<Hotkey view="dark" value={hotkey} className={b('hotkey')} /> | ||
)} | ||
</div> | ||
{description && <div className={b('description')}>{description}</div>} | ||
</React.Fragment> | ||
</div> | ||
</Popup> | ||
); | ||
}; | ||
|
||
const child = React.Children.only(children); | ||
const childRef = (child as any).ref; | ||
|
||
const ref = useForkRef(setAnchorElement, childRef); | ||
|
||
return ( | ||
<Tooltip | ||
{...tooltipProps} | ||
className={b(null, tooltipProps.className)} | ||
contentClassName={b('layout')} | ||
content={ | ||
<React.Fragment> | ||
<div className={b('heading')}> | ||
<div className={b('title')}>{title}</div> | ||
{hotkey && <Hotkey view="dark" value={hotkey} className={b('hotkey')} />} | ||
</div> | ||
{description && <div className={b('description')}>{description}</div>} | ||
</React.Fragment> | ||
} | ||
> | ||
{children} | ||
</Tooltip> | ||
<React.Fragment> | ||
{React.cloneElement(child, {ref})} | ||
{anchorElement ? renderPopup() : null} | ||
</React.Fragment> | ||
); | ||
} |
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,35 @@ | ||
<!--GITHUB_BLOCK--> | ||
|
||
# ActionTooltip | ||
|
||
<!--/GITHUB_BLOCK--> | ||
|
||
A simple text tip that uses its children node as an anchor. For correct functioning, the anchor node | ||
must be able to handle mouse events and focus or blur events. | ||
|
||
## Usage | ||
|
||
```tsx | ||
import {ActionTooltip} from '@gravity-ui/uikit'; | ||
|
||
<ActionTooltip title="Content"> | ||
<div tabIndex={0}>Anchor</div> | ||
</ActionTooltip>; | ||
``` | ||
|
||
## Properties | ||
|
||
| Name | Description | Type | Default | | ||
| :--------------- | --------------------------------------------------------------------------------------- | :----------------------------------------------: | :-----: | | ||
| children | An anchor element for a `Tooltip`. Must accept a `ref` that will provide a DOM element. | `React.ReactElement` | | | ||
| closeDelay | Number of ms to delay hiding the `Tooltip` after the hover ends | `number` | `0` | | ||
| openDelay | Number of ms to delay showing the `Tooltip` after the hover begins | `number` | `250` | | ||
| placement | `Tooltip` position relative to its anchor | [`PopupPlacement`](../Popup/README.md#placement) | | | ||
| qa | HTML `data-qa` attribute, used in tests | `string` | | | ||
| title | Tooltip title text | `string` | | | ||
| description | Tooltip description text | `string` | | | ||
| hotkey | Hot keys that are assigned to an interface action. | `string` | | | ||
| id | This prop is used to help implement the accessibility logic. | `string` | | | ||
| disablePortal | Do not use Portal for children | `boolean` | | | ||
| contentClassName | HTML class attribute for content node | `string` | | | ||
| disabled | Prevent popup from opening | `boolean` | `false` | |
128 changes: 128 additions & 0 deletions
128
src/components/ActionTooltip/__tests__/ActionTooltip.tsx
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,128 @@ | ||
import React from 'react'; | ||
|
||
import {createEvent, fireEvent, render, screen} from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import {ActionTooltip} from '../ActionTooltip'; | ||
|
||
export function fireAnimationEndEvent(el: Node | Window, animationName = 'animation') { | ||
const ev = createEvent.animationEnd(el, {animationName}); | ||
Object.assign(ev, { | ||
animationName, | ||
}); | ||
|
||
fireEvent(el, ev); | ||
} | ||
|
||
test('should preserve ref on anchor element', () => { | ||
const ref = jest.fn(); | ||
render( | ||
<ActionTooltip title="text"> | ||
<button ref={ref} /> | ||
</ActionTooltip>, | ||
); | ||
|
||
expect(ref).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('should show tooltip on hover and hide on un hover', async () => { | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<ActionTooltip title="test content"> | ||
<button /> | ||
</ActionTooltip>, | ||
); | ||
|
||
const button = await screen.findByRole('button'); | ||
|
||
await user.hover(button); | ||
|
||
const tooltip = await screen.findByRole('tooltip'); | ||
|
||
expect(tooltip).toBeVisible(); | ||
|
||
await user.unhover(button); | ||
|
||
fireAnimationEndEvent(tooltip); | ||
|
||
expect(tooltip).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('should show tooltip on focus and hide on blur', async () => { | ||
const user = userEvent.setup(); | ||
render( | ||
<ActionTooltip title="test content"> | ||
<button /> | ||
</ActionTooltip>, | ||
); | ||
|
||
const button = await screen.findByRole('button'); | ||
|
||
await user.tab(); | ||
expect(button).toHaveFocus(); | ||
|
||
const tooltip = await screen.findByRole('tooltip'); | ||
|
||
expect(tooltip).toBeVisible(); | ||
|
||
await user.tab(); | ||
|
||
fireAnimationEndEvent(tooltip); | ||
|
||
expect(button).not.toHaveFocus(); | ||
expect(tooltip).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('should hide on press Escape', async () => { | ||
const user = userEvent.setup(); | ||
render( | ||
<ActionTooltip title="test content"> | ||
<button /> | ||
</ActionTooltip>, | ||
); | ||
|
||
const button = await screen.findByRole('button'); | ||
|
||
await user.tab(); | ||
expect(button).toHaveFocus(); | ||
|
||
const tooltip = await screen.findByRole('tooltip'); | ||
|
||
expect(tooltip).toBeVisible(); | ||
|
||
await user.keyboard('[Escape]'); | ||
|
||
fireAnimationEndEvent(tooltip); | ||
|
||
expect(button).toHaveFocus(); | ||
expect(tooltip).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('should show on focus and hide on un hover', async () => { | ||
const user = userEvent.setup(); | ||
render( | ||
<ActionTooltip title="test content"> | ||
<button /> | ||
</ActionTooltip>, | ||
); | ||
|
||
const button = screen.getByRole('button'); | ||
|
||
button.focus(); | ||
|
||
const tooltip = await screen.findByRole('tooltip'); | ||
|
||
expect(tooltip).toBeVisible(); | ||
|
||
await user.hover(button); | ||
|
||
expect(tooltip).toBeVisible(); | ||
|
||
await user.unhover(button); | ||
|
||
fireAnimationEndEvent(tooltip); | ||
|
||
expect(button).toHaveFocus(); | ||
expect(tooltip).not.toBeInTheDocument(); | ||
}); |
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
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 |
---|---|---|
|
@@ -3,16 +3,41 @@ | |
$block: '.#{variables.$ns}tooltip'; | ||
|
||
#{$block} { | ||
--g-popup-border-width: 0; | ||
--g-popup-background-color: var(--g-color-base-float-heavy); | ||
// [class] for increasing specificity | ||
&[class] { | ||
--g-popup-border-width: 0; | ||
|
||
> div { | ||
padding: 4px 8px; | ||
max-width: 360px; | ||
box-sizing: border-box; | ||
box-shadow: 0px 1px 5px 0px rgba(0, 0, 0, 0.15); | ||
|
||
animation-duration: unset; | ||
animation-timing-function: unset; | ||
animation-fill-mode: unset; | ||
} | ||
} | ||
|
||
&__popup-content { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as for ActionTooltip |
||
// prevent glitch between two nearby tooltip refs | ||
pointer-events: none; | ||
} | ||
|
||
&__content { | ||
padding: 6px 12px; | ||
color: var(--g-color-text-light-primary); | ||
// -webkit-line-clamp will not work without display: -webkit-box; | ||
/* stylelint-disable-next-line */ | ||
display: -webkit-box; | ||
|
||
-webkit-box-orient: vertical; | ||
-moz-box-orient: vertical; | ||
-ms-box-orient: vertical; | ||
|
||
-webkit-line-clamp: 20; | ||
-moz-line-clamp: 20; | ||
-ms-line-clamp: 20; | ||
|
||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems this class is not exists anymore