Skip to content

Commit 597f40c

Browse files
authored
Merge pull request #24 from switchnollie/controller-as-context
Controller as context
2 parents 963bd3b + 21051b8 commit 597f40c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/Controller.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export type ControllerState = {
1616
controller: ?any,
1717
}
1818

19+
const ControllerContext = React.createContext(null);
20+
1921
class Controller extends React.Component<ControllerProps, ControllerState> {
2022
controller: any;
2123

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

45-
return React.Children.map(children, (child) => {
46-
if (child.type.displayName !== 'Scene') {
47-
return child;
48-
}
49-
const props = {...child.props, controller};
50-
return <child.type {...props} />;
51-
});
47+
return (
48+
<ControllerContext.Provider value={controller}>
49+
{children}
50+
</ControllerContext.Provider>
51+
);
5252
}
5353
}
5454

55-
export { Controller };
55+
export { Controller, ControllerContext };

src/Scene.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22
import { default as React } from 'react';
3+
import { ControllerContext } from './Controller';
34
import ScrollMagic from './lib/scrollmagic';
45
import debugAddIndicators from './lib/debug.addIndicators.js';
56

@@ -228,7 +229,7 @@ class SceneBase extends React.PureComponent<SceneBaseProps, SceneBaseState> {
228229
}
229230
}
230231

231-
class Scene extends React.PureComponent<SceneProps, {}> {
232+
class SceneWrapper extends React.PureComponent<SceneProps, {}> {
232233
static displayName = 'Scene';
233234

234235
render() {
@@ -246,4 +247,12 @@ class Scene extends React.PureComponent<SceneProps, {}> {
246247
}
247248
}
248249

249-
export { Scene };
250+
export const Scene = ({ children, ...props }) => (
251+
<ControllerContext.Consumer>
252+
{controller => (
253+
<SceneWrapper controller={controller} {...props}>
254+
{children}
255+
</SceneWrapper>
256+
)}
257+
</ControllerContext.Consumer>
258+
);

0 commit comments

Comments
 (0)