@hackplan/polaris
Version:
Shopify’s product component library
54 lines (53 loc) • 2.41 kB
JavaScript
import React from 'react';
import hoistStatics from 'hoist-non-react-statics';
import StickyManager from '../StickyManager';
import { ThemeProviderContext, } from '../../../ThemeProvider';
import AppProviderContext from '../../context';
function withScrollable(WrappedComponent) {
class WithScrollable extends React.Component {
constructor() {
super(...arguments);
this.stickyManager = new StickyManager();
}
render() {
return (<AppProviderContext.Consumer>
{(polaris) => (<AppProviderContext.Provider value={Object.assign({}, polaris, { stickyManager: this.stickyManager })}>
<WrappedComponent {...this.props}/>
</AppProviderContext.Provider>)}
</AppProviderContext.Consumer>);
}
}
WithScrollable.contextTypes = WrappedComponent.contextTypes;
return WithScrollable;
}
export default function withAppProvider({ withinScrollable, } = {}) {
return function addProvider(WrappedComponent) {
// eslint-disable-next-line react/prefer-stateless-function
class WithProvider extends React.Component {
render() {
return (<AppProviderContext.Consumer>
{(polaris) => {
return (<ThemeProviderContext.Consumer>
{(polarisTheme) => {
const polarisContext = Object.assign({}, polaris, { theme: polarisTheme });
if (Object.keys(polaris).length < 1) {
throw new Error(`The <AppProvider> component is required as of v2.0 of Polaris React. See
https://polaris.shopify.com/components/structure/app-provider for implementation
instructions.`);
}
return (<WrappedComponent {...this.props} polaris={polarisContext}/>);
}}
</ThemeProviderContext.Consumer>);
}}
</AppProviderContext.Consumer>);
}
}
WithProvider.contextTypes = WrappedComponent.contextTypes;
let WithScrollable;
if (withinScrollable) {
WithScrollable = withScrollable(WithProvider);
}
const FinalComponent = hoistStatics(WithScrollable || WithProvider, WrappedComponent);
return FinalComponent;
};
}