This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
feat(Tooltip): Add label behavior for Tooltip #1635
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3873676
feat(Tooltip): Add label behavior for Tooltip
e918578
small improvement
4757eb9
update changelog
a729993
Update CHANGELOG.md
sophieH29 dce93f5
Merge branch 'master' into feat/tooltip-label-behavior
sophieH29 b9869d4
Merge branch 'master' into feat/tooltip-label-behavior
sophieH29 915af90
Add tooltip best practices
d9e8c8b
Update CHANGELOG.md
sophieH29 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
docs/src/examples/components/Tooltip/BestPractices/TooltipBestPractices.tsx
This file contains hidden or 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,13 @@ | ||
| import * as React from 'react' | ||
|
|
||
| import ComponentBestPractices from 'docs/src/components/ComponentBestPractices' | ||
|
|
||
| const doList = [ | ||
| 'Use `tooltipAsLabelBehavior` if adding tooltip to icon-only button or to another visual-only element without any text, title or label.', | ||
| ] | ||
|
|
||
| const DropdownBestPractices: React.FunctionComponent<{}> = () => { | ||
| return <ComponentBestPractices doList={doList} /> | ||
| } | ||
|
|
||
| export default DropdownBestPractices | ||
12 changes: 12 additions & 0 deletions
12
docs/src/examples/components/Tooltip/BestPractices/index.tsx
This file contains hidden or 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,12 @@ | ||
| import * as React from 'react' | ||
|
|
||
| import TooltipBestPractices from '././TooltipBestPractices' | ||
| import ExampleSection from 'docs/src/components/ComponentDoc/ExampleSection' | ||
|
|
||
| const BestPractices = () => ( | ||
| <ExampleSection title="Best Practices"> | ||
| <TooltipBestPractices /> | ||
| </ExampleSection> | ||
| ) | ||
|
|
||
| export default BestPractices |
This file contains hidden or 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 hidden or 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
39 changes: 39 additions & 0 deletions
39
packages/react/src/lib/accessibility/Behaviors/Tooltip/tooltipAsLabelBehavior.ts
This file contains hidden or 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,39 @@ | ||
| import { Accessibility } from '../../types' | ||
| import tooltipBehavior, { TooltipBehaviorProps } from './tooltipBehavior' | ||
|
|
||
| /** | ||
| * @specification | ||
| * Adds attribute 'role=tooltip' to 'tooltip' slot. | ||
| * Adds attribute 'aria-hidden=false' to 'tooltip' slot if 'open' property is true. Sets the attribute to 'true' otherwise. | ||
| * Adds attribute 'aria-labelledby' based on the property 'aria-labelledby' to 'trigger' slot. | ||
| * Triggers 'close' action with 'Escape' on 'trigger'. | ||
| */ | ||
| const tooltipAsLabelBehavior: Accessibility<TooltipBehaviorProps> = props => { | ||
| const behaviorData = tooltipBehavior(props) | ||
| const defaultAriaLabeledBy = getDefaultAriaLabelledBy(props) | ||
|
|
||
| behaviorData.attributes = { | ||
| trigger: { | ||
| 'aria-labelledby': defaultAriaLabeledBy || props['aria-labelledby'], | ||
kolaps33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| tooltip: { | ||
| ...behaviorData.attributes.tooltip, | ||
| id: defaultAriaLabeledBy, | ||
| }, | ||
| } | ||
|
|
||
| return behaviorData | ||
| } | ||
|
|
||
| export default tooltipAsLabelBehavior | ||
|
|
||
| /** | ||
| * Returns the element id of the tooltip, it is used when user does not provide aria-label or | ||
| * aria-labelledby as props. | ||
| */ | ||
| const getDefaultAriaLabelledBy = (props: TooltipBehaviorProps) => { | ||
| if (props['aria-label'] || props['aria-labelledby']) { | ||
| return undefined | ||
| } | ||
| return props.contentId | ||
| } | ||
This file contains hidden or 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 hidden or 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
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.
👍