forked from tgstation/tgstation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch tgui tooltips to popper.js (tgstation#57992)
* Preliminary popper support * Switch tooltips to popper.js * Documentation, change DEFAULT_PLACEMENT
- Loading branch information
1 parent
a7cbc00
commit 206c821
Showing
25 changed files
with
258 additions
and
281 deletions.
There are no files selected for viewing
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
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
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
This file was deleted.
Oops, something went wrong.
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,84 @@ | ||
import { createPopper, Placement } from '@popperjs/core'; | ||
import { Component, createPortal, createRef, InfernoNode } from 'inferno'; | ||
|
||
const DEFAULT_PLACEMENT = "top"; | ||
|
||
type TooltipProps = { | ||
children?: InfernoNode; | ||
content: string; | ||
position?: Placement; | ||
}; | ||
|
||
type TooltipState = { | ||
hovered: boolean; | ||
}; | ||
|
||
export class Tooltip extends Component<TooltipProps, TooltipState> { | ||
containerRef = createRef<HTMLSpanElement>(); | ||
tooltipRef = createRef<HTMLDivElement>(); | ||
portalNode = document.createElement("div"); | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.onMouseEnter = this.onMouseEnter.bind(this); | ||
this.onMouseLeave = this.onMouseLeave.bind(this); | ||
|
||
this.state = { | ||
hovered: false, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
document.body.appendChild(this.portalNode); | ||
|
||
createPopper(this.containerRef.current, this.tooltipRef.current, { | ||
placement: this.props.position || DEFAULT_PLACEMENT, | ||
}); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.body.removeChild(this.portalNode); | ||
this.portalNode = null; | ||
} | ||
|
||
onMouseEnter() { | ||
this.setState({ | ||
hovered: true, | ||
}); | ||
} | ||
|
||
onMouseLeave() { | ||
this.setState({ | ||
hovered: false, | ||
}); | ||
} | ||
|
||
render() { | ||
const { | ||
children, | ||
content, | ||
}: TooltipProps = this.props; | ||
|
||
return ( | ||
<> | ||
<span | ||
ref={this.containerRef} | ||
onmouseenter={this.onMouseEnter} | ||
onmouseleave={this.onMouseLeave} | ||
> | ||
{ children } | ||
</span> | ||
|
||
{createPortal( | ||
<div class="Tooltip" ref={this.tooltipRef} style={{ | ||
opacity: this.state.hovered ? 1 : 0, | ||
}}> | ||
{content} | ||
</div>, | ||
this.portalNode, | ||
)} | ||
</> | ||
); | ||
} | ||
} |
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
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
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
Oops, something went wrong.