Skip to content

Commit c0396ae

Browse files
authored
chore: add button to settings popup for dark mode (#7136)
1 parent 42e6def commit c0396ae

5 files changed

Lines changed: 95 additions & 3 deletions

File tree

src/static/js/pad.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,12 @@ const pad = {
480480
setTimeout(() => { checkChatAndUsersVisibility(mobileMatch); }, 0); // check now after load
481481

482482
$('#editorcontainer').addClass('initialized');
483-
if (window.location.hash.toLowerCase() !== '#skinvariantsbuilder' && window.clientVars.enableDarkMode && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
483+
484+
if (window.clientVars.enableDarkMode) {
485+
$('#theme-switcher').attr('style', 'display: flex;');
486+
}
487+
488+
if (window.location.hash.toLowerCase() !== '#skinvariantsbuilder' && window.clientVars.enableDarkMode && (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) && !skinVariants.isWhiteModeEnabledInLocalStorage()) {
484489
skinVariants.updateSkinVariantsClasses(['super-dark-editor', 'dark-background', 'super-dark-toolbar']);
485490
}
486491

src/static/js/pad_editor.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import padutils,{Cookies} from "./pad_utils";
2626
const padcookie = require('./pad_cookie').padcookie;
2727
const Ace2Editor = require('./ace').Ace2Editor;
2828
import html10n from '../js/vendors/html10n'
29+
const skinVariants = require('./skin_variants');
2930

3031
const padeditor = (() => {
3132
let pad = undefined;
@@ -86,11 +87,22 @@ const padeditor = (() => {
8687
$('#delete-pad').on('click', () => {
8788
if (window.confirm(html10n.get('pad.delete.confirm'))) {
8889
pad.collabClient.sendMessage({type: 'PAD_DELETE', data:{padId: pad.getPadId()}});
89-
// redirect to home page after deletion
90+
// redirect to home page after deletion
9091
window.location.href = '/';
9192
}
9293
})
9394

95+
// theme switch
96+
$('#theme-switcher').on('click',()=>{
97+
if (skinVariants.isDarkMode()) {
98+
skinVariants.setDarkModeInLocalStorage(false);
99+
skinVariants.updateSkinVariantsClasses(['super-light-toolbar super-light-editor light-background']);
100+
} else {
101+
skinVariants.setDarkModeInLocalStorage(true);
102+
skinVariants.updateSkinVariantsClasses(['super-dark-editor', 'dark-background', 'super-dark-toolbar']);
103+
}
104+
})
105+
94106
// Language
95107
html10n.bind('localized', () => {
96108
$('#languagemenu').val(html10n.getLanguage());

src/static/js/skin_variants.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ const updateSkinVariantsClasses = (newClasses) => {
2323
domsToUpdate.forEach((el) => { el.addClass(newClasses.join(' ')); });
2424
};
2525

26+
27+
const isDarkMode = ()=>{
28+
return $('html').hasClass('super-dark-editor')
29+
}
30+
31+
32+
const setDarkModeInLocalStorage = (isDark)=>{
33+
localStorage.setItem('ep_darkMode', isDark?'true':'false');
34+
}
35+
36+
const isDarkModeEnabledInLocalStorage = ()=>{
37+
return localStorage.getItem('ep_darkMode')==='true';
38+
}
39+
40+
const isWhiteModeEnabledInLocalStorage = ()=>{
41+
return localStorage.getItem('ep_darkMode')==='false';
42+
}
43+
2644
// Specific hash to display the skin variants builder popup
2745
if (window.location.hash.toLowerCase() === '#skinvariantsbuilder') {
2846
$('#skin-variants').addClass('popup-show');
@@ -60,4 +78,8 @@ if (window.location.hash.toLowerCase() === '#skinvariantsbuilder') {
6078
updateSkinVariantsClasses(getNewClasses());
6179
}
6280

81+
exports.isDarkMode = isDarkMode;
82+
exports.setDarkModeInLocalStorage = setDarkModeInLocalStorage
83+
exports.isWhiteModeEnabledInLocalStorage = isWhiteModeEnabledInLocalStorage
84+
exports.isDarkModeEnabledInLocalStorage = isDarkModeEnabledInLocalStorage
6385
exports.updateSkinVariantsClasses = updateSkinVariantsClasses;

src/static/skins/colibris/src/components/popup.css

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,43 @@
8585
#delete-pad {
8686
background-color: #ff7b72;
8787
}
88+
89+
#theme-switcher div {
90+
position: relative;
91+
width: 30px;
92+
background-color: white;
93+
height: 10px;
94+
border-radius: 5px;
95+
align-self: center;
96+
}
97+
98+
#theme-switcher {
99+
display: flex;
100+
margin-top: 20px;
101+
flex-direction: row;
102+
}
103+
104+
#theme-switcher div span {
105+
width: 15px;
106+
display: block;
107+
height: 15px;
108+
border-radius: 20px;
109+
position: absolute;
110+
top: -2px;
111+
background-color: white;
112+
transition: background-color 0.25s;
113+
}
114+
115+
html.super-light-editor #theme-switcher div {
116+
background-color: #ccc;
117+
}
118+
119+
html.super-light-editor #theme-switcher div span {
120+
left: 0;
121+
background-color: var(--primary-color);;
122+
}
123+
124+
html.super-dark-editor #theme-switcher div span {
125+
right: 0;
126+
background-color: var(--primary-color);;
127+
}

src/templates/pad.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,20 @@ <h2 data-l10n-id="pad.settings.myView"></h2>
164164
<% e.end_block(); %>
165165
</div>
166166
<button data-l10n-id="pad.settings.deletePad" id="delete-pad">Delete pad</button>
167-
<h2 data-l10n-id="pad.settings.about">About</h2>
167+
<div id="theme-switcher" style="display: none;">
168+
<svg width="2rem" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
169+
<path stroke-linecap="round" stroke-linejoin="round" d="M12 3v2.25m6.364.386-1.591 1.591M21 12h-2.25m-.386 6.364-1.591-1.591M12 18.75V21m-4.773-4.227-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0Z" />
170+
</svg>
171+
<div>
172+
<span aria-label="theme-switcher-knob"></span>
173+
</div>
174+
<svg width="2rem" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
175+
<path stroke-linecap="round" stroke-linejoin="round" d="M21.752 15.002A9.72 9.72 0 0 1 18 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 0 0 3 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 0 0 9.002-5.998Z" />
176+
</svg>
177+
</div>
178+
179+
180+
<h2 data-l10n-id="pad.settings.about">About</h2>
168181
<span data-l10n-id="pad.settings.poweredBy">Powered by</span>
169182
<a href="https://etherpad.org" target="_blank" referrerpolicy="no-referrer" rel="noopener">Etherpad</a>
170183
<% if (settings.exposeVersion) { %>(commit <%= settings.gitVersion %>)<% } %> </div></div>

0 commit comments

Comments
 (0)