Skip to content

Commit

Permalink
Issue #4: Kebab case attributes never trigger the attribute changed c…
Browse files Browse the repository at this point in the history
…allback

Signed-off-by: macdonst <[email protected]>
  • Loading branch information
macdonst committed May 2, 2024
1 parent 79922e9 commit d9b18f6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ if (typeof process !== 'undefined') {
global.Worker = function() { return { postMessage: function() { } } }
}

function kebabToCamel(attribute) {
if (attribute.includes('-')) {
return attribute.split('-').map((word, index) => index !== 0 ? word.charAt(0). toUpperCase() + word.slice(1) : word
).join('')
} else {
return attribute
}
}

export default class BaseElement extends HTMLElement {
constructor() {
super()
Expand All @@ -31,7 +40,7 @@ export default class BaseElement extends HTMLElement {

attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) {
const fun = `${name}Changed`
const fun = `${kebabToCamel(name)}Changed`
if (this[fun]) {
this[fun](newValue)
}
Expand Down

0 comments on commit d9b18f6

Please sign in to comment.