Skip to content

Commit 225f212

Browse files
feat(Tooltip): allow focusTrap prop passthrough (#6722)
* feat(Tooltip): allow focusTrap prop passthrough * docs(Tooltip): add focusTrap prop * docs(Tooltip): update uncontrolled tooltip content * docs(Tooltip): remove focusTrap from controlled stories Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent f95a421 commit 225f212

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

packages/react/src/components/Tooltip/Tooltip-story.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
import React, { useState } from 'react';
99
import { settings } from 'carbon-components';
10-
import { withKnobs, select, text, number } from '@storybook/addon-knobs';
10+
import {
11+
withKnobs,
12+
select,
13+
text,
14+
number,
15+
boolean,
16+
} from '@storybook/addon-knobs';
1117
import Tooltip from '../Tooltip';
1218
import Button from '../Button';
1319
import { OverflowMenuVertical16 } from '@carbon/icons-react';
@@ -96,10 +102,23 @@ function UncontrolledTooltipExample() {
96102
</Button>
97103
<div style={{ padding: '15px', margin: '4px 20px' }}>
98104
<Tooltip
105+
{...{
106+
...props.withoutIcon(),
107+
focusTrap: boolean('Focus trap (focusTrap)', true),
108+
}}
99109
triggerText={<div>My text wrapped with tooltip</div>}
100-
open={value}
101-
showIcon={false}>
102-
Some text
110+
open={value}>
111+
<p id="tooltip-body">
112+
This is some tooltip text. This box shows the maximum amount of text
113+
that should appear inside. If more room is needed please use a modal
114+
instead.
115+
</p>
116+
<div className={`${prefix}--tooltip__footer`}>
117+
<a href="/" className={`${prefix}--link`}>
118+
Learn More
119+
</a>
120+
<Button size="small">Create</Button>
121+
</div>
103122
</Tooltip>
104123
</div>
105124
</>

packages/react/src/components/Tooltip/Tooltip.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ class Tooltip extends Component {
107107
*/
108108
direction: PropTypes.oneOf(['bottom', 'top', 'left', 'right']),
109109

110+
/**
111+
* Enable or disable focus trap behavior
112+
*/
113+
focusTrap: PropTypes.bool,
114+
110115
/**
111116
* The name of the default tooltip icon.
112117
*/
@@ -203,6 +208,7 @@ class Tooltip extends Component {
203208

204209
static defaultProps = {
205210
direction: DIRECTION_BOTTOM,
211+
focusTrap: true,
206212
renderIcon: Information,
207213
showIcon: true,
208214
triggerText: null,
@@ -271,6 +277,9 @@ class Tooltip extends Component {
271277
}
272278

273279
_handleUserInputOpenClose = (event, { open }) => {
280+
if (this.isControlled) {
281+
return;
282+
}
274283
// capture tooltip body element before it is removed from the DOM
275284
const tooltipBody = this._tooltipEl;
276285
this.setState({ open }, () => {
@@ -396,6 +405,7 @@ class Tooltip extends Component {
396405
className,
397406
triggerClassName,
398407
direction,
408+
focusTrap,
399409
triggerText,
400410
showIcon,
401411
iconName,
@@ -474,7 +484,7 @@ class Tooltip extends Component {
474484
</ClickListener>
475485
{open && (
476486
<FloatingMenu
477-
focusTrap
487+
focusTrap={focusTrap}
478488
selectorPrimaryFocus={this.props.selectorPrimaryFocus}
479489
target={this._getTarget}
480490
triggerRef={this._triggerRef}

packages/react/src/internal/FloatingMenu.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ class FloatingMenu extends React.Component {
343343
tabbableNode || // First sequentially focusable node
344344
focusableNode || // First programmatic focusable node
345345
menuBody;
346-
focusTarget.focus();
346+
if (this.props.focusTrap) {
347+
focusTarget.focus();
348+
}
347349
if (focusTarget === menuBody && __DEV__) {
348350
warning(
349351
focusableNode === null,

0 commit comments

Comments
 (0)