All files / ToolTip ToolTip.tsx

88.89% Statements 48/54
91.67% Branches 22/24
61.54% Functions 8/13
94.12% Lines 48/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406                            176x 176x                     176x   176x             176x 176x 176x         176x           176x 176x 176x         176x           176x 176x 176x         176x                                                                                                                               62x 62x           176x   176x 176x 176x   176x                                                 176x   176x                                                                                                                                                       176x                         62x 2x         2x 2x 1x         62x 1x     62x         62x 3x 3x       62x 2x 2x     62x                                             68x   68x     68x       68x       68x 3x           68x                                                                                                                                        
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import ContextMenu from '../ContextMenu/ContextMenu';
import CloseIcon, { ICloseIconProps } from '../Icon/CloseIcon/CloseIcon';
import * as reducers from './ToolTip.reducers';
import { lucidClassNames } from '../../util/style-helpers';
import {
	StandardProps,
	findTypes,
	omitProps,
} from '../../util/component-types';
import { buildModernHybridComponent } from '../../util/state-management';
 
const cx = lucidClassNames.bind('&-ToolTip');
const flyOutCx = cx.bind('&-FlyOut');
 
const {
	bool,
	func,
	node,
	number,
	object,
	oneOf,
	string,
	oneOfType,
} = PropTypes;
 
const { Target, FlyOut } = ContextMenu;
 
interface IToolTipTargetProps extends StandardProps {
	description?: string;
	elementType?: string;
}
 
const ToolTipTarget = (_props: IToolTipTargetProps): null => null;
ToolTipTarget.displayName = 'ToolTip.Target';
ToolTipTarget.peek = {
	description: `
		The hover target that will trigger the ToolTip to be displayed.
	`,
};
ToolTipTarget.propName = 'Target';
 
interface IToolTipTitleProps extends StandardProps {
	description?: string;
}
 
const ToolTipTitle = (_props: IToolTipTitleProps): null => null;
ToolTipTitle.displayName = 'ToolTip.Title';
ToolTipTitle.peek = {
	description: `
		The title displayed at the top of the ToolTip.
	`,
};
ToolTipTitle.propName = 'Title';
 
interface IToolTipBodyProps extends StandardProps {
	description?: string;
}
 
const ToolTipBody = (_props: IToolTipBodyProps): null => null;
ToolTipBody.displayName = 'ToolTip.Body';
ToolTipBody.peek = {
	description: `
		The body of the ToolTip displayed below the Title.
	`,
};
ToolTipBody.propName = 'Body';
 
export interface IToolTipState {
	isExpanded: boolean;
	isMouseOverFlyout: boolean;
	isMouseOverTarget: boolean;
}
 
export interface IToolTipProps extends StandardProps {
	/** Set this to \`true\` if you want to have a \`x\` close icon. */
	isCloseable?: boolean;
 
	/** Offers a lighter style for the tooltip window. Defaults to false. */
	isLight?: boolean;
 
	/** Called when the user closes the \`Banner\`. */
	onClose?: ({
		event,
		props,
	}: {
		event: React.MouseEvent;
		props: IToolTipProps;
	}) => void;
 
	/** Passed through to the root FlyOut element. */
	flyOutStyle?: React.CSSProperties;
 
	/** maximum width of the ToolTip FlyOut. Defaults to 200px. */
	flyOutMaxWidth?: number | string;
 
	/** direction of the FlyOut relative to Target. */
	direction?: 'down' | 'up' | 'right' | 'left';
 
	/** alignment of the Flyout relative to Target in the cross axis from \`direction\ */
	alignment?: 'start' | 'center' | 'end';
 
	/** Indicates whether the ToolTip will render or not. */
	isExpanded?: boolean;
 
	/** Called when cursor moves over the target */
	onMouseOver?: ({
		event,
		props,
	}: {
		event: React.MouseEvent;
		props: IToolTipProps;
	}) => void;
 
	/** Called when cursor leaves the target and the ToolTip */
	onMouseOut?: ({
		event,
		props,
	}: {
		event: React.MouseEvent;
		props: IToolTipProps;
	}) => void;
 
	/** 			The \`id\` of the FlyOut portal element that is appended to
			\`document.body\`. Defaults to a generated \`id\`. */
	portalId?: string | null;
}
 
class ToolTip extends React.Component<IToolTipProps, IToolTipState> {
	constructor(props: IToolTipProps) {
		super(props);
		this.state = {
			isMouseOverFlyout: false,
			isMouseOverTarget: false,
			isExpanded: false,
		};
	}
	static displayName = 'ToolTip';
 
	static Title = ToolTipTitle;
	static Target = ToolTipTarget;
	static Body = ToolTipBody;
 
	static peek = {
		description: `
				A utility component that creates a transient message anchored to
				another component.
			`,
		notes: {
			overview: `
					A text popup shown on hover.
				`,
			intendedUse: `
					Use to provide an explanation for a button, text, or an operation. Often used in conjunction with \`HelpIcon\`.
										
					**Styling notes**
					
					- Use the {direction} and {alignment} that best suit your layout.
					- Tooltip titles should fit on a single line and not wrap.
					- Use black tooltips in most interactions. White tooltips are reserved for use within charts, for example \`LineChart\`.
				`,
			technicalRecommendations: `
				`,
		},
		categories: ['communication'],
		madeFrom: ['ContextMenu'],
	};
 
	static reducers = reducers;
 
	static propTypes = {
		children: node`
			\`children\` should include exactly one ToolTip.Target and one
			ToolTip.FlyOut.
		`,
 
		className: string`
			Appended to the component-specific class names set on the root element.
		`,
 
		isCloseable: bool`
			Set this to \`true\` if you want to have a \`x\` close icon.
		`,
 
		isLight: bool`
			Offers a lighter style for the tooltip window. Defaults to false.
		`,
 
		onClose: func`
			Called when the user closes the \`Banner\`.  Signature:
			\`({ event, props }) => {}\`
		`,
 
		style: object`
			Passed through to the root target element.
		`,
 
		flyOutStyle: object`
			Passed through to the root FlyOut element.
		`,
 
		flyOutMaxWidth: oneOfType([number, string])`
			maximum width of the ToolTip FlyOut. Defaults to 200px.
		`,
 
		direction: oneOf(['down', 'up', 'right', 'left'])`
			direction of the FlyOut relative to Target.
		`,
 
		alignment: oneOf(['start', 'center', 'end'])`
			alignment of the Flyout relative to Target in the cross axis from
			\`direction\`.
		`,
 
		isExpanded: bool`
			Indicates whether the ToolTip will render or not.
		`,
 
		onMouseOver: func`
			Called when cursor moves over the target Signature:
			\`({ props, event }) => {}\`
		`,
 
		onMouseOut: func`
			Called when cursor leaves the target and the ToolTip Signature:
			\`({ props, event }) => {}\`
		`,
 
		portalId: string`
			The \`id\` of the FlyOut portal element that is appended to
			\`document.body\`. Defaults to a generated \`id\`.
		`,
 
		Body: node`
			The body of the ToolTip displayed below the Title.
		`,
 
		Title: node`
			The title displayed at the top of the ToolTip.
		`,
 
		Target: node`
			The hover target that will trigger the ToolTip to be displayed.
		`,
	};
 
	static defaultProps = {
		alignment: ContextMenu.CENTER,
		direction: ContextMenu.UP,
		flyOutStyle: {},
		isCloseable: false,
		isExpanded: false,
		isLight: false,
		onClose: _.noop,
		onMouseOut: _.noop,
		onMouseOver: _.noop,
		portalId: null,
	};
 
	handleMouseOut = (event: React.MouseEvent): void => {
		setTimeout(() => {
			const {
				props,
				state: { isMouseOverFlyout, isMouseOverTarget },
				props: { onMouseOut },
			} = this;
			if (!isMouseOverFlyout && !isMouseOverTarget) {
				onMouseOut && onMouseOut({ props, event });
			}
		}, 100);
	};
 
	handleMouseOverFlyout = () => {
		this.setState({ isMouseOverFlyout: true });
	};
 
	handleMouseOutFlyout = (event: React.MouseEvent) => {
		this.setState({ isMouseOverFlyout: false });
		this.handleMouseOut(event);
	};
 
	handleMouseOverTarget = (event: React.MouseEvent) => {
		this.setState({ isMouseOverTarget: true });
		this.props.onMouseOver &&
			this.props.onMouseOver({ props: this.props, event });
	};
 
	handleMouseOutTarget = (event: React.MouseEvent) => {
		this.setState({ isMouseOverTarget: false });
		this.handleMouseOut(event);
	};
 
	handleClose = ({
		event,
		props,
	}: {
		event: React.MouseEvent;
		props: ICloseIconProps;
	}) => {
		this.props.onClose && this.props.onClose({ event, props: this.props });
	};
 
	render() {
		const {
			className,
			alignment,
			direction,
			flyOutMaxWidth,
			flyOutStyle,
			isCloseable,
			isExpanded,
			isLight,
			portalId,
			style,
			...passThroughs
		} = this.props;
 
		const targetProps = _.first(
			_.map(findTypes(this.props, ToolTip.Target), 'props')
		);
		const title = _.get(
			_.first(_.map(findTypes(this.props, ToolTip.Title), 'props')),
			'children'
		);
		const body = _.get(
			_.first(_.map(findTypes(this.props, ToolTip.Body), 'props')),
			'children'
		);
		const getAlignmentOffset = (n: number) =>
			alignment === ContextMenu.CENTER
				? 0
				: alignment === ContextMenu.START
				? n / 2 - 22.5
				: -(n / 2 - 22.5);
 
		return (
			<ContextMenu
				className={cx('&', className)}
				// WARNING: Alignment is always set to center because the getAlignmentOffset function
				// handles the alignment instead of delegating to ContextMenu
				alignment={ContextMenu.CENTER}
				direction={direction}
				directonOffset={15}
				getAlignmentOffset={getAlignmentOffset}
				isExpanded={isExpanded}
				style={style}
				portalId={portalId}
				{...omitProps(
					passThroughs,
					undefined,
					_.keys(ToolTip.propTypes),
					false
				)}
				onMouseOver={this.handleMouseOverTarget}
				onMouseOut={this.handleMouseOutTarget}
			>
				<Target
					{...targetProps}
					className={cx(_.get(targetProps, 'className'), '&-Target')}
				>
					{_.get(targetProps, 'children')}
				</Target>
				<FlyOut
					style={{
						...flyOutStyle,
						maxWidth:
							flyOutMaxWidth || (flyOutStyle && flyOutStyle.maxWidth) || 200,
					}}
					className={flyOutCx(
						className,
						'&',
						`&-${direction}`,
						`&-${alignment}`,
						isLight ? '&-light' : '&-default'
					)}
					onMouseOver={this.handleMouseOverFlyout}
					onMouseOut={this.handleMouseOutFlyout}
				>
					{isCloseable ? (
						<CloseIcon
							isClickable
							size={8}
							onClick={this.handleClose}
							className={flyOutCx('&-close')}
						/>
					) : null}
					{!_.isNil(title) ? (
						<h2 className={flyOutCx('&-Title')}>{title}</h2>
					) : null}
					{body}
				</FlyOut>
			</ContextMenu>
		);
	}
}
 
export default buildModernHybridComponent<
	IToolTipProps,
	IToolTipState,
	typeof ToolTip
>(ToolTip, { reducers });
 
export { ToolTip as ToolTipDumb };