diff --git a/docs/components/api.md b/docs/components/api.md index 3019c08ef..8d866e650 100644 --- a/docs/components/api.md +++ b/docs/components/api.md @@ -25,6 +25,7 @@ Once all the metadata has been collected, all the decorators are removed from th - [@Listen()](./events.md#listen-decorator) listens for DOM events - [@AttrDeserialize()](./serialization-deserialization.md#the-attrdeserialize-decorator-attrdeserialize) declares a hook to translate a component's attribute string to its JS property - [@PropSerialize()](./serialization-deserialization.md#the-propserialize-decorator-propserialize) declares a hook that translates a component's JS property to its attribute string +- [@AttachInternals()](./attach-internals.md) attaches ElementInternals to a component ## Lifecycle hooks diff --git a/docs/components/attach-internals.md b/docs/components/attach-internals.md new file mode 100644 index 000000000..b624e569e --- /dev/null +++ b/docs/components/attach-internals.md @@ -0,0 +1,68 @@ +--- +title: Attach Internals +sidebar_label: Attach Internals +description: attach internals decorator +slug: /attach-internals +--- + +# Attach Internals Decorator + +The `@AttachInternals` decorator is used to attach [ElementInternals](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals) to a Stencil component. +This allows you to leverage features such as [Custom States](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet) and [Form Association](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals#form_association). + +## Form Association + +Read the dedicated guide on [Form Associated Components](./form-associated.md) and how to use `@AttachInternals` there. + +## Custom States + +You can use the `@AttachInternals` decorator to define [Custom States](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet) for your component. + +```tsx +import { Component, h, AttachInternals } from '@stencil/core'; +/** + * My Element component + */ +@Component({ + tag: 'my-element', + styleUrl: 'my-element.css', +}) +export class MyElement { + @AttachInternals({ + states: { + /** A custom state for my element */ + 'my-custom-state': true, + /** Another custom state for my element */ + 'another-state': false + } + }) internals: ElementInternals; + render() { + return
My Element
; + } +} +``` + +In the example above, we define two custom states: `my-custom-state` and `another-state`. + +The initial values of the custom states can be set via the `states` object and +later on, you can update the states dynamically using the `internals.states` property. For example: + +```tsx +handleClick() { + this.internals.states.add('another-state'); // to add a state + this.internals.states.delete('my-custom-state'); // to remove a state +} +``` + +You or your element users can then use custom states in CSS like so: + +```css +/* Use them internally... */ +:host(:state(my-custom-state)) { + background-color: yellow; +} +/* ... Or use them externally */ +my-element:state(another-state) { + border: 2px solid red; +} +``` diff --git a/docs/components/templating-and-jsx.md b/docs/components/templating-and-jsx.md index cd49442f2..74f92c3a2 100644 --- a/docs/components/templating-and-jsx.md +++ b/docs/components/templating-and-jsx.md @@ -468,6 +468,23 @@ export class AppHome { In this example we are using `ref` to get a reference to our input `ref={(el) => this.textInput = el as HTMLInputElement}`. We can then use that ref to do things such as grab the value from the text input directly `this.textInput.value`. +## Explicitly setting attributes or properties + +By default, Stencil tries to intelligently determine whether to set an attribute or (more commonly) a property on an element when using JSX. +However, in some cases you may want to explicitly set one or the other. You can do this by using the `attr:` or `prop:` prefixes. For example: + +```tsx + +``` + +will set the `value` attribute on the input element, while: + +```tsx + +``` + +will explicitly set the `value` property on the input element. + ## Avoid Shared JSX Nodes diff --git a/docs/documentation-generation/01-overview.md b/docs/documentation-generation/01-overview.md index 2ea2375f6..d080402c2 100644 --- a/docs/documentation-generation/01-overview.md +++ b/docs/documentation-generation/01-overview.md @@ -9,6 +9,7 @@ slug: /doc-generation - [`docs-readme`: Documentation readme files formatted in markdown](./docs-readme.md) - [`docs-json`: Documentation data formatted in JSON](./docs-json.md) +- [`docs-custom-elements-manifest`: Custom Elements Manifest (CEM) format](./docs-custom-elements-manifest.md) - [`docs-custom`: Custom documentation generation](./docs-custom.md) - [`docs-vscode`: Documentation generation for VS Code](./docs-vscode.md) - [`stats`: Stats about the compiled files](./docs-stats.md) diff --git a/docs/documentation-generation/docs-custom-elements-manifest.md b/docs/documentation-generation/docs-custom-elements-manifest.md new file mode 100644 index 000000000..d0bb5ad93 --- /dev/null +++ b/docs/documentation-generation/docs-custom-elements-manifest.md @@ -0,0 +1,213 @@ +--- +title: Docs Custom Elements Manifest Output Target +sidebar_label: CEM (docs-custom-elements-manifest) +description: Custom Elements Manifest +slug: /docs-custom-elements-manifest +--- + +# Generating Documentation in Custom Elements Manifest (CEM) format + +Since Stencil v4.42, Stencil supports automatically generating a [Custom Elements Manifest (CEM)](https://custom-elements-manifest.open-wc.org/) +file in your project. The CEM format is a standardized JSON format for describing +custom elements and is supported by a variety of tools in the web components ecosystem. + + +```tsx title="stencil.config.ts" +import { Config } from '@stencil/core'; + +export const config: Config = { + outputTargets: [ + { + type: 'docs-custom-elements-manifest', + file: 'path/to/cem.json' + } + ] +}; +``` + +The JSON file output by Stencil conforms to the [CEM Schema interface](https://github.com/webcomponents/custom-elements-manifest/blob/main/schema.d.ts). + +## Properties, Methods, Events, and Attributes + +The CEM output target includes information about your components' properties, +methods, events, and attributes based on the decorators you use in your Stencil +components. + +## CSS Properties + +Stencil can document CSS variables if you annotate them with JSDoc-style +comments in your CSS/SCSS files. If, for instance, you had a component with a +CSS file like the following: + +```css title="src/components/my-button/my-button.css" +:host { + /** + * @prop --background: Background of the button + * @prop --background-activated: Background of the button when activated + * @prop --background-focused: Background of the button when focused + */ + --background: pink; + --background-activated: aqua; + --background-focused: fuchsia; +} +``` + +Then you'd get the following in the JSON output: + +```json title="Example docs-custom-elements-manifest Output" +[ + { + "cssProperties": [ + { + "name": "background", + "description": "Background of the button" + }, + { + "name": "background-activated", + "description": "Background of the button when activated" + }, + { + "name": "background-focused", + "description": "Background of the button when focused" + } + ] + } +] +``` + +## Slots and CSS Parts + +If one of your Stencil components makes use of +[slots](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot) for +rendering children or [CSS Parts](https://developer.mozilla.org/en-US/docs/Web/CSS/::part) 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: + +```tsx title="src/components/my-button/my-button.tsx" +import { Component, h } 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', + shadow: true, +}) +export class MyButton { + render() { + return + } +} +``` + +This would show up in the generated JSON file like so: + +```json +"slots": [ + { + "name": "buttonContent", + "description": "Slot for the content of the button" + } +], +"cssParts": [ + { + "name": "button", + "description": "The button element" + } +] +``` + +:::caution +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](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet) for your components using the `@AttachInternals` decorator +and JSDoc comments. + +For example: + +```tsx title="src/components/my-element/my-element.tsx" +import { Component, h, AttachInternals } from '@stencil/core'; +/** + * My Element component + */ +@Component({ + tag: 'my-element', + styleUrl: 'my-element.css', + shadow: true, +}) +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: + +```json +"customStates": [ + { + "name": "new", + "description": "" + }, + { + "name": "archived", + "description": "If this item is older than 6 months old. Use via `:state(archived)" + } +] +``` + +## Demos + +You can save demos for a component in the `usage/` subdirectory within +that component's directory. The content of these files will be added to the +`demos` property of the generated JSON. This allows you to keep examples right +next to the code, making it easy to include them in a documentation site or +other downstream consumer(s) of your docs. + +:::caution +Stencil doesn't check that your demos are up-to-date! If you make any +changes to your component's API you'll need to remember to update your demos +manually. +::: + +If, for instance, you had a usage example like this: + +````md title="src/components/my-button/usage/my-button-usage.md" +# How to use `my-button` + +A button is often a great help in adding interactivity to an app! + +You could use it like this: + +```html +My Button! +``` +```` + + +You'd get the following in the JSON output under the `"usage"` key: + +```json +"demos": [{ + "url": "my-button-usage.md", + "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\nMy Button!\n```\n" +}] +``` diff --git a/docs/documentation-generation/docs-custom.md b/docs/documentation-generation/docs-custom.md index 61da15c79..da64b8a84 100644 --- a/docs/documentation-generation/docs-custom.md +++ b/docs/documentation-generation/docs-custom.md @@ -68,8 +68,8 @@ The generated docs JSON data will in the type of `JsonDocs` which consists of ma | `events` | Array of metadata objects for each usage of the [`@Event` decorator](../components/events.md#event-decorator) on the current component. | | `listeners` | Array of metadata objects for each usage of the [`@Listen` decorator](../components/events.md#listen-decorator) on the current component. | | `styles` | Array of objects documenting annotated [CSS variables](./docs-json.md#css-variables) used in the current component's CSS. | -| `slots` | Array of objects documenting [slots](./docs-json.md#slots) which are tagged with `@slot` in the current component's JSDoc comment. | -| `parts` | Array of objects derived from `@part` tags in the current component's JSDoc comment. | +| `slots` | Array of objects documenting [slots](./docs-json.md#slots-and-css-parts) which are tagged with `@slot` in the current component's JSDoc comment. | +| `parts` | Array of objects documenting [CSS Parts](./docs-json.md#slots-and-css-parts) which are derived from `@part` tags in the current component's JSDoc comment. | | `dependents` | Array of components where current component is used | | `dependencies` | Array of components which is used in current component | | `dependencyGraph` | Describes a tree of components coupling | diff --git a/docs/documentation-generation/docs-json.md b/docs/documentation-generation/docs-json.md index 418c21399..a9a6fccc0 100644 --- a/docs/documentation-generation/docs-json.md +++ b/docs/documentation-generation/docs-json.md @@ -101,7 +101,7 @@ CSS file like the following: Then you'd get the following in the JSON output: ```json title="Example docs-json Output" -[ +"styles": [ { "name": "--background", "annotation": "prop", @@ -153,12 +153,13 @@ latter you'll need to have the and configured. ::: -## Slots +## Slots and CSS Parts If one of your Stencil components makes use of [slots](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot) for -rendering children you can document them by using the `@slot` JSDoc tag in the -component's comment. +rendering children or [CSS Parts](https://developer.mozilla.org/en-US/docs/Web/CSS/::part) 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: @@ -168,6 +169,8 @@ import { Component, h } from '@stencil/core'; /** * @slot buttonContent - Slot for the content of the button + * + * @part button - The button element */ @Component({ tag: 'my-button', @@ -176,7 +179,7 @@ import { Component, h } from '@stencil/core'; }) export class MyButton { render() { - return + return } } ``` @@ -184,10 +187,18 @@ export class MyButton { This would show up in the generated JSON file like so: ```json -"slots": { - "name": "buttonContent", - "docs": "Slot for the content of the button" -} +"slots": [ + { + "name": "buttonContent", + "docs": "Slot for the content of the button" + } +], +"parts": [ + { + "name": "button", + "docs": "The button element" + } +] ``` :::caution @@ -199,6 +210,49 @@ 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](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet) for your components using the `@AttachInternals` decorator +and JSDoc comments. + +For example: + +```tsx title="src/components/my-element/my-element.tsx" +import { Component, h, AttachInternals } from '@stencil/core'; +/** + * My Element component + */ +@Component({ + tag: 'my-element', + styleUrl: 'my-element.css', + shadow: true, +}) +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: + +```json +"states": [ + { + "name": "new", + "initialValue": true, + "description": "" + }, + { + "name": "archived", + "initialValue": false, + "description": "If this item is older than 6 months old. Use via `:state(archived)" + } +] +``` ## Usage diff --git a/docs/documentation-generation/docs-readme.md b/docs/documentation-generation/docs-readme.md index 54e4967b6..6a692047f 100644 --- a/docs/documentation-generation/docs-readme.md +++ b/docs/documentation-generation/docs-readme.md @@ -466,6 +466,44 @@ The shadow parts section will be placed after the [@Slot Details](#slot-details) If a component's top-level JSDoc does not use `@part` tags, this section will be omitted from the generated README. +### Custom States + +You can document [Custom States](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet) for your components using the `@AttachInternals` decorator +and JSDoc comments. + +For example: + +```tsx title="src/components/my-element/my-element.tsx" +import { Component, h, AttachInternals } from '@stencil/core'; +/** + * My Element component + */ +@Component({ + tag: 'my-element', + styleUrl: 'my-element.css', + shadow: true, +}) +export class MyElement { + @AttachInternals({ + states: { + new: true, + /** If this item is older than 6 months old */ + archived: false + }, + }) internals!: ElementInternals; +``` + +The following table will be generated: + +```md +## Custom States + +| State | Initial Value | Description | +| ------------------ | ------------- | ---------------------------------------------------------- | +| `:state(new)` | `true` | | +| `:state(archived)` | `false` | If this item is older than 6 months old | +``` + ### Styling Details Styling in CSS files can be documented in Stencil components as well. diff --git a/versioned_docs/version-v4.42/build-variables.md b/versioned_docs/version-v4.42/build-variables.md new file mode 100644 index 000000000..30cc41d9e --- /dev/null +++ b/versioned_docs/version-v4.42/build-variables.md @@ -0,0 +1,48 @@ +--- +title: Build Constants +description: Stencil has a number of add-ons that you can use with the build process. +slug: /build-variables +--- + +# Build Constants + +Build Constants in Stencil allow you to run specific code only when Stencil is running in development mode. This code is stripped from your bundles when doing a production build, therefore keeping your bundles as small as possible. + +### Using Build Constants + +Lets dive in and look at an example of how to use our build constants: + +```tsx +import { Component, Build } from '@stencil/core'; + +@Component({ + tag: 'stencil-app', + styleUrl: 'stencil-app.scss' +}) +export class StencilApp { + + componentDidLoad() { + if (Build.isDev) { + console.log('im in dev mode'); + } else { + console.log('im running in production'); + } + + if (Build.isBrowser) { + console.log('im in the browser'); + } else { + console.log('im in prerendering (server)'); + } + } +} +``` + +As you can see from this example, we just need to import `Build` from `@stencil/core` and then we can use the `isDev` constant to detect when we are running in dev mode or production mode. + +### Use Cases + +Some use cases we have come up with are: + +- Diagnostics code that runs in dev to make sure logic is working like you would expect +- `console.log()`'s that may be useful for debugging in dev mode but that you don't want to ship +- Disabling auth checks when in dev mode diff --git a/versioned_docs/version-v4.42/components/_category_.json b/versioned_docs/version-v4.42/components/_category_.json new file mode 100644 index 000000000..57bbce615 --- /dev/null +++ b/versioned_docs/version-v4.42/components/_category_.json @@ -0,0 +1,4 @@ +{ + "label": "Components", + "position": 2 +} diff --git a/versioned_docs/version-v4.42/components/api.md b/versioned_docs/version-v4.42/components/api.md new file mode 100644 index 000000000..8d866e650 --- /dev/null +++ b/versioned_docs/version-v4.42/components/api.md @@ -0,0 +1,312 @@ +--- +title: Component API +sidebar_label: API +description: Component API +slug: /api +--- + +# Component API + +The whole API provided by stencil can be condensed in a set of decorators, lifecycles hooks and rendering methods. + + +## Decorators + +Decorators are a pure compiler-time construction used by stencil to collect all the metadata about a component, the properties, attributes and methods it might expose, the events it might emit or even the associated stylesheets. +Once all the metadata has been collected, all the decorators are removed from the output, so they don't incur any runtime overhead. + +- [@Component()](./component.md) declares a new web component +- [@Prop()](./properties.md#the-prop-decorator-prop) declares an exposed property/attribute +- [@State()](./state.md#the-state-decorator-state) declares an internal state of the component +- [@Watch()](./reactive-data.md#the-watch-decorator-watch) declares a hook that runs when a property or state changes +- [@Element()](./host-element.md#element-decorator) declares a reference to the host element +- [@Method()](./methods.md) declares an exposed public method +- [@Event()](./events.md#event-decorator) declares a DOM event the component might emit +- [@Listen()](./events.md#listen-decorator) listens for DOM events +- [@AttrDeserialize()](./serialization-deserialization.md#the-attrdeserialize-decorator-attrdeserialize) declares a hook to translate a component's attribute string to its JS property +- [@PropSerialize()](./serialization-deserialization.md#the-propserialize-decorator-propserialize) declares a hook that translates a component's JS property to its attribute string +- [@AttachInternals()](./attach-internals.md) attaches ElementInternals to a component + + +## Lifecycle hooks + +- [connectedCallback()](./component-lifecycle.md#connectedcallback) +- [disconnectedCallback()](./component-lifecycle.md#disconnectedcallback) +- [componentWillLoad()](./component-lifecycle.md#componentwillload) +- [componentDidLoad()](./component-lifecycle.md#componentdidload) +- [componentShouldUpdate(newValue, oldValue, propName): boolean](./component-lifecycle.md#componentshouldupdate) +- [componentWillRender()](./component-lifecycle.md#componentwillrender) +- [componentDidRender()](./component-lifecycle.md#componentdidrender) +- [componentWillUpdate()](./component-lifecycle.md#componentwillupdate) +- [componentDidUpdate()](./component-lifecycle.md#componentdidupdate) +- **[render()](./templating-and-jsx.md)** + +## componentOnReady() + +This isn't a true "lifecycle" method that would be declared on the component class definition, but instead is a utility method that +can be used by an implementation consuming your Stencil component to detect when a component has finished its first render cycle. + +This method returns a promise which resolves after `componentDidRender()` on the _first_ render cycle. + +:::note +`componentOnReady()` only resolves once per component lifetime. If you need to hook into subsequent render cycle, use +`componentDidRender()` or `componentDidUpdate()`. +::: + +Executing code after `componentOnReady()` resolves could look something like this: + +```ts +// Get a reference to the element +const el = document.querySelector('my-component'); + +el.componentOnReady().then(() => { + // Place any code in here you want to execute when the component is ready + console.log('my-component is ready'); +}); +``` + +The availability of `componentOnReady()` depends on the component's compiled output type. This method is only available for lazy-loaded +distribution types ([`dist`](../output-targets/dist.md) and [`www`](../output-targets/www.md)) and, as such, is not available for +[`dist-custom-elements`](../output-targets/custom-elements.md) output. If you want to simulate the behavior of `componentOnReady()` for non-lazy builds, +you can implement a helper method to wrap the functionality similar to what the Ionic Framework does [here](https://github.com/ionic-team/ionic-framework/blob/main/core/src/utils/helpers.ts#L60-L79). + +## The `appload` event + +In addition to component-specific lifecycle hooks, a special event called `appload` will be emitted when the app and all of its child components have finished loading. You can listen for it on the `window` object. + +If you have multiple apps on the same page, you can determine which app emitted the event by checking `event.detail.namespace`. This will be the value of the [namespace config option](../config/01-overview.md#namespace) you've set in your Stencil config. + +```tsx +window.addEventListener('appload', (event) => { + console.log(event.detail.namespace); +}); +``` + +## Other + +The following primitives can be imported from the `@stencil/core` package and used within the lifecycle of a component: + +### [**Host**](./host-element.md): + +``, is a functional component that can be used at the root of the render function to set attributes and event listeners to the host element itself. Refer to the [Host Element](./host-element.md) page for usage info. + +### **Fragment**: + +``, often used via `<>...` syntax, lets you group elements without a wrapper node. + + To use this feature, ensure that the following TypeScript compiler options are set: + - [`jsxFragmentFactory` is set](https://www.typescriptlang.org/tsconfig#jsxFragmentFactory) to "Fragment" + - [`jsxFactory` is set](https://www.typescriptlang.org/tsconfig#jsxFactory) to "h" + + __Type:__ `FunctionalComponent`
+ __Example:__ + ```tsx + import { Component, Fragment, h } from '@stencil/core' + @Component({ + tag: 'cmp-fragment', + }) + export class CmpFragment { + render() { + return ( + <> +
...
+
...
+
...
+ + ); + } + } + ``` + +### [**h()**](./templating-and-jsx.md): + +Turns JSX syntax into Virtual DOM elements. Read more on the [Templating and JSX](./templating-and-jsx.md#the-h-and-fragment-functions) page. + +### **render()**: + +A utility method to render a virtual DOM created by `h()` into a container. + + __Type:__ `(vnode: VNode, container: Element) => void` + __Example:__ + ```tsx + import { render } from '@stencil/core' + const vdom = ( +
Hello World!
+ ) + render(vdom, document.body) + ``` + +### [**readTask()**](https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing): + +Schedules a DOM-read task. The provided callback will be executed in the best moment to perform DOM reads without causing layout thrashing. + + __Type:__ `(task: Function) => void` + +### [**writeTask()**](https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing): + +Schedules a DOM-write task. The provided callback will be executed in the best moment to perform DOM mutations without causing layout thrashing. + + __Type:__ `(task: Function) => void` + +### **forceUpdate()**: + +Schedules a new render of the given instance or element even if no state changed. Notice `forceUpdate()` is not synchronous and might perform the DOM render in the next frame. + + __Type:__ `(ref: any) => void`
+ __Example:__ + ```ts + import { forceUpdate } from '@stencil/core' + + // inside a class component function + forceUpdate(this); + ``` + +### **getAssetPath()**: + + Gets the path to local assets. Refer to the [Assets](../guides/assets.md#getassetpath) page for usage info. + + __Type:__ `(path: string) => string`
+ __Example:__ + ```tsx + import { Component, Prop, getAssetPath, h } from '@stencil/core' + @Component({ + tag: 'cmp-asset', + }) + export class CmpAsset { + @Prop() icon: string; + + render() { + return ( + + ); + } + } + ``` + +### **setAssetPath()**: + +Sets the path for Stencil to resolve local assets. Refer to the [Assets](../guides/assets.md#setassetpath) page for usage info. + + __Type:__ `(path: string) => string`
+ __Example:__ + ```ts + import { setAssetPath } from '@stencil/core'; + setAssetPath(`{window.location.origin}/`); + ``` + + ### **setMode()**: + + Sets the style mode of a component. Refer to the [Styling](./styling.md#style-modes) page for usage info. + + __Type:__ `((elm: HTMLElement) => string | undefined | null) => void`
+ __Example:__ + ```ts + import { setMode } from '@stencil/core' + + // set mode based on a property + setMode((el) => el.getAttribute('mode')); + ``` + +### **getMode()**: + +Get the current style mode of your application. Refer to the [Styling](./styling.md#style-modes) page for usage info. + + __Type:__ `(ref: any) => string | undefined`
+ __Example:__ + ```ts + import { getMode } from '@stencil/core' + + getMode(this); + ``` + +### **getElement()**: + +Retrieve a Stencil element for a given reference. + + __Type:__ `(ref: any) => HTMLStencilElement`
+ __Example:__ + ```ts + import { getElement } from '@stencil/core' + + const stencilComponent = getElement(document.querySelector('my-cmp')) + if (stencilComponent) { + stencilComponent.componentOnReady().then(() => { ... }) + } + ``` + + ### **resolveVar()**: + + Because Stencil's decorators rely heavily on static analysis, you cannot use dynamic variables within them. `resolveVar` provides a deterministic way to find the string value of a given variable at compile time: + + __Type:__ `(variable: T) => string`
+ __Example:__ + ```tsx + import { Listen, Event, resolveVar } from '@stencil/core` + + const COMPONENT_A_EVENT: string = 'componentAEvent'; + const EVENTS = { + COMPONENT_B_EVENT: 'componentBEvent', + }; + + // inside an @Component class + + @Component({ + tag: 'dynamic-event-names', + }) + export class DynamicEventNames { + @Event({ eventName: resolveVar(COMPONENT_A_EVENT) }) myEvent; + + @Listen(resolveVar(EVENTS.COMPONENT_B_EVENT)) + listenHandler(){ + // + } + } + ``` + +### **Mixin()**: + +Compose multiple classes into a single constructor using factory functions. + + __Type:__ + ```ts + ( + ...mixinFactories: TMixins + ): abstract new (...args: any[]) => UnionToIntersection>>; + ``` + __Example:__ + ```ts + import { Mixin, MixedInCtor, Component, h, Prop, State } from '@stencil/core' + + const aFactory = (Base: B) => { + class A extends Base { propA = 'A' }; + return A; + } + const bFactory = (Base: B) => { + class B extends Base { @Prop() propB = 'B' }; + return B; + } + const cFactory = (Base: B) => { + class C extends Base { @State() propC = 'C' }; + return C; + } + + @Component({ + tag: 'its-mixing-time', + }) + export class X extends Mixin(aFactory, bFactory, cFactory) { + render() { + return
{this.propA} {this.propB} {this.propC}
+ } + } + ``` + +:::caution +If your Stencil component library uses `Mixin()` (or `extends`) and *might* be used by other Stencil component libraries, ensure that all mixed-in factories are imported directly and **not** via [barrel files](https://basarat.gitbook.io/typescript/main-1/barrel). +The static-analysis that Stencil uses to find mixed-in classes does not work within 3rd party (node_module) barrel files. +::: + +For detailed guidance on using `Mixin()` and `extends` for component architecture, including when to use inheritance vs composition patterns, see the [Extends & Mixins](../guides/extends.md) guide. + +### [**setTagTransformer()** and **transformTag()**](../guides/tag-transformation.md): + +Manage tag name transformation at runtime. Refer to the [Tag Transformation](../guides/tag-transformation.md) page for usage info. \ No newline at end of file diff --git a/versioned_docs/version-v4.42/components/attach-internals.md b/versioned_docs/version-v4.42/components/attach-internals.md new file mode 100644 index 000000000..b624e569e --- /dev/null +++ b/versioned_docs/version-v4.42/components/attach-internals.md @@ -0,0 +1,68 @@ +--- +title: Attach Internals +sidebar_label: Attach Internals +description: attach internals decorator +slug: /attach-internals +--- + +# Attach Internals Decorator + +The `@AttachInternals` decorator is used to attach [ElementInternals](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals) to a Stencil component. +This allows you to leverage features such as [Custom States](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet) and [Form Association](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals#form_association). + +## Form Association + +Read the dedicated guide on [Form Associated Components](./form-associated.md) and how to use `@AttachInternals` there. + +## Custom States + +You can use the `@AttachInternals` decorator to define [Custom States](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet) for your component. + +```tsx +import { Component, h, AttachInternals } from '@stencil/core'; +/** + * My Element component + */ +@Component({ + tag: 'my-element', + styleUrl: 'my-element.css', +}) +export class MyElement { + @AttachInternals({ + states: { + /** A custom state for my element */ + 'my-custom-state': true, + /** Another custom state for my element */ + 'another-state': false + } + }) internals: ElementInternals; + render() { + return
My Element
; + } +} +``` + +In the example above, we define two custom states: `my-custom-state` and `another-state`. + +The initial values of the custom states can be set via the `states` object and +later on, you can update the states dynamically using the `internals.states` property. For example: + +```tsx +handleClick() { + this.internals.states.add('another-state'); // to add a state + this.internals.states.delete('my-custom-state'); // to remove a state +} +``` + +You or your element users can then use custom states in CSS like so: + +```css +/* Use them internally... */ +:host(:state(my-custom-state)) { + background-color: yellow; +} +/* ... Or use them externally */ +my-element:state(another-state) { + border: 2px solid red; +} +``` diff --git a/versioned_docs/version-v4.42/components/component-lifecycle.md b/versioned_docs/version-v4.42/components/component-lifecycle.md new file mode 100644 index 000000000..72609e476 --- /dev/null +++ b/versioned_docs/version-v4.42/components/component-lifecycle.md @@ -0,0 +1,182 @@ +--- +title: Component Lifecycle Methods +sidebar_label: Lifecycle Methods +description: Component Lifecycle Methods +slug: /component-lifecycle +--- + +# Component Lifecycle Methods + +Components have numerous lifecycle methods which can be used to know when the component "will" and "did" load, update, and render. These methods can be added to a component to hook into operations at the right time. + +Implement one of the following methods within a component class and Stencil will automatically call them in the right order: + +import LifecycleMethodsChart from '@site/src/components/LifecycleMethodsChart'; + + + +## connectedCallback() + +Called every time the component is connected to the DOM. +When the component is first connected, this method is called before `componentWillLoad`. + +It's important to note that this method can be called more than once, every time, the element is **attached** or **moved** in the DOM. For logic that needs to run every time the element is attached or moved in the DOM, it is considered a best practice to use this lifecycle method. + +```tsx +const el = document.createElement('my-cmp'); +document.body.appendChild(el); +// connectedCallback() called +// componentWillLoad() called (first time) + +el.remove(); +// disconnectedCallback() + +document.body.appendChild(el); +// connectedCallback() called again, but `componentWillLoad()` is not. +``` + + +This `lifecycle` hook follows the same semantics as the one described by the [Custom Elements Spec](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) + +## disconnectedCallback() + +Called every time the component is disconnected from the DOM, ie, it can be dispatched more than once, DO not confuse with a "onDestroy" kind of event. + +This `lifecycle` hook follows the same semantics as the one described by the [Custom Elements Spec](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements). + +## componentWillLoad() + +Called once just after the component is first connected to the DOM. Since this method is only called once, it's a good place to load data asynchronously and to setup the state without triggering extra re-renders. + +A promise can be returned, that can be used to wait for the first `render()`. + +## componentDidLoad() + +Called once just after the component is fully loaded and the first `render()` occurs. + +## componentShouldUpdate() + +This hook is called when a component's [`Prop`](./properties.md) or [`State`](./state.md) property changes and a rerender is about to be requested. This hook receives three arguments: the new value, the old value and the name of the changed state. It should return a boolean to indicate if the component should rerender (`true`) or not (`false`). + +A couple of things to notice is that this method will not be executed before the initial render, that is, when the component is first attached to the dom, nor when a rerender is already scheduled in the next frame. + +Let’s say the following two props of a component change synchronously: + +```tsx +component.somePropA = 42; +component.somePropB = 88; +``` + +The `componentShouldUpdate` will be first called with arguments: `42`, `undefined` and `somePropA`. If it does return `true`, the hook will not be called again since the rerender is already scheduled to happen. Instead, if the first hook returned `false`, then `componentShouldUpdate` will be called again with `88`, `undefined` and `somePropB` as arguments, triggered by the `component.somePropB = 88` mutation. + +Since the execution of this hook might be conditioned, it's not good to rely on it to watch for prop changes, instead use the `@Watch` decorator for that. + +## componentWillRender() + +Called before every `render()`. + +A promise can be returned, that can be used to wait for the upcoming render. + +## componentDidRender() + +Called after every `render()`. + + +## componentWillUpdate() + +Called when the component is about to be updated because some `Prop()` or `State()` changed. +It's never called during the first `render()`. + +A promise can be returned, that can be used to wait for the next render. + + +## componentDidUpdate() + +Called just after the component updates. +It's never called during the first `render()`. + + +## Rendering State + +It's always recommended to make any rendered state updates within `componentWillRender()`, since this is the method which get called _before_ the `render()` method. Alternatively, updating rendered state with the `componentDidLoad()`, `componentDidUpdate()` and `componentDidRender()` methods will cause another rerender, which isn't ideal for performance. + +If state _must_ be updated in `componentDidUpdate()` or `componentDidRender()`, it has the potential of getting components stuck in an infinite loop. If updating state within `componentDidUpdate()` is unavoidable, then the method should also come with a way to detect if the props or state is "dirty" or not (is the data actually different or is it the same as before). By doing a dirty check, `componentDidUpdate()` is able to avoid rendering the same data, and which in turn calls `componentDidUpdate()` again. + + +## Lifecycle Hierarchy + +A useful feature of lifecycle methods is that they take their child component's lifecycle into consideration too. For example, if the parent component, `cmp-a`, has a child component, `cmp-b`, then `cmp-a` isn't considered "loaded" until `cmp-b` has finished loading. Another way to put it is that the deepest components finish loading first, then the `componentDidLoad()` calls bubble up. + +It's also important to note that even though Stencil can lazy-load components, and has asynchronous rendering, the lifecycle methods are still called in the correct order. So while the top-level component could have already been loaded, all of its lifecycle methods are still called in the correct order, which means it'll wait for a child components to finish loading. The same goes for the exact opposite, where the child components may already be ready while the parent isn't. + +In the example below we have a simple hierarchy of components. The numbered list shows the order of which the lifecycle methods will fire. + +```markup + + + + + +``` + +1. `cmp-a` - `componentWillLoad()` +2. `cmp-b` - `componentWillLoad()` +3. `cmp-c` - `componentWillLoad()` +4. `cmp-c` - `componentDidLoad()` +5. `cmp-b` - `componentDidLoad()` +6. `cmp-a` - `componentDidLoad()` + +Even if some components may or may not be already loaded, the entire component hierarchy waits on its child components to finish loading and rendering. + + +## Async Lifecycle Methods + +Some lifecycle methods, e.g. `componentWillRender`, `componentWillLoad` and `componentWillUpdate`, can also return promises which allows the method to asynchronously retrieve data or perform any async tasks. A great example of this is fetching data to be rendered in a component. For example, this very site you're reading first fetches content data before rendering. But because `fetch()` is async, it's important that `componentWillLoad()` returns a `Promise` to ensure its parent component isn't considered "loaded" until all of its content has rendered. + +Below is a quick example showing how `componentWillLoad()` is able to have its parent component wait on it to finish loading its data. + +```tsx +componentWillLoad() { + return fetch('/some-data.json') + .then(response => response.json()) + .then(data => { + this.content = data; + }); +} +``` + +## Example + +This simple example shows a clock and updates the current time every second. The timer is started when the component is added to the DOM. Once it's removed from the DOM, the timer is stopped. + +```tsx +import { Component, State, h } from '@stencil/core'; + +@Component({ + tag: 'custom-clock' +}) +export class CustomClock { + + timer: number; + + @State() time: number = Date.now(); + + connectedCallback() { + this.timer = window.setInterval(() => { + this.time = Date.now(); + }, 1000); + } + + disconnectedCallback() { + window.clearInterval(this.timer); + } + + render() { + const time = new Date(this.time).toLocaleTimeString(); + + return ( + { time } + ); + } +} +``` diff --git a/versioned_docs/version-v4.42/components/component.md b/versioned_docs/version-v4.42/components/component.md new file mode 100644 index 000000000..c3b97580e --- /dev/null +++ b/versioned_docs/version-v4.42/components/component.md @@ -0,0 +1,400 @@ +--- +title: Component Decorator +sidebar_label: Component +description: Documentation for the @Component decorator +slug: /component +--- + +# Component Decorator + +`@Component()` is a decorator that designates a TypeScript class as a Stencil component. +Every Stencil component gets transformed into a web component at build time. + +```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + // additional options +}) +export class TodoList { + // implementation omitted +} +``` + +## Component Options + +The `@Component()` decorator takes one argument, an object literal containing configuration options for the component. +This allows each component to be individually configured to suit the unique needs of each project. + +Each option, its type, and whether it's required is described below. + +### tag + +**Required** + +**Type: `string`** + +**Details:**
+This value sets the name of the custom element that Stencil will generate. +To adhere to the [HTML spec](https://html.spec.whatwg.org/#valid-custom-element-name), the tag name must contain a dash ('-'). + +Ideally, the tag name is a globally unique value. +Having a globally unique value helps prevent naming collisions with the global `CustomElementsRegistry`, where all custom elements are defined. +It's recommended to choose a unique prefix for all your components within the same collection. + +**Example**:
+```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', +}) +export class TodoList { + // implementation omitted +} +``` +After compilation, the component defined in `TodoList` can be used in HTML or another TSX file: +```html + + +``` +```tsx +{/* Here we use the component in a TSX file */} + +``` + +### assetsDirs + +**Optional** + +**Type: `string[]`** + +**Details:**
+`assetsDirs` is an array of relative paths from the component to a directory containing the static files (assets) the component requires. + +**Example**:
+Below is an example project's directory structure containing an example component and assets directory. + +``` +src/ +└── components/ + ├── assets/ + │ └── sunset.jpg + └── todo-list.tsx +``` + +Below, the `todo-list` component will correctly load the `sunset.jpg` image from the `assets/` directory, using Stencil's [`getAssetPath()`](../guides/assets.md#getassetpath). + +```tsx +import { Component, Prop, getAssetPath, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + // 1. assetsDirs lists the 'assets' directory as a relative (sibling) + // directory + assetsDirs: ['assets'] +}) +export class TodoList { + image = "sunset.jpg"; + + render() { + // 2. the asset path is retrieved relative to the asset base path to use in + // the tag + const imageSrc = getAssetPath(`./assets/${this.image}`); + return + } +} +``` + +In the example above, the following allows `todo-list` to display the provided asset: +1. The `TodoList`'s `@Component()` decorator has the `assetsDirs` property, and lists the file's sibling directory, `assets/`. + This will copy the `assets` directory over to the distribution directory. +2. Stencil's [`getAssetPath()`](../guides/assets.md#getassetpath) is used to retrieve the path to the image to be used in the `` tag + +For more information on configuring assets, please see Stencil's [Assets Guide](../guides/assets.md) + + +### formAssociated + +**Optional** + +**Type: `boolean`** + +**Default: `false`** + +If `true` the component will be +[form-associated](https://html.spec.whatwg.org/dev/custom-elements.html#form-associated-custom-element), +allowing you to take advantage of the +[`ElementInternals`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/attachInternals) +API to enable your Stencil component to participate in forms. + +A minimal form-associated Stencil component could look like this: + +```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'form-associated', + formAssociated: true +}) +export class FormAssociated { + render() { + return form associated! + } +} +``` + +See the documentation for [form-associated components](./form-associated.md) +for more info and examples. + +### scoped + +**Optional** + +**Type: `boolean`** + +**Default: `false`** + +**Details:**
+If `true`, the component will use [scoped stylesheets](./styling.md#scoped-css). + +Scoped CSS is an alternative to using the native [shadow DOM](./styling.md#shadow-dom) style encapsulation. +It appends a data attribute to your styles to make them unique and thereby scope them to your component. +It does not, however, prevent styles from the light DOM from seeping into your component. + +To use the native [shadow DOM](./styling.md#shadow-dom), see the configuration for [`shadow`](#shadow). + +This option cannot be set to `true` if `shadow` is enabled. + +**Example**:
+```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + scoped: true +}) +export class TodoList { + // implementation omitted +} +``` + +### shadow + +**Optional** + +**Type: `boolean | { delegatesFocus?: boolean, slotAssignment?: 'manual' | 'named' }`** + +**Default: `false`** + +**Details:**
+If `true`, the component will use [native Shadow DOM encapsulation](./styling.md#shadow-dom). +It will fall back to `scoped` if the browser does not support shadow-dom natively. + +`delegatesFocus` is a property that [provides focus](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/delegatesFocus) to the first focusable entry in a component using Shadow DOM. +If an object literal containing `delegatesFocus` is provided, the component will use [native Shadow DOM encapsulation](./styling.md#shadow-dom), regardless of the value assigned to `delegatesFocus`. + +When `delegatesFocus` is set to `true`, the component will have `delegatesFocus: true` added to its shadow DOM. + +When `delegatesFocus` is `true` and a non-focusable part of the component is clicked: +- the first focusable part of the component is given focus +- the component receives any available `focus` styling + +When `slotAssignment` is set to `'manual'` or `'named'`, the component will have `slotAssignment` added to its shadow DOM. + +`slotAssignment: 'manual'` enables [manual slot assignment](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/slotAssignment) (otherwise known as [imperative slot assignment](https://github.com/WICG/webcomponents/blob/gh-pages/proposals/Imperative-Shadow-DOM-Distribution-API.md)) for the component. + +If `shadow` is set to `false`, the component will not use native shadow DOM encapsulation. + +This option cannot be set to enabled if `scoped` is enabled. + +**Example 1**:
+```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + shadow: true +}) +export class TodoList { + // implementation omitted +} +``` + +**Example 2**:
+```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + shadow: { + delegatesFocus: true, + } +}) +export class TodoList { + // implementation omitted +} +``` + +### styleUrl + +**Optional** + +**Type: `string`** + +**Details:**
+Relative URL to an external stylesheet containing styles to apply to your component. +Out of the box, Stencil will only process CSS files (files ending with `.css`). +Support for additional CSS variants, like Sass, can be added via [a plugin](https://stenciljs.com/docs/plugins#related-plugins). + +**Example**:
+Below is an example project's directory structure containing an example component and stylesheet. +``` +src/ +└── components/ + ├── todo-list.css + └── todo-list.tsx +``` + +By setting `styleUrl`, Stencil will apply the `todo-list.css` stylesheet to the `todo-list` component: + +```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + styleUrl: './todo-list.css', +}) +export class TodoList { + // implementation omitted +} +``` + +### styleUrls + +**Optional** + +**Type: `string[] | { [modeName: string]: string | string[]; }`** + +**Details:**
+A list of relative URLs to external stylesheets containing styles to apply to your component. + +Alternatively, an object can be provided that maps a named "mode" to one or more stylesheets. + +Out of the box, Stencil will only process CSS files (ending with `.css`). +Support for additional CSS variants, like Sass, can be added via [a plugin](https://stenciljs.com/docs/plugins#related-plugins). + +**Example**:
+Below is an example project's directory structure containing an example component and stylesheet. +``` +src/ +└── components/ + ├── todo-list-1.css + ├── todo-list-2.css + └── todo-list.tsx +``` + +By setting `styleUrls`, Stencil will apply both stylesheets to the `todo-list` component: + +```tsx title="Using an array of styles" +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + styleUrls: ['./todo-list-1.css', './todo-list-2.css'] +}) +export class TodoList { + // implementation omitted +} +``` + +```tsx title="Using modes" +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + styleUrls: { + ios: 'todo-list-1.ios.scss', + md: 'todo-list-2.md.scss', + } +}) +export class TodoList { + // implementation omitted +} +``` + +Read more on styling modes in the Components [Styling](./styling.md#style-modes) section. + +### styles + +**Optional** + +**Type: `string | { [modeName: string]: any }`** + +**Details:**
+A string that contains inlined CSS instead of using an external stylesheet. +The performance characteristics of this feature are the same as using an external stylesheet. + +When using `styles`, only CSS is permitted. +See [`styleUrl`](#styleurl) if you need more advanced features. + +**Example**:
+```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'todo-list', + styles: 'div { background-color: #fff }' +}) +export class TodoList { + // implementation omitted +} +``` + +## Embedding or Nesting Components + +Components can be composed easily by adding the HTML tag to the JSX code. Since the components are just HTML tags, nothing needs to be imported to use a Stencil component within another Stencil component. + +Here's an example of using a component within another component: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'my-embedded-component' +}) +export class MyEmbeddedComponent { + @Prop() color: string = 'blue'; + + render() { + return ( +
My favorite color is {this.color}
+ ); + } +} +``` + +```tsx +import { Component, h } from '@stencil/core'; + +@Component({ + tag: 'my-parent-component' +}) +export class MyParentComponent { + + render() { + return ( +
+ +
+ ); + } +} +``` + +The `my-parent-component` includes a reference to the `my-embedded-component` in the `render()` function. + +## Component Architecture Patterns + +Stencil supports advanced component architecture patterns including class inheritance and composition. Learn more about organizing component logic with controllers in the [Extends & Mixins](../guides/extends.md) guide. diff --git a/versioned_docs/version-v4.42/components/events.md b/versioned_docs/version-v4.42/components/events.md new file mode 100644 index 000000000..96ed92f07 --- /dev/null +++ b/versioned_docs/version-v4.42/components/events.md @@ -0,0 +1,225 @@ +--- +title: Events +sidebar_label: Events +description: Events +slug: /events +--- + +# Events + +There is **NOT** such a thing as *stencil events*, instead, Stencil encourages the use of [DOM events](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events). +However, Stencil does provide an API to specify the events a component can emit, and the events a component listens to. It does so with the `Event()` and `Listen()` decorators. + +## Event Decorator + +Components can emit data and events using the Event Emitter decorator. + +To dispatch [Custom DOM events](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events) for other components to handle, use the `@Event()` decorator. + +```tsx +import { Event, EventEmitter } from '@stencil/core'; + +... +export class TodoList { + + @Event() todoCompleted: EventEmitter; + + todoCompletedHandler(todo: Todo) { + this.todoCompleted.emit(todo); + } +} +``` + +The code above will dispatch a custom DOM event called `todoCompleted`. + +The `Event(opts: EventOptions)` decorator optionally accepts an options object to shape the behavior of dispatched events. The options and defaults are described below. + +```tsx +export interface EventOptions { + /** + * A string custom event name to override the default. + */ + eventName?: string; + /** + * A Boolean indicating whether the event bubbles up through the DOM or not. + */ + bubbles?: boolean; + + /** + * A Boolean indicating whether the event is cancelable. + */ + cancelable?: boolean; + + /** + * A Boolean value indicating whether or not the event can bubble across the boundary between the shadow DOM and the regular DOM. + */ + composed?: boolean; +} +``` + +Example: + +```tsx +import { Event, EventEmitter } from '@stencil/core'; + +... +export class TodoList { + + // Event called 'todoCompleted' that is "composed", "cancellable" and it will bubble up! + @Event({ + eventName: 'todoCompleted', + composed: true, + cancelable: true, + bubbles: true, + }) todoCompleted: EventEmitter; + + todoCompletedHandler(todo: Todo) { + const event = this.todoCompleted.emit(todo); + if(!event.defaultPrevented) { + // if not prevented, do some default handling code + } + } +} +``` + +:::note +In the case where the Stencil `Event` type conflicts with the native web `Event` type, there are two possible solutions: + +1. Import aliasing: +```tsx +import { Event as StencilEvent, EventEmitter } from '@stencil/core'; + +@StencilEvent() myEvent: EventEmitter<{value: string, ev: Event}>; +``` + +2. Namespace the native web `Event` type with `globalThis`: +```tsx +@Event() myEvent: EventEmitter<{value: string, ev: globalThis.Event}>; +``` +::: + +## Listen Decorator + +The `Listen()` decorator is for listening to DOM events, including the ones dispatched from `@Events`. The event listeners are automatically added and removed when the component gets added or removed from the DOM. + +In the example below, assume that a child component, `TodoList`, emits a `todoCompleted` event using the `EventEmitter`. + +```tsx +import { Listen } from '@stencil/core'; + +... +export class TodoApp { + + @Listen('todoCompleted') + todoCompletedHandler(event: CustomEvent) { + console.log('Received the custom todoCompleted event: ', event.detail); + } +} +``` + +### Listen's options + +The `@Listen(eventName, opts?: ListenOptions)` includes a second optional argument that can be used to configure how the DOM event listener is attached. + +```tsx +export interface ListenOptions { + target?: 'body' | 'document' | 'window'; + capture?: boolean; + passive?: boolean; +} +``` + +The available options are `target`, `capture` and `passive`: + + +#### target + +Handlers can also be registered for an event other than the host itself. +The `target` option can be used to change where the event listener is attached, this is useful for listening to application-wide events. + +In the example below, we're going to listen for the scroll event, emitted from `window`: + +```tsx + @Listen('scroll', { target: 'window' }) + handleScroll(ev) { + console.log('the body was scrolled', ev); + } +``` + +#### passive + +By default, Stencil uses several heuristics to determine if it must attach a `passive` event listener or not. The `passive` option can be used to change the default behavior. + +Please check out [https://developers.google.com/web/updates/2016/06/passive-event-listeners](https://developers.google.com/web/updates/2016/06/passive-event-listeners) for further information. + + +#### capture + +Event listener attached with `@Listen` does not "capture" by default. +When a event listener is set to "capture", it means the event will be dispatched during the "capture phase". +Check out [https://www.quirksmode.org/js/events_order.html](https://www.quirksmode.org/js/events_order.html) for further information. + + +```tsx + @Listen('click', { capture: true }) + handleClick(ev) { + console.log('click'); + } +``` + +## Keyboard events + +For keyboard events, you can use the standard `keydown` event in `@Listen()` and use `event.keyCode` or `event.which` to get the key code, or `event.key` for the string representation of the key. + +```tsx +@Listen('keydown') +handleKeyDown(ev: KeyboardEvent){ + if (ev.key === 'ArrowDown'){ + console.log('down arrow pressed') + } +} +``` +More info on event key strings can be found in the [w3c spec](https://www.w3.org/TR/uievents-key/#named-key-attribute-values). + + +## Using events in JSX + +Within a stencil compiled application or component you can also bind listeners to events directly in JSX. This works very similar to normal DOM events such as `onClick`. + +Let's use our TodoList component from above: + +```tsx +import { Event, EventEmitter } from '@stencil/core'; + +... +export class TodoList { + + @Event() todoCompleted: EventEmitter; + + todoCompletedHandler(todo: Todo) { + this.todoCompleted.emit(todo); + } +} +``` + +We can now listen to this event directly on the component in our JSX using the following syntax: + +```tsx + this.someMethod(ev)} /> +``` + +This property is generated automatically and is prefixed with "on". For example, if the event emitted is called `todoDeleted` the property will be called `onTodoDeleted`: + +```tsx + this.someOtherMethod(ev)} /> +``` + +## Listening to events from a non-JSX element + +```tsx + + +``` diff --git a/versioned_docs/version-v4.42/components/form-associated.md b/versioned_docs/version-v4.42/components/form-associated.md new file mode 100644 index 000000000..a60fa6806 --- /dev/null +++ b/versioned_docs/version-v4.42/components/form-associated.md @@ -0,0 +1,289 @@ +--- +title: Form-Associated Components +sidebar_label: Form-Associated Components +description: Form-Associated Stencil Components +slug: /form-associated +--- + +# Building Form-Associated Components in Stencil + +As of v4.5.0, Stencil has support for form-associated custom elements. This +allows Stencil components to participate in a rich way in HTML forms, +integrating with native browser features for validation and accessibility while +maintaining encapsulation and control over their styling and presentation. + +## Creating a Form-Associated Component + +A form-associated Stencil component is one which sets the new [`formAssociated`](./component.md#formassociated) +option in the argument to the `@Component` +decorator to `true`, like so: + +```tsx +import { Component } from '@stencil/core'; + +@Component({ + tag: 'my-face', + formAssociated: true, +}) +export class MyFACE { +} +``` + +This element will now be marked as a form-associated custom element via the +[`formAssociated`](https://html.spec.whatwg.org/#custom-elements-face-example) +static property, but by itself this is not terribly useful. + +In order to meaningfully interact with a `
` element that is an ancestor +of our custom element we'll need to get access to an +[`ElementInternals`](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals) +object corresponding to our element instance. Stencil provides a decorator, +`@AttachInternals`, which does just this, allowing you to decorate a property on +your component and bind an `ElementInternals` object to that property which you +can then use to interact with the surrounding form. + +:::info +Under the hood the `AttachInternals` decorator makes use of the very similarly +named +[`attachInternals`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/attachInternals) +method on `HTMLElement` to associate your Stencil component with an ancestor +`` element. During compilation, Stencil will generate code that calls +this method at an appropriate point in the component lifecycle for both +[lazy](../output-targets/dist.md) and [custom elements](../output-targets/custom-elements.md) builds. +::: + +A Stencil component using this API to implement a custom text input could look +like this: + +```tsx title="src/components/custom-text-input.tsx" +import { Component, h, AttachInternals, State } from '@stencil/core'; + +@Component({ + tag: 'custom-text-input', + shadow: true, + formAssociated: true +}) +export class CustomTextInput { + @State() value: string; + + @AttachInternals() internals: ElementInternals; + + handleChange(event) { + this.value = event.target.value; + this.internals.setFormValue(event.target.value); + } + + componentWillLoad() { + this.internals.setFormValue("a default value"); + } + + render() { + return ( + this.handleChange(event)} + /> + ) + } +} +``` + +If this component is rendered within a `` element like so: + + +```html + + +
+``` + +then it will automatically be linked up to the surrounding form. The +`ElementInternals` object found at `this.internals` will have a bunch of +methods on it for interacting with that form and getting key information out of +it. + +In our `` example above we use the +[`setFormValue`](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/setFormValue) +method to set a value in the surrounding form. This will read the `name` +attribute off of the element and use it when setting the value, so the value +typed by a user into the `input` will added to the form under the +`"my-custom-input"` name. + +This example just scratches the surface, and a great deal more is possible with +the `ElementInternals` API, including [setting the element's +validity](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/setValidity), +reading the validity state of the form, reading other form values, and more. + +## Lifecycle Callbacks + +Stencil allows developers building form-associated custom elements to define a +[standard series of lifecycle +callbacks](https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reactions) +which enable their components to react dynamically to events in their +lifecycle. These could allow fetching data when a form loads, changing styles +when a form's `disabled` state is toggled, resetting form data cleanly, and more. + +### `formAssociatedCallback` + +This callback is called when the browser both associates the element with and +disassociates the element from a form element. The function is called with the +form element as an argument. This could be used to set an `ariaLabel` when the +form is ready to use, like so: + +```tsx title='src/components/form-associated-cb.tsx' +import { Component, h, AttachInternals } from '@stencil/core'; + +@Component({ + tag: 'form-associated', + formAssociated: true, +}) +export class FormAssociatedCmp { + @AttachInternals() + internals: ElementInternals; + + formAssociatedCallback(form) { + form.ariaLabel = 'formAssociated called'; + } + + render() { + return ; + } +} +``` + +### `formDisabledCallback` + +This is called whenever the `disabled` state on the element _changes_. This +could be used to keep a CSS class in sync with the disabled state, like so: + +```tsx title='src/components/form-disabled-cb.tsx' +import { Component, h, State } from '@stencil/core'; + +@Component({ + tag: 'form-disabled-cb', + formAssociated: true, +}) +export class MyComponent { + @State() cssClass: string = ""; + + formDisabledCallback(disabled: boolean) { + if (disabled) { + this.cssClass = "background-mode"; + } else { + this.cssClass = ""; + } + } + + render() { + return + } +} +``` + +### `formResetCallback` + +This is called when the form is reset, and should be used to reset the +form-associated component's internal state and validation. For example, you +could do something like the following: + +```tsx title="src/components/form-reset-cb.tsx" +import { Component, h, AttachInternals } from '@stencil/core'; + +@Component({ + tag: 'form-reset-cb', + formAssociated: true, +}) +export class MyComponent { + @AttachInternals() + internals: ElementInternals; + + formResetCallback() { + this.internals.setValidity({}); + this.internals.setFormValue(""); + } + + render() { + return + } +} +``` + +### `formStateRestoreCallback` + +This method will be called in the event that the browser automatically fills +out your form element, an event that could take place in two different +scenarios. The first is that the browser can restore the state of an element +after navigating or restarting, and the second is that an input was made using a +form auto-filling feature. + +In either case, in order to correctly reset itself your form-associated component +will need the previously selected value, but other state may also be necessary. +For instance, the form value to be submitted for a date picker component would +be a specific date, but in order to correctly restore the component's visual +state it might also be necessary to know whether the picker should display a +week or month view. + +The +[`setFormValue`](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/setFormValue) +method on `ElementInternals` provides some support for this use-case, allowing +you to submit both a _value_ and a _state_, where the _state_ is not added to +the form data sent to the server but could be used for storing some +client-specific state. For instance, a pseudocode sketch of a date picker +component that correctly restores whether the 'week' or 'month' view is active +could look like: + +```tsx title="src/components/fa-date-picker.tsx" +import { Component, h, State, AttachInternals } from '@stencil/core'; + +@Component({ + tag: 'fa-date-picker', + formAssociated: true, +}) +export class MyDatePicker { + @State() value: string = ""; + @State() view: "weeks" | "months" = "weeks"; + + @AttachInternals() + internals: ElementInternals; + + onInputChange(e) { + e.preventDefault(); + const date = e.target.value; + this.setValue(date); + } + + setValue(date: string) { + // second 'state' parameter is used to store both + // the input value (`date`) _and_ the current view + this.internals.setFormValue(date, `${date}#${this.view}`); + } + + formStateRestoreCallback(state, _mode) { + const [date, view] = state.split("#"); + this.view = view; + this.setValue(date); + } + + render() { + return
+ Mock Date Picker, mode: {this.view} + this.onInputChange(e)}> +
+ } +} +``` + +Note that the `formStateRestoreCallback` also receives a second argument, +`mode`, which can be either `"restore"` or `"autocomplete"`, indicating the +reason for the form restoration. + +For more on form restoration, including a complete example, check out [this +great blog post on the +subject](https://web.dev/articles/more-capable-form-controls#restoring-form-state). + +## Resources + +- [WHATWG specification for form-associated custom elements](https://html.spec.whatwg.org/dev/custom-elements.html#form-associated-custom-elements) +- [ElementInternals and Form-Associated Custom Elements](https://webkit.org/blog/13711/elementinternals-and-form-associated-custom-elements/) from the WebKit blog +- [Web.dev post detailing how form-associated lifecycle callbacks work](https://web.dev/articles/more-capable-form-controls#lifecycle_callbacks) + diff --git a/versioned_docs/version-v4.42/components/functional-components.md b/versioned_docs/version-v4.42/components/functional-components.md new file mode 100644 index 000000000..cd3b27af2 --- /dev/null +++ b/versioned_docs/version-v4.42/components/functional-components.md @@ -0,0 +1,108 @@ +--- +title: Functional Components +sidebar_label: Functional Components +description: Functional Components +slug: /functional-components +--- + +# Working with Functional Components + +Functional components are quite different to normal Stencil web components because they are a part of Stencil's JSX compiler. A functional component is basically a function that takes an object of props and turns it into JSX. + +```tsx +const Hello = props =>

Hello, {props.name}!

; +``` + +When the JSX transpiler encounters such a component, it will take its attributes, pass them into the function as the `props` object, and replace the component with the JSX that is returned by the function. + +```tsx + +``` + +Functional components also accept a second argument `children`. + +```tsx +const Hello = (props, children) => [ +

Hello, {props.name}

, + children +]; +``` + +The JSX transpiler passes all child elements of the component as an array into the function's `children` argument. + +```tsx + +

I'm a child element.

+
+``` + +Stencil provides a `FunctionalComponent` generic type that allows to specify an interface for the component's properties. + +```tsx +// Hello.tsx + +import { FunctionalComponent, h } from '@stencil/core'; + +interface HelloProps { + name: string; +} + +export const Hello: FunctionalComponent = ({ name }) => ( +

Hello, {name}!

+); +``` + +## Working with children + +The second argument of a functional component receives the passed children, but in order to work with them, `FunctionalComponent` provides a utils object that exposes a `map()` method to transform the children, and a `forEach()` method to read them. Reading the `children` array is not recommended since the stencil compiler can rename the vNode properties in prod mode. + +```tsx +export interface FunctionalUtilities { + forEach: (children: VNode[], cb: (vnode: ChildNode, index: number, array: ChildNode[]) => void) => void; + map: (children: VNode[], cb: (vnode: ChildNode, index: number, array: ChildNode[]) => ChildNode) => VNode[]; +} +export interface ChildNode { + vtag?: string | number | Function; + vkey?: string | number; + vtext?: string; + vchildren?: VNode[]; + vattrs?: any; + vname?: string; +} +``` + +**Example:** + +```tsx +export const AddClass: FunctionalComponent = (_, children, utils) => ( + utils.map(children, child => ({ + ...child, + vattrs: { + ...child.vattrs, + class: `${child.vattrs.class} add-class` + } + } + )) +); +``` + +:::note +When using a functional component in JSX, its name must start with a capital letter. Therefore it makes sense to export it as such. +::: + + +## Disclaimer + +There are a few major differences between functional components and class components. Since functional components are just syntactic sugar within JSX, they... + +* aren't compiled into web components, +* don't create a DOM node, +* don't have a Shadow DOM or scoped styles, +* don't have lifecycle hooks, +* are stateless. + +When deciding whether to use functional components, one concept to keep in mind is that often the UI of your application can be a function of its state, i. e., given the same state, it always renders the same UI. If a component has to hold state, deal with events, etc, it should probably be a class component. If a component's purpose is to simply encapsulate some markup so it can be reused across your app, it can probably be a functional component (especially if you're using a component library and thus don't need to style it). + +:::caution +Stencil does not support re-exporting a functional component from a "barrel file" and dynamically rendering it in another component. This is a [known limitation](https://github.com/ionic-team/stencil/issues/5246) within Stencil. Instead, either use class components and remove the import or import the functional component directly. +::: \ No newline at end of file diff --git a/versioned_docs/version-v4.42/components/host-element.md b/versioned_docs/version-v4.42/components/host-element.md new file mode 100644 index 000000000..c391fbe35 --- /dev/null +++ b/versioned_docs/version-v4.42/components/host-element.md @@ -0,0 +1,159 @@ +--- +title: Working with host elements +sidebar_label: Host Element +description: Working with host elements +slug: /host-element +--- + +# Working with host elements + +Stencil components render their children declaratively in their `render` method [using JSX](./templating-and-jsx.md). Most of the time, the `render()` function describes the children elements that are about to be rendered, but it can also be used to render attributes of the host element itself. + + +## `` + +The `Host` functional component can be used at the root of the render function to set attributes and event listeners to the host element itself. This works just like any other JSX: + +```tsx +// Host is imported from '@stencil/core' +import { Component, Host, h } from '@stencil/core'; + +@Component({tag: 'todo-list'}) +export class TodoList { + @Prop() open = false; + render() { + return ( + + ) + } +} +``` + +If `this.open === true`, it will render: +```tsx + +``` + +similarly, if `this.open === false`: + +```tsx + +``` + +`` is a virtual component, a virtual API exposed by stencil to declaratively set the attributes of the host element, it will never be rendered in the DOM, i.e. you will never see `` in Chrome Dev Tools for instance. + + +### `` can work as a `` + +`` can also be used when more than one component needs to be rendered at the root level for example: + +It could be achieved by a `render()` method like this: + +```tsx +@Component({tag: 'my-cmp'}) +export class MyCmp { + render() { + return ( + +

Title

+

Message

+
+ ); + } +} +``` + +This JSX would render the following HTML: + +```markup + +

