Skip to content

Commit 1b8694c

Browse files
authored
feat(HubspotForm): add render outside virtual dom (#138)
1 parent 0eb45b1 commit 1b8694c

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

src/models/constructor-items/sub-blocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export interface HubspotFormProps extends HubspotEventHandlers {
8888
onLoad?: (arg: HubspotEventData) => void;
8989
pixelEvents?: string | string[] | PixelEvent | PixelEvent[] | ButtonPixel;
9090
hubspotEvents?: string[];
91+
createDOMElement?: boolean;
9192
}
9293

9394
//cards

src/sub-blocks/HubspotForm/HubspotFormContainer.tsx

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, {useRef} from 'react';
22

33
import loadHubspotScript from './loadHubspotScript';
44
import {HubspotFormProps} from '../../models';
@@ -10,7 +10,8 @@ type HubspotFormContainerPropsKeys =
1010
| 'formInstanceId'
1111
| 'portalId'
1212
| 'region'
13-
| 'formClassName';
13+
| 'formClassName'
14+
| 'createDOMElement';
1415

1516
type HubspotFormContainerProps = Pick<HubspotFormProps, HubspotFormContainerPropsKeys>;
1617

@@ -23,18 +24,25 @@ const HubspotFormContainer = (props: HubspotFormContainerProps) => {
2324
portalId,
2425
region,
2526
formClassName,
27+
createDOMElement,
2628
} = props;
2729

30+
const containerRef = useRef<HTMLDivElement>(null);
31+
const hsContainerRef = useRef<HTMLDivElement>();
32+
2833
const containerId = formInstanceId
2934
? `hubspot-form-${formId}-${formInstanceId}`
3035
: `hubspot-form-${formId}`;
3136

32-
useMount(() => {
33-
(async () => {
34-
if (!window.hbspt) {
35-
await loadHubspotScript();
36-
}
37+
const createForm = () => {
38+
if (containerRef.current && !hsContainerRef.current && createDOMElement) {
39+
hsContainerRef.current = document.createElement('div');
40+
containerRef.current.id = '';
41+
hsContainerRef.current.id = containerId;
42+
containerRef.current.appendChild(hsContainerRef.current);
43+
}
3744

45+
if (!createDOMElement || hsContainerRef.current) {
3846
if (window.hbspt) {
3947
window.hbspt.forms.create({
4048
region,
@@ -45,10 +53,26 @@ const HubspotFormContainer = (props: HubspotFormContainerProps) => {
4553
formInstanceId,
4654
});
4755
}
56+
}
57+
};
58+
59+
useMount(() => {
60+
(async () => {
61+
if (!window.hbspt) {
62+
await loadHubspotScript();
63+
}
64+
65+
createForm();
4866
})();
67+
68+
return () => {
69+
if (createDOMElement && containerRef.current && containerRef.current.lastChild) {
70+
containerRef.current.removeChild(containerRef.current.lastChild);
71+
}
72+
};
4973
});
5074

51-
return <div className={className} id={containerId} />;
75+
return <div className={className} id={containerId} ref={containerRef} />;
5276
};
5377

5478
export default HubspotFormContainer;

src/sub-blocks/HubspotForm/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
`hubspotEvents: string[]` — An array of hubspot events
2222

23+
`createDOMElement?` - If you put `true`, then a container will be created using `document.createElement` to insert the hubspot form. Default `false`
24+
2325
[Form global events:](https://legacydocs.hubspot.com/global-form-events)
2426

2527
`onBeforeLoadForNonIFrameForm?` - onBeforeFormInit

src/sub-blocks/HubspotForm/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const HubspotForm: React.FunctionComponent<HubspotFormProps> = (props) => {
2929
onSubmit,
3030
onBeforeLoad,
3131
onLoad,
32+
createDOMElement,
3233
onSubmitError,
3334
} = props;
3435

@@ -57,6 +58,7 @@ const HubspotForm: React.FunctionComponent<HubspotFormProps> = (props) => {
5758

5859
return (
5960
<HubspotFormContainer
61+
createDOMElement={createDOMElement}
6062
key={[formClassName, formId, formInstanceId, portalId, region].join()}
6163
className={b({theme, mobile}, className)}
6264
formClassName={formClassName}

0 commit comments

Comments
 (0)