Skip to content

Commit

Permalink
feat: add useTheme & useUp / useDown
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Nov 13, 2019
1 parent a6ea205 commit 36d2909
Show file tree
Hide file tree
Showing 20 changed files with 3,027 additions and 2,645 deletions.
52 changes: 26 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,47 @@
"test": "jest"
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-transform-modules-commonjs": "^7.4.4",
"@babel/plugin-transform-runtime": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@emotion/core": "^10.0.16",
"@emotion/styled": "^10.0.15",
"@testing-library/jest-dom": "^4.1.0",
"@testing-library/react": "^9.1.3",
"@babel/core": "^7.7.2",
"@babel/plugin-proposal-class-properties": "^7.7.0",
"@babel/plugin-transform-modules-commonjs": "^7.7.0",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.7.1",
"@babel/preset-react": "^7.7.0",
"@emotion/core": "^10.0.22",
"@emotion/styled": "^10.0.23",
"@testing-library/jest-dom": "^4.2.3",
"@testing-library/react": "^9.3.2",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"bundlesize": "^0.18.0",
"codecov": "^3.5.0",
"codecov": "^3.6.1",
"conventional-github-releaser": "^3.1.3",
"emotion-theming": "^10.0.10",
"eslint": "^6.2.2",
"emotion-theming": "^10.0.19",
"eslint": "^6.6.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.1.0",
"eslint-config-smooth": "^2.0.1",
"eslint-config-prettier": "^6.5.0",
"eslint-config-smooth": "^2.1.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^2.0.1",
"eslint-plugin-react": "^7.16.0",
"eslint-plugin-react-hooks": "^2.2.0",
"jest": "^24.9.0",
"jest-emotion": "^10.0.11",
"jest-styled-components": "^6.3.1",
"lerna": "^3.16.4",
"prettier": "^1.18.2",
"jest-emotion": "^10.0.17",
"jest-styled-components": "^6.3.4",
"lerna": "^3.18.4",
"prettier": "^1.19.1",
"prop-types": "^15.7.2",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"rollup": "^1.20.2",
"react": "^16.11.0",
"react-dom": "^16.11.0",
"rollup": "^1.27.0",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.0.2",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-size-snapshot": "^0.10.0",
"rollup-plugin-terser": "^5.0.0",
"rollup-plugin-terser": "^5.1.2",
"shx": "^0.3.2",
"styled-components": "^4.3.1"
"styled-components": "^4.4.1"
}
}
8 changes: 4 additions & 4 deletions packages/babel-preset-emotion-css-prop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"access": "public"
},
"dependencies": {
"@babel/plugin-transform-react-jsx": "^7.3.0",
"@babel/runtime": "^7.5.5",
"@emotion/babel-plugin-jsx-pragmatic": "^0.1.2",
"babel-plugin-emotion": "^10.0.16"
"@babel/plugin-transform-react-jsx": "^7.7.0",
"@babel/runtime": "^7.7.2",
"@emotion/babel-plugin-jsx-pragmatic": "^0.1.4",
"babel-plugin-emotion": "^10.0.23"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"access": "public"
},
"dependencies": {
"@babel/runtime": "^7.5.5",
"@babel/runtime": "^7.7.2",
"@xstyled/system": "^1.11.0"
}
}
72 changes: 72 additions & 0 deletions packages/core/src/breakpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-disable no-undef */
import React from 'react'
import { getBreakpoints } from '@xstyled/system'

export function useThemeBreakpoints(theme) {
return getBreakpoints({ theme })
}

/**
* Minimum breakpoint width.
* Null for the smallest breakpoint.
*/
function useThemeMinValue(theme, key) {
const breakpoints = useThemeBreakpoints(theme)
const value = breakpoints[key]
return value === 0 ? null : value
}

/**
* Maximum breakpoint width. Null for the largest (last) breakpoint.
* The maximum value is calculated as the minimum of the next one less 0.02px
* to work around the limitations of `min-` and `max-` prefixes and viewports with fractional widths.
* See https://www.w3.org/TR/mediaqueries-4/#mq-min-max
* Uses 0.02px rather than 0.01px to work around a current rounding bug in Safari.
* See https://bugs.webkit.org/show_bug.cgi?id=178261
*/
function useThemeMaxValue(theme, key) {
const breakpoints = useThemeBreakpoints(theme)
const breakPoint = breakpoints[key]
return breakPoint === 0 ? null : breakPoint - 0.02
}

export function useViewportWidth() {
const [width, setWidth] = React.useState(null)

React.useLayoutEffect(() => {
setWidth(window.innerWidth)

function handleResize() {
setWidth(window.innerWidth)
}

window.addEventListener('resize', handleResize)
return () => window.removeEventListener('resize', handleResize)
}, [])

return width
}

export function useThemeBreakpoint(theme) {
const breakpoints = useThemeBreakpoints(theme)
const width = useViewportWidth()
return React.useMemo(() => {
return (
Object.keys(breakpoints)
.reverse()
.find(breakpoint => width > breakpoints[breakpoint]) || null
)
}, [breakpoints, width])
}

