Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 309f7bb

Browse files
authored
Display a tooltip when you hover over a location (#7472)
1 parent 707f8cd commit 309f7bb

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

src/components/views/elements/Tooltip.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export enum Alignment {
3232
Right,
3333
Top, // Centered
3434
Bottom, // Centered
35+
InnerBottom, // Inside the target, at the bottom
3536
}
3637

3738
export interface ITooltipProps {
@@ -50,6 +51,8 @@ export interface ITooltipProps {
5051
// id describing tooltip
5152
// used to associate tooltip with target for a11y
5253
id?: string;
54+
// If the parent is over this width, act as if it is only this wide
55+
maxParentWidth?: number;
5356
}
5457

5558
@replaceableComponent("views.elements.Tooltip")
@@ -107,11 +110,18 @@ export default class Tooltip extends React.Component<ITooltipProps> {
107110
offset = Math.floor(parentBox.height - MIN_TOOLTIP_HEIGHT);
108111
}
109112
const width = UIStore.instance.windowWidth;
113+
const parentWidth = (
114+
this.props.maxParentWidth
115+
? Math.min(parentBox.width, this.props.maxParentWidth)
116+
: parentBox.width
117+
);
110118
const baseTop = (parentBox.top - 2 + this.props.yOffset) + window.pageYOffset;
111119
const top = baseTop + offset;
112120
const right = width - parentBox.right - window.pageXOffset - 16;
113121
const left = parentBox.right + window.pageXOffset + 6;
114-
const horizontalCenter = parentBox.right - window.pageXOffset - (parentBox.width / 2);
122+
const horizontalCenter = (
123+
parentBox.left - window.pageXOffset + (parentWidth / 2)
124+
);
115125
switch (this.props.alignment) {
116126
case Alignment.Natural:
117127
if (parentBox.right > width / 2) {
@@ -136,6 +146,10 @@ export default class Tooltip extends React.Component<ITooltipProps> {
136146
style.top = baseTop + parentBox.height;
137147
style.left = horizontalCenter;
138148
break;
149+
case Alignment.InnerBottom:
150+
style.top = baseTop + parentBox.height - 50;
151+
style.left = horizontalCenter;
152+
style.transform = "translate(-50%)";
139153
}
140154

141155
return style;

src/components/views/elements/TooltipTarget.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const TooltipTarget: React.FC<IProps> = ({
3636
alignment,
3737
yOffset,
3838
tooltipClassName,
39+
maxParentWidth,
3940
...rest
4041
}) => {
4142
const [isVisible, setIsVisible] = useState(false);
@@ -63,6 +64,7 @@ const TooltipTarget: React.FC<IProps> = ({
6364
yOffset={yOffset}
6465
alignment={alignment}
6566
visible={isVisible}
67+
maxParentWidth={maxParentWidth}
6668
/>
6769
</div>
6870
);

src/components/views/messages/MLocationBody.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import { _t } from '../../../languageHandler';
2727
import MemberAvatar from '../avatars/MemberAvatar';
2828
import Modal from '../../../Modal';
2929
import LocationViewDialog from '../location/LocationViewDialog';
30+
import TooltipTarget from '../elements/TooltipTarget';
31+
import { Alignment } from '../elements/Tooltip';
3032

3133
interface IState {
3234
error: Error;
@@ -93,6 +95,7 @@ export default class MLocationBody extends React.Component<IBodyProps, IState> {
9395
bodyId={this.getBodyId()}
9496
markerId={this.getMarkerId()}
9597
error={this.state.error}
98+
tooltip={_t("Expand map")}
9699
onClick={this.onClick}
97100
/>;
98101
}
@@ -103,10 +106,17 @@ interface ILocationBodyContentProps {
103106
bodyId: string;
104107
markerId: string;
105108
error: Error;
109+
tooltip?: string;
106110
onClick?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
107111
}
108112

109113
export function LocationBodyContent(props: ILocationBodyContentProps) {
114+
const mapDiv = <div
115+
id={props.bodyId}
116+
onClick={props.onClick}
117+
className="mx_MLocationBody_map"
118+
/>;
119+
110120
return <div className="mx_MLocationBody">
111121
{
112122
props.error
@@ -115,11 +125,17 @@ export function LocationBodyContent(props: ILocationBodyContentProps) {
115125
</div>
116126
: null
117127
}
118-
<div
119-
id={props.bodyId}
120-
onClick={props.onClick}
121-
className="mx_MLocationBody_map"
122-
/>
128+
{
129+
props.tooltip
130+
? <TooltipTarget
131+
label={props.tooltip}
132+
alignment={Alignment.InnerBottom}
133+
maxParentWidth={450}
134+
>
135+
{ mapDiv }
136+
</TooltipTarget>
137+
: mapDiv
138+
}
123139
<div className="mx_MLocationBody_marker" id={props.markerId}>
124140
<div className="mx_MLocationBody_markerBorder">
125141
<MemberAvatar

src/i18n/strings/en_EN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,7 @@
20912091
"Declining …": "Declining …",
20922092
"%(name)s wants to verify": "%(name)s wants to verify",
20932093
"You sent a verification request": "You sent a verification request",
2094+
"Expand map": "Expand map",
20942095
"Failed to load map": "Failed to load map",
20952096
"Vote not registered": "Vote not registered",
20962097
"Sorry, your vote was not registered. Please try again.": "Sorry, your vote was not registered. Please try again.",

0 commit comments

Comments
 (0)