Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(www): Hubspot form UI #13080

Merged
merged 3 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"react-dom": "^16.4.1",
"react-helmet": "^5.2.0",
"react-highcharts": "^16.0.2",
"react-hubspot-form": "^1.3.5",
"react-icons": "^2.2.7",
"react-instantsearch": "^4.5.1",
"react-modal": "^3.4.4",
Expand Down
5 changes: 2 additions & 3 deletions www/src/components/hubspot-form.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { Component } from "react"
import HubspotForm from "react-hubspot-form"
import HubspotForm from "./react-hubspot-form"
import hex2rgba from "hex2rgba"

import { colors, radii, space, scale } from "../utils/presets"
import { formInput } from "../utils/styles"
import { buttonStyles } from "../utils/styles"
import { formInput, buttonStyles } from "../utils/styles"

export default class GatsbyHubspotForm extends Component {
render() {
Expand Down
108 changes: 108 additions & 0 deletions www/src/components/react-hubspot-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// this is a copy of [email protected], patched
// to work around a problem with emotion's `css` prop —
// see the comment starting with "patch" in this file
//
// react-hubspot-form allows passing options to HubSpot
// via props https://github.com/escaladesports/react-hubspot-form#options
//
// we want to set the `css` option to an empty string to bypass
// loading HubSpot's default form CSS, see
// https://developers.hubspot.com/docs/methods/forms/advanced_form_options
//
// to bypass emotion, we used to spread the prop
// which used to work:
// https://github.com/gatsbyjs/gatsby/blob/bb43fe0c926f2b5d06fb6e9fa3a057ad5d3a685d/www/src/components/hubspot-form.js#L64
// … but now it doesn't anymore

import React from "react"

let globalId = 0

class HubspotForm extends React.Component {
constructor(props) {
super(props)
this.state = {
loaded: false,
}
this.id = globalId++
this.createForm = this.createForm.bind(this)
this.findFormElement = this.findFormElement.bind(this)
}
createForm() {
if (window.hbspt) {
// protect against component unmounting before window.hbspt is available
if (this.el === null) {
return
}
let props = {
...this.props,
}
delete props.loading
delete props.onSubmit
delete props.onReady
let options = {
...props,
// patch: disable HubSpot's default CSS
css: ``,
target: `#${this.el.getAttribute(`id`)}`,
onFormSubmit: $form => {
// ref: https://developers.hubspot.com/docs/methods/forms/advanced_form_options
var formData = $form.serializeArray()
this.props.onSubmit(formData)
},
}
window.hbspt.forms.create(options)
return true
} else {
setTimeout(this.createForm, 1)
}
}
loadScript() {
let script = document.createElement(`script`)
script.defer = true
script.onload = () => {
this.createForm()
this.findFormElement()
}
script.src = `//js.hsforms.net/forms/v2.js`
document.head.appendChild(script)
}
findFormElement() {
// protect against component unmounting before form is added
if (this.el === null) {
return
}
let form = this.el.querySelector(`iframe`)
if (form) {
this.setState({ loaded: true })
if (this.props.onReady) {
this.props.onReady(form)
}
} else {
setTimeout(this.findFormElement, 1)
}
}
componentDidMount() {
if (!window.hbspt && !this.props.noScript) {
this.loadScript()
} else {
this.createForm()
this.findFormElement()
}
}
componentWillUnmount() {}
render() {
return (
<div>
<div
ref={el => (this.el = el)}
id={`reactHubspotForm${this.id}`}
style={{ display: this.state.loaded ? `block` : `none` }}
/>
{!this.state.loaded && this.props.loading}
</div>
)
}
}

export default HubspotForm
4 changes: 2 additions & 2 deletions www/src/utils/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ export const formInput = {
border: `1px solid ${colors.ui.bright}`,
borderRadius: radii[1],
color: colors.brand,
fontFamily: fonts.header,
padding: space[3],
padding: space[2],
fontSize: scale[2],
verticalAlign: `middle`,
transition: `all ${transition.speed.default} ${transition.curve.default}`,
"::placeholder": {
Expand Down