Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme Toggling Feature #2972

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b85d15e
Intial theme toggle within settings page
ayanaar Oct 3, 2023
9605154
Add theme selector mockups (#2971)
mathjazz Oct 3, 2023
09a018a
Intial theme toggle within settings page
ayanaar Oct 3, 2023
b169c96
Make suggested changes and add updated UI
ayanaar Oct 3, 2023
b309221
Resolving conflicts
ayanaar Oct 3, 2023
776df7f
Deleting old migration
ayanaar Oct 3, 2023
b308185
Update UserProfile model
ayanaar Oct 3, 2023
4793129
Satisfy linters
ayanaar Oct 3, 2023
e7f93df
Add requested changes
ayanaar Oct 3, 2023
0ff60fd
Satisfy linters
ayanaar Oct 3, 2023
b8865f8
Remove extra space
ayanaar Oct 3, 2023
771f4d1
Add link to dark-theme.css in base.html
ayanaar Oct 3, 2023
4f14364
Add updates
ayanaar Oct 10, 2023
2982ad1
Remove extra theme-switcher.js file
ayanaar Oct 10, 2023
9340418
Update theme-switcher.js and translate.html
ayanaar Oct 12, 2023
c99db80
Implemented dynamic theme switching based on user preferences and sys…
ayanaar Oct 12, 2023
b7bbe71
Fix spacing in settings.html
ayanaar Oct 12, 2023
5a99d21
Fixing header and inconsistencies
ayanaar Oct 16, 2023
90b707c
Add system theme switching in translate view
ayanaar Oct 16, 2023
34f18a6
Remove console.log statements
ayanaar Oct 16, 2023
8b5a713
Satisfy linters
ayanaar Oct 16, 2023
5e8960e
Satisfy prettier
ayanaar Oct 16, 2023
d8b3a81
Modify templates and update Theme.tsx logic
ayanaar Oct 17, 2023
05b8510
Fix insights page
ayanaar Oct 17, 2023
832b621
Fix profile and insights page, refactor templates, and modify Theme.tsx
ayanaar Oct 17, 2023
6f370dd
Fix migrations and inconsistencies
ayanaar Oct 18, 2023
9737f59
Update model to fix pytests
ayanaar Oct 18, 2023
afcd620
Remove theme field from forms
ayanaar Oct 18, 2023
b4e63cc
Fix python linter
ayanaar Oct 18, 2023
f590ee1
Remove spec files
ayanaar Oct 19, 2023
ba8a87b
Update light theme file
ayanaar Oct 19, 2023
2d73318
Add light theme spec
ayanaar Oct 19, 2023
d99376e
Update light theme spec
ayanaar Oct 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pontoon/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class Meta:
"chat",
"github",
"bugzilla",
"theme",
)


Expand Down
22 changes: 22 additions & 0 deletions pontoon/base/migrations/0047_userprofile_themes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.15 on 2023-10-02 03:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("base", "0046_projectslughistory"),
]

operations = [
migrations.AddField(
model_name="userprofile",
name="themes",
field=models.CharField(
choices=[("Dark", "Dark"), ("Light", "Light"), ("System", "System")],
default="Dark",
max_length=20,
),
),
]
18 changes: 18 additions & 0 deletions pontoon/base/migrations/0048_rename_themes_userprofile_theme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.15 on 2023-10-02 19:34

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("base", "0047_userprofile_themes"),
]

operations = [
migrations.RenameField(
model_name="userprofile",
old_name="themes",
new_name="theme",
),
]
mathjazz marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions pontoon/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,11 +1569,18 @@ class UserProfile(models.Model):
User, models.CASCADE, related_name="profile", primary_key=True
)

# Themes
class Themes(models.TextChoices):
DARK = "Dark", "Dark"
LIGHT = "Light", "Light"
SYSTEM = "System", "System"
mathjazz marked this conversation as resolved.
Show resolved Hide resolved

# Personal information
username = models.SlugField(unique=True, blank=True, null=True)
contact_email = models.EmailField("Contact email address", blank=True, null=True)
contact_email_verified = models.BooleanField(default=False)
bio = models.TextField(max_length=160, blank=True, null=True)
theme = models.CharField(choices=Themes.choices, max_length=20, default=Themes.DARK)
mathjazz marked this conversation as resolved.
Show resolved Hide resolved

# External accounts
chat = models.CharField("Chat username", max_length=255, blank=True, null=True)
Expand Down
2 changes: 1 addition & 1 deletion pontoon/base/static/css/dark-theme.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Dark Theme Variables */
:root {
.dark-theme {
--black-1: #1c1e21;
--black-2: #000000;
--black-3: #272a2f;
Expand Down
12 changes: 12 additions & 0 deletions pontoon/base/static/css/light-theme.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* Light Theme Variables */
.light-theme {
--dark-grey-2: #fbfbfb;
--dark-grey-1: #eee;
--light-grey-7: #aaaaaa;
--grey-3: #eee;
--light-grey-1: #e8e8e8;
--white-1: #222;
--white-3: #222;
--light-grey-7: #888;
--light-grey-2: #888888;
}
2 changes: 1 addition & 1 deletion pontoon/base/static/css/toggle.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
border-bottom-right-radius: 0;
}

.toggle-button button:nth-child(2) {
.toggle-button button:last-child {
border-left: none;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
Expand Down
83 changes: 83 additions & 0 deletions pontoon/base/static/js/theme-switcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
document.addEventListener('DOMContentLoaded', function () {
const themeHiddenInput = document.querySelector('[name="theme"]');
const darkButton = document.querySelector('.dark');
const lightButton = document.querySelector('.light');
const systemButton = document.querySelector('.system');

if (!themeHiddenInput || !darkButton || !lightButton || !systemButton) {
console.error('Theme elements not found!');
return;
}

darkButton.addEventListener('click', function () {
applyTheme('Dark');
});

lightButton.addEventListener('click', function () {
applyTheme('Light');
});

systemButton.addEventListener('click', function () {
applyTheme('System');
});
});

function applyTheme(theme) {
const bodyElement = document.body;
const themeHiddenInput = document.querySelector('[name="theme"]');

// Remove all theme classes first
bodyElement.classList.remove('dark-theme', 'light-theme');

if (theme === 'Dark') {
bodyElement.classList.add('dark-theme');
} else if (theme === 'Light') {
bodyElement.classList.add('light-theme');
} else if (theme === 'System') {
if (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
bodyElement.classList.add('dark-theme');
} else {
bodyElement.classList.add('light-theme');
}
}

// Update hidden input value
themeHiddenInput.value = theme;
}

// document.addEventListener('DOMContentLoaded', function() {
// const themeSelector = document.querySelector('[name="themes"]');

// if (!themeSelector) {
// console.error('Theme selector not found!');
// return;
// }

// themeSelector.addEventListener('change', function() {
// applyTheme(this.value);
// });
// });

// function applyTheme(theme) {
// alert("Hello")
// const bodyElement = document.body;

// if (theme === 'Dark') {
// bodyElement.classList.add('dark-theme');
// bodyElement.classList.remove('light-theme');
// } else if (theme === 'Light') {
// bodyElement.classList.add('light-theme');
// bodyElement.classList.remove('dark-theme');
// } else if (theme === 'System') {
// if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
// bodyElement.classList.add('dark-theme');
// bodyElement.classList.remove('light-theme');
// } else {
// bodyElement.classList.add('light-theme');
// bodyElement.classList.remove('dark-theme');
// }
// }
// }
9 changes: 7 additions & 2 deletions pontoon/base/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@
</head>

<body
class="{% block class %}{% endblock %}"
class="{% block class %}{% endblock %} {% if request.user.is_authenticated %}
{% if request.user.profile.theme == 'Light' %}light-theme{% elif request.user.profile.theme == 'Dark' %}dark-theme{% else %}system-theme{% endif %}
{% else %}
system-theme
{% endif %}"
mathjazz marked this conversation as resolved.
Show resolved Hide resolved
{% if csrf_token %}data-csrf="{{ csrf_token }}"{% endif %}
>
>
mathjazz marked this conversation as resolved.
Show resolved Hide resolved

mathjazz marked this conversation as resolved.
Show resolved Hide resolved
{% block content %}

{% include "addon_promotion.html" %}
Expand Down
5 changes: 4 additions & 1 deletion pontoon/base/templates/django/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
<meta name="description" content="Mozilla’s Localization Platform">
<meta name="author" content="Mozilla">
<meta name="color-scheme" content="only light">
<link rel="stylesheet" href="{% static 'css/dark-theme.css' %}" title="" type="text/css" />
<link rel="stylesheet" href="{% static 'css/light-theme.css' %}" title="" type="text/css"/>
mathjazz marked this conversation as resolved.
Show resolved Hide resolved
<link rel="stylesheet" href="{% static 'css/fontawesome-all.css' %}" title="" type="text/css" />
<link rel="stylesheet" href="{% static 'css/nprogress.css' %}" title="" type="text/css" />
<link rel="stylesheet" href="{% static 'css/fonts.css' %}" title="" type="text/css" />
<link rel="stylesheet" href="{% static 'css/boilerplate.css' %}" title="" type="text/css" />
<link rel="stylesheet" href="{% static 'css/style.css' %}" title="" type="text/css" />
<link rel="stylesheet" href="{% static 'css/dark-theme.css' %}" title="" type="text/css" />

mathjazz marked this conversation as resolved.
Show resolved Hide resolved

{% include "tracker.html" %}

Expand Down Expand Up @@ -121,5 +123,6 @@
<script type="text/javascript" charset="utf-8" src="{% static 'js/lib/jquery-3.6.1.js' %}"></script>
<script type="text/javascript" charset="utf-8" src="{% static 'js/lib/nprogress.js' %}"></script>
<script type="text/javascript" charset="utf-8" src="{% static 'js/main.js' %}"></script>
<script type="text/javascript" charset="utf-8" src="{% static 'js/theme-switcher.js' %}"></script>
mathjazz marked this conversation as resolved.
Show resolved Hide resolved
</body>
</html>
20 changes: 20 additions & 0 deletions pontoon/contributors/static/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@
font-style: italic;
}

#main .appearance .field .help {
margin-left: 0;
}

#main .appearance .toggle-button button {
width: 207px;
}

