-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
342 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@coldwired/react": minor | ||
--- | ||
|
||
lazy load react |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export * from './root'; | ||
export * from './plugin'; | ||
export * from './observable'; | ||
export { hydrate, preload, createReactTree, createState, type State } from './react-tree-builder'; | ||
export { preload } from './preload'; | ||
|
||
export type { State } from './state.react'; | ||
export type { LayoutComponent, ErrorBoundaryFallbackComponent } from './root.react'; | ||
export type { ReactElement, ReactComponent, ReactValue } from './tree-builder.react'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { DocumentFragmentLike, Manifest, Schema } from './tree-builder.react'; | ||
|
||
export const defaultSchema: Schema = { | ||
componentTagName: 'react-component', | ||
slotTagName: 'react-slot', | ||
nameAttribute: 'name', | ||
propsAttribute: 'props', | ||
}; | ||
|
||
export function preload( | ||
documentOrFragment: Document | DocumentFragmentLike, | ||
loader: (names: string[]) => Promise<Manifest>, | ||
schema?: Partial<Schema>, | ||
): Promise<Manifest> { | ||
const { componentTagName, nameAttribute } = Object.assign({}, defaultSchema, schema); | ||
const components = documentOrFragment.querySelectorAll(componentTagName); | ||
const componentNames = new Set( | ||
Array.from(components).map((component) => { | ||
const name = component.getAttribute(nameAttribute); | ||
if (!name) { | ||
throw new Error(`Missing "${nameAttribute}" attribute on <${componentTagName}>`); | ||
} | ||
return name; | ||
}), | ||
); | ||
return loader([...componentNames]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { createPortal } from 'react-dom'; | ||
import { createRoot as createReactRoot } from 'react-dom/client'; | ||
import { | ||
useSyncExternalStore, | ||
useEffect, | ||
StrictMode, | ||
type ReactNode, | ||
type FunctionComponent, | ||
} from 'react'; | ||
import { ErrorBoundary, FallbackProps } from 'react-error-boundary'; | ||
|
||
type LayoutProps = { children: ReactNode }; | ||
export type LayoutComponent = FunctionComponent<LayoutProps>; | ||
type ErrorBoundaryFallbackProps = FallbackProps & { element: Element }; | ||
export type ErrorBoundaryFallbackComponent = FunctionComponent<ErrorBoundaryFallbackProps>; | ||
|
||
export { hydrate } from './tree-builder.react'; | ||
|
||
export function createAndRenderReactRoot({ | ||
container, | ||
subscribe, | ||
getSnapshot, | ||
onMounted, | ||
LayoutComponent, | ||
ErrorBoundaryFallbackComponent, | ||
}: { | ||
container: Element; | ||
subscribe: (callback: () => void) => () => void; | ||
getSnapshot: () => Map<Element, ReactNode>; | ||
onMounted: () => void; | ||
LayoutComponent?: LayoutComponent; | ||
ErrorBoundaryFallbackComponent?: ErrorBoundaryFallbackComponent; | ||
}) { | ||
const props = { | ||
subscribe, | ||
getSnapshot, | ||
onMounted, | ||
ErrorBoundaryFallback: ErrorBoundaryFallbackComponent || DefaultErrorBoundaryFallbackComponent, | ||
}; | ||
const Layout = LayoutComponent || DefaultLayoutComponent; | ||
const root = createReactRoot(container); | ||
root.render( | ||
<Layout> | ||
<RootProvider {...props} /> | ||
</Layout>, | ||
); | ||
return () => root.unmount(); | ||
} | ||
|
||
const DefaultLayoutComponent: LayoutComponent = StrictMode; | ||
const DefaultErrorBoundaryFallbackComponent: ErrorBoundaryFallbackComponent = ({ | ||
error, | ||
element, | ||
}) => { | ||
const message = element.getAttribute('fallback-message') ?? error.message; | ||
return ( | ||
<div role="alert"> | ||
<pre style={{ color: 'red' }}>{message}</pre> | ||
</div> | ||
); | ||
}; | ||
|
||
function RootProvider({ | ||
subscribe, | ||
getSnapshot, | ||
onMounted, | ||
ErrorBoundaryFallback, | ||
}: { | ||
subscribe(callback: () => void): () => void; | ||
getSnapshot(): Map<Element, ReactNode>; | ||
onMounted: () => void; | ||
ErrorBoundaryFallback: ErrorBoundaryFallbackComponent; | ||
}) { | ||
useEffect(onMounted, []); | ||
const cache = useSyncExternalStore(subscribe, getSnapshot); | ||
|
||
return ( | ||
<> | ||
{...Array.from(cache).map(([element, content]) => | ||
createPortal( | ||
<ErrorBoundary | ||
fallbackRender={(props) => <ErrorBoundaryFallback element={element} {...props} />} | ||
> | ||
{content} | ||
</ErrorBoundary>, | ||
element, | ||
getKeyForElement(element), | ||
), | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
const keys = new WeakMap<Element, string>(); | ||
|
||
function getKeyForElement(element: Element): string { | ||
let key = keys.get(element); | ||
if (!key) { | ||
key = Math.random().toString(36).slice(2); | ||
keys.set(element, key); | ||
} | ||
return key; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.