|
| 1 | +--- |
| 2 | +title: Docs Custom Elements Manifest Output Target |
| 3 | +sidebar_label: CEM (docs-custom-elements-manifest) |
| 4 | +description: Custom Elements Manifest |
| 5 | +slug: /docs-custom-elements-manifest |
| 6 | +--- |
| 7 | + |
| 8 | +# Generating Documentation in Custom Elements Manifest (CEM) format |
| 9 | + |
| 10 | +Since Stencil v4.42, Stencil supports automatically generating a [Custom Elements Manifest (CEM)](https://custom-elements-manifest.open-wc.org/) |
| 11 | +file in your project. The CEM format is a standardized JSON format for describing |
| 12 | +custom elements and is supported by a variety of tools in the web components ecosystem. |
| 13 | + |
| 14 | + |
| 15 | +```tsx title="stencil.config.ts" |
| 16 | +import { Config } from '@stencil/core'; |
| 17 | + |
| 18 | +export const config: Config = { |
| 19 | + outputTargets: [ |
| 20 | + { |
| 21 | + type: 'docs-custom-elements-manifest', |
| 22 | + file: 'path/to/cem.json' |
| 23 | + } |
| 24 | + ] |
| 25 | +}; |
| 26 | +``` |
| 27 | + |
| 28 | +The JSON file output by Stencil conforms to the [CEM Schema interface](https://github.com/webcomponents/custom-elements-manifest/blob/main/schema.d.ts). |
| 29 | + |
| 30 | +## Properties, Methods, Events, and Attributes |
| 31 | + |
| 32 | +The CEM output target includes information about your components' properties, |
| 33 | +methods, events, and attributes based on the decorators you use in your Stencil |
| 34 | +components. |
| 35 | + |
| 36 | +## CSS Properties |
| 37 | + |
| 38 | +Stencil can document CSS variables if you annotate them with JSDoc-style |
| 39 | +comments in your CSS/SCSS files. If, for instance, you had a component with a |
| 40 | +CSS file like the following: |
| 41 | + |
| 42 | +```css title="src/components/my-button/my-button.css" |
| 43 | +:host { |
| 44 | + /** |
| 45 | + * @prop --background: Background of the button |
| 46 | + * @prop --background-activated: Background of the button when activated |
| 47 | + * @prop --background-focused: Background of the button when focused |
| 48 | + */ |
| 49 | + --background: pink; |
| 50 | + --background-activated: aqua; |
| 51 | + --background-focused: fuchsia; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Then you'd get the following in the JSON output: |
| 56 | + |
| 57 | +```json title="Example docs-custom-elements-manifest Output" |
| 58 | +[ |
| 59 | + { |
| 60 | + "cssProperties": [ |
| 61 | + { |
| 62 | + "name": "background", |
| 63 | + "description": "Background of the button" |
| 64 | + }, |
| 65 | + { |
| 66 | + "name": "background-activated", |
| 67 | + "description": "Background of the button when activated" |
| 68 | + }, |
| 69 | + { |
| 70 | + "name": "background-focused", |
| 71 | + "description": "Background of the button when focused" |
| 72 | + } |
| 73 | + ] |
| 74 | + } |
| 75 | +] |
| 76 | +``` |
| 77 | + |
| 78 | +## Slots and CSS Parts |
| 79 | + |
| 80 | +If one of your Stencil components makes use of |
| 81 | +[slots](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot) for |
| 82 | +rendering children or [CSS Parts](https://developer.mozilla.org/en-US/docs/Web/CSS/::part) for styling, |
| 83 | +you can document them by using the `@slot` and `@part` JSDoc tags in the |
| 84 | +component's comments. |
| 85 | + |
| 86 | +For instance, if you had a `my-button` component with a slot you might document |
| 87 | +it like so: |
| 88 | + |
| 89 | +```tsx title="src/components/my-button/my-button.tsx" |
| 90 | +import { Component, h } from '@stencil/core'; |
| 91 | + |
| 92 | +/** |
| 93 | + * @slot buttonContent - Slot for the content of the button |
| 94 | + * |
| 95 | + * @part button - The button element |
| 96 | + */ |
| 97 | +@Component({ |
| 98 | + tag: 'my-button', |
| 99 | + styleUrl: 'my-button.css', |
| 100 | + shadow: true, |
| 101 | +}) |
| 102 | +export class MyButton { |
| 103 | + render() { |
| 104 | + return <button part="button"><slot name="buttonContent"></slot></button> |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +This would show up in the generated JSON file like so: |
| 110 | + |
| 111 | +```json |
| 112 | +"slots": [ |
| 113 | + { |
| 114 | + "name": "buttonContent", |
| 115 | + "description": "Slot for the content of the button" |
| 116 | + } |
| 117 | +], |
| 118 | +"cssParts": [ |
| 119 | + { |
| 120 | + "name": "button", |
| 121 | + "description": "The button element" |
| 122 | + } |
| 123 | +] |
| 124 | +``` |
| 125 | + |
| 126 | +:::caution |
| 127 | +Stencil does not check that the slots you document in a component's JSDoc |
| 128 | +comment using the `@slot` tag are actually present in the JSX returned by the |
| 129 | +component's `render` function. |
| 130 | + |
| 131 | +It is up to you as the component author to ensure the `@slot` tags on a |
| 132 | +component are kept up to date. |
| 133 | +::: |
| 134 | + |
| 135 | +## Custom States |
| 136 | + |
| 137 | +You can document [Custom States](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet) for your components using the `@AttachInternals` decorator |
| 138 | +and JSDoc comments. |
| 139 | + |
| 140 | +For example: |
| 141 | + |
| 142 | +```tsx title="src/components/my-element/my-element.tsx" |
| 143 | +import { Component, h, AttachInternals } from '@stencil/core'; |
| 144 | +/** |
| 145 | + * My Element component |
| 146 | + */ |
| 147 | +@Component({ |
| 148 | + tag: 'my-element', |
| 149 | + styleUrl: 'my-element.css', |
| 150 | + shadow: true, |
| 151 | +}) |
| 152 | +export class MyElement { |
| 153 | + @AttachInternals({ |
| 154 | + states: { |
| 155 | + new: true, |
| 156 | + /** If this item is older than 6 months old. Use via `:state(archived)` */ |
| 157 | + archived: false |
| 158 | + }, |
| 159 | + }) internals!: ElementInternals; |
| 160 | +``` |
| 161 | +
|
| 162 | +This would show up in the generated JSON file like so: |
| 163 | +
|
| 164 | +```json |
| 165 | +"customStates": [ |
| 166 | + { |
| 167 | + "name": "new", |
| 168 | + "description": "" |
| 169 | + }, |
| 170 | + { |
| 171 | + "name": "archived", |
| 172 | + "description": "If this item is older than 6 months old. Use via `:state(archived)" |
| 173 | + } |
| 174 | +] |
| 175 | +``` |
| 176 | +
|
| 177 | +## Demos |
| 178 | +
|
| 179 | +You can save demos for a component in the `usage/` subdirectory within |
| 180 | +that component's directory. The content of these files will be added to the |
| 181 | +`demos` property of the generated JSON. This allows you to keep examples right |
| 182 | +next to the code, making it easy to include them in a documentation site or |
| 183 | +other downstream consumer(s) of your docs. |
| 184 | +
|
| 185 | +:::caution |
| 186 | +Stencil doesn't check that your demos are up-to-date! If you make any |
| 187 | +changes to your component's API you'll need to remember to update your demos |
| 188 | +manually. |
| 189 | +::: |
| 190 | +
|
| 191 | +If, for instance, you had a usage example like this: |
| 192 | +
|
| 193 | +````md title="src/components/my-button/usage/my-button-usage.md" |
| 194 | +# How to use `my-button` |
| 195 | +
|
| 196 | +A button is often a great help in adding interactivity to an app! |
| 197 | +
|
| 198 | +You could use it like this: |
| 199 | +
|
| 200 | +```html |
| 201 | +<my-button>My Button!</my-button> |
| 202 | +``` |
| 203 | +```` |
| 204 | +
|
| 205 | +
|
| 206 | +You'd get the following in the JSON output under the `"usage"` key: |
| 207 | +
|
| 208 | +```json |
| 209 | +"demos": [{ |
| 210 | + "url": "my-button-usage.md", |
| 211 | + "description": "# How to use `my-button`\n\nA button is often a great help in adding interactivity to an app!\n\nYou could use it like this:\n\n```html\n<my-button>My Button!</my-button>\n```\n" |
| 212 | +}] |
| 213 | +``` |
0 commit comments