From be8e6c3c7572b4e7074f7b0217400bd030dd20ab Mon Sep 17 00:00:00 2001 From: TheComputerM Date: Wed, 9 Sep 2020 18:43:37 +0530 Subject: [PATCH] feat: added new actions --- .../src/internal/BackgroundColor/index.js | 56 +++++++++++++++++++ .../src/internal/TextColor/index.js | 48 ++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 packages/svelte-materialify/src/internal/BackgroundColor/index.js create mode 100644 packages/svelte-materialify/src/internal/TextColor/index.js diff --git a/packages/svelte-materialify/src/internal/BackgroundColor/index.js b/packages/svelte-materialify/src/internal/BackgroundColor/index.js new file mode 100644 index 00000000..d5120f18 --- /dev/null +++ b/packages/svelte-materialify/src/internal/BackgroundColor/index.js @@ -0,0 +1,56 @@ +/* eslint-disable no-param-reassign */ + +const themeColors = ['primary', 'secondary', 'success', 'info', 'warning', 'error']; + +/** + * @param node {Element} + * @param text {string|boolean} + */ +export default (node, text) => { + let klass; + if (text) { + if (/^#([0-9A-F]{3}){1,2}$/i.test(text)) { + // This is a CSS hex. + node.style.color = text; + klass = false; + } else if (themeColors.includes(text)) { + klass = `${text}-color`; + node.classList.add(klass); + } else if (text.startsWith('--')) { + // This is a CSS variable. + node.style.color = `var(${text})`; + klass = false; + } else { + klass = text; + node.classList.add(klass); + } + } + + return { + update(newText) { + if (klass) { + node.classList.remove(klass); + } else { + node.style.color = null; + } + + if (newText) { + if (/^#([0-9A-F]{3}){1,2}$/i.test(newText)) { + // This is a CSS hex. + node.style.color = newText; + klass = false; + } else if (themeColors.includes(newText)) { + klass = `${newText}-color`; + node.classList.add(klass); + } else if (text.startsWith('--')) { + // This is a CSS variable. + node.style.color = `var(${newText})`; + klass = false; + } else { + klass = `${newText}-text`; + node.classList.add(klass); + } + } + }, + }; +}; diff --git a/packages/svelte-materialify/src/internal/TextColor/index.js b/packages/svelte-materialify/src/internal/TextColor/index.js new file mode 100644 index 00000000..64f3e72b --- /dev/null +++ b/packages/svelte-materialify/src/internal/TextColor/index.js @@ -0,0 +1,48 @@ +/* eslint-disable no-param-reassign */ + +/** + * @param node {Element} + * @param text {string|boolean} + */ +export default (node, text) => { + let klass; + if (text) { + if (/^#([0-9A-F]{3}){1,2}$/i.test(text)) { + // This is a CSS hex. + node.style.color = text; + klass = false; + } else if (text.startsWith('--')) { + // This is a CSS variable. + node.style.color = `var(${text})`; + klass = false; + } else { + klass = `${text}-text`; + node.classList.add(klass); + } + } + + return { + update(newText) { + if (klass) { + node.classList.remove(klass); + } else { + node.style.color = null; + } + + if (newText) { + if (/^#([0-9A-F]{3}){1,2}$/i.test(newText)) { + // This is a CSS hex. + node.style.color = newText; + klass = false; + } else if (text.startsWith('--')) { + // This is a CSS variable. + node.style.color = `var(${newText})`; + klass = false; + } else { + klass = `${newText}-text`; + node.classList.add(klass); + } + } + }, + }; +};