From e26ba0d945b3151040fdfa3e18ac87260e60055f Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Mon, 28 Feb 2022 09:54:03 -0800 Subject: [PATCH] Encapsulate styleguide styles Summary: Prevent leaking Flipper styles and antd styles into the website by embedding the styleguide into an iframe. Reviewed By: nikoant Differential Revision: D34522771 fbshipit-source-id: a05bf1e7f54fe172fb012a0a02296b3a4e0100f1 --- website/.gitignore | 1 + website/package.json | 6 ++-- website/src/components/IFrame.js | 23 +++++++++++++ website/src/components/StyleGuide.js | 51 ++++++++++++++++++++++++---- 4 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 website/src/components/IFrame.js diff --git a/website/.gitignore b/website/.gitignore index 57214590f..ca9be16a3 100644 --- a/website/.gitignore +++ b/website/.gitignore @@ -8,6 +8,7 @@ build/ website/yarn.lock website/node_modules flipper-themes +static/css/style-guide.css website/i18n/* !website/i18n/en.json diff --git a/website/package.json b/website/package.json index 17d9a146d..5dac4a1c7 100644 --- a/website/package.json +++ b/website/package.json @@ -1,14 +1,14 @@ { "scripts": { "copy-schema": "fcli ensure static/schemas/plugin-package && fcli copy ../desktop/pkg/schemas/plugin-package-v2.json static/schemas/plugin-package/v2.json -o", - "start": "yarn copy-schema && yarn generate-plugin-docs && yarn build:flipper-theme && docusaurus start --port 3001", - "build": "yarn copy-schema && yarn generate-plugin-docs && yarn build:flipper-theme && docusaurus build", + "start": "yarn copy-schema && yarn generate-plugin-docs && yarn build:style-guide && docusaurus start --port 3001", + "build": "yarn copy-schema && yarn generate-plugin-docs && yarn build:style-guide && docusaurus build", "publish-gh-pages": "docusaurus deploy", "write-translations": "docusaurus write-translations", "version": "docusaurus version", "rename-version": "docusaurus rename-version", "generate-plugin-docs": "ts-node ./generate-plugin-docs.ts", - "build:flipper-theme": "fcli remove flipper-themes && fcli copy ../desktop/themes flipper-themes && lessc --js flipper-themes/light.less flipper-themes/flipper-theme.css" + "build:style-guide": "fcli remove flipper-themes && fcli copy ../desktop/themes flipper-themes && lessc --js flipper-themes/light.less static/css/style-guide.css" }, "devDependencies": { "@ant-design/icons": "^4.7.0", diff --git a/website/src/components/IFrame.js b/website/src/components/IFrame.js new file mode 100644 index 000000000..53205f539 --- /dev/null +++ b/website/src/components/IFrame.js @@ -0,0 +1,23 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import React, {useState, useEffect} from 'react'; +import {createPortal} from 'react-dom'; + +// https://stackoverflow.com/a/34744946 +export const IFrame = ({children, ...props}) => { + const [contentRef, setContentRef] = useState(null); + const mountNode = contentRef?.contentWindow?.document?.body; + + return ( + + ); +}; diff --git a/website/src/components/StyleGuide.js b/website/src/components/StyleGuide.js index be07fb60f..e805b21ba 100644 --- a/website/src/components/StyleGuide.js +++ b/website/src/components/StyleGuide.js @@ -7,7 +7,7 @@ * @format */ -import React from 'react'; +import React, {useState, useLayoutEffect} from 'react'; import { Typography, Button, @@ -32,9 +32,7 @@ import { } from 'flipper-plugin'; import {css} from '@emotion/css'; import reactElementToJSXString from 'react-element-to-jsx-string'; - -import '../../flipper-themes/flipper-theme.css'; -import 'antd/dist/antd.css'; +import {IFrame} from './IFrame'; const {Title, Text, Link} = Typography; @@ -551,9 +549,30 @@ const DesignComponentDemos = () => ( ); -export default function SandyDesignSystem() { +function SandyDesignSystem() { + const [root, setRoot] = useState(null); + + useLayoutEffect(() => { + if (root) { + const iframe = window.parent.document.getElementById('styleguide'); + iframe.style.height = `${root.scrollHeight}px`; + + const observer = new MutationObserver(() => { + iframe.style.height = `${root.scrollHeight}px`; + }); + observer.observe(root, { + subtree: true, + childList: true, + attributes: true, + characterData: true, + }); + + return () => observer.disconnect(); + } + }, [root]); + return ( - +

Welcome to the Flipper Design System. The Flipper design system is @@ -674,6 +693,16 @@ export default function SandyDesignSystem() { ); } +export default function DesignSystemFramed() { + return ( + + ); +} + function ColorPreview({name}) { return ( @@ -707,3 +736,13 @@ const reset = css` background: transparent; } `; + +const iframe = css` + width: 100%; +`; + +const innerCss = ` + body { + overflow: hidden; + } +`;