Skip to content

Commit 337994b

Browse files
committed
Add withPIXIApp higher order component
1 parent 2a696ab commit 337994b

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { TYPES, PixiComponent } from './utils/element'
22
import { render } from './render'
33
import Stage from './stage'
4-
import Provider from './stage/provider'
4+
import { Provider, withPixiApp } from './stage/provider'
55

66
/**
77
* -------------------------------------------
88
* Public API
99
* -------------------------------------------
1010
*/
1111

12-
export { render, Stage, Provider, PixiComponent }
12+
export { render, Stage, Provider, withPixiApp, PixiComponent }
1313

1414
export const BitmapText = TYPES.BitmapText
1515
export const Container = TYPES.Container

src/stage/provider.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
33

4+
/**
5+
* Provider for exposing the PIXI.Application
6+
*
7+
* @param {Function} children
8+
* @param {Object} app from context
9+
* @returns {React.Component}
10+
* @usage:
11+
*
12+
* const App = () => (
13+
* <Stage>
14+
* <Provider>
15+
* {app => <MyComponent app={app} />}
16+
* </Provider>
17+
* </Stage>
18+
* )
19+
*
20+
*/
421
const Provider = ({ children }, { app }) => children(app)
522
Provider.contextTypes = { app: PropTypes.object }
623
Provider.propTypes = { children: PropTypes.func }
724

8-
export default Provider
25+
/**
26+
* Or as a Higher Order Component
27+
*
28+
* @param {React.Component|Function} BaseComponent
29+
* @returns {React.Component|Function}
30+
* @usage
31+
*
32+
* const App = withPIXIApp(({ app ) => ())
33+
*
34+
*/
35+
const withPixiApp = BaseComponent => {
36+
class WithPIXIApp extends React.Component {
37+
render() {
38+
return <BaseComponent {...this.props} app={this.context.app} />
39+
}
40+
}
41+
42+
WithPIXIApp.contextTypes = { app: PropTypes.object }
43+
return WithPIXIApp
44+
}
45+
46+
export { withPixiApp, Provider }

test/__snapshots__/index.spec.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ Object {
1717
"Text": "Text",
1818
"TilingSprite": "TilingSprite",
1919
"render": [Function],
20+
"withPixiApp": [Function],
2021
}
2122
`;

test/stage.spec.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as PIXI from 'pixi.js'
33
import renderer from 'react-test-renderer'
44
import { PixiFiber, PACKAGE_NAME, VERSION } from '../src/reconciler'
55
import { runningInBrowser } from '../src/helpers'
6-
import { Stage, Provider, Container, Text } from '../src'
6+
import { Stage, Provider, withPixiApp, Container, Text } from '../src'
77
import { getCanvasProps } from '../src/stage'
88
import { mockToSpy } from './__utils__/mock'
99

@@ -240,7 +240,26 @@ describe('stage', () => {
240240

241241
const instance = el.getInstance()
242242

243-
expect(fn).toHaveBeenCalled()
243+
expect(fn).toHaveBeenCalledTimes(1)
244+
expect(fn).toHaveBeenCalledWith(instance.app)
245+
})
246+
})
247+
248+
describe('provider as a higher order component', () => {
249+
test('pass down app to child component', () => {
250+
const fn = jest.fn(() => <Container />)
251+
const Comp = withPixiApp(({ app }) => fn(app))
252+
253+
const el = renderer.create(
254+
<Stage>
255+
<Container>
256+
<Comp />
257+
</Container>
258+
</Stage>
259+
)
260+
261+
const instance = el.getInstance()
262+
244263
expect(fn).toHaveBeenCalledTimes(1)
245264
expect(fn).toHaveBeenCalledWith(instance.app)
246265
})

0 commit comments

Comments
 (0)