#main .appearance .toggle-button button:first-child {
margin-right: -1px;
}

#main .appearance .toggle-button button:nth-child(2) {
border-radius: 0;
}

#main .appearance .toggle-button button .icon {
margin-right: 10px;
}

#main .field .verify {
color: var(--light-green-1);
font-style: italic;
Expand Down
14 changes: 14 additions & 0 deletions pontoon/contributors/templates/contributors/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ <h3>Personal information</h3>
</div>
</section>

<section class="appearance" >
mathjazz marked this conversation as resolved.
Show resolved Hide resolved
<input type="hidden" name="{{ user_profile_form.theme.name }}" id="{{ user_profile_form.theme.id_for_label }}" value="{{ user_profile_form.theme.value }}">
<h3>Appearance</h3>
<span class="toggle-button">
<button type="button" class="dark active" title="Dark theme"><i class="icon far fa-moon"></i>Dark theme</button>
<button type="button" class="light" title="Light theme"><i class="icon fa fa-sun"></i>Light theme</button>
<button type="button" class="system" title="System theme"><i class="icon fa fa-laptop"></i>Match system</button>
</span>
<div class="field">
<p class="help">Choose if Pontoon’s appearance should be dark, light, or follow your system’s settings</a></p>
mathjazz marked this conversation as resolved.
Show resolved Hide resolved
</div>
</section>

<section>
<h3>External accounts</h3>
<div class="field">
Expand Down Expand Up @@ -160,4 +173,5 @@ <h3><span class="small stress">Preferred locales to get suggestions from</span><

{% block extend_js %}
{% javascript 'settings' %}
{% javascript 'theme-switcher' %}
mathjazz marked this conversation as resolved.
Show resolved Hide resolved
{% endblock %}
5 changes: 5 additions & 0 deletions pontoon/contributors/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ def settings(request):
request.POST,
instance=profile,
)
theme = request.POST.get("theme", None)
if theme:
if theme in ["Light", "Dark", "System"]:
profile.theme = theme
profile.save()

mathjazz marked this conversation as resolved.
Show resolved Hide resolved
if (
locales_form.is_valid()
Expand Down
2 changes: 2 additions & 0 deletions pontoon/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ def _default_from_email():
"base": {
"source_filenames": (
"css/dark-theme.css",
"css/light-theme.css",
"css/fontawesome-all.css",
"css/nprogress.css",
"css/boilerplate.css",
Expand Down Expand Up @@ -515,6 +516,7 @@ def _default_from_email():
"js/lib/jquery.color-2.1.2.js",
"js/lib/nprogress.js",
"js/main.js",
"js/theme-switcher.js",
),
"output_filename": "js/base.min.js",
},
Expand Down