Skip to content

Documenting Web Components

The analyzer reads a component's TypeScript file, and the HTML and CSS files it passes to $shadow or $inline, and generates the docs, the framework types, and the manifests from them. It infers names, types, and defaults from the code, and JSDoc tags and comments add the descriptions and fix what it cannot infer. The ESLint plugin reports a public item with no description, and a tag that repeats what the analyzer infers.


Component Overview

Tag Name and Title

The analyzer finds a component class by its static _componentName and takes the tag name from it. @tagName or @name sets another tag name, like when _componentName is not a string literal, and @title replaces the tag name as the heading of the generated page.

/**
* @tagName my-component
* @title My Component
*/
class MyComponent extends HTMLElement {}

Description

The description of the class JSDoc is Markdown.

/**
* A button component that supports icons and loading states.
*
* ## Usage
*
* ```html
* <my-button>Click me</my-button>
* ```
*/
class MyButton extends HTMLElement {}

External Markdown Files

For long docs, @markdown appends a Markdown file to the description, with the path in braces relative to the component file. Several @markdown tags add their files in order, after the inline description.

/**
* Short inline description.
*
* @markdown {./docs/overview.md}
* @markdown {./docs/usage.md}
*/
class MyComponent extends HTMLElement {}

Code Examples

@example adds a TypeScript code block below the description.

/**
* @example
* const el = document.createElement("my-component");
* el.value = "hello";
* document.body.appendChild(el);
*/
class MyComponent extends HTMLElement {}

Properties

The analyzer documents each public instance property, getter, and setter, with the type of its annotation and the default of its initial value or of what its getter returns. It leaves out members that start with # or _, private, protected, and static members, declarations like name!: string, and the lifecycle callbacks and native members a component often defines, like form, validity, focus, and blur.

@prop | @property

The type slot replaces the documented type, like to spell out an interface or an imported type that the docs would show only by name.

class MyComponent extends HTMLElement {
/**
* The current value.
*
* @property {{ name: string; age: number }}
*/
value: ComplexType = {};
}

@default

@default sets the default when the analyzer cannot read it, like from a getter that parses an attribute.

class MyComponent extends HTMLElement {
/**
* Animation duration in milliseconds.
*
* @default 300
*/
get duration(): number {
return Number(this.getAttribute("duration")) || 300;
}
}

@attr | @attribute

An observed attribute takes its details from the property of the same name, as Attributes describes. @attr on the property documents that attribute even when it is not observed, and changes it in three ways.

A different attribute type, without changing the property type:

class MyComponent extends HTMLElement {
/** @attr {string} */
value = 0;
}

A description of its own, with - in place of the name:

class MyComponent extends HTMLElement {
/** @attr - Custom description for this attribute. */
value = 0;
}

A different attribute name, with an optional description:

class MyComponent extends HTMLElement {
/** @attr data-value The value exposed as a data attribute. */
value = 0;
}

@reflect | @no-reflect

The analyzer marks a property as reflecting when its setter calls this.setAttribute or this.toggleAttribute with the property's attribute name. A setter that writes the passed value to exactly one other attribute, like value for defaultValue, reflects to that attribute. @reflect marks reflection the analyzer cannot see, with the attribute name when it differs from the property's attribute, and @no-reflect turns the detection off.

class MyComponent extends HTMLElement {
/** @reflect */
set vertical(value: boolean) {
this._reflect(value); // reflection through a helper, not detectable
}
/** @no-reflect */
set value(value: string) {
this.setAttribute("value", value); // not treated as reflection
}
}

Attributes

The analyzer documents each name in the static observedAttributes array, and each attribute that a property reflects, even when it is not observed, like the name of a form component. An attribute takes its type, default, and description from the property with the camelCase form of its name, like closeButton for close-button. The match ignores case, like readOnly for readonly.

class MyComponent extends HTMLElement {
static readonly observedAttributes = ["value"] as const;
/** The selected value. */
value: "foo" | "bar" | "baz" = "foo";
}

Documenting Other Attributes

Document any other attribute on the class JSDoc with @attr or @attribute, with the default in brackets after the name.

/**
* @attr {boolean} open Whether the panel is open.
* @attr {string} [close-label="Close"] The name of the close button. Default is `"Close"`
*/
class MyComponent extends HTMLElement {}

Attributes on Other Elements

@globalAttr or @globalAttribute on the class JSDoc documents an attribute that the component reads on other elements, like the bind-text attribute of sv-ui-state. Editors then suggest it on every element, and the generated page leaves it out.

/** @globalAttr bind-text Sets the text content of the element from an expression. */
class MyComponent extends HTMLElement {}

Methods

The analyzer documents each public readonly property that holds a function as a method. Annotate its type, since a function without an annotation is documented as Function.

class MyComponent extends HTMLElement {
/** Opens the panel. */
readonly open: (animate: boolean) => void = (animate = true) => {
// ...
};
}

