Digital Team
About UsEdit in Gitbook
  • Welcome
  • Getting started
    • Life on the Digital team
      • Meetings
      • Communication
      • Software engineering working agreement
    • Contributing to Boston.gov
    • Using GitBook
  • Standards & best practices
    • Digital Team Release Notes
    • Working with Partners
    • Accessibility at COB
      • Developers
      • Content Editors
        • How to guide
      • Resources
      • Working with Iterators
    • Analytics and Metrics
    • Code of Conduct
    • General
    • Code reviews
    • Project Management
    • Git / GitHub
      • Contacts at Github
      • Git Command Tips
      • GitHub Service Accounts
    • Code quality
      • Automated tests & static analysis
      • Code comments
      • Style guides
        • Drupal/PHP
          • D8 Dependency Injection (DI)
        • React/TypeScript
    • Technical documentation
    • Hosting and monitoring
    • Deployment
  • Guides
    • Technology stack and technologies used
      • Web applications
    • Drupal - boston.gov
      • Custom Development & Configuration
        • On Demand Instances
          • Acquia Environment setup checklist
        • Continuous Deployment Process
        • Developer Onboarding
          • Step 1: Local Dev Environments
          • Step 2: Version control
          • Step 3: Introduction to Drupal
          • (to be sorted)
            • Development environment
              • PHP CodeSniffer
              • VSCode IDE Setup
              • AWS for Developers
              • Using Windows
            • Installation instructions
              • Typical build output
              • Lando 101
              • Verify Installation
                • Local Patterns installation
              • Windows install
              • PhpStorm settings configurations
          • Step 4: Site Building in Drupal 8
        • Site Development Notes
          • Git Best Practices - Drupal
          • Drupal Cache
          • Drupal Config
          • Custom Modules
            • Custom Themes
              • Front-end Theme (bos_theme)
                • Site Breadcrumbs
              • Back-end Theme (bos_admin)
            • Adding Templates to Custom Modules
            • Custom Content Types
              • D7 -> D8 Conversion
              • Content Editor UX
                • Content Moderation
              • In-page Navigation Menu
            • Custom Paragraphs
              • D7 -> D8 Conversion
            • Custom Taxonomies
            • WebApps
          • Drupal UX-specific
            • Image Styles & UX
            • Example Content Pages
          • PHPStorm IDE
        • CKEditor
      • Drupal Apps/Content Types
        • Budget Website
        • Building Housing
          • BH Drupal Entities
          • BH Map Webpage
          • BH Property Webpage
            • BH Project Timeline
          • BH Salesforce Sync
            • Salesforce Contributed Module
        • Contact Form
        • Election results
        • Google reCAPTCHA
        • My Neighborhood Lookup
        • Metrolist
        • Metrolist (Drupal)
        • Project Tracker
          • Content Types (& Paragraphs)
          • Taxonomies
          • Views
          • Developer Notes
      • Drupal Features & Components
        • Single Sign On (SSO)
          • Drupal SAML Knowledgebase
          • SamlAuth
        • Maps on boston.gov
        • Charts on boston.gov
          • Quick Overview
          • Chart Data
          • Chart Configuration
          • Advanced Concepts and Techniques
          • Charts on boston.gov (legacy)
          • Useful Resources
      • Drupal micro-services (API end-points)
        • Integrating with Boston.gov
        • Assessing Forms Endpoint
        • Bos311 API
        • Cityscore
          • Knowledge Base
        • PDF Manager Module
        • PostMark Email Services
          • Postmark Knowledgebase
        • Upaknee Email List Services
        • Public Notices
        • Site Alerts
          • CodeRed Subscription
      • Drupal - Weekly Maintenance
      • Drupal - Periodic Maintenance
    • Digital Webapps
      • Libraries and Tools
        • Emotion
        • Storybook
        • Rollbar
      • Services
        • AWS-hosted Microservices
          • SQL Proxy API (DBConnector)
            • Developer Notes
          • PDFToolkit API (DB Connector)
            • Developer Notes
      • Webapps - Maintenance
      • Webapps
        • Boston Family Days
        • Property Tax Calculator
        • Access-Boston
          • Updating IAMDIR/Group Management/LDAP certificates
          • Node Server
          • Portal App Tile Configurations
          • Ownership of Concerns
          • Updating SAML Certificates
          • Applications/Services
            • Group Management
            • Confirm ID/ID Verification
            • Preferred Name
        • Sanitation Scheduling
        • Registry-Certs
          • Marriage Intention
      • DevOps
        • New service setup
          • Non-Monorepo Service Setup
        • Service Configuration
          • Editing a project’s configuration using Cyberduck
        • Managing AWS
          • Production Overview
          • AWS Bastion Access
          • Terraform
            • Updating the ECS cluster AMI
          • Restarting an ECS service
          • Encrypting service configuration for S3
          • Mounting AWS SFTP as a Drive (Mac)
        • Webapp Deployment
          • Deploy to AWS 2021
            • Deploy Tool (cob_ecrDeploy)
    • Fleet - Pattern Library
      • Patterns Library Architecture
      • Icon Library Architecture
      • Developers
        • Local Development for Drupal Developers
      • Patterns Library Maintenance
    • Legacy Website - cityofboston.gov
      • Animal Control
        • Dog Licenses
      • No Tow
        • Street Sweeping Reminders
        • Street Occupancy Alerts
        • Towing Alerts
        • Towing Search
        • Subscription Search
        • Proposed Restructure
          • Backend
        • Reillys Notes
      • Workers' Compensation Inquiry Form
      • Streetbook
      • Legacy Website - Maintenance
        • Animal Control Maintenance
        • Assessing Online (AOL) Maintenance
          • Knowledge-base
          • Disclosure Period
          • Annual PDF Initialization
          • Database Tables
        • No-Tow Maintenance
    • AgilePoint
      • AgilePoint: Adding Users
      • Migrating AGP Applications from one platform to another
    • The Hub - hub.boston.gov
      • The Hub - Maintenance
    • Maintenance
      • Updating SSL Certificates
    • Redirects
      • Redirecting from cityofboston.gov
      • URL redirects versus URL aliases - Drupal
      • DNS Redirects
    • Decommissioned Apps or Services
      • Archived Forms Information
      • CodeRed
      • Drupal 7
        • Deployment (2019)
          • Why do we peer-review pull-requests ?
      • Rentsmart
      • SnowStats
      • Ruby
    • Weglot translation
      • What to do in Weglot
      • What to do on the website or page
        • Softr
        • Drupal Powered Pages
        • Custom Pages
  • Projects
    • Project: Patterns Library Cleanup
      • Project: Refactoring Legacy CSS
        • Strategy
        • Regression Testing
        • Maintenance
    • Project: Upaknee
    • Project: Everbridge API + UI
    • Project: 311 CRM Upgrade
      • Project: City Worker Upgrade to City Worker 5
      • Project: Lagan 311 CRM upgrade to 15r4
    • Project: Fleet (Pattern Library Design System)
    • Project: Monorepo Decoupling
    • Inactive projects
      • Project: 311 (Salesforce Upgrade)
      • Project: Access Boston
        • General/Historical Documentation
          • Edit Config and Upload Icons
        • Processes
          • Process: Adding New Icon to Access Boston Website
          • Process: Non-icon Access Boston Feature or Bug Requests
          • Self-Service
      • Project: Alexa Skill
      • Project: Assessing Online
        • 2022 Notes
      • Project: Boards and commissions
      • Project: City Hall Appointment Scheduler
      • Project: CityScore
      • Project: Mobile Apps
      • Project: Permit Finder
      • Project: Public Notice Signage
      • Project: Registry Suite
        • Birth certificates
        • Marriage Certificates
        • Marriage Intention
        • Death Certificates
      • Project: Work With U.S. Digital Response Team
      • Project: TDM Points App
      • Project: Translation on boston.gov
  • External resources
    • Learning resources
    • Reference links
    • Applications and extensions
Powered by GitBook
On this page
  • Writing Stories
  • Setting where a Story should be organized in the sidebar
  • The most basic Story
  • Using a visual wrapper component
  • Allowing for quick prop changes

Was this helpful?

Export as PDF
  1. Guides
  2. Digital Webapps
  3. Libraries and Tools

Storybook

Using Storybook to develop and document UI components

PreviousEmotionNextRollbar

Last updated 6 years ago

Was this helpful?

Storybook lets us:

  • quickly build UI components in isolation

  • mock out a component’s different states/variations

  • experiment and test with different props (i.e. what happens if the text for a label is really, really long?)

It also:

  • provides documentation of our components

  • is used by Percy to perform its visual diffs

  • works with Jest to do automatic code snapshot testing ()

  • can catch simple a11y violations as soon as they occur ()

Writing Stories

If you write a Story for every different use case or state of a component, Percy will take a snapshot and catch any visual changes later on.

Setting where a Story should be organized in the sidebar

CompatibilityWarningContent.stories.tsx
storiesOf('Notifications|CompatibilityWarningContent', module)
StatusModal.stories.tsx
storiesOf('Notifications|Modals/StatusModal', module)

If you group any Stories (i.e. Group|Folder/Story) in your Storybook, Stories without a defined group will now appear in an automatically-generated "Others" group in the sidebar.

