Skip to content

Commit f9404d2

Browse files
danielr18renatorib
authored andcommitted
Add force fallback option (#36)
* Add force fallback flag * Fix highlight syntax theme
1 parent ccbeced commit f9404d2

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

.storybook/stories/index.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { storiesOf } from '@storybook/react'
33
import { Code, Result } from '../components'
44
import MobileBreakpoint from '../components/MobileBreakpoint'
55
import withSizes from '../../src/withSizes'
6+
import SizesProvider from '../../src/SizesProvider'
67

78
const mapSizesToProps = sizes => ({
89
backgroundColor: sizes.width > 800 ? 'green' : 'blue',
@@ -24,6 +25,33 @@ const ExampleSizedComponent = withSizes(mapSizesToProps)(
2425
)
2526
)
2627

28+
class ForceFallbackExample extends React.Component {
29+
state = {
30+
forceFallback: true,
31+
}
32+
33+
componentDidMount() {
34+
setTimeout(() => {
35+
this.setState({ forceFallback: false })
36+
}, 5000)
37+
}
38+
39+
render() {
40+
const { forceFallback } = this.state
41+
42+
return (
43+
<SizesProvider
44+
config={{ fallbackHeight: 640, fallbackWidth: 280, forceFallback }}
45+
>
46+
<Result>
47+
{forceFallback && <p>Forcing fallback to mobile</p>}
48+
<ExampleSizedComponent />
49+
</Result>
50+
</SizesProvider>
51+
)
52+
}
53+
}
54+
2755
storiesOf('Sizes', module)
2856
.add('default behavior', () => (
2957
<div>
@@ -54,11 +82,11 @@ const ExampleSizedComponent = withSizes(mapSizesToProps)(
5482
</Code>
5583
</div>
5684
))
57-
5885
.add('mobileBreakpoint', () => (
5986
<div>
6087
<MobileBreakpoint breakpoint={300} />
6188
<MobileBreakpoint breakpoint={500} />
6289
<MobileBreakpoint breakpoint={700} />
6390
</div>
6491
))
92+
.add('force fallback', () => <ForceFallbackExample />)

src/utils/getWindowSizes.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const getWindowSizes = ({ fallbackWidth = null, fallbackHeight = null }) => {
1+
const getWindowSizes = ({ fallbackWidth = null, fallbackHeight = null, forceFallback = false }) => {
22
const canUseDOM = typeof window !== 'undefined'
33

44
return {
5-
width: canUseDOM ? window.innerWidth : fallbackWidth,
6-
height: canUseDOM ? window.innerHeight : fallbackHeight,
5+
width: canUseDOM && !forceFallback ? window.innerWidth : fallbackWidth,
6+
height: canUseDOM && !forceFallback ? window.innerHeight : fallbackHeight,
77
canUseDOM,
88
}
99
}

src/withSizes.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import SizesContext from './SizesContext'
1010
import * as presets from './presets'
1111

1212
const getWindowSizesWithFallback = props => {
13-
const { fallbackHeight, fallbackWidth } = props
14-
return getWindowSizes({ fallbackHeight, fallbackWidth })
13+
const { fallbackHeight, fallbackWidth, forceFallback } = props
14+
return getWindowSizes({ fallbackHeight, fallbackWidth, forceFallback })
1515
}
1616

1717
const withSizes = (...mappedSizesToProps) => WrappedComponent => {
@@ -76,6 +76,7 @@ const withSizes = (...mappedSizesToProps) => WrappedComponent => {
7676
const {
7777
fallbackHeight,
7878
fallbackWidth,
79+
forceFallback,
7980
...otherProps
8081
} = this.props
8182

0 commit comments

Comments
 (0)