Skip to content

Commit ce0a43e

Browse files
committed
fix all positions
1 parent 3625058 commit ce0a43e

File tree

9 files changed

+259
-254
lines changed

9 files changed

+259
-254
lines changed

example/hello-world/withScroll.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ <h1 data-step="3" data-intro="This is a tooltip!">Works with a Scrollable Elemen
5151
<hr>
5252

5353
<div class="row-fluid marketing">
54-
<h4 data-step="2" data-intro="Another step.">Section One</h4>
54+
<h4 data-step="2" data-intro="Another step." data-position="left-middle-aligned">Section One</h4>
5555
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis mollis augue a neque cursus ac blandit orci faucibus. Phasellus nec metus purus.</p>
5656

5757
<h4>Section Two</h4>
@@ -69,7 +69,7 @@ <h4>Section Four</h4>
6969
<h4>Section Five</h4>
7070
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis mollis augue a neque cursus ac blandit orci faucibus. Phasellus nec metus purus.</p>
7171

72-
<h4 data-step="1" data-intro="A scrolling step.">Section Six</h4>
72+
<h4 data-step="1" data-intro="A scrolling step." data-position="right-middle-aligned">Section Six</h4>
7373
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis mollis augue a neque cursus ac blandit orci faucibus. Phasellus nec metus purus.</p>
7474

7575
<h4>Section Seven</h4>

example/html-tooltip/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ <h4>Section Six</h4>
9494
{
9595
element: '#step4',
9696
intro: "<span style='font-family: Tahoma'>Another step with new font!</span>",
97-
position: 'bottom'
97+
position: 'bottom-middle-aligned'
9898
},
9999
{
100100
element: '#step5',
101-
intro: '<strong>Get</strong> it, <strong>use</strong> it.'
101+
intro: '<strong>Get</strong> it, <strong>use</strong> it.',
102+
position: 'top-middle-aligned'
102103
}
103104
]
104105
});

src/packages/hint/option.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TooltipPosition } from "../../packages/tooltip";
1+
import { TooltipBasePosition } from "../../packages/tooltip";
22
import { HintItem, HintPosition } from "./hintItem";
33
import { Translator, LanguageCode } from "../../i18n/language";
44

