Skip to content

Commit

Permalink
Merge pull request #24 from switchnollie/controller-as-context
Browse files Browse the repository at this point in the history
Controller as context
  • Loading branch information
bitworking authored Mar 26, 2021
2 parents 963bd3b + 21051b8 commit 597f40c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type ControllerState = {
controller: ?any,
}

const ControllerContext = React.createContext(null);

class Controller extends React.Component<ControllerProps, ControllerState> {
controller: any;

Expand All @@ -42,14 +44,12 @@ class Controller extends React.Component<ControllerProps, ControllerState> {
return children;
}

return React.Children.map(children, (child) => {
if (child.type.displayName !== 'Scene') {
return child;
}
const props = {...child.props, controller};
return <child.type {...props} />;
});
return (
<ControllerContext.Provider value={controller}>
{children}
</ControllerContext.Provider>
);
}
}

export { Controller };
export { Controller, ControllerContext };
13 changes: 11 additions & 2 deletions src/Scene.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import { default as React } from 'react';
import { ControllerContext } from './Controller';
import ScrollMagic from './lib/scrollmagic';
import debugAddIndicators from './lib/debug.addIndicators.js';

Expand Down Expand Up @@ -228,7 +229,7 @@ class SceneBase extends React.PureComponent<SceneBaseProps, SceneBaseState> {
}
}

class Scene extends React.PureComponent<SceneProps, {}> {
class SceneWrapper extends React.PureComponent<SceneProps, {}> {
static displayName = 'Scene';

render() {
Expand All @@ -246,4 +247,12 @@ class Scene extends React.PureComponent<SceneProps, {}> {
}
}

export { Scene };
export const Scene = ({ children, ...props }) => (
<ControllerContext.Consumer>
{controller => (
<SceneWrapper controller={controller} {...props}>
{children}
</SceneWrapper>
)}
</ControllerContext.Consumer>
);

0 comments on commit 597f40c

Please sign in to comment.