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

[Tooltip] Converted to a functional component #2543

Merged
merged 5 commits into from
Jan 6, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@

- Converted `BulkActionButton` into a functional component ([#2542](https://github.com/Shopify/polaris-react/pull/2542))
- Converted `Focus` into a functional component ([#2540](https://github.com/Shopify/polaris-react/pull/2540))
- Converted `Tooltip` into a functional component ([#2543](https://github.com/Shopify/polaris-react/pull/2543))

### Deprecations
186 changes: 79 additions & 107 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import {createUniqueIDFactory} from '@shopify/javascript-utilities/other';
import React, {useEffect, useState, useRef} from 'react';
import {findFirstFocusableNode} from '@shopify/javascript-utilities/focus';

import {PreferredPosition} from '../PositionedOverlay';
import {Portal} from '../Portal';
import {useUniqueId} from '../../utilities/unique-id';
import {useToggle} from '../../utilities/use-toggle';
import {TooltipOverlay} from './components';
import styles from './Tooltip.scss';

Expand All @@ -28,122 +29,93 @@ export interface TooltipProps {
activatorWrapper?: string;
}

interface State {
active: boolean;
activatorNode: HTMLElement | null;
}

const getUniqueID = createUniqueIDFactory('TooltipContent');

export class Tooltip extends React.PureComponent<TooltipProps, State> {
state: State = {
active: Boolean(this.props.active),
activatorNode: null,
};

private id = getUniqueID();
private activatorContainer: HTMLElement | null = null;
private mouseEntered = false;

componentDidMount() {
this.setAccessibilityAttributes();
}

componentDidUpdate() {
this.setAccessibilityAttributes();
}

render() {
const {id} = this;

const {
children,
content,
light,
preferredPosition = 'below',
activatorWrapper: WrapperComponent = 'span' as any,
} = this.props;

const {active, activatorNode} = this.state;
export function Tooltip({
children,
content,
light,
active: originalActive,
preferredPosition = 'below',
activatorWrapper = 'span',
}: TooltipProps) {
const WrapperComponent: any = activatorWrapper;
const {value: active, setTrue: handleFocus, setFalse: handleBlur} = useToggle(
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL about useToggle 😄

Boolean(originalActive),
);
const [activatorNode, setActivatorNode] = useState<HTMLElement | null>(null);

const id = useUniqueId('TooltipContent');
const activatorContainer = useRef<HTMLElement>(null);
const mouseEntered = useRef(false);

useEffect(() => {
const firstFocusable = activatorContainer.current
? findFirstFocusableNode(activatorContainer.current)
: null;
const accessibilityNode = firstFocusable || activatorContainer.current;

if (!accessibilityNode) return;

const portal = activatorNode ? (
<Portal idPrefix="tooltip">
<TooltipOverlay
id={id}
preferredPosition={preferredPosition}
activator={activatorNode}
active={active}
onClose={noop}
light={light}
>
<div className={styles.Label} testID="TooltipOverlayLabel">
{content}
</div>
</TooltipOverlay>
</Portal>
) : null;

return (
<WrapperComponent
testID="WrapperComponent"
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onMouseLeave={this.handleMouseLeave}
onMouseOver={this.handleMouseEnterFix}
ref={this.setActivator}
accessibilityNode.tabIndex = 0;
accessibilityNode.setAttribute('aria-describedby', id);
}, [id, children]);

const portal = activatorNode ? (
<Portal idPrefix="tooltip">
<TooltipOverlay
id={id}
preferredPosition={preferredPosition}
activator={activatorNode}
active={active}
onClose={noop}
light={light}
>
{children}
{portal}
</WrapperComponent>
);
}

private setActivator = (node: HTMLElement | null) => {
<div className={styles.Label} testID="TooltipOverlayLabel">
{content}
</div>
</TooltipOverlay>
</Portal>
) : null;

return (
<WrapperComponent
testID="WrapperComponent"
onFocus={handleFocus}
onBlur={handleBlur}
onMouseLeave={handleMouseLeave}
onMouseOver={handleMouseEnterFix}
ref={setActivator}
>
{children}
{portal}
</WrapperComponent>
);

function setActivator(node: HTMLElement | null) {
const activatorContainerRef: any = activatorContainer;
if (node == null) {
this.activatorContainer = null;
this.setState({activatorNode: null});
activatorContainerRef.current = null;
setActivatorNode(null);
return;
}

this.setState({activatorNode: node.firstElementChild as HTMLElement});
this.activatorContainer = node;
};

private handleFocus = () => {
this.setState({active: true});
};

private handleBlur = () => {
this.setState({active: false});
};
setActivatorNode(node.firstElementChild as HTMLElement);
activatorContainerRef.current = node;
}

private handleMouseEnter = () => {
this.mouseEntered = true;
this.setState({active: true});
};
function handleMouseEnter() {
mouseEntered.current = true;
handleFocus();
}

private handleMouseLeave = () => {
this.mouseEntered = false;
this.setState({active: false});
};
function handleMouseLeave() {
mouseEntered.current = false;
handleBlur();
}

// https://github.com/facebook/react/issues/10109
// Mouseenter event not triggered when cursor moves from disabled button
private handleMouseEnterFix = () => {
!this.mouseEntered && this.handleMouseEnter();
};

private setAccessibilityAttributes() {
const {activatorContainer, id} = this;
if (activatorContainer == null) {
return;
}

const firstFocusable = findFirstFocusableNode(activatorContainer);
const accessibilityNode = firstFocusable || activatorContainer;

accessibilityNode.tabIndex = 0;
accessibilityNode.setAttribute('aria-describedby', id);
function handleMouseEnterFix() {
!mouseEntered.current && handleMouseEnter();
}
}

Expand Down