Title

+

Message

+
+``` + +Even if we don't use `` to render any attribute in the host element, it's a useful API to render many elements at the root level. + +## Element Decorator + +The `@Element()` decorator is how to get access to the host element within the class instance. This returns an instance of an `HTMLElement`, so standard DOM methods/events can be used here. + +```tsx +import { Element } from '@stencil/core'; + +... +export class TodoList { + + @Element() el: HTMLElement; + + getListHeight(): number { + return this.el.getBoundingClientRect().height; + } +} +``` + +In order to reference the host element when initializing a class member you'll need to use TypeScript's definite assignment assertion modifier to avoid a +type error: + +```tsx +import { Element } from '@stencil/core'; + +... +export class TodoList { + + @Element() el!: HTMLElement; + + private listHeight = this.el.getBoundingClientRect().height; +} +``` + +If you need to update the host element in response to prop or state changes, you should do so in the `render()` method using the `` element. + +## Styling + +See full information about styling on the [Styling page](./styling.md#shadow-dom-in-stencil). + +CSS can be applied to the `` element by using its component tag defined in the `@Component` decorator. + +```tsx +@Component({ + tag: 'my-cmp', + styleUrl: 'my-cmp.css' +}) +... +``` + +my-cmp.css: + +```css +my-cmp { + width: 100px; +} +``` + +### Shadow DOM + +Something to beware of is that Styling the `` element when using shadow DOM does not work quite the same. Instead of using the `my-cmp` element selector you must use `:host`. + +```tsx +@Component({ + tag: 'my-cmp', + styleUrl: 'my-cmp.css', + shadow: true +}) +... +``` + +my-cmp.css: + +```css +:host { + width: 100px; +} +``` diff --git a/versioned_docs/version-v4.42/components/methods.md b/versioned_docs/version-v4.42/components/methods.md new file mode 100644 index 000000000..931839b91 --- /dev/null +++ b/versioned_docs/version-v4.42/components/methods.md @@ -0,0 +1,98 @@ +--- +title: Methods +sidebar_label: Methods +description: methods +slug: /methods +--- + +# Method Decorator + +The `@Method()` decorator is used to expose methods on the public API. Functions decorated with the `@Method()` decorator can be called directly from the element, i.e. they are intended to be callable from the outside! + +:::note +Developers should try to rely on publicly exposed methods as little as possible, and instead default to using properties and events as much as possible. As an app scales, we've found it's easier to manage and pass data through @Prop rather than public methods. +::: + +```tsx +import { Method } from '@stencil/core'; + +export class TodoList { + + @Method() + async showPrompt() { + // show a prompt + } +} +``` + +Call the method like this: + +:::note +Developers should ensure that the component is defined by using the whenDefined method of the custom element registry before attempting to call public methods. +::: + +```tsx +(async () => { + await customElements.whenDefined('todo-list'); + const todoListElement = document.querySelector('todo-list'); + await todoListElement.showPrompt(); +})(); +``` + +## Public methods must be async + +Stencil's architecture is async at all levels which allows for many performance benefits and ease of use. By ensuring publicly exposed methods using the `@Method` decorator return a promise: + +- Developers can call methods before the implementation was downloaded without componentOnReady(), which queues the method calls and resolves after the component has finished loading. + +- Interaction with the component is the same whether it still needs to be lazy-loaded, or is already fully hydrated. + +- By keeping a component's public API async, apps could move the components transparently to [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) and the API would still be the same. + +- Returning a promise is only required for publicly exposed methods which have the `@Method` decorator. All other component methods are private to the component and are not required to be async. + + +```tsx +// VALID: using async +@Method() +async myMethod() { + return 42; +} + +// VALID: using Promise.resolve() +@Method() +myMethod2() { + return Promise.resolve(42); +} + +// VALID: even if it returns nothing, it needs to be async +@Method() +async myMethod3() { + console.log(42); +} + +// INVALID +@Method() +notOk() { + return 42; +} +``` + +## Private methods + +Non-public methods can still be used to organize the business logic of your component and they do NOT have to return a Promise. + +```tsx +class Component { + // Since `getData` is not a public method exposed with @Method + // it does not need to be async + getData() { + return this.someData; + } + render() { + return ( +
{this.getData()}
+ ); + } +} +``` diff --git a/versioned_docs/version-v4.42/components/properties.md b/versioned_docs/version-v4.42/components/properties.md new file mode 100644 index 000000000..72ffab61a --- /dev/null +++ b/versioned_docs/version-v4.42/components/properties.md @@ -0,0 +1,1020 @@ +--- +title: Properties +sidebar_label: Properties +description: Properties +slug: /properties +--- + +# Properties + +Props are custom attributes/properties exposed publicly on an HTML element. They allow developers to pass data to a +component to render or otherwise use. + +## The Prop Decorator (`@Prop()`) + +Props are declared on a component using Stencil's `@Prop()` decorator, like so: + +```tsx +// First, we import Prop from '@stencil/core' +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list', +}) +export class TodoList { + // Second, we decorate a class member with @Prop() + @Prop() name: string; + + render() { + // Within the component's class, its props are + // accessed via `this`. This allows us to render + // the value passed to `todo-list` + return
To-Do List Name: {this.name}
+ } +} +``` + +In the example above, `@Prop()` is placed before (decorates) the `name` class member, which is a string. By adding +`@Prop()` to `name`, Stencil will expose `name` as an attribute on the element, which can be set wherever the component +is used: + +```tsx +{/* Here we use the component in a TSX file */} + +``` +```html + + +``` + +In the example above the `todo-list` component is used almost identically in TSX and HTML. The only difference between +the two is that in TSX, the value assigned to a prop (in this case, `name`) is wrapped in curly braces. In some cases +however, the way props are passed to a component differs slightly between HTML and TSX. + +## Variable Casing + +In the JavaScript ecosystem, it's common to use 'camelCase' when naming variables. The example component below has a +class member, `thingToDo` that is camelCased. + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + // thingToDo is 'camelCased' + @Prop() thingToDo: string; + + render() { + return
{this.thingToDo}
; + } +} +``` + +Since `thingToDo` is a prop, we can provide a value for it when we use our `todo-list-item` component. Providing a +value to a camelCased prop like `thingToDo` is nearly identical in TSX and HTML. + +When we use our component in a TSX file, an attribute uses camelCase: + +```tsx + +``` + +In HTML, the attribute must use 'dash-case' like so: + +```html + +``` + +## Data Flow + +Props should be used to pass data down from a parent component to its child component(s). + +The example below shows how a `todo-list` component uses three `todo-list-item` child components to render a ToDo list. + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list', +}) +export class TodoList { + render() { + return ( +
+

