Skip to content

Include mixin and inherited props in compiled web component #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
.DS_Store
.idea/
package-lock.json
22 changes: 22 additions & 0 deletions abas/README2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This readme is only for abas

1. Checkout this wrapper repo into the same level of the project what you want to convert. e.g. abas-elements
2. To fix the icons styles
Put 'WcIconMixin.js' file into abas-elements/src/mixins
Add 'import WcIconMixin from '../mixins/WcIconMixin';' in Icon.vue
Add mixin 'mixins: [WcIconMixin],' in Icon.vue
3. Adjust 'vue.config.js' in abas-elements project by adding alias for the '@vue/web-component-wrapper'

'''
configureWebpack: {
resolve: {
alias: {
'@vue/web-component-wrapper': path.join(__dirname, '../vue-web-component-wrapper'),
},
},
},
'''
4. add dependency into package.json in block 'devDependencies'
'''
"eslint-plugin-vue-libs": "^2.1.0"
'''
28 changes: 28 additions & 0 deletions abas/WcIconMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { dom } from '@fortawesome/fontawesome-svg-core'

/**
* @mixin
*/
export default {
mounted () {
const id = 'fa-styles'
const shadowRoot = this.getShadowRoot(this)
if (shadowRoot && !shadowRoot.getElementById(id)) {
const faStyles = document.createElement('style')
faStyles.setAttribute('id', id)
faStyles.textContent = dom.css()
shadowRoot.appendChild(faStyles)
}
},
methods: {
getShadowRoot (obj) {
if (obj.$parent) {
if (obj.$parent.$options && obj.$parent.$options.shadowRoot) {
return obj.$parent.$options.shadowRoot
}
return this.getShadowRoot(obj.$parent)
}
return null
}
}
}
74 changes: 61 additions & 13 deletions dist/vue-wc-wrapper.global.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,54 @@ function getAttributes (node) {
return res
}

function spreadProps (component) {
const result = {};
spreadNext(result, component);
return result
}

function spreadNext (result, component) {
if (component.props) {
appendProps(result, component.props);
}

if (component.mixins) {
component.mixins.forEach(function (mixin) {
spreadNext(result, mixin);
});
}
if (component.extends) {
spreadNext(result, component.extends);
}
}

function appendProps (result, props) {
if (Array.isArray(props)) {
processArrayProps(result, props);
} else {
processObjectProps(result, props);
}
}

function processObjectProps (result, props) {
for (const key in props) {
const camelKey = camelize(key);
if (!(camelKey in result)) {
result[camelKey] = props[key];
}
}
}
function processArrayProps (result, props) {
props.forEach(function (prop) {
if (typeof prop === 'string') {
const camelKey = camelize(prop);
if (!(camelKey in result)) {
result[camelKey] = undefined;
}
}
});
}

