-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-react): Fix issue with useCustomPages when making changes o…
…n the custom pages in dev
- Loading branch information
Showing
3 changed files
with
97 additions
and
42 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
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,24 +1,36 @@ | ||
import React, { useState } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
|
||
export type UseCustomElementPortalParams = { | ||
component: React.ReactNode; | ||
id: number; | ||
}; | ||
|
||
export type UseCustomElementPortalReturn = { | ||
portal: () => JSX.Element; | ||
mount: (node: Element) => void; | ||
unmount: () => void; | ||
id: number; | ||
}; | ||
|
||
// This function takes a component as prop, and returns functions that mount and unmount | ||
// the given component into a given node | ||
export const useCustomElementPortal = (elements: UseCustomElementPortalParams[]) => { | ||
const [nodes, setNodes] = useState<(Element | null)[]>(Array(elements.length).fill(null)); | ||
|
||
export const useCustomElementPortal = (component: JSX.Element) => { | ||
const [node, setNode] = useState<Element | null>(null); | ||
|
||
const mount = (node: Element) => { | ||
setNode(node); | ||
}; | ||
const unmount = () => { | ||
setNode(null); | ||
}; | ||
const portals: UseCustomElementPortalReturn[] = []; | ||
|
||
// If mount has been called, CustomElementPortal returns a portal that renders `component` | ||
// into the passed node | ||
elements.forEach((el, index) => { | ||
const mount = (node: Element) => { | ||
setNodes(prevState => prevState.map((n, i) => (i === index ? node : n))); | ||
}; | ||
const unmount = () => { | ||
setNodes(prevState => prevState.map((n, i) => (i === index ? null : n))); | ||
}; | ||
|
||
// Otherwise, CustomElementPortal returns nothing | ||
const CustomElementPortal = () => <>{node ? createPortal(component, node) : null}</>; | ||
const portal = () => <>{nodes[index] ? createPortal(el.component, nodes[index] as Element) : null}</>; | ||
portals.push({ portal, mount, unmount, id: el.id }); | ||
}); | ||
|
||
return { CustomElementPortal, mount, unmount }; | ||
return portals; | ||
}; |
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