Skip to content

Tooltip: anchoredPosition + IconButton #2006

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

Merged
merged 29 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f427dbf
Add breaking story
siddharthkp Mar 30, 2022
8772f31
Add memex story
siddharthkp Mar 30, 2022
b9cbf8c
use behaviors deploy preview
siddharthkp Mar 30, 2022
3aaf1f4
add tooltip triangle
siddharthkp Mar 30, 2022
67a5eed
update snapshot
siddharthkp Apr 4, 2022
3ad30cc
add label tooltips for story
siddharthkp Apr 12, 2022
5a91f96
update @primer/behaviors to latest
siddharthkp Apr 19, 2022
68bbdde
Merge branch 'main' into siddharthkp/fix-anchored-position
siddharthkp Apr 19, 2022
ecfb54e
lint: remove unused import
siddharthkp Apr 19, 2022
8a872c5
Refactor Tooltip
siddharthkp Apr 28, 2022
98fdc4a
Use Tooltip in IconButton
siddharthkp Apr 28, 2022
623f2b2
Add triangle styles for all directions
siddharthkp Apr 28, 2022
c4c65fd
Add docs
siddharthkp Apr 29, 2022
b9ed36c
Added delay
siddharthkp Apr 29, 2022
505c1a5
change ReactElement to ReactNode
siddharthkp Apr 29, 2022
1e26ba1
keep ReactElement
siddharthkp Apr 29, 2022
24b01a4
Add tests!
siddharthkp Apr 29, 2022
13461ea
compatible types :)
siddharthkp Apr 29, 2022
36bf0fc
Fix docs
siddharthkp Apr 29, 2022
8d199cb
Merge branch 'main' into siddharthkp/fix-anchored-position
siddharthkp Apr 29, 2022
3aaf29a
update snapshots
siddharthkp Apr 29, 2022
8a98ac8
update behaviors to next minor
siddharthkp May 9, 2022
e228b09
update snapshots
siddharthkp May 9, 2022
1b1166a
Fix IconButton duplicate label
siddharthkp May 9, 2022
a81b671
Merge branch 'main' into siddharthkp/fix-anchored-position
siddharthkp May 9, 2022
6c62bf0
missed a spot!
siddharthkp May 9, 2022
38ffda3
Fix Button story with tooltip
siddharthkp May 9, 2022
393c440
Apply suggestions from code review
siddharthkp May 10, 2022
3bd3382
fix alignment with span
siddharthkp May 10, 2022
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
10 changes: 10 additions & 0 deletions docs/content/IconButton.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ A separate component called `IconButton` is used if the action shows only an ico
</>
```

### Customize description / tooltip text

To add description for the button, wrap `IconButton` in a `Tooltip`. Make sure you pass `aria-label` to the button as well.

```jsx live
<Tooltip text="You have no unread notifications">
<IconButton icon={BellIcon} aria-label="Notifications" />
</Tooltip>
```

## API reference

Native `<button>` HTML attributes are forwarded to the underlying React `button` component and are not listed below.
Expand Down
34 changes: 0 additions & 34 deletions docs/content/Tooltip.md

This file was deleted.

192 changes: 192 additions & 0 deletions docs/content/Tooltip.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
componentId: tooltip
title: Tooltip
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love these docs updates 💖

status: Alpha
source: https://github.com/primer/react/tree/main/src/Tooltip
storybook: '/react/storybook?path=/story/composite-components-tooltip'
description: The Tooltip component adds a tooltip to add context to elements on the page.
---

Tooltip only appears on mouse hover or keyboard focus and contain a label or description text. Use tooltips sparingly and as a last resort. [Consider these alternatives](https://primer.style/design/accessibility/tooltip-alternatives).

import {Tooltip, IconButton, Button} from '@primer/react'
import {BellIcon, MentionIcon} from '@primer/octicons-react'
import InlineCode from '@primer/gatsby-theme-doctocat/src/components/inline-code'

<Box
sx={{
display: 'flex',
justifyContent: 'center',
border: '1px solid',
borderColor: 'border.default',
borderRadius: 2,
padding: 6,
paddingTop: 8,
marginBottom: 3
}}
>
<Tooltip text="You have no unread notifications" sx={{visibility: 'visible', opacity: 1}}>
<IconButton icon={BellIcon} aria-label="Notifications" />
</Tooltip>
</Box>

When using a tooltip, follow the provided guidelines to avoid accessibility issues.

- Tooltip text should be brief and to the point.
- Tooltips should contain only **non-essential text**. Tooltips can easily be missed and are not accessible on touch devices so never use tooltips to convey critical information.

## Examples

### As a description for icon-only button

If the tooltip content provides supplementary description, wrap the target in a `Tooltip`. The trigger element should also have a concise accessible label via `aria-label`.

```jsx live
<Tooltip text="Directly mention a team or user">
<IconButton aria-label="Mentions" icon={MentionIcon} variant="invisible" />
</Tooltip>
```

### As a description for a button with visible label

```jsx live
<Tooltip text="This will immediately impact all organization members">
<Button variant="primary">Save</Button>
</Tooltip>
```

### With direction

Set direction of tooltip with `direction`. The tooltip is responsive and will automatically adjust direction to avoid cutting off.

```jsx live
<Box
sx={{
display: 'flex',
flexDirection: 'column',
gap: 8,
marginY: 4,
'[data-component=tooltip]': {visibility: 'visible', opacity: 1}
}}
>
<Box sx={{display: 'flex', justifyContent: 'space-between', gap: 1}}>
<Tooltip direction="nw" text="Tooltip text">
<Button>North West</Button>
</Tooltip>
<Tooltip direction="n" text="Tooltip text">
<Button>North</Button>
</Tooltip>
<Tooltip direction="ne" text="Tooltip text">
<Button>North East</Button>
</Tooltip>
</Box>
<Box sx={{display: 'flex', justifyContent: 'space-between', gap: 1}}>
<Tooltip direction="e" text="Tooltip text">
<Button>East</Button>
</Tooltip>
<Tooltip direction="w" text="Tooltip text">
<Button>West</Button>
</Tooltip>
</Box>
<Box sx={{display: 'flex', justifyContent: 'space-between', gap: 1}}>
<Tooltip direction="sw" text="Tooltip text">
<Button>South West</Button>
</Tooltip>
<Tooltip direction="s" text="Tooltip text">
<Button>South</Button>
</Tooltip>
<Tooltip direction="se" text="Tooltip text">
<Button>South East</Button>
</Tooltip>
</Box>
</Box>
```

## Props

<PropsTable>
<PropsTableRow name="children" required type="React.ReactNode" description="Tooltip target, single element" />

<PropsTableRow
name="text"
type="string"
description="The text content of the tooltip. This should be brief and no longer than a sentence"
/>
<PropsTableRow
deprecated
name="aria-label"
type="string"
description={
<>
Use <InlineCode>text</InlineCode> instead
</>
}
/>
<PropsTableRow
name="type"
type="description | label"
defaultValue="description"
description={
<>
Use <InlineCode>aria-describedby</InlineCode> or <InlineCode>aria-labelledby</InlineCode>
</>
}
/>
<PropsTableRow
name="direction"
type="nw | n | ne | e | se | s | sw | w"
defaultValue="n"
description="Sets where the tooltip renders in relation to the target"
/>
<PropsTableRow name="align" deprecated type="left | right" description="Use direction instead. Alignment relative to target" />
<PropsTableRow
name="noDelay"
type="boolean"
defaultValue="false"
description={
<>
When set to <InlineCode>true</InlineCode>, tooltip appears without any delay
</>
}
/>
<PropsTableRow
name="wrap"
type="boolean"
deprecated
description={
<>
Use to allow text within tooltip to wrap. Deprecated: always set to <InlineCode>true</InlineCode> now.
</>
}
/>
<PropsTableSxRow />
</PropsTable>

## Status

<ComponentChecklist
items={{
propsDocumented: true,
noUnnecessaryDeps: true,
adaptsToThemes: true,
adaptsToScreenSizes: true,
fullTestCoverage: true,
usedInProduction: true,
usageExamplesDocumented: true,
hasStorybookStories: true,
designReviewed: false,
a11yReviewed: false,
stableApi: true,
addressedApiFeedback: false,
hasDesignGuidelines: false,
hasFigmaComponent: true
}}
/>

## Further reading

- [Tooltip alternatives](https://primer.style/design/accessibility/tooltip-alternatives)

## Related components

- [IconButton](/IconButton)
Loading