-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor victory-portal to remove dependency on react-dom (#2870)
- Loading branch information
1 parent
3a4b911
commit ffc9841
Showing
11 changed files
with
248 additions
and
102 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
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
15 changes: 0 additions & 15 deletions
15
packages/victory-core/src/victory-portal/portal-context.ts
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
packages/victory-core/src/victory-portal/portal-context.tsx
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,63 @@ | ||
import React from "react"; | ||
|
||
export interface PortalContextValue { | ||
addChild: (id: string, node: React.ReactElement) => void; | ||
removeChild: (id: string) => void; | ||
children: Map<string, React.ReactElement>; | ||
} | ||
|
||
export const PortalContext = React.createContext< | ||
PortalContextValue | undefined | ||
>(undefined); | ||
PortalContext.displayName = "PortalContext"; | ||
|
||
export const usePortalContext = () => { | ||
const context = React.useContext(PortalContext); | ||
return context; | ||
}; | ||
|
||
export interface PortalProviderProps { | ||
children?: React.ReactNode; | ||
} | ||
|
||
export const PortalProvider = ({ children }: PortalProviderProps) => { | ||
const [portalChildren, setPortalChildren] = React.useState< | ||
Map<string, React.ReactElement> | ||
>(new Map()); | ||
const addChild = React.useCallback( | ||
(id: string, element: React.ReactElement) => { | ||
setPortalChildren((prevChildren) => { | ||
const nextChildren = new Map(prevChildren); | ||
nextChildren.set(id, element); | ||
return nextChildren; | ||
}); | ||
}, | ||
[setPortalChildren], | ||
); | ||
|
||
const removeChild = React.useCallback( | ||
(id: string) => { | ||
setPortalChildren((prevChildren) => { | ||
const nextChildren = new Map(prevChildren); | ||
nextChildren.delete(id); | ||
return nextChildren; | ||
}); | ||
}, | ||
[setPortalChildren], | ||
); | ||
|
||
const contextValue: PortalContextValue = React.useMemo( | ||
() => ({ | ||
addChild, | ||
removeChild, | ||
children: portalChildren, | ||
}), | ||
[addChild, removeChild, portalChildren], | ||
); | ||
|
||
return ( | ||
<PortalContext.Provider value={contextValue}> | ||
{children} | ||
</PortalContext.Provider> | ||
); | ||
}; |
26 changes: 26 additions & 0 deletions
26
packages/victory-core/src/victory-portal/portal-outlet.tsx
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,26 @@ | ||
import React from "react"; | ||
import { usePortalContext } from "./portal-context"; | ||
|
||
export interface PortalOutletProps { | ||
as: React.ReactElement; | ||
width?: number; | ||
height?: number; | ||
viewBox?: string; | ||
preserveAspectRatio?: string; | ||
style?: React.CSSProperties; | ||
children?: (children: React.ReactElement[]) => React.ReactNode | undefined; | ||
} | ||
|
||
export const PortalOutlet = ({ | ||
as: portalComponent, | ||
...props | ||
}: PortalOutletProps) => { | ||
const portalContext = usePortalContext(); | ||
|
||
if (!portalContext) { | ||
return null; | ||
} | ||
|
||
const children = Array.from(portalContext.children.values()); | ||
return React.cloneElement(portalComponent, props, children); | ||
}; |
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.