Skip to content

Commit 0b840d2

Browse files
fehmerMiodec
andauthored
feat: leaderboard scroll to users position when using the user button (@fehmer) (monkeytypegame#6285)
- scroll to users position when using the user button - add scroll to top button on leaderboard page --------- Co-authored-by: Miodec <[email protected]>
1 parent 7edbd95 commit 0b840d2

File tree

7 files changed

+82
-54
lines changed

7 files changed

+82
-54
lines changed

frontend/src/html/pages/about.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<div class="page pageAbout hidden full-width content-grid" id="pageAbout">
2-
<div class="scrollToTopButton invisible">
3-
<i class="fas fa-angle-double-up"></i>
4-
</div>
52
<div class="created">
63
Created with love by Miodec.
74
<br />

frontend/src/html/pages/account.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<div class="page pageAccount hidden full-width" id="pageAccount">
2-
<div class="scrollToTopButton invisible">
3-
<i class="fas fa-angle-double-up"></i>
4-
</div>
52
<div class="preloader hidden">
63
<div class="icon">
74
<i class="fas fa-fw fa-spin fa-circle-notch"></i>

frontend/src/html/pages/settings.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<div class="page pageSettings hidden full-width content-grid" id="pageSettings">
2-
<div class="scrollToTopButton invisible">
3-
<i class="fas fa-angle-double-up"></i>
4-
</div>
52
<div class="settingsGroup quickNav">
63
<div class="links">
74
<a class="textButton" href="#group_behavior">

frontend/src/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<div class="bar"></div>
2222
</div>
2323
</div>
24+
<div class="content-grid scrollToTopContainer">
25+
<div class="scrollToTopButton invisible breakout">
26+
<i class="fas fa-angle-double-up"></i>
27+
</div>
28+
</div>
2429
<div id="popups">
2530
<load src="html/popups.html" />
2631
</div>

frontend/src/styles/core.scss

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,37 @@
5959
--padding-inline: 2rem;
6060
--content-max-width: 1536px;
6161

62-
// --breakout-max-width: 1200px;
63-
64-
// --breakout-size: calc(
65-
// (var(--breakout-max-width) - var(--content-max-width)) / 2
66-
// );
62+
--breakout-size: calc(
63+
(calc(var(--content-max-width) + 12rem) - var(--content-max-width)) / 2
64+
);
6765

6866
justify-content: center;
6967
display: grid;
7068
grid-template-columns:
71-
[full-width-start] minmax(0, var(--padding-inline))
72-
[full-width-padding-start] minmax(0, 1fr)
73-
// [breakout-start] minmax(0, var(--breakout-size))
74-
[content-start] min(
75-
100% - (var(--padding-inline) * 2),
76-
var(--content-max-width)
77-
)
69+
[full-width-start]
70+
var(--padding-inline)
71+
[full-width-padding-start]
72+
minmax(0, 1fr)
73+
[breakout-start]
74+
minmax(0, var(--breakout-size))
75+
[content-start]
76+
min(100% - (var(--padding-inline) * 2), var(--content-max-width))
7877
[content-end]
79-
// minmax(0, var(--breakout-size)) [breakout-end]
80-
minmax(0, 1fr) [full-width-padding-end]
81-
minmax(0, var(--padding-inline)) [full-width-end];
78+
minmax(0, var(--breakout-size))
79+
[breakout-end]
80+
minmax(0, 1fr)
81+
[full-width-padding-end]
82+
var(--padding-inline)
83+
[full-width-end];
8284
}
8385

8486
.content-grid > :not(.breakout, .full-width, .full-width-padding) {
8587
grid-column: content;
8688
}
8789

88-
// #app > .breakout {
89-
// grid-column: breakout;
90-
// }
90+
.content-grid > .breakout {
91+
grid-column: breakout;
92+
}
9193

