react ui 组件_如何使用React和Storybook构建UI组件

news/2024/7/4 13:04:55

react ui 组件

介绍 (Introduction)

In the previous article, I gave an Introduction to Storybook and how it can be used to organize and build JavaScript components. Storybook is a UI library that can be used to document components. In this article, I’ll be explaining how to build an interactive UI component using React and Storybook. We’ll be creating a UI component using Storybook and React and at the end of the tutorial, we’ll deploy the storybook as a stand-alone application to serve as a style guide.

在上一篇文章中,我对Storybook进行了介绍,并介绍了如何将其用于组织和构建JavaScript组件。 故事书是一个UI库,可用于记录组件。 在本文中,我将解释如何使用React和Storybook构建交互式UI组件。 我们将使用Storybook和React创建一个UI组件,并在本教程的最后,将Storybook作为独立的应用程序进行部署,以用作样式指南。

In this tutorial we will:

在本教程中,我们将:

  • Create a React Project

    创建一个React项目
  • Add storybook/react as a dependency

    将故事书/React添加为依赖项
  • Create a UI component Library with Storybook

    使用Storybook创建UI组件库
  • Customize themes

    自定义主题
  • Deploy as a static site

    部署为静态站点

步骤1 —安装先决条件 (Step 1 — Installing Prerequisites)

To get started we’ll need to create a new React project and for this, we’ll be using create-react-app which is a great tool for scaffolding React applications.

首先,我们需要创建一个新的React项目,为此,我们将使用create-react-app ,它是搭建React应用程序的绝佳工具。

  • npx create-react-app storybook-app

    npx create-react-app故事书-app
  • cd storybook-app

    cd故事书应用

And the next step will be to install the storybook package into our project:

下一步是将故事书包安装到我们的项目中:

  • npx -p @storybook/cli sb init

    npx -p @ storybook / cli sb初始化

The command can automatically detect the framework we are using from the package.json file and install the storybook version of that framework. After running both commands, we can confirm we have storybook-react dependencies installed by going over to the package.json file to see if the packages are installed and then we can run the application as well as run storybook.

该命令可以从package.json文件自动检测我们正在使用的框架,并安装该框架的故事书版本。 运行这两个命令后,我们可以转到package.json文件以查看是否已安装软件包,从而确定我们已安装了Storybook-react依赖项,然后可以运行该应用程序以及运行Storybook。

  • npm start

    npm开始
  • npm run storybook

    npm运行故事书

After running both commands, We’ll have both the react app and the storybook app running on different ports.

运行两个命令后,我们将在不同的端口上运行react应用程序和Storybook应用程序。

第2步—将故事添加到React应用 (Step 2 — Adding Stories to a React App)

Now that we are done with the setup, we can create components using Storybook. The idea here is to first setup storybook if we followed the steps outlined above, then it’s already preconfigured for us else if we installed the storybook/react package in an already existing react project then we’ll need to follow these steps to setup storybook. We’ll first want to create a folder .storybook in the root directory of our React project and then create a file within the folder called config.js and add the following lines of code:

现在我们完成了设置,我们可以使用Storybook创建组件。 这里的想法是,如果我们按照上述步骤进行操作,则首先设置故事书,然后已经为我们进行了预配置;否则,如果我们在已经存在的react项目中安装了storybook/react包,则需要按照以下步骤来设置故事书。 我们首先要在React项目的根目录中创建一个文件夹.storybook ,然后在名为config.js的文件夹中创建一个文件,并添加以下代码行:

import { configure } from '@storybook/react';
function loadStories() {
  require('../src/stories');
}
configure(loadStories, module);

The block of code above is a configuration for storybook and it tells Storybook to find the stories in the src/stories directory. We’ll go ahead and create that folder if we don’t have it already set up. Within the src folder create a folder called stories. This is where all the components we will create stories for will eventually be located. In this tutorial, we’ll be creating a Button component and for this component, we will document stories for them within the stories folder. Go ahead and create a Button Component within the component directory with the following lines of code:

上面的代码块是故事书的配置,它告诉故事书在src/stories目录中找到src/stories 。 如果尚未设置该文件夹,我们将继续创建该文件夹。 在src文件夹中,创建一个名为stories的文件夹。 这是我们将为其创建故事的所有组件的最终位置。 在本教程中,我们将创建一个Button组件,为此组件,我们将在stories文件夹中为它们记录故事。 继续,使用以下代码行在component目录中创建一个Button Component:

import React from 'react';
import './Button.css';
const Button = props => (
  <button onClick={props.onClick} style={props.style}>
    {props.label && <span>{props.label}</span>}
  </button>
)
export default Button;

Now, we can add a story for the button component created. Within the stories directory create a file and call it buttonStories.js and then add the following lines of code into the file:

现在,我们可以为创建的按钮组件添加一个故事。 在stories目录中,创建一个文件并将其命名为buttonStories.js ,然后将以下代码行添加到该文件中:

import React from 'react';
import { storiesOf } from '@storybook/react';

import Button from '../components/Button';

storiesOf('Button', module)
  .add('with primary', () => <Button label="Primary Button" />)
  .add('with outline', () => <Button
    label="Ouline Button"
    style={{ background: 'transparent', border: '3px solid #fecd43' }}
  />)
  .add('with rounder corners', () => <Button
    label="Rounded Button"
    style={{ borderRadius: '15px' }}
  />)
  .add('disabled', () => <Button disabled
    label="Disabled Button"
    style={{ background: 'gray' , border: 'gray', cursor: 'not-allowed' }}
  />)

Next, create an index.js file which will serve as a base file with all stories, then we can import all into this file. Go ahead and import the buttonStories.js file and then for every new file created under stories directory we will import them into this file.

接下来,创建一个index.js文件,该文件将用作包含所有故事的基本文件,然后我们可以将其全部导入到该文件中。 继续并导入buttonStories.js文件,然后对于在buttonStories.js目录下创建的每个新文件,我们都将它们导入到该文件中。

import './buttonStories';

步骤3 —集成插件 (Step 3 — Integrating Addons)

Addons with Storybook helps implement extra features to make them more useful and interactive. In this article, we will be adding the Action addon to the stories created. The Actions Addon can be used to display data received by event handlers in Storybook. A full list of all addons available with Storybook can be found here. We can set up the addon if it’s not already done by default you will need to follow these steps. Install the addon package with this command.

带有Storybook的附加组件有助于实现额外的功能,以使其更加有用和互动。 在本文中,我们将把Action插件添加到创建的故事中。 Actions插件可用于显示Storybook中事件处理程序接收的数据。 可在此处找到Storybook可用的所有附加组件的完整列表。 如果默认情况下尚未完成加载项,我们可以设置该加载项。 使用此命令安装插件软件包。

  • npm i -D @storybook/addon-actions or yarn add @stroybook/addon-actions

    npm i -D @ storybook / addon-actions或yarn add @ stroybook / addon-actions

Then, add the following content to .storybook/addons.js:

然后,将以下内容添加到.storybook/addons.js

import '@storybook/addon-actions/register';

To make our Button story interactive with Storybook we can add the following code to buttonStories.js:

为了使Button故事与Storybook互动,我们可以将以下代码添加到buttonStories.js

import React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';

import Button from '../components/Button';

storiesOf('Button', module)
  .add('with primary', () => <Button
    onClick={action('click')}
    label="Primary Button"
  />)
  .add('with outline', () => <Button
    label="Ouline Button"
    onClick={action('click')}
    style={{ background: 'transparent', border: '3px solid #fecd43' }}
  />)
  .add('with rounder corners', () => <Button
    label="Rounded Button"
    onClick={action('click')}
    style={{ borderRadius: '15px' }}
  />)
  .add('disabled', () => <Button disabled
    label="Disabled Button"
    onClick={action('click')}
    style={{ background: 'gray' , border: 'gray', cursor: 'not-allowed' }}
  />)

There you have it. To see this in action when you run Storybook, on the tab below you’ll see an Actions tab where the action is logged any time we interact with the buttons:

你有它。 要在运行Storybook时看到此操作的实际效果,在下面的选项卡上,您将看到一个Actions tab ,只要我们与这些按钮进行交互,便会在其中记录该操作:

第4步-自定义主题 (Step 4 — Customizing Themes)

With Storybook, we have the ability to customize the default theme used and storybook comes bundled with a default light theme. Now let’s see how we can customize this to something different. Dark theme maybe or something entirely different. To get started we’ll need to modify the configurations we have set in config.js file by adding the following lines of code:

使用Storybook,我们可以自定义使用的默认主题,并且Storybook与默认的灯光主题捆绑在一起。 现在,让我们看一下如何自定义其他内容。 黑暗主题也许或完全不同的东西。 首先,我们需要通过添加以下代码行来修改在config.js文件中设置的配置:

import { addParameters } from '@storybook/react';
import { themes } from '@storybook/theming'
addParameters({
  options: {
    theme: themes.dark
  }
})

With the dark theme configured, we can see the entire storybook theme look different and have switched to the dark theme. At any point, this can be switched back to either light or dark theme depending on our preference. We can also go ahead and define a dynamic theme out of the box if we want a different theme entirely. Let’s take a look at how we can achieve this. The first step is to create a file for our theme, within .storybook folder create a new file called yourTheme.js. I’ll be using pinkPanther as the name of our theme. Next step is to generate a new theme using the create() function from storybook/theming

配置了深色主题后,我们可以看到整个故事书主题看起来都不同,并已切换为深色主题。 在任何时候,都可以根据我们的喜好将其切换回浅色或深色主题。 如果我们想要完全不同的主题,我们也可以直接定义动态主题。 让我们看一下如何实现这一目标。 第一步是为我们的主题创建一个文件,在.storybook文件夹中创建一个名为yourTheme.js的新文件。 我将使用pinkPanther作为我们主题的名称。 下一步是使用storybook/themingcreate()函数生成一个新storybook/theming

import {create} from '@storybook/theming'
export default create ({
  base: 'light',
  colorPrimary: 'hotpink',
  colorSecondary: 'deepskyblue',
  // UI
  appBg: 'rgb(234, 0, 133)',
  appContentBg: 'white',
  appBorderColor: 'white',
  appBorderRadius: 4,
  // Typography
  fontBase: '"Open Sans", sans-serif',
  fontCode: 'monospace',
  // Text colors
  textColor: 'rgb(255, 250, 250)',
  textInverseColor: 'white',
  // Toolbar default and active colors
  barTextColor: 'white',
  barSelectedColor: 'white',
  barBg: 'rgb(246, 153, 190)',
  // Form colors
  inputBg: 'white',
  inputBorder: 'silver',
  inputTextColor: 'black',
  inputBorderRadius: 4,
  brandTitle: 'Pink Panther Storybook',
  brandUrl: 'https://example.com',
  brandImage: 'https://botw-pd.s3.amazonaws.com/styles/logo-thumbnail/s3/0019/2539/brand.gif?itok=97rSwE0a',
});

Then we can go ahead and import pinkPanther.js in our storybook config file.

然后,我们可以继续在我们的故事书配置文件中导入pinkPanther.js

import pinkPanther from  './pinkPanther';
addParameters({
  options: {
    theme: pinkPanther
  }
})

This gives us a completely different theme as seen in the previous image.

如上图所示,这给了我们一个完全不同的主题。

步骤5 —部署故事书 (Step 5 — Deploying Storybook)

One feature that comes bundled with Storybook is the fact that we can deploy our storybook as a static site that we can deploy to any hosting option of our choice. To configure this we will need to add a build-storybook script to our package.json:

Storybook附带的一项功能是,我们可以将Storybook部署为静态站点,然后将其部署到我们选择的任何托管选项中。 要配置这一点,我们需要一个添加build-storybook剧本给我们package.json

{
  "scripts": {
    "build-storybook": "build-storybook -c .storybook -o .out"
  }
}

What this script will do is that it’ll build our storybook directory into a static web app and output it in a .out directory. We can run this script and deploy the content of the .out directory when the build is complete. Go ahead and run the command:

该脚本将执行的操作是将故事书目录构建到静态Web应用程序中,并将其输出到.out目录中。 构建完成后,我们可以运行此脚本并部署.out目录的内容。 继续运行命令:

  • npm run build-storybook

    npm运行build-storybook

When the build is complete we can now deploy the content of the directory using any hosting of our choice. To test this works locally, we can run the following command and it’ll serve the content of the folder as a static site:

构建完成后,我们现在可以使用我们选择的任何托管来部署目录的内容。 要在本地测试此功能,我们可以运行以下命令,它将以静态站点的形式提供文件夹的内容:

  • npx http-server .out

    npx http-server .out

结论 (Conclusion)

In this article, we’ve learned how to build interactive components using Storybook and React. Now we have seen what is possible with Storybook and also have a guide on how to integrate storybook as a component library in our react projects. Developing with Storybook serves as a single source of truth for both developers and designers working in a team and it can be integrated with other front end frameworks. This post only outlines how it is used with React.

在本文中,我们学习了如何使用Storybook和React构建交互式组件。 现在,我们已经了解了Storybook的功能,并提供了有关如何将Storybook作为组件库集成到我们的React项目中的指南。 使用Storybook进行开发对于团队中的开发人员和设计人员而言都是唯一的真理来源,并且可以与其他前端框架集成。 这篇文章仅概述了它如何与React一起使用。

In case you’re looking out for a guide on other frameworks feel free to check Storybook’s official documentation. You can also find the code for this article on GitHub.

如果您正在寻找其他框架的指南,请随时查看Storybook的官方文档 。 您还可以在GitHub上找到本文的代码。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-build-a-ui-component-with-react-and-storybook

react ui 组件


http://www.niftyadmin.cn/n/3649734.html

相关文章

了解JavaScript中的变量范围

The scope is the execution context of a variable or function. It defines what data it has access to. This concept may seem pretty straightforward, but has some important subtleties. 作用域是变量或函数的执行上下文。 它定义了它有权访问的数据。 这个概念可能看…

[j2me]二级菜单界面演练[四]

Opera Mini的菜单也是最下方绘制的矩形条&#xff0c;然后drawString上菜单命令&#xff0c;我也要这么做&#xff0c;毕竟这么做简单些。若要做到类似于ucweb那种几乎接近于Windows任务栏/工具栏的那种效果&#xff0c;还尚需时日。今日花费点时间调整到这样吧&#xff0c;把左…

[j2me]二级菜单界面演练[三][0215Update]

今日又花费了点时间&#xff0c;调整界面上各种按键之下的效果&#xff0c;比如&#xff1a;上下左右方向键的作用&#xff1b;点击一次“选择”左软键就是展开主菜单&#xff0c;再点击一次“选择”左软键的作用就是消隐主菜单&#xff1b;等等诸如此类的效果&#xff0c;和Wi…

如何安装svelte_Svelte 3入门

如何安装svelte介绍 (Introduction) When building applications with JavaScript, high performance and penetrability will always be at the crux of the decisions made by software developers. Svelte is a high-performance component framework for imperative code. …

如何使用信号量持续集成和交付将Node.js应用程序构建和部署到DigitalOcean Kubernetes

The author selected the Open Internet / Free Speech fund to receive a donation as part of the Write for DOnations program. 作者选择了“ 开放式互联网/言论自由”基金来接受捐赠&#xff0c;这是“ 为捐赠写信”计划的一部分。 介绍 (Introduction) Kubernetes allow…

[j2me]二级菜单界面演练[二]

全赖朋友指点迷津&#xff0c;终于调试出来些许效果。实际上调整这种Opera Mini风格的二级菜单效果&#xff0c;颇为费时费力&#xff0c;也颇为折磨人的锐气。我只能按照以前对付bloglines手机伴侣的制作手法&#xff0c;每日花费一点时间调试&#xff0c;但也并不投入过多的精…

Meme Engine话题(三):从噪音中寻找信号

从噪音中寻找信号 现在的Rss Reader们(如Bloglines)都做得很好&#xff0c;他们简化了我们的生活&#xff0c;但是当RSS已经或即将成为过量信息时&#xff0c;我们都会烦恼如何从噪音中找到信号。herock在《Attention.xml初探》也提到&#xff1a;“Attention.XML试图解决的&am…

用Technorati Mini来跟踪blog世界

是一个小窗口&#xff0c;他会根据你的搜索关键词每隔1分钟就自动搜索1次&#xff0c;这样你一直让他保持着&#xff0c;你就可以像一个疯子一样持续不断地跟踪blog世界中每时每秒发表的和这个关键词有关的文章了。一个不错的主意&#xff0c;莫非keso是凭借这个小工具不断地自…