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

Added a Togglable Dark Mode and Refactored Existing CSS Color Variables #535

Closed
wants to merge 25 commits into from

Conversation

trevorkw7
Copy link
Contributor

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

image
image
image
image

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 and theme-color-primary-6 having the darkest.

Feedback

To test, pull the code and run yarn install and yarn start like normal. Please let me know if you have any feedback or suggestions by replying to this request :0

@steets250 steets250 added the PR: Needs Review This PR needs review label Feb 1, 2022
@steets250 steets250 self-requested a review February 1, 2022 02:40
Copy link
Member

@steets250 steets250 left a 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.

  1. 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 sure yarn install is run to keep things synced correctly.
  2. 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.
  3. 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.
  4. 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.

src/admin/components/AdminPage/style.less Outdated Show resolved Hide resolved
src/assets/icons/moon.svg Outdated Show resolved Hide resolved
src/assets/icons/sun.svg Outdated Show resolved Hide resolved
src/index.tsx Outdated Show resolved Hide resolved
src/layout/components/Header/custom.less Outdated Show resolved Hide resolved
src/leaderboard/components/LeaderPage/style.less Outdated Show resolved Hide resolved
src/profile/components/ProfilePage/style.less Outdated Show resolved Hide resolved
src/styles/ThemeContext/themeContext.tsx Outdated Show resolved Hide resolved
@steets250 steets250 added PR: Reviewed w/ Comments This PR has been reviewed and removed PR: Needs Review This PR needs review labels Feb 1, 2022
Copy link
Contributor

@duanwilliam duanwilliam left a 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

src/layout/components/DiscordPage/index.tsx Outdated Show resolved Hide resolved
src/styles/ThemeContext/themeProvider.tsx Outdated Show resolved Hide resolved
Comment on lines 7 to 33
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]);
Copy link
Contributor

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

Copy link
Member

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

src/styles/ThemeContext/themeProvider.tsx Outdated Show resolved Hide resolved
@@ -1,5 +1,7 @@
@import "./vars.less";
Copy link
Contributor

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

src/admin/components/CreateEventPage/style.less Outdated Show resolved Hide resolved
public/index.html Outdated Show resolved Hide resolved
src/admin/components/AddAttendancePage/style.less Outdated Show resolved Hide resolved
src/admin/components/AdminPage/style.less Outdated Show resolved Hide resolved
@trevorkw7
Copy link
Contributor Author

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 😉

@trevorkw7 trevorkw7 requested a review from steets250 February 22, 2022 10:03
@trevorkw7 trevorkw7 changed the title Added a Togglable Dark Mode and Refactored CSS Color Variables Added a Togglable Dark Mode and Refactored Existing CSS Color Variables Feb 24, 2022
@@ -50,6 +136,7 @@ video {
border: 0;
margin: 0;
padding: 0;
transition: background 0.3s ease;
Copy link
Member

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

Screen Shot 2022-02-27 at 12 23 22 AM

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

Copy link
Member

Choose a reason for hiding this comment

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

Screen Shot 2022-02-27 at 12 41 29 AM
the tiny arrow on the dropdown is also impossible to see in dark mode. would look better in white but it's an ant design component so this might not be possible to fix

Copy link
Member

@StormFireFox1 StormFireFox1 left a 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!

@StormFireFox1
Copy link
Member

@trevorkw7 just a heads up; @paulpan05 decided to work on the last review comments, so no need to worry about that.

@StormFireFox1
Copy link
Member

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Reviewed w/ Comments This PR has been reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants