Skip to content

Commit d1ac5b2

Browse files
authored
feat: Extends createWebComponent (#18890)
* feat: Extends createWebComponent Add optional properties to createWebComponent to be able to use it instead of loadComponentScript and createElement when adding properties. Add fucntion hook to listen to onLoad for the webcomponent script. part of #18816 * add missing @param javadocs
1 parent ac62979 commit d1ac5b2

File tree

1 file changed

+17
-3
lines changed
  • flow-server/src/main/resources/com/vaadin/flow/server/frontend

1 file changed

+17
-3
lines changed

flow-server/src/main/resources/com/vaadin/flow/server/frontend/Flow.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,15 @@ export const serverSideRoutes = [
368368
* Load the script for an exported WebComponent with the given tag
369369
*
370370
* @param tag name of the exported web-component to load
371+
* @param onload optional callback to be called for script onload
371372
*/
372-
export const loadComponentScript = (tag: String) => {
373+
export const loadComponentScript = (tag: String, onload?: () => void) => {
373374
useEffect(() => {
374375
const script = document.createElement('script');
375376
script.src = `/web-component/${tag}.js`;
377+
if(onload) {
378+
script.onload = onload;
379+
}
376380
document.head.appendChild(script);
377381

378382
return () => {
@@ -381,12 +385,22 @@ export const loadComponentScript = (tag: String) => {
381385
}, []);
382386
};
383387

388+
interface Properties {
389+
[key: string]: string;
390+
}
391+
384392
/**
385393
* Load WebComponent script and create a React element for the WebComponent.
386394
*
387395
* @param tag custom web-component tag name.
396+
* @param props optional Properties object to create element attributes with
397+
* @param onload optional callback to be called for script onload
388398
*/
389-
export const createWebComponent = (tag: string) => {
390-
loadComponentScript(tag);
399+
export const createWebComponent = (tag: string, props?: Properties, onload?: () => void) => {
400+
loadComponentScript(tag, onload);
401+
if(props) {
402+
return React.createElement(tag, props);
403+
}
391404
return React.createElement(tag);
392405
};
406+

0 commit comments

Comments
 (0)