Skip to content

Commit

Permalink
feat(windmill): add usePreferences to windmill
Browse files Browse the repository at this point in the history
Instead of adding all storage, user preferences and theme customization utilities as a default, they
are now under the usePreferences prop, being optional

BREAKING CHANGE: Now you need to use the prop `usePreferences` in the root Windmill component if you
want access to theme utilities like the current theme or theme toggler, user preferences and user
theme storage. These features were enabled by default before. You get the old behavior just adding
`usePreferences` to the root Windmill component.
  • Loading branch information
estevanmaito committed Jul 11, 2020
1 parent 7cfa036 commit a619a28
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
28 changes: 25 additions & 3 deletions __tests__/Windmill.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,19 @@ describe('Windmill Context', () => {
expect(wrapper.find('button').getDOMNode().getAttribute('class')).toContain(expected)
})

it('should not add dark theme class to html element', () => {
it('should add theme-light class to html element is usePreferences is present', () => {
const expected = 'theme-light'
mount(
<Windmill usePreferences>
<Button />
</Windmill>
)

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

it('should not add any class to html element if usePreferences is ausent', () => {
const expected = ''
mount(
<Windmill>
<Button />
Expand All @@ -57,7 +68,7 @@ describe('Windmill Context', () => {
it('should execute the toggleTheme method', () => {
const expected = 'theme-dark'
const wrapper = mount(
<Windmill>
<Windmill usePreferences>
<TestButton />
</Windmill>
)
Expand All @@ -82,7 +93,7 @@ describe('Windmill Context', () => {

const expected = 'theme-dark'
mount(
<Windmill>
<Windmill usePreferences>
<Button />
</Windmill>
)
Expand All @@ -100,4 +111,15 @@ describe('Windmill Context', () => {

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

it('should add dark theme class to html element when usePreferences is enabled', () => {
const expected = 'theme-dark'
mount(
<Windmill dark usePreferences>
<Button />
</Windmill>
)

expect(document.documentElement.getAttribute('class')).toBe(expected)
})
})
13 changes: 10 additions & 3 deletions src/Windmill.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import defaultTheme from './themes/default'
import { mergeDeep } from '../utils/mergeDeep'
import useDarkMode from '../utils/useDarkMode'

function Windmill({ children, theme: customTheme, dark }) {
function Windmill({ children, theme: customTheme, dark, usePreferences }) {
const mergedTheme = mergeDeep(defaultTheme, customTheme)
const [mode, setMode, toggleMode] = useDarkMode()
const [mode, setMode, toggleMode] = useDarkMode(usePreferences)

useLayoutEffect(() => {
if (dark) {
setMode('dark')
if (mode !== null) {
setMode('dark')
}
document.documentElement.classList.add(`theme-dark`)
}
}, [dark])
Expand All @@ -32,6 +34,11 @@ Windmill.propTypes = {
children: PropTypes.node.isRequired,
theme: PropTypes.object,
dark: PropTypes.bool,
usePreferences: PropTypes.bool,
}

Windmill.defaultProps = {
usePreferences: false,
}

export default Windmill
4 changes: 3 additions & 1 deletion utils/useDarkMode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useEffect, useState } from 'react'

export const useDarkMode = () => {
export const useDarkMode = (usePreferences) => {
if (!usePreferences) return [null, null, null]

const [mode, setMode] = useState()

const toggleMode = () => {
Expand Down

0 comments on commit a619a28

Please sign in to comment.