Skip to content

Commit

Permalink
Adds support for async react renders for admin extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-marcucci committed Jan 26, 2024
1 parent a1a9a88 commit cf51fb0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-spiders-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/ui-extensions-react': patch
---

Adds support for async render functions for Admin extensions
12 changes: 8 additions & 4 deletions packages/ui-extensions-react/src/surfaces/admin/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ import {ExtensionApiContext} from './context';
*/
export function reactExtension<ExtensionTarget extends RenderExtensionTarget>(
target: ExtensionTarget,
render: (api: ApiForRenderExtension<ExtensionTarget>) => ReactElement<any>,
render: (
api: ApiForRenderExtension<ExtensionTarget>,
) => Promise<ReactElement<any>> | ReactElement<any>,
): ExtensionTargets[ExtensionTarget] {
// TypeScript can’t infer the type of the callback because it’s a big union
// type. To get around it, we’ll just fake like we are rendering the
// Playground extension, since all render extensions have the same general
// shape (`RenderExtension`).
return extension<'Playground'>(target as any, (root, api) => {
return new Promise((resolve, reject) => {
return extension<'Playground'>(target as any, async (root, api) => {
const element = await render(api as ApiForRenderExtension<ExtensionTarget>);

await new Promise<void>((resolve, reject) => {
try {
remoteRender(
<ExtensionApiContext.Provider value={api}>
{render(api as ApiForRenderExtension<ExtensionTarget>)}
{element}
</ExtensionApiContext.Provider>,
root,
() => {
Expand Down

0 comments on commit cf51fb0

Please sign in to comment.