cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
iwaldman
Advocate II
Advocate II

How to create a responsive iframe in a React (virtual) code component with Fluent UI

I am developing a React (virtual) code component with Fluent UI that is a simple container for an iframe. The iframe must be responsive to the available height and width i.e., grow and shrink.

 

I am thinking I could leverage the Fluent UI Stack control since it provides a flexbox abstraction and can apply flexbox properties on children.

 

Has anyone considered or solved this problem? I could use some guidance on how to proceed.

 

Thanks in advance, Irvin 

1 ACCEPTED SOLUTION

Accepted Solutions
iwaldman
Advocate II
Advocate II

I leverage useEffect to traverse the parental hierarchy up to the form tab and adjust the DOM. Not ideal but it is working. 

 

Here's my final code

 

  // Adjust flex layout causing the contents to reflow
  const adjustFlexLayout = React.useCallback((div: Element): void => {
    let parent = div?.parentElement

    while (
      parent?.id.indexOf('qtx_tab_section_cart') === -1 &&
      parent?.nodeName.indexOf('section') === -1
    ) {
      parent.style.display = 'flex'
      parent.style.flex = '1'
      parent.style.height = '100%'

      parent = parent.parentElement
    }
  }, [])

  // Reset flex layout causing the contents to reflow
  const resetFlexLayout = React.useCallback((div: Element): void => {
    let parent = div?.parentElement

    if (parent?.style?.flexDirection) {
      parent.style.flexDirection = ''
    }

    while (
      parent?.id.indexOf('qtx_tab_section_cart') === -1 &&
      parent?.nodeName.indexOf('section') === -1
    ) {
      parent.style.display = ''
      parent.style.flex = ''
      parent.style.height = ''

      parent = parent.parentElement
    }
  }, [])

  React.useEffect(() => {
    if (!loading && !error) {
      const iframeContainer = document.querySelector('div[class*="container-"]')

      if (iframeContainer) {
        isFullScreen ? resetFlexLayout(iframeContainer) : adjustFlexLayout(iframeContainer)
      }
    }
  })

View solution in original post

7 REPLIES 7
DianaBirkelbach
Super User
Super User

Hi @iwaldman , 

 

I might be wrong, but I don't think the FluentUI Stack would apply to the Iframe children. 

Fluent UI Stack is basically a "div". 

 

The PCF gives you the div container. I would just try to set the iframe width and height to 100%.

Kind regards,
Diana
----------
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Hi @DianaBirkelbach ,

 

Thank you for the reply.

 

Just setting the height to 100% isn't enough to be responsive. I believe it requires the parental elements to be probably styled and that is what I am struggling with. And, I am NOT a CSS whiz 😕

 

I will share some code later today that should shed some light on the challenge. 

 

BTW I really appreciate your blog posts. Thank you for them.

 

Kind regards, Irvin 

 

iwaldman
Advocate II
Advocate II

I was able to achieve this by traversing the parent and adjusting styles. Ugly...

 

const getClassNames = (isFullScreen: boolean = false): IClassNames => {
  return mergeStyleSets({
    iframeContainer: [
      {
        display: 'flex',
        flex: 1,
        height: '100%',
        width: '100%',
        overflow: 'hidden',
      },
      isFullScreen
        ? {
            position: 'absolute',
          }
        : {
            position: 'relative',
          },
    ],
    iframe: [
      {
        display: 'flex',
        flex: 1,
        position: 'relative',
        height: '100%',
        width: '100%',
        border: 0,
        top: 'auto',
        left: 'auto',
      },
      isFullScreen && {
        paddingBottom: '1em',
      },
    ],
    miniExpand: {
      height: '16px',
      width: '16px',
      cursor: 'pointer',
    },
  })
}

// Adjust flex layout causing the contents to reflow
  const adjustFlexLayout = (div: HTMLElement): void => {
    let parent = div?.parentElement

    console.log(`*** adjustFlexLayout - parent = ${parent}`)

    while (parent?.id.indexOf('tab-section') === -1 && parent?.nodeName.indexOf('BODY') === -1) {
      parent.style.display = 'flex'
      parent.style.flex = '1'
      parent.style.height = '100%'

      parent = parent.parentElement
    }
  }

<div ref={iframeContainerRef} className={getClassNames(isFullScreen).iframeContainer}>
  <iframe src={url} className={getClassNames(isFullScreen).iframe}></iframe>
</div>

 

 

cchannon
Super User
Super User

Stack can force your IFrame window to be responsive, but it can't force the individual elements in the hosted page to be responsive. If the site you are Iframing is not responsive, then the only thing you can do is force a "scale" on the IFrame which manually resizes it (https://stackoverflow.com/questions/11382473/resize-external-website-content-to-fit-iframe-width). If you need to accommodate multiple form factors, you can get the right factor from Context.Client obj.

iwaldman
Advocate II
Advocate II

I leverage useEffect to traverse the parental hierarchy up to the form tab and adjust the DOM. Not ideal but it is working. 

 

Here's my final code

 

  // Adjust flex layout causing the contents to reflow
  const adjustFlexLayout = React.useCallback((div: Element): void => {
    let parent = div?.parentElement

    while (
      parent?.id.indexOf('qtx_tab_section_cart') === -1 &&
      parent?.nodeName.indexOf('section') === -1
    ) {
      parent.style.display = 'flex'
      parent.style.flex = '1'
      parent.style.height = '100%'

      parent = parent.parentElement
    }
  }, [])

  // Reset flex layout causing the contents to reflow
  const resetFlexLayout = React.useCallback((div: Element): void => {
    let parent = div?.parentElement

    if (parent?.style?.flexDirection) {
      parent.style.flexDirection = ''
    }

    while (
      parent?.id.indexOf('qtx_tab_section_cart') === -1 &&
      parent?.nodeName.indexOf('section') === -1
    ) {
      parent.style.display = ''
      parent.style.flex = ''
      parent.style.height = ''

      parent = parent.parentElement
    }
  }, [])

  React.useEffect(() => {
    if (!loading && !error) {
      const iframeContainer = document.querySelector('div[class*="container-"]')

      if (iframeContainer) {
        isFullScreen ? resetFlexLayout(iframeContainer) : adjustFlexLayout(iframeContainer)
      }
    }
  })

That's going to mean a lot of re-rendering, so there will be a ton of overhead, but I don't have a better solution.

Unfortunately, yes. I had another idea but it requires me to fallback to a standard control. I will report back it the idea pans out. Thanks.

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Users online (3,428)