如何在 Gatsby 站点的 iframe 中渲染带有 Emotion 样式的 React 组件?

Mis*_*hko 4 iframe emotion reactjs webpack gatsby

我在一个组件库和一个演示站点上工作。

组件使用Emotion设计样式,演示站点使用Gatsby构建。

出于预览目的,我想在 iframe 中呈现组件。这将确保来自网站的样式不会级联到组件,更容易处理响应式布局等。

我还想在 iframe 中保留热重载。

在这里,您可以看到一个示例,说明如何line-height从网站级联到Button组件,使其变得非常高。

在此处输入图片说明

我怎么能Button在 iframe 中渲染它的所有样式?

Der*_*yen 6

我认为这里的问题是将生成的样式应用于emotioniframe 内的按钮。

我找到了 Mitchell(情感核心团队)的这个很好的例子,它完全符合你的需要:github

这是你的 codeandbox 的一个分支,复制了代码,带有一个基本的自制<Iframe>元素:codesandbox

结果

这是相关的代码:

// src/components/Iframe.js

import React, { useRef, useEffect, useState } from 'react'
import { createPortal } from 'react-dom'

import { CacheProvider } from '@emotion/core'
import createCache from '@emotion/cache'
import weakMemoize from '@emotion/weak-memoize'

// literally copied from Mitchell's codesandbox
// https://github.com/emotion-js/emotion/issues/760#issuecomment-404353706
let memoizedCreateCacheWithContainer = weakMemoize(container => {
  let newCache = createCache({ container });
  return newCache;
});


/* render Emotion style to iframe's head element */
function EmotionProvider({ children, $head }) {
  return (
    <CacheProvider value={memoizedCreateCacheWithContainer($head)}>
      {children}
    </CacheProvider>
  )
}

/* hack-ish: force iframe to update */
function useForceUpdate(){
  const [_, setValue] = useState()
  return () => setValue(0)
}

/* rudimentary Iframe component with Portal */
export function Iframe({ children, ...props }) {
  const iFrameRef = useRef(null)
  const [$iFrameBody, setIframeBody] = useState(null)
  const [$iFrameHead, setIframeHead] = useState(null)
  const forceUpdate = useForceUpdate()

  useEffect(function(){
    if (!iFrameRef.current) return

    const $iframe = iFrameRef.current
    $iframe.addEventListener('load', onLoad)

    function onLoad() {
      // TODO can probably attach these to ref itself?
      setIframeBody($iframe.contentDocument.body)
      setIframeHead($iframe.contentDocument.head)

      // force update, otherwise portal children won't show up
      forceUpdate()
    }

    return function() {
      // eslint-disable-next-line no-restricted-globals
      $iframe.removeEventListener('load', onload)
    }
  })

  return (<iframe {...props} title="s" ref={iFrameRef}>
      {$iFrameBody && $iFrameHead && createPortal((
        <EmotionProvider $head={$iFrameHead}>{children}</EmotionProvider>
      ), $iFrameBody)}
    </iframe>)
}
Run Code Online (Sandbox Code Playgroud)

如果您希望 iFrame 在gatsby build.

对于styled-components用户,我发现Stephen Haney 的这个片段看起来比以下代码优雅得多emotion

[...]styled-components包括一个 StyleSheetManager 组件,它可以接受一个目标道具。目标需要一个 DOM 节点,并将其动态创建的样式表附加到该节点。

react-frame-component使用 React 新版本的 Context API 来公开一个FrameContextProvider. 它包括IFrame上下文中的文档和窗口。

您可以按如下方式组合这两个 API 以styled-components 在您的 IFrame 中使用:

    {
      frameContext => (
        <StyleSheetManager target={frameContext.document.head}>
          <React.Fragment>
            {/* your children here */}
          </React.Fragment>
        </StyleSheetManager>
      )
    }   </FrameContextConsumer> </Frame> 

Run Code Online (Sandbox Code Playgroud)

这与 react v16.4.1、styled-components v3.3.3 和 react-frame-component v4.0.0 完美配合。