Skip to content

Commit fcbc00b

Browse files
committed
refactor(share custom theme): use new modal system
1 parent bc6cf22 commit fcbc00b

File tree

7 files changed

+107
-133
lines changed

7 files changed

+107
-133
lines changed

frontend/src/html/popups.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -834,16 +834,16 @@
834834
</form>
835835
</div>
836836
</div>
837-
<div id="shareCustomThemeWrapper" class="popupWrapper hidden">
838-
<div id="shareCustomThemeEdit" action="">
839-
<div class="title">Share Custom Theme</div>
837+
<dialog id="shareCustomThemeModal" class="modalWrapper hidden">
838+
<div class="modal">
839+
<div class="title">Share custom theme</div>
840840
<label class="checkbox">
841841
<input type="checkbox" id="includeBackground" />
842842
Include background information
843843
</label>
844-
<div class="button copy-button">copy link to clipboard</div>
844+
<button>copy link to clipboard</button>
845845
</div>
846-
</div>
846+
</dialog>
847847
<div id="leaderboardsWrapper" class="popupWrapper hidden">
848848
<div id="leaderboards">
849849
<div class="leaderboardsTop">

frontend/src/styles/popups.scss

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,15 +1445,9 @@
14451445
}
14461446
}
14471447
}
1448-
#shareCustomThemeWrapper {
1449-
#shareCustomThemeEdit {
1450-
background: var(--bg-color);
1451-
border-radius: var(--roundness);
1452-
padding: 2rem;
1453-
display: grid;
1454-
gap: 1rem;
1455-
overflow-y: scroll;
1456-
width: 500px;
1448+
#shareCustomThemeModal {
1449+
.modal {
1450+
max-width: 400px;
14571451

14581452
.buttons {
14591453
display: grid;
@@ -1463,7 +1457,7 @@
14631457

14641458
.title {
14651459
font-size: 1.5rem;
1466-
line-height: 2rem;
1460+
line-height: 1.5rem;
14671461
color: var(--sub-color);
14681462
}
14691463
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as ShareCustomThemeModal from "../modals/share-custom-theme";
2+
3+
const settingsPage = document.querySelector("#pageSettings");
4+
5+
settingsPage
6+
?.querySelector("#shareCustomThemeButton")
7+
?.addEventListener("click", () => {
8+
ShareCustomThemeModal.show();
9+
});

frontend/src/ts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import "./event-handlers/footer";
77
import "./event-handlers/keymap";
88
import "./event-handlers/test";
99
import "./event-handlers/about";
10+
import "./event-handlers/settings";
1011

1112
import "./firebase";
1213
import * as Logger from "./utils/logger";
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import * as ThemeController from "../controllers/theme-controller";
2+
import Config from "../config";
3+
import * as Notifications from "../elements/notifications";
4+
import * as CustomThemePopup from "../popups/custom-theme-popup";
5+
import { createErrorMessage } from "../utils/misc";
6+
import AnimatedModal from "../utils/animated-modal";
7+
8+
type State = {
9+
includeBackground: boolean;
10+
};
11+
12+
const state: State = {
13+
includeBackground: false,
14+
};
15+
16+
export function show(): void {
17+
void modal.show({
18+
beforeAnimation: async (m) => {
19+
(m.querySelector("input[type='checkbox']") as HTMLInputElement).checked =
20+
false;
21+
state.includeBackground = false;
22+
},
23+
});
24+
}
25+
26+
async function generateUrl(): Promise<string> {
27+
const newTheme: {
28+
c: string[]; //colors
29+
i?: string; //image
30+
s?: string; //size
31+
f?: object; //filter
32+
} = {
33+
c: ThemeController.colorVars.map(
34+
(color) =>
35+
$(
36+
`.pageSettings .customTheme .customThemeEdit #${color}[type='color']`
37+
).attr("value") as string
38+
),
39+
};
40+
41+
if (state.includeBackground) {
42+
newTheme.i = Config.customBackground;
43+
newTheme.s = Config.customBackgroundSize;
44+
newTheme.f = Config.customBackgroundFilter;
45+
}
46+
47+
return (
48+
window.location.origin + "?customTheme=" + btoa(JSON.stringify(newTheme))
49+
);
50+
}
51+
52+
async function copy(): Promise<void> {
53+
const url = await generateUrl();
54+
55+
navigator.clipboard.writeText(url).then(
56+
function () {
57+
Notifications.add("URL Copied to clipboard", 1);
58+
void modal.hide();
59+
},
60+
function (e) {
61+
Notifications.add(
62+
createErrorMessage(e, "Failed to copy to clipboard"),
63+
-1
64+
);
65+
Notifications.add(
66+
"Looks like we couldn't copy the link straight to your clipboard. Please copy it manually.",
67+
0,
68+
{
69+
duration: 5,
70+
}
71+
);
72+
void modal.hide();
73+
CustomThemePopup.show(url);
74+
}
75+
);
76+
}
77+
78+
const modal = new AnimatedModal("shareCustomThemeModal", "popups", undefined, {
79+
setup: (modal): void => {
80+
modal.querySelector("button")?.addEventListener("click", copy);
81+
modal
82+
.querySelector("input[type='checkbox']")
83+
?.addEventListener("change", (e) => {
84+
state.includeBackground = (e.target as HTMLInputElement).checked;
85+
});
86+
},
87+
});

frontend/src/ts/popups/share-custom-theme-popup.ts

Lines changed: 0 additions & 117 deletions
This file was deleted.

frontend/src/ts/settings/theme-picker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as Misc from "../utils/misc";
44
import * as Notifications from "../elements/notifications";
55
import * as ThemeColors from "../elements/theme-colors";
66
import * as ChartController from "../controllers/chart-controller";
7-
import * as ShareCustomThemePopup from "../popups/share-custom-theme-popup";
7+
import * as ShareCustomThemePopup from "../modals/share-custom-theme";
88
import * as Loader from "../elements/loader";
99
import * as DB from "../db";
1010
import * as ConfigEvent from "../observables/config-event";

0 commit comments

Comments
 (0)