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

feat: toggle button for switching between light and dark theme #97

Merged
merged 2 commits into from
Oct 11, 2024

Conversation

hinakhadim
Copy link
Collaborator

@hinakhadim hinakhadim commented Aug 20, 2024

This PR introduces toggle theme between light and dark on runtime in 5 MFEs (learner-dashboard, learning, account, profile, discussions):

Architecture:

MFEs:

  • The package @edly-io/indigo-brand-openedx@^2.1.0 includes SCSS files supporting light and dark themes.
  • The package @edly-io/indigo-frontend-component-header@^3.1.3 provides an option to show or hide the theme toggle icon based on the presence of MFE_CONFIG['INDIGO_ENABLE_DARK_TOGGLE'].
  • This PR includes the env.config.jsx file that loads the theme in the MFE by reading the cookie value to determine which theme is to be applied.
  • Learner-Dashboard header will not have toggle icon because the MFE does not install a custom header now. More details are mentioned here.

LMS:

  • The header is responsible for setting the theme.
  • A JavaScript file dark-theme.js loads the cookie value when the page is first loaded.
image image
image image

@hinakhadim hinakhadim marked this pull request as draft August 20, 2024 07:13
@hinakhadim hinakhadim requested a review from regisb August 23, 2024 09:55
@hinakhadim hinakhadim marked this pull request as ready for review August 23, 2024 09:55
tutorindigo/plugin.py Outdated Show resolved Hide resolved
@hinakhadim
Copy link
Collaborator Author

Improved the toggle button for better user experience:

LMS:

Light theme:

Screenshot 2024-09-06 at 3 40 03 PM

Dark theme:

Screenshot 2024-09-06 at 3 33 14 PM

MFE:

Light theme:
Screenshot 2024-09-06 at 3 36 52 PM

Dark theme:
Screenshot 2024-09-06 at 3 36 18 PM

@hinakhadim hinakhadim requested review from regisb and removed request for regisb September 13, 2024 04:24
README.rst Outdated Show resolved Hide resolved
tutorindigo/plugin.py Outdated Show resolved Hide resolved
@hinakhadim
Copy link
Collaborator Author

@regisb Could you please go through it again?

README.rst Outdated Show resolved Hide resolved
tutorindigo/plugin.py Outdated Show resolved Hide resolved
tutorindigo/plugin.py Show resolved Hide resolved
tutorindigo/plugin.py Outdated Show resolved Hide resolved
tutorindigo/templates/indigo/env.config.jsx Outdated Show resolved Hide resolved
tutorindigo/templates/indigo/lms/static/js/dark-theme.js Outdated Show resolved Hide resolved
@hinakhadim
Copy link
Collaborator Author

The code implements the following conditions:

LMS:

  • The cookie name is hardcoded as indigo-toggle-dark.
  • The cookie expiration is set to 3 months (90 days).
  • The toggle button is visible only if INDIGO_ENABLE_DARK_TOGGLE is set to True.
  • On page load, if INDIGO_ENABLE_DARK_TOGGLE is True, the theme (light/dark) will be applied to the body. The cookie expiration date is refreshed on each page load.
  • If INDIGO_ENABLE_DARK_TOGGLE is False, the system will always load with the light theme, the toggle button will not be displayed, the theme will not be applied on page load, and the cookie expiration will not be updated.

MFE (Micro-Frontend):

  • The cookie name is hardcoded as indigo-toggle-dark.
  • The theme cookie expiration is set to 90 days.
  • The MFE receives the INDIGO_ENABLE_DARK_TOGGLE value via getConfig().INDIGO_ENABLE_DARK_TOGGLE from the LMS, passed as MFE_CONFIG['INDIGO_ENABLE_DARK_TOGGLE'] = True.
  • If MFE_CONFIG['INDIGO_ENABLE_DARK_TOGGLE'] = True:
    - On page load, the theme (light/dark) is applied to the body, and the cookie expiration is refreshed.
    - The toggle button will be visible in the header.
  • Otherwise:
    - The theme will not be applied on page load, and the cookie expiration will not be updated. The MFE will always start in the light theme.
    - The toggle button will not be visible.

LMS and MFE Integration:

We set MFE_CONFIG['INDIGO_ENABLE_DARK_TOGGLE'] = True to inform the MFEs that the toggle button or theme switching are disabled within our system. And MFEs should only show light theme.