To-Do List Name: Stencil To Do List

+
    + {/* Below are three Stencil components that are children of `todo-list`, each representing an item on our list */} + + + +
+
+ ) + } +} +``` +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop() thingToDo: string; + + render() { + return
  • {this.thingToDo}
  • ; + } +} +``` + +:::note +Children components should not know about or reference their parent components. This allows Stencil to +efficiently re-render your components. Passing a reference to a component as a prop may cause unintended side effects. +::: + +## Mutability + +A Prop is by default immutable from inside the component logic. Once a value is set by a user, the component cannot +update it internally. For more advanced control over the mutability of a prop, please see the +[mutable option](#prop-mutability-mutable) section of this document. + +## Types + +Props can be a `boolean`, `number`, `string`, or even an `Object` or `Array`. The example below expands the +`todo-list-item` to add a few more props with different types. + +```tsx +import { Component, Prop, h } from '@stencil/core'; +// `MyHttpService` is an `Object` in this example +import { MyHttpService } from '../some/local/directory/MyHttpService'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop() isComplete: boolean; + @Prop() timesCompletedInPast: number; + @Prop() thingToDo: string; + @Prop() myHttpService: MyHttpService; +} +``` + +### Boolean Props + +A property on a Stencil component that has a type of `boolean` may be declared as: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop() isComplete: boolean; +} +``` + +To use this version of `todo-list-item` in HTML, we pass the string `"true"`/`"false"` to the component: +```html + + + + +``` + +To use this version of `todo-list-item` in TSX, `true`/`false` is used, surrounded by curly braces: +```tsx +// Set isComplete to 'true' + +// Set isComplete to 'false' + +``` + +There are a few ways in which Stencil treats props that are of type `boolean` that are worth noting: + +1. The value of a boolean prop will be `false` if provided the string `"false"` in HTML + +```html + + +``` +2. The value of a boolean prop will be `true` if provided a string that is not `"false"` in HTML + +```html + + + + + +``` +3. The value of a boolean prop will be `undefined` if it has no [default value](#default-values) and one of +the following applies: + 1. the prop is not included when using the component + 2. the prop is included when using the component, but is not given a value + +```html + + + + +``` + +### Number Props + +A property on a Stencil component that has a type of `number` may be declared as: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop() timesCompletedInPast: number; +} +``` + +To use this version of `todo-list-item` in HTML, we pass the numeric value as a string to the component: +```html + + + + +``` + +To use this version of `todo-list-item` in TSX, a number surrounded by curly braces is passed to the component: +```tsx +// Set timesCompletedInPast to '0' + +// Set timesCompletedInPast to '23' + +``` + +### String Props + +A property on a Stencil component that has a type of `string` may be declared as: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop() thingToDo: string; +} +``` + +To use this version of `todo-list-item` in HTML, we pass the value as a string to the component: +```html + + + + +``` + +To use this version of `todo-list-item` in TSX, we pass the value as a string to the component. Curly braces aren't +required when providing string values to props in TSX, but are permitted: +```tsx +// Set thingToDo to 'Learn about Stencil Props' + +// Set thingToDo to 'Write some Stencil Code with Props' + +// Set thingToDo to 'Write some Stencil Code with Props' with curly braces + +``` + +### Object Props + +A property on a Stencil component that has a type of `Object` may be declared as: + +```tsx +// TodoListItem.tsx +import { Component, Prop, h } from '@stencil/core'; +import { MyHttpService } from '../path/to/MyHttpService'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + // Use `@Prop()` to declare the `httpService` class member + @Prop() httpService: MyHttpService; +} +``` +```tsx +// MyHttpService.ts +export class MyHttpService { + // This implementation intentionally left blank +} +``` + +In TypeScript, `MyHttpService` is both an `Object` and a 'type'. When using user-defined types like `MyHttpService`, the +type must always be exported using the `export` keyword where it is declared. The reason for this is Stencil needs to +know what type the prop `httpService` is when passing an instance of `MyHttpService` to `TodoListItem` from a parent +component. + +To set `httpService` in TSX, assign the property name in the custom element's tag to the desired value like so: +```tsx +// TodoList.tsx +import { Component, h } from '@stencil/core'; +import { MyHttpService } from '../MyHttpService'; + +@Component({ + tag: 'todo-list', + styleUrl: 'todo-list.css', + shadow: true, +}) +export class ToDoList { + private httpService = new MyHttpService(); + + render() { + return ; + } +} +``` +Note that the prop name is using `camelCase`, and the value is surrounded by curly braces. + +It is not possible to set `Object` props via an HTML attribute like so: +```html + + +``` +The reason for this is that Stencil will not attempt to serialize object-like strings written in HTML into a JavaScript object. +Similarly, Stencil does not have any support for deserializing objects from JSON. +Doing either can be expensive at runtime, and runs the risk of losing references to other nested JavaScript objects. + +Instead, properties may be set via ` +``` + + +### Array Props + +A property on a Stencil component that is an Array may be declared as: + +```tsx +// TodoList.tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop() itemLabels: string[]; +} +``` + +To set `itemLabels` in TSX, assign the prop name in the custom element's tag to the desired value like so: +```tsx +// TodoList.tsx +import { Component, h } from '@stencil/core'; +import { MyHttpService } from '../MyHttpService'; + +@Component({ + tag: 'todo-list', + styleUrl: 'todo-list.css', + shadow: true, +}) +export class ToDoList { + private labels = ['non-urgent', 'weekend-only']; + + render() { + return ; + } +} +``` +Note that the prop name is using `camelCase`, and the value is surrounded by curly braces. + +It is not possible to set `Array` props via an HTML attribute like so: +```html + + +``` +The reason for this is that Stencil will not attempt to serialize array-like strings written in HTML into a JavaScript object. +Doing so can be expensive at runtime, and runs the risk of losing references to other nested JavaScript objects. + +Instead, properties may be set via ` +``` + +### Advanced Prop Types + +#### `any` Type + +TypeScript's [`any` type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any) is a special type +that may be used to prevent type checking of a specific value. Because `any` is a valid type in TypeScript, Stencil +props can also be given a type of `any`. The example below demonstrates three different ways of using props with type +`any`: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + // isComplete has an explicit type annotation + // of `any`, and no default value + @Prop() isComplete: any; + // label has an explicit type annotation of + // `any` with a default value of 'urgent', + // which is a string + @Prop() label: any = 'urgent'; + // thingToDo has no type and no default value, + // and will be considered to be type `any` by + // TypeScript + @Prop() thingToDo; + + render() { + return ( +
      +
    • isComplete has a value of - {this.isComplete} - and a typeof value of "{typeof this.isComplete}"
    • +
    • label has a value of - {this.label} - and a typeof value of "{typeof this.label}"
    • +
    • thingToDo has a value of - {this.thingToDo} - and a typeof value of "{typeof this.thingToDo}"
    • +
    + ); + } +} +``` + +When using a Stencil prop typed as `any` (implicitly or explicitly), the value that is provided to a prop retains its +own type information. Neither Stencil nor TypeScript will try to change the type of the prop. To demonstrate, let's use +`todo-list-item` twice, each with different prop values: + +```tsx +{/* Using todo-list-item in TSX using differnt values each time */} + + +``` + +The following will be rendered from the usage example above: +```md +- isComplete has a value of - 42 - and a typeof value of "number" +- label has a value of - - and a typeof value of "object" +- thingToDo has a value of - Learn about any-typed props - and a typeof value of "string" + +- isComplete has a value of - 42 - and a typeof value of "string" +- label has a value of - 1 - and a typeof value of "number" +- thingToDo has a value of - Learn about any-typed props - and a typeof value of "string" +``` + +In the first usage of `todo-list-item`, `isComplete` is provided a number value of 42, whereas in the second usage it +receives a string containing "42". The types on `isComplete` reflect the type of the value it was provided, 'number' and +'string', respectively. + +Looking at `label`, it is worth noting that although the prop has a [default value](#default-values), it does +not narrow the type of `label` to be of type 'string'. In the first usage of `todo-list-item`, `label` is provided a +value of null, whereas in the second usage it receives a number value of 1. The types of the values stored in `label` +are correctly reported as 'object' and 'number', respectively. + +#### Optional Types + +TypeScript allows members to be marked optional by appending a `?` at the end of the member's name. The example below +demonstrates making each a component's props optional: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + // completeMsg is optional, has an explicit type + // annotation of `string`, and no default value + @Prop() completeMsg?: string; + // label is optional, has no explicit type + // annotation, but does have a default value + // of 'urgent' + @Prop() label? = 'urgent'; + // thingToDo has no type annotation and no + // default value + @Prop() thingToDo?; + + render() { + return ( +
      +
    • completeMsg has a value of - {this.completeMsg} - and a typeof value of "{typeof this.completeMsg}"
    • +
    • label has a value of - {this.label} - and a typeof value of "{typeof this.label}"
    • +
    • thingToDo has a value of - {this.thingToDo} - and a typeof value of "{typeof this.thingToDo}"
    • +
    + ); + } +} +``` + +When using a Stencil prop that is marked as optional, Stencil will try to infer the type of the prop if a type is +not explicitly given. In the example above, Stencil is able to understand that: + +- `completeMsg` is of type string, because it has an explicit type annotation +- `label` is of type string, because it has a [default value](#default-values) that is of type string +- `thingToDo` [is of type `any`](#any-type), because it has no explicit type annotation, nor default value + +Because Stencil can infer the type of `label`, the following will fail to compile due to a type mismatch: + +```tsx +{/* This fails to compile with the error "Type 'number' is not assignable to type 'string'" for the label prop. */} + +``` + +It is worth noting that when using a component in an HTML file, such type checking is unavailable. This is a constraint +on HTML, where all values provided to attributes are of type string: + +```html + + +``` +renders: +```md +- completeMsg has a value of - 42 - and a typeof value of "string" +- label has a value of - null - and a typeof value of "string" +- thingToDo has a value of - Learn about any-typed props - and a typeof value of "string" +``` + +#### Union Types + +Stencil allows props types be [union types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types), +which allows you as the developer to combine two or more pre-existing types to create a new one. The example below shows +a `todo-list-item` who accepts a `isComplete` prop that can be either a string or boolean. + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop() isComplete: string | boolean; +} +``` + +This component can be used in both HTML: +```html + + + + +``` +and TSX: +```tsx + + +``` + +When using union types, the type of a component's `@Prop()` value can be ambiguous at runtime. +In the provided example, under what circumstances does `@Prop() isComplete` function as a `string`, and when does it serve as a `boolean`? + +When using a component in HTML, the runtime value of a `@Prop()` is a string whenever an attribute is set. +This is a result of setting the HTML attribute for the custom element. +```html + + + + + + + + +``` +However, if an attribute is not specified, the runtime value of the property will be `undefined`: +```html + + +``` + +When the attribute on a component is set using `setAttribute`, the runtime value of a `@Prop()` is always [coerced to a string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_coercion). +```html + +``` + +However, if the property of a custom element is directly changed, its type will match the value that was provided. +```html + +``` + +When using a component in TSX, a `@Prop()`'s type will match the value that was provided. +```tsx +// Since this is TSX, the value of `isComplete` in `ToDoListItem` +// depends on the type of the value passed to the component. +// +// Set the property `isComplete` to `true` (boolean) + +// Set the property `isComplete` to "true" (string) + +// Set the property `isComplete` to `false` (boolean) + +// Set the property `isComplete` to "false" (string) + +``` + +## Default Values + +Stencil props can be given a default value as a fallback in the event a prop is not provided: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'component-with-some-props', +}) +export class ComponentWithSomeProps { + @Prop() aNumber = 42; + @Prop() aString = 'defaultValue'; + + render() { + return
    The number is {this.aNumber} and the string is {this.aString}
    + } +} +``` +Regardless of if we use this component in HTML or TSX, "The number is 42 and the string is defaultValue" is displayed +when no values are passed to our component: +```html + +``` + +The default values on a component can be overridden by specifying a value for a prop with a default value. For the +example below, "The number is 7 and the string is defaultValue" is rendered. Note how the value provided to `aNumber` +overrides the default value, but the default value of `aString` remains the same: +```html + +``` + +### Inferring Types from Default Values + +When a default value is provided, Stencil is able to infer the type of the prop from the default value: + +```tsx +import { Component, Prop, h } from '@stencil/core'; +@Component({ + tag: 'component-with-many-props', +}) +export class ComponentWithManyProps { + // both props below are of type 'boolean' + @Prop() boolean1: boolean; + @Prop() boolean2 = true; + + // both props below are of type 'number' + @Prop() number1: number; + @Prop() number2 = 42; + + // both props below are of type 'string' + @Prop() string1: string; + @Prop() string2 = 'defaultValue'; +} +``` + +## Required Properties + +By placing a `!` after a prop name, Stencil mark that the attribute/property as required. This ensures that when the +component is used in TSX, the property is used: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + // Note the '!' after the variable name. + @Prop() thingToDo!: string; +} +``` + +## Prop Validation + +To do validation of a Prop, you can use the [@Watch()](./reactive-data.md#the-watch-decorator-watch) decorator: + +```tsx +import { Component, Prop, Watch, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class TodoList { + // Mark the prop as required, to make sure it is provided when we use `todo-list-item`. + // We want stricter guarantees around the contents of the string, so we'll use `@Watch` to perform additional validation. + @Prop() thingToDo!: string; + + @Watch('thingToDo') + validateName(newValue: string, _oldValue: string) { + // don't allow `thingToDo` to be the empty string + const isBlank = typeof newValue !== 'string' || newValue === ''; + if (isBlank) { + throw new Error('thingToDo is a required property and cannot be empty') + }; + // don't allow `thingToDo` to be a string with a length of 1 + const has2chars = typeof newValue === 'string' && newValue.length >= 2; + if (!has2chars) { + throw new Error('thingToDo must have a length of more than 1 character') + }; + } +} +``` + +Alternatively, you could do validation within a setter. [Read about using `get()` / `set()` methods with decorated props](#prop-getters-and-setters) + +## @Prop() Options + +The `@Prop()` decorator accepts an optional argument to specify certain options to modify how a prop on a component +behaves. `@Prop()`'s optional argument is an object literal containing one or more of the following fields: + +```tsx +export interface PropOptions { + attribute?: string; + mutable?: boolean; + reflect?: boolean; +} +``` + +### Attribute Name (`attribute`) + +Properties and component attributes are strongly connected but not necessarily the same thing. While attributes are an +HTML concept, properties are a JavaScript concept inherent to Object-Oriented Programming. + +In Stencil, the `@Prop()` decorator applied to a **property** will instruct the Stencil compiler to also listen for +changes in a DOM attribute. + +Usually, the name of a property is the same as the attribute, but this is not always the case. Take the following +component as example: + +```tsx +import { Component, Prop, h } from '@stencil/core'; +// `MyHttpService` is an `Object` in this example +import { MyHttpService } from '../some/local/directory/MyHttpService'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop() isComplete: boolean; + @Prop() thingToDo: string; + @Prop() httpService: MyHttpService; +} +``` + +This component has **3 properties**, but the compiler will create **only 2 attributes**: `is-complete` and +`thing-to-do`. + +```html + +``` + +Notice that the `httpService` type is not a primitive (e.g. not a `number`, `boolean`, or `string`). Since DOM +attributes can only be strings, it does not make sense to have an associated DOM attribute called `"http-service"`. +Stencil will not attempt to serialize object-like strings written in HTML into a JavaScript object. +See [Object Props](#object-props) for guidance as to how to configure `httpService`. + +At the same time, the `isComplete` & `thingToDo` properties follow 'camelCase' naming, but attributes are +case-insensitive, so the attribute names will be `is-complete` & `thing-to-do` by default. + +Fortunately, this "default" behavior can be changed using the `attribute` option of the `@Prop()` decorator: + +```tsx +import { Component, Prop, h } from '@stencil/core'; +// `MyHttpService` is an `Object` in this example +import { MyHttpService } from '../some/local/directory/MyHttpService'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop({ attribute: 'complete' }) isComplete: boolean; + @Prop({ attribute: 'thing' }) thingToDo: string; + @Prop({ attribute: 'my-service' }) httpService: MyHttpService; +} +``` + +By using this option, we are being explicit about which properties have an associated DOM attribute and the name of it +when using the component in HTML. + +```html + +``` + +### Prop Mutability (`mutable`) + +A Prop is by default immutable from inside the component logic. +However, it's possible to explicitly allow a Prop to be mutated from inside the component, by declaring it as mutable, as in the example below: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop({ mutable: true }) thingToDo: string; + + componentDidLoad() { + this.thingToDo = 'Ah! A new value!'; + } +} +``` + +#### Mutable Arrays and Objects + +Stencil compares Props by reference in order to efficiently rerender components. +Setting `mutable: true` on a Prop that is an object or array allows the _reference_ to the Prop to change inside the component and trigger a render. +It does not allow a mutable change to an existing object or array to trigger a render. + +For example, to update an array Prop: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'my-component', +}) +export class MyComponent { + @Prop({mutable: true}) contents: string[] = []; + timer: NodeJS.Timer; + + connectedCallback() { + this.timer = setTimeout(() => { + // this does not create a new array. when stencil + // attempts to see if any of its Props have changed, + // it sees the reference to its `contents` Prop is + // the same, and will not trigger a render + + // this.contents.push('Stencil') + + // this does create a new array, and therefore a + // new reference to the Prop. Stencil will pick up + // this change and rerender + this.contents = [...this.contents, 'Stencil']; + // after 3 seconds, the component will re-render due + // to the reference change in `this.contents` + }, 3000); + } + + disconnectedCallback() { + if (this.timer) { + clearTimeout(this.timer); + } + } + + render() { + return
    Hello, World! I'm {this.contents[0]}
    ; + } +} +``` + +In the example above, updating the Prop in place using `this.contents.push('Stencil')` would have no effect. +Stencil does not see the change to `this.contents`, since it looks at the _reference_ of the Prop, and sees that it has not changed. +This is done for performance reasons. +If Stencil had to walk every slot of the array to determine if it changed, it would incur a performance hit. +Rather, it is considered better for performance and more idiomatic to re-assign the Prop (in the example above, we use the spread operator). + +The same holds for objects as well. +Rather than mutating an existing object in-place, a new object should be created using the spread operator. This object will be different-by-reference and therefore will trigger a re-render: + + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +export type MyContents = {name: string}; + +@Component({ + tag: 'my-component', +}) +export class MyComponent { + @Prop({mutable: true}) contents: MyContents; + timer: NodeJS.Timer; + + connectedCallback() { + this.timer = setTimeout(() => { + // this does not create a new object. when stencil + // attempts to see if any of its Props have changed, + // it sees the reference to its `contents` Prop is + // the same, and will not trigger a render + + // this.contents.name = 'Stencil'; + + // this does create a new object, and therefore a + // new reference to the Prop. Stencil will pick up + // this change and rerender + this.contents = {...this.contents, name: 'Stencil'}; + // after 3 seconds, the component will re-render due + // to the reference change in `this.contents` + }, 3000); + } + + disconnectedCallback() { + if (this.timer) { + clearTimeout(this.timer); + } + } + + render() { + return
    Hello, World! I'm {this.contents.name}
    ; + } +} +``` + +### Reflect Properties Values to Attributes (`reflect`) + +In some cases it may be useful to keep a Prop in sync with an attribute. In this case you can set the `reflect` option +in the `@Prop()` decorator to `true`. When a prop is reflected, it will be rendered in the DOM as an HTML attribute. + +Take the following component as example: + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'todo-list-item', +}) +export class ToDoListItem { + @Prop({ reflect: false }) isComplete: boolean = false; + @Prop({ reflect: true }) timesCompletedInPast: number = 2; + @Prop({ reflect: true }) thingToDo: string = "Read Reflect Section of Stencil Docs"; +} +``` + +The component in the example above uses [default values](#default-values), and can be used in HTML like so: +```html + + +``` + +When rendered in the DOM, the properties configured with `reflect: true` will be reflected in the DOM: + +```html + +``` + +While the properties not set to "reflect", such as `isComplete`, are not rendered as attributes, it does not mean it's +not there - the `isComplete` property still contains the `false` value as assigned: + +```tsx +const cmp = document.querySelector('todo-list-item'); +console.log(cmp.isComplete); // it prints 'false' +``` + +### Prop Getters and Setters + +`@Prop` decorated members can additionally be used with `get()` and `set()` methods which can be useful for validation +([as an alternative to using a @Watch decorator](#prop-validation)), transforming incoming-data or exposing read-only properties. + +```tsx +import { Component, Prop, h } from '@stencil/core'; + +@Component({ + tag: 'get-set-props', +}) +export class GetSetProps { + // A read-only prop + private internalValue = 'should not change'; + @Prop() + get readOnlyProp () { return this.internalValue; } + + // A validated prop + private safeValue: 'this' | 'or maybe this' = 'this'; + @Prop() + get validatedProp () { + return this.safeValue; + } + set validatedProp (incomingDodgyValue: any) { + if (['this', 'or maybe this'].includes(incomingDodgyValue)) { + this.safeValue = incomingDodgyValue; + } + } + + private dateValue: Date = new Date(); + // A transformed prop + @Prop() + get transformedProp () { + return this.dateValue; + } + set transformedProp (incomingStringVal: string) { + this.dateValue = new Date(Date.parse(incomingStringVal)); + } +} +``` + +Most of the documentation referring to [types](#types) and [options](#prop-options) also apply to get / set `@Prop`s +(with the exception of [mutable](#prop-mutability-mutable) as this makes little logical sense). diff --git a/versioned_docs/version-v4.42/components/reactive-data.md b/versioned_docs/version-v4.42/components/reactive-data.md new file mode 100644 index 000000000..a973e244d --- /dev/null +++ b/versioned_docs/version-v4.42/components/reactive-data.md @@ -0,0 +1,255 @@ +--- +title: Reactive Data, Handling arrays and objects +sidebar_label: Reactive Data +description: Reactive Data, Handling arrays and objects +slug: /reactive-data +--- + +# Reactive Data + +Stencil components update when props or state on a component change. + +## Rendering methods + +When props or state change on a component, the [`render()` method](./templating-and-jsx.md) is scheduled to run. + +## The Watch Decorator (`@Watch()`) + +`@Watch()` is a decorator that is applied to a method of a Stencil component. +The decorator's first required argument is the name of a class member that is decorated with `@Prop()` or `@State()`, or +a host attribute. A method decorated with `@Watch()` will automatically run when its associated class member or attribute changes. + +```tsx +// We import Prop & State to show how `@Watch()` can be used on +// class members decorated with either `@Prop()` or `@State()` +import { Component, Prop, State, Watch } from '@stencil/core'; + +@Component({ + tag: 'loading-indicator' +}) +export class LoadingIndicator { + // We decorate a class member with @Prop() so that we + // can apply @Watch() + @Prop() activated: boolean; + // We decorate a class member with @State() so that we + // can apply @Watch() + @State() busy: boolean; + + // Apply @Watch() for the component's `activated` member. + // Whenever `activated` changes, this method will fire. + @Watch('activated') + watchPropHandler(newValue: boolean, oldValue: boolean) { + console.log('The old value of activated is: ', oldValue); + console.log('The new value of activated is: ', newValue); + } + + // Apply @Watch() for the component's `busy` member. + // Whenever `busy` changes, this method will fire. + @Watch('busy') + watchStateHandler(newValue: boolean, oldValue: boolean) { + console.log('The old value of busy is: ', oldValue); + console.log('The new value of busy is: ', newValue); + } + + @Watch('activated') + @Watch('busy', {immediate: true}) + watchMultiple(newValue: boolean, oldValue: boolean, propName:string) { + console.log(`The new value of ${propName} is: `, newValue); + } +} +``` + +In the example above, there are four `@Watch()` decorators: +- The first decorates `watchPropHandler`, which will fire when the class member `activated` changes. +- The second decorates `watchStateHandler`, which will fire when the class member `busy` changes. +- The third and fourth decorators both decorate `watchMultiple`, which will fire when either `activated` or `busy` change. + +Passing `{immediate: true}` as the second argument to `@Watch()` causes the decorated +method to fire when the component initially loads, in addition to when the watched member changes. + +When fired, the decorated method will receive the old and new values of the prop/state. +This is useful for validation or the handling of side effects. + +:::info +By default, the `@Watch()` decorator does not fire when a component initially loads. +Use `@Watch('propName', {immediate: true})` to have the decorated method fire when the component first loads. +`immediate` watchers will be invoked before the component's first render, so be careful when trying to access DOM elements +that may not yet be available. +::: + +### Watching Native HTML Attributes + +Stencil's `@Watch()` decorator also allows you to watch native HTML attributes on the constructed host element. Simply +include the attribute name as the argument to the decorator (this is case-sensitive): + +```tsx +@Watch('aria-label') +onAriaLabelChange(newVal: string, oldVal: string) { + console.log('Label changed:', newVal, oldVal); +} +``` + +:::note +Since native attributes are not `@Prop()` or `State()` members of the Stencil component, they will not automatically trigger a +re-render when changed. If you wish to re-render a component in this instance, you can leverage the `forceUpdate()` method: + +```tsx +import { Component, forceUpdate, h } from '@stencil/core'; + +@Watch('aria-label') +onAriaLabelChange() { + forceUpdate(this); // Forces a re-render +} +``` +::: + +## Handling Arrays and Objects + +When Stencil checks if a class member decorated with `@Prop()` or `@State()` has changed, it checks if the reference to the class member has changed. +When a class member is an object or array, and is marked with `@Prop()` or `@State`, in-place mutation of an existing entity will _not_ cause `@Watch()` to fire, as it does not change the _reference_ to the class member. + +### Updating Arrays + +For arrays, the standard mutable array operations such as `push()` and `unshift()` won't trigger a component update. +These functions will change the content of the array, but won't change the reference to the array itself. + +In order to make changes to an array, non-mutable array operators should be used. +Non-mutable array operators return a copy of a new array that can be detected in a performant manner. +These include `map()` and `filter()`, and the [spread operator syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator). +The value returned by `map()`, `filter()`, etc., should be assigned to the `@Prop()` or `@State()` class member being watched. + +For example, to push a new item to an array, create a new array with the existing values and the new value at the end: + +```tsx +import { Component, State, Watch, h } from '@stencil/core'; + +@Component({ + tag: 'rand-numbers' +}) +export class RandomNumbers { + // We decorate a class member with @State() so that we + // can apply @Watch(). This will hold a list of randomly + // generated numbers + @State() randNumbers: number[] = []; + + private timer: NodeJS.Timer; + + // Apply @Watch() for the component's `randNumbers` member. + // Whenever `randNumbers` changes, this method will fire. + @Watch('randNumbers') + watchStateHandler(newValue: number[], oldValue: number[]) { + console.log('The old value of randNumbers is: ', oldValue); + console.log('The new value of randNumbers is: ', newValue); + } + + connectedCallback() { + this.timer = setInterval(() => { + // generate a random whole number + const newVal = Math.ceil(Math.random() * 100); + + /** + * This does not create a new array. When stencil + * attempts to see if any Watched members have changed, + * it sees the reference to its `randNumbers` State is + * the same, and will not trigger `@Watch` or a re-render + */ + // this.randNumbers.push(newVal) + + /** + * Using the spread operator, on the other hand, does + * create a new array. `randNumbers` is reassigned + * using the value returned by the spread operator. + * The reference to `randNumbers` has changed, which + * will trigger `@Watch` and a re-render + */ + this.randNumbers = [...this.randNumbers, newVal] + }, 1000) + } + + disconnectedCallback() { + if (this.timer) { + clearInterval(this.timer) + } + } + + render() { + return( +
    + randNumbers contains: +
      + {this.randNumbers.map((num) =>
    1. {num}
    2. )} +
    +
    + ) + } +} +``` + +### Updating an object + +The spread operator should be used to update objects. +As with arrays, mutating an object will not trigger a view update in Stencil. +However, using the spread operator and assigning its return value to the `@Prop()` or `@State()` class member being watched will. +Below is an example: + +```tsx +import { Component, State, Watch, h } from '@stencil/core'; + +export type NumberContainer = { + val: number, +} + +@Component({ + tag: 'rand-numbers' +}) +export class RandomNumbers { + // We decorate a class member with @State() so that we + // can apply @Watch(). This will hold a randomly generated + // number. + @State() numberContainer: NumberContainer = { val: 0 }; + + private timer: NodeJS.Timer; + + // Apply @Watch() for the component's `numberContainer` member. + // Whenever `numberContainer` changes, this method will fire. + @Watch('numberContainer') + watchStateHandler(newValue: NumberContainer, oldValue: NumberContainer) { + console.log('The old value of numberContainer is: ', oldValue); + console.log('The new value of numberContainer is: ', newValue); + } + + connectedCallback() { + this.timer = setInterval(() => { + // generate a random whole number + const newVal = Math.ceil(Math.random() * 100); + + /** + * This does not create a new object. When stencil + * attempts to see if any Watched members have changed, + * it sees the reference to its `numberContainer` State is + * the same, and will not trigger `@Watch` or are-render + */ + // this.numberContainer.val = newVal; + + /** + * Using the spread operator, on the other hand, does + * create a new object. `numberContainer` is reassigned + * using the value returned by the spread operator. + * The reference to `numberContainer` has changed, which + * will trigger `@Watch` and a re-render + */ + this.numberContainer = {...this.numberContainer, val: newVal}; + }, 1000) + } + + disconnectedCallback() { + if (this.timer) { + clearInterval(this.timer) + } + } + + render() { + return
    numberContainer contains: {this.numberContainer.val}
    ; + } +} +``` diff --git a/versioned_docs/version-v4.42/components/serialization-deserialization.md b/versioned_docs/version-v4.42/components/serialization-deserialization.md new file mode 100644 index 000000000..586cc1613 --- /dev/null +++ b/versioned_docs/version-v4.42/components/serialization-deserialization.md @@ -0,0 +1,196 @@ +--- +title: Serialization & Deserialization +sidebar_label: Serialization & Deserialization +description: Serialization & Deserialization +slug: /serialization +--- + +# Serialization & Deserialization + +Custom elements interact with the DOM either via HTML [attributes](https://open-wc.org/guides/knowledge/attributes-and-properties/#attributes) (always strings) or JavaScript [properties](https://open-wc.org/guides/knowledge/attributes-and-properties/#properties). Stencil automatically tries to keep properties and attributes in-sync when possible via **serialization** (turning properties into strings) and **deserialization** (turning strings back into properties). + +For example, if you have a component defined like this: + +```tsx +@Component({ + tag: 'my-component', +}) +export class MyComponent { + // Stencil 'sees' this as a number type. + // Numbers are easy to convert to/from strings + @Prop({ reflect: true }) myNumber: number; +} +``` + +When the property is set via JavaScript: + +```js +const myComponent = document.querySelector('my-component'); +myComponent.myNumber = 42; +``` + +Stencil will automatically serialize `myNumber` to an attribute: + +```html + +``` + +Conversely, if the attribute is set in HTML: + +```html + + + + +``` + +Stencil will automatically deserialize the attribute back to the property: + +```js +console.log(myComponent.myNumber); // 43 +``` + +Most of the time Stencil's automatic serialization and deserialization is enough - especially with primitive data types, however there are cases where you might want to customize this behavior, especially when dealing with complex data. + + +## The PropSerialize Decorator (`@PropSerialize()`) + +The `@PropSerialize()` decorator allows you to define custom serialization logic; converting a JavaScript property to a attribute string. The decorator accepts a single argument; the name of the class member `@Prop()` it is associated with. A method decorated with `@PropSerialize()` will automatically run when its associated property changes. + +```tsx +import { Component, Prop, PropSerialize } from '@stencil/core'; + +@Component({ + tag: 'my-component', +}) +export class MyComponent { + @Prop() aStringArray: string[]; + + @PropSerialize('aStringArray') + serializeStringArray(value: string[]) { + try { + return JSON.stringify(value); // must return a string + } catch (e) { + return null; // returning null removes the attribute + } + } +} +``` + +In the example above, the `serializeStringArray` method will run whenever the `aStringArray` property changes - the returned value will be used to update the attribute (no need to set `{reflect: true}` on the `@Prop()` decorator). E.g. + +```js +const myComponent = document.querySelector('my-component'); +myComponent.aStringArray = ['Hello', 'World']; +``` + +Becomes: + +```html + +``` + +## The AttrDeserialize Decorator (`@AttrDeserialize()`) + +The `@AttrDeserialize()` decorator allows you to define custom deserialization logic; converting an attribute string to a JavaScript property. The decorator accepts a single argument; the name of the class member `@Prop()` it is associated with. A method decorated with `@AttrDeserialize()` will automatically run when its associated attribute changes. + +```tsx +import { Component, Prop, AttrDeserialize } from '@stencil/core'; + +@Component({ + tag: 'my-component', +}) +export class MyComponent { + @Prop() aStringArray: string[]; + + @AttrDeserialize('aStringArray') + deserializeStringArray(value: string): string[] | null { + try { + return JSON.parse(value); + } catch (e) { + return null; + } + } +} +``` + +In the example above, the `deserializeStringArray` method will run whenever the `a-string-array` attribute changes. The method takes the new value of the attribute as an argument and must return the deserialized value. + +Now, when you set the attribute in HTML: + +```html + +``` + +Stencil will automatically deserialize the attribute back to the property: + +```js +const myComponent = document.querySelector('my-component'); +console.log(myComponent.aStringArray); // ['Hello', 'World'] +``` + +## Practical uses of PropSerialize + +Practically speaking, there is little disadvantage in using a `@AttrDeserialize()` on a complex property; it just adds another method for users to provide data to your component. + +The use-cases around using `@PropSerialize()` is slightly less obvious as in general, [it is not considered best practice to reflect complex data (like objects or arrays) as attributes](https://web.dev/articles/custom-elements-best-practices#aim-to-only-accept-rich-data-objects,-arrays-as-properties.) + +The following example illustrates a practical use case for `@PropSerialize()` using the [hydrate script output](../guides/hydrate-app.md) on a server we can fetch and serialize complex data to an attribute. When the same component loads in a browser, the component can de-serialize the data immediately without having to do another fetch. + +```tsx +import { AttrDeserialize, Build, Component, h, Prop, PropSerialize } from '@stencil/core'; + +interface User { + userName: string; + avatarUrl: string; + posts: any[] +} + +@Component({ + tag: 'user-login-panel', +}) +export class UserLogin { + @Prop() user: User; + + // On the server *only* let's represent the user's data as an attribute + // this allows the browser to get the data immediately without having to do a client-side fetch + + @PropSerialize('user') + userSerialize(newVal: User) { + if (Build.isBrowser) { + return null; + } + try { return JSON.stringify(newVal); } + catch (e) { return null; } + } + + // Whenever we have an attribute (including on client init) + // let's turn it back into an object that we can use and render + + @AttrDeserialize('user') + userDeserialize(newVal: string) { + try { return JSON.parse(newVal); } + catch (e) { return null; } + } + + async componentWillLoad() { + + // On the server *only*, let's do a secret login involving private keys etc. + + if (Build.isServer) { + // Because we have a serializer method, + // setting a value automatically reflects it to the dom attribute + + this.user = login(credentials); + } + } + + render() { + if (this.user) return (`Welcome ${this.user.userName}!`); + else return (`Please login`); + } +} +``` \ No newline at end of file diff --git a/versioned_docs/version-v4.42/components/state.md b/versioned_docs/version-v4.42/components/state.md new file mode 100644 index 000000000..5a78b0447 --- /dev/null +++ b/versioned_docs/version-v4.42/components/state.md @@ -0,0 +1,265 @@ +--- +title: Internal state +sidebar_label: Internal State +description: Use the State() for component's internal state +slug: /state +--- + +# State + +'State' is a general term that refers to the values and objects that are stored on a class or an instance of a class for +use now or in the future. + +Like a regular TypeScript class, a Stencil component may have one or more internal class members for holding value(s) +that make up the component's state. Stencil allows developers to optionally mark class members holding some part of the +class's state with the `@State()` decorator to trigger a rerender when the state changes. + +## The State Decorator (`@State`) + +Stencil provides a decorator to trigger a rerender when certain class members change. A component's class members that +should trigger a rerender must be decorated using Stencil's `@State()` decorator, like so: +```tsx +// First, we import State from '@stencil/core' +import { Component, State, h } from '@stencil/core'; + +@Component({ + tag: 'current-time', +}) +export class CurrentTime { + // Second, we decorate a class member with @State() + // When `currentTime` changes, a rerender will be + // triggered + @State() currentTime: number = Date.now(); + + render() { + // Within the component's class, its members are + // accessed via `this`. This allows us to render + // the value stored in `currentTime` + const time = new Date(this.currentTime).toLocaleTimeString(); + + return ( + {time} + ); + } +} +``` + +In the example above, `@State()` is placed before (decorates) the `currentTime` class member, which is a number. This +marks `currentTime` so that any time its value changes, the component rerenders. + +However, the example above doesn't demonstrate the real power of using `@State`. `@State` members are meant to only be +updated within a class, which the example above never does after the initial assignment of `currentTime`. This means +that our `current-time` component will never rerender! We fix that in the example below to update `current-time` every +1000 milliseconds (1 second): + +```tsx +import { Component, State, h } from '@stencil/core'; + +@Component({ + tag: 'current-time', +}) +export class CurrentTime { + timer: number; + + // `currentTime` is decorated with `@State()`, + // as we need to trigger a rerender when its + // value changes to show the latest time + @State() currentTime: number = Date.now(); + + connectedCallback() { + this.timer = window.setInterval(() => { + // the assignment to `this.currentTime` + // will trigger a re-render + this.currentTime = Date.now(); + }, 1000); + } + + disconnectedCallback() { + window.clearInterval(this.timer); + } + + render() { + const time = new Date(this.currentTime).toLocaleTimeString(); + + return ( + {time} + ); + } +} +``` + +The example above makes use of the [connectedCallback() lifecycle method](./component-lifecycle.md#connectedcallback) +to set `currentTime` to the value of `Date.now()` every 1000 milliseconds (or, every one second). Because the value of +`currentTime` changes every second, Stencil calls the `render` function on `current-time`, which pretty-prints the +current time. + +The example above also makes use of the +[disconnectedCallback() lifecycle method](./component-lifecycle.md#disconnectedcallback) to properly clean up the timer +that was created using `setInterval` in `connectedCallback()`. This isn't necessary for using `@State`, but is a general +good practice when using `setInterval`. + +## When to Use `@State()`? + +`@State()` should be used for all class members that should trigger a rerender when they change. However, not all +internal state might need to be decorated with `@State()`. If you know for sure that the value will either not change or +that it does not need to trigger a re-rendering, `@State()` is not necessary. It is considered a 'best practice' to +only use `@State()` when absolutely necessary. Revisiting our `current-time` component: + +```tsx +import { Component, State, h } from '@stencil/core'; + +@Component({ + tag: 'current-time', +}) +export class CurrentTime { + // `timer` is not decorated with `@State()`, as + // we do not wish to trigger a rerender when its + // value changes + timer: number; + + // `currentTime` is decorated with `@State()`, + // as we need to trigger a rerender when its + // value changes to show the latest time + @State() currentTime: number = Date.now(); + + connectedCallback() { + // the assignment to `this.timer` will not + // trigger a re-render + this.timer = window.setInterval(() => { + // the assignment to `this.currentTime` + // will trigger a re-render + this.currentTime = Date.now(); + }, 1000); + } + + disconnectedCallback() { + window.clearInterval(this.timer); + } + + render() { + const time = new Date(this.currentTime).toLocaleTimeString(); + + return ( + {time} + ); + } +} +``` + +## Examples + +### Using `@State()` with `@Listen()` + +This example makes use of `@State` and [`@Listen`](./events.md#listen-decorator) decorators. We define a class member +called `isOpen` and decorate it with `@State()`. With the use of `@Listen()`, we respond to click events toggling the +value of `isOpen`. + +```tsx +import { Component, Listen, State, h } from '@stencil/core'; + +@Component({ + tag: 'my-toggle-button' +}) +export class MyToggleButton { + // `isOpen` is decorated with `@State()`, + // changes to it will trigger a rerender + @State() isOpen: boolean = true; + + @Listen('click', { capture: true }) + handleClick() { + // whenever a click event occurs on + // the component, update `isOpen`, + // triggering the rerender + this.isOpen = !this.isOpen; + } + + render() { + return ; + } +} +``` + +### Complex Types + +For more advanced use cases, `@State()` can be used with a complex type. In the example below, we print a list of `Item` +entries. Although we start with zero `Item`s initially, we use the same pattern as we did before to add a new `Item` to +`ItemList`'s `items` array once every 2000 milliseconds (2 seconds). Every time a new entry is added to `items`, a +rerender occurs: + +```tsx +import { Component, State, h } from '@stencil/core'; + +// a user defined, complex type describing an 'Item' +type Item = { + id: number; + description: string, +} + +@Component({ + tag: 'item-list', +}) +export class ItemList { + // `timer` is not decorated with `@State()`, as + // we do not wish to trigger a rerender when its + // value changes + timer: number; + + // `items` will trigger a rerender if + // the value assigned to the variable changes + @State() items: Item[] = []; + + connectedCallback() { + // the assignment to `this.timer` will not + // trigger a re-render + this.timer = window.setInterval(() => { + const newTodo: Item = { + description: "Item", + id: this.items.length + 1 + }; + // the assignment to `this.items` will + // trigger a re-render. the assignment + // using '=' is important here, as we + // need that to make sure the rerender + // occurs + this.items = [...this.items, newTodo]; + }, 2000); + } + + disconnectedCallback() { + window.clearInterval(this.timer); + } + + render() { + return ( +
    +

    To-Do List

    +
      + {this.items.map((todo) =>
    • {todo.description} #{todo.id}
    • )} +
    +
    + ); + } +} +``` + +It's important to note that it's the reassignment of `this.items` that is causing the rerender in `connectedCallback()`: +```ts +this.items = [...this.items, newTodo]; +``` + +Mutating the existing reference to `this.items` like in the examples below will not cause a rerender, as Stencil will +not know that the contents of the array has changed: +```ts +// updating `items` either of these ways will not +// cause a rerender +this.items.push(newTodo); +this.items[this.items.length - 1] = newTodo; +``` + +Similar to the examples above, this code sample makes use of the +[connectedCallback() lifecycle method](./component-lifecycle.md#connectedcallback) to create a new `Item` and add +it to `items` every 2000 milliseconds (every two seconds). The example above also makes use of the +[disconnectedCallback() lifecycle method](./component-lifecycle.md#disconnectedcallback) to properly clean up the timer +that was created using `setInterval` in `connectedCallback()`. diff --git a/versioned_docs/version-v4.42/components/styling.md b/versioned_docs/version-v4.42/components/styling.md new file mode 100644 index 000000000..1da0f41c4 --- /dev/null +++ b/versioned_docs/version-v4.42/components/styling.md @@ -0,0 +1,374 @@ +--- +title: Styling Components +sidebar_label: Styling +description: Styling Components +slug: /styling +--- + +# Styling Components + +## Shadow DOM + +### What is the Shadow DOM? + +The [shadow DOM](https://developers.google.com/web/fundamentals/web-components/shadowdom) is an API built into the browser that allows for DOM encapsulation and style encapsulation. It is a core aspect of the Web Component standards. The shadow DOM shields a component's styles, markup, and behavior from its surrounding environment. This means that we do not need to be concerned about scoping our CSS to our component, nor worry about a component's internal DOM being interfered with by anything outside the component. + +When talking about the shadow DOM, we use the term "light DOM" to refer to the "regular" DOM. The light DOM encompasses any part of the DOM that does not use the shadow DOM. + +### Shadow DOM in Stencil + +The shadow DOM hides and separates the DOM of a component in order to prevent clashing styles or unwanted side effects. We can use the shadow DOM in our Stencil components to ensure our components won't be affected by the applications in which they are used. + +To use the Shadow DOM in a Stencil component, you can set the `shadow` option to `true` in the component decorator. + +```tsx +@Component({ + tag: 'shadow-component', + styleUrl: 'shadow-component.css', + shadow: true, +}) +export class ShadowComponent {} +``` + +If you'd like to learn more about enabling and configuring the shadow DOM, see the [shadow field of the component api](./component.md#component-options). + +By default, components created with the [`stencil generate` command](../config/cli.md#stencil-generate) use the shadow DOM. + +### Styling with the Shadow DOM + +With the shadow DOM enabled, elements within the shadow root are scoped, and styles outside of the component do not apply. As a result, CSS selectors inside the component can be simplified, as they will only apply to elements within the component. We do not have to include any specific selectors to scope styles to the component. + +```css +:host { + color: black; +} + +div { + background: blue; +} +``` + +:::note +The `:host` pseudo-class selector is used to select the [`Host` element](./host-element.md) of the component +::: + +With the shadow DOM enabled, only these styles will be applied to the component. Even if a style in the light DOM uses a selector that matches an element in the component, those styles will not be applied. + +### Shadow DOM QuerySelector + +When using Shadow DOM and you want to query an element inside your web component, you must first use the [`@Element` decorator](./host-element.md#element-decorator) to gain access to the host element, and then you can use the `shadowRoot` property to perform the query. This is because all of your DOM inside your web component is in a shadowRoot that Shadow DOM creates. For example: + +```tsx +import { Component, Element } from '@stencil/core'; + +@Component({ + tag: 'shadow-component', + styleUrl: 'shadow-component.css', + shadow: true +}) +export class ShadowComponent { + + @Element() el: HTMLElement; + + componentDidLoad() { + const elementInShadowDom = this.el.shadowRoot.querySelector('.a-class-selector'); + + ... + } + +} +``` + +### Shadow DOM Browser Support + +The shadow DOM is currently natively supported in the following browsers: + +- Chrome +- Firefox +- Safari +- Edge (v79+) +- Opera + +In browsers which do not support the shadow DOM we fall back to scoped CSS. This gives you the style encapsulation that comes along with the shadow DOM but without loading in a huge shadow DOM polyfill. + +### Scoped CSS + +An alternative to using the shadow DOM is using scoped components. You can use scoped components by setting the `scoped` option to `true` in the component decorator. + +```tsx +@Component({ + tag: 'scoped-component', + styleUrl: 'scoped-component.css', + scoped: true, +}) +export class ScopedComponent {} +``` + +Scoped CSS is a proxy for style encapsulation. It works by appending a data attribute to your styles to make them unique and thereby scope them to your component. It does not, however, prevent styles from the light DOM from seeping into your component. + +## CSS Custom Properties + +CSS custom properties, also often referred to as CSS variables, are used to contain values that can then be used in multiple CSS declarations. For example, we can create a custom property called `--color-primary` and assign it a value of `blue`. + +```css +:host { + --color-primary: blue; +} +``` + +And then we can use that custom property to style different parts of our component + +```css +h1 { + color: var(--color-primary); +} +``` + +### Customizing Components with Custom Properties + +CSS custom properties can allow the consumers of a component to customize a component's styles from the light DOM. Consider a `shadow-card` component that uses a custom property for the color of the card heading. + +```css +:host { + --heading-color: black; +} + +.heading { + color: var(--heading-color); +} +``` + +:::note +CSS custom properties must be declared on the `Host` element (`:host`) in order for them to be exposed to the consuming application. +::: + +The `shadow-card` heading will have a default color of `black`, but this can now be changed in the light DOM by selecting the `shadow-card` and changing the value of the `--heading-color` custom property. + +```css +shadow-card { + --heading-color: blue; +} +``` + +## CSS Parts + +CSS custom properties can be helpful for customizing components from the light DOM, but they are still a little limiting as they only allow a user to modify specific properties. For situations where users require a higher degree of flexibility, we recommend using the [CSS `::part()` pseudo-element](https://developer.mozilla.org/en-US/docs/Web/CSS/::part). You can define parts on elements of your component with the "part" attribute. + +```tsx +@Component({ + tag: 'shadow-card', + styleUrl: 'shadow-card.css', + shadow: true, +}) +export class ShadowCard { + @Prop() heading: string; + + render() { + return ( + +

    {this.heading}

    + +
    + ); + } +} +``` + +Then you can use the `::part()` pseudo-class on the host element to give any styles you want to the element with the corresponding part. + +```css +shadow-card::part(heading) { + text-transform: uppercase; +} +``` + +This allows for greater flexibility in styling as any styles can now be added to this element. + +### Exportparts + +If you have a Stencil component nested within another component, any `part` specified on elements of the child component will not be exposed through the parent component. In order to expose the `part`s of the child component, you need to use the `exportparts` attribute. Consider this `OuterComponent` which contains the `InnerComponent`. + +```tsx +@Component({ + tag: 'outer-component', + styleUrl: 'outer-component.css', + shadow: true, +}) +export class OuterComponent { + render() { + return ( + +

    Outer Component

    + +
    + ); + } +} + +@Component({ + tag: 'inner-component', + styleUrl: 'inner-component.css', + shadow: true, +}) +export class InnerComponent { + render() { + return ( + +

    Inner Component

    +
    + ); + } +} +``` + +By specifying "inner-text" as the value of the `exportparts` attribute, elements of the `InnerComponent` with a `part` of "inner-text" can now be styled in the light DOM. Even though the `InnerComponent` is not used directly, we can style its parts through the `OuterComponent`. + +```html + + + +``` + +## Style Modes + +Component Style Modes enable you to create versatile designs for your components by utilizing different styling configurations. This is achieved by assigning the styleUrls property of a component to a collection of style mode names, each linked to their respective CSS files. + +### Example: Styling a Button Component + +Consider a basic button component that supports both iOS and Material Design aesthetics: + +```tsx title="Using style modes to style a component" +@Component({ + tag: 'simple-button', + styleUrls: { + md: './simple-button.md.css', // styles for Material Design + ios: './simple-button.ios.css' // styles for iOS + }, +}) +export class SimpleButton { + // ... +} +``` + +In the example above, two different modes are declared. One mode is named `md` (for 'Material Design') and refers back to a Material Design-specific stylesheet. Likewise, the other is named `ios` (for iOS) and references a different stylesheet for iOS-like styling. Both stylesheets are relative paths to the file that declares the component. While we have chosen short names in the above example, there's no limitation to the keys used in the `styleUrls` object. + +To dictate the style mode (Material Design or iOS) in which the button should be rendered, you must initialize the desired mode before any component rendering occurs. This can be done as follows: + +```ts +import { setMode } from '@stencil/core'; +setMode(() => 'ios'); // Setting iOS as the default mode for all components +``` + +The `setMode` function processes all elements, enabling the assignment of modes individually based on specific element attributes. For instance, by assigning the `mode` attribute to a component: + +```html + +``` + +You can conditionally set the style mode based on the `mode` property: + +```ts +import { setMode } from '@stencil/core'; + +const defaultMode = 'md'; // Default to Material Design +setMode((el) => el.getAttribute('mode') || defaultMode); +``` + +The reason for deciding which mode to apply can be very arbitrary and based on your requirements, using an element property called `mode` is just one example. + +### Important Considerations + +- __Initialization:__ Style modes must be defined at the start of the component lifecycle and cannot be changed thereafter. If you like to change the components mode dynamically you will have to re-render it entirely. +- __Usage Requirement:__ A style mode must be set to ensure the component loads with styles. Without specifying a style mode, the component will not apply any styles. +- __Input Validation:__ Verify a style mode is supported by a component you are setting it for. Setting an un-supported style mode keeps the component unstyled. +- __Querying Style Mode:__ To check the current style mode and e.g. provide different functionality based on the mode, use the `getMode` function: + +```ts +import { getMode } from '@stencil/core'; + +const simpleButton = document.queryElement('simple-button') +console.log(getMode(simpleButton)); // Outputs the current style mode of component +``` + +This approach ensures your components are adaptable and can dynamically switch between different styles, enhancing the user experience across various platforms and design preferences. + +## Global styles + +While most styles are usually scoped to each component, sometimes it's useful to have styles that are available to all the components in your project. To create styles that are globally available, start by creating a global stylesheet. For example, you can create a folder in your `src` directory called `global` and create a file called `global.css` within that. Most commonly, this file is used to declare CSS custom properties on the root element via the `:root` pseudo-class. This is because styles provided via the `:root` pseudo-class can pass through the shadow boundary. For example, you can define a primary color that all your components can use. + +```css +:root { + --color-primary: blue; +} +``` + +In addition to CSS custom properties, other use cases for a global stylesheet include + +- Theming: defining CSS variables used across the app +- Load fonts with `@font-face` +- App wide font-family +- CSS resets + +To make the global styles available to all the components in your project, the `stencil.config.ts` file comes with an optional [`globalStyle` setting](../config/01-overview.md#globalstyle) that accepts the path to your global stylesheet. + +```tsx +export const config: Config = { + namespace: 'app', + globalStyle: 'src/global/global.css', + outputTarget: [ + { + type: 'www', + }, + ], +}; +``` + +The compiler will run the same minification, autoprefixing, and plugins over `global.css` and generate an output file for the [`www`](../output-targets/www.md) and [`dist`](../output-targets/dist.md) output targets. The generated file will always have the `.css` extension and be named as the specified `namespace`. + +In the example above, since the namespace is `app`, the generated global styles file will be located at: `./www/build/app.css`. + +This file must be manually imported in the `index.html` of your application. + +```html + +``` + +### Constructable Stylesheets + +In addition to being available in the light DOM, global styles are automatically registered to every shadow root via [constructable stylesheets](https://web.dev/constructable-stylesheets/). This means that your global styles can target and style shadow DOM components directly. + +This allows you to apply styles to specific component types using the `:host()` pseudo-class with a tag name selector. For example, you can target all instances of a specific component: + +```css +/* In your global stylesheet */ +:host(my-button) { + --button-border-radius: 8px; + display: inline-block; +} + +:host(my-card) { + --card-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); + margin: 16px 0; +} + +/* You can also use attribute selectors */ +:host(my-input[type="password"]) { + --input-font-family: monospace; +} +``` + +The `:host()` function allows you to select the host element of a component when it matches the given selector. This is particularly useful for: + +- Setting default CSS custom properties for specific component types +- Applying consistent spacing or layout styles across all instances of a component +- Theming components based on their tag names or attributes + +:::note +The `:host()` selector in global styles will only affect components that use shadow DOM. For scoped components, you should use regular tag selectors in your global styles. +::: + +This behavior can be turned off via the [`extras.addGlobalStyleToComponents`](../config/extras.md#addglobalstyletocomponents) flag. diff --git a/versioned_docs/version-v4.42/components/templating-and-jsx.md b/versioned_docs/version-v4.42/components/templating-and-jsx.md new file mode 100644 index 000000000..74f92c3a2 --- /dev/null +++ b/versioned_docs/version-v4.42/components/templating-and-jsx.md @@ -0,0 +1,540 @@ +--- +title: Using JSX +sidebar_label: Using JSX +description: Using JSX +slug: /templating-jsx +--- + +# Using JSX + +Stencil components are rendered using JSX, a popular, declarative template syntax. Each component has a `render` function that returns a tree of components that are rendered to the DOM at runtime. + +## Basics + +### The `h` and `Fragment` functions + +The `h` function (short for "hyperscript") is a factory function that creates virtual DOM elements. When you write JSX like `
    Hello
    `, it gets transformed into function calls like `h('div', null, 'Hello')`. The `Fragment` function is used to group a list of children without adding extra nodes to the DOM. + +#### Set up in Stencil + +Within your Stencil project, make sure to add or modify your `tsconfig.json` file, adding: + +```json +{ + "compilerOptions": { + "jsx": "react", + "jsxFactory": "h", + "jsxFragmentFactory": "Fragment" + } +} +``` + +You will then need to import `h` (and `Fragment` if you plan to use it) from `@stencil/core` at the top of your component file: + +```tsx +import { h, Fragment, Component } from '@stencil/core'; + +@Component({ + tag: 'my-component', +}) +class MyComponent { ... } +``` + +(Read more about using the `Fragment` component in the [Complex Template Content](#complex-template-content) section below.) + +### `jsxImportSource` alternative + +`jsxImportSource` is a more modern approach that automatically imports the necessary JSX runtime functions instead of manually importing `h`; the compiler automatically imports what it needs from the specified package. + +#### Set up in Stencil + +To use `jsxImportSource`, modify your `tsconfig.json` file as follows: + +```json +{ + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "@stencil/core" + } +} +``` + +Using this approach means you do not need to manually import `h` or `Fragment` in your component files. + +### The `render` function + +The `render` function is used within your component to output a tree of elements that will be drawn to the screen. + +```tsx +class MyComponent { + render() { + return ( +
    +

    Hello World

    +

    This is JSX!

    +
    + ); + } +} +``` + +In this example we're returning the JSX representation of a `div`, with two child elements: an `h1` and a `p`. + +### Host Element + +If you want to modify the host element itself, such as adding a class or an attribute to the component itself, use the `` functional component. Check for more details [here](./host-element.md) + + +## Data Binding + +Components often need to render dynamic data. To do this in JSX, use `{ }` around a variable: + +```tsx +render() { + return ( +
    Hello {this.name}
    + ) +} +``` + +:::note +If you're familiar with ES6 template variables, JSX variables are very similar, just without the `$`: +::: + +```tsx +//ES6 +`Hello ${this.name}` + +//JSX +Hello {this.name} +``` + + +## Conditionals + +If we want to conditionally render different content, we can use JavaScript if/else statements: +Here, if `name` is not defined, we can just render a different element. + +```tsx +render() { + if (this.name) { + return (
    Hello {this.name}
    ) + } else { + return (
    Hello, World
    ) + } +} +``` + +Additionally, inline conditionals can be created using the JavaScript ternary operator: + +```tsx +render() { + return ( +
    + {this.name + ?

    Hello {this.name}

    + :

    Hello World

    + } +
    + ); +} +``` + +**Please note:** Stencil reuses DOM elements for better performance. Consider the following code: + +```tsx +{someCondition + ? + : +} +``` + +The above code behaves exactly the same as the following code: + +```tsx + +``` + +Thus, if `someCondition` changes, the internal state of `` won't be reset and its lifecycle methods such as `componentWillLoad()` won't fire. Instead, the conditional merely triggers an update to the very same component. + +If you want to destroy and recreate a component in a conditional, you can assign the `key` attribute. This tells Stencil that the components are actually different siblings: + +```tsx +{someCondition + ? + : +} +``` + +This way, if `someCondition` changes, you get a new `` component with fresh internal state that also runs the lifecycle methods `componentWillLoad()` and `componentDidLoad()`. + + +## Slots + +Components often need to render dynamic children in specific locations in their component tree, allowing a developer to supply child content when using our component, with our component placing that child component in the proper location. + +To do this, you can use the [Slot](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot) tag inside of your `my-component`. + +```tsx +// my-component.tsx + +render() { + return ( +
    +

    A Component

    +
    +
    + ); +} + +``` + +Then, if a user passes child components when creating our component `my-component`, then `my-component` will place that +component inside of the second `
    ` above: + +```tsx +render(){ + return( + +

    Child Element

    +
    + ) +} +``` + +Slots can also have `name`s to allow for specifying slot output location: + +```tsx +// my-component.tsx + +render(){ + return [ + , +

    Here is my main content

    , + + ] +} +``` + +```tsx +render(){ + return( + +

    I'll be placed before the h1

    +

    I'll be placed after the h1

    +
    + ) +} +``` + +### Slots Outside Shadow DOM + +:::caution +Slots are native to the [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM), but Stencil polyfills +the behavior to work for non-shadow components as well. However, you may encounter issues using slots outside the Shadow DOM especially with +component trees mixing shadow and non-shadow components, or when passing a slot through many levels of components. In many cases, this behavior can +be remedied by wrapping the `slot` in an additional element (like a `div` or `span`) so the Stencil runtime can correctly "anchor" the relocated +content in its new location. +::: + +There are known use cases that the Stencil runtime is not able to support: + +- Forwarding slotted content to another slot with a different name:
    + It is recommended that slot names stay consistent when slotting content through multiple levels of components. **Avoid** defining slot tags like + ``. + +## Dealing with Children + +The children of a node in JSX correspond at runtime to an array of nodes, +whether they are created by mapping across an array with +[`Array.prototype.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) +or simply declared as siblings directly in JSX. This means that at runtime the +children of the two top-level divs below (`.todo-one` and `.todo-two`) will be +represented the same way: + + +```tsx +render() { + return ( + <> +
    + {this.todos.map((todo) => ( + { todo.taskName } + )} +
    +
    + { todos[0].taskName } + { todos[1].taskName } +
    + + ) +} +``` + +If this array of children is dynamic, i.e., if any nodes may be added, +removed, or reordered, then it's a good idea to set a unique `key` attribute on +each element like so: + +```tsx +render() { + return ( +
    + {this.todos.map((todo) => ( +
    +
    {todo.taskName}
    +
    + ))} +
    + ) +} +``` + +When nodes in a children array are rearranged Stencil makes an effort to +preserve DOM nodes across renders but it isn't able to do so in all cases. +Setting a `key` attribute lets Stencil ensure it can match up new and old +children across renders and thereby avoid recreating DOM nodes unnecessarily. + +:::caution +Do not use an array index or some other non-unique value as a key. Try to +ensure that each child has a key which does not change and which is unique +among all its siblings. +::: + +### Automatic Key Insertion + +During compilation Stencil will automatically add key attributes to any JSX +nodes in your component's render method which are not nested within curly +braces. This allows Stencil’s runtime to accurately reconcile children when +their order changes or when a child is conditionally rendered. + +For instance, consider a render method looking something like this: + +```tsx + render() { + return ( +
    + { this.disabled &&
    no key!
    } +
    + +
    +
    + ); + } +``` + +While it might seem like adding a key attribute to the `#slot-wrapper` div +could help ensure that elements will be matched up correctly when the component +re-renders, this is actually superfluous because Stencil will automatically add +a key to that element when it compiles your component. + +:::note +The Stencil compiler can only safely perform automatic key insertion in certain +scenarios where there is no danger of the keys accidentally causing elements to +be considered different when they should be treated the same (or vice versa). + +In particular, the compiler will not automatically insert `key` attributes if a +component's `render` method has more than one `return` statement or if it +returns a [conditional +expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator). +Additionally, the compiler will not add key attributes to any JSX which is +found within curly braces (`{ }`). +::: + +## Handling User Input + +Stencil uses native [DOM events](https://developer.mozilla.org/en-US/docs/Web/Events). + +Here's an example of handling a button click. Note the use of the [Arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). + +```tsx +... +export class MyComponent { + private handleClick = () => { + alert('Received the button click!'); + } + + render() { + return ( + + ); + } +} +``` + +Here's another example of listening to input `change`. Note the use of the [Arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). + +```tsx +... +export class MyComponent { + private inputChanged = (event: Event) => { + console.log('input changed: ', (event.target as HTMLInputElement).value); + } + + render() { + return ( + + ); + } +} +``` + + +## Complex Template Content + +So far we've seen examples of how to return only a single root element. We can also nest elements inside our root element + +In the case where a component has multiple "top level" elements, the `render` function can return an array. +Note the comma in between the `
    ` elements. + +```tsx +render() { + return ([ + // first top level element +
    +
      +
    • Item 1
    • +
    • Item 2
    • +
    • Item 3
    • +
    +
    , + + // second top level element, note the , above +
    + ... more html content ... +
    + ]); +} +``` + +Alternatively you can use the `Fragment` functional component, in which case you won't need to add commas: + +```tsx +import { Fragment } from '@stencil/core'; +... +render() { + return ( + // first top level element +
    +
      +
    • Item 1
    • +
    • Item 2
    • +
    • Item 3
    • +
    +
    + +
    + ... more html content ... +
    +
    ); +} +``` + +It is also possible to use `innerHTML` to inline content straight into an element. This can be helpful when, for example, loading an svg dynamically and then wanting to render that inside of a `div`. This works just like it does in normal HTML: + +```markup +
    +``` + +## Getting a reference to a DOM element + +In cases where you need to get a direct reference to an element, like you would normally do with `document.querySelector`, you might want to use a `ref` in JSX. Lets look at an example of using a `ref` in a form: + +```tsx +@Component({ + tag: 'app-home', +}) +export class AppHome { + + textInput!: HTMLInputElement; + + handleSubmit = (event: Event) => { + event.preventDefault(); + console.log(this.textInput.value); + } + + render() { + return ( +
    + + +
    + ); + } +} +``` + +In this example we are using `ref` to get a reference to our input `ref={(el) => this.textInput = el as HTMLInputElement}`. We can then use that ref to do things such as grab the value from the text input directly `this.textInput.value`. + +## Explicitly setting attributes or properties + +By default, Stencil tries to intelligently determine whether to set an attribute or (more commonly) a property on an element when using JSX. +However, in some cases you may want to explicitly set one or the other. You can do this by using the `attr:` or `prop:` prefixes. For example: + +```tsx + +``` + +will set the `value` attribute on the input element, while: + +```tsx + +``` + +will explicitly set the `value` property on the input element. + + +## Avoid Shared JSX Nodes + +The renderer caches element lookups in order to improve performance. However, a side effect from this is that the exact same JSX node should not be shared within the same renderer. + +In the example below, the `sharedNode` variable is reused multiple times within the `render()` function. The renderer is able to optimize its DOM element lookups by caching the reference, however, this causes issues when nodes are reused. Instead, it's recommended to always generate unique nodes like the changed example below. + +```diff +@Component({ + tag: 'my-cmp', +}) +export class MyCmp { + + render() { +- const sharedNode =
    Text
    ; + return ( +
    +- {sharedNode} +- {sharedNode} ++
    Text
    ++
    Text
    +
    + ); + } +} +``` + +Alternatively, creating a factory function to return a common JSX node could be used instead since the returned value would be a unique instance. For example: + +```tsx +@Component({ + tag: 'my-cmp', +}) +export class MyCmp { + + getText() { + return
    Text
    ; + } + + render() { + return ( +
    + {this.getText()} + {this.getText()} +
    + ); + } +} +``` + +## Other Resources + +- [Understanding JSX for StencilJS Applications](https://www.joshmorony.com/understanding-jsx-for-stencil-js-applications/) diff --git a/versioned_docs/version-v4.42/config/01-overview.md b/versioned_docs/version-v4.42/config/01-overview.md new file mode 100644 index 000000000..dc96c76fe --- /dev/null +++ b/versioned_docs/version-v4.42/config/01-overview.md @@ -0,0 +1,586 @@ +--- +title: Config +sidebar_label: Overview +description: Config +slug: /config +--- + +# Stencil Config + +In most cases, the `stencil.config.ts` file does not require any customization since Stencil comes with great default values out-of-the-box. In general, it's preferred to keep the config as minimal as possible. In fact, you could even delete the `stencil.config.ts` file entirely and an app would compile just fine. But at the same time, the compiler can be configured at the lowest levels using this config. Below are the many *optional* config properties. + +Example `stencil.config.ts`: + +```tsx +import { Config } from '@stencil/core'; + +export const config: Config = { + namespace: 'MyApp', + srcDir: 'src' +}; +``` + +## buildDist + +*default: true (prod), false (dev)* + +Sets whether or not Stencil will execute output targets and write output to +`dist/` when `stencil build` is called. Defaults to `false` when building for +development and `true` when building for production. If set to `true` then +Stencil will always build all output targets, regardless of whether the build +is in dev or prod mode or using watch mode. + +```tsx +buildDist: true +``` + +## buildEs5 + +Sets if the ES5 build should be generated or not. +It defaults to `false`. +Setting `buildEs5` to `true` will also create ES5 builds for both dev and prod modes. +Setting `buildEs5` to `prod` will only build ES5 in prod mode. + +```tsx +buildEs5: boolean | 'prod' +``` + +## bundles + +By default, Stencil will statically analyze the application and generate a component graph of how all the components are interconnected. From the component graph it is able to best decide how components should be grouped depending on their usage with one another within the app. By doing so it's able to bundle components together in order to reduce network requests. However, bundles can be manually generated using the `bundles` config. + +The `bundles` config is an array of objects that represent how components are grouped together in lazy-loaded bundles. This config is rarely needed as Stencil handles this automatically behind the scenes. + +```tsx +bundles: [ + { components: ['ion-button'] }, + { components: ['ion-card', 'ion-card-header'] } +] +``` + +## cacheDir + +*default: '.stencil'* + +The directory where sub-directories will be created for caching when [`enableCache`](#enablecache) is set `true` or if using +[Stencil's Screenshot Connector](../testing/stencil-testrunner/07-screenshot-connector.md). + +A Stencil config like the following: + +```ts title='stencil.config.ts' +import { Config } from '@stencil/core'; + +export const config: Config = { + ..., + enableCache: true, + cacheDir: '.cache', + testing: { + screenshotConnector: 'connector.js' + } +} +``` + +Will result in the following file structure: + +```tree +stencil-project-root +└── .cache + ├── .build <-- Where build related file caching is written + | + └── screenshot-cache.json <-- Where screenshot caching is written +``` + +## devServer + +Please see the [Dev-Server docs](./dev-server.md). + +## docs + +Please see the [docs config](./docs.md). + +## enableCache + +*default: `true`* + +Stencil will cache build results in order to speed up rebuilds. To disable this feature, set `enableCache` to `false`. + +```tsx +enableCache: true +``` + +## excludeComponents + +*default: `[]`* + +An array of component tag names to exclude from production builds. +Useful to remove test, demo or experimental components from final output. + +Supports glob patterns for matching multiple components: +- `['demo-*']` - Excludes all components starting with "demo-" +- `['*-test', '*-demo']` - Excludes components ending with "-test" or "-demo" +- `['my-component']` - Excludes a specific component + +Components matching these patterns will be completely excluded from all output targets when *not* using the `--dev` flag. + +## extras + +Please see the [Extras docs](./extras.md). + +## env + +*default: `{}`* + +An object that can hold environment variables for your components to import and use. These variables can hold data objects depending on the environment you compile the components for. For example, let's say we want to provide an URL to our API based on a specific environment, we could provide it as such: + +```ts title="stencil.config.ts" +import { Config } from '@stencil/core'; + +export const config: Config = { + ..., + env: { + API_BASE_URL: process.env.API_BASE_URL + } +} +``` + +Now when you build your components with this environment variable set, you can import it in your component as follows: + +```ts +import { Component, h, Env, Host } from '@stencil/core'; + +@Component({ + tag: 'api-component', +}) +export class APIComponent { + async connectedCallback () { + const res = await fetch(Env.API_BASE_URL) + // ... + } +} +``` + +## generateExportMaps + +*default: `false`* + +Stencil will generate [export maps](https://nodejs.org/api/packages.html#packages_exports) that correspond with various output target outputs. This includes the root +entry point based on the [primary output target](../output-targets/01-overview.md#primary-package-output-target-validation) (or first eligible output target if not specified), +the entry point for the lazy-loader (if using the `dist` output target), and entry points for each component (if using `dist-custom-elements`). + +## globalScript + +The global script config option takes a file path as a string. + +The global script runs once before your library/app loads, so you can do things like setting up a connection to an external service or configuring a library you are using. + +The code to be executed should be placed within a default function that is exported by the global script. Ensure all of the code in the global script is wrapped in the function that is exported: + +```javascript +export default function() { // or export default async function() + initServerConnection(); +} +``` + +:::note +The exported function can also be `async` but be aware that this can have implications on the performance of your application as all rendering operations will be deferred until after the global script finishes. +::: + +Imported methods from Stencil, such as `setMode`, `BUILD` and others, can only be accessed within the function exported by the script. They will not be available in the global scope outside of this function. + +```javascript +import { setMode, BUILD } from '@stencil/core'; + +// ❌ This won't work as Stencil primitives are not initialized at this point +// console.log('Build mode:', BUILD.isDev); + +export default function() { + // ✅ This works - Stencil methods are available within the exported function + if (BUILD.isDev) { + console.log('Development mode detected'); + setMode((elm) => elm.getAttribute('mode') || 'dev'); + } else { + console.log('Production mode'); + setMode((elm) => elm.getAttribute('mode') || 'prod'); + } +} +``` + +## globalStyle + +Stencil is traditionally used to compile many components into an app, and each component comes with its own compartmentalized styles. However, it's still common to have styles which should be "global" across all components and the website. A global CSS file is often useful to set [CSS Variables](../components/styling.md). + +Additionally, the `globalStyle` config can be used to precompile styles with Sass, PostCSS, etc. + +Below is an example folder structure containing a webapp's global css file, named `app.css`. + +```bash +src/ + components/ + global/ + app.css +``` + +The global style config takes a file path as a string. The output from this build will go to the `buildDir`. In this example it would be saved to `www/build/app.css`. Additionally, these global styles are automatically applied to all components with shadow roots via constructable stylesheets, allowing you to style shadow DOM components directly. + +```tsx +globalStyle: 'src/global/app.css' +``` + +Check out the [styling docs](../components/styling.md#global-styles) of how to use global styles in your app. + +## hashedFileNameLength + +*default: `8`* + +When the `hashFileNames` config is set to `true`, and it is a production build, the `hashedFileNameLength` config is used to determine how many characters the file name's hash should be. + +```tsx +hashedFileNameLength: 8 +``` + +## hashFileNames + +*default: `true`* + +During production builds, the content of each generated file is hashed to represent the content, and the hashed value is used as the filename. If the content isn't updated between builds, then it receives the same filename. When the content is updated, then the filename is different. By doing this, deployed apps can "forever-cache" the build directory and take full advantage of content delivery networks (CDNs) and heavily caching files for faster apps. + +```tsx +hashFileNames: true +``` + +## hydratedFlag + +When using the [lazy build](https://stenciljs.com/docs/distribution) Stencil +has support for automatically applying a class or attribute to a component and +all of its child components when they have finished hydrating. This can be used +to prevent a [flash of unstyled content +(FOUC)](https://en.wikipedia.org/wiki/Flash_of_unstyled_content), a +typically-undesired 'flicker' of unstyled HTML that might otherwise occur +during component rendering while various components are asynchronously +downloaded and rendered. + +By default, Stencil will add the `hydrated` CSS class to elements to indicate +hydration. The `hydratedFlag` config field allows this behavior to be +customized, by changing the name of the applied CSS class, setting it to use an +attribute to indicate hydration, or changing which type of CSS properties and +values are assigned before and after hydrating. This config can also be used to +turn off this behavior by setting it to `null`. + +If a Stencil configuration does not supply a value for `hydratedFlag` then +Stencil will automatically generate the following default configuration: + +```ts +const defaultHydratedFlag: HydratedFlag = { + hydratedValue: 'inherit', + initialValue: 'hidden', + name: 'hydrated', + property: 'visibility', + selector: 'class', +}; +``` + +If `hydratedFlag` is explicitly set to `null`, Stencil will not set a default +configuration and the behavior of marking hydration with a class or attribute +will be disabled. + +```tsx +hydratedFlag: null | { + name?: string, + selector?: 'class' | 'attribute', + property?: string, + initialValue?: string, + hydratedValue?: string +} +``` + +The supported options are as follows: + +### name + +*default: 'hydrated'* + +The name which Stencil will use for the attribute or class that it sets on +elements to indicate that they are hydrated. + +```tsx +name: string +``` + +### selector + +*default: 'class'* + +The way that Stencil will indicate that a component has been hydrated. When +`'class'`, Stencil will set the `name` option on the element as a class, and +when `'attribute'`, Stencil will similarly set the `name` option as an +attribute. + +```tsx +selector: 'class' | 'attribute' +``` + +### property + +*default: 'visibility'* + +The CSS property used to show and hide components. This defaults to the CSS +`visibility` property. Other possible CSS properties might include `display` +with the `initialValue` setting as `none`, or `opacity` with the `initialValue` +as `0`. Defaults to `visibility`. + +```tsx +property: string +``` + +### initialValue + +*default: 'hidden'* + +This is the value which should be set for the property specified by `property` +on all components before hydration. + +```tsx +initialValue: string +``` + +### hydratedValue + +*default: 'inherit'* + +This is the value which should be set for the property specified by `property` +on all components once they've completed hydration. + +```tsx +hydratedValue: string +``` + +## invisiblePrehydration + +*default: `true`* + +When `true`, `invisiblePrehydration` will visually hide components before they are hydrated by adding an automatically injected style tag to the document's head. Setting `invisiblePrehydration` to `false` will not inject the style tag into the head, allowing you to style your web components pre-hydration. + +:::note +Setting `invisiblePrehydration` to `false` will cause everything to be visible when your page is loaded, causing a more prominent Flash of Unstyled Content (FOUC). However, you can style your web component's fallback content to your preference. +::: + +```tsx +invisiblePrehydration: true +``` + +## minifyCss + +_default: `true` in production_ + +When `true`, the browser CSS file will be minified. + +## minifyJs + +_default: `true` in production_ + +When `true`, the browser JS files will be minified. Stencil uses [Terser](https://terser.org/) under-the-hood for file minification. + +## namespace + +*default: `App`* + +The `namespace` config is a `string` representing a namespace for the app. For apps that are not meant to be a library of reusable components, the default of `App` is just fine. However, if the app is meant to be consumed as a third-party library, such as `Ionic`, a unique namespace is required. + +```tsx +namespace: "Ionic" +``` +## outputTargets + +Please see the [Output Target docs](../output-targets/01-overview.md). + +## plugins + +Please see the [Plugin docs](./plugins.md). + +## preamble + +*default: `undefined`* + +Used to help to persist a banner or add relevant information about the resulting build, the `preamble` configuration +field is a `string` that will be converted into a pinned comment and placed at the top of all emitted JavaScript files, +with the exception of any emitted polyfills. Escaped newlines may be placed in the provided value for this field and +will be honored by Stencil. + +Example: +```tsx +preamble: 'Built with Stencil\nCopyright (c) SomeCompanyInc.' +``` +Will generate the following comment: +```tsx +/*! + * Built with Stencil + * Copyright (c) SomeCompanyInc. + */ +``` + +## sourceMap + +*default: `'dev'`* + +- Set to `true` to always generate source maps, +- Set to `false` to never generate source maps +- Set to `'dev'` to only generate source maps during development (`--dev`) builds. + +```tsx +sourceMap: true | false | 'dev' +``` + +Sourcemaps create a translation between Stencil components that are written in TypeScript/JSX and the resulting +JavaScript that is output by Stencil. Enabling source maps in your project allows for an improved debugging experience +for Stencil components. For example, they allow external tools (such as an Integrated Development Environment) to add +breakpoints directly in the original source code, which allows you to 'step through' your code line-by-line, to inspect +the values held in variables, to observe logic flow, and more. + +Please note: Stencil will always attempt to minify a component's source code as much as possible during compilation. +When `sourceMap` is enabled, it is possible that a slightly different minified result will be produced by Stencil when +compared to the minified result produced when `sourceMap` is not enabled. + +Developers are responsible for determining whether or not they choose to serve sourcemaps in each environment their +components are served and implementing their decision accordingly. + +## srcDir + +*default: `src`* + +The `srcDir` config specifies the directory which should contain the source typescript files for each component. The standard for Stencil apps is to use `src`, which is the default. + +```tsx +srcDir: 'src' +``` + +## taskQueue + +*default: `async`* + +Sets the task queue used by stencil's runtime. The task queue schedules DOM read and writes +across the frames to efficiently render and reduce layout thrashing. By default, the +`async` is used. It's recommended to also try each setting to decide which works +best for your use-case. In all cases, if your app has many CPU intensive tasks causing the +main thread to periodically lock-up, it's always recommended to try +[Web Workers](../guides/workers.md) for those tasks. + +* `congestionAsync`: DOM reads and writes are scheduled in the next frame to prevent layout + thrashing. When the app is heavily tasked and the queue becomes congested it will then + split the work across multiple frames to prevent blocking the main thread. However, it can + also introduce unnecessary reflows in some cases, especially during startup. `congestionAsync` + is ideal for apps running animations while also simultaneously executing intensive tasks + which may lock-up the main thread. + +* `async`: DOM read and writes are scheduled in the next frame to prevent layout thrashing. + During intensive CPU tasks it will not reschedule rendering to happen in the next frame. + `async` is ideal for most apps, and if the app has many intensive tasks causing the main + thread to lock-up, it's recommended to try [Web Workers](../guides/workers.md) + rather than the congestion async queue. + +* `immediate`: Makes writeTask() and readTask() callbacks to be executed synchronously. Tasks + are not scheduled to run in the next frame, but do note there is at least one microtask. + The `immediate` setting is ideal for apps that do not provide long-running and smooth + animations. Like the async setting, if the app has intensive tasks causing the main thread + to lock-up, it's recommended to try [Web Workers](../guides/workers.md). + +```tsx +taskQueue: 'async' +``` + +## testing + +Please see the [testing config docs](../testing/stencil-testrunner/02-config.md). + +## transformAliasedImportPaths + +*default: `true`* + +This sets whether or not Stencil should transform [path aliases]( +https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) set +in a project's `tsconfig.json` from the assigned module aliases to resolved +relative paths. This will not transform external imports (like `@stencil/core`) or +relative imports (like `'../utils'`). + +This option applies globally and will affect all code processed by Stencil, +including `.d.ts` files and spec tests. + +An example of path transformation could look something like the following. + +First, a set of `paths` aliases in `tsconfig.json`: + +```json title="tsconfig.json" +{ + "compilerOptions": { + "paths": { + "@utils": [ + "../path/to/utils" + ] + } + } +} +``` + +Then with the following input: + +```ts title="src/my-module.ts" +import { utilFunc, UtilInterface } from '@utils' + +export function util(arg: UtilInterface) { + utilFunc(arg) +} +``` + +Stencil will produce the following output: + +```js title="dist/my-module.js" +import { utilFunc } from '../path/to/utils'; +export function util(arg) { + utilFunc(arg); +} +``` + +```ts title="dist/my-module.d.ts" +import { UtilInterface } from '../path/to/utils'; +export declare function util(arg: UtilInterface): void; +``` + +## validatePrimaryPackageOutputTarget + +*default: `false`* + +When `true`, validation for common `package.json` fields will occur based on setting an output target's `isPrimaryPackageOutputTarget` flag. +For more information on package validation, please see the [output target docs](../output-targets/01-overview.md#primary-package-output-target-validation). + +## rollupConfig + +Passes custom configuration down to rollup itself. The following options can be overwritten: + +- `inputOptions`: [`context`](https://rollupjs.org/configuration-options/#context), [`external`](https://rollupjs.org/configuration-options/#external), [`moduleContext`](https://rollupjs.org/configuration-options/#modulecontext) [`treeshake`](https://rollupjs.org/configuration-options/#treeshake) +- `outputOptions`: [`globals`](https://rollupjs.org/configuration-options/#output-globals) + +*default: `{}`* + +## watchIgnoredRegex + +*default: `[]`* + +*type: `RegExp | RegExp[]`* + +A regular expression (or array of regular expressions) that can be used to omit files from triggering a rebuild in watch mode. During compile-time, each file in the Stencil +project will be tested against each regular expression to determine if changes to the file (or directory) should trigger a project rebuild. + +:::note +If you want to ignore TS files such as `.ts`/`.js` or `.tsx`/`.jsx` extensions, these files will also need to be specified in your project's tsconfig's +[`watchOptions`](https://www.typescriptlang.org/docs/handbook/configuring-watch.html#configuring-file-watching-using-a-tsconfigjson) _in addition_ to the +`watchIgnoredRegex` option. For instance, if we wanted to ignore the `my-component.tsx` file, we'd specify: + +```json title="tsconfig.json" +{ + ..., + "watchOptions": { + "excludeFiles": ["src/components/my-component/my-component.tsx"] + } +} +``` + +::: diff --git a/versioned_docs/version-v4.42/config/_category_.json b/versioned_docs/version-v4.42/config/_category_.json new file mode 100644 index 000000000..b9de453d1 --- /dev/null +++ b/versioned_docs/version-v4.42/config/_category_.json @@ -0,0 +1,4 @@ +{ + "label": "Config", + "position": 5 +} diff --git a/versioned_docs/version-v4.42/config/cli.md b/versioned_docs/version-v4.42/config/cli.md new file mode 100644 index 000000000..445c42996 --- /dev/null +++ b/versioned_docs/version-v4.42/config/cli.md @@ -0,0 +1,111 @@ +--- +title: Stencil CLI +sidebar_label: CLI +description: Stencil CLI +slug: /cli +--- + +# Command Line Interface (CLI) + +Stencil's command line interface (CLI) is how developers can build their projects, run tests, and more. +Stencil's CLI is included in the compiler, and can be invoked with the `stencil` command in a project where `@stencil/core` is installed. + +## `stencil build` + +Builds a Stencil project. The flags below are the available options for the `build` command. + +| Flag | Description | Alias | +|------|-------------|-------| +| `--ci` | Run a build using recommended settings for a Continuous Integration (CI) environment. Defaults the number of workers to 4, allows for extra time if taking screenshots via the tests and modifies the console logs. | | +| `--config` | Path to the `stencil.config.ts` file. This flag is not needed in most cases since Stencil will find the config. Additionally, a Stencil config is not required. | `-c` | +| `--debug` | Adds additional runtime code to help debug, and sets the log level for more verbose output. | | +| `--dev` | Runs a development build. | | +| `--docs` | Generate all docs based on the component types, properties, methods, events, JSDocs, CSS Custom Properties, etc. | | +| `--es5` | Creates an ES5 compatible build. By default ES5 builds are not created during development in order to improve build times. However, ES5 builds are always created during production builds. Use this flag to create ES5 builds during development. | | +| `--log` | Write logs for the `stencil build` into `stencil-build.log`. The log file is written in the same location as the config. | | +| `--prerender` | Prerender the application using the `www` output target after the build has completed. | | +| `--prod` | Runs a production build which will optimize each file, improve bundling, remove unused code, minify, etc. A production build is the default, this flag is only used to override the `--dev` flag. | | +| `--max-workers` | Max number of workers the compiler should use. Defaults to use the same number of CPUs the Operating System has available. | | +| `--next` | Opt-in to test the "next" Stencil compiler features. | | +| `--no-cache` | Disables using the cache. | | +| `--no-open` | By default the `--serve` command will open a browser window. Using the `--no-open` command will not automatically open a browser window. | | +| `--port` | Port for the [Integrated Dev Server](./dev-server.md). Defaults to `3333`. | `-p` | +| `--serve` | Starts the [Integrated Dev Server](./dev-server.md). | | +| `--stats` | Write stats about the project to `stencil-stats.json`. The stats file is written in the same location as the config. | | +| `--verbose` | Logs additional information about each step of the build. | | +| `--watch` | Watches files during development and triggers a rebuild when files are updated. | | + +## `stencil docs` + +Performs a one-time generation of documentation for your project. +For more information on documentation generation, please see the [Documentation Generation section](../documentation-generation/01-overview.md). + +:::info +This command runs with dev mode enabled, which does not run a full build. As a result, documentation that needs to be built first, like CSS styles, will not be generated. You will need to run `npx stencil build --docs` to generate documentation that requires building. +::: + +## `stencil generate` + +Alias: `stencil g` + +Starts the interactive generator for a new Stencil component. +The generator will ask you for a name for your component, and whether any stylesheets or testing files should be generated. + +If you wish to skip the interactive generator, a component tag name may be provided on the command line: +```shell +stencil generate my-new-component +``` + +All components will be generated within the `src/components` folder. +Within `src/components`, a directory will be created with the same name as the component tag name you provided containing the generated files. +For example, if you specify `page-home` as the component tag name, the files will be generated in `src/components/page-home`: +```plain +src +└── components + └── page-home + ├── page-home.css + ├── page-home.e2e.ts + ├── page-home.spec.ts + └── page-home.tsx +``` + +It is also possible to specify one or more sub-folders to generate the component in. +For example, if you specify `pages/page-home` as the component tag name, the files will be generated in `src/components/pages/page-home`: +```shell +stencil generate pages/page-home +``` +The command above will result in the following directory structure: +```plain +src +└── components + └── pages + └── page-home + ├── page-home.css + ├── page-home.e2e.ts + ├── page-home.spec.ts + └── page-home.tsx +``` + +## `stencil help` + +Aliases: `stencil --help`, `stencil -h` + +Prints various tasks that can be run and their associated flags to the terminal. + +## `stencil test` + +Tests a Stencil project. The flags below are the available options for the `test` command. + +| Flag | Description | +|------|-------------| +| `--spec` | Tests `.spec.ts` files using [Jest](https://jestjs.io/). | +| `--e2e` | Tests `.e2e.ts` files using [Puppeteer](https://developers.google.com/web/tools/puppeteer) and [Jest](https://jestjs.io/). | +| `--no-build` | Skips the build process before running end-to-end tests. When using this flag, it is assumed that your Stencil project has been built prior to running `stencil test`. Unit tests do not require this flag. | +| `--devtools` | Opens the dev tools panel in Chrome for end-to-end tests. Setting this flag will disable `--headless` | +| `--headless` | Sets the headless mode to use in Chrome for end-to-end tests. `--headless` and `--headless=true` will enable the "old" headless mode in Chrome, that was used by default prior to Chrome v112. `--headless=new` will enable the new headless mode introduced in Chrome v112. See [this article](https://developer.chrome.com/articles/new-headless/) for more information on Chrome's new headless mode. | + +## `stencil version` + +Aliases: `stencil -v`, `stencil --version` + +Prints the version of Stencil to the terminal. diff --git a/versioned_docs/version-v4.42/config/dev-server.md b/versioned_docs/version-v4.42/config/dev-server.md new file mode 100644 index 000000000..705b32724 --- /dev/null +++ b/versioned_docs/version-v4.42/config/dev-server.md @@ -0,0 +1,162 @@ +--- +title: Integrated Dev Server Config +sidebar_label: Dev Server +description: Integrated Dev Server Config +slug: /dev-server +--- + +# Integrated Dev Server + +Stencil comes with an integrated dev server in order to simplify development. By integrating the build process and the dev server, Stencil is able to drastically improve the development experience without requiring complicated build scripts and configuration. As app builds and re-builds take place, the compiler is able to communicate with the dev server, and vice versa. + +## Hot Module Replacement + +The compiler already provides a watch mode, but coupled with the dev server it's able to go one step farther by reloading only what has changed within the browser. Hot Module Replacement allows the app to keep its state within the browser, while switching out individual components with their updated logic after file saves. + +## Style Replacement + +Web components can come with their own css, can use shadow dom, and can have individual style tags. Traditionally, live-reload external css links usually does the trick, however, updating components with inline styles within shadow roots has been a challenge. With the integrated dev server, Stencil is able to dynamically update styles for all components, whether they're using shadow dom or not, without requiring a page refresh. + +## Development Errors + +When errors happen during development, such as printing an error for invalid syntax, Stencil will not only log the error and the source of the error in the console, but also overlay the app with the error so it's easier to read. + +## Open In Editor + +When a development error is shown and overlays the project within the browser, line numbers pointing to the source text are clickable, +which will open the source file directly in your IDE. + +## Dev Server Config + +### `address` + +**Optional** + +**Type: `string`** + +**Default: `0.0.0.0`** + +IP address used by the dev server. The default is `0.0.0.0`, which points to all IPv4 addresses on the local machine, such as `localhost`. + +### `basePath` + +**Optional** + +**Type: `string`** + +**Default: `/`** + +Base path to be used by the server. Defaults to the root pathname. + +### `https` + +**Optional** + +**Type: `{ key: string; cert: string; } | false`** + +**Default: `false`** + +By default the dev server runs over the http protocol. Instead you can run it over https by providing your own SSL certificate and key (see example below). + +#### Example + +```tsx +import { readFileSync } from 'fs'; +import { Config } from '@stencil/core'; + +export const config: Config = { + devServer: { + reloadStrategy: 'pageReload', + port: 4444, + https: { + cert: readFileSync('cert.pem', 'utf8'), + key: readFileSync('key.pem', 'utf8'), + }, + }, +}; +``` + +### `initialLoadUrl` + +**Optional** + +**Type: `string`** + +**Default: `/`** + +The URL the dev server should first open to. + +### `logRequests` + +**Optional** + +**Type: `boolean`** + +**Default: `false`** + +Every request to the server will be logged within the terminal. + +### `openBrowser` + +**Optional** + +**Type: `boolean`** + +**Default: `true`** + +By default, when dev server is started the local dev URL is opened in your default browser. However, to prevent this URL to be opened change this value to `false`. + +### `pingRoute` + +**Optional** + +**Type: `string | null`** + +**Default: `/ping`** + +Route to be registered on the dev server that will respond with a 200 OK response once the Stencil build has completed. The Stencil dev server will "spin up" +before the build process has completed, which can cause issues with processes that rely on the compiled and served output (like E2E testing). This route provides +a way for these processes to know when the build has finished and is ready to be accessed. + +If set to `null`, no route will be registered. + +### `port` + +**Optional** + +**Type: `number`** + +**Default: `3333`** + +Sets the server's port. + +### `strictPort` + +**Optional** + +**Type: `boolean`** + +**Default: `false`** + +When set to `true`, the dev server will fail to start if the specified `port` is already in use. When set to `false`, the dev server will try the next available port if the specified port is in use. + +### `reloadStrategy` + +**Optional** + +**Type: `'hmr' | 'pageReload' | null`** + +**Default: `hmr`** + +When files are watched and updated, by default the dev server will use `hmr` (Hot Module Replacement) to update the page without a full page refresh. +To have the page do a full refresh use `pageReload`. To disable any reloading, use `null`. + +### `root` + +**Optional** + +**Type: `string`** + +**Default: `www` output directory if exists, project root otherwise** + +The directory to serve files from. diff --git a/versioned_docs/version-v4.42/config/docs.md b/versioned_docs/version-v4.42/config/docs.md new file mode 100644 index 000000000..2ceb4458c --- /dev/null +++ b/versioned_docs/version-v4.42/config/docs.md @@ -0,0 +1,44 @@ +--- +title: Documentation Generation Config +sidebar_label: Docs Config +description: Documentation Generation Config +slug: /docs-config +--- + +# Docs Config + +The `docs` config option allows global configuration of certain behaviors related to [documentation generation output targets](../documentation-generation/01-overview.md). + +:::note +These configurations are **global** and will be applied to all output target instances including those defined in the [`outputTargets`](../output-targets/01-overview.md) +configuration, as well as those injected by CLI flags (like `--docs`). +::: + +## markdown + +The `markdown` config object allows certain customizations for markdown files generated by the [`docs-readme` output target](../documentation-generation/docs-readme.md) or the +`--docs` CLI flag. + +### targetComponent + +**Optional** + +**Default: `{ textColor: '#333', background: '#f9f' }`** + +This option allows you to change the colors used when generating the dependency graph mermaid diagrams for components. Any hex color string is a valid +value. + +```tsx title="stencil.config.ts" +import { Config } from '@stencil/core'; + +export const config: Config = { + docs: { + markdown: { + targetComponent: { + textColor: '#fff', + background: '#000', + }, + }, + }, +}; +``` diff --git a/versioned_docs/version-v4.42/config/extras.md b/versioned_docs/version-v4.42/config/extras.md new file mode 100644 index 000000000..70085fc9e --- /dev/null +++ b/versioned_docs/version-v4.42/config/extras.md @@ -0,0 +1,188 @@ +--- +title: Extras Config +sidebar_label: Extras +description: Extras Config +slug: /config-extras +--- + +# Extras + +The `extras` config contains options to enable new/experimental features in +Stencil, add & remove runtime for DOM features that require manipulations to +polyfills, etc. For example, not all DOM APIs are fully polyfilled when using +the Slot polyfill. Most of these are opt-in, since not all users require the +additional runtime. + +### appendChildSlotFix + +By default, the slot polyfill does not update `appendChild()` so that it appends new child nodes into the correct child slot like how shadow dom works. This is an opt-in polyfill for those who need it. + +### cloneNodeFix + +By default, the runtime does not polyfill `cloneNode()` when cloning a component that uses the slot polyfill. This is an opt-in polyfill for those who need it. + +### tagNameTransform + +The `tagNameTransform` option in the `extras` config enables support for customizing the tag names of your Stencil components at runtime. This is especially useful if you want to add a prefix, suffix, or otherwise modify the tag names when registering your custom elements, for example to avoid naming conflicts. + +When `tagNameTransform` is enabled in your `stencil.config.ts`: + +```typescript +extras: { + tagNameTransform: true +} +``` + +You can use the `transformTagName` function when calling `defineCustomElements` in your consuming project: + +```typescript +import { defineCustomElements } from 'my-button/loader'; + +defineCustomElements(window, { + transformTagName: (tagName: string) => `${tagName}` +} as never); +``` + +In this example, the function simply returns the original tag name, but you can customize it as needed. For example, to add a suffix: + +```typescript +defineCustomElements(window, { + transformTagName: (tagName: string) => `${tagName}-v1` +} as never); +``` + +With this configuration, a component originally named `` would be registered as ``. + +**Note:** +- The `tagNameTransform` option in your Stencil config enables this feature at build time. +- The `transformTagName` function is used at runtime when registering the components. + +### enableImportInjection + +In some cases, it can be difficult to lazily load Stencil components in a separate project that uses a bundler such as +[Vite](https://vitejs.dev/). + +Enabling this flag will allow downstream projects that consume a Stencil library and use a bundler such as Vite to lazily load the Stencil library's components. + +In order for this flag to work: + +1. The Stencil library must expose lazy loadable components, such as those created with the + [`dist` output target](../output-targets/dist.md) +2. The Stencil library must be recompiled with this flag set to `true` + +This flag works by creating dynamic import statements for every lazily loadable component in a Stencil project. +Users of this flag should note that they may see an increase in their bundle size. + +Defaults to `false`. + +### experimentalImportInjection + +:::caution +This flag has been deprecated in favor of [`enableImportInjection`](#enableimportinjection), which provides the same +functionality. `experimentalImportInjection` will be removed in a future major version of Stencil. +::: + +In some cases, it can be difficult to lazily load Stencil components in a separate project that uses a bundler such as +[Vite](https://vitejs.dev/). + +This is an experimental flag that, when set to `true`, will allow downstream projects that consume a Stencil library +and use a bundler such as Vite to lazily load the Stencil library's components. + +In order for this flag to work: + +1. The Stencil library must expose lazy loadable components, such as those created with the + [`dist` output target](../output-targets/dist.md) +2. The Stencil library must be recompiled with this flag set to `true` + +This flag works by creating dynamic import statements for every lazily loadable component in a Stencil project. +Users of this flag should note that they may see an increase in their bundle size. + +Defaults to `false`. + +### experimentalScopedSlotChanges + +This option updates runtime behavior for Stencil's support of slots in **scoped** components to match more closely with +the native Shadow DOM behaviors. + +When set to `true`, the following behaviors will be applied: + +- Stencil will hide projected nodes that do not have a destination `slot` ([#2778](https://github.com/ionic-team/stencil/issues/2877)) (since v4.10.0) +- The `textContent` getter will return the text content of all nodes located in a slot (since v4.10.0) +- The `textContent` setter will overwrite all nodes located in a slot (since v4.10.0) + +Defaults to `false`. + +:::note +These behaviors only apply to components using scoped encapsulation! +::: + +### experimentalSlotFixes + +This option enables all current and future slot-related fixes. When enabled it +will enable the following options, overriding their values if they are +specified separately: + +- [`slotChildNodesFix`](#slotchildnodesfix) +- [`scopedSlotTextContentFix`](#scopedslottextcontentfix). +- [`appendChildSlotFix`](#appendchildslotfix) +- [`cloneNodeFix`](#clonenodefix) + +Slot-related fixes to the runtime will be added over the course of Stencil v4, +with the intent of making these the default behavior in Stencil v5. When set to +`true` fixes for the following issues will be applied: + +- Elements rendered outside of slot when shadow not enabled [(#2641)](https://github.com/ionic-team/stencil/issues/2641) (since v4.2.0) +- A slot gets the attribute hidden when it shouldn't [(#4523)](https://github.com/ionic-team/stencil/issues/4523) (since v4.7.0) +- Nested slots mis-ordered when not using Shadow DOM [(#2997)](https://github.com/ionic-team/stencil/issues/2997) (since v4.7.0) +- Inconsistent behavior: slot-fb breaks styling of default slot content in component with 'shadow: false' [(#2937)](https://github.com/ionic-team/stencil/issues/2937) (since v4.7.2) +- Slot content went missing within dynamic component [(#4284)](https://github.com/ionic-team/stencil/issues/4284) (since v4.8.2) +- Slot element loses its parent reference and disappears when its parent is rendered conditionally [(#3913)](https://github.com/ionic-team/stencil/issues/3913) (since v4.8.2) +- Failed to execute 'removeChild' on 'Node' [(#3278)](https://github.com/ionic-team/stencil/issues/3278) (since v4.9.0) +- React fails to manage children in Stencil slot [(#2259)](https://github.com/ionic-team/stencil/issues/2259) (since v4.9.0) +- Slot name is not updated when it is bind to a prop [(#2982)](https://github.com/ionic-team/stencil/issues/2982) (since 4.12.1) +- Conditionally rendered slots not working [(#5335)](https://github.com/ionic-team/stencil/issues/5335) (since 4.13.0) + +:::note +New fixes enabled by this experimental flag are not subject to Stencil's +[semantic versioning policy](../reference/versioning.md). +::: + +### lifecycleDOMEvents + +Dispatches component lifecycle events. By default these events are not dispatched, but by enabling this to `true` these events can be listened for on `window`. Mainly used for testing. + +| Event Name | Description | +| ----------------------------- | ------------------------------------------------------ | +| `stencil_componentWillLoad` | Dispatched for each component's `componentWillLoad`. | +| `stencil_componentWillUpdate` | Dispatched for each component's `componentWillUpdate`. | +| `stencil_componentWillRender` | Dispatched for each component's `componentWillRender`. | +| `stencil_componentDidLoad` | Dispatched for each component's `componentDidLoad`. | +| `stencil_componentDidUpdate` | Dispatched for each component's `componentDidUpdate`. | +| `stencil_componentDidRender` | Dispatched for each component's `componentDidRender`. | + +### scopedSlotTextContentFix + +An experimental flag that when set to `true`, aligns the behavior of invoking the `textContent` getter/setter on a scoped component to act more like a component that uses the shadow DOM. Specifically, invoking `textContent` on a component will adhere to the return values described in [MDN's article on textContent](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent#description). Defaults to `false`. + +### scriptDataOpts + +:::caution +This option has been deprecated and will be removed in the next major Stencil release. +::: + +It is possible to assign data to the actual ` + + + + + +``` + +Alternatively, if you wanted to take advantage of ES Modules, you could include the components using an import statement. + +```markup + + + + + + + + +``` + +## Passing object props from a non-JSX element + +### Setting the prop manually + +```tsx +import { Prop } from '@stencil/core'; + +export class TodoList { + @Prop() myObject: object; + @Prop() myArray: Array; +} +``` + +```markup + + +``` + +### Watching props changes + +```tsx +import { Prop, State, Watch } from '@stencil/core'; + +export class TodoList { + @Prop() myObject: string; + @Prop() myArray: string; + @State() myInnerObject: object; + @State() myInnerArray: Array; + + componentWillLoad() { + this.parseMyObjectProp(this.myObject); + this.parseMyArrayProp(this.myArray); + } + + @Watch('myObject') + parseMyObjectProp(newValue: string) { + if (newValue) this.myInnerObject = JSON.parse(newValue); + } + + @Watch('myArray') + parseMyArrayProp(newValue: string) { + if (newValue) this.myInnerArray = JSON.parse(newValue); + } +} +``` + +```tsx + +``` diff --git a/versioned_docs/version-v4.42/framework-integration/react.md b/versioned_docs/version-v4.42/framework-integration/react.md new file mode 100644 index 000000000..2b9e3881e --- /dev/null +++ b/versioned_docs/version-v4.42/framework-integration/react.md @@ -0,0 +1,518 @@ +--- +title: React Integration with Stencil +sidebar_label: React +description: Learn how to wrap your components so that people can use them natively in React +slug: /react +--- + +# React Integration + +**Supports: React v17+ • TypeScript v5+ • Stencil v4.2.0+** + +Automate the creation of React component wrappers for your Stencil web components. + +This package includes an output target for code generation that allows developers to generate a React component wrapper for each Stencil component and a minimal runtime package built around [@lit/react](https://www.npmjs.com/package/@lit/react) that is required to use the generated React components in your React library or application. + +- ♻️ Automate the generation of React component wrappers for Stencil components +- 🌐 Generate React functional component wrappers with JSX bindings for custom events and properties +- ⌨️ Typings and auto-completion for React components in your IDE +- 🚀 Support for Server Side Rendering (SSR) when used with frameworks like [Next.js](https://nextjs.org/) + +To generate these framework wrappers, Stencil provides an Output Target library called [`@stencil/react-output-target`](https://www.npmjs.com/package/@stencil/react-output-target) that can be added to your `stencil.config.ts` file. This also enables Stencil components to be used within e.g. Next.js or other React based application frameworks. + +## Setup + +### Project Structure + +We recommend using a [monorepo](https://www.toptal.com/front-end/guide-to-monorepos) structure for your component library with component +wrappers. Your project workspace should contain your Stencil component library and the library for the generated React component wrappers. + +An example project set-up may look similar to: + +``` +top-most-directory/ +└── packages/ + ├── stencil-library/ + │ ├── stencil.config.js + │ └── src/components/ + └── react-library/ + └── src/ + ├── components/ + └── index.ts +``` + +This guide uses Lerna for the monorepo, but you can use other solutions such as Nx, Turborepo, etc. + +To use Lerna with this walk through, globally install Lerna: + +```bash npm2yarn +npm install --global lerna +``` + +#### Creating a Monorepo + +:::note +If you already have a monorepo, skip this section. +::: + +```bash npm2yarn +# From your top-most-directory/, initialize a workspace +lerna init + +# install dependencies +npm install + +# install typescript and node types +npm install typescript @types/node --save-dev +``` + +#### Creating a Stencil Component Library + +:::note +If you already have a Stencil component library, skip this section. +::: + +From the `packages/` directory, run the following commands to create a Stencil component library: + +```bash npm2yarn +npm init stencil components stencil-library +cd stencil-library +# Install dependencies +npm install +``` + +##### Component Imports + +If you encounter import errors in your `components.ts` file, update the Stencil library's `package.json` to include comprehensive exports: + +```json title="package.json" +"exports": { + ".": { + "import": "./dist/stencil-library/stencil-library.esm.js", + "require": "./dist/stencil-library/stencil-library.cjs.js" + }, + "./dist/*": { + "import": "./dist/*", + "types": "./dist/*" + }, + "./components/*": { + "import": "./dist/components/*.js", + "types": "./dist/components/*.d.ts" + }, + "./loader": { + "import": "./loader/index.js", + "require": "./loader/index.cjs", + "types": "./loader/index.d.ts" + } +} +``` + +#### Creating a React Component Library + +:::note +If you already have a React component library, skip this section. +::: + +The first time you want to create the component wrappers, you will need to have a React library package to write to. + +Run the following commands from the root directory of your monorepo to create a React component library: + +```bash npm2yarn +# Create a project +lerna create react-library # fill out the prompts accordingly +cd packages/react-library + +# Install core dependencies +npm install react react-dom typescript @types/react --save-dev + +# Install output target runtime dependency +npm install @stencil/react-output-target --save +``` + +Lerna does not ship with a TypeScript configuration. At the root of the workspace, create a `tsconfig.json`: + +```json title="tsconfig.json" +{ + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "noImplicitAny": false, + "removeComments": true, + "noLib": false, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "target": "es6", + "sourceMap": true, + "lib": ["es6"] + }, + "exclude": ["node_modules", "**/*.spec.ts", "**/__tests__/**"] +} +``` + +In your `react-library` project, create a project specific `tsconfig.json` that will extend the root config: + +```json title="packages/react-library/tsconfig.json" +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "target": "ES2020", + "useDefineForClassFields": true, + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "module": "ESNext", + "moduleResolution": "bundler", + "skipLibCheck": true, + "jsx": "react", + "allowSyntheticDefaultImports": true, + "declarationDir": "./dist/types" + }, + "include": ["lib"], + "exclude": ["node_modules"] +} +``` + +Update the generated `package.json` in your `react-library`, adding the following options to the existing config: + +```diff title="packages/react-library/package.json" +{ +- "main": "lib/react-library.js", ++ "main": "dist/index.js", ++ "module": "dist/index.js", ++ "types": "dist/types/index.d.ts", + "scripts": { +- "test": "node ./__tests__/react-library.test.js" ++ "test": "node ./__tests__/react-library.test.js", ++ "build": "npm run tsc", ++ "tsc": "tsc -p . --outDir ./dist" +- } ++ }, + "files": [ +- "lib" ++ "dist" + ], ++ "publishConfig": { ++ "access": "public" ++ }, ++ "dependencies": { ++ "stencil-library": "*" ++ } +} +``` + +:::note +The `stencil-library` dependency is how Lerna knows to resolve the internal Stencil library dependency. See Lerna's documentation on +[package dependency management](https://lerna.js.org/docs/getting-started#package-dependency-management) for more information. +::: + +### Adding the React Output Target + +#### Step 1 - Stencil Component Library + +Install the `@stencil/react-output-target` dependency to your Stencil component library package. + +```bash npm2yarn +# Install dependency +npm install @stencil/react-output-target --save-dev +``` + +In your project's `stencil.config.ts`, add the `reactOutputTarget` configuration to the `outputTargets` array: + +```ts title="stencil.config.ts" +import { reactOutputTarget } from '@stencil/react-output-target'; + +export const config: Config = { + namespace: 'stencil-library', + outputTargets: [ + reactOutputTarget({ + // Relative path to where the React components will be generated + outDir: '../react-library/lib/components/stencil-generated/', + }), + // dist-custom-elements output target is required for the React output target + { type: 'dist-custom-elements' }, + ], +}; +``` + +:::tip + +The `outDir` is the relative path to the file that will be generated with all of the React component wrappers. You will replace the +file path to match your project's structure and respective names. + +::: + +See the [API section below](#api) for details on each of the output target's options. + +:::note +In order to compile Stencil components optimized for server side rendering in e.g. Next.js applications that use [AppRouter](https://nextjs.org/docs/app), make sure to provide the [`hydrateModule`](#hydratemodule) property to the output target configuration. +::: + +You can now build your Stencil component library to generate the component wrappers. + +```bash npm2yarn +# Build the library and wrappers +npm run build +``` + +If the build is successful, you’ll see the new generated file in your React component library at the location specified by the `outDir` argument. + +#### Step 2 - React Component Library + +Install the `@stencil/react-output-target` dependency to your React component library package. This step is required to add the runtime dependencies required to use the generated React components. + +```bash npm2yarn +# Install dependency +npm install @stencil/react-output-target --save +``` + +Verify or update your `tsconfig.json` file to include the following settings: + +```json +{ + "compilerOptions": { + "module": "esnext", + "moduleResolution": "bundler" + } +} +``` + +:::info + +`moduleResolution": "bundler"` is required to resolve the secondary entry points in the `@stencil/react-output-target` runtime package. You can learn more about this setting in the [TypeScript documentation](https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution). + +::: + +Verify or install TypeScript v5.0 or later in your project: + +```bash npm2yarn +# Install dependency +npm install typescript@5 --save-dev +``` + +No additional configuration is needed in the React component library. The generated component wrappers will reference the runtime dependencies directly. + +### Add the Components to your React Component Library's Entry File + +In order to make the generated files available within your React component library and its consumers, you’ll need to export everything from within your entry file. First, rename `react-library.js` to `index.ts`. Then, modify the contents to match the following: + +```tsx title="packages/react-library/src/index.ts" +export * from './components/stencil-generated/components'; +``` + +### Link Your Packages (Optional) + +:::note +If you are using a monorepo tool (Lerna, Nx, etc.), skip this section. +::: + +Before you can successfully build a local version of your React component library, you will need to link the Stencil package to the React package. + +From your Stencil project's directory, run the following command: + +```bash npm2yarn +# Link the working directory +npm link +``` + +From your React component library's directory, run the following command: + +```bash npm2yarn +# Link the package name +npm link name-of-your-stencil-package +``` + +The name of your Stencil package should match the `name` property from the Stencil component library's `package.json`. + +Your component libraries are now linked together. You can make changes in the Stencil component library and run `npm run build` to propagate the +changes to the React component library. + +:::tip +As an alternative to `npm link` , you can also run `npm install` with a relative path to your Stencil component library. This strategy, however, will +modify your `package.json` so it is important to make sure you do not commit those changes. +::: + +## Consumer Usage + +### Creating a Consumer React App + +:::note +If you already have a React app, skip this section. +::: + +From the `packages/` directory, run the following commands to create a starter React app: + + + + +```bash +# Create the React app +npm create vite@latest my-app -- --template react-ts +# of if using yarn +yarn create vite my-app --template react-ts + +cd ./my-app + +# install dependencies +npm install +# or if using yarn +yarn install +``` + +You'll also need to link your React component library as a dependency. This step makes it so your React app will be able to correctly resolve imports from your React library. This +is easily done by modifying your React app's `package.json` to include the following: + +```json +"dependencies": { + "react-library": "*" +} +``` + +### Consuming the React Wrapper Components + +This section covers how developers consuming your React component wrappers will use your package and component wrappers. + +Before you can consume your React component wrappers, you'll need to build your React component library. From `packages/react-library` run: + +```bash npm2yarn +npm run build +``` + +To make use of your React component library in your React application, import your components from your React component library in the file where you want to use them. + +```tsx title="App.tsx" +import './App.css'; +import { MyComponent } from 'react-library'; + +function App() { + return ( +
    + +
    + ); +} + +export default App; +``` + +## API + +### outDir + +**Required** + +**Type: `string`** + +The directory where the React components will be generated. Accepts a relative path from the Stencil project's root directory. + +### excludeComponents + +**Optional** + +**Type: `string[]`** + +An array of component tag names to exclude from the React output target. Useful if you want to prevent certain web components from being in the React library. + +### stencilPackageName + +**Optional** + +**Type: `string`** + +The name of the package that exports the Stencil components. Defaults to the `package.json` detected by the Stencil compiler. + +### customElementsDir + +**Optional** + +**Type: `string`** + +The directory where the custom elements are saved. + +This value is automatically detected from the Stencil configuration file for the dist-custom-elements output target. If you are working in an environment that uses absolute paths, consider setting this value manually. + +### hydrateModule + +**Optional** + +**Type: `string`** + +Enable React server side rendering (short SSR) for e.g. [Next.js](https://nextjs.org/) applications by providing an import path to the [hydrate module](../guides/hydrate-app.md) of your Stencil project that is generated through the `dist-hydrate-script` output target, e.g.: + +```ts title="stencil.config.ts" +import type { Config } from '@stencil/core'; + +/** + * excerpt from the Stencil example project: + * https://github.com/ionic-team/stencil-ds-output-targets/tree/cb/nextjs/packages/example-project + */ +export const config: Config = { + namespace: 'component-library', + outputTargets: [ + reactOutputTarget({ + outDir: '../next-app/src/app', + hydrateModule: 'component-library/hydrate' + }), + { + type: 'dist-hydrate-script', + dir: './hydrate', + }, + // ... + ], +}; +``` + +### excludeServerSideRenderingFor + +**Optional** + +**Type: `string[]`** + +Allows users to exclude a list of components from server side rendering by Next.js or other React +frameworks. This may be useful if you would like to generally ignore some components from being +rendered on the server or if you like roll out SSR support for your design system one component at +a time. + +### esModules + +**Optional** + +**Default: `true`** + +**Type: `boolean`** + +If `true`, the output target will generate a separate ES module for each React component wrapper. Defaults to `false`. + +### serializeShadowRoot + +**Optional** + +**Default: `'declarative-shadow-dom'`** + +**Type: `'declarative-shadow-dom' | 'scoped' | { 'declarative-shadow-dom'?: string[]; scoped?: string[]; } | boolean`** + +Configure how Stencil serializes the components shadow root. +- If set to `declarative-shadow-dom` the component will be rendered within a Declarative Shadow DOM. +- If set to `scoped` Stencil will render the contents of the shadow root as a `scoped: true` component + and the shadow DOM will be created during client-side hydration. +- Alternatively you can mix and match the two by providing an object with `declarative-shadow-dom` and `scoped` keys, +the value arrays containing the tag names of the components that should be rendered in that mode. + +**Examples:** + +- `{ 'declarative-shadow-dom': ['my-component-1', 'another-component'], default: 'scoped' }` +Render all components as `scoped` apart from `my-component-1` and `another-component` +- `{ 'scoped': ['an-option-component'], default: 'declarative-shadow-dom' }` +Render all components within `declarative-shadow-dom` apart from `an-option-component` +- `'scoped'` Render all components as `scoped` +- `false` disables shadow root serialization + +*NOTE* `true` has been deprecated in favor of `declarative-shadow-dom` and `scoped` + +## FAQ's + +### What is the best format to write event names? + +Event names shouldn’t include special characters when initially written in Stencil. Try to lean on using camelCased event names for interoperability between frameworks. + +### Can I use `dist` output target with the React output target? + +No, the React output target requires the `dist-custom-elements` output target to be present in the Stencil project's configuration. The `dist-custom-elements` output target generates a separate entry for each component which best aligns with the expectations of React developers. diff --git a/versioned_docs/version-v4.42/framework-integration/vue.md b/versioned_docs/version-v4.42/framework-integration/vue.md new file mode 100644 index 000000000..ed5686ac0 --- /dev/null +++ b/versioned_docs/version-v4.42/framework-integration/vue.md @@ -0,0 +1,679 @@ +--- +title: VueJS Integration with Stencil +sidebar_label: Vue +description: Learn how to wrap your components so that people can use them natively in Vue +slug: /vue +--- + +# Vue Integration + +**Supports: Vue 3 • TypeScript 4.0+ • Stencil v2.9.0+** + +Stencil can generate Vue component wrappers for your web components. This allows your Stencil components to be used within a Vue 3 application. The benefits of using Stencil's component wrappers over the standard web components include: + +- Type checking with your components. +- Integration with the router link and Vue router. +- Optionally, form control web components can be used with `v-model`. + +## Setup + +### Project Structure + +We recommend using a [monorepo](https://www.toptal.com/front-end/guide-to-monorepos) structure for your component library with component wrappers. Your project workspace should contain your Stencil component library and the library for the generate Vue component wrappers. + +An example project set-up may look similar to: + +``` +top-most-directory/ +└── packages/ + ├── vue-library/ + │ └── lib/ + │ ├── plugin.ts + │ └── index.ts + └── stencil-library/ + ├── stencil.config.js + └── src/components +``` + +This guide uses Lerna for the monorepo, but you can use other solutions such as Nx, Turborepo, etc. + +To use Lerna with this walk through, globally install Lerna: + +```bash npm2yarn +npm install --global lerna +``` + +#### Creating a Monorepo + +:::note +If you already have a monorepo, skip this section. +::: + +```bash npm2yarn +# From your top-most-directory/, initialize a workspace +lerna init + +# install dependencies +npm install + +# install typescript and node types +npm install typescript @types/node --save-dev +``` + +#### Creating a Stencil Component Library + +:::note +If you already have a Stencil component library, skip this section. +::: + +```bash npm2yarn +cd packages/ +npm init stencil components stencil-library +cd stencil-library +# Install dependencies +npm install +``` + +#### Creating a Vue Component Library + +:::note +If you already have a Vue component library, skip this section. +::: + +The first time you want to create the component wrappers, you will need to have a Vue library package to write to. + +Using Lerna and Vue's CLI, generate a workspace and a library for your Vue component wrappers: + +```bash npm2yarn +# From your top-most-directory/ +lerna create vue-library +# Follow the prompts and confirm +cd packages/vue-library +# Install Vue dependency +npm install vue@3 --save-dev +``` + +Lerna does not ship with a TypeScript configuration. At the root of the workspace, create a `tsconfig.json`: + +```json +{ + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "noImplicitAny": false, + "removeComments": true, + "noLib": false, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "target": "es6", + "sourceMap": true, + "lib": ["es6"] + }, + "exclude": ["node_modules", "**/*.spec.ts", "**/__tests__/**"] +} +``` + +In your `vue-library` project, create a project specific `tsconfig.json` that will extend the root config: + +```json +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "lib": ["dom", "es2020"], + "module": "es2015", + "moduleResolution": "node", + "target": "es2017", + "skipLibCheck": true + }, + "include": ["lib"], + "exclude": ["node_modules"] +} +``` + +Update the generated `package.json` in your `vue-library`, adding the following options to the existing config: + +```diff +{ +- "main": "lib/vue-library.js", ++ "main": "dist/index.js", ++ "types": "dist/index.d.ts", + "files": [ +- 'lib' ++ 'dist' + ], + "scripts": { +- "test": "echo \"Error: run tests from root\" && exit 1" ++ "test": "echo \"Error: run tests from root\" && exit 1", ++ "build": "npm run tsc", ++ "tsc": "tsc -p . --outDir ./dist" +- } ++ }, ++ "publishConfig": { ++ "access": "public" ++ }, ++ "dependencies": { ++ "stencil-library": "*" ++ } +} +``` + +:::note +The `stencil-library` dependency is how Lerna knows to resolve the internal Stencil library dependency. See Lerna's documentation on +[package dependency management](https://lerna.js.org/docs/getting-started#package-dependency-management) for more information. +::: + +### Adding the Vue Output Target + +Install the `@stencil/vue-output-target` dependency to your Stencil component library package. + +```bash npm2yarn +# Install dependency (from `packages/stencil-library`) +npm install @stencil/vue-output-target --save-dev +``` + +In your project's `stencil.config.ts`, add the `vueOutputTarget` configuration to the `outputTargets` array: + +```ts +import { vueOutputTarget } from '@stencil/vue-output-target'; + +export const config: Config = { + namespace: 'stencil-library', + outputTargets: [ + // By default, the generated proxy components will + // leverage the output from the `dist` target, so we + // need to explicitly define that output alongside the + // Vue target + { + type: 'dist', + esmLoaderPath: '../loader', + }, + vueOutputTarget({ + componentCorePackage: 'stencil-library', + proxiesFile: '../vue-library/lib/components.ts', + }), + ], +}; +``` + +:::tip +The `proxiesFile` is the relative path to the file that will be generated with all the Vue component wrappers. You will replace the file path to match +your project's structure and respective names. You can generate any file name instead of `components.ts`. + +The `componentCorePackage` should match the `name` field in your Stencil project's `package.json`. +::: + +You can now build your Stencil component library to generate the component wrappers. + +```bash npm2yarn +# Build the library and wrappers (from `packages/stencil-library`) +npm run build +``` + +If the build is successful, you will now have contents in the file specified in `proxiesFile`. + +### Registering Custom Elements + +To register your web components for the lazy-loaded (hydrated) bundle, you will need to create a new file for the Vue plugin: + +```ts +// packages/vue-library/lib/plugin.ts + +import { Plugin } from 'vue'; +import { applyPolyfills, defineCustomElements } from 'stencil-library/loader'; + +export const ComponentLibrary: Plugin = { + async install() { + applyPolyfills().then(() => { + defineCustomElements(); + }); + }, +}; +``` + +You can now finally export the generated component wrappers and the Vue plugin for your component library to make them available to implementers. Export +the `plugin.ts` file created in the previous step, as well as the file `proxiesFile` generated by the Vue Output Target: + +```ts +// packages/vue-library/lib/index.ts +export * from './components'; +export * from './plugin'; +``` + +### Link Your Packages (Optional) + +:::note +If you are using a monorepo tool (Lerna, Nx, etc.), skip this section. +::: + +Before you can successfully build a local version of your Vue component library, you will need to link the Stencil package to the Vue package. + +From your Stencil project's directory, run the following command: + +```bash npm2yarn +# Link the working directory +npm link +``` + +From your Vue component library's directory, run the following command: + +```bash npm2yarn +# Link the package name +npm link name-of-your-stencil-package +``` + +The name of your Stencil package should match the `name` property from the Stencil component library's `package.json`. + +Your component libraries are now linked together. You can make changes in the Stencil component library and run `npm run build` to propagate the +changes to the Vue component library. + +:::note +As an alternative to `npm link`, you can also run `npm install` with a relative path to your Stencil component library. This strategy, +however, will modify your `package.json` so it is important to make sure you do not commit those changes. +::: + +## Consumer Usage + +### Creating a Consumer Vue App + +From the `packages/` directory, run the following command to generate a Vue app: + +```bash npm2yarn +npm init vue@3 my-app +``` + +Follow the prompts and choose the options best for your project. + +You'll also need to link your Vue component library as a dependency. This step makes it so your Vue app will be able to correctly +resolve imports from your Vue library. This is easily done by modifying your Vue app's `package.json` to include the following: + +```json +"dependencies": { + "vue-library": "*" +} +``` + +For more information, see the Lerna documentation on [package dependency management](https://lerna.js.org/docs/getting-started#package-dependency-management). + +Lastly, you'll want to update the generated `vite.config.ts`: + +```diff +export default defineConfig({ +- plugins: [vue(), vueJsx()], ++ plugins: [ ++ vue({ ++ template: { ++ compilerOptions: { ++ // treat all tags with a dash as custom elements ++ isCustomElement: (tag) => tag.includes('-'), ++ }, ++ }, ++ }), ++ vueJsx(), ++ ], + resolve: { + alias: { + '@': fileURLToPath(new URL('./src', import.meta.url)) + } + } +}) +``` + +This will prevent Vue from logging a warning about failing to resolve components (e.g. "Failed to resolve component: my-component"). + +### Consuming the Vue Wrapper Components + +This section covers how developers consuming your Vue component wrappers will use your package and component wrappers. + +Before you can use your Vue proxy components, you'll need to build your Vue component library. From `packages/vue-library` simply run: + +```bash npm2yarn +npm run build +``` + +In your `main.js` file, import your component library plugin and use it: + +```js +// src/main.js +import { ComponentLibrary } from 'vue-library'; + +createApp(App).use(ComponentLibrary).mount('#app'); +``` + +In your page or component, you can now import and use your component wrappers: + +```html + +``` + +## API + +### componentCorePackage + +**Optional** + +**Default: The `components.d.ts` file in the Stencil project's `package.json` types field** + +**Type: `string`** + +The name of the Stencil package where components are available for consumers (i.e. the value of the `name` property in your Stencil component library's `package.json`). +This is used during compilation to write the correct imports for components. + +For a starter Stencil project generated by running: + +```bash npm2yarn +npm init stencil component my-component-lib +``` + +The `componentCorePackage` would be set to: + +```ts +// stencil.config.ts + +export const config: Config = { + ..., + outputTargets: [ + vueOutputTarget({ + componentCorePackage: 'my-component-lib', + // ... additional config options + }) + ] +} +``` + +Which would result in an import path like: + +```js +import { defineCustomElement as defineMyComponent } from 'my-component-lib/components/my-component.js'; +``` + +:::note +Although this field is optional, it is _highly_ recommended that it always be defined to avoid potential issues with paths not being generated correctly +when combining other API arguments. +::: + +### componentModels + +**Optional** + +**Default: `[]`** + +**Type: `ComponentModelConfig[]`** + +This option is used to define which components should be integrated with `v-model`. It allows you to set what the target prop is (i.e. `value`), +which event will cause the target prop to change, and more. + +```tsx +const componentModels: ComponentModelConfig[] = [ + { + elements: ['my-input', 'my-textarea'], + event: 'v-on-change', + targetAttr: 'value', + }, +]; + +export const config: Config = { + namespace: 'stencil-library', + outputTargets: [ + vueOutputTarget({ + componentCorePackage: 'component-library', + proxiesFile: '{path to your proxy file}', + componentModels: componentModels, + }), + ], +}; +``` + +By default, v-model value is taken from event's `detail.${targetAttr}` property. +This behavior can be overridden by setting `eventAttr` property on the `ComponentModelConfig`: +```tsx +eventAttr: 'detail.nested.value' +``` +In this example, if event emits a custom structure like `{ detail: { nested: { value: 'some value' } } }`, the `v-model` will be set to `'some value'`. + +### customElementsDir + +**Optional** + +**Default: 'dist/components'** + +**Type: `string`** + +If [includeImportCustomElements](#includeimportcustomelements) is `true`, this option can be used to specify the directory where the generated +custom elements live. This value only needs to be set if the `dir` field on the `dist-custom-elements` output target was set to something other than +the default directory. + +### excludeComponents + +**Optional** + +**Default: `[]`** + +**Type: `string[]`** + +This lets you specify component tag names for which you don't want to generate Vue wrapper components. This is useful if you need to write framework-specific versions of components. For instance, in Ionic Framework, this is used for routing components - like tabs - so that +Ionic Framework can integrate better with Vue's Router. + +### includeDefineCustomElements + +**Optional** + +**Default: `true`** + +**Type: `boolean`** + +If `true`, all Web Components will automatically be registered with the Custom Elements Registry. This can only be used when lazy loading Web Components and will not work when `includeImportCustomElements` is `true`. + +### includeImportCustomElements + +**Optional** + +**Default: `undefined`** + +**Type: `boolean`** + +If `true`, the output target will import the custom element instance and register it with the Custom Elements Registry when the component is imported inside of a user's app. This can only be used with the [Custom Elements Bundle](../output-targets/custom-elements.md) and will not work with lazy loaded components. + +:::note +The configuration for the [Custom Elements](../output-targets/custom-elements.md) output target must set the +[export behavior](../output-targets/custom-elements.md#customelementsexportbehavior) to `single-export-module` for the wrappers to generate correctly. +::: + +### includePolyfills + +**Optional** + +**Default: `true`** + +**Type: `boolean`** + +If `true`, polyfills will automatically be imported and the `applyPolyfills` function will be called in your proxies file. This can only be used when lazy loading Web Components and will not work when `includeImportCustomElements` is enabled. + +### loaderDir + +**Optional** + +**Default: `/dist/loader`** + +**Type: `string`** + +The path to where the `defineCustomElements` helper method exists within the built project. This option is only used when `includeDefineCustomElements` is enabled. + +### proxiesFile + +**Required** + +**Type: `string`** + +This parameter allows you to name the file that contains all the component wrapper definitions produced during the compilation process. This is the first file you should import in your Vue project. + +## FAQ + +### Do I have to use the `dist` output target? + +No! By default, this output target will look to use the `dist` output, but the output from `dist-custom-elements` can be used alternatively. + +To do so, simply set the `includeImportCustomElements` option in the output target's config and ensure the +[custom elements output target](../output-targets/custom-elements.md) is added to the Stencil config's output target array: + +```ts +// stencil.config.ts + +export const config: Config = { + ..., + outputTargets: [ + // Needs to be included + { + type: 'dist-custom-elements' + }, + vueOutputTarget({ + componentCorePackage: 'component-library', + proxiesFile: '{path to your proxy file}', + // This is what tells the target to use the custom elements output + includeImportCustomElements: true + }) + ] +} +``` + +Now, all generated imports will point to the default directory for the custom elements output. If you specified a different directory +using the `dir` property for `dist-custom-elements`, you need to also specify that directory for the Vue output target. See +[the API section](#customelementsdir) for more information. + +In addition, all the Web Components will be automatically defined as the generated component modules are bootstrapped. + +### TypeError: Cannot read properties of undefined (reading 'isProxied') + +If you encounter this error when running the Vue application consuming your proxy components, you can set the [`enableImportInjection`](../config/extras.md#enableimportinjection) +flag on the Stencil config's `extras` object. Once set, this will require you to rebuild the Stencil component library and the Vue component library. + +### Vue warns "Failed to resolve component: my-component" + +#### Lazy loaded bundle + +If you are using Vue CLI, update your `vue.config.js` to match your custom element selector as a custom element: + +```js +const { defineConfig } = require('@vue/cli-service'); +module.exports = defineConfig({ + transpileDependencies: true, + chainWebpack: (config) => { + config.module + .rule('vue') + .use('vue-loader') + .tap((options) => { + options.compilerOptions = { + ...options.compilerOptions, + // The stencil-library components start with "my-" + isCustomElement: (tag) => tag.startsWith('my-'), + }; + return options; + }); + }, +}); +``` + +#### Custom elements bundle + +If you see this warning, then it is likely you did not import your component from your Vue library: `vue-library`. By default, all Vue components are locally registered, meaning you need to import them each time you want to use them. + +Without importing the component, you will only get the underlying Web Component, and Vue-specific features such as `v-model` will not work. + +To resolve this issue, you need to import the component from `vue-library` (your package name) and provide it to your Vue component: + +```html + + + +``` + +### Vue warns: "slot attributes are deprecated vue/no-deprecated-slot-attribute" + +The slots that are used in Stencil are [Web Component](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_templates_and_slots) slots, which are different than the slots used in Vue 2. Unfortunately, the APIs for both are very similar, and your linter is likely getting the two confused. + +You will need to update your lint rules in `.eslintrc.js` to ignore this warning: + +```js +module.exports = { + rules: { + 'vue/no-deprecated-slot-attribute': 'off', + }, +}; +``` + +If you are using VSCode and have the Vetur plugin installed, you are likely getting this warning because of Vetur, not ESLint. By default, Vetur loads the default Vue 3 linting rules and ignores any custom ESLint rules. + +To resolve this issue, you will need to turn off Vetur's template validation with `vetur.validation.template: false`. See the [Vetur Linting Guide](https://vuejs.github.io/vetur/guide/linting-error.html#linting) for more information. + +### Method on component is not a function + +In order to access a method on a Stencil component in Vue, you will need to access the underlying Web Component instance first: + +```ts +// ✅ This is correct +myComponentRef.value.$el.someMethod(); + +// ❌ This is incorrect and will result in an error. +myComponentRef.value.someMethod(); +``` + +### Output commonjs bundle for Node environments + +First, install `rollup` and `rimraf` as dev dependencies: + +```bash npm2yarn +npm i -D rollup rimraf +``` + +Next, create a `rollup.config.js` in `/packages/vue-library/`: + +```js +const external = ['vue', 'vue-router']; + +export default { + input: 'dist-transpiled/index.js', + output: [ + { + dir: 'dist/', + entryFileNames: '[name].esm.js', + chunkFileNames: '[name]-[hash].esm.js', + format: 'es', + sourcemap: true, + }, + { + dir: 'dist/', + format: 'commonjs', + preferConst: true, + sourcemap: true, + }, + ], + external: (id) => external.includes(id) || id.startsWith('stencil-library'), +}; +``` + +:::info +Update the `external` list for any external dependencies. Update the `stencil-library` to match your Stencil library's package name. +::: + +Next, update your `package.json` to include the scripts for rollup: + +```json +{ + "scripts": { + "build": "npm run clean && npm run tsc && npm run bundle", + "bundle": "rollup --config rollup.config.js", + "clean": "rimraf dist dist-transpiled" + } +} +``` diff --git a/versioned_docs/version-v4.42/guides/_category_.json b/versioned_docs/version-v4.42/guides/_category_.json new file mode 100644 index 000000000..afdb49ff4 --- /dev/null +++ b/versioned_docs/version-v4.42/guides/_category_.json @@ -0,0 +1,4 @@ +{ + "label": "Guides", + "position": 8 +} diff --git a/versioned_docs/version-v4.42/guides/assets.md b/versioned_docs/version-v4.42/guides/assets.md new file mode 100644 index 000000000..f6dda9fce --- /dev/null +++ b/versioned_docs/version-v4.42/guides/assets.md @@ -0,0 +1,284 @@ +--- +title: Assets +sidebar_label: Assets +description: Learn how to reference assets in your components +slug: /assets +--- + +# Assets + +Stencil components may need one or more static files as a part of their design. +These types of files are referred to as 'assets', and include images, fonts, etc. + +In this guide, we describe different strategies for resolving assets on the filesystem. + +:::note +CSS files are handled differently than assets; for more on using CSS, please see the [styling documentation](../components/styling.md). +::: + +## Asset Base Path + +The **asset base path** is the directory that Stencil will use to resolve assets. +When a component uses an asset, the asset's location is resolved relative to the asset base path. + +The asset base path is automatically set for the following output targets: +- [dist](../output-targets/dist.md) +- [hydrate](./hydrate-app.md) +- [www](../output-targets/www.md) + +For all other output targets, assets must be [moved](#manually-moving-assets) and the asset base path must be [manually set](#setassetpath). + +For each instance of the Stencil runtime that is loaded, there is a single asset base path. +Oftentimes, this means there is only one asset base path per application using Stencil. + +## Resolution Overview + +The process of resolving an asset involves asking Stencil to build a path to the asset on the filesystem. + +When an asset's path is built, the resolution is always done in a project's compiled output, not the directory containing the original source code. + +The example below uses the output of the [`www` output target](../output-targets/www.md) to demonstrate how assets are resolved. +Although the example uses the output of `www` builds, the general principle of how an asset is found holds for all output targets. + +When using the `www` output target, a `build/` directory is automatically created and set as the asset base path. +An example `build/` directory and the assets it contains can be found below. + +``` +www/ +├── build/ +│ ├── assets/ +│ │ ├── logo.png +│ │ └── scenery/ +│ │ ├── beach.png +│ │ └── sunset.png +│ └── other-assets/ +│ └── font.tiff +└── ... +``` + +To resolve the path to an asset, Stencil's [`getAssetPath()` API](#getassetpath) may be used. +When using `getAssetPath`, the assets in the directory structure above are resolved relative to `build/`. + +The code sample below demonstrates the return value of `getAssetPath` for different `path` arguments. +The return value is a path that Stencil has built to retrieve the asset on the filesystem. +```ts +import { getAssetPath } from '@stencil/core'; + +// with an asset base path of "/build/": + +// '/build/assets/logo.png' +getAssetPath('assets/logo.png'); +// '/build/assets/scenery/beach.png' +getAssetPath('assets/scenery/beach.png'); +// '/build/other-assets/font.tiff' +getAssetPath('other-assets/font.tiff'); +``` + +## Making Assets Available + +In order to be able to find assets at runtime, they need to be found on the filesystem from the output of a Stencil build. +In other words, we need to ensure they exist in the distribution directory. +This section describes how to make assets available under the asset base path. + +### assetsDirs + +The `@Component` decorator can be [configured with the `assetsDirs` option](../components/component.md#component-options). +`assetsDirs` takes an array of strings, where each entry is a relative path from the component to a directory containing the assets the component requires. + +When using the `dist` or `www` output targets, setting `assetsDirs` instructs Stencil to copy that folder into the distribution folder. +When using other output targets, Stencil will not copy assets into the distribution folder. + +Below is an example project's directory structure containing an example component and an assets directory. + +``` +src/ +└── components/ + ├── assets/ + │ ├── beach.jpg + │ └── sunset.jpg + └── my-component.tsx +``` + +Below, the `my-component` component will correctly load the assets based on it's `image` prop. + +```tsx +// file: my-component.tsx +// 1. getAssetPath is imported from '@stencil/core' +import { Component, Prop, getAssetPath, h } from '@stencil/core'; + +@Component({ + tag: 'my-component', + // 2. assetsDirs lists the 'assets' directory as a relative + // (sibling) directory + assetsDirs: ['assets'] +}) +export class MyComponent { + + @Prop() image = "sunset.jpg"; + + render() { + // 3. the asset path is retrieved relative to the asset + // base path to use in the tag + const imageSrc = getAssetPath(`./assets/${this.image}`); + return + } +} +``` + +In the example above, the following allows `my-component` to display the provided asset: +1. [`getAssetPath()`](#getassetpath) is imported from `@stencil/core` +2. The `my-component`'s component decorator has the `assetsDirs` property, and lists the sibling directory, `assets`. This will copy `assets` over to the distribution directory. +3. `getAssetPath` is used to retrieve the path to the image to be used in the `` tag + +### Manually Moving Assets + +For the [dist-custom-elements](../output-targets/custom-elements.md) output target, options like `assetsDirs` do not copy assets to the distribution directory. + +It's recommended that a bundler (such as rollup) or a Stencil `copy` task is used to ensure the static assets are copied to the distribution directory. + +#### Stencil Copy Task + +[Stencil `copy` task](../output-targets/copy-tasks.md)s can be used to define files and folders to be copied over to the distribution directory. + +The example below shows how a copy task can be used to find all '.jpg' and '.png' files under a project's `src` directory and copy them to `dist/components/assets` at build time. + +```ts +import { Config } from '@stencil/core'; + +export const config: Config = { + namespace: 'your-component-library', + outputTargets: [ + { + type: 'dist-custom-elements', + copy: [ + { + src: '**/*.{jpg,png}', + dest: 'dist/components/assets', + warn: true, + } + ] + }, + ], + // ... +}; +``` +#### Rollup Configuration + +[Rollup Plugins](../config/plugins.md#rollup-plugins)'s can be used to define files and folders to be copied over to the distribution directory. + +The example below shows how a the `rollup-plugin-copy` NPM module can be used to find all '.jpg' and '.png' files under a project's `src` directory and copy them to `dist/components/assets` at build time. + +```javascript +import { Config } from '@stencil/core'; +import copy from 'rollup-plugin-copy'; + +export const config: Config = { + namespace: 'copy', + outputTargets: [ + { + type: 'dist-custom-elements', + }, + ], + rollupPlugins: { + after: [ + copy({ + targets: [ + { + src: 'src/**/*.{jpg,png}', + dest: 'dist/components/assets', + }, + ], + }), + ] + } +}; +``` + +## API Reference + +### getAssetPath + +`getAssetPath()` is an API provided by Stencil to build the path to an asset, relative to the asset base path. + +```ts +/** + * Builds a URL to an asset. This is achieved by combining the + * provided `path` argument with the base asset path. + * @param path the path of the asset to build a URL to + * @returns the built URL + */ +declare function getAssetPath(path: string): string; +``` + +The code sample below demonstrates the return value of `getAssetPath` for different `path` arguments, when an asset base path of `/build/` has been set. +```ts +import { getAssetPath } from '@stencil/core'; + +// with an asset base path of "/build/": +// "/build/" +getAssetPath(''); +// "/build/my-image.png" +getAssetPath('my-image.png'); +// "/build/assets/my-image.png" +getAssetPath('assets/my-image.png'); +// "/build/assets/my-image.png" +getAssetPath('./assets/my-image.png'); +// "/assets/my-image.png" +getAssetPath('../assets/my-image.png'); +// "/assets/my-image.png" +getAssetPath('/assets/my-image.png'); +``` + +### setAssetPath + +`setAssetPath` is an API provided by Stencil's runtime to manually set the asset base path where assets can be found. If you are using `getAssetPath` to compose the path for your component assets, `setAssetPath` allows you or the consumer of the component to change that path. + +```ts +/** + * Set the base asset path for resolving components + * @param path the base asset path + * @returns the new base asset path + */ +export declare function setAssetPath(path: string): string; +``` + +Calling this API will set the asset base path for all Stencil components attached to a Stencil runtime. As a result, calling `setAssetPath` should not be done from within a component in order to prevent unwanted side effects when using a component. + +Make sure as component author to export this function as part of your module in order to also make it accessible to the consumer of your component, e.g. in your package entry file export the function via: + +```ts title="/src/index.ts" +export { setAssetPath } from '@stencil/core'; +``` + +Now your users can import it directly from your component library, e.g.: + +```ts +import { setAssetPath } from 'my-component-library'; +setAssetPath(`${window.location.protocol}//assets.${window.location.host}/`); +``` + +Alternatively, one may use [`document.currentScript.src`](https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript) when working in the browser and not using modules or environment variables (e.g. `document.env.ASSET_PATH`) to set the +asset base path. This configuration depends on how your script is bundled, (or lack of bundling), and where your assets can be loaded from. + +:::note + +If your component library exports components compiled with [`dist-output-target`](/output-targets/custom-elements.md) and `externalRuntime` set to `true`, then `setAssetPath` has to be imported from `@stencil/core` directly. + +::: + +In case you import a component directly via script tag, this would look like: + +```html + + + + + + + + + +``` diff --git a/versioned_docs/version-v4.42/guides/build-conditionals.md b/versioned_docs/version-v4.42/guides/build-conditionals.md new file mode 100644 index 000000000..0f540e8de --- /dev/null +++ b/versioned_docs/version-v4.42/guides/build-conditionals.md @@ -0,0 +1,41 @@ +--- +title: Build Conditionals +description: Build Conditionals +--- + +# Build Conditionals + +Build Conditionals in Stencil allow you to run specific code only when Stencil is running in development mode. This code is stripped from your bundles when doing a production build, therefore keeping your bundles as small as possible. + +### Using Build Conditionals + +Lets dive in and look at an example of how to use our build conditional: + +```tsx +import { Component, Build } from '@stencil/core'; + +@Component({ + tag: 'stencil-app', + styleUrl: 'stencil-app.css' +}) +export class StencilApp { + + componentDidLoad() { + if (Build.isDev) { + console.log('im in dev mode'); + } else { + console.log('im running in production'); + } + } +} +``` + +As you can see from this example, we just need to import `Build` from `@stencil/core` and then we can use the `isDev` constant to detect when we are running in dev mode or production mode. + +### Use Cases + +Some use cases we have come up with are: + +- Diagnostics code that runs in dev to make sure logic is working like you would expect +- `console.log()`'s that may be useful for debugging in dev mode but that you don't want to ship +- Disabling auth checks when in dev mode diff --git a/versioned_docs/version-v4.42/guides/csp-nonce.md b/versioned_docs/version-v4.42/guides/csp-nonce.md new file mode 100644 index 000000000..e18636cd8 --- /dev/null +++ b/versioned_docs/version-v4.42/guides/csp-nonce.md @@ -0,0 +1,119 @@ +--- +title: Content Security Policy Nonces +description: How to leverage CSP nonces in Stencil projects. +slug: /csp-nonce +--- + +# Using Content Security Policy Nonces in Stencil + +[Content Security Policies (CSPs)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) can help protect an application from Cross-Site Scripting (XSS) +attacks by adding a security layer to help prevent unauthorized code from running in the browser. + +An application that is served with a CSP other than 'unsafe-inline' and contains web components without a Shadow DOM will likely run into errors on load. +This is often first detected in the browser's console, which reports an error stating that certain styles or scripts violate the effective CSP. + +To resolve this issue, Stencil supports using [CSP nonces](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce) in +many of the output targets. + +:::caution +NOTE: CSPs and some CSP strategies are not supported by certain browsers. For a more detailed list, please see the [CSP browser compatibility table](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP#browser_compatibility). +::: + +## How to Use a Nonce + +The actual generation of the nonce value and enforcement of the correct CSP are not the responsibility of Stencil. Instead, the server of +the application will need to generate the nonce value for each page view, construct the CSP, and then correctly handle passing the generated nonce to +Stencil based on which output target is being consumed. + +There are many resources available that walk through setting up a CSP and using the nonce behavior. +[This](https://towardsdatascience.com/content-security-policy-how-to-create-an-iron-clad-nonce-based-csp3-policy-with-webpack-and-nginx-ce5a4605db90) +article walks through the process using Nginx and Webpack. Obviously, these resources don't account for the Stencil specifics, but any specifics will +be called out in this guide. + +Per the [MDN Guide on nonces](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce#generating_values), a nonce should be "a random base64-encoded string of at least 128 bits of data from a cryptographically secure random number generator". + +### Output Targets + +Using nonces may differ slightly between output targets, so please be sure to use the correct pattern based on the context in which your +Stencil components are consumed. + +#### Dist + +Consuming a `nonce` in the `dist` output target is easy using the provided `setNonce` helper function. This function is exported from the index +file of the output target's designated output directory. + +This function simply accepts the `nonce` string value that you want set for every `style` and `script` tag. + +This is an example of consuming the `dist` output in an Angular app's entrypoint: + +```ts +// main.ts + +import { defineCustomElements, setNonce } from 'my-lib/loader'; + +// Will set the `nonce` attribute for all scripts/style tags +// i.e. will run styleTag.setAttribute('nonce', 'r4nd0m') +// Obviously, you should use the nonce generated by your server +setNonce('r4nd0m'); + +// Generic Angular bootstrapping +platformBrowserDynamic() + .bootstrapModule(AppModule) + .catch(err => console.log(err)); + +defineCustomElements(); +``` + +#### Custom Elements + +Consuming a `nonce` in the `dist-custom-elements` output target is easy using the provided `setNonce` helper function. This function is exported +from the index file of the output target's designated output directory. + +This function simply accepts the `nonce` string value that you want set for every `style` and `script` tag. + +This is an example of consuming the `dist-custom-elements` output in an Angular app's entrypoint: + +```ts +// main.ts + +import { defineCustomElements, setNonce } from 'my-lib/dist/components'; +// Assume `customElementsExportBehavior: 'auto-define-custom-elements'` is set +import 'my-lib/dist/components/my-component'; + +// Will set the `nonce` attribute for all scripts/style tags +// i.e. will run styleTag.setAttribute('nonce', 'r4nd0m') +// Obviously, you should use the nonce generated by your server +setNonce('r4nd0m'); + +// Generic Angular bootstrapping +platformBrowserDynamic() + .bootstrapModule(AppModule) + .catch(err => console.log(err)); +``` + +#### WWW + +Unfortunately, setting `nonce` attributes gets a bit trickier when it comes to [SSR and SSG](../static-site-generation/01-overview.md). As a `nonce` needs +to be unique per page view, it cannot be defined/set at build time. So, this responsibility now falls on the +[hydrate app](../guides/hydrate-app.md)'s execution of runtime code. + +**SSR** + +Since there is not an easy way (or any way) of exposing and executing helper functions to manipulate the outcome of the runtime code, Stencil +has fallback behavior for pulling the `nonce` off of a `meta` tag in the DOM head. + +So, for SSR, your app can simply inject a `meta` element into the header _on each page request_. Yes, this does involve some manual configuration +for the code served by your server. To work correctly, the created tag must be generated as follows: + +```html + +``` + +This isn't a security risk because, for an attacker to execute a script to pull the nonce from the meta tag, they would have needed to know the +nonce _ahead_ of the script's execution. + +**SSG** + +Stencil cannot support CSP nonces with SSG. Because all of the code is generated during [pre-rendering](../static-site-generation/01-overview.md#how-static-site-generation-and-prerendering-works), Stencil doesn't generate the `style` or `script` tags at runtime. +If an application wants to leverage nonces in SSG, they can build a mechanism to scrape the pre-rendered code and apply the attribute server-side before +it is served to the client. diff --git a/versioned_docs/version-v4.42/guides/design-systems.md b/versioned_docs/version-v4.42/guides/design-systems.md new file mode 100644 index 000000000..356f9dcbc --- /dev/null +++ b/versioned_docs/version-v4.42/guides/design-systems.md @@ -0,0 +1,77 @@ +--- +title: Design Systems +sidebar_label: Design Systems +description: Design Systems in Stencil +slug: /design-systems +--- + +# Design Systems + +## What is a Design System? + +A Design System consists of UI components and a clearly defined visual style, released as both code implementations and design artifacts. +When adopted by all product teams, a more cohesive customer experience emerges. + +There are several aspects that Design Systems consist of: + +### Components +A component is a standalone UI element designed to be reusable across many projects. +Its goal is to do one thing well, while remaining abstract enough to allow for a variety of use cases. +Developers can use them as building blocks to build new user experiences. +One of the key benefits of reusable components is that developers don't have to worry about the core design and functionality of each component every time they use them. +Examples include buttons, links, forms, input fields, and modals. + +### Patterns +A pattern is an opinionated use of components. +Often, multiple components are combined in order to create a standardized user experience (UX). +As a result, they improve both the user and developer experience. +After implementing patterns, users will understand the application better and accomplish their tasks faster. +When the development team understands the proper way to use components together, software applications become easier to use. +Examples include saving data to the system, capturing data from forms, and filtering and analyzing data. + +### Visual Language +A cohesive company brand strengthens its value in the minds of the customer. +In the context of design systems, this means defining various aspects of the visual style, including colors, typography, and icons. +Defining primary, secondary, and tertiary colors helps an application stand out and is more user-friendly. +The right typography ensures users are not distracted while using an app. +Finally, icons increase engagement in a product and make it “pop” visually. + +### Design Artifacts and Code Implementations +By leveraging the components, patterns, and visual language of the design system, designers can create design artifacts representing UI workflows. +Developers refer to the artifacts as guidance for implementing the design with code. + +## The Value of Design Systems +With a design system in place, its true value is revealed. +The entire product development team is freed up to focus on what matters most: solving customer problems and delivering value. +Additionally, the avoidance of having teams working in parallel, recreating the same UI elements over and over, has a real-world project impact in terms of reduced time to market and increased cost savings. + +Design systems allow project teams to work better together. +Designers define a centralized “source of truth” for software application best practices which can be referenced by anyone in a product organization. +Developers no longer need to spend time rethinking how to build common app scenarios, such as application search or data table grids. +When the business inevitably makes changes to the design system, they can easily be applied to all projects. +The end result is a better product for your users. + +## Using Stencil to Build a Design System + +There’s a lot that goes into creating amazing UI components. +Performance, accessibility, cross-platform capabilities, and user experience (not only of the UI component itself but how it fits into the entire design system) all must be considered. + +These aspects take real effort to do well. + +Enter Stencil, a robust and highly extensible tool for building components and patterns, the building blocks of a design system. +With its intentionally minimalistic tooling and API footprint, it’s simple to incorporate into your existing development workflows. +It brings substantial performance out of the box by leveraging a tiny runtime. +Most importantly, all UI components built with Stencil are based 100% on open web standards. + +### The Importance of Open Web Standards +By using the web components standard, supported in all modern browsers, Stencil-built UI components offer many distinct advantages for use in a design system, namely: + +* They work on any platform or device +* They work with any front-end framework, so they can easily be used across teams and projects using different tech stacks +* They facilitate the creation of one company-wide code implementation instead of one per framework or platform + +Learn more about why web components are ideal for design systems in [this blog post](https://blog.ionicframework.com/5-reasons-web-components-are-perfect-for-design-systems/). + +### How to Get Started +Stencil’s out-the-box features will help you build your own library of universal UI components that will work across platforms, devices, and front-end frameworks. +Review the documentation on this site to get started. diff --git a/versioned_docs/version-v4.42/guides/extends.md b/versioned_docs/version-v4.42/guides/extends.md new file mode 100644 index 000000000..a50361f58 --- /dev/null +++ b/versioned_docs/version-v4.42/guides/extends.md @@ -0,0 +1,112 @@ +--- +title: Extends & Mixins +sidebar_label: Extends & Mixins +description: Using extends and Mixin() to organize component logic with controllers +slug: /extends +--- + +# Extends & Mixins + +As of v4.37.0, Stencil supports class inheritance (`extends`) and mixin composition (`Mixin()`). These features let you move logic out of components into reusable controller classes. + +Instead of putting all your logic directly in components, you can extract it into separate controller classes. This makes your code reusable, testable, and easier to maintain. + +## Two Approaches + +### Inheritance (Mixins) + +Use `Mixin()` to compose multiple controllers into your component: + +```typescript +@Component({ tag: 'my-component' }) +export class MyComponent extends Mixin( + ValidationControllerMixin, + FocusControllerMixin +) { + componentDidLoad() { + super.componentDidLoad(); // Required! + // Your logic here + } + + // All mixin methods are directly available + private onBlur = () => { + this.handleBlur(); + this.validate(this.values); + }; +} +``` + +:::note +With mixins, watch out for API collisions. Methods or properties with the same name can exist at different levels of the inheritance hierarchy, which can cause unexpected behavior. +::: + +### Composition + +Extend `ReactiveControllerHost` and add controller instances: + +```typescript +@Component({ tag: 'my-component' }) +export class MyComponent extends ReactiveControllerHost { + private validationController = new ValidationController(this); + private focusController = new FocusController(this); + + constructor() { + super(); + this.addController(this.validationController); + this.addController(this.focusController); + } + + componentDidLoad() { + super.componentDidLoad(); // Required! + // Your logic here + } + + // Only expose what you need + getValidationState() { + return this.validationController.getValidationState(); + } +} +``` + +## ReactiveControllerHost + +When using the composition pattern, components extend `ReactiveControllerHost`, which automatically manages the lifecycle methods. Here's what it looks like: + +```typescript +export interface ReactiveController { + hostConnected?(): void; + hostDisconnected?(): void; + hostWillLoad?(): Promise | void; + hostDidLoad?(): void; + hostWillRender?(): Promise | void; + hostDidRender?(): void; + hostWillUpdate?(): Promise | void; + hostDidUpdate?(): void; +} + +export class ReactiveControllerHost implements ComponentInterface { + controllers = new Set(); + + addController(controller: ReactiveController) { + this.controllers.add(controller); + } + + componentDidLoad() { + this.controllers.forEach((controller) => controller.hostDidLoad?.()); + } + // ... other lifecycle methods +} +``` + +## Quick Comparison + +| Aspect | Mixins | Composition | +|--------|--------|-------------| +| API Surface | More prone to collisions | Controlled, explicit | +| Method Discovery | Harder to find where methods come from | Clear, explicit delegation | +| Setup | Simple, direct access | More boilerplate | +| Testing | Harder to test in isolation | Controllers are independent and testable | +| Maintenance | Gets complex with many mixins | Easier to maintain and extend | + +For more examples, see the [test cases in the Stencil repository](https://github.com/stenciljs/core/tree/main/test/wdio/ts-target). + diff --git a/versioned_docs/version-v4.42/guides/forms.md b/versioned_docs/version-v4.42/guides/forms.md new file mode 100644 index 000000000..555162b98 --- /dev/null +++ b/versioned_docs/version-v4.42/guides/forms.md @@ -0,0 +1,165 @@ +--- +title: Forms +sidebar_label: Forms +description: Forms +slug: /forms +--- + +# Forms + +## Basic forms + +Here is an example of a component with a basic form: + +```tsx +@Component({ + tag: 'my-name', + styleUrl: 'my-name.css' +}) +export class MyName { + + @State() value: string; + + handleSubmit(e) { + e.preventDefault() + console.log(this.value); + // send data to our backend + } + + handleChange(event) { + this.value = event.target.value; + } + + render() { + return ( +
    this.handleSubmit(e)}> + + +
    + ); + } +} +``` + +Let's go over what is happening here. First we bind the value of the input to a state variable, in this case `this.value`. We then set our state variable to the new value of the input with the `handleChange` method we have bound to `onInput`. `onInput` will fire every keystroke that the user types into the input. + +## Using form-associated custom elements + +In addition to using a `
    ` element inside of a Stencil component, as shown +in the above example, you can also use Stencil's support for building +form-associated custom elements to create a Stencil component that integrates +in a native-like way with a `` tag _around_ it. This allows you to build +rich form experiences using Stencil components which leverage built-in form +features like validation and accessibility. + +As an example, translating the above example to be a form-associated component +(instead of one which includes a `` element in its JSX) would look like +this: + +```tsx +@Component({ + tag: 'my-name', + styleUrl: 'my-name.css', + formAssociated: true +}) +export class MyName { + @State() value: string; + @AttachInternals() internals: ElementInternals; + + handleChange(event) { + this.internals.setFormValue(event.target.value); + } + + render() { + return ( + + ); + } +} +``` + +For more check out the docs for [form-association in Stencil](../components/form-associated.md). + +## Advanced forms + +Here is an example of a component with a more advanced form: + +```tsx +@Component({ + tag: 'my-name', + styleUrl: 'my-name.css' +}) +export class MyName { + selectedReceiverIds = [102, 103]; + @State() value: string; + @State() selectValue: string; + @State() secondSelectValue: string; + @State() avOptions: any[] = [ + { 'id': 101, 'name': 'Mark' }, + { 'id': 102, 'name': 'Smith' } + ]; + + handleSubmit(e) { + e.preventDefault(); + console.log(this.value); + } + + handleChange(event) { + this.value = event.target.value; + + if (event.target.validity.typeMismatch) { + console.log('this element is not valid') + } + } + + handleSelect(event) { + console.log(event.target.value); + this.selectValue = event.target.value; + } + + handleSecondSelect(event) { + console.log(event.target.value); + this.secondSelectValue = event.target.value; + } + + render() { + return ( + this.handleSubmit(e)}> + + + + + + + +
    + ); + } +} +``` + +This form is a little more advanced in that it has two select inputs along with an email input. We also do validity checking of our email input in the `handleChange` method. We handle the `select` element in a very similar manner to how we handle text inputs. + +For the validity checking, we are #usingtheplatform and are using the [constraint validation api](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation) that is built right into the browser to check if the user is actually entering an email or not. diff --git a/versioned_docs/version-v4.42/guides/hydrate-app.md b/versioned_docs/version-v4.42/guides/hydrate-app.md new file mode 100644 index 000000000..1f9dc604b --- /dev/null +++ b/versioned_docs/version-v4.42/guides/hydrate-app.md @@ -0,0 +1,300 @@ +--- +title: Hydrate App +sidebar_label: Hydrate App +description: Hydrate App +slug: /hydrate-app +--- + +# Hydrate App + +The hydrate app is a Stencil output target which generates a module that can be +used on a NodeJS server to hydrate HTML and implement server side rendering (SSR). +This functionality is used internally by the Stencil compiler for +prerendering, as well as for the Angular Universal SSR for the Ionic +framework. However, like Stencil components, the hydrate app itself is not +restricted to one framework. + +_Note that Stencil does **NOT** use Puppeteer for SSR or prerendering._ + +## How to Use the Hydrate App + +Server side rendering (SSR) can be accomplished in a similar way to +prerendering. Instead of using the `--prerender` CLI flag, you can an output +target of type `'dist-hydrate-script'` to your `stencil.config.ts`, like so: + +```ts +outputTargets: [ + { + type: 'dist-hydrate-script', + }, +]; +``` + +This will generate a `hydrate` app in your root project directory that can be +imported and used by your Node server. + +After publishing your component library, you can import the hydrate app into +your server's code like this: + +```javascript +import { createWindowFromHtml, hydrateDocument, renderToString, streamToString } from 'yourpackage/hydrate'; +``` + +The hydrate app module exports 3 functions, `hydrateDocument`, `renderToString` and `streamToString`. `hydrateDocument` takes a [document](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDocument) as its input while `renderToString` as well as `streamToString` takes a raw HTML string. While `hydrateDocument` and `renderToString` return a Promise which wraps a result object, `streamToString` returns a [`Readable`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) stream that can be passed into a server response. + +### hydrateDocument + +You can use `hydrateDocument` as a part of your server's response logic before serving the web page. `hydrateDocument` takes two arguments, a [document](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDocument) and a config object. The function returns a promise with the hydrated results, with the hydrated HTML under the `html` property. + +*Example taken from Ionic Angular server* + + ```ts +import { hydrateDocument, createWindowFromHtml } from 'yourpackage/hydrate'; + +export function hydrateComponents(template: string) { + const win = createWindowFromHtml(template, Math.random().toString()) + + return hydrateDocument(win.document) + .then((hydrateResults) => { + // execute logic based on results + console.log(hydrateResults.html); + return hydrateResults; + }); +} +``` + +You can call the `hydrateComponents` function from your Node.js server, e.g.: + +```ts +import Koa from 'koa'; + +const app = new Koa(); +app.use(async (ctx) => { + const res = await hydrateComponents(` + + + + + Document + + + + + + +`) + ctx.body = res +}) +``` + +Please note that Stencil injects scoped component styles immediately after `` tags with a `rel="preconnect"` attribute, but before your custom styles. This setup allows you to define custom styles for your components effectively. + +#### hydrateDocument Options + + - `canonicalUrl` - string + - `constrainTimeouts` - boolean + - `clientHydrateAnnotations` - boolean + - `cookie` - string + - `direction` - string + - `language` - string + - `maxHydrateCount` - number + - `referrer` - string + - `removeScripts` - boolean + - `removeUnusedStyles` - boolean + - `resourcesUrl` - string + - `timeout` - number + - `title` - string + - `url` - string + - `userAgent` - string + +### renderToString + +The hydrate app also has a `renderToString` function that takes an HTML string +and returns a promise of `HydrateResults`. The optional second parameter is a +config object that can alter the output of the markup. Like `hydrateDocument`, +the hydrated HTML can be found under the `html` property. + +*Example taken from Ionic Core* + +```javascript +const results = await hydrate.renderToString( + ``, + { + fullDocument: false, + serializeShadowRoot: 'declarative-shadow-dom', + prettyHtml: true, + } +); + +console.log(results.html); +/** + * outputs: + * ```html + * + * + * + * + * ``` + */ +``` + +#### renderToString Options + +##### `approximateLineWidth` + +__Type:__ `number` + +Determines when line breaks are being set when serializing the component. + +##### `prettyHtml` + +__Default:__ `false` + +__Type:__ `boolean` + +If set to `true` it prettifies the serialized HTML code, intends elements and escapes text nodes. + +##### `removeAttributeQuotes` + +__Type:__ `boolean` + +__Default:__ `false` + +If set to `true` it removes attribute quotes when possible, e.g. replaces `someAttribute="foo"` to `someAttribute=foo`. + +##### `removeEmptyAttributes` + +__Type:__ `boolean` + +__Default:__ `true` + +If set to `true` it removes attribute that don't have values, e.g. remove `class=""`. + +##### `removeHtmlComments` + +__Type:__ `boolean` + +__Default:__ `false` + +If set to `true` it removes any abundant HTML comments. Stencil still requires to insert hydration comments to be able to reconcile the component. + +##### `beforeHydrate` + +__Type:__ `(document: Document, url: URL) => | Promise` + +Allows to modify the document and all its containing components to be modified before the hydration process starts. This allows e.g. to assign properties to the components dynamically: + +```ts +await renderToString(response.body, { + beforeHydrate: (doc: Document) => { + doc.querySelector(`my-component`).someComplexThing = new Map(...) + }, +}) +``` + +##### `afterHydrate` + +__Type:__ `(document: Document, url: URL, results: PrerenderUrlResults) => | Promise` + +Allows to modify the document and all its containing components after the component was rendered in the virtual DOM and before the serialization process starts. + +##### `serializeShadowRoot` + +__Default:__ `'declarative-shadow-dom'` + +__Type:__ + +```ts +'declarative-shadow-dom' | 'scoped' | { + 'declarative-shadow-dom'?: string[]; + scoped?: string[]; + default: 'declarative-shadow-dom' | 'scoped'; +} | false; +``` + +Configure how Stencil serializes a component's shadow-root: +- `declarative-shadow-dom` - all `shadow: true` components will be rendered with a [Declarative Shadow DOM](https://developer.chrome.com/docs/css-ui/declarative-shadow-dom). +- `scoped` - all `shadow: true` components will be rendered with Stencil's custom scoped behavior; a light-dom tree and single ` + *
    + * + * Hello, World! I'm Stencil 'Don't call me a framework' JS + *
    + * + * + * + * ``` + */ +``` + +**Scoped Example:** + +```javascript +const results = await hydrate.renderToString( + ``, + { + fullDocument: true, + serializeShadowRoot: `scoped`, + prettyHtml: true, + } +); + +console.log(results.html); +/** + * outputs: + * ```html + * + * + * + * + * + * + *
    + * + * Hello, World! I'm Stencil 'Don't call me a framework' JS + *
    + *
    + * + * ``` + */ +``` + +##### `fullDocument` + +__Type:__ `boolean` + +__Default:__ `true` + +If set to `true`, Stencil will serialize a complete HTML document for a server to respond. If set to `false` it will only render the components within the given template. diff --git a/versioned_docs/version-v4.42/guides/module-bundling.md b/versioned_docs/version-v4.42/guides/module-bundling.md new file mode 100644 index 000000000..769bea76b --- /dev/null +++ b/versioned_docs/version-v4.42/guides/module-bundling.md @@ -0,0 +1,150 @@ +--- +title: Module Bundling +sidebar_label: Bundling +description: Module Bundling +slug: /module-bundling +--- + +# Module Bundling + +Stencil uses [Rollup](https://rollupjs.org/guide/en/) under the hood to bundle your components. This guide will explain and recommend certain workarounds for some of the most common bundling issues you might encounter. + +## One Component Per Module + +For Stencil to bundle your components most efficiently, you must declare a single component (class decorated with `@Component`) per *TypeScript* file, and the component itself **must** have a unique `export`. By doing so, Stencil is able to easily analyze the entire component graph within the app, and best understand how components should be bundled together. Under-the-hood it uses the Rollup bundler to efficiently bundled shared code together. Additionally, lazy-loading is a default feature of Stencil, so code-splitting is already happening automatically, and only dynamically importing components which are being used on the page. + +Modules that contain a component are entry-points, which means that no other module should import anything from them. + +The following example is **NOT** valid: + +```tsx title="src/components/my-cmp.tsx" +// This module has a component, you cannot export anything else +export function someUtilFunction() { + console.log('do stuff'); +} + +@Component({ + tag: 'my-cmp' +}) +export class MyCmp {} +``` + +In this case, the compiler will emit an error that looks like this: + +```bash +[ ERROR ] src/components/my-cmp.tsx:4:1 + To allow efficient bundling, modules using @Component() can only have a single export which is the component + class itself. Any other exports should be moved to a separate file. For further information check out: + https://stenciljs.com/docs/module-bundling + + L4: export function someUtilFunction() { + L5: console.log('do stuff'); +``` + +The solution is to move any shared functions or classes to a different `.ts` file, like this: + +```tsx title="src/utils.ts" +export function someUtilFunction() { + console.log('do stuff'); +} +``` + +```tsx title="src/components/my-cmp.tsx" +import { someUtilFunction } from '../utils.ts'; + +@Component({ + tag: 'my-cmp' +}) +export class MyCmp {} +``` + +```tsx title="src/components/my-cmp-two.tsx" +import { someUtilFunction } from '../utils.ts'; + +@Component({ + tag: 'my-cmp-two' +}) +export class MyCmpTwo {} +``` + + +## CommonJS Dependencies + +Rollup depends on [ES modules (ESM)](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) to properly tree-shake the module graph, unfortunately, some third-party libraries ship their code using the [CommonJS](https://requirejs.org/docs/commonjs.html) module system, which is not ideal. + +Since CommonJS libraries are still common today, Stencil comes with [`rollup-plugin-commonjs`](https://github.com/rollup/rollup-plugin-commonjs) already installed and configured. + +At compiler-time, the `rollup-plugin-commonjs` plugin does a best-effort to **transform CommonJS into ESM**, but this is not always an easy task. CommonJS is dynamic by nature, while ESM is static by design. + +For further information, check out the [rollup-plugin-commonjs docs](https://github.com/rollup/plugins/tree/master/packages/commonjs). + + + +## Custom Rollup plugins + +Stencil provides an API to pass custom rollup plugins to the bundling process in `stencil.config.ts`. Under the hood, stencil ships with some built-in plugins including `node-resolve` and `commonjs`, since the execution order of some rollup plugins is important, stencil provides an API to inject custom plugin **before node-resolve** and after **commonjs transform**: + +```tsx +export const config = { + rollupPlugins: { + before: [ + // Plugins injected before rollupNodeResolve() + resolvePlugin() + ], + after: [ + // Plugins injected after commonjs() + nodePolyfills() + ] + } +} +``` + +As a rule of thumb, plugins that need to resolve modules, should be place in `before`, while code transform plugins like: `node-polyfills`, `replace`... should be placed in `after`. Follow the plugin's documentation to make sure it's executed in the right order. + + +## Node Polyfills + +Depending on which libraries a project is dependent on, the [rollup-plugin-node-polyfills](https://www.npmjs.com/package/rollup-plugin-node-polyfills) plugin may be required. In such cases, an error message similar to the following will be displayed at build time. + +```bash +[ ERROR ] Bundling Node Builtin + For the import "crypto" to be bundled from 'problematic-dep', + ensure the "rollup-plugin-node-polyfills" plugin is installed + and added to the stencil config plugins. +``` + +This is caused by some third-party dependencies that use [Node APIs](https://nodejs.org/dist/latest-v10.x/docs/api/) that are not available in the browser, the `rollup-plugin-node-polyfills` plugin works by transparently polyfilling this missing APIs in the browser. + +### 1. Install `rollup-plugin-node-polyfills`: + +```bash npm2yarn +npm install rollup-plugin-node-polyfills --save-dev +``` + +### 2. Update the `stencil.config.ts` file including the plugin: + +```tsx +import { Config } from '@stencil/core'; +import nodePolyfills from 'rollup-plugin-node-polyfills'; + +export const config: Config = { + namespace: 'mycomponents', + rollupPlugins: { + after: [ + nodePolyfills(), + ] + } +}; +``` + +:::note +`rollup-plugin-node-polyfills` is a code-transform plugin, so it needs to run AFTER the commonjs transform plugin, that's the reason it's placed in the "after" array of plugins. +::: + +## Strict Mode + +ES modules are always parsed in strict mode. That means that certain non-strict constructs (like octal literals) will be treated as syntax errors when Rollup parses modules that use them. Some older CommonJS modules depend on those constructs, and if you depend on them your bundle will blow up. There's nothing we can do about that. + +Luckily, there is absolutely no good reason not to use strict mode for everything — so the solution to this problem is to lobby the authors of those modules to update them. + +*Source: [https://github.com/rollup/rollup-plugin-commonjs#strict-mode](https://github.com/rollup/rollup-plugin-commonjs#strict-mode)* diff --git a/versioned_docs/version-v4.42/guides/publishing.md b/versioned_docs/version-v4.42/guides/publishing.md new file mode 100644 index 000000000..1c4acdf01 --- /dev/null +++ b/versioned_docs/version-v4.42/guides/publishing.md @@ -0,0 +1,159 @@ +--- +title: Publishing A Component Library +sidebar_label: Publishing +description: Publishing A Component Library +slug: /publishing +--- + +There are numerous strategies to publish and distribute your component library to be consumed by external projects. One of the benefits of Stencil is that is makes it easy to generate the various [output targets](../output-targets/01-overview.md) that are right for your use-case. + +## Use Cases + +To use your Stencil components in other projects, there are two different output targets to consider: [`dist`](../output-targets/dist.md) and [`dist-custom-elements`](../output-targets/custom-elements.md). Both export your components for different use cases. Luckily, both can be generated at the same time, using the same source code, and shipped in the same distribution. It would be up to the consumer of your component library to decide which build to use. + +### Lazy Loading + +If you prefer to have your components automatically loaded when used in your application, we recommend enabling the [`dist`](../output-targets/dist.md) output target. The bundle gives you a small entry file that registers all your components and defers loading the full component logic until it is rendered in your application. It doesn't matter if the actual application is written in HTML or created with vanilla JavaScript, jQuery, React, etc. + +Your users can import your component library, e.g. called `my-design-system`, either via a `script` tag: + +```html + +``` + +or by importing it in the bootstrap script of your application: + +```ts +import 'my-design-system'; +``` + +To ensure that the right entry file is loaded when importing the project, define the following fields in your `package.json`: + +```json +{ + "exports": "./dist/esm/my-design-system.js", + "main": "./dist/cjs/my-design-system.js", + "unpkg": "dist/my-design-system/my-design-system.esm.js", +} +``` + +Read more about various options when it comes to configuring your project's components for lazy loading in the [`dist`](../output-targets/dist.md) output target section. + +#### Considerations + +To start, Stencil was designed to lazy-load itself only when the component was actually used on a page. There are many benefits to this approach, such as simply adding a script tag to any page and the entire library is available for use, yet only the components actually used are downloaded. For example, [`@ionic/core`](https://www.npmjs.com/package/@ionic/core) comes with over 100 components, but a webpage may only need `ion-toggle`. Instead of requesting the entire component library, or generating a custom bundle for just `ion-toggle`, the `dist` output target is able to generate a tiny entry build ready to load any of its components on-demand. + +However be aware that this approach is not ideal in all cases. It requires your application to ship the bundled components as static assets in order for them to load properly. Furthermore, having many nested component dependencies can have an impact on the performance of your application. For example, given you have a component `CmpA` which uses a Stencil component `CmpB` which itself uses another Stencil component `CmpC`. In order to fully render `CmpA` the browser has to load 3 scripts sequentially which can result in undesired rendering delays. + +### Standalone + +The [`dist-custom-elements`](../output-targets/custom-elements.md) output target builds each component as a stand-alone class that extends `HTMLElement`. The output is a standardized custom element with the styles already attached and without any of Stencil's lazy-loading. This may be preferred for projects that are already handling bundling, lazy-loading and defining the custom elements themselves. + +The generated files will each export a component class and will already have the styles bundled. However, this build does not define the custom elements or apply any polyfills. Static assets referenced within components will need to be set using `setAssetPath` (see [Making Assets Available](../output-targets/custom-elements.md#making-assets-available)). + +You can use these standalone components by importing them via: + +```ts +import { MyComponent, defineCustomElementMyComponent } from 'my-design-system' + +// register to CustomElementRegistry +defineCustomElementMyComponent() + +// or extend custom element via +class MyCustomComponent extends MyComponent { + // ... +} +define('my-custom-component', MyCustomComponent) +``` + +To ensure that the right entry file is loaded when importing the project, define different [exports fields](https://nodejs.org/api/packages.html#exports) in your `package.json`: + +```json +{ + "exports": { + ".": { + "import": "./dist/components/index.js", + "types": "./dist/components/index.d.ts" + }, + "./my-component": { + "import": "./dist/components/my-component.js", + "types": "./dist/components/my-component.d.ts" + } + }, + "types": "dist/components/index.d.ts", +} +``` + +This allows us to map certain import paths to specific components within our project and allows users to only import the component code they are interested in and reduce the amount of code that needs to downloaded by the browser, e.g.: + +```js +// this import loads all compiled components +import { MyComponent } from 'my-design-system' +// only import compiled code for MyComponent +import { MyComponent } from 'my-design-system/my-component' +``` + +If you define exports targets for all your components as shown above and by using [`customElementsExportBehavior: 'auto-define-custom-elements'`](../output-targets/custom-elements.md#customelementsexportbehavior) as output target option, you can skip the `defineCustomElement` call and directly import the component where you need it: + +```ts +import 'my-design-system/my-component' +``` + +:::note +If you are distributing both the `dist` and `dist-custom-elements`, then it's best to pick one of them as the main entry depending on which use case is more prominent. +::: + +Read more about various options when it comes to distributing your components as standalone components in the [`dist-custom-elements`](../output-targets/custom-elements.md) output target section. + +The output directory will also contain an `index.js` file which exports some helper methods by default. The contents of the file will look something like: + +```js +export { setAssetPath, setPlatformOptions } from '@stencil/core/internal/client'; +``` + +:::note +The contents may look different if [`customElementsExportBehavior`](../output-targets/custom-elements.md#customelementsexportbehavior) is specified! +::: + +#### Considerations + +The `dist-custom-elements` is a direct build of the custom element that extends `HTMLElement`, without any lazy-loading. This distribution strategy may be preferred for projects that use an external bundler such as [Vite](https://vitejs.dev/), [WebPack](https://webpack.js.org/) or [Rollup](https://rollupjs.org) to compile the application. They ensure that only the components used within your application are bundled into compilation. + +#### Usage in TypeScript + +If you plan to support consuming your component library in TypeScript you'll need to set `generateTypeDeclarations: true` on the output target in your `stencil.config.ts`, like so: + +```tsx title="stencil.config.ts" +import { Config } from '@stencil/core'; + +export const config: Config = { + outputTargets: [ + { + type: 'dist-custom-elements', + generateTypeDeclarations: true, + }, + // ... + ], + // ... +}; +``` + +Then you can set the `types` property in `package.json` so that consumers of your package can find the type definitions, like so: + +```json title="package.json" +{ + "types": "dist/components/index.d.ts", + "dependencies": { + "@stencil/core": "latest" + }, + ... +} +``` + +:::note +If you set the `dir` property on the output target config, replace `dist/components` in the above snippet with the path set in the config. +::: + +## Publishing to NPM + +[NPM](https://www.npmjs.com/) is an online software registry for sharing libraries, tools, utilities, packages, etc. To make your Stencil project widely available to be consumed, it's recommended to [publish the component library to NPM](https://docs.npmjs.com/getting-started/publishing-npm-packages). Once the library is published to NPM, other projects are able to add your component library as a dependency and use the components within their own projects. diff --git a/versioned_docs/version-v4.42/guides/server-side-rendering.md b/versioned_docs/version-v4.42/guides/server-side-rendering.md new file mode 100644 index 000000000..a6fa27613 --- /dev/null +++ b/versioned_docs/version-v4.42/guides/server-side-rendering.md @@ -0,0 +1,588 @@ +--- +title: Server Side Rendering +sidebar_label: Server Side Rendering +description: Server Side Rendering +slug: /server-side-rendering +--- + +# Server-Side Rendering (SSR) with Stencil + +Stencil provides server-side rendering (SSR) support for React and Vue Output Targets. If you're using frameworks like [Vite](https://vite.dev/), [Remix](https://remix.run/), [Next.js](https://nextjs.org/) or [Nuxt](https://nuxt.com/), Stencil automatically enhances these frameworks to render components on the server using a [Declarative Shadow DOM](https://web.dev/articles/declarative-shadow-dom) or in [scoped mode](/docs/styling#scoped-css). + +The first step to enable server side rendering is to generate a hydrate module of your components. + +## Hydrate Module + +The Hydrate Module is a standalone bundle of all your components that uses a JavaScript implementation of various HTML and DOM standards to render Stencil components in a Node.js environment. To create this bundle you have to add `dist-hydrate-script` to your Stencil configuration: + +```tsx title="stencil.config.ts" +import { Config } from '@stencil/core'; + +export const config: Config = { + outputTargets: [ + { + type: 'dist-hydrate-script', + dir: './hydrate', + }, + // ... + ] +}; +``` + +This will create the Hydrate Module which you can export separately via: + +```ts title="package.json" +{ + "name": "component-library", + ... + "exports": { + ... + "./hydrate": { + "types": "./hydrate/index.d.ts", + "import": "./hydrate/index.mjs", + "require": "./hydrate/index.js", + "default": "./hydrate/index.js" + }, + ... + }, + ... +} +``` + +## Two SSR Approaches + +Stencil provides two distinct strategies for server-side rendering, each designed to solve different challenges and use cases. These approaches emerged from the complex nature of rendering Web Components on the server, where traditional browser APIs don't exist. + +### Strategy 1: The Compiler Approach (Universal SSR) + +The compiler-based approach, implemented in the `@stencil/ssr` package, works as a build-time plugin that intercepts your code and performs AST (Abstract Syntax Tree) transformations. It transforms components at compile time. + +#### How It Works + +1. **Build-time interception**: The plugin (Vite or Webpack) receives your JSX code after transformation +2. **AST analysis**: Parses JavaScript into an Abstract Syntax Tree to identify Stencil components +3. **Prop analysis**: Analyzes the props being passed to each component +4. **Pre-rendering**: Calls Stencil's hydrate module to render the component server-side +5. **Code replacement**: Replaces the original component with a wrapper containing pre-rendered HTML + +#### When to Use Compiler-Based SSR + +✅ **Multiple framework support**: Need to support Vite, Remix, Next.js, and other meta-frameworks +✅ **Performance critical applications**: Where response speed is paramount +✅ **Predictable props**: Components with static or build-time determinable data +✅ **"Set it and forget it" solutions**: Want minimal runtime complexity + +#### Advantages + +- **Universal compatibility**: Works with any React meta-framework +- **Zero runtime overhead**: No server processing during requests +- **Handles deep nesting**: Excellent at rendering complex component compositions +- **Clean separation**: Clear distinction between build-time and runtime concerns +- **Consistent performance**: Predictable response times + +#### Disadvantages + +- **Static props only**: Cannot resolve dynamic props at compile time (e.g., `prop={calculateValue()}`) +- **Build-time configuration required**: Needs plugin setup +- **Hydration mismatches**: Still prone to occasional client/server differences + +### Strategy 2: The Runtime Approach (Next.js Server Components) + +The runtime approach leverages Next.js Server Components to perform real-time SSR. When the server encounters a Stencil component, it renders it on-demand during the request cycle. + +#### How It Works + +1. **Component interception**: Next.js hits a Stencil component during server rendering +2. **Prop serialization**: Uses `serializeProperty` to handle complex objects, Maps, Sets, and even `Infinity` +3. **Children transformation**: Attempts to transform React children into strings using `react-dom/server` +4. **Async rendering**: Calls Stencil's `renderToString` (Promise-based) on the server +5. **React node recreation**: Parses resulting HTML back into React nodes using `html-react-parser` + +#### When to Use Runtime-Based SSR + +✅ **Next.js commitment**: When you're fully invested in the Next.js ecosystem +✅ **Dynamic values**: Props that are highly dynamic or computed at runtime +✅ **Light DOM access**: Need to include children in server rendering + +#### Advantages + +- **Full prop access**: All props available with resolved values at runtime +- **Light DOM inclusion**: Can include children during serialization +- **Dynamic value support**: Handles runtime-computed values perfectly +- **True isomorphic rendering**: Complete server-client parity +- **Complex object handling**: Built-in serialization for advanced data types + +#### Disadvantages + +- **Next.js only**: Requires Server Components support +- **Dual component management**: Must maintain both client and server wrappers +- **Performance overhead**: Runtime serialization adds latency +- **Additional Setup**: Requires importing components from a separate export path, e.g. `my-react-components/next` + +### Choosing Your Strategy + +Here's the battle-tested decision tree from real-world implementation: + +#### Use the Compiler Approach when: +- You need to support multiple frameworks beyond Next.js +- Performance is your top priority +- Your components have predictable, static props +- You want a "set it and forget it" solution +- You're building a content-focused site (marketing, docs, blogs) + +#### Use the Runtime Approach when: +- You're committed to Next.js and Server Components +- You need full Light DOM access for complex compositions +- Your props are highly dynamic or computed at runtime +- You're okay with additional complexity for more rendering power +- You're building highly interactive, data-driven applications + +## Enable SSR for StencilJS + +For serializing Stencil components on the server, Stencil uses a package called `@stencil/ssr` which you can install via: + +```sh +npm install --save-dev @stencil/ssr +``` + +It exports compiler plugins for Vite based projects, e.g. Remix or Webpack based ones like Next.js. The plugin requires the following options: + +#### `module` + +The import of the package that exports all your Stencil React components. It helps the package to understand which components can be server side rendered. + +#### `from` + +The name of the package that exports all your Stencil React components. Stencil will look up all imports from that package and transforms the statement to use a server side rendered version of the component. + +#### `hydrateModule` + +Your generated hydrate module that gives the package the primitives to serialize a given Stencil component. + +#### `serializeShadowRoot` + +**optional** + +**default: __declarative-shadow-dom__** + +Configurations on how the components should be rendered on the server, e.g. as Declarative Shadow DOM, as scoped components or as a mixture of both. + +### Vite + +If your project is based on the Vite compiler, this includes frameworks like [Remix](https://remix.run/) that are rely on Vite under the hood, you can use the `@stencil/ssr` package to enable SSR support for Stencil components by adding the plugin to the configuration: + +```ts title="vite.config.ts" +import { defineConfig } from 'vite'; +import { stencilSSR } from '@stencil/ssr'; +import react from '@vitejs/plugin-react'; + +// https://vite.dev/config/ +export default defineConfig({ + plugins: [ + react(), + stencilSSR({ + module: import('component-library-react'), + from: 'component-library-react', + hydrateModule: import('component-library/hydrate'), + serializeShadowRoot: { + 'scoped': ['my-counter'], + default: 'declarative-shadow-dom', + }, + }), + ], +}) +``` + +or in case of a Remix project: + +```ts title="vite.config.ts" +import { vitePlugin as remix } from "@remix-run/dev"; +import { defineConfig } from "vite"; +import { stencilSSR } from "@stencil/ssr"; + +declare module "@remix-run/node" { + interface Future { + v3_singleFetch: true; + } +} + +export default defineConfig({ + plugins: [ + remix(), + stencilSSR({ + module: import('component-library-react'), + from: 'component-library-react', + hydrateModule: import('component-library/hydrate'), + serializeShadowRoot: { + 'scoped': ['my-counter'], + default: 'declarative-shadow-dom', + }, + }), + ], +}); +``` + +### Next.js + +When using Stencil with Next.js, you can server-side render (SSR) components in two ways: **at runtime** or **at compile time**. Each approach has its trade-offs, and the best choice depends on your component architecture. + +* If your components rely heavily on **slots**, the **compiler-based** SSR approach is generally more reliable. +* If your components use **dynamically computed attributes**, the **runtime-based** SSR approach is more flexible. + +#### Runtime-Based SSR + +In this method, Stencil serializes the component to a **Declarative Shadow DOM** during runtime, as Next.js renders the component on the server. Since Next.js supports asynchronous server components, this allows Stencil to perform serialization as part of the render process. + +To enable runtime SSR, set the `hydrateModule` option in your React output target: + +```ts +import { Config } from '@stencil/core'; +import { reactOutputTarget } from '@stencil/react-output-target'; + +export const config: Config = { + namespace: 'component-library', + outputTargets: [ + reactOutputTarget({ + /** + * tell Stencil where to generate the `components.ts` and `components.server.ts` files + */ + outDir: '../component-library-react/src', + /** + * give Stencil the import name of the hydrate module + */ + hydrateModule: 'component-library/hydrate', + /** + * tell the server component where it would import the client version of the components + */ + clientModule: 'component-library-react', + serializeShadowRoot: { /* options */ }, + }), + ], +}; +``` + +This will generate: + +* `components.ts` — for use on the client +* `components.server.ts` — for server-side rendering + +If you distribute your React wrapper as a separate package, consider exposing the server entry point via a custom export path in your `package.json`: + +```json +{ + "name": "component-library-react", + "version": "0.0.0", + "type": "module", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js", + "node": "./dist/components.server.js" + }, + "./next": { + "types": "./dist/components.server.d.ts", + "import": "./dist/components.server.js" + } + } +} +``` + +Now you can use the server-optimized components in your Next.js app: + +```tsx title="/src/app/page.tsx" +import { MyComponent } from 'component-library-react/next'; + +export default function Home() { + return ( + <> + ... + + + ); +} +``` + +##### ✅ Advantages + +* All component props are available with their **resolved values** at runtime. +* Props can be **computed dynamically** or retrieved from functions: + +```tsx +const value = getValueFromAPI(); +return ; +``` + +##### ⚠️ Disadvantages + +* **Nested Stencil components** may fail to render correctly on the server. +* Components using slots may confuse Next.js's `BAILOUT_TO_CLIENT_SIDE_RENDERING` template tags as entries + +#### Compiler-Based SSR + +With this approach, the `@stencil/ssr` package pre-processes your application during the build step to wrap and serialize Stencil components for server-side rendering. + +To enable it, wrap your Next.js configuration using the `stencilSSR()` helper: + +```js title="next.config.js" +import stencilSSR from '@stencil/ssr/next'; + +/** @type {import('next').NextConfig} */ +const nextConfig = { + // your base config +}; + +export default stencilSSR({ + module: import('component-library-react'), + from: 'component-library-react', + hydrateModule: import('component-library/hydrate'), + serializeShadowRoot: { + scoped: ['my-counter'], + default: 'declarative-shadow-dom', + }, +})(nextConfig); +``` + +> 📌 **Note:** The integration import path is `@stencil/ssr/next`. + +##### ✅ Advantages + +* More **reliable SSR** for Stencil components rendered in the **light DOM**. +* Avoids hydration mismatch issues commonly seen with client-heavy components. + +##### ⚠️ Disadvantages + +* Since components are pre-rendered at **build time**, **runtime values** (e.g. function calls or dynamic props) are not available: + +```tsx +// ✅ Static objects are fine +const staticProp = { key: 'value' }; + +// ❌ Dynamic values will not be resolved +const runtimeValue = computeAtRuntime(); +return ; +``` + +* Components using **slots** may render incorrectly due to internal Next.js behavior, such as injected `