create-react-signals
Version:
A factory function to create signals for React
51 lines (50 loc) • 1.8 kB
JavaScript
/* eslint @typescript-eslint/no-explicit-any: off */
import { setValueForStyles } from './vendor/react-dom.js';
const applyPropsDOM = (instance, props) => {
Object.entries(props).forEach(([key, value]) => {
if (key === 'children') {
instance.textContent = value;
}
else if (key === 'style') {
setValueForStyles(instance, Array.isArray(value) ? Object.assign({}, ...value) : value);
}
else {
instance[key] = value;
}
});
};
const applyPropsR3F = (instance, props) => {
Object.entries(props).forEach(([key, value]) => {
var _a, _b, _c;
if (((_a = instance[key]) === null || _a === void 0 ? void 0 : _a.fromArray) && Array.isArray(value)) {
instance[key].fromArray(value);
}
else if ((_b = instance[key]) === null || _b === void 0 ? void 0 : _b.set) {
instance[key].set(...(Array.isArray(value) ? value : [value]));
}
else if (((_c = instance[key]) === null || _c === void 0 ? void 0 : _c.copy) &&
(value === null || value === void 0 ? void 0 : value.constructor) &&
instance[key].constructor.name === value.constructor.name) {
instance[key].copy(value);
}
else {
instance[key] = value;
}
});
};
export const applyProps = (instance, props) => {
let fn;
if (typeof __CREATE_REACT_SIGNALS_ATTACH_PROPS === 'function') {
fn = __CREATE_REACT_SIGNALS_ATTACH_PROPS;
}
else if (typeof Element !== 'undefined' && instance instanceof Element) {
fn = applyPropsDOM;
}
else if (instance.__r3f) {
fn = applyPropsR3F;
}
else {
throw new Error('Cannot detect renderer type');
}
fn(instance, props);
};