|
| 1 | +import Tippy from "@tippyjs/react"; |
| 2 | +import { followCursor } from "tippy.js"; |
| 3 | +import React from "react"; |
| 4 | +import { StringUtils } from "andculturecode-javascript-core"; |
| 5 | + |
| 6 | +// ------------------------------------------------------------------------------------------------- |
| 7 | +// #region Interfaces |
| 8 | +// ------------------------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +export interface TooltipProps { |
| 11 | + /** |
| 12 | + * Required. The element that triggers the tooltip on hovering. |
| 13 | + * Surround the trigger with the `<Tooltip>` component. |
| 14 | + */ |
| 15 | + children: React.ReactElement; |
| 16 | + /** |
| 17 | + * The content of the tooltip itself. |
| 18 | + */ |
| 19 | + content: React.ReactChild | React.ReactChild[]; |
| 20 | + cssClassName?: string; |
| 21 | + /** |
| 22 | + * Delay to wait before showing tooltip, in ms. Defaults to 500. |
| 23 | + */ |
| 24 | + delay?: number; |
| 25 | + /** |
| 26 | + * Disable the tooltip. For example, if you have overflowing text, |
| 27 | + * and you only want to show if the text is actually truncated with ... |
| 28 | + * you can get a ref to the HTML element and set |
| 29 | + * disabled={elRef.offsetWidth < elRef.scrollWidth} |
| 30 | + * @see resource-subtext-label.tsx for an example of how to achieve this behavior |
| 31 | + */ |
| 32 | + disabled?: boolean; |
| 33 | + /** |
| 34 | + * True by default. If true, the tooltip will appear at the cursor's location. |
| 35 | + */ |
| 36 | + showOnCursor?: boolean; |
| 37 | + /** |
| 38 | + * Manually control tooltip visibility. |
| 39 | + * Useful for debugging styles. |
| 40 | + */ |
| 41 | + visible?: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +// #endregion Interfaces |
| 45 | + |
| 46 | +// ------------------------------------------------------------------------------------------------- |
| 47 | +// #region Component |
| 48 | +// ------------------------------------------------------------------------------------------------- |
| 49 | + |
| 50 | +const Tooltip: React.FC<TooltipProps> = (props: TooltipProps) => { |
| 51 | + const CSS_CLASS_NAME = "c-tooltip"; |
| 52 | + const classNames = [CSS_CLASS_NAME]; |
| 53 | + |
| 54 | + if (StringUtils.hasValue(props.cssClassName)) { |
| 55 | + classNames.push(props.cssClassName!); |
| 56 | + } |
| 57 | + |
| 58 | + const getContent = () => ( |
| 59 | + <React.Fragment> |
| 60 | + {props.content} |
| 61 | + <div className={`${CSS_CLASS_NAME}__arrow`} /> |
| 62 | + </React.Fragment> |
| 63 | + ); |
| 64 | + |
| 65 | + if (props.disabled === true) { |
| 66 | + return props.children; |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <Tippy |
| 71 | + className={classNames.join(" ")} |
| 72 | + content={getContent()} |
| 73 | + delay={props.delay ?? 500} |
| 74 | + followCursor={props.showOnCursor === false ? undefined : "initial"} |
| 75 | + hideOnClick={false} |
| 76 | + plugins={[followCursor]} |
| 77 | + visible={props.visible}> |
| 78 | + {props.children} |
| 79 | + </Tippy> |
| 80 | + ); |
| 81 | +}; |
| 82 | + |
| 83 | +// #endregion Component |
| 84 | + |
| 85 | +// ------------------------------------------------------------------------------------------------- |
| 86 | +// #region Exports |
| 87 | +// ------------------------------------------------------------------------------------------------- |
| 88 | + |
| 89 | +export { Tooltip }; |
| 90 | + |
| 91 | +// #endregion Exports |
0 commit comments