The most basic Story

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

import MyComponent from './MyComponent';

storiesOf('Group|Folder/MyComponent', module)
  .add('default', () => (
    <MyComponent />
  ));

You can have multiple storiesOf blocks if it helps you stay organized; any blocks that share a name string will be combined in the sidebar as a single collection of Stories.

Using a visual wrapper component

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

import MyComponent from './MyComponent';

const NARROW_STYLING = {
  maxWidth: '600px',
  margin: '2rem auto',
};

storiesOf('Group|Folder/MyComponent', module)
  .addDecorator(story => <div style={NARROW_STYLING}>{story()}</div>)
  .add('default', () => (
    <MyComponent />
  ))
  .add('variant', () => (
    <MyComponent />
  ));

Allowing for quick prop changes

The Knobs addon lets us change the props passed into our component inside Storybook’s UI. It’s super helpful to quickly test out component appearance against different values.

import React from 'react';

import { storiesOf } from '@storybook/react';
import { withKnobs, boolean, text } from '@storybook/addon-knobs';

import TextInput from './TextInput';

storiesOf('Form Elements|Inputs/TextInput', module)
  .addDecorator(withKnobs)
  .add('default', () => (
    <TextInput
      label={text('Label text', 'Standard text input')}
      placeholder={text('Placeholder text', '')}
      small={boolean('Small variant', false)}
      required={boolean('Required', false)}
      disabled={boolean('Disabled', false)}
      error={boolean('Error', false)}
    />
  ));

Add/remove options from a <select>

import React from 'react';

import { storiesOf } from '@storybook/react';
import { withKnobs, array, text } from '@storybook/addon-knobs';

import SelectDropdown from './SelectDropdown';

storiesOf('Form Elements|SelectDropdown', module)
  .addDecorator(withKnobs)
  .add('default', () => (
    <SelectDropdown
      label={text('Label text', 'Default Dropdown')}
      options={array('Options', ['Apples', 'Oranges', 'Pears'])}
    />
  ));

Simulate performing an action

You can also use Knobs to add buttons that trigger a change in UI:

import React from 'react';

import { storiesOf } from '@storybook/react';
import { withKnobs, button } from '@storybook/addon-knobs';

import ProgressBar from './ProgressBar';

class InteractiveStep extends React.Component<Props, State> {
  constructor(props) {
    super(props);

    this.state = {
      completed: false,
    };
  }

  render() {
    const label = 'Complete current task';
    const handler = () => this.setState({ completed: true });

    button(label, handler);
    
    return (
      <ProgressBar
        totalSteps={taskList.length}
        currentStep={this.props.currentStep}
        currentStepCompleted={this.state.completed}
      />
    );
  }
}

storiesOf('ProgressBar', module)
  .addDecorator(withKnobs)
  .add('default', () => (
    <InteractiveStep currentStep={1} />
  ));

Good for components that stretch too wide without a container. Using , all of the Stories will be children of the wrapper component.

Documentation:

addDecorator
https://github.com/storybooks/storybook/tree/master/addons/knobs
StoryShots addon
a11y addon
Result
Result
Result
Result