From a5fc04952a3aae321c1ef0cdc3e8a2225d663ef0 Mon Sep 17 00:00:00 2001 From: Robert-Frampton Date: Thu, 28 Sep 2017 16:53:30 -0700 Subject: [PATCH] Add basic guide for metal-custom-element --- src/pages/docs/guides/web-components.md | 110 ++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/pages/docs/guides/web-components.md diff --git a/src/pages/docs/guides/web-components.md b/src/pages/docs/guides/web-components.md new file mode 100644 index 00000000..eaec5852 --- /dev/null +++ b/src/pages/docs/guides/web-components.md @@ -0,0 +1,110 @@ +--- +title: "Web Components" +description: "Using metal components as custom elements (web components)." +layout: "guide" +weight: 230 +--- + +
+ +## [Custom Elements](#custom_elements) + +Metal components are generally invoked in one of three ways. + +1) JavaScript + +```javascript +new metal.MyComponent({ + title: 'Hello, World!' +}, '#element'); +``` + +2) Soy + +```soy +{call MyComponent.render} + {param title: "Hello, World!" /} +{/call} +``` + +3) JSX + +```jsx + +``` + +However, with the help of the [metal-custom-element](https://www.npmjs.com/package/metal-custom-element) package, Metal components can be invoked as [Custom Elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Custom_Elements) in +plain HTML. + +```xml + +``` + +
+ +
+ +## [Install](#install) + +First step is to install the `metal-custom-element` package. + +```bash +npm i --save metal-custom-element +``` + +Currently custom elements do not work on every browser, so a pollyfill must be +used. Include the [webcomponents-lite pollyfill](https://www.webcomponents.org/polyfills) if +you intend to use custom elements on Firefox, Edge, or IE11. + +
+ +
+ +## [Define custom elements](#define_custom_elements) + +This package exposes a single helper function that can be used to wrap any Metal +component in a custom element. It receives two arguments, the tag name you want +the custom element to receive, and the constructor of the Metal component. + +```javascript +import JSXComponent from 'metal-jsx'; +import defineWebComponent from 'metal-custom-element'; + +class MyComponent extends JSXComponent { + render() { + return

{this.props.message}

+ } +} + +MyComponent.PROPS = { + message: { + value: '' + } +}; + +defineWebComponent('my-component', MyComponent); +``` + +Now that the custom element is defined, it can be invoked in plain html. + +```xml + +``` + +This will then result in the following HTML on the page. + +```xml +

This is a web component

+``` + +If you would like the component's markup to be rendered using the Shadow DOM, +simply set the `useshadowdom` attribute to `true` when calling the custom element. + +```xml + +``` + +This means that any styling on the page will not cascade to your component's +markup. See [MDN's documentation](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM) for more info. + +
\ No newline at end of file