From d9b18f6aada8475b71c3028748c8b8675e43fc77 Mon Sep 17 00:00:00 2001 From: macdonst Date: Thu, 2 May 2024 11:10:48 -0400 Subject: [PATCH] Issue #4: Kebab case attributes never trigger the attribute changed callback Signed-off-by: macdonst --- index.mjs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.mjs b/index.mjs index 87e20d2..129dd7c 100644 --- a/index.mjs +++ b/index.mjs @@ -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() @@ -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) }