tutor config save --set INDIGO_ENABLE_DARK_THEME=True
tutor config save --set INDIGO_THEME_COOKIE_NAME=null
tutor images build openedx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing left is to update the instructions :)

tutor config save --set INDIGO_ENABLE_DARK_TOGGLE=false
tutor local restart lms

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the instructions:

tutor config save --set INDIGO_ENABLE_DARK_TOGGLE=false
tutor images build openedx             # required after enable/disable toggle
tutor local start -d

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh it's too bad that we have to rebuild the openedx image is there any way to avoid that, such as accessing the setting from the js code?

Copy link
Collaborator Author

@hinakhadim hinakhadim Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are depending on rebuilding of image due to the following two reasons:

  1. JS file
  2. Scss file if condition to show/hide toggle. [This can be handled in js file too.]

I'm exploring it. So far, I'm unable to find a solution. Just thinking, if we can somehow pass variable from HTML/Mako to js . something like this: but this isn't working.

<%!
from django.conf import settings
%>

<%
  indigoDark = settings.MFE_CONFIG.get('INDIGO_ENABLE_DARK_TOGGLE', true)
%>

<div class="theme-toggle-button mr-4">
    .....HTML.....
</div>

<script type="text/javascript">
    window.darkkTheme = '{{ indigoDark }}'
</script>

OR pass value to a tag and hide it like the below:

<%!
from django.conf import settings
%>

<%
  indigo_dark = settings.MFE_CONFIG.get('INDIGO_ENABLE_DARK_TOGGLE', true)
%>

<div class="theme-toggle-button mr-4">
    .....HTML.....
</div>
<span class='hide indigo-toggle-value'>{indigo_dark}</span>

then get value in js file using. $('indigo-toggle-value');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't spend too much time on this. If you can't find a quick fix, let's just ask people to rebuild the openedx image.

@regisb
Copy link
Contributor

regisb commented Oct 9, 2024

Pretty rad @hinakhadim! Let's fix the instructions and merge in master, such that Redwood can benefit from that feature.


const AddDarkTheme = () => {
const cookies = new Cookies();
const isThemeToggleEnabled = getConfig().INDIGO_ENABLE_DARK_TOGGLE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unable to see the toggle in the MFEs, so I'm guessing this is not working as expected. The LMS config API call is properly returning INDIGO_ENABLE_DARK_TOGGLE: true. Can you please troubleshoot?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, header package is not published yet in which we are using INDIGO_ENABLE_DARK_TOGGLE . Here's the PR for header. I am currently testing the header package and then publish it.
Then, I have to update header package version here too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Publishing the header is done and the version is updated too. I've tested on my system and openedx and mfe image building is successful and working. I will test on sandboxdev and inform the results here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested on sandboxdev and it's working fine. Steps I've taken:

tutor local stop
rm -rf tutor/env
pip install git+https://.......this branch
tutor config save
tutor images build openedx
tutor images build mfe
tutor local start -d


tutor  config save  --set INDIGO_ENABLE_DARK_TOGGLE=false
tutor images build openedx
tutor local start -d


tutor config save --set INDIGO_ENABLE_DARK_TOGGLE=true
tutor images build openedx
tutor local start -d

CHANGELOG.md Outdated
Comment on lines 25 to 26
- [Feature] Introduced theme toggle feature with enable/disable option and runtime switch between light and dark modes. (by @hinakhadim) -->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove --> at the end

{% if INDIGO_ENABLE_DARK_TOGGLE %}
$('body').toggleClass("indigo-dark-theme", theme === 'dark'); // append or remove dark-class based on cookie-value
// update expiry
$.cookie(themeCookie, theme, { domain: window.location.hostname, expires: 90, path: '/' });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this different from operation in above file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The applyThemeOnPage() function is the minimum criterion directly affecting whether the dark theme is applied. Instead of adding to the whole file, we focus specifically on this function.
But there are no significant performance gains in either way because LMS has to compile JS file whenever building an image.


function applyThemeOnPage(){
const theme = $.cookie(themeCookie);
{% if INDIGO_ENABLE_DARK_TOGGLE %}
Copy link

@DawoudSheraz DawoudSheraz Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use django template if the condition here? It is JS file and not being included in django template such that the context variables will work here.

@hinakhadim hinakhadim merged commit e442c78 into overhangio:master Oct 11, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

3 participants