-
Notifications
You must be signed in to change notification settings - Fork 528
fix(Web): Dark Mode isn't friendly to find #7721
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,179 @@ | ||
| import React, { FC, useState } from 'react' | ||
| import React, { FC, useEffect, useLayoutEffect, useRef, useState } from 'react' | ||
| import classNames from 'classnames' | ||
| import ConfigProvider from 'common/providers/ConfigProvider' | ||
| import Setting from './Setting' | ||
| import { getDarkMode, setDarkMode as persistDarkMode } from 'project/darkMode' | ||
| import { calculateListPosition } from 'common/utils/calculateListPosition' | ||
| import useOutsideClick from 'common/useOutsideClick' | ||
| import InlinePillToggle from './base/forms/InlinePillToggle' | ||
| import Icon, { type IconName } from './icons/Icon' | ||
| import { | ||
| getResolvedDarkMode, | ||
| getThemePreference, | ||
| listenToThemePreference, | ||
| setThemePreference, | ||
| type ThemePreference, | ||
| } from 'project/darkMode' | ||
| import { createPortal } from 'react-dom' | ||
|
|
||
| type DarkModeSwitchType = {} | ||
| const themeOptions: { | ||
| icon: IconName | ||
| label: string | ||
| value: ThemePreference | ||
| }[] = [ | ||
| { icon: 'sun', label: 'Light', value: 'light' }, | ||
| { icon: 'moon', label: 'Dark', value: 'dark' }, | ||
| { icon: 'options-2', label: 'System', value: 'system' }, | ||
| ] | ||
|
|
||
| const DarkModeSwitch: FC<DarkModeSwitchType> = ({}) => { | ||
| const [darkModeLocal, setDarkModeLocal] = useState(getDarkMode()) | ||
| const getThemeOption = (preference: ThemePreference) => | ||
| themeOptions.find((option) => option.value === preference) ?? themeOptions[0] | ||
|
|
||
| const toggleDarkMode = () => { | ||
| const newDarkMode = !getDarkMode() | ||
| setDarkModeLocal(newDarkMode) | ||
| persistDarkMode(newDarkMode) | ||
| const getActiveThemeIcon = ( | ||
| preference: ThemePreference, | ||
| resolvedDarkMode: boolean, | ||
| ) => { | ||
| if (preference === 'system') { | ||
| return resolvedDarkMode ? 'moon' : 'sun' | ||
| } | ||
|
|
||
| return getThemeOption(preference).icon | ||
| } | ||
|
|
||
| const getThemeState = () => ({ | ||
| preference: getThemePreference(), | ||
| resolvedDarkMode: getResolvedDarkMode(), | ||
| }) | ||
|
|
||
| const useThemePreference = () => { | ||
| const [themeState, setThemeState] = useState(getThemeState) | ||
|
|
||
| useEffect( | ||
| () => | ||
| listenToThemePreference(() => { | ||
| setThemeState(getThemeState()) | ||
| }), | ||
| [], | ||
| ) | ||
|
|
||
| return { | ||
| ...themeState, | ||
| setPreference: setThemePreference, | ||
| } | ||
| } | ||
|
|
||
| const DarkModeSwitch: FC = () => { | ||
| const { preference, setPreference } = useThemePreference() | ||
|
|
||
| return ( | ||
| <> | ||
| <Row className='mb-2 align-items-center justify-content-between'> | ||
| <h5 className='mb-0'>Theme</h5> | ||
| <InlinePillToggle | ||
| data-test='theme-preference-setting' | ||
| options={themeOptions.map(({ label, value }) => ({ label, value }))} | ||
| size='small' | ||
| value={preference} | ||
| onChange={setPreference} | ||
| /> | ||
| </Row> | ||
| <p className='fs-small lh-sm'> | ||
| Choose a light or dark theme, or follow your system setting. | ||
| </p> | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| export const ThemeModeDropdown: FC = () => { | ||
| const { preference, resolvedDarkMode, setPreference } = useThemePreference() | ||
| const [isOpen, setIsOpen] = useState(false) | ||
| const btnRef = useRef<HTMLButtonElement>(null) | ||
| const dropDownRef = useRef<HTMLDivElement>(null) | ||
| const activeOption = getThemeOption(preference) | ||
| const activeIcon = getActiveThemeIcon(preference, resolvedDarkMode) | ||
|
|
||
| useOutsideClick(dropDownRef as React.RefObject<HTMLElement>, () => | ||
| setIsOpen(false), | ||
| ) | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (!isOpen || !dropDownRef.current || !btnRef.current) return | ||
| const listPosition = calculateListPosition( | ||
| btnRef.current, | ||
| dropDownRef.current, | ||
| ) | ||
| dropDownRef.current.style.top = `${listPosition.top}px` | ||
| dropDownRef.current.style.left = `${listPosition.left}px` | ||
| }, [isOpen]) | ||
|
Comment on lines
+97
to
+105
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dropdown position is currently calculated only once when |
||
|
|
||
| return ( | ||
| <Setting | ||
| title='Dark Mode' | ||
| description='Adjust the theme you see when using Flagsmith.' | ||
| checked={darkModeLocal} | ||
| onChange={toggleDarkMode} | ||
| /> | ||
| <div className='feature-action' tabIndex={-1}> | ||
| <button | ||
| aria-expanded={isOpen} | ||
| aria-label='Theme' | ||
| className='account-dropdown-trigger d-flex ps-3 lh-1 align-items-center text-default' | ||
| data-test='theme-preference-trigger' | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| setIsOpen(!isOpen) | ||
| }} | ||
| ref={btnRef} | ||
| title='Theme' | ||
| type='button' | ||
| > | ||
| <span className='mr-1 icon-secondary'> | ||
| <Icon name={activeIcon} width={18} /> | ||
| </span> | ||
| <span className='d-none d-lg-block'>{activeOption.label}</span> | ||
| </button> | ||
|
|
||
| {isOpen && | ||
| createPortal( | ||
| <div ref={dropDownRef} className='feature-action__list'> | ||
| <div | ||
| className='feature-action__item feature-action__header' | ||
| style={{ | ||
| color: '#656D7B', | ||
| cursor: 'default', | ||
| fontSize: '12px', | ||
| fontWeight: 600, | ||
| padding: '8px 16px', | ||
| }} | ||
| > | ||
| Theme | ||
| </div> | ||
| {themeOptions.map((option) => { | ||
| const isSelected = preference === option.value | ||
| return ( | ||
| <button | ||
| aria-pressed={isSelected} | ||
| className={classNames('feature-action__item theme-option', { | ||
| 'feature-action__item--selected': isSelected, | ||
| })} | ||
| data-test={`theme-preference-${option.value}`} | ||
| key={option.value} | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| setPreference(option.value) | ||
| setIsOpen(false) | ||
| }} | ||
| type='button' | ||
| > | ||
| <Icon name={option.icon} width={18} fill='#9DA4AE' /> | ||
| <span>{option.label}</span> | ||
| {isSelected && ( | ||
| <Icon | ||
| className='ms-auto' | ||
| name='checkmark' | ||
| width={16} | ||
| fill='#6837FC' | ||
| /> | ||
| )} | ||
| </button> | ||
| ) | ||
| })} | ||
| </div>, | ||
| document.body, | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
|
|
||
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.
Since
AsyncStorage.clear()is an asynchronous operation that returns a Promise, executing synchronousstorageSetcalls immediately after it creates a race condition. The storage might be cleared after the theme preferences are restored, causing them to be lost on logout. Wrapping the restoration in the.then()callback ofAsyncStorage.clear()ensures they are safely preserved.