diff --git a/index.d.ts b/index.d.ts index 5414c29..e7e80fc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,5 @@ declare namespace ReactWebComponent { - export const create: (app: JSX.Element, tagName: string, optOutFromShadowRoot?: boolean) => void; + export const create: (app: JSX.Element, tagName: string, optOutFromShadowRoot?: boolean, observedAttributes?: string[]) => void; } export default ReactWebComponent; diff --git a/src/camelCasedAttribute.js b/src/camelCasedAttribute.js new file mode 100644 index 0000000..b1e7c80 --- /dev/null +++ b/src/camelCasedAttribute.js @@ -0,0 +1 @@ +"use strict";module.exports=function(a){return a.replace(/-([a-z])/g,function(a){return a[1].toUpperCase()})}; \ No newline at end of file diff --git a/src/dev/camelCasedAttribute.js b/src/dev/camelCasedAttribute.js new file mode 100644 index 0000000..6f39280 --- /dev/null +++ b/src/dev/camelCasedAttribute.js @@ -0,0 +1,8 @@ +/** + * Takes in a node attributes map and returns an object with camelCased properties and values + * @param attribute + * @returns {{}} + */ +module.exports = function camelCasedAttribute(attribute) { + return attribute.replace(/-([a-z])/g, (g) => g[1].toUpperCase()) +}; diff --git a/src/dev/extractAttributes.js b/src/dev/extractAttributes.js index 4205642..f6bfa5c 100644 --- a/src/dev/extractAttributes.js +++ b/src/dev/extractAttributes.js @@ -1,3 +1,5 @@ +const camelCasedAttribute = require('./camelCasedAttribute'); + /** * Takes in a node attributes map and returns an object with camelCased properties and values * @param nodeMap @@ -15,7 +17,7 @@ module.exports = function extractAttributes(nodeMap) { for (attribute of attributes) { const key = Object.keys(attribute)[0]; - const camelCasedKey = key.replace(/-([a-z])/g, (g) => g[1].toUpperCase()); + const camelCasedKey = camelCasedAttribute(key); obj[camelCasedKey] = attribute[key]; } diff --git a/src/dev/index.js b/src/dev/index.js index b4ba315..37405b8 100644 --- a/src/dev/index.js +++ b/src/dev/index.js @@ -2,6 +2,7 @@ const React = require('react'); const ReactDOM = require('react-dom'); const retargetEvents = require('react-shadow-dom-retarget-events'); const getStyleElementsFromReactWebComponentStyleLoader = require('./getStyleElementsFromReactWebComponentStyleLoader'); +const camelCasedAttribute = require('./camelCasedAttribute'); const extractAttributes = require('./extractAttributes'); require('@webcomponents/shadydom'); @@ -12,8 +13,9 @@ module.exports = { * @param {JSX.Element} app * @param {string} tagName - The name of the web component. Has to be minus "-" delimited. * @param {boolean} useShadowDom - If the value is set to "true" the web component will use the `shadowDom`. The default value is true. + * @param {string[]} observedAttributes - The observed attributes of the web component */ - create: (app, tagName, useShadowDom = true) => { + create: (app, tagName, useShadowDom = true, observedAttributes = []) => { let appInstance; const lifeCycleHooks = { @@ -40,6 +42,9 @@ module.exports = { } const proto = class extends HTMLElement { + static get observedAttributes() { + return observedAttributes; + } connectedCallback() { const webComponentInstance = this; let mountPoint = webComponentInstance; @@ -73,7 +78,7 @@ module.exports = { callLifeCycleHook('disconnectedCallback'); } attributeChangedCallback (attributeName, oldValue, newValue, namespace) { - callLifeCycleHook('attributeChangedCallback', [attributeName, oldValue, newValue, namespace]); + callLifeCycleHook('attributeChangedCallback', [camelCasedAttribute(attributeName), oldValue, newValue, namespace]); } adoptedCallback (oldDocument, newDocument) { callLifeCycleHook('adoptedCallback', [oldDocument, newDocument]); diff --git a/src/extractAttributes.js b/src/extractAttributes.js index 89af623..d756663 100644 --- a/src/extractAttributes.js +++ b/src/extractAttributes.js @@ -1 +1 @@ -"use strict";function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toConsumableArray(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b