@method | @function | @func

These tags document a property as a method, like one that is not readonly, and their type slot sets the type.

class MyComponent extends HTMLElement {
/**
* Opens the dialog.
*
* @function {() => void}
*/
open = () => {};
}

Documenting Undetected Methods

A method the analyzer misses entirely goes on the class JSDoc with @method or @function.

/** @function {() => void} refresh Forces a re-render. */
class MyComponent extends HTMLElement {}

Events

The analyzer documents each event that a function of the static _events object constructs with new CustomEvent(), with its name, the type of event.detail, and the cancelable, bubbles, and composed options. The JSDoc on the function describes the event.

class MyComponent extends HTMLElement {
static readonly _events = {
/** Fired when the panel opens. */
_opened: (detail: HTMLElement) => new CustomEvent("opened", { detail, bubbles: true }),
};
}

The event above is documented as a bubbling CustomEvent<HTMLElement> with no tag. An event constructed elsewhere in the class is documented too, unless it is dispatched on another element, but only _events gives an event its framework types. A plain new Event() counts as a native event and stays out of the docs.

The tags below describe what the source cannot show, and override what the analyzer infers.

@detail | @eventDetail | @payload | @eventPayload

@detail describes event.detail, and its type slot replaces the inferred type, like for a parameter without a type. Start the description with -, or its first word is taken as a name.

class MyComponent extends HTMLElement {
static readonly _events = {
/**
* Fired before the value changes.
*
* @detail - The proposed `value` and the `previous` one.
*/
_beforeChange: (detail: ChangeDetail) => new CustomEvent("beforechange", { detail }),
/**
* Fired when an item is selected.
*
* @detail {{ index: number }} - The index of the selected item.
*/
_selected: detail => new CustomEvent("selected", { detail }), // untyped parameter
};
}

@cancelable | @no-cancelable

An event constructed with cancelable: true is documented as cancelable. @cancelable says what canceling the event does, or marks an event as cancelable when the analyzer cannot see it, and @no-cancelable clears it. Start the description of @cancelable with - too.

class MyComponent extends HTMLElement {
static readonly _events = {
/**
* Fired when the user clicks the trigger.
*
* @cancelable - Stops the panel from toggling.
*/
_clicked: () => new CustomEvent("clicked", { cancelable: true }),
};
}

@bubbles | @composed

Both are read from the event options. @bubbles and @composed mark propagation the analyzer cannot see, like options from a shared object, and @no-bubbles and @no-composed clear it.

class MyComponent extends HTMLElement {
static readonly _events = {
/**
* Fired when the panel opens.
*
* @bubbles
* @composed
*/
_opened: () => new CustomEvent("opened", eventOptions), // options from a shared object
};
}

Events on Properties

@event, @fires, or @emits on a property documents it as an event named after the property, with the type of event.detail in the type slot. The name has to match the one the event is constructed with, or the docs list two events. Prefer _events, since only it gives an event its framework types.

class MyComponent extends HTMLElement {
/**
* Fired when the value changes.
*
* @event {{ value: string }}
*/
change = (value: string) => new CustomEvent("change", { detail: { value } });
}

Documenting Undetected Events

An event the analyzer misses goes on the class JSDoc with @event, @fires, or @emits, and the type slot holds the type of event.detail.

/**
* @event opened Fired when the panel opens.
* @event {{ reason: string }} closed Fired when the dialog closes.
*/
class MyComponent extends HTMLElement {}

The other event tags take the event name in the same slot, in any order:

/**
* @event {{ reason: string }} closed Fired when the dialog closes.
* @cancelable closed Keeps the dialog open.
* @bubbles closed
*/
class MyComponent extends HTMLElement {}

Tags always win over what the analyzer reads from the source, and tags written next to the event win over tags on the class.


Slots and CSS Parts

The analyzer documents each <slot> of the template, with Default as the name of the unnamed slot, and each name in a part or exportparts attribute. A comment with @slot or @cssPart directly above the element describes it, with - in place of the name. A comment that names its slot or part can stand anywhere in the HTML file, and it describes one that the template does not contain, like one the script adds.

<!-- @slot dynamic A slot that the script adds. -->
<!-- @cssPart - The header of the panel. -->
<header part="header">
<!-- @slot - The heading text. -->
<slot name="heading"></slot>
</header>
<!-- @slot - The content of the panel. -->
<slot></slot>

Documenting Slots and Parts Manually

@slot and @cssPart on the class JSDoc also describe slots and parts, but the check-template-documentation rule of the ESLint plugin reads only the template.

/**
* @slot Default The main content area.
* @slot header Content placed in the header area.
* @cssPart track The track circle.
*/
class MyComponent extends HTMLElement {}

CSS Variables

The analyzer documents each custom property declared in a top-level rule whose selector is :host or a root class, like .root, .combobox-root, .combobox_root, or .comboboxRoot. A comment directly above the declaration describes it, and the declared value becomes the default, with a var() documented by its name.

