@@ -105,6 +105,98 @@ def reactjs_component_from_url(
105105 return _vdom_from_web_module (module , import_names , fallback , allow_children )
106106
107107
108+ @overload
109+ def reactjs_component_from_npm (
110+ package : str ,
111+ import_names : str ,
112+ version : str = "latest" ,
113+ file : str = "" ,
114+ cdn : str = "https://esm.sh" ,
115+ fallback : Any | None = ...,
116+ resolve_imports : bool | None = ...,
117+ resolve_imports_depth : int = ...,
118+ unmount_before_update : bool = ...,
119+ allow_children : bool = ...,
120+ ) -> VdomConstructor : ...
121+
122+
123+ @overload
124+ def reactjs_component_from_npm (
125+ package : str ,
126+ import_names : list [str ] | tuple [str , ...],
127+ version : str = "latest" ,
128+ file : str = "" ,
129+ cdn : str = "https://esm.sh" ,
130+ fallback : Any | None = ...,
131+ resolve_imports : bool | None = ...,
132+ resolve_imports_depth : int = ...,
133+ unmount_before_update : bool = ...,
134+ allow_children : bool = ...,
135+ ) -> list [VdomConstructor ]: ...
136+
137+
138+ def reactjs_component_from_npm (
139+ package : str ,
140+ import_names : str | list [str ] | tuple [str , ...],
141+ version : str = "latest" ,
142+ file : str = "" ,
143+ cdn : str = "https://esm.sh" ,
144+ fallback : Any | None = None ,
145+ resolve_imports : bool | None = None ,
146+ resolve_imports_depth : int = 5 ,
147+ unmount_before_update : bool = False ,
148+ allow_children : bool = True ,
149+ ) -> VdomConstructor | list [VdomConstructor ]:
150+ """Import a component from an NPM package.
151+
152+ Parameters:
153+ package:
154+ The name of the NPM package.
155+ import_names:
156+ One or more component names to import. If given as a string, a single component
157+ will be returned. If a list is given, then a list of components will be
158+ returned.
159+ version:
160+ The version of the package to use. Defaults to "latest".
161+ file:
162+ A specific file to import from the package.
163+ cdn:
164+ The CDN to use. Defaults to "https://esm.sh".
165+ fallback:
166+ What to temporarily display while the module is being loaded.
167+ resolve_imports:
168+ Whether to try and find all the named imports of this module.
169+ resolve_imports_depth:
170+ How deeply to search for those imports.
171+ unmount_before_update:
172+ Cause the component to be unmounted before each update. This option should
173+ only be used if the imported package fails to re-render when props change.
174+ Using this option has negative performance consequences since all DOM
175+ elements must be changed on each render. See :issue:`461` for more info.
176+ allow_children:
177+ Whether or not these components can have children.
178+ """
179+ url = f"{ cdn } /{ package } @{ version } "
180+ if file :
181+ url += f"/{ file } "
182+
183+ if "esm.sh" in cdn :
184+ if "?" in url :
185+ url += "&external=react,react-dom"
186+ else :
187+ url += "?external=react,react-dom"
188+
189+ return reactjs_component_from_url (
190+ url ,
191+ import_names ,
192+ fallback = fallback ,
193+ resolve_imports = resolve_imports ,
194+ resolve_imports_depth = resolve_imports_depth ,
195+ unmount_before_update = unmount_before_update ,
196+ allow_children = allow_children ,
197+ )
198+
199+
108200@overload
109201def reactjs_component_from_file (
110202 file : str | Path ,
0 commit comments