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: add dark theme support #430

Merged
merged 4 commits into from
Jan 25, 2023
Merged

feat: add dark theme support #430

merged 4 commits into from
Jan 25, 2023

Conversation

ecmchow
Copy link
Contributor

@ecmchow ecmchow commented Jan 14, 2023

Added dark theme support

  1. Moved common colors to CSS variables to allow theme switching
  2. Added theme toggle button in sidebar
  3. Use prefers-color-scheme as default value
  4. User selected/toggled theme will be persisted via localStorage

Screenshots:
dark-mode-screenshot_home
dark-mode-screenshot_repo
dark-mode-screenshot_log
dark-mode-screenshot_graph-log
dark-mode-screenshot_graph
dark-mode-screenshot_modal
dark-mode-screenshot_search
dark-mode-screenshot_settings
dark-mode-screenshot_mobile

@d1wilko
Copy link
Contributor

d1wilko commented Jan 17, 2023

@ecmchow this is really cool

but it's a big change, so I will need to take some time to go through it

Thanks for the work on this

@d1wilko
Copy link
Contributor

d1wilko commented Jan 17, 2023

hey @ecmchow just a heads up that I have asked one of our designers to look at your PR to make sure they are happy

they might ask for some color changes but we will address that if/when it happens :)

@ecmchow
Copy link
Contributor Author

ecmchow commented Jan 18, 2023

@d1wilko Thanks for letting me know. I've kind of guessed the purpose of these color and hand-picked the dark theme color by reducing lightness only, so it might not fit the design system/standard. So feel free to adjust the color and variable names to your needs :)

Copy link
Contributor

@d1wilko d1wilko left a comment

Choose a reason for hiding this comment

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

hey @ecmchow great work on this - some suggestions to improve it a little :)

// Gray
$color-gray-200: #d9dae5;
$color-gray-300: #b0b1c3;
$color-gray-400: #9293ab;
$color-gray-400: #9293ab;;
Copy link
Contributor

Choose a reason for hiding this comment

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

does this need 2 semi ciolons?

@@ -1,11 +1,14 @@
import classNames from 'classnames/bind';
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

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

is useEffect needed here?

Comment on lines 1 to 36
import { useEffect, useCallback, useState } from 'react';

import useLocalStorage from './use-local-storage';

const useTheme = () => {
const [storedTheme, setStoredTheme] = useLocalStorage('selected_theme', 'system');
const [selectedTheme, setSelectedTheme] = useState(storedTheme);

const isSystemThemeDark = () => window.matchMedia('(prefers-color-scheme: dark)').matches === true;

useEffect(() => {
if (
(selectedTheme === 'system' && isSystemThemeDark()) || (selectedTheme === 'dark')
) {
document.body.classList.add('dark');
} else {
document.body.classList.remove('dark');
}
}, [selectedTheme]);

const toggleTheme = useCallback(() => {
let newTheme = 'system';
if (selectedTheme === 'system') {
newTheme = isSystemThemeDark() ? 'light' : 'dark';
} else {
newTheme = selectedTheme === 'dark' ? 'light' : 'dark';
}

setStoredTheme(newTheme);
setSelectedTheme(newTheme);
}, [selectedTheme, setStoredTheme, setSelectedTheme]);

return { selectedTheme, toggleTheme };
};

export default useTheme;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can simplify this a bit - there no need for state AND localStorage - localStorage should suffice

something like this could work

import { useEffect } from 'react';

import useLocalStorage from './use-local-storage';

const DARK = 'dark';
const LIGHT = 'light';

const useTheme = () => {
  const isSystemThemeDark = () => window.matchMedia('(prefers-color-scheme: dark)').matches === true;

  const [storedTheme, setStoredTheme] = useLocalStorage('selected_theme', isSystemThemeDark ? DARK : LIGHT);

  useEffect(() => {
    if (
      (storedTheme === DARK)
    ) {
      document.body.classList.add(DARK);
    } else {
      document.body.classList.remove(DARK);
    }
  }, [storedTheme]);

  const toggleTheme = () => {
    setStoredTheme(storedTheme === DARK ? LIGHT : DARK);
  };

  return { storedTheme, toggleTheme };
};

export default useTheme;

@@ -58,6 +62,18 @@ export default function Sidebar(props) {

const bottomPart = (
<div className={cx('bottom')}>
<Button
className={cx('sidebar-item', 'theme-btn')}
theme="plain"
Copy link
Contributor

@d1wilko d1wilko Jan 18, 2023

Choose a reason for hiding this comment

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

I'm not sure what the theme is?

label="Search"
onClick={toggleTheme}
>
{selectedTheme === 'dark' ? (
Copy link
Contributor

Choose a reason for hiding this comment

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

if you change the hook - this will need to change :)

@ecmchow
Copy link
Contributor Author

ecmchow commented Jan 21, 2023

@d1wilko Thanks for the suggestions. I've simplified the theme hook and cleanup the style a bit, please have a look and let me know if there is more comments :)

@ecmchow ecmchow requested a review from d1wilko January 21, 2023 13:26
Copy link
Contributor

@d1wilko d1wilko left a comment

Choose a reason for hiding this comment

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

looks great thank you

I need to make some changes to the colors as per our UX guys but I will do that and add you as a reviewer before we do a proper release

@d1wilko d1wilko merged commit 62843e8 into drone:main Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants