Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: support multi window apps when detecting if target is <canvas> element #584

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/core/createRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
import { reconciler } from './reconciler';
import { roots } from './roots';

function getIsCanvas(target: HTMLElement) {

Check warning on line 16 in src/core/createRoot.tsx

View workflow job for this annotation

GitHub Actions / Verify (Lint, test:lint, false)

Opening curly brace appears on the same line as controlling statement

Check warning on line 16 in src/core/createRoot.tsx

View workflow job for this annotation

GitHub Actions / Verify (Lint, test:lint, false)

Opening curly brace appears on the same line as controlling statement
if (target instanceof HTMLCanvasElement) return true;

// It is possible that the app is rendered inside another window via react portals.
// If this is the case, it is owned by a different document, so will be instance of otherDocument.defaultView.HTMLCanvasElement instead of simply HTMLCanvasElement.
// (defaultView is the window object of the other document)
const ownerWindowHTMLCanvasElement = target?.ownerDocument?.defaultView?.HTMLCanvasElement;

if (!ownerWindowHTMLCanvasElement) return false;

return target instanceof ownerWindowHTMLCanvasElement;
}

/** Creates a new root for a Pixi React app. */
export function createRoot(
/** @description The DOM node which will serve as the root for this tree. */
Expand Down Expand Up @@ -58,7 +71,7 @@
{
let canvas;

if (target instanceof HTMLCanvasElement)
if (getIsCanvas(target))
{
canvas = target;
}
Expand Down
Loading