Storybook Controls - Live edit UI components with no code

栏目: IT技术 · 发布时间: 4年前

内容简介:Storybook helps you build UI components in isolation and save their states as stories. A story is a simple function that renders a component given a set ofBut components are complicated. It’s hard to predict how a component will react to different values a

Storybook Controls

Live edit UI components with no code

Jul 20 ·4min read

Storybook is the world’s most popular UI component workshop. It’s used by Airbnb, Slack, Lyft, IBM, and thousands of top teams across the industry.

Storybook helps you build UI components in isolation and save their states as stories. A story is a simple function that renders a component given a set of arguments (props, inputs, slots, etc.).

But components are complicated. It’s hard to predict how a component will react to different values and write stories for all meaningful combinations. So it’s super useful to be able to explore the state space interactively.

That’s why I’m excited to announce Controls, a new Storybook 6.0 addon that allows you to dynamically edit a component’s inputs without touching code . Controls are auto-generated UI for rapid component experimentation.

  • :zap: Live edit components. Dynamically tweak component arguments.
  • :rabbit2: Instant setup. No setup cost. No maintenance as you modify APIs.
  • Automatic generation. No extra glue code needed.
  • :key: No lock-in. No dependency on Storybook-specific APIs.

Storybook Controls - Live edit UI components with no code

‍♀️ Can’t I already do this?

Dynamically editing components is not a new concept. Component “playgrounds” have existed for years. Storybook’s own Knobs addon is downloaded over 1M times each week!

We researched how teams from Auth0, Shopify, and our vibrant community live edit their components. Sophisticated techniques require setup and ongoing maintenance. Simpler methods expect folks to know how to code.

Fortunately, trends in the JavaScript ecosystem work in our favor. As developers mass adopt type systems (e.g. TypeScript, Flow, propTypes), components get well-specified sets of arguments they accept. And those arguments are machine readable.

Controls uses types to generate graphical controls.It requires no extra work and is seamlessly integrated into the Storybook developer experience.

Storybook Controls - Live edit UI components with no code

:sparkles: Stories get Controls automatically

Controls are auto-generated from component arguments. Consider the following component that declares its interface in TypeScript:

interface ButtonProps { label?: string; }
const Button = ({ label='' }: ButtonProps) => (
  <button>{label}</button>
);

If you’re using React, Vue, Angular, or Web Components, Storybook analyzes your component arguments and automatically generates UI to edit these arguments.

This saves hundreds of lines of code in a component library that no longer need to be written and updated as your components change.

Storybook stories (component examples) are functions that return a rendered component. Control values are passed to your Story functions as arguments, and you can also declare the initial values succinctly:

export const Basic = (args) => <Button {...args} />;
Basic.args = { label: 'hello button' };

:books: Perfect for StorybookDocs

Controls works with Docs, Storybook’s automated documentation generator for UI components, including bothDocsPage andMDX.

The generated “args table” includes controls for each component argument. That allows you to adjust the component examples while browsing the docs.

Storybook Controls - Live edit UI components with no code

:truck:Write stories once and take anywhere

Stories written for Controls are portable to other testing libraries and tools.

They are based on Component Story Format , an open ES6 based standard for component examples. We’re working with a variety of design, dev, and testing tools to make sure Controls stories are supported in other contexts.

If you already use the Knobs addon, Controls provides a more ergonomic syntax. It replaces “side-loaded” data with standard functional code. Here’s how you’d instrument a component with Knobs:

import { text } from '@storybook/addon-knobs';export const Basic = () => (
  <Button label={text('Label', 'hello')} />
);

See that pesky Storybook import? Recall the Controls syntax from above, which is completely free of proprietary dependencies:

export const Basic = (args) => <Button {...args} />;
Basic.args = { label: 'hello' };

When you write stories in this function style, it also helps you reduce story boilerplate, reuse fixture data across stories, and recycle stories more easily in other tools.These DX benefits are elaborated upon in the Storybook Args release post .

:zap: 1 minute install

Try out Controls today in Storybook 6.0 RC:

npx sb@next upgrade --prerelease

Or bootstrap Storybook into an existing app:

npx sb@next init

To create your first Controls story, create a function that takes an Args object as its first input, and then annotate the function with the data you want to receive:

import { Button } from '@storybook/react/demo';export default { title: 'Button/Controls', component: Button };export const Basic = (args) => <Button {...args} />;
Basic.args = { children: 'hello' };

To get auto-generated controls for that story (available in React, Vue, Angular, and Web-components), follow the quick installation instructions in the docs .


很遗憾的说,推酷将在这个月底关闭。人生海海,几度秋凉,感谢那些有你的时光。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据挖掘导论

数据挖掘导论

(美)Pang-Ning Tan、Michael Steinbach、Vipin Kumar / 机械工业出版社 / 2010-9 / 59.00元

本书全面介绍了数据挖掘的理论和方法,着重介绍如何用数据挖掘知识解决各种实际问题,涉及学科领域众多,适用面广。 书中涵盖5个主题:数据、分类、关联分析、聚类和异常检测。除异常检测外,每个主题都包含两章:前面一章讲述基本概念、代表性算法和评估技术,后面一章较深入地讨论高级概念和算法。目的是使读者在透彻地理解数据挖掘基础的同时,还能了解更多重要的高级主题。 本书特色 ·包含大量的图表、......一起来看看 《数据挖掘导论》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器