Skip to content

Commit

Permalink
fix(settings): always start the dark mode switch in the correct posit…
Browse files Browse the repository at this point in the history
…ion (#195)
  • Loading branch information
wKovacs64 authored May 7, 2021
1 parent fb08f0b commit d6bfe12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
23 changes: 23 additions & 0 deletions src/components/dark-mode-toggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Switch from 'react-switch';
import useDarkMode from 'use-dark-mode';
import { appDefaultDarkMode } from '../state';
import { palette } from '../theme';

function DarkModeToggle(): JSX.Element | null {
const darkMode = useDarkMode(appDefaultDarkMode);

// defer rendering of the toggle until the client has been hydrated
if (typeof window === 'undefined') return null;

return (
<Switch
id="dark-mode"
aria-checked={darkMode.value}
checked={darkMode.value}
onChange={darkMode.toggle}
onColor={palette.primary}
/>
);
}

export default DarkModeToggle;
15 changes: 3 additions & 12 deletions src/pages/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import * as React from 'react';
import Switch from 'react-switch';
import styled from '@emotion/styled';
import Layout from '../components/layout';
import NumericInput from '../components/numeric-input';
import DarkModeToggle from '../components/dark-mode-toggle';
import Content from '../elements/content';
import BrandButton from '../elements/brand-button';
import {
appDefaultDarkMode,
appDefaultPartySize,
appDefaultTipPercent,
useDarkMode,
useDefaultPartySize,
useDefaultTipPercent,
} from '../state';
import { rhythm, scale, palette } from '../theme';
import { rhythm, scale } from '../theme';
import { mq } from '../utils';

const SettingsGrid = styled.section`
Expand Down Expand Up @@ -63,7 +61,6 @@ const SettingInput = styled(NumericInput)`
const SettingsPage: React.FunctionComponent<
import('reach__router').RouteComponentProps
> = ({ navigate }) => {
const darkMode = useDarkMode(appDefaultDarkMode);
const [defaultPartySize, setDefaultPartySize] = useDefaultPartySize(
appDefaultPartySize,
);
Expand Down Expand Up @@ -100,13 +97,7 @@ const SettingsPage: React.FunctionComponent<
<SettingsGrid>
<SingleRowSetting>
<SettingLabel htmlFor="dark-mode">Dark mode:</SettingLabel>
<Switch
id="dark-mode"
aria-checked={darkMode.value}
checked={darkMode.value}
onChange={darkMode.toggle}
onColor={palette.primary}
/>
<DarkModeToggle />
</SingleRowSetting>
<Setting>
<SettingLabel htmlFor="default-party-size">
Expand Down
2 changes: 0 additions & 2 deletions src/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import createPersistedState from 'use-persisted-state';

export { default as useDarkMode } from 'use-dark-mode';

export const useDefaultPartySize = createPersistedState('defaultPartySize');
export const useDefaultTipPercent = createPersistedState('defaultTipPercent');

Expand Down

0 comments on commit d6bfe12

Please sign in to comment.