Skip to content

Commit 8490ecb

Browse files
authored
Merge pull request #529 from pixijs/add-extensions-support
Add support for managing `extensions`
2 parents e808f22 + 2e79fc0 commit 8490ecb

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,15 @@ Setting `attachToDevtools` to `true` will automatically attach the application t
126126

127127
###### `defaultTextStyle`
128128

129-
`defaultTextStyle` is a convenience property. Whatever is passed will automatically be assigned to Pixi.js's[`TextStyle.defaultTextStyle`](https://pixijs.download/release/docs/text.TextStyle.html#defaultTextStyle).
129+
`defaultTextStyle` is a convenience property. Whatever is passed will automatically be assigned to Pixi.js's [`TextStyle.defaultTextStyle`](https://pixijs.download/release/docs/text.TextStyle.html#defaultTextStyle).
130130

131131
> [!NOTE]
132132
> This property **is not retroactive**. It will only apply to text components created after `defaultTextStyle` is set. Any text components created before setting `defaultTextStyle` will retain the base styles they had before `defaultTextStyle` was changed.
133133
134+
###### `extensions`
135+
136+
`extensions` is an array of extensions to be loaded. Adding and removing items from this array will automatically load/unload the extensions. The first time this is handled happens before the application is initialised. See Pixi.js's [`extensions`](https://pixijs.download/release/docs/extensions.html) documentation for more info on extensions.
137+
134138
###### `resizeTo`
135139

136140
The `<Application>` component supports the `resizeTo` property, with some additional functionality: it can accept any HTML element **or** it can take a React `ref` directly.

src/components/Application.ts

+42-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { TextStyle } from 'pixi.js';
1+
import {
2+
extensions as PixiExtensions,
3+
TextStyle,
4+
} from 'pixi.js';
25
import {
36
createElement,
47
forwardRef,
8+
type ForwardRefRenderFunction,
9+
type MutableRefObject,
510
useCallback,
611
useRef,
712
} from 'react';
813
import { createRoot } from '../core/createRoot';
914
import { useIsomorphicLayoutEffect } from '../hooks/useIsomorphicLayoutEffect';
15+
import { type ApplicationProps } from '../typedefs/ApplicationProps';
16+
import { type Root } from '../typedefs/Root';
1017

1118
import type { Application as PixiApplication } from 'pixi.js';
12-
import type {
13-
ForwardRefRenderFunction,
14-
MutableRefObject,
15-
} from 'react';
16-
import type { ApplicationProps } from '../typedefs/ApplicationProps';
17-
import type { Root } from '../typedefs/Root';
1819

1920
const originalDefaultTextStyle = { ...TextStyle.defaultTextStyle };
2021

@@ -29,13 +30,15 @@ export const ApplicationFunction: ForwardRefRenderFunction<PixiApplication, Appl
2930
children,
3031
className,
3132
defaultTextStyle,
33+
extensions,
3234
onInit,
3335
resizeTo,
3436
...applicationProps
3537
} = props;
3638

3739
const applicationRef: MutableRefObject<PixiApplication | null> = useRef(null);
3840
const canvasRef: MutableRefObject<HTMLCanvasElement | null> = useRef(null);
41+
const extensionsRef: MutableRefObject<Set<any>> = useRef(new Set());
3942
const rootRef: MutableRefObject<Root | null> = useRef(null);
4043

4144
const updateResizeTo = useCallback(() =>
@@ -95,6 +98,38 @@ export const ApplicationFunction: ForwardRefRenderFunction<PixiApplication, Appl
9598
}
9699
}, [onInit]);
97100

101+
useIsomorphicLayoutEffect(() =>
102+
{
103+
if (extensions)
104+
{
105+
const extensionsToHandle = [...extensions];
106+
const extensionsState = extensionsRef.current;
107+
108+
// Check for extensions that have been removed from the array
109+
for (const extension of extensionsState.values())
110+
{
111+
const extensionIndex = extensionsToHandle.indexOf(extension);
112+
113+
// If the extension is no longer in the array, we'll remove it from Pixi.js
114+
if (extensionIndex === -1)
115+
{
116+
PixiExtensions.remove(extension);
117+
extensionsState.delete(extension);
118+
}
119+
120+
// Since the extension already existed in the state, we can remove it to prevent any further handling
121+
extensionsToHandle.splice(extensionIndex, 1);
122+
}
123+
124+
// Load any remaining extensions.
125+
for (const extension in extensionsToHandle)
126+
{
127+
PixiExtensions.add(extension);
128+
extensionsState.add(extension);
129+
}
130+
}
131+
}, [extensions]);
132+
98133
useIsomorphicLayoutEffect(() =>
99134
{
100135
const canvasElement = canvasRef.current;

src/typedefs/ApplicationProps.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
Application,
33
ApplicationOptions,
4+
ExtensionFormatLoose,
45
TextStyle,
56
TextStyleOptions,
67
} from 'pixi.js';
@@ -24,6 +25,9 @@ export interface BaseApplicationProps
2425
/** @description The default style to be applied to text nodes. */
2526
defaultTextStyle?: TextStyle | TextStyleOptions,
2627

28+
/** @description An array of Pixi extensions to be loaded before initialisation. */
29+
extensions?: (ExtensionFormatLoose | any)[],
30+
2731
/** @description A unique key which allows React to manage this component across changes in parent state. */
2832
key?: Key,
2933

0 commit comments

Comments
 (0)