Generating Documentation in JSON format
Stencil supports automatically generating README files in
your project which pull in JSDoc comments and provide a
straightforward way to document your components.
If you need more flexibility, Stencil can also write documentation to a JSON file which you could use for a custom downstream documentation website.
You can try this out is using the --docs-json CLI flag like so:
stencil build --docs-json path/to/docs.json
You can also add the docs-json output target to your project's configuration
file in order to auto-generate this file every time you build:
import { Config } from '@stencil/core';
export const config: Config = {
outputTargets: [
{
type: 'docs-json',
file: 'path/to/docs.json'
}
]
};
The JSON file output by Stencil conforms to the JsonDocs interface in
Stencil's public TypeScript
declarations.
supplementalPublicTypes
As of Stencil v4 the JSON documentation generation functionality in Stencil
supports a new configuration option, supplementalPublicTypes.
This functionality makes it easy to automatically document types and interfaces
which otherwise wouldn't be included in the documentation that Stencil
generates. By default, Stencil includes extensive information about the types
used in the public APIs of all your components, meaning the properties on your
components decorated with @Watch, @Event, @Prop and so on. This makes it
easy to document your components' APIs; however, if your project uses other
types which aren't found in the public API of a component then those types
won't be included.
The new supplementalPublicTypes option fills in this gap by allowing you to
designate a file of types which should be included in the output of the
docs-json output target.
This information will be found in a top-level property called typeLibrary on
the JSON output and will conform to the JsonDocsTypeLibrary interface in
Stencil's public TypeScript
declarations.
Using this option could look something like this:
import { Config } from '@stencil/core';
export const config: Config = {
outputTargets: [
{
type: 'docs-json',
file: 'path/to/docs.json',
supplementalPublicTypes: 'src/public-interfaces.ts',
}
]
};
CSS Variables
Stencil can document CSS variables if you add JSDoc-style comments above their declarations in your CSS/SCSS files (see Styling Details for where they're picked up). If, for instance, you had a component with a CSS file like the following:
:host {
/** Background of the button */
--background: pink;
/** Background of the button when activated */
--background-activated: aqua;
/** Background of the button when focused */
--background-focused: fuchsia;
}
Then you'd get the following in the JSON output:
"styles": [
{
"name": "--background",
"annotation": "prop",
"docs": "Background of the button"
},
{
"name": "--background-activated",
"annotation": "prop",
"docs": "Background of the button when activated"
},
{
"name": "--background-focused",
"annotation": "prop",
"docs": "Background of the button when focused"
}
]
If the style sheet is configured to be used with a specific mode, the mode associated with the CSS property will be provided as well:
[
{
"name": "--background",
"annotation": "prop",
"docs": "Background of the button"
+ "mode": "ios",
},
{
"name": "--background-activated",
"annotation": "prop",
"docs": "Background of the button when activated"
+ "mode": "ios",
},
{
"name": "--background-focused",
"annotation": "prop",
"docs": "Background of the button when focused"
+ "mode": "ios",
}
]
This functionality works with both standard CSS and with Sass, although for the latter you'll need to have the @stencil/sass plugin installed and configured.
Slots and CSS Parts
If one of your Stencil components makes use of
slots for
rendering children or CSS Parts for styling,
you can document them by using the @slot and @part JSDoc tags in the
component's comments.
For instance, if you had a my-button component with a slot you might document
it like so:
import { Component } from '@stencil/core';
/**
* @slot buttonContent - Slot for the content of the button
*
* @part button - The button element
*/
@Component({
tag: 'my-button',
styleUrl: 'my-button.css',
encapsulation: { type: 'shadow' },
})
export class MyButton {
render() {
return <button part="button"><slot name="buttonContent"></slot></button>
}
}
This would show up in the generated JSON file like so:
"slots": [
{
"name": "buttonContent",
"docs": "Slot for the content of the button"
}
],
"parts": [
{
"name": "button",
"docs": "The button element"
}
]
Stencil does not check that the slots you document in a component's JSDoc
comment using the @slot tag are actually present in the JSX returned by the
component's render function.
It is up to you as the component author to ensure the @slot tags on a
component are kept up to date.
Custom States
You can document Custom States for your components using the @AttachInternals decorator
and JSDoc comments.
For example:
import { Component, AttachInternals } from '@stencil/core';
/**
* My Element component
*/
@Component({
tag: 'my-element',
styleUrl: 'my-element.css',
encapsulation: { type: 'shadow' },
})
export class MyElement {
@AttachInternals({
states: {
new: true,
/** If this item is older than 6 months old. Use via `:state(archived)` */
archived: false
},
}) internals!: ElementInternals;
This would show up in the generated JSON file like so:
"states": [
{
"name": "new",
"initialValue": true,
"description": ""
},
{
"name": "archived",
"initialValue": false,
"description": "If this item is older than 6 months old. Use via `:state(archived)"
}
]