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

Commit 7ed3089

Browse files
committed
Improve tooltip positioning
Signed-off-by: Michael Weimann <[email protected]>
1 parent 1e73184 commit 7ed3089

File tree

16 files changed

+112
-68
lines changed

16 files changed

+112
-68
lines changed

res/css/views/elements/_MiniAvatarUploader.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ limitations under the License.
2525
z-index: unset;
2626
width: max-content;
2727
left: 72px;
28-
top: 0;
28+
// top edge starting at 50 % of parent - 50 % of itself -> centered vertically
29+
top: 50%;
30+
transform: translateY(-50%);
2931
}
3032

3133
.mx_MiniAvatarUploader_indicator {

res/css/views/elements/_Tooltip.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ limitations under the License.
7070
font-weight: 500;
7171
max-width: 300px;
7272
word-break: break-word;
73-
margin-left: 6px;
74-
margin-right: 6px;
7573

7674
background-color: #21262C; // Same on both themes
7775
color: $accent-fg-color;

res/css/views/rooms/_RoomSublist.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,6 @@ limitations under the License.
406406
}
407407
}
408408

409-
.mx_RoomSublist_addRoomTooltip {
410-
margin-top: -3px;
411-
}
412-
413409
.mx_RoomSublist_skeletonUI {
414410
position: relative;
415411
margin-left: 4px;

src/components/structures/SpaceHierarchy.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import { ViewRoomPayload } from "../../dispatcher/payloads/ViewRoomPayload";
6464
import { JoinRoomReadyPayload } from "../../dispatcher/payloads/JoinRoomReadyPayload";
6565
import { KeyBindingAction } from "../../accessibility/KeyboardShortcuts";
6666
import { getKeyBindingsManager } from "../../KeyBindingsManager";
67+
import { Alignment } from "../views/elements/Tooltip";
6768

6869
interface IProps {
6970
space: Room;
@@ -583,7 +584,7 @@ const ManageButtons = ({ hierarchy, selected, setSelected, setError }: IManageBu
583584
Button = AccessibleTooltipButton;
584585
props = {
585586
tooltip: _t("Select a room below first"),
586-
yOffset: -40,
587+
alignment: Alignment.Top,
587588
};
588589
}
589590

src/components/views/beta/BetaCard.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export const BetaPill = ({
6060
</div>
6161
</div>}
6262
onClick={onClick}
63-
yOffset={-10}
6463
>
6564
{ _t("Beta") }
6665
</AccessibleTooltipButton>;

src/components/views/dialogs/ForwardDialog.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ const Entry: React.FC<IEntryProps> = ({ room, type, content, matrixClient: cli,
136136
className="mx_ForwardList_roomButton"
137137
onClick={jumpToRoom}
138138
title={_t("Open room")}
139-
yOffset={-20}
140139
alignment={Alignment.Top}
141140
>
142141
<DecoratedRoomAvatar room={room} avatarSize={32} />
@@ -151,7 +150,6 @@ const Entry: React.FC<IEntryProps> = ({ room, type, content, matrixClient: cli,
151150
onClick={send}
152151
disabled={disabled}
153152
title={title}
154-
yOffset={-20}
155153
alignment={Alignment.Top}
156154
>
157155
<div className="mx_ForwardList_sendLabel">{ _t("Send") }</div>

src/components/views/elements/AccessibleTooltipButton.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ interface IProps extends React.ComponentProps<typeof AccessibleButton> {
2626
label?: string;
2727
tooltipClassName?: string;
2828
forceHide?: boolean;
29-
yOffset?: number;
3029
alignment?: Alignment;
3130
onHover?: (hovering: boolean) => void;
3231
onHideTooltip?(ev: SyntheticEvent): void;
@@ -76,13 +75,12 @@ export default class AccessibleTooltipButton extends React.PureComponent<IProps,
7675

7776
render() {
7877
// eslint-disable-next-line @typescript-eslint/no-unused-vars
79-
const { title, tooltip, children, tooltipClassName, forceHide, yOffset, alignment, onHideTooltip,
78+
const { title, tooltip, children, tooltipClassName, forceHide, alignment, onHideTooltip,
8079
...props } = this.props;
8180

8281
const tip = this.state.hover && <Tooltip
8382
tooltipClassName={tooltipClassName}
8483
label={tooltip || title}
85-
yOffset={yOffset}
8684
alignment={alignment}
8785
/>;
8886
return (

src/components/views/elements/FacePile.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const FacePile: FC<IProps> = ({ members, faceSize, overflow, tooltip, children,
5151

5252
return <div {...props} className="mx_FacePile">
5353
{ tooltip ? (
54-
<TextWithTooltip class="mx_FacePile_faces" tooltip={tooltip} tooltipProps={{ yOffset: 32 }}>
54+
<TextWithTooltip class="mx_FacePile_faces" tooltip={tooltip}>
5555
{ pileContents }
5656
</TextWithTooltip>
5757
) : (

src/components/views/elements/Tooltip.tsx

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import classNames from 'classnames';
2323

2424
import UIStore from "../../../stores/UIStore";
2525

26-
const MIN_TOOLTIP_HEIGHT = 25;
27-
2826
export enum Alignment {
2927
Natural, // Pick left or right
3028
Left,
@@ -33,7 +31,6 @@ export enum Alignment {
3331
Bottom, // Centered
3432
InnerBottom, // Inside the target, at the bottom
3533
TopRight, // On top of the target, right aligned
36-
TopCenter, // On top of the target, center aligned
3734
}
3835

3936
export interface ITooltipProps {
@@ -48,7 +45,6 @@ export interface ITooltipProps {
4845
// the react element to put into the tooltip
4946
label: React.ReactNode;
5047
alignment?: Alignment; // defaults to Natural
51-
yOffset?: number;
5248
// id describing tooltip
5349
// used to associate tooltip with target for a11y
5450
id?: string;
@@ -66,7 +62,6 @@ export default class Tooltip extends React.Component<ITooltipProps> {
6662

6763
public static readonly defaultProps = {
6864
visible: true,
69-
yOffset: 0,
7065
alignment: Alignment.Natural,
7166
};
7267

@@ -102,65 +97,60 @@ export default class Tooltip extends React.Component<ITooltipProps> {
10297
// positioned, also taking into account any window zoom
10398
private updatePosition(style: CSSProperties) {
10499
const parentBox = this.parent.getBoundingClientRect();
105-
let offset = 0;
106-
if (parentBox.height > MIN_TOOLTIP_HEIGHT) {
107-
offset = Math.floor((parentBox.height - MIN_TOOLTIP_HEIGHT) / 2);
108-
} else {
109-
// The tooltip is larger than the parent height: figure out what offset
110-
// we need so that we're still centered.
111-
offset = Math.floor(parentBox.height - MIN_TOOLTIP_HEIGHT);
112-
}
113100
const width = UIStore.instance.windowWidth;
101+
const spacing = 6;
114102
const parentWidth = (
115103
this.props.maxParentWidth
116104
? Math.min(parentBox.width, this.props.maxParentWidth)
117105
: parentBox.width
118106
);
119-
const baseTop = (parentBox.top - 2 + this.props.yOffset) + window.scrollY;
120-
const top = baseTop + offset;
107+
const baseTop = parentBox.top + window.scrollY;
108+
const centerTop = parentBox.top + window.scrollY + (parentBox.height / 2);
121109
const right = width - parentBox.left - window.scrollX;
122110
const left = parentBox.right + window.scrollX;
123111
const horizontalCenter = (
124112
parentBox.left - window.scrollX + (parentWidth / 2)
125113
);
114+
126115
switch (this.props.alignment) {
127116
case Alignment.Natural:
128117
if (parentBox.right > width / 2) {
129-
style.right = right;
130-
style.top = top;
118+
style.right = right + spacing;
119+
style.top = centerTop;
120+
style.transform = "translateY(-50%)";
131121
break;
132122
}
133123
// fall through to Right
134124
case Alignment.Right:
135-
style.left = left;
136-
style.top = top;
125+
style.left = left + spacing;
126+
style.top = centerTop;
127+
style.transform = "translateY(-50%)";
137128
break;
138129
case Alignment.Left:
139-
style.right = right;
140-
style.top = top;
130+
style.right = right + spacing;
131+
style.top = centerTop;
132+
style.transform = "translateY(-50%)";
141133
break;
142134
case Alignment.Top:
143-
style.top = baseTop - 16;
135+
style.top = baseTop - spacing;
144136
style.left = horizontalCenter;
137+
style.transform = "translate(-50%, -100%)";
145138
break;
146139
case Alignment.Bottom:
147-
style.top = baseTop + parentBox.height;
140+
style.top = baseTop + parentBox.height + spacing;
148141
style.left = horizontalCenter;
142+
style.transform = "translate(-50%)";
149143
break;
150144
case Alignment.InnerBottom:
151145
style.top = baseTop + parentBox.height - 50;
152146
style.left = horizontalCenter;
153147
style.transform = "translate(-50%)";
154148
break;
155149
case Alignment.TopRight:
156-
style.top = baseTop - 5;
150+
style.top = baseTop - spacing;
157151
style.right = width - parentBox.right - window.scrollX;
158-
style.transform = "translate(5px, -100%)";
152+
style.transform = "translateY(-100%)";
159153
break;
160-
case Alignment.TopCenter:
161-
style.top = baseTop - 5;
162-
style.left = horizontalCenter;
163-
style.transform = "translate(-50%, -100%)";
164154
}
165155

166156
return style;

src/components/views/elements/TooltipTarget.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const TooltipTarget: React.FC<IProps> = ({
3434
id,
3535
label,
3636
alignment,
37-
yOffset,
3837
tooltipClassName,
3938
maxParentWidth,
4039
...rest
@@ -51,7 +50,6 @@ const TooltipTarget: React.FC<IProps> = ({
5150
className={className}
5251
tooltipClassName={tooltipClassName}
5352
label={label}
54-
yOffset={yOffset}
5553
alignment={alignment}
5654
visible={isVisible}
5755
maxParentWidth={maxParentWidth}

0 commit comments

Comments
 (0)