📜  storybook iframe 添加填充 (1)

📅  最后修改于: 2023-12-03 15:05:22.819000             🧑  作者: Mango

Storybook is a powerful tool for developing, testing and presenting UI components in isolation. One of its features is the ability to add custom themes to the UI of the Storybook itself. In this guide, we will be exploring how to add padding to the Storybook iframe using a custom theme.

To add padding to the iframe of your Storybook, you will need to create a custom theme. Here are the steps you need to follow:

  1. Create a new file in your Storybook project called "myTheme.js".
  2. In "myTheme.js", import "@storybook/theming" and "@emotion/core".
  3. Create a new theme object using "@storybook/theming.create".
  4. In the theme object, set the margins and padding of the iframe using "@emotion/core".
  5. Export the theme object as the default export of "myTheme.js".

Here is an example of what your "myTheme.js" file should look like:

import { create } from "@storybook/theming";
import { css } from "@emotion/core";

const myTheme = create({
  base: "light",

  // Set the padding and margin for the Storybook iframe
  appContent: css`
    margin: 24px;
    padding: 24px;
  `,
});

export default myTheme;

Once you have created your custom theme, you can use it in Storybook by importing it in your ".storybook/preview.js" file:

import { addDecorator } from "@storybook/react";
import myTheme from "./myTheme";

addDecorator(myTheme);

That's it! Your Storybook iframe should now have padding around it, making it easier to read and interact with.

Code snippets

Here are the code snippets you can use to add padding to the Storybook iframe:

  1. "myTheme.js":
import { create } from "@storybook/theming";
import { css } from "@emotion/core";

const myTheme = create({
  base: "light",

  // Set the padding and margin for the Storybook iframe
  appContent: css`
    margin: 24px;
    padding: 24px;
  `,
});

export default myTheme;
  1. ".storybook/preview.js":
import { addDecorator } from "@storybook/react";
import myTheme from "./myTheme";

addDecorator(myTheme);

Please note that the code snippets are written in JavaScript syntax and should be included in their respective files accordingly.