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(Tooltip): allow focusTrap prop passthrough #6722

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 23 additions & 4 deletions packages/react/src/components/Tooltip/Tooltip-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

import React, { useState } from 'react';
import { settings } from 'carbon-components';
import { withKnobs, select, text, number } from '@storybook/addon-knobs';
import {
withKnobs,
select,
text,
number,
boolean,
} from '@storybook/addon-knobs';
import Tooltip from '../Tooltip';
import Button from '../Button';
import { OverflowMenuVertical16 } from '@carbon/icons-react';
Expand Down Expand Up @@ -96,10 +102,23 @@ function UncontrolledTooltipExample() {
</Button>
<div style={{ padding: '15px', margin: '4px 20px' }}>
<Tooltip
{...{
...props.withoutIcon(),
focusTrap: boolean('Focus trap (focusTrap)', true),
}}
triggerText={<div>My text wrapped with tooltip</div>}
open={value}
showIcon={false}>
Some text
open={value}>
<p id="tooltip-body">
This is some tooltip text. This box shows the maximum amount of text
that should appear inside. If more room is needed please use a modal
instead.
</p>
<div className={`${prefix}--tooltip__footer`}>
<a href="/" className={`${prefix}--link`}>
Learn More
</a>
<Button size="small">Create</Button>
</div>
</Tooltip>
</div>
</>
Expand Down
12 changes: 11 additions & 1 deletion packages/react/src/components/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ class Tooltip extends Component {
*/
direction: PropTypes.oneOf(['bottom', 'top', 'left', 'right']),

/**
* Enable or disable focus trap behavior
*/
focusTrap: PropTypes.bool,

/**
* The name of the default tooltip icon.
*/
Expand Down Expand Up @@ -203,6 +208,7 @@ class Tooltip extends Component {

static defaultProps = {
direction: DIRECTION_BOTTOM,
focusTrap: true,
renderIcon: Information,
showIcon: true,
triggerText: null,
Expand Down Expand Up @@ -271,6 +277,9 @@ class Tooltip extends Component {
}

_handleUserInputOpenClose = (event, { open }) => {
if (this.isControlled) {
return;
}
// capture tooltip body element before it is removed from the DOM
const tooltipBody = this._tooltipEl;
this.setState({ open }, () => {
Expand Down Expand Up @@ -396,6 +405,7 @@ class Tooltip extends Component {
className,
triggerClassName,
direction,
focusTrap,
triggerText,
showIcon,
iconName,
Expand Down Expand Up @@ -474,7 +484,7 @@ class Tooltip extends Component {
</ClickListener>
{open && (
<FloatingMenu
focusTrap
focusTrap={focusTrap}
selectorPrimaryFocus={this.props.selectorPrimaryFocus}
target={this._getTarget}
triggerRef={this._triggerRef}
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/internal/FloatingMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ class FloatingMenu extends React.Component {
tabbableNode || // First sequentially focusable node
focusableNode || // First programmatic focusable node
menuBody;
focusTarget.focus();
if (this.props.focusTrap) {
focusTarget.focus();
}
if (focusTarget === menuBody && __DEV__) {
warning(
focusableNode === null,
Expand Down