function wrap (Vue, Component) {
const isAsync = typeof Component === 'function' && !Component.cid;
let isInitialized = false;
Expand All @@ -112,13 +160,13 @@ function wrap (Vue, Component) {
? Component.options
: Component;

// spread props
options.props = spreadProps(options);
// extract props info
const propsList = Array.isArray(options.props)
? options.props
: Object.keys(options.props || {});
const propsList = Object.keys(options.props || {});
hyphenatedPropsList = propsList.map(hyphenate);
camelizedPropsList = propsList.map(camelize);
const originalPropsAsObject = Array.isArray(options.props) ? {} : options.props || {};
const originalPropsAsObject = options.props || {};
camelizedPropsMap = camelizedPropsList.reduce((map, key, i) => {
map[key] = originalPropsAsObject[propsList[i]];
return map
Expand Down Expand Up @@ -169,13 +217,13 @@ function wrap (Vue, Component) {

class CustomElement extends HTMLElement {
constructor () {
super();
this.attachShadow({ mode: 'open' });
const self = super();
self.attachShadow({ mode: 'open' });

const wrapper = this._wrapper = new Vue({
const wrapper = self._wrapper = new Vue({
name: 'shadow-root',
customElement: this,
shadowRoot: this.shadowRoot,
customElement: self,
shadowRoot: self.shadowRoot,
data () {
return {
props: {},
Expand All @@ -195,20 +243,20 @@ function wrap (Vue, Component) {
let hasChildrenChange = false;
for (let i = 0; i < mutations.length; i++) {
const m = mutations[i];
if (isInitialized && m.type === 'attributes' && m.target === this) {
syncAttribute(this, m.attributeName);
if (isInitialized && m.type === 'attributes' && m.target === self) {
syncAttribute(self, m.attributeName);
} else {
hasChildrenChange = true;
}
}
if (hasChildrenChange) {
wrapper.slotChildren = Object.freeze(toVNodes(
wrapper.$createElement,
this.childNodes
self.childNodes
));
}
});
observer.observe(this, {
observer.observe(self, {
childList: true,
subtree: true,
characterData: true,
Expand Down
74 changes: 61 additions & 13 deletions dist/vue-wc-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,54 @@ function getAttributes (node) {
return res
}

function spreadProps (component) {
const result = {};
spreadNext(result, component);
return result
}

function spreadNext (result, component) {
if (component.props) {
appendProps(result, component.props);
}

if (component.mixins) {
component.mixins.forEach(function (mixin) {
spreadNext(result, mixin);
});
}
if (component.extends) {
spreadNext(result, component.extends);
}
}

function appendProps (result, props) {
if (Array.isArray(props)) {
processArrayProps(result, props);
} else {
processObjectProps(result, props);
}
}

function processObjectProps (result, props) {
for (const key in props) {
const camelKey = camelize(key);
if (!(camelKey in result)) {
result[camelKey] = props[key];
}
}
}
function processArrayProps (result, props) {
props.forEach(function (prop) {
if (typeof prop === 'string') {
const camelKey = camelize(prop);
if (!(camelKey in result)) {
result[camelKey] = undefined;
}
}
});
}

function wrap (Vue, Component) {
const isAsync = typeof Component === 'function' && !Component.cid;
let isInitialized = false;
Expand All @@ -109,13 +157,13 @@ function wrap (Vue, Component) {
? Component.options
: Component;

// spread props
options.props = spreadProps(options);
// extract props info
const propsList = Array.isArray(options.props)
? options.props
: Object.keys(options.props || {});
const propsList = Object.keys(options.props || {});
hyphenatedPropsList = propsList.map(hyphenate);
camelizedPropsList = propsList.map(camelize);
const originalPropsAsObject = Array.isArray(options.props) ? {} : options.props || {};
const originalPropsAsObject = options.props || {};
camelizedPropsMap = camelizedPropsList.reduce((map, key, i) => {
map[key] = originalPropsAsObject[propsList[i]];
return map
Expand Down Expand Up @@ -166,13 +214,13 @@ function wrap (Vue, Component) {

class CustomElement extends HTMLElement {
constructor () {
super();
this.attachShadow({ mode: 'open' });
const self = super();
self.attachShadow({ mode: 'open' });

const wrapper = this._wrapper = new Vue({
const wrapper = self._wrapper = new Vue({
name: 'shadow-root',
customElement: this,
shadowRoot: this.shadowRoot,
customElement: self,
shadowRoot: self.shadowRoot,
data () {
return {
props: {},
Expand All @@ -192,20 +240,20 @@ function wrap (Vue, Component) {
let hasChildrenChange = false;
for (let i = 0; i < mutations.length; i++) {
const m = mutations[i];
if (isInitialized && m.type === 'attributes' && m.target === this) {
syncAttribute(this, m.attributeName);
if (isInitialized && m.type === 'attributes' && m.target === self) {
syncAttribute(self, m.attributeName);
} else {
hasChildrenChange = true;
}
}
if (hasChildrenChange) {
wrapper.slotChildren = Object.freeze(toVNodes(
wrapper.$createElement,
this.childNodes
self.childNodes
));
}
});
observer.observe(this, {
observer.observe(self, {
childList: true,
subtree: true,
characterData: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vue/web-component-wrapper",
"version": "1.2.0",
"version": "1.3.0",
"description": "wrap a vue component as a web component.",
"main": "dist/vue-wc-wrapper.js",
"unpkg": "dist/vue-wc-wrapper.global.js",
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
injectHook,
getInitialProps,
createCustomEvent,
convertAttributeValue
convertAttributeValue,
spreadProps
} from './utils.js'

export default function wrap (Vue, Component) {
Expand All @@ -23,13 +24,13 @@ export default function wrap (Vue, Component) {
? Component.options
: Component

// spread props
options.props = spreadProps(options)
// extract props info
const propsList = Array.isArray(options.props)
? options.props
: Object.keys(options.props || {})
const propsList = Object.keys(options.props || {})
hyphenatedPropsList = propsList.map(hyphenate)
camelizedPropsList = propsList.map(camelize)
const originalPropsAsObject = Array.isArray(options.props) ? {} : options.props || {}
const originalPropsAsObject = options.props || {}
camelizedPropsMap = camelizedPropsList.reduce((map, key, i) => {
map[key] = originalPropsAsObject[propsList[i]]
return map
Expand Down
49 changes: 49 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,52 @@ function getAttributes (node) {
}
return res
}

export function spreadProps (component) {
const result = {}
spreadNext(result, component)
return result
}

function spreadNext (result, component) {
if (component.props) {
appendProps(result, component.props)
}

if (component.mixins) {
component.mixins.forEach(function (mixin) {
spreadNext(result, mixin)
})
}
if (component.extends) {
spreadNext(result, component.extends)
}
}

function appendProps (result, props) {
if (Array.isArray(props)) {
processArrayProps(result, props)
} else {
processObjectProps(result, props)
}
}

function processObjectProps (result, props) {
for (const key in props) {
const camelKey = camelize(key)
if (!(camelKey in result)) {
result[camelKey] = props[key]
}
}
}
function processArrayProps (result, props) {
props.forEach(function (prop) {
if (typeof prop === 'string') {
const camelKey = camelize(prop)
if (!(camelKey in result)) {
result[camelKey] = undefined
}
}
})
}

Loading