export function useThemeUp(theme, key) {
const value = useThemeMinValue(theme, key)
const width = useViewportWidth()
return width > value
}

export function useThemeDown(theme, key) {
const value = useThemeMaxValue(theme, key)
const width = useViewportWidth()
return width < value
}
1 change: 1 addition & 0 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './breakpoints'
export * from './createBox'
export * from './propGetters'
export * from './transform'
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"emotion-theming": "^10.0.0"
},
"dependencies": {
"@babel/runtime": "^7.5.5",
"@babel/runtime": "^7.7.2",
"@xstyled/core": "^1.13.1",
"@xstyled/util": "^1.11.0"
}
Expand Down
25 changes: 25 additions & 0 deletions packages/emotion/src/breakpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
useThemeBreakpoints,
useThemeBreakpoint,
useThemeUp,
useThemeDown,
} from '@xstyled/core'
import { useTheme } from 'emotion-theming'

export { useViewportWidth } from '@xstyled/core'

export function useBreakpoints() {
return useThemeBreakpoints(useTheme())
}

export function useBreakpoint() {
return useThemeBreakpoint(useTheme())
}

export function useUp(key) {
return useThemeUp(useTheme(), key)
}

export function useDown(key) {
return useThemeDown(useTheme(), key)
}
3 changes: 2 additions & 1 deletion packages/emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export {
Global,
ClassNames,
} from '@emotion/core'
export { ThemeProvider, withTheme } from 'emotion-theming'
export { ThemeProvider, withTheme, useTheme } from 'emotion-theming'
export * from './breakpoints'
2 changes: 1 addition & 1 deletion packages/prop-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"prop-types": "^15.0.0"
},
"dependencies": {
"@babel/runtime": "^7.5.5"
"@babel/runtime": "^7.7.2"
}
}
2 changes: 1 addition & 1 deletion packages/styled-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"styled-components": "^4.0.0"
},
"dependencies": {
"@babel/runtime": "^7.5.5",
"@babel/runtime": "^7.7.2",
"@xstyled/core": "^1.13.1",
"@xstyled/util": "^1.11.0"
}
Expand Down
25 changes: 25 additions & 0 deletions packages/styled-components/src/breakpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
useThemeBreakpoints,
useThemeBreakpoint,
useThemeUp,
useThemeDown,
} from '@xstyled/core'
import { useTheme } from './theme'

export { useViewportWidth } from '@xstyled/core'

export function useBreakpoints() {
return useThemeBreakpoints(useTheme())
}

export function useBreakpoint() {
return useThemeBreakpoint(useTheme())
}

export function useUp(key) {
return useThemeUp(useTheme(), key)
}

export function useDown(key) {
return useThemeDown(useTheme(), key)
}
2 changes: 2 additions & 0 deletions packages/styled-components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export { css } from './css'
export { createGlobalStyle } from './createGlobalStyle'
export { Box, styled as default } from './styled'
export * from './colorModes'
export * from './theme'
export * from './breakpoints'
6 changes: 6 additions & 0 deletions packages/styled-components/src/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react'
import { ThemeContext } from 'styled-components'

export function useTheme() {
return React.useContext(ThemeContext)
}
27 changes: 27 additions & 0 deletions packages/styled-components/src/theme.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import '@testing-library/jest-dom/extend-expect'
import { render, cleanup } from '@testing-library/react'
import { ThemeProvider } from 'styled-components'
import { useTheme } from '.'

afterEach(cleanup)

describe('#useTheme', () => {
it('returns theme', () => {
const theme = { foo: 'bar' }
const spy = jest.fn()
function ThemeLogger() {
const theme = useTheme()
React.useEffect(() => {
spy(theme)
}, [theme])
return null
}
render(
<ThemeProvider theme={theme}>
<ThemeLogger />
</ThemeProvider>,
)
expect(spy).toHaveBeenCalledWith(theme)
})
})
2 changes: 1 addition & 1 deletion packages/system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"access": "public"
},
"dependencies": {
"@babel/runtime": "^7.5.5",
"@babel/runtime": "^7.7.2",
"@xstyled/util": "^1.11.0"
}
}
2 changes: 1 addition & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"access": "public"
},
"dependencies": {
"@babel/runtime": "^7.5.5"
"@babel/runtime": "^7.7.2"
}
}
10 changes: 5 additions & 5 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"@smooth-ui/core-sc": "^10.1.0",
"@xstyled/styled-components": "^1.13.0",
"@xstyled/system": "^1.11.0",
"gatsby": "^2.15.28",
"gatsby-plugin-layout": "^1.1.10",
"react": "^16.10.1",
"react-dom": "^16.10.1",
"gatsby": "^2.17.12",
"gatsby-plugin-layout": "^1.1.14",
"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-live": "^2.2.1",
"smooth-code-landers": "^1.3.2",
"smooth-doc": "^2.14.2",
"styled-components": "^4.4.0"
"styled-components": "^4.4.1"
},
"devDependencies": {}
}

0 comments on commit 36d2909

Please sign in to comment.