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

Apply React polyfill directly to globalThis #492

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/sharp-frogs-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remote-dom/react': patch
---

Apply React polyfill directly to `globalThis`
50 changes: 27 additions & 23 deletions packages/react/source/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import {window} from '@remote-dom/core/polyfill';

class HTMLIFrameElement extends HTMLElement {}

// React checks whether elements are Iframes on initialization.
defineGlobalProperty('HTMLIFrameElement', {
value: HTMLIFrameElement,
configurable: true,
});

// React ues the `location` and `navigator` properties when printing help text in
// development, and the `Window` polyfill from Remote DOM doesn’t define these properties.
// We copy their implementation from the existing global scope when it exists, and
// provide a minimal working implementation otherwise.
defineGlobalProperty('location', {
value: globalThis.location ?? {protocol: 'https:'},
configurable: true,
});

defineGlobalProperty('navigator', {
value: globalThis.navigator ?? {userAgent: ''},
configurable: true,
});

class CSSStyleDeclaration {
getPropertyValue(_key: string): string | null | undefined {
return undefined;
Expand All @@ -24,27 +42,6 @@ class CSSStyleDeclaration {
}
}

// React ues the `location` property when printing help text in development.
if (!window.location) {
Object.defineProperty(window, 'location', {
value: globalThis.location ?? {protocol: 'https:'},
configurable: true,
});
}

if (!window.navigator) {
Object.defineProperty(window, 'navigator', {
value: globalThis.navigator ?? {userAgent: ''},
configurable: true,
});
}

// React checks whether elements are Iframes on initialization.
Object.defineProperty(window, 'HTMLIFrameElement', {
value: HTMLIFrameElement,
configurable: true,
});

// React checks for a few properties in `document.createElement('div').style`
const STYLE = Symbol('style');
Object.defineProperty(Element.prototype, 'style', {
Expand All @@ -61,3 +58,10 @@ Object.defineProperty(Element.prototype, 'style', {
this.style.cssText = String(cssText);
},
});

function defineGlobalProperty(name: string, descriptor: PropertyDescriptor) {
Object.defineProperty(window, name, descriptor);
Object.defineProperty(globalThis, name, descriptor);
}

export {};
Loading