Skip to content

Commit

Permalink
fix: when creating root, check if canvas is rendered into another window
Browse files Browse the repository at this point in the history
  • Loading branch information
pie6k authored Jul 26, 2024
1 parent f32a0d6 commit a66a207
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/createRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ContextProvider } from '../components/Context.ts';
import { isReadOnlyProperty } from '../helpers/isReadOnlyProperty.ts';
import { log } from '../helpers/log.ts';
import { prepareInstance } from '../helpers/prepareInstance.ts';
import { getIsCanvasElement } from '../helpers/canvas.ts';
import { reconciler } from './reconciler.ts';
import { roots } from './roots.ts';

Expand Down Expand Up @@ -50,7 +51,7 @@ export function createRoot(
{
let canvas;

if (target instanceof HTMLCanvasElement)
if (getIsCanvasElement(target))
{
canvas = target;
}
Expand Down
15 changes: 15 additions & 0 deletions src/helpers/canvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function getIsCanvasElement(target: HTMLElement): target is HTMLCanvasElement {
if (target instanceof HTMLCanvasElement) return true;

/**
* In case Application is rendered into another window (created with window.open() and rendered into it using React portal)
* canvas is instance of openedWindow.HTMLCanvasElement, not global window.HTMLCanvasElement
*
* Thus we need to check for that.
*/
const OwnerWindowHTMLCanvasElement = target?.ownerDocument?.defaultView?.HTMLCanvasElement;

if (!OwnerWindowHTMLCanvasElement) return false;

return target instanceof OwnerWindowHTMLCanvasElement;
}

0 comments on commit a66a207

Please sign in to comment.