fleet/frontend/components/TooltipTruncatedText/TooltipTruncatedText.tsx
jacobshandling 5f820febdc
UI: New side nav styles, abstractions (#30568)
## #16846 


[Demo](https://drive.google.com/file/d/1xocZDfOUbu29tPpf2J6dngy3pLACIe62/view?usp=drivesdk)

- [x] Changes file added for user-visible changes in `changes/`
- [x] Manual QA for all new/changed functionality

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added tooltips to navigation and category menu items for improved
accessibility and clarity.
* Introduced a new optional tooltip position setting, allowing tooltips
to appear on any side of the element.
  * Expanded the color palette with a new light shade option.

* **Style**
* Refactored navigation and category menu styles to use centralized,
reusable mixins for a more consistent appearance.
* Updated navigation and category menu layouts for better structure and
maintainability.

* **Chores**
* Added new SCSS mixins for navigation styling, improving code
maintainability and consistency.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2025-07-07 08:29:09 -07:00

67 lines
2.2 KiB
TypeScript

import React, { useRef } from "react";
import { uniqueId } from "lodash";
import classnames from "classnames";
import ReactTooltip from "react-tooltip";
import { COLORS } from "styles/var/colors";
import { useCheckTruncatedElement } from "hooks/useCheckTruncatedElement";
interface ITooltipTruncatedTextCellProps {
value: React.ReactNode;
/** Tooltip to display. If this is provided then this will be rendered as the tooltip content. If
* not, the value will be displayed as the tooltip content. Default: undefined */
tooltip?: React.ReactNode;
/** If set to `true` the text inside the tooltip will break on words instead of any character.
* By default the tooltip text breaks on any character. Default: false */
tooltipBreakOnWord?: boolean;
className?: string;
tooltipPosition?: "top" | "bottom" | "left" | "right";
}
const baseClass = "tooltip-truncated-text";
const TooltipTruncatedText = ({
value,
tooltip,
tooltipBreakOnWord = false,
className,
tooltipPosition = "top",
}: ITooltipTruncatedTextCellProps): JSX.Element => {
const classNames = classnames(baseClass, className, {
"tooltip-break-on-word": tooltipBreakOnWord,
});
// Tooltip visibility logic: Enable only when text is truncated
const ref = useRef<HTMLInputElement>(null);
const isTruncated = useCheckTruncatedElement(ref);
const tooltipId = uniqueId();
return (
<div className={classNames}>
<div className="tooltip-truncated" data-tip data-for={tooltipId}>
<div ref={ref} className={isTruncated ? "truncated" : undefined}>
{value}
</div>
</div>
<ReactTooltip
place={tooltipPosition}
effect="solid"
backgroundColor={COLORS["tooltip-bg"]}
id={tooltipId}
data-html
className="truncated-tooltip" // responsive widths
clickable
delayHide={200} // need delay set to hover using clickable
disable={!isTruncated}
>
<>
{tooltip ?? value}
<div className="safari-hack">&nbsp;</div>
{/* Fixes triple click selecting next element in Safari */}
</>
</ReactTooltip>
</div>
);
};
export default TooltipTruncatedText;