@@ -7,6 +7,10 @@ const ELEMENT_TYPE_SVG = 'svg';
7
7
8
8
type ANY_ELEMENT_TYPE = typeof ELEMENT_TYPE_HTML | typeof ELEMENT_TYPE_SVG ;
9
9
10
+ type Options = {
11
+ attributes : { [ key : string ] : string } ;
12
+ } ;
13
+
10
14
// ReactDOM can handle several different namespaces, but they're not exported publicly
11
15
// https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/shared/DOMNamespaces.js#L8-L10
12
16
const SVG_NAMESPACE = 'http://www.w3.org/2000/svg' ;
@@ -50,7 +54,10 @@ const validateElementType = (domElement: Element, elementType: ANY_ELEMENT_TYPE)
50
54
} ;
51
55
52
56
// 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 > => {
54
61
let initialProps = { } as ComponentProps < C > ;
55
62
56
63
let parent : Node | undefined ;
@@ -65,6 +72,12 @@ const createPortalNode = <C extends Component<any>>(elementType: ANY_ELEMENT_TYP
65
72
throw new Error ( `Invalid element type "${ elementType } " for createPortalNode: must be "html" or "svg".` ) ;
66
73
}
67
74
75
+ if ( options && typeof options === "object" ) {
76
+ for ( const [ key , value ] of Object . entries ( options ?. attributes ) ) {
77
+ element . setAttribute ( key , value ) ;
78
+ }
79
+ }
80
+
68
81
const portalNode : AnyPortalNode < C > = {
69
82
element,
70
83
elementType,
0 commit comments