-
Notifications
You must be signed in to change notification settings - Fork 14
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
Added a Togglable Dark Mode and Refactored Existing CSS Color Variables #535
Conversation
Dashboard, Profile, About, Admin, Discord Pages Have Dark Mode Ready
…embership-portal-ui into colors-and-dark-mode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, the dark theme looks great! Couple comments / issues but overall looks super solid. Thanks for taking the time to clean up all the color styling variables we had.
- For the portal, we use yarn as our package manager, so we don't want to have a
package-lock.json
file in the repo. Let's remove this from the PR but make sureyarn install
is run to keep things synced correctly. - The code isn't currently passing the linting tests. Before pushing, it's generally a good idea to run the
yarn run lint:fix
command to automatically clean the code and notify of any issues. If you use Visual Studio Code install the eslint extension, then it runs the linter upon file save. - For dark mode on https://deploy-preview-535--members-nightly.netlify.app/register, the icons don't switch to light colors. Let me know if you have issues fixing this.
- For dark mode on https://deploy-preview-535--members-nightly.netlify.app/about, the Facebook logo doesn't display correctly. I'll DM you an updated svg, but I want to make sure to have a note of this as a to-do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some pretty nice work, still rough around the edges though and doesn't "properly" address dark mode in react per se
not to shill or anything but https://github.com/acmeet/acmeet/tree/main/client/src/context/Theme (honestly id refactor it more now, eg it has an unnecessary usememo for initial theme + handles localstorage stuff on its own instead of custom hook) can be used as a general reference. do note that that uses [data-theme=dark]
bound to the html element as opposed to a class or something on body though, and that InitialThemeScript
goes in the head, but since portal is CRA the direct html can just be inserted in the index.html template
const [theme, setTheme] = useState("light"); | ||
|
||
const toggleTheme = () => { | ||
// change body className | ||
let currentTheme = document.body.className; | ||
let newTheme = currentTheme === 'light-theme' ? 'dark-theme' : 'light-theme'; | ||
document.body.className! = newTheme | ||
// update state | ||
setTheme(theme === 'light' ? 'dark' : 'light'); | ||
}; | ||
|
||
useEffect(() => { | ||
// sync theme state with localStorage on first render | ||
const localTheme = localStorage.getItem('theme'); | ||
if (localTheme) { | ||
setTheme(localTheme); | ||
} | ||
// if dark, toggle theme to light on first render | ||
if (localTheme === "dark") { | ||
document.body.className! = 'dark-theme'; | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
// sync localStorage with theme state when it changes | ||
localStorage.setItem('theme', theme); | ||
}, [theme]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can abstract persistent state to a custom hook
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this line is just moved to within toggleTheme
, is there an issue? I don't see how a custom hook is necessary
@@ -1,5 +1,7 @@ | |||
@import "./vars.less"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be better to define the css variables in a separate file
also, would prob be better to have e.g. light mode variables be default, then override with dark mode selector
Thanks for all the feedback guys, I'll take a look at each and every one of your comments and try incorporating them into the branch. This is my first time really contributing to a React project so thanks for being patient with me. I'm learning a lot of valuable information about how to write cleaner and higher spec code which is really exciting 😉 |
@@ -50,6 +136,7 @@ video { | |||
border: 0; | |||
margin: 0; | |||
padding: 0; | |||
transition: background 0.3s ease; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking super clean - nice work!
one small nitpick: if you toggle dark mode on /register
page, the major and graduation year's dropdown fields don't fade colors in sync with everything else on the page.
this is probably caused by multiple transition css properties on a single element taking incorrect precedence in the stylesheets (some of ant design's dropdown css is being used instead of yours). here's a really good example of the simplest case of this happening https://stackoverflow.com/a/34757242
If you look at the computed style on the right side, we can see that the line transition: background 0.3s ease;
is being ignored due to another transition attribute above it.
I was playing around and found that one way to fix this would be to force the precedence with transition: background 0.3s ease !important;
. This is a pretty hacky solution though so feel free to try other cleaner approaches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Thanks for all the hard work, and also thanks to everyone who reviewed this PR!
I'm glad this has set the ball for our CSS refactor as well, so props to everyone's inputs!
@trevorkw7 just a heads up; @paulpan05 decided to work on the last review comments, so no need to worry about that. |
@trevorkw7, thank you so much for your hard work! We've been doing a lot of work for Portal to revitalize it for some upcoming changes, and we figured we could continue on your work and get this feature pushed in a little bit faster. I'll close this PR, but your changes got merged in with #555. We'll be announcing Dark Mode in the main ACM Discord soon, we'll make sure to credit you as well! |
Feature description
A togglable dark mode switch has been added to the header of the website to make night viewing of the portal easier on the eyes. In addition, colors used across the entire project have now been refactored into CSS color variables defined in vars.less and reset.less, making it easier to tweak colors at a universal level.
Screenshots
Behavior Changes
The original LESS CSS variables that existed in vars.less have now been refactored as Native CSS variables that are defined in reset.less to be accessible anywhere within the body of the website. There are two sets of these variables with the same name but with different color values, one set for when the body class name is "light-theme", and the other set for when the body class name is "dark-theme". Despite this, vars.less continues to exist to define the hex codes for the colors themselves and are referenced in reset.less.
The new color system is weighted, with
theme-color-primary-1
having the lightest shade of the primary color andtheme-color-primary-6
having the darkest.Feedback
To test, pull the code and run
yarn install
andyarn start
like normal. Please let me know if you have any feedback or suggestions by replying to this request :0