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: dark/light mode toggle in settings #150

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

JiyaGupta-cs
Copy link

@JiyaGupta-cs JiyaGupta-cs commented Feb 27, 2025

Description

This PR introduces a dark/light mode toggle feature, allowing users to switch between themes dynamically. The selected theme remains persistent even after refreshing the website.

Key Features:

  • Implemented dark/light mode toggle using DaisyUI.
  • Users can switch between light mode and dark mode theme.
  • Theme preference is stored persistently using cookie.
  • Ensured smooth UI updates on theme change.

Additional Context

The logo also needs to be changed for the light mode and the theme change option need to be styled as the dropdown like other similar fields.
If you prefer to change the light mode colors, it can be selected from any theme of DaisyUI: https://daisyui.com/docs/themes. Let me know if you'd like a specific theme to be set as the default.

Looking forward to your feedback!

Screenshot

Screencast.from.2025-02-28.04-38-36.webm

Fixes bug(s)

Closes the dark/light mode feature on #2

Copy link

vercel bot commented Feb 27, 2025

@JiyaGupta-cs is attempting to deploy a commit to the openfoodfacts Team on Vercel.

A member of the Team first needs to authorize it.

@VaiTon
Copy link
Member

VaiTon commented Feb 28, 2025

Hello @JiyaGupta-cs and thanks for the PR. I don't think storing the theme in cookies is a pretty way to do so. We already have a mechanism to store user preferences in local storage (src/lib/settings.ts). Please use that to store the user theme.

If you'd like a reference, look at this code: https://github.com/cartabinaria/dynamik/blob/main/src/lib/settings.ts

Copy link

vercel bot commented Mar 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
openfoodfacts-explorer ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 5, 2025 11:18am

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This PR introduces a dark/light mode toggle feature that dynamically updates the website's theme and persists the user’s choice using cookies.

  • Added a SvelteKit hook in src/hooks.server.ts to adjust the HTML attribute according to the theme.
  • Introduced a page action in src/routes/+page.server.ts to set the theme cookie based on URL parameters.

Reviewed Changes

File Description
src/hooks.server.ts Added logic to update the HTML theme attribute based on cookie/URL.
src/routes/+page.server.ts Created an action to set a persistent theme preference cookie.

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

@VaiTon
Copy link
Member

VaiTon commented Mar 4, 2025

Hey @JiyaGupta-cs , any update on this one?

@JiyaGupta-cs
Copy link
Author

JiyaGupta-cs commented Mar 4, 2025

@VaiTon I am working on this issue.
It was working fine before:
875456b6b9621afaed0f000b91dc92c6a5a2020b
I am looking into it

@JiyaGupta-cs
Copy link
Author

Screencast.from.2025-03-05.03-50-42.webm

I have updated the code taking inferences from the reference provided. Thanks !

theme: Theme;
}

const settings = persisted<Settings>('settings', {
Copy link
Member

Choose a reason for hiding this comment

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

Why another persisted? Can't we use the preferences on line 3?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, you are right. I have corrected that

@VaiTon
Copy link
Member

VaiTon commented Mar 5, 2025

Users can switch between various light mode themes provided by DaisyUI.

Mh, but with this PR we're just making changing theme between light and dark mode manual. I don't see any new theme added.

Now the question is, why should we do something like this?

@JiyaGupta-cs
Copy link
Author

JiyaGupta-cs commented Mar 5, 2025

Users can switch between various light mode themes provided by DaisyUI.

Mh, but with this PR we're just making changing theme between light and dark mode manual. I don't see any new theme added.

Now the question is, why should we do something like this?

Yes, you are right we are just switching between the dark and light mode. I have updated the PR description now

@VaiTon
Copy link
Member

VaiTon commented Mar 6, 2025

Yes, you are right we are just switching between the dark and light mode. I have updated the PR description now

The PR description is not the problem. The question is: why should the user NOT use the automatic color switching and instead fix the theme? At least we should provide a way to use the auto color switching.

@JiyaGupta-cs
Copy link
Author

JiyaGupta-cs commented Mar 7, 2025

@VaiTon
using prefersdark: true; uses auto color switching and changes it according to the user's browser preference but using that the user is unable to switch between the light and dark mode manually.
I have tried to imitate the same behaviour as of https://github.com/cartabinaria/dynamik
If needed, can change it to the auto color switching but then the user will not be able to switch the mode manually.

@JiyaGupta-cs
Copy link
Author

@VaiTon could you please guide more about it

@VaiTon
Copy link
Member

VaiTon commented Mar 8, 2025

@JiyaGupta-cs if we want to make the user able to select a light / dark mode and keep it, we should at least add a "system" / "auto" mode that follows OS defaults

@JiyaGupta-cs
Copy link
Author

JiyaGupta-cs commented Mar 8, 2025

@JiyaGupta-cs if we want to make the user able to select a light / dark mode and keep it, we should at least add a "system" / "auto" mode that follows OS defaults

The problem that is occuring is ff --prefersdark is used as mentioned in https://daisyui.com/docs/themes/ to detect the current theme of the system then the user is unable to switch between the modes
Then there will be no use of giving three options of System, Light, Dark in the menu if the user is unable to select

@VaiTon
Copy link
Member

VaiTon commented Mar 8, 2025

Then I'm sorry @JiyaGupta-cs but I don't think that is worth the change. Not a lot of users would want to fix a theme anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

Add a dark mode / light mode toggle in settings
2 participants