Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion .storybook/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { storiesOf } from '@storybook/react'
import { Code, Result } from '../components'
import MobileBreakpoint from '../components/MobileBreakpoint'
import withSizes from '../../src/withSizes'
import SizesProvider from '../../src/SizesProvider'

const mapSizesToProps = sizes => ({
backgroundColor: sizes.width > 800 ? 'green' : 'blue',
Expand All @@ -24,6 +25,33 @@ const ExampleSizedComponent = withSizes(mapSizesToProps)(
)
)

class ForceFallbackExample extends React.Component {
state = {
forceFallback: true,
}

componentDidMount() {
setTimeout(() => {
this.setState({ forceFallback: false })
}, 5000)
}

render() {
const { forceFallback } = this.state

return (
<SizesProvider
config={{ fallbackHeight: 640, fallbackWidth: 280, forceFallback }}
>
<Result>
{forceFallback && <p>Forcing fallback to mobile</p>}
<ExampleSizedComponent />
</Result>
</SizesProvider>
)
}
}

storiesOf('Sizes', module)
.add('default behavior', () => (
<div>
Expand Down Expand Up @@ -54,11 +82,11 @@ const ExampleSizedComponent = withSizes(mapSizesToProps)(
</Code>
</div>
))

.add('mobileBreakpoint', () => (
<div>
<MobileBreakpoint breakpoint={300} />
<MobileBreakpoint breakpoint={500} />
<MobileBreakpoint breakpoint={700} />
</div>
))
.add('force fallback', () => <ForceFallbackExample />)
6 changes: 3 additions & 3 deletions src/utils/getWindowSizes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const getWindowSizes = ({ fallbackWidth = null, fallbackHeight = null }) => {
const getWindowSizes = ({ fallbackWidth = null, fallbackHeight = null, forceFallback = false }) => {
const canUseDOM = typeof window !== 'undefined'

return {
width: canUseDOM ? window.innerWidth : fallbackWidth,
height: canUseDOM ? window.innerHeight : fallbackHeight,
width: canUseDOM && !forceFallback ? window.innerWidth : fallbackWidth,
height: canUseDOM && !forceFallback ? window.innerHeight : fallbackHeight,
canUseDOM,
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/withSizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import SizesContext from './SizesContext'
import * as presets from './presets'

const getWindowSizesWithFallback = props => {
const { fallbackHeight, fallbackWidth } = props
return getWindowSizes({ fallbackHeight, fallbackWidth })
const { fallbackHeight, fallbackWidth, forceFallback } = props
return getWindowSizes({ fallbackHeight, fallbackWidth, forceFallback })
}

const withSizes = (...mappedSizesToProps) => WrappedComponent => {
Expand Down Expand Up @@ -76,6 +76,7 @@ const withSizes = (...mappedSizesToProps) => WrappedComponent => {
const {
fallbackHeight,
fallbackWidth,
forceFallback,
...otherProps
} = this.props

Expand Down