Skip to content

Commit

Permalink
refactor: cookie popup
Browse files Browse the repository at this point in the history
use animated modal class
should fix brave having issues with the cookie popup
  • Loading branch information
Miodec committed Mar 3, 2024
1 parent e093b51 commit f35e8fb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 93 deletions.
11 changes: 5 additions & 6 deletions frontend/src/html/popups.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,17 @@
<div id="simplePopup" popupId=""></div>
</div>

<div
id="cookiePopupWrapper"
class="cookie-popup-wrapper cookiePopupWrapper popupWrapper hidden"
tabindex="-1"
<dialog
id="cookiePopup"
class="cookie-popup-wrapper cookiePopupWrapper modalWrapper hidden"
>
<div class="extensionMessage">
If you see this text, that means an extension is blocking a cookie consent
popup. This will cause the website to incorrectly assume its still visible
and stop you from enjoying Monkeytype. Please disable any extensions that
block cookie popups and refresh the page.
</div>
<div id="cookiePopup">
<div class="modal">
<div class="title">
<i class="fas far fa-cookie-bite"></i>
We use cookies by the way
Expand Down Expand Up @@ -126,7 +125,7 @@
<button class="active acceptSelected">accept selected</button>
</div>
</div>
</div>
</dialog>

<div id="shareTestSettingsPopupWrapper" class="popupWrapper hidden">
<div id="shareTestSettingsPopup">
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/styles/popups.scss
Original file line number Diff line number Diff line change
Expand Up @@ -602,16 +602,17 @@
color: var(--error-color);
}
}
#cookiePopupWrapper {
#cookiePopup {
.extensionMessage {
position: fixed;
right: 2rem;
bottom: 2rem;
padding: 1rem;
width: 465px;
font-size: 0.75rem;
color: var(--text-color);
}
#cookiePopup {
.modal {
position: fixed;
right: 2rem;
bottom: 2rem;
Expand All @@ -620,7 +621,7 @@
padding: 2rem;
display: grid;
gap: 1rem;
width: 465px;
min-width: 465px;
z-index: 100000001;
// outline: 0.5rem solid var(--bg-color)
-webkit-user-select: none;
Expand Down
11 changes: 0 additions & 11 deletions frontend/src/ts/controllers/input-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import * as KeymapEvent from "../observables/keymap-event";
import { IgnoredKeys } from "../constants/ignored-keys";
import { ModifierKeys } from "../constants/modifier-keys";
import { navigate } from "./route-controller";
import * as CookiePopup from "../popups/cookie-popup";

let dontInsertSpace = false;
let correctShiftUsed = true;
Expand Down Expand Up @@ -874,15 +873,12 @@ $("#wordsInput").on("keydown", (event) => {
const commandLineVisible = Misc.isPopupVisible("commandLineWrapper");
const leaderboardsVisible = Misc.isPopupVisible("leaderboardsWrapper");
const popupVisible: boolean = Misc.isAnyPopupVisible();
const cookiePopupVisible = CookiePopup.isVisible();

const allowTyping: boolean =
pageTestActive &&
!commandLineVisible &&
!leaderboardsVisible &&
!popupVisible &&
!TestUI.resultVisible &&
!cookiePopupVisible &&
event.key !== "Enter";

if (!allowTyping) {
Expand Down Expand Up @@ -913,13 +909,6 @@ $(document).on("keydown", async (event) => {

const popupVisible: boolean = Misc.isAnyPopupVisible();

const cookiePopupVisible = CookiePopup.isVisible();

if (cookiePopupVisible) {
console.debug("Ignoring keydown event because cookie popup is visible.");
return;
}

const allowTyping: boolean =
pageTestActive &&
!commandLineVisible &&
Expand Down
8 changes: 1 addition & 7 deletions frontend/src/ts/pages/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { updateFooterAndVerticalAds } from "../controllers/ad-controller";
import * as ModesNotice from "../elements/modes-notice";
import * as Keymap from "../elements/keymap";
import * as TestConfig from "../test/test-config";
import * as CookiePopup from "../popups/cookie-popup";

export const page = new Page(
"test",
Expand Down Expand Up @@ -36,11 +35,6 @@ export const page = new Page(
void Keymap.refresh();
},
async () => {
if (CookiePopup.isVisible()) {
TestUI.blurWords();
$("#cookiePopupWrapper").trigger("focus");
} else {
TestUI.focusWords();
}
TestUI.focusWords();
}
);
7 changes: 7 additions & 0 deletions frontend/src/ts/popups/animated-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,11 @@ export default class AnimatedModal {
}
});
}

destroy(): void {
this.wrapperEl.close();
this.wrapperEl.classList.add("hidden");
Skeleton.remove(this.wrapperId);
this.open = false;
}
}
87 changes: 21 additions & 66 deletions frontend/src/ts/popups/cookie-popup.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { activateAnalytics } from "../controllers/analytics-controller";
import { focusWords } from "../test/test-ui";
import * as Notifications from "../elements/notifications";
import * as Skeleton from "./skeleton";
import { isPopupVisible } from "../utils/misc";
import * as AdController from "../controllers/ad-controller";

const wrapperId = "cookiePopupWrapper";

let visible = false;
import AnimatedModal from "./animated-modal";

type Accepted = {
security: boolean;
Expand All @@ -34,61 +29,18 @@ export function check(): void {
}
}

export function isVisible(): boolean {
return visible;
}

export function show(): void {
Skeleton.append(wrapperId);
$("#cookiePopupWrapper").removeClass("hidden");
visible = true;
if (
$("#cookiePopupWrapper")[0] === undefined ||
!$("#cookiePopupWrapper").is(":visible") ||
$("#cookiePopupWrapper").outerHeight(true) === 0
) {
//removed by cookie popup blocking extension
$("#cookiePopupWrapper").addClass("hidden");
visible = false;
Skeleton.remove(wrapperId);
return;
}
if (!isPopupVisible(wrapperId)) {
$("#cookiePopupWrapper")
.stop(true, true)
.css("opacity", 0)
.removeClass("hidden")
.animate({ opacity: 1 }, 0, () => {
if (
!$("#cookiePopupWrapper").is(":visible") ||
$("#cookiePopupWrapper").outerHeight(true) === 0
) {
visible = false;
} else {
visible = true;
}
});
}
void modal.show({
afterAnimation: async () => {
if (!isPopupVisible("cookiePopup")) {
modal.destroy();
}
},
});
}

async function hide(): Promise<void> {
if (isPopupVisible(wrapperId)) {
focusWords();
$("#cookiePopupWrapper")
.stop(true, true)
.css("opacity", 1)
.animate(
{
opacity: 0,
},
125,
() => {
$("#cookiePopupWrapper").addClass("hidden");
visible = false;
Skeleton.remove(wrapperId);
}
);
}
void modal.hide();
}

export function showSettings(): void {
Expand All @@ -97,14 +49,10 @@ export function showSettings(): void {
}

function verifyVisible(): void {
if (!visible) return;
if (
$("#cookiePopupWrapper")[0] === undefined ||
!$("#cookiePopupWrapper").is(":visible") ||
$("#cookiePopupWrapper").outerHeight(true) === 0
) {
if (!modal.isOpen()) return;
if (!isPopupVisible("cookiePopup")) {
//removed by cookie popup blocking extension
visible = false;
void modal.destroy();
}
}

Expand Down Expand Up @@ -147,7 +95,7 @@ $("#cookiePopup .openSettings").on("click", () => {

$(document).on("keypress", (e) => {
verifyVisible();
if (visible) {
if (modal.isOpen()) {
e.preventDefault();
}
});
Expand All @@ -164,4 +112,11 @@ $("#cookiePopup .cookie.ads .textButton").on("click", () => {
}
});

Skeleton.save(wrapperId);
const modal = new AnimatedModal("cookiePopup", undefined, {
customEscapeHandler: (): void => {
//
},
customWrapperClickHandler: (): void => {
//
},
});

0 comments on commit f35e8fb

Please sign in to comment.