Skip to content

Instantly share code, notes, and snippets.

@renoirb
Last active March 8, 2018 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renoirb/a39069722261328ef23863b9f3df4af0 to your computer and use it in GitHub Desktop.
Save renoirb/a39069722261328ef23863b9f3df4af0 to your computer and use it in GitHub Desktop.
CgiFooter exported as a WebComponent
<template>
<div :class="[classNames]">
<a
:href="productUrl"
>
{{ productName }}
</a>
&nbsp;
&copy;
&nbsp;
{{ firstYear }}-{{ year }}
&nbsp;
<a
:href="publisherUrl"
>
{{ publisherName }}
</a>
</div>
</template>
<script>
const classNames = ['grey', 'lighten-4', 'elevation-4'];
// @vue/component
export default {
props: {
productName: {
type: String,
default: 'CGI Unify360',
},
productUrl: {
type: String,
default: 'https://www.cgi.com/en/solutions/cgi-unify360',
},
publisherName: {
type: String,
default: 'CGI',
},
publisherUrl: {
type: String,
default: 'https://www.cgi.com/',
},
firstYear: {
type: Number,
default: 2016,
},
},
data: () => ({
classNames,
}),
computed: {
year: () => new Date().getFullYear(),
},
};
</script>
@renoirb
Copy link
Author

renoirb commented Mar 8, 2018

Introduction

This is a prototype to show off how we could create HTML custom elements using Vue.js

Parts of this demo has been inspired by Evan You - State of VueJS 2018 around 21m

Reproduce

  1. Ensure you have the following installed within a working NodeJS installation.

    npm install -g yarn@latest
    yarn global add @vue/cli
    yarn global add @vue/cli-service-global
  2. Create a Vue component as a file, e.g. CgiFooter.vue, paste contents from example here

  3. Compile as a Web Component

    vue build --target wc --name cgi-footer CgiFooter.vue

    creating-web-component-2018030801

  4. Use it somewhere, for example in JSBin, See in action NOTE that this currently works only in Google Chrome, because Web Components and Custom Elements aren't native everywhere, yet

    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
    <script src="https://rawgit.com/renoirb/a39069722261328ef23863b9f3df4af0/raw/cgi-footer.js"></script>
    <cgi-footer first-year="1976" product-name="ACME Corp Inc. FooBar Global Extra Pro Plus"></cgi-footer>

    creating-web-component-2018030802

Enable in Firefox

  1. Go to about:config
  2. Set to true the following keys
  • dom.webcomponents.shadowdom.enabled
  • dom.webcomponents.customelements.enabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment