2022-12-04 20:47:43 +00:00
|
|
|
import React, { MouseEvent } from 'react';
|
|
|
|
import Tippy from '@tippyjs/react';
|
2022-11-21 20:40:44 +00:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import _ from 'lodash';
|
2022-12-04 20:47:43 +00:00
|
|
|
|
|
|
|
import 'tippy.js/dist/tippy.css';
|
2022-11-21 20:40:44 +00:00
|
|
|
|
|
|
|
import { FeatureId } from '@/react/portainer/feature-flags/enums';
|
|
|
|
|
|
|
|
import { getFeatureDetails } from '@@/BEFeatureIndicator/utils';
|
|
|
|
|
|
|
|
import styles from './TooltipWithChildren.module.css';
|
|
|
|
|
2022-12-04 20:47:43 +00:00
|
|
|
export type Position = 'top' | 'right' | 'bottom' | 'left';
|
2022-11-21 20:40:44 +00:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
position?: Position;
|
2022-12-08 21:41:11 +00:00
|
|
|
message: React.ReactNode;
|
2022-11-21 20:40:44 +00:00
|
|
|
className?: string;
|
2022-12-04 20:47:43 +00:00
|
|
|
children: React.ReactElement;
|
2022-11-21 20:40:44 +00:00
|
|
|
heading?: string;
|
|
|
|
BEFeatureID?: FeatureId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function TooltipWithChildren({
|
|
|
|
message,
|
|
|
|
position = 'bottom',
|
|
|
|
className,
|
|
|
|
children,
|
|
|
|
heading,
|
|
|
|
BEFeatureID,
|
|
|
|
}: Props) {
|
|
|
|
const id = _.uniqueId('tooltip-');
|
|
|
|
|
|
|
|
const { url, limitedToBE } = BEFeatureID
|
|
|
|
? getFeatureDetails(BEFeatureID)
|
|
|
|
: { url: '', limitedToBE: false };
|
|
|
|
|
|
|
|
const messageHTML = (
|
2022-12-04 20:47:43 +00:00
|
|
|
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
|
|
|
|
<div className={styles.tooltipContainer} onClick={onClickHandler}>
|
|
|
|
{(heading || (BEFeatureID && limitedToBE)) && (
|
2023-02-12 21:04:24 +00:00
|
|
|
<div className="mb-3 inline-flex w-full justify-between">
|
2022-12-04 20:47:43 +00:00
|
|
|
<span>{heading}</span>
|
|
|
|
{BEFeatureID && limitedToBE && (
|
|
|
|
<a
|
|
|
|
href={url}
|
|
|
|
target="_blank"
|
|
|
|
rel="noreferrer"
|
|
|
|
className={styles.tooltipBeteaser}
|
|
|
|
>
|
|
|
|
Business Edition Only
|
|
|
|
</a>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2023-01-22 21:15:47 +00:00
|
|
|
<div className={styles.tooltipMessage}>{message}</div>
|
2022-11-21 20:40:44 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-12-04 20:47:43 +00:00
|
|
|
<Tippy
|
|
|
|
className={clsx(id, styles.tooltip, className)}
|
|
|
|
content={messageHTML}
|
|
|
|
delay={[50, 500]} // 50ms to open, 500ms to hide
|
|
|
|
zIndex={1000}
|
|
|
|
placement={position}
|
|
|
|
maxWidth={400}
|
|
|
|
arrow
|
|
|
|
allowHTML
|
|
|
|
interactive
|
2023-05-05 02:02:31 +00:00
|
|
|
disabled={!message}
|
2022-11-21 20:40:44 +00:00
|
|
|
>
|
|
|
|
{children}
|
2022-12-04 20:47:43 +00:00
|
|
|
</Tippy>
|
2022-11-21 20:40:44 +00:00
|
|
|
);
|
|
|
|
}
|
2022-12-04 20:47:43 +00:00
|
|
|
|
|
|
|
// Preventing click bubbling to the parent as it is affecting
|
|
|
|
// mainly toggles when full row is clickable.
|
|
|
|
function onClickHandler(e: MouseEvent) {
|
|
|
|
const target = e.target as HTMLInputElement;
|
|
|
|
if (target.tagName.toLowerCase() === 'a') {
|
|
|
|
const url = target.getAttribute('href');
|
|
|
|
if (url) {
|
|
|
|
window.open(url, '_blank');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
}
|