Skip to content

Commit

Permalink
feat(windmill): add theme storage and user system preferences getter …
Browse files Browse the repository at this point in the history
…as default
  • Loading branch information
estevanmaito committed Jun 29, 2020
1 parent 192286c commit 04c06ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
51 changes: 36 additions & 15 deletions __tests__/Windmill.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import React, { useContext } from 'react'
import { mount } from 'enzyme'
import Button from '../src/Button'
import Windmill from '../src/Windmill'
import { ThemeContext } from '../src/context/ThemeContext'
import { WindmillContext } from '../src'

function TestButton() {
const { toggleTheme } = useContext(ThemeContext)
const { toggleTheme } = useContext(WindmillContext)

return <button onClick={toggleTheme}>Click</button>
}

beforeEach(() => {
document.documentElement.className = ''
})

describe('Windmill Context', () => {
beforeEach(() => {
document.documentElement.className = ''
})

it('should use defaultTheme styles', () => {
const expected =
'inline-flex items-center justify-center cursor-pointer leading-5 transition-colors duration-150 font-medium focus:outline-none'
Expand Down Expand Up @@ -54,28 +54,49 @@ describe('Windmill Context', () => {
expect(document.documentElement.getAttribute('class')).toBe(expected)
})

it('should add dark theme class to html element', () => {
it('should execute the toggleTheme method', () => {
const expected = 'theme-dark'
mount(
<Windmill dark>
<Button />
const wrapper = mount(
<Windmill>
<TestButton />
</Windmill>
)

const button = wrapper.find('button')

button.simulate('click')

expect(document.documentElement.getAttribute('class')).toBe(expected)
})

it('should execute the toggleTheme method', () => {
it('should add dark theme based on users preference', () => {
Object.defineProperty(window, 'matchMedia', {
value: jest.fn(() => {
return {
matches: true,
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
}
}),
})

const expected = 'theme-dark'
const wrapper = mount(
mount(
<Windmill>
<TestButton />
<Button />
</Windmill>
)

const button = wrapper.find('button')
expect(document.documentElement.getAttribute('class')).toBe(expected)
})

button.simulate('click')
it('should add dark theme class to html element', () => {
const expected = 'theme-dark'
mount(
<Windmill dark>
<Button />
</Windmill>
)

expect(document.documentElement.getAttribute('class')).toBe(expected)
})
Expand Down
13 changes: 13 additions & 0 deletions src/Windmill.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import { mergeDeep } from '../utils/mergeDeep'
function Windmill({ children, theme, dark }) {
const mergedTheme = mergeDeep(defaultTheme, theme)
const [isDark, setIsDark] = useState(false)
const userPrefersDark =
!!window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches

useLayoutEffect(() => {
const userPreference = JSON.parse(window.localStorage.getItem('dark')) || userPrefersDark
if (userPreference) {
document.documentElement.classList.add(`theme-dark`)
}
}, [])

useLayoutEffect(() => {
window.localStorage.setItem('dark', isDark)
}, [isDark])

useLayoutEffect(() => {
if (dark) {
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ThemeContext } from './context/ThemeContext'

export { default as Windmill } from './Windmill'
export { default as Button } from './Button'
export { default as Card } from './Card'
Expand Down Expand Up @@ -25,3 +27,4 @@ export { default as TableHeader } from './TableHeader'
export { default as TableFooter } from './TableFooter'
export { default as TableRow } from './TableRow'
export { default as Pagination } from './Pagination'
export const WindmillContext = ThemeContext

0 comments on commit 04c06ef

Please sign in to comment.