-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
monkey/monkey_island/cc/ui/src/components/ui-components/MonkeyTooltip.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, {useState} from 'react'; | ||
import Zoom from '@mui/material/Zoom'; | ||
import {Tooltip} from '@mui/material'; | ||
|
||
export const TOOLTIP_POSITION = { | ||
TOP: 'top', | ||
BOTTOM: 'bottom', | ||
LEFT: 'left', | ||
RIGHT: 'right' | ||
}; | ||
|
||
/* | ||
The CSS is located inside App.css ('.MuiTooltip-tooltip') as the tooltip is a global component which is appended | ||
on the body element. | ||
*/ | ||
|
||
//* MonkeyTooltip is a wrapper for the Tooltip component from @mui/material. | ||
//* It is used to set default values for the Tooltip component. | ||
//* props are forwarded to the Tooltip component. | ||
//* props can be overridden by passing them to the MonkeyTooltip component. | ||
//* props are: title, placement, className, key, isOverflow. | ||
//* The 'isOverflow' prop is meant to used in textual nodes only | ||
const MonkeyTooltip = (props) => { | ||
const {placement, isOverflow = false, children, ...rest} = {...props}; | ||
|
||
const [tooltipEnabled, setTooltipEnabled] = useState(false); | ||
|
||
const handleShouldShow = ({currentTarget}) => { | ||
if (currentTarget.scrollWidth > currentTarget.clientWidth) { | ||
setTooltipEnabled(true); | ||
} | ||
}; | ||
|
||
const forwardedProps = Object.assign( | ||
{...rest}, | ||
{ | ||
placement: placement || TOOLTIP_POSITION.TOP, | ||
TransitionComponent: Zoom | ||
} | ||
); | ||
|
||
if (isOverflow) { | ||
return ( | ||
<Tooltip {...forwardedProps} | ||
arrow | ||
open={tooltipEnabled}> | ||
<div className="text-truncate" onMouseEnter={handleShouldShow} onMouseLeave={()=> setTooltipEnabled(false)}> | ||
{children} | ||
</div> | ||
</Tooltip> | ||
) | ||
} | ||
|
||
return <Tooltip {...forwardedProps} arrow>{children}</Tooltip>; | ||
}; | ||
|
||
export default MonkeyTooltip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters