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

Add a dark mode #10

Open
jletey opened this issue Jul 19, 2019 · 16 comments · May be fixed by #22
Open

Add a dark mode #10

jletey opened this issue Jul 19, 2019 · 16 comments · May be fixed by #22
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jletey
Copy link
Member

jletey commented Jul 19, 2019

For anyone using this in the future at night, it would be nice to have a dark mode in the desktop app!

@johno johno added enhancement New feature or request help wanted Extra attention is needed labels Jul 19, 2019
@jletey jletey mentioned this issue Jul 22, 2019
5 tasks
@jletey
Copy link
Member Author

jletey commented Jul 22, 2019

@johno Would you want to use a hook for this?

@johno
Copy link
Member

johno commented Jul 22, 2019

I'll need to do a bit of thinking on this, but perhaps we allow users to select a Theme UI theme (and we provide a couple defaults like light/dark/etc) and we store the selection in the config. If we go this approach we'd prolly use some sort of useConfig hook which sets local app state and persists to the electron-config.

@jletey
Copy link
Member Author

jletey commented Jul 30, 2019

@jxnblk When I add const [colorMode, setColorMode] = useColorMode() to src/index.js, all of the sudden the app doesn't work

Do you have any idea why this is happening?

You can find my code here on my fork

@jxnblk
Copy link

jxnblk commented Jul 30, 2019

@johnletey The hook needs to be inside the ThemeProvider to work, but I don't see one in src/index.js

@jletey
Copy link
Member Author

jletey commented Jul 30, 2019

@jxnblk The ThemeProvider is in src/layout.js

@jxnblk
Copy link

jxnblk commented Jul 30, 2019

@johnletey right, so it looks like the useColorMode hook is being called outside of the ThemeProvider, right?

@jletey
Copy link
Member Author

jletey commented Jul 30, 2019

That is correct

@jletey
Copy link
Member Author

jletey commented Jul 30, 2019

@jxnblk So, still doesn't work when I have the following in src/layout.js:

import React from 'react'
import { ThemeProvider, ColorMode, useColorMode } from 'theme-ui'
import { theme } from '@blocks/editor'
import desktopTheme from './theme'

const [colorMode, setColorMode] = useColorMode()

export default ({ children }) => (
  <ThemeProvider theme={theme && desktopTheme}>
    <button
      onClick={e => {
        setColorMode(colorMode === 'light' ? 'dark' : 'light')
      }}>
      Toggle {colorMode === 'light' ? 'Dark' : 'Light'}
    </button>
    <ColorMode />
    {children}
  </ThemeProvider>
)

and no other theme code in src/index.js

@jletey
Copy link
Member Author

jletey commented Jul 31, 2019

@jxnblk @johno Still no progress ... any ideas?

@jxnblk
Copy link

jxnblk commented Jul 31, 2019

@johnletey the useContext hook needs to be inside a component that's rendered inside the provider.

E.g.

// header component
import React from 'react'
import { useColorMode } from 'theme-ui'
export default props => {
  const [colorMode, setColorMode] = useColorMode()
  return (
    <header>
      <button
        onClick={e => {
          setColorMode(colorMode === 'light' ? 'dark' : 'light')
        }}>
        Toggle {colorMode === 'light' ? 'Dark' : 'Light'}
      </button>
    </header>
  )
}
// layout component
const Layout = props =>
  <ThemeProvider theme={theme}>
    <Header />
    {props.children}
  </ThemeProvider>

@jletey
Copy link
Member Author

jletey commented Jul 31, 2019

@jxnblk Thanks for the help ... but still not working ... here's the code:

src/layout.js:

import React from 'react'
import { ThemeProvider } from 'theme-ui'
import ThemeSwitcher from './themeSwitcher';
import theme from './theme'

export default ({ children }) => (
  <ThemeProvider theme={theme}>
    <ThemeSwitcher />
    {children}
  </ThemeProvider>
)

src/themeSwitcher.js:

import React from 'react'
import { useColorMode, Styled } from 'theme-ui'

export default () => {
  const [colorMode, setColorMode] = useColorMode()
  return (
    <Styled.root
      css={{
        maxWidth: '48em',
        padding: 32,
        marginLeft: 'auto',
        marginRight: 'auto',
        marginTop: '40px'
      }}
    >
      <Styled.button
        css={{
          display: 'flex',
          alignItems: 'center',
          cursor: 'pointer'
        }}
        onClick={e => {
          setColorMode(colorMode === 'light' ? 'dark' : 'light')
        }}>
        Toggle {colorMode === 'light' ? 'Dark' : 'Light'}
      </Styled.button>
    </Styled.root>
  )
}

src/theme.js:

import { theme } from '@blocks/editor'

export default {
    ...theme,
    initialColorMode: 'light',
    useCustomProperties: true,
    colors: {
        background: '#fff',
        modes: {
            dark: {
                background: '#000',
            }
        }
    }
}

@jxnblk
Copy link

jxnblk commented Jul 31, 2019

Are you rendering the <ColorMode /> component? The setup is a little more cumbersome without the Gatsby plugin https://theme-ui.com/color-modes#applying-colors

@jletey
Copy link
Member Author

jletey commented Jul 31, 2019

No I am not @jxnblk ... I never use the <ColorMode /> component ... where exactly should I put it?

If I put it in src/layout.js like so:

import React from 'react'
import { ThemeProvider, ColorMode } from 'theme-ui'
import ThemeSwitcher from './themeSwitcher';
import theme from './theme'

export default ({ children }) => (
  <ThemeProvider theme={theme}>
    <ThemeSwitcher />
    <ColorMode />
    {children}
  </ThemeProvider>
)

the app still doesn't work

@jxnblk
Copy link

jxnblk commented Jul 31, 2019

Hmm, not sure then. Let me know when you push the branch up and I can try to take a look

@jletey
Copy link
Member Author

jletey commented Jul 31, 2019

@jxnblk Just pushed my most recent edits to my feat/dark-mode branch ... you can also see #22

@jletey
Copy link
Member Author

jletey commented Aug 1, 2019

@jxnblk @johno Got it to work this morning ... there are still some problems (it doesn't look like the @blocks/editor styles are being injected properly, doesn't switch themes when in editor view, and so on), but it works!

Shot 2019-08-01 at 14 11 05
Shot 2019-08-01 at 14 11 08

Thanks for all of the help! 🙏

@jletey jletey linked a pull request Aug 6, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants