Skip to content

Controller as context #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
);