-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the
<Application>
wrapper component
- Loading branch information
Showing
7 changed files
with
101 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
createElement, | ||
useEffect, | ||
useRef, | ||
} from 'react'; | ||
import { render } from '../render.js'; | ||
|
||
/** @typedef {import('pixi.js').Application} Application */ | ||
/** @typedef {import('pixi.js').ApplicationOptions} ApplicationOptions */ | ||
/** | ||
* @template T | ||
* @typedef {import('react').PropsWithChildren<T>} PropsWithChildren | ||
*/ | ||
/** | ||
* @template T | ||
* @typedef {import('react').PropsWithRef<T>} PropsWithRef | ||
*/ | ||
/** | ||
* @template T | ||
* @typedef {import('react').RefObject<T>} RefObject | ||
*/ | ||
|
||
/** | ||
* @template T | ||
* @typedef {import('../typedefs/OmitChildren.js').OmitChildren<T>} OmitChildren | ||
*/ | ||
|
||
/** | ||
* @typedef {object} BaseApplicationProps | ||
* @property {string} [className] CSS classes to be applied to the Pixi Application's canvas element. | ||
*/ | ||
|
||
/** @typedef {PropsWithChildren<Partial<OmitChildren<ApplicationOptions>>>} ApplicationPropsWithChildren */ | ||
/** @typedef {PropsWithRef<{ ref?: RefObject<Application> }>} ApplicationPropsWithRef */ | ||
/** @typedef {BaseApplicationProps & ApplicationPropsWithChildren & ApplicationPropsWithRef} ApplicationProps */ | ||
|
||
/** | ||
* Creates a React root and renders a Pixi application. | ||
* | ||
* @param {ApplicationProps} props All props. | ||
*/ | ||
export function Application(props) | ||
{ | ||
const { | ||
children, | ||
className, | ||
...applicationProps | ||
} = props; | ||
|
||
/** @type {RefObject<HTMLCanvasElement>} */ | ||
const canvasRef = useRef(null); | ||
|
||
useEffect(() => | ||
{ | ||
const canvasElement = canvasRef.current; | ||
|
||
if (canvasElement) | ||
{ | ||
render(children, canvasElement, applicationProps); | ||
} | ||
}, []); | ||
|
||
return createElement('canvas', { | ||
ref: canvasRef, | ||
className, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* @template T | ||
* @typedef {T extends undefined ? never : Omit<T, 'children'>} OmitChildren | ||
*/ | ||
export const OmitChildren = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters