Skip to content

Commit 49c9dd9

Browse files
committed
Add options parameter to createPortalNode
1 parent 7cba832 commit 49c9dd9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/index.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const ELEMENT_TYPE_SVG = 'svg';
77

88
type ANY_ELEMENT_TYPE = typeof ELEMENT_TYPE_HTML | typeof ELEMENT_TYPE_SVG;
99

10+
type Options = {
11+
attributes: { [key: string]: string };
12+
};
13+
1014
// ReactDOM can handle several different namespaces, but they're not exported publicly
1115
// https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/shared/DOMNamespaces.js#L8-L10
1216
const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
@@ -50,7 +54,10 @@ const validateElementType = (domElement: Element, elementType: ANY_ELEMENT_TYPE)
5054
};
5155

5256
// This is the internal implementation: the public entry points set elementType to an appropriate value
53-
const createPortalNode = <C extends Component<any>>(elementType: ANY_ELEMENT_TYPE): AnyPortalNode<C> => {
57+
const createPortalNode = <C extends Component<any>>(
58+
elementType: ANY_ELEMENT_TYPE,
59+
options?: Options
60+
): AnyPortalNode<C> => {
5461
let initialProps = {} as ComponentProps<C>;
5562

5663
let parent: Node | undefined;
@@ -65,6 +72,12 @@ const createPortalNode = <C extends Component<any>>(elementType: ANY_ELEMENT_TYP
6572
throw new Error(`Invalid element type "${elementType}" for createPortalNode: must be "html" or "svg".`);
6673
}
6774

75+
if (options && typeof options === "object") {
76+
for (const [key, value] of Object.entries(options?.attributes)) {
77+
element.setAttribute(key, value);
78+
}
79+
}
80+
6881
const portalNode: AnyPortalNode<C> = {
6982
element,
7083
elementType,

0 commit comments

Comments
 (0)