Code Monkey home page Code Monkey logo

Comments (2)

ttstauss avatar ttstauss commented on June 12, 2024

I think this was due to an environment issue (embedding it in Nextjs). I solved it with the following (for anyone who may stumble upon this), took inspiration from here: https://github.com/rottitime/react-hook-window-message-event:

export default function FormBlock({attributes}: Props): JSX.Element {
  const {data}: {data?: string} = attributes
  let formData: FormData = {}
  if (data !== undefined) {
    formData = JSON.parse(data)
  }

  const [height, setHeight] = useState(500)
  const [width, setWidth] = useState('100%')

  const onWatchEventHandler = useCallback(
    ({origin, data}) => {
      const {message, height, index, width} = data
      if (
        origin === process.env.NEXT_PUBLIC_GRAVITY_FORMS_URL &&
        message === 'size' &&
        index === formData?.gravity_form_id
      ) {
        setHeight(height)
        setWidth(width)
      }
    },
    [formData?.gravity_form_id],
  )

  useEffect(() => {
    window.addEventListener('message', onWatchEventHandler)
    return () => window.removeEventListener('message', onWatchEventHandler)
  })

  return (
    <>
      <div className="max-w-4xl mx-auto">
        {formData?.gravity_form_id && (
          <iframe
            id="gfiframe"
            src={`${process.env.NEXT_PUBLIC_GRAVITY_FORMS_URL}/gfembed/?f=${formData?.gravity_form_id}`}
            width={width}
            height={height}
            frameBorder="0"
            className="gfiframe"
            scrolling="no"
            onLoad={e => {
              const iframe = e.target as HTMLIFrameElement
              iframe.contentWindow.postMessage(
                {
                  message: 'size',
                  index: formData?.gravity_form_id,
                },
                '*',
              )
            }}
          ></iframe>
        )}
      </div>
    </>
  )
}

from gravity-forms-iframe.

bradyvercher avatar bradyvercher commented on June 12, 2024

Thanks for the update and letting us know how you solved this @ttstauss!

from gravity-forms-iframe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.