9294
.content-grid > .full-width {
9395
grid-column: full-width;
@@ -283,24 +285,34 @@ key {
283285
visibility: hidden !important;
284286
}
285287

286-
.scrollToTopButton {
287-
bottom: 4rem;
288-
right: 2rem;
288+
.scrollToTopContainer {
289289
position: fixed;
290-
font-size: 2rem;
291-
width: 4rem;
292-
height: 4rem;
293-
text-align: center;
294-
line-height: 4rem;
295-
background: var(--sub-alt-color);
296-
border-radius: 99rem;
297-
z-index: 99;
298-
cursor: pointer;
299-
color: var(--sub-color);
300-
transition: 0.25s;
301-
&:hover {
302-
background: var(--text-color);
303-
color: var(--bg-color);
290+
width: 100%;
291+
height: 100%;
292+
pointer-events: none;
293+
z-index: 99999999;
294+
295+
.scrollToTopButton {
296+
pointer-events: all;
297+
place-self: end end;
298+
margin-bottom: 2rem;
299+
font-size: 2rem;
300+
width: 4rem;
301+
height: 4rem;
302+
text-align: center;
303+
304+
line-height: 4rem;
305+
background: var(--sub-alt-color);
306+
border-radius: 99rem;
307+
outline: 0.5rem solid var(--bg-color);
308+
309+
cursor: pointer;
310+
color: var(--sub-color);
311+
transition: 0.25s;
312+
&:hover {
313+
background: var(--text-color);
314+
color: var(--bg-color);
315+
}
304316
}
305317
}
306318

frontend/src/ts/elements/scroll-to-top.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { prefersReducedMotion } from "../utils/misc";
44
let visible = false;
55

66
$(document).on("click", ".scrollToTopButton", () => {
7+
$(".scrollToTopButton").addClass("invisible");
78
window.scrollTo({
89
top: 0,
910
behavior: prefersReducedMotion() ? "instant" : "smooth",
@@ -13,14 +14,13 @@ $(document).on("click", ".scrollToTopButton", () => {
1314
$(window).on("scroll", () => {
1415
const page = ActivePage.get();
1516
if (page === "test") return;
16-
if (page === "about" || page === "settings" || page === "account") {
17-
const scroll = window.scrollY;
18-
if (!visible && scroll > 100) {
19-
$(".scrollToTopButton").removeClass("invisible");
20-
visible = true;
21-
} else if (visible && scroll < 100) {
22-
$(".scrollToTopButton").addClass("invisible");
23-
visible = false;
24-
}
17+
18+
const scroll = window.scrollY;
19+
if (!visible && scroll > 100) {
20+
$(".scrollToTopButton").removeClass("invisible");
21+
visible = true;
22+
} else if (visible && scroll < 100) {
23+
$(".scrollToTopButton").addClass("invisible");
24+
visible = false;
2525
}
2626
});

frontend/src/ts/pages/leaderboards.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import { differenceInSeconds } from "date-fns/differenceInSeconds";
1616
import * as DateTime from "../utils/date-and-time";
1717
import { getHtmlByUserFlags } from "../controllers/user-flag-controller";
1818
import { getHTMLById as getBadgeHTMLbyId } from "../controllers/badge-controller";
19-
import { getDiscordAvatarUrl, isDevEnvironment } from "../utils/misc";
19+
import {
20+
applyReducedMotion,
21+
getDiscordAvatarUrl,
22+
isDevEnvironment,
23+
} from "../utils/misc";
2024
import { abbreviateNumber } from "../utils/numbers";
2125
import {
2226
getCurrentWeekTimestamp,
@@ -66,6 +70,7 @@ type State = {
6670
title: string;
6771
error?: string;
6872
discordAvatarUrls: Map<string, string>;
73+
scrollToUserAfterFill: boolean;
6974
} & (AllTimeState | WeeklyState | DailyState);
7075

7176
const state = {
@@ -79,6 +84,7 @@ const state = {
7984
pageSize: 50,
8085
title: "All-time English Time 15 Leaderboard",
8186
discordAvatarUrls: new Map<string, string>(),
87+
scrollToUserAfterFill: false,
8288
} as State;
8389

8490
function updateTitle(): void {
@@ -896,6 +902,19 @@ function updateContent(): void {
896902
updateJumpButtons();
897903
updateTimerVisibility();
898904
fillTable();
905+
906+
if (state.scrollToUserAfterFill) {
907+
const windowHeight = $(window).height() ?? 0;
908+
const offset = $(`.tableAndUser .me`).offset()?.top ?? 0;
909+
const scrollTo = offset - windowHeight / 2;
910+
$([document.documentElement, document.body]).animate(
911+
{
912+
scrollTop: scrollTo,
913+
},
914+
applyReducedMotion(500)
915+
);
916+
state.scrollToUserAfterFill = false;
917+
}
899918
}
900919

901920
function updateTypeButtons(): void {
@@ -1068,6 +1087,7 @@ function handleJumpButton(action: string, page?: number): void {
10681087
}
10691088

10701089
state.page = page;
1090+
state.scrollToUserAfterFill = true;
10711091
}
10721092
}
10731093
}

0 commit comments

Comments
 (0)