Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Nov 3, 2023
1 parent b9220cd commit 103a637
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 34 deletions.
4 changes: 2 additions & 2 deletions dist/context/scene.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference types="react" />
import type Phaser from 'phaser';
export declare const SceneContext: import("react").Context<Phaser.Scene>;
export declare const SceneProvider: import("react").Provider<Phaser.Scene>;
export declare const SceneContext: import("react").Context<Phaser.Scene | null>;
export declare const SceneProvider: import("react").Provider<Phaser.Scene | null>;
2 changes: 1 addition & 1 deletion dist/hooks/use-match-media.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*
* @param query - Media query
*/
export declare function useMatchMedia(query: string): any;
export declare function useMatchMedia(query: string): boolean | null;
2 changes: 1 addition & 1 deletion dist/hooks/use-relative-position.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import { RelativePositionProps } from '../types/relative-position';
* @param props.y - World position Y
* @param props.camera - Camera
*/
export declare function useRelativePosition<T extends HTMLElement>({ x, y, camera, }: RelativePositionProps): import("react").MutableRefObject<T>;
export declare function useRelativePosition<T extends HTMLElement>({ x, y, camera, }: RelativePositionProps): import("react").RefObject<T>;
2 changes: 1 addition & 1 deletion dist/hooks/use-relative-scale.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ import { RelativeScaleProps } from '../types/relative-scale';
* @param props.max - Max scale
* @param props.round - Rounding
*/
export declare function useRelativeScale<T extends HTMLElement>({ target, min, max, round, }: RelativeScaleProps): import("react").MutableRefObject<T>;
export declare function useRelativeScale<T extends HTMLElement>({ target, min, max, round, }: RelativeScaleProps): import("react").RefObject<T>;
2 changes: 1 addition & 1 deletion dist/hooks/use-texture.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*
* @param key - Texture key
*/
export declare function useTexture(key: string): string;
export declare function useTexture(key: string): string | null;
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/render.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export declare class Interface<T = {}> {
readonly container: HTMLDivElement;
readonly root: Root;
readonly scene: Phaser.Scene;
constructor(scene: Phaser.Scene, Component: React.FC<T>, props?: T);
constructor(scene: Phaser.Scene, Component: React.FC<T | {}>, props?: T);
setInteractive(state: boolean): void;
destroy(): void;
private configureContainer;
Expand Down
8 changes: 4 additions & 4 deletions dist/utils/get-modified.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
* @param target - New array
* @param keys - Keys to compare
*/
export declare function getModifiedArray<T>(current: T[], target: T[], keys?: (keyof T)[]): T[];
export declare function getModifiedArray<T extends {}>(current: T[], target: T[], keys?: (keyof T)[]): T[];
/**
* Return callback for safe update state.
*
* @param target - New array
* @param keys - Keys to compare
*/
export declare function ifModifiedArray<T>(value: T[], keys?: (keyof T)[]): (currentValue: T[]) => T[];
export declare function ifModifiedArray<T extends {}>(value: T[], keys?: (keyof T)[]): (currentValue: T[]) => T[];
/**
* Get modified object between current and target value.
*
* @param current - Current object
* @param target - New object
* @param keys - Keys to compare
*/
export declare function getModifiedObject<T>(current: T, target: T, keys?: (keyof T)[]): T;
export declare function getModifiedObject<T extends {}>(current: T, target: T, keys?: (keyof T)[]): T;
/**
* Return callback for safe update state.
*
* @param target - New object
* @param keys - Keys to compare
*/
export declare function ifModifiedObject<T>(value: T, keys?: (keyof T)[]): (currentValue: T) => T;
export declare function ifModifiedObject<T extends {}>(value: T, keys?: (keyof T)[]): (currentValue: T) => T;
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "phaser-react-ui",
"description": "React interface render for Phaser engine",
"version": "1.14.4",
"version": "1.14.5",
"keywords": [
"phaser",
"interface",
Expand Down
4 changes: 3 additions & 1 deletion src/components/relative-position.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export const RelativePosition: React.FC<Props> = ({ children, ...props }) => {
const ref = useRelativePosition<HTMLDivElement>(props);

useLayoutEffect(() => {
ref.current.style.position = 'absolute';
if (ref.current) {
ref.current.style.position = 'absolute';
}
}, []);

return <div ref={ref}>{children}</div>;
Expand Down
4 changes: 3 additions & 1 deletion src/components/relative-scale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export const RelativeScale: React.FC<Props> = ({ children, ...props }) => {
const ref = useRelativeScale<HTMLDivElement>(props);

useLayoutEffect(() => {
ref.current.style.position = 'absolute';
if (ref.current) {
ref.current.style.position = 'absolute';
}
}, []);

return <div ref={ref}>{children}</div>;
Expand Down
2 changes: 1 addition & 1 deletion src/components/texture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type Props = {
export const Texture: React.FC<Props> = ({ name }) => {
const imageSrc = useTexture(name);

return <img src={imageSrc} role="texture" alt={name} />;
return imageSrc ? <img src={imageSrc} role="texture" alt={name} /> : null;
};
2 changes: 2 additions & 0 deletions src/hooks/use-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ export function useClick(
event = (type === 'up') ? 'mouseup' : 'mousedown';
}

// @ts-ignore
element.addEventListener(event, onClick);

return () => {
// @ts-ignore
element.removeEventListener(event, onClick);
};
}, [type, isMobile, onClick]);
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-match-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useEffect, useState } from 'react';
export function useMatchMedia(
query: string,
) {
const [matched, setMatched] = useState(null);
const [matched, setMatched] = useState<boolean | null>(null);

const onChange = (event: MediaQueryListEvent) => {
setMatched(event.matches);
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/use-relative-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export function useRelativeScale<T extends HTMLElement>({
}

const container = game.canvas.parentElement;

if (!container) {
return;
}

let zoom = container.clientWidth / target;

if (typeof max === 'number') {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { HTMLTextureElement } from '../types/texture';
*
* @param key - Texture key
*/
export function useTexture(key: string) {
export function useTexture(key: string): string | null {
const game = useGame();

return useMemo(() => {
Expand Down
16 changes: 10 additions & 6 deletions src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ export class Interface<T = {}> {

readonly scene: Phaser.Scene;

constructor(scene: Phaser.Scene, Component: React.FC<T>, props?: T) {
constructor(scene: Phaser.Scene, Component: React.FC<T | {}>, props?: T) {
const parent = scene.game.canvas.parentElement;

if (!parent) {
throw Error('Undefined canvas parent element');
}

if (scene.interface) {
console.warn('Scene already had an existing interface');
scene.interface.destroy();
Expand All @@ -25,23 +31,21 @@ export class Interface<T = {}> {
this.configureContainer();
this.setInteractive(false);

const parent = scene.game.canvas.parentElement;

parent.style.position = 'relative';
parent.append(this.container);

const ComponentMiddleware: React.FC<T> = (propsMiddleware: T) => {
const ComponentMiddleware: React.FC = () => {
useEffect(() => {
scene.events.emit(Phaser.Interface.Events.MOUNT);
}, []);

return <Component {...propsMiddleware} />;
return <Component {...(props ?? {})} />;
};

this.root = createRoot(this.container);
this.root.render(
<SceneProvider value={scene}>
<ComponentMiddleware {...props} />
<ComponentMiddleware />
</SceneProvider>,
);

Expand Down
10 changes: 6 additions & 4 deletions src/utils/get-modified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* @param target - New array
* @param keys - Keys to compare
*/
export function getModifiedArray<T>(
export function getModifiedArray<T extends {}>(
current: T[],
target: T[],
keys?: (keyof T)[],
) {
if (!target) {
return current;
}

if (!current) {
return target;
}
Expand Down Expand Up @@ -40,7 +41,7 @@ export function getModifiedArray<T>(
* @param target - New array
* @param keys - Keys to compare
*/
export function ifModifiedArray<T>(value: T[], keys?: (keyof T)[]) {
export function ifModifiedArray<T extends {}>(value: T[], keys?: (keyof T)[]) {
return (currentValue: T[]) => getModifiedArray(currentValue, value, keys);
}

Expand All @@ -51,14 +52,15 @@ export function ifModifiedArray<T>(value: T[], keys?: (keyof T)[]) {
* @param target - New object
* @param keys - Keys to compare
*/
export function getModifiedObject<T>(
export function getModifiedObject<T extends {}>(
current: T,
target: T,
keys?: (keyof T)[],
) {
if (!target) {
return current;
}

if (!current) {
return target;
}
Expand All @@ -80,6 +82,6 @@ export function getModifiedObject<T>(
* @param target - New object
* @param keys - Keys to compare
*/
export function ifModifiedObject<T>(value: T, keys?: (keyof T)[]) {
export function ifModifiedObject<T extends {}>(value: T, keys?: (keyof T)[]) {
return (currentValue: T) => getModifiedObject(currentValue, value, keys);
}
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"alwaysStrict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictBindCallApply": true,
"strict": true,
"declaration": true
},
"include": ["./src/**/*"]
Expand Down

0 comments on commit 103a637

Please sign in to comment.