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

Display a tooltip when you hover over a location #7472

Merged
merged 16 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/components/views/elements/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export enum Alignment {
Right,
Top, // Centered
Bottom, // Centered
InnerBottom, // Inside the target, at the bottom
}

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

@replaceableComponent("views.elements.Tooltip")
Expand Down Expand Up @@ -107,11 +110,18 @@ export default class Tooltip extends React.Component<ITooltipProps> {
offset = Math.floor(parentBox.height - MIN_TOOLTIP_HEIGHT);
}
const width = UIStore.instance.windowWidth;
const parentWidth = (
this.props.maxParentWidth
? Math.min(parentBox.width, this.props.maxParentWidth)
: parentBox.width
);
const baseTop = (parentBox.top - 2 + this.props.yOffset) + window.pageYOffset;
const top = baseTop + offset;
const right = width - parentBox.right - window.pageXOffset - 16;
const left = parentBox.right + window.pageXOffset + 6;
const horizontalCenter = parentBox.right - window.pageXOffset - (parentBox.width / 2);
const horizontalCenter = (
parentBox.left - window.pageXOffset + (parentWidth / 2)
);
switch (this.props.alignment) {
case Alignment.Natural:
if (parentBox.right > width / 2) {
Expand All @@ -136,6 +146,10 @@ export default class Tooltip extends React.Component<ITooltipProps> {
style.top = baseTop + parentBox.height;
style.left = horizontalCenter;
break;
case Alignment.InnerBottom:
style.top = baseTop + parentBox.height - 50;
style.left = horizontalCenter;
style.transform = "translate(-50%)";
}

return style;
Expand Down
2 changes: 2 additions & 0 deletions src/components/views/elements/TooltipTarget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const TooltipTarget: React.FC<IProps> = ({
alignment,
yOffset,
tooltipClassName,
maxParentWidth,
...rest
}) => {
const [isVisible, setIsVisible] = useState(false);
Expand Down Expand Up @@ -63,6 +64,7 @@ const TooltipTarget: React.FC<IProps> = ({
yOffset={yOffset}
alignment={alignment}
visible={isVisible}
maxParentWidth={maxParentWidth}
/>
</div>
);
Expand Down
26 changes: 21 additions & 5 deletions src/components/views/messages/MLocationBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { _t } from '../../../languageHandler';
import MemberAvatar from '../avatars/MemberAvatar';
import Modal from '../../../Modal';
import LocationViewDialog from '../location/LocationViewDialog';
import TooltipTarget from '../elements/TooltipTarget';
import { Alignment } from '../elements/Tooltip';

interface IState {
error: Error;
Expand Down Expand Up @@ -93,6 +95,7 @@ export default class MLocationBody extends React.Component<IBodyProps, IState> {
bodyId={this.getBodyId()}
markerId={this.getMarkerId()}
error={this.state.error}
tooltip={_t("Expand map")}
onClick={this.onClick}
/>;
}
Expand All @@ -103,10 +106,17 @@ interface ILocationBodyContentProps {
bodyId: string;
markerId: string;
error: Error;
tooltip?: string;
onClick?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
}

export function LocationBodyContent(props: ILocationBodyContentProps) {
const mapDiv = <div
id={props.bodyId}
onClick={props.onClick}
className="mx_MLocationBody_map"
/>;

return <div className="mx_MLocationBody">
{
props.error
Expand All @@ -115,11 +125,17 @@ export function LocationBodyContent(props: ILocationBodyContentProps) {
</div>
: null
}
<div
id={props.bodyId}
onClick={props.onClick}
className="mx_MLocationBody_map"
/>
{
props.tooltip
? <TooltipTarget
label={props.tooltip}
alignment={Alignment.InnerBottom}
maxParentWidth={450}
>
{ mapDiv }
</TooltipTarget>
: mapDiv
}
<div className="mx_MLocationBody_marker" id={props.markerId}>
<div className="mx_MLocationBody_markerBorder">
<MemberAvatar
Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,7 @@
"Declining …": "Declining …",
"%(name)s wants to verify": "%(name)s wants to verify",
"You sent a verification request": "You sent a verification request",
"Expand map": "Expand map",
"Failed to load map": "Failed to load map",
"Vote not registered": "Vote not registered",
"Sorry, your vote was not registered. Please try again.": "Sorry, your vote was not registered. Please try again.",
Expand Down