Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add responsive component, make banner responsive
- Loading branch information
Showing
4 changed files
with
140 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React from "react" | ||
|
||
const ResponsiveContext = React.createContext({}) | ||
|
||
interface Breakpoints { | ||
[breakpoint: string]: string | ||
} | ||
|
||
interface ResponsiveProviderProps { | ||
breakpoints: Breakpoints | ||
} | ||
|
||
interface BreakpointState { | ||
[breakpoint: string]: boolean | ||
} | ||
|
||
interface ResponsiveProviderState { | ||
breakpoints: BreakpointState | ||
mediaMatchers: MediaQueryList[] | ||
mqHandler: null | MediaQueryListListener | ||
} | ||
|
||
export class ResponsiveProvider extends React.Component< | ||
ResponsiveProviderProps, | ||
ResponsiveProviderState | ||
> { | ||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
breakpoints: { | ||
...Object.keys(props.breakpoints) | ||
.map(breakpoint => ({ | ||
[breakpoint]: false, | ||
})) | ||
.reduce((acc, curr) => ({ ...acc, ...curr }), {}), | ||
}, | ||
mediaMatchers: [], | ||
mqHandler: null, | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
this.setupObservers(this.props.breakpoints) | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.state.mqHandler) { | ||
this.state.mediaMatchers.forEach(mediaQuery => | ||
mediaQuery.removeListener(this.state.mqHandler) | ||
) | ||
} | ||
} | ||
|
||
setupObservers = breakpoints => { | ||
const breakpointKeys = Object.keys(breakpoints) | ||
const mediaMatchers = breakpointKeys.map(breakpoint => | ||
window.matchMedia(breakpoints[breakpoint]) | ||
) | ||
const mqHandler = () => { | ||
for (let i = 0; i < mediaMatchers.length; ++i) { | ||
this.setState({ | ||
breakpoints: { [breakpointKeys[i]]: mediaMatchers[i].matches }, | ||
}) | ||
} | ||
} | ||
this.setState({ mediaMatchers, mqHandler }) | ||
mediaMatchers.forEach(mediaQuery => { | ||
mediaQuery.addListener(mqHandler) | ||
}) | ||
mqHandler() | ||
} | ||
|
||
render() { | ||
return ( | ||
<ResponsiveContext.Provider value={this.state.breakpoints}> | ||
{this.props.children} | ||
</ResponsiveContext.Provider> | ||
) | ||
} | ||
} | ||
|
||
export const Responsive: React.ComponentType< | ||
React.ConsumerProps<BreakpointState> | ||
> = | ||
ResponsiveContext.Consumer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
import React from "react" | ||
import { storiesOf as _storiesOf } from "@storybook/react" | ||
import { Theme, injectGlobalCSS } from "@artsy/palette" | ||
import { ResponsiveProvider } from "../Styleguide/Elements/Responsive" | ||
|
||
injectGlobalCSS() | ||
|
||
const breakpoints = { | ||
xl: "(min-width: 1192px)", | ||
lg: "(min-width: 1024px) and (max-width: 1191px)", | ||
md: "(min-width: 900px) and (max-width: 1023px)", | ||
sm: "(min-width: 768px) and (max-width: 889px)", | ||
xs: "(max-width: 767px)", | ||
} | ||
|
||
export function storiesOf(desc, mod) { | ||
return _storiesOf(desc, mod).addDecorator(storyFn => { | ||
return <Theme>{storyFn()}</Theme> | ||
return ( | ||
<Theme> | ||
<ResponsiveProvider breakpoints={breakpoints}> | ||
{storyFn()} | ||
</ResponsiveProvider> | ||
</Theme> | ||
) | ||
}) | ||
} |