Skip to content

Instantly share code, notes, and snippets.

@fernandocamargo
Created August 10, 2023 00:09
Show Gist options
  • Save fernandocamargo/d0114fdd16b3f192c8490aad8119c674 to your computer and use it in GitHub Desktop.
Save fernandocamargo/d0114fdd16b3f192c8490aad8119c674 to your computer and use it in GitHub Desktop.
import { Suspense, createElement, forwardRef, lazy } from 'react';
import { css } from '@/tools/emotion';
export const enhance = ([{ default: render }, { default: style }]) => {
const className = css({ name: render.hash }, style);
const component = (props, ref) =>
createElement(forwardRef(Object.assign(render, { displayName: '✨' })), {
...props,
className,
ref
});
return { default: forwardRef(Object.assign(component, { displayName: '💅' })) };
};
export const require = () => [import('./render'), import('./style')];
export const load = () => Promise.all(require()).then(enhance);
export const Lazy = lazy(load);
export const fallback = 'Loading...';
export const Core = (props, ref) => {
const children = createElement(Lazy, { ...props, ref });
return createElement(Suspense, { fallback }, children);
};
export default forwardRef(Core);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment