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
This commit is contained in:
Andrey Goncharov
2022-02-28 09:54:03 -08:00
committed by Facebook GitHub Bot
parent 8ee788239b
commit e26ba0d945
4 changed files with 72 additions and 9 deletions

1
website/.gitignore vendored
View File

@@ -8,6 +8,7 @@ build/
website/yarn.lock website/yarn.lock
website/node_modules website/node_modules
flipper-themes flipper-themes
static/css/style-guide.css
website/i18n/* website/i18n/*
!website/i18n/en.json !website/i18n/en.json

View File

@@ -1,14 +1,14 @@
{ {
"scripts": { "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", "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", "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:flipper-theme && docusaurus build", "build": "yarn copy-schema && yarn generate-plugin-docs && yarn build:style-guide && docusaurus build",
"publish-gh-pages": "docusaurus deploy", "publish-gh-pages": "docusaurus deploy",
"write-translations": "docusaurus write-translations", "write-translations": "docusaurus write-translations",
"version": "docusaurus version", "version": "docusaurus version",
"rename-version": "docusaurus rename-version", "rename-version": "docusaurus rename-version",
"generate-plugin-docs": "ts-node ./generate-plugin-docs.ts", "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": { "devDependencies": {
"@ant-design/icons": "^4.7.0", "@ant-design/icons": "^4.7.0",

View File

@@ -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 (
<iframe {...props} ref={setContentRef}>
{mountNode && createPortal(children, mountNode)}
</iframe>
);
};

View File

@@ -7,7 +7,7 @@
* @format * @format
*/ */
import React from 'react'; import React, {useState, useLayoutEffect} from 'react';
import { import {
Typography, Typography,
Button, Button,
@@ -32,9 +32,7 @@ import {
} from 'flipper-plugin'; } from 'flipper-plugin';
import {css} from '@emotion/css'; import {css} from '@emotion/css';
import reactElementToJSXString from 'react-element-to-jsx-string'; import reactElementToJSXString from 'react-element-to-jsx-string';
import {IFrame} from './IFrame';
import '../../flipper-themes/flipper-theme.css';
import 'antd/dist/antd.css';
const {Title, Text, Link} = Typography; const {Title, Text, Link} = Typography;
@@ -551,9 +549,30 @@ const DesignComponentDemos = () => (
</Layout.Container> </Layout.Container>
); );
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 ( return (
<Layout.Container className={reset} gap="large"> <Layout.Container className={reset} gap="large" ref={setRoot}>
<Card title="Flipper Design System" bordered={false}> <Card title="Flipper Design System" bordered={false}>
<p> <p>
Welcome to the Flipper Design System. The Flipper design system is Welcome to the Flipper Design System. The Flipper design system is
@@ -674,6 +693,16 @@ export default function SandyDesignSystem() {
); );
} }
export default function DesignSystemFramed() {
return (
<IFrame className={iframe} id="styleguide">
<link rel="stylesheet" href="/css/style-guide.css" />
<style>{innerCss}</style>
<SandyDesignSystem />
</IFrame>
);
}
function ColorPreview({name}) { function ColorPreview({name}) {
return ( return (
<List.Item> <List.Item>
@@ -707,3 +736,13 @@ const reset = css`
background: transparent; background: transparent;
} }
`; `;
const iframe = css`
width: 100%;
`;
const innerCss = `
body {
overflow: hidden;
}
`;