Developing a Component

This guide explains the techniques involved in the ongoing development a Spectrum control as a spectrum-web-components.

What is a web component?

Section titled What is a web component?

According to webcomponents.org, web components are:

... a set of web platform APIs that allow you to create new custom, reusable, encapsulated HTML tags to use in web pages and web apps. Custom components and widgets build on the Web Component standards, will work across modern browsers, and can be used with any JavaScript library or framework that works with HTML.

In order to add a new component to this library, you will need to develop a working knowledge of the following technologies:

Setting up the styling

Section titled Setting up the styling

The most complicated part of implementing a Spectrum web component is getting the styles set up correctly. The shadow DOM is the heart of a web component. It isolates the component from the styles and DOM of the containing page. While this offers many benefits, it also means that we must structure our CSS very differently.

For more information on how to structure your CSS, see the Styling guide.

Structure of a Spectrum Web Component

Section titled Structure of a Spectrum Web Component

If you look at an sp-button in the Chrome developer tools, you will see a DOM structure that looks like this.

<sp-button tabindex="0" variant="accent">#shadow-root (open)<button id="button" tabindex="0"><div id="label>
                ▼ <slot>
                    ↳ #text
                </slot>
            </div>
        </button>
    "Click Me"
</sp-button>

If anything here looks unfamiliar, it is probably a good time to do some reading about web components.

Coding the Component

Section titled Coding the Component

All of the spectrum-web-components are written using the lit-element framework and Typescript. Your best bet is to look at similar components and match the style.

We have a working specification for the APIs for each of the Spectrum components. If you file an issue for the component that you want to implement, we can provide the necessary specifications for it.

Documenting the component

Section titled Documenting the component

The documentation for each component in the documentation site is adopted from the README.md in said package. The pages are written in Markdown. See one of the existing pages for an example.

To run the local documentation server, use the command:

yarn docs:start

The documentation automatically extracts the properties and attributes from the source code. You should document your component using the appropriate jsdoc tags. See Button.ts for an example.

Documentation standards

Section titled Documentation standards

Each component's packages/_componentname_/README.md. These files must meet our standards below:

Documentation structure

Section titled Documentation structure

Our component documentation should follow the structure below. See packages/menu/README.md and packages/help-text/README.md for overview.

## Overview

### Usage

<--
Information on how to import the component.
-->

### Anatomy

<--
Information and examples about the parts of the component
(eg., labels, icons, etc.)
of the component. -->

### Options

<--
Information and examples about options
(eg., sizes, variants, states, etc.)
of the component.
-->

### States

<--
Information and examples about states
(eg., disabled, loading, etc.)
of the component.
-->

### Behaviors

<--
Information and examples about
values, events, and methods
of the component.
-->

### Accessibility

<--
Tips and examples on how to use the component accessibly
with notes how accessibility considerations
for how the component was developed.
-->

Working with Storybook

Section titled Working with Storybook

We use Storybook for developing our components. This gives us a rapid way to test our components in various configurations. The best way to start is to copy one of the existing stories.

To run Storybook, use the command:

yarn storybook