:host {
/* Duration of the open and close animation. */
--sv-accordion-dur: 350ms;
}

Since every variable declared there is documented, an internal variable needs a @cssVar tag or @ignore, as the sections below show.

The @cssVar Tag

@cssVar, @cssProp, and @cssProperty take a name, an optional default in brackets, and a description, like on the class JSDoc. Use them when the documented name or default differs from the declaration below, like for a calc() the analyzer cannot read.

.root {
/* @cssVar [--my-card-gap=1em] The space between the items. */
--half-gap: calc(var(--my-card-gap, 1em) / 2);
}

The brackets control the default:

  • [--my-card-gap=1em] sets it to 1em.
  • [--my-card-gap] documents the variable with no default.
  • Without brackets, a name has no default, and a placeholder takes its default from the declaration below.

Two placeholders stand in for the name:

  • - keeps the name of the declaration below, like @cssVar [-=1] Direction of the motion.
  • (from-value) takes the name of the variable the declaration reads, as shown in Reading a Public Variable.

A separate @default line after the tag also sets the default, for a value that does not fit in the brackets.

Reading a Public Variable

An internal variable usually reads a public one, with a theme variable or a literal as the fallback, as in Internal CSS Variables. (from-value) as the name documents the public variable in place of the internal one. The fallback becomes the default, with a nested var() documented by its name.

.root {
/* @cssVar (from-value) Background color of the panel. */
--background-color: var(--sv-accordion-bg, var(--sv-gray-6, Canvas));
/* @cssVar [(from-value)=200ms] Duration of the open and close animation. */
--animation-duration: var(--sv-accordion-dur, 350ms);
}

The first is documented as --sv-accordion-bg with the default --sv-gray-6. The second is documented as --sv-accordion-dur with the default 200ms, in place of the fallback.

Variables Without a Declaration

A public variable that other rules read directly needs no declaration in the token rule, and a @cssVar tag documents it. Put the tags in a comment that no declaration follows, since tags above a declaration replace it in the docs. One comment can hold a tag for each variable.

.root {
/*
@cssVar [--sv-text-area-bg=--sv-gray-6] Background color of the field.
@cssVar [--sv-text-area-bdr-clr=--sv-gray-5] Border color of the field.
*/
}
.field {
background-color: var(--sv-text-area-bg, var(--sv-gray-6, Canvas));
border-color: var(--sv-text-area-bdr-clr, var(--sv-gray-5, ButtonBorder));
}

Documenting Variables Manually

@cssVar, @cssProp, or @cssProperty on the class JSDoc also documents a variable, but the check-stylesheet-documentation rule of the ESLint plugin reads only the stylesheet.

/**
* @cssVar --sv-color The primary text color.
* @cssVar [--sv-radius=4px] The border radius applied to the root element. Default is `4px`
*/
class MyComponent extends HTMLElement {}

CSS States

The analyzer documents each state name that the class passes as a string to states.add() on the member that holds this.attachInternals(). @cssState on the class JSDoc describes it.

/**
* @cssState open Applied when the panel is open.
* @cssState loading Applied while async content is loading.
*/
class MyComponent extends HTMLElement {
#internals = this.attachInternals();
#setOpen(value: boolean) {
if (value) {
this.#internals.states.add("open");
} else {
this.#internals.states.delete("open");
}
}
}

Leaving Items Out

@ignore or @internal in the comment that documents an item leaves that item out of the docs.

class MyComponent extends HTMLElement {
/** @ignore */
internalCache = null;
static readonly _events = {
/** @ignore */
_clicked: () => new CustomEvent("clicked", { cancelable: true }),
};
}
<!-- @ignore -->
<slot name="header"></slot>
.root {
/* @ignore */
--background-color: var(--sv-base-2);
}

@ignoreIn on the class JSDoc leaves out items by name, with the categories in braces: properties, attributes, methods, events, slots, cssParts, cssVars, and cssStates. Both lists are comma separated, and a CSS variable keeps its --.

/**
* @ignoreIn {attributes,properties} name
* @ignoreIn {cssVars} --sv-color,--sv-size
*/
class MyComponent extends HTMLElement {}

Extending Another Component

@extends

When a component extends another component, point @extends at the base component's source file, relative to the component file. The docs then include each item of the base that the component does not document itself.

/** @extends {../flyout/flyout.ts} */
class MySelect extends Flyout {}

Ignoring Inherited Members

@ignoreInherited on the class JSDoc leaves out members that come from the base. It takes the same categories as @ignoreIn, and * leaves out every inherited member of a category.

/**
* @extends {../flyout/flyout.ts}
* @ignoreInherited {properties} popupRole,openOnHover
* @ignoreInherited {attributes,cssStates} *
*/
class MySelect extends Flyout {}