Skip to content

Commit

Permalink
Add force fallback option (#36)
Browse files Browse the repository at this point in the history
* Add force fallback flag

* Fix highlight syntax theme
  • Loading branch information
danielr18 authored and renatorib committed Mar 18, 2019
1 parent ccbeced commit f9404d2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
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

0 comments on commit f9404d2

Please sign in to comment.