@@ -28,7 +28,7 @@ export interface HintOptions {
2828
/* To determine the tooltip position automatically based on the window.width/height */
2929
autoPosition: boolean;
3030
/* Precedence of positions, when auto is enabled */
31-
positionPrecedence: TooltipPosition[];
31+
positionPrecedence: TooltipBasePosition[];
3232
/* Optional property to determine if content should be rendered as HTML */
3333
tooltipRenderAsHtml?: boolean;
3434
/* Optional property to set the language of the hint.

src/packages/tooltip/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { TooltipPosition } from "./tooltipPosition";
1+
export { TooltipPosition, TooltipBasePosition } from "./tooltipPosition";

src/packages/tooltip/tooltip.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import getOffset, { Offset } from "../../util/getOffset";
22
import getWindowSize from "../../util/getWindowSize";
33
import dom, { ChildDom, State } from "../dom";
44
import { arrowClassName, tooltipClassName } from "../tour/classNames";
5-
import { determineAutoPosition, TooltipPosition } from "./tooltipPosition";
5+
import {
6+
determineAutoPosition,
7+
TooltipPosition,
8+
TooltipBasePosition,
9+
} from "./tooltipPosition";
610

711
const { div } = dom.tags;
812

@@ -313,7 +317,7 @@ export type TooltipProps = {
313317

314318
// auto-alignment properties
315319
autoPosition: boolean;
316-
positionPrecedence: TooltipPosition[];
320+
positionPrecedence: TooltipBasePosition[];
317321

318322
onClick?: (e: any) => void;
319323
className?: string;
Lines changed: 73 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,89 @@
1-
import { determineAutoPosition, TooltipPosition } from "./tooltipPosition";
1+
import { determineAutoPosition } from "../tooltip/tooltipPosition";
2+
import type { Offset } from "../../util/getOffset";
23

3-
const positionPrecedence: TooltipPosition[] = [
4-
"bottom",
5-
"top",
6-
"right",
7-
"left",
8-
];
4+
const mockViewport = { width: 1000, height: 800 };
95

10-
describe("placeTooltip", () => {
11-
test("should automatically place the tooltip position when there is enough space", () => {
12-
// Arrange
13-
// Act
14-
const position = determineAutoPosition(
15-
positionPrecedence,
16-
{
17-
top: 200,
18-
left: 200,
19-
height: 100,
20-
width: 100,
21-
right: 300,
22-
bottom: 300,
23-
absoluteTop: 200,
24-
absoluteLeft: 200,
25-
absoluteRight: 300,
26-
absoluteBottom: 300,
27-
},
28-
100,
6+
const makeOffset = (
7+
left: number,
8+
top: number,
9+
width = 100,
10+
height = 50
11+
): Offset => ({
12+
top,
13+
left,
14+
width,
15+
height,
16+
bottom: top + height,
17+
right: left + width,
18+
absoluteTop: top,
19+
absoluteLeft: left,
20+
absoluteBottom: top + height,
21+
absoluteRight: left + width,
22+
});
23+
24+
describe("determineAutoPosition", () => {
25+
it("should return 'bottom-left-aligned' when there is enough space below", () => {
26+
const target = makeOffset(400, 200);
27+
const pos = determineAutoPosition(
28+
["bottom", "top"],
29+
target,
30+
200,
2931
100,
30-
"top",
31-
{ height: 1000, width: 1000 }
32+
"bottom",
33+
mockViewport
3234
);
33-
34-
// Assert
35-
expect(position).toBe("top-right-aligned");
35+
expect(pos).toBe("bottom-left-aligned");
3636
});
3737

38-
test("should use floating tooltips when height/width is limited", () => {
39-
// Arrange
40-
// Act
41-
const position = determineAutoPosition(
42-
positionPrecedence,
43-
{
44-
top: 0,
45-
left: 0,
46-
height: 100,
47-
width: 100,
48-
right: 0,
49-
bottom: 0,
50-
absoluteTop: 0,
51-
absoluteLeft: 0,
52-
absoluteRight: 0,
53-
absoluteBottom: 0,
54-
},
55-
100,
38+
it("should return 'top-left-aligned' when there is no space below", () => {
39+
const target = makeOffset(400, 750);
40+
const pos = determineAutoPosition(
41+
["bottom", "top"],
42+
target,
43+
200,
5644
100,
57-
"top",
58-
{ height: 100, width: 100 }
45+
"bottom",
46+
mockViewport
5947
);
60-
61-
// Assert
62-
expect(position).toBe("floating");
48+
expect(pos).toBe("top-left-aligned");
6349
});
6450

65-
test("should use bottom middle aligned when there is enough vertical space", () => {
66-
// Arrange
67-
// Act
68-
const position = determineAutoPosition(
69-
positionPrecedence,
70-
{
71-
top: 0,
72-
left: 0,
73-
height: 100,
74-
width: 100,
75-
right: 0,
76-
bottom: 0,
77-
absoluteTop: 0,
78-
absoluteLeft: 0,
79-
absoluteRight: 0,
80-
absoluteBottom: 0,
81-
},
51+
it("should switch to 'left' when right side has no space", () => {
52+
const target = makeOffset(950, 400);
53+
const pos = determineAutoPosition(
54+
["right", "left", "top", "bottom"],
55+
target,
8256
100,
57+
50,
58+
"right",
59+
mockViewport
60+
);
61+
expect(pos).toBe("left");
62+
});
63+
64+
it("should fall back to 'floating' when no space anywhere", () => {
65+
const target = makeOffset(0, 0, 1200, 900);
66+
const pos = determineAutoPosition(
67+
["top", "bottom", "left", "right"],
68+
target,
69+
200,
8370
100,
84-
"left",
85-
{ height: 500, width: 100 }
71+
"bottom",
72+
mockViewport
8673
);
74+
expect(pos).toBe("floating");
75+
});
8776

88-
// Assert
89-
expect(position).toBe("bottom-middle-aligned");
77+
it("should respect desired alignment if possible", () => {
78+
const target = makeOffset(400, 200);
79+
const pos = determineAutoPosition(
80+
["bottom", "top"],
81+
target,
82+
200,
83+
100,
84+
"bottom-right-aligned",
85+
mockViewport
86+
);
87+
expect(pos).toBe("bottom-right-aligned");
9088
});
9189
});

0 commit comments

Comments
 (0)