From bcbac21cc8f71e28b20f7ae5db1146d049761ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20F=C3=B6ldi?= <6977583+DaniFoldi@users.noreply.github.com> Date: Mon, 30 Sep 2024 21:50:13 +0100 Subject: [PATCH] feat: Add dark mode color overrides --- src/sanitizers.ts | 2 ++ src/simple-svg-placeholder.ts | 18 ++++++++++++++++++ src/utils.ts | 2 ++ .../simple-svg-placeholder.test.ts.snap | 4 ++++ test/simple-svg-placeholder.test.ts | 16 ++++++++++++++++ 5 files changed, 42 insertions(+) diff --git a/src/sanitizers.ts b/src/sanitizers.ts index 57d00c2..5140642 100644 --- a/src/sanitizers.ts +++ b/src/sanitizers.ts @@ -50,5 +50,7 @@ export const sanitizers = { fontSize: sanitizeNumber, bgColor: sanitizeColor, textColor: sanitizeColor, + darkBgColor: sanitizeColor, + darkTextColor: sanitizeColor, textWrap: sanitizeBoolean, }; diff --git a/src/simple-svg-placeholder.ts b/src/simple-svg-placeholder.ts index f965489..3f9aa39 100644 --- a/src/simple-svg-placeholder.ts +++ b/src/simple-svg-placeholder.ts @@ -12,6 +12,8 @@ export type Options = { lineHeight?: number; bgColor?: string; textColor?: string; + darkBgColor?: string; + darkTextColor?: string; dataUri?: boolean; charset?: string; textWrap?: boolean; @@ -30,12 +32,27 @@ export function simpleSvgPlaceholder({ dy = fontSize * 0.35, bgColor = '#ddd', textColor = 'rgba(0,0,0,0.5)', + darkBgColor, + darkTextColor, dataUri = true, charset = 'utf8', textWrap = false, padding = '0.5em', }: Options = {}) { let content = ''; + + let style = ''; + + if (darkBgColor || darkTextColor) { + style = ``; + } + if (textWrap) { content = `
+ ${style} ${content} `; diff --git a/src/utils.ts b/src/utils.ts index 53c6d21..d3b3099 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -64,6 +64,8 @@ export const availableImageOptions = [ 'fontSize', 'bgColor', 'textColor', + 'darkBgColor', + 'darkTextColor', 'textWrap', ] as const; diff --git a/test/__snapshots__/simple-svg-placeholder.test.ts.snap b/test/__snapshots__/simple-svg-placeholder.test.ts.snap index 27fddd0..8b44d3d 100644 --- a/test/__snapshots__/simple-svg-placeholder.test.ts.snap +++ b/test/__snapshots__/simple-svg-placeholder.test.ts.snap @@ -8,6 +8,10 @@ exports[`simpleSVGPlaceholder > should return accurate svg placeholder for { dat exports[`simpleSVGPlaceholder > should return accurate svg placeholder for { dataUri: false, width: 300, height: 300, text: 'Some super long string', textColor: 'rgba(255,255,255,0.5)', bgColor: 'rgba(0,0,0,0.5)', textWrap: true } 1`] = `"
Some super long string
"`; +exports[`simpleSVGPlaceholder > should return accurate svg placeholder for { dataUri: false, width: 1920, height: 1080, text: 'Dark mode background', darkBgColor: 'rgba(255,255,255,0.5)', bgColor: 'rgba(0,0,0,0.5)' } 1`] = `"Dark mode background"`; + +exports[`simpleSVGPlaceholder > should return accurate svg placeholder for { dataUri: false, width: 1920, height: 1080, text: 'Dark mode text', darkTextColor: 'rgba(255,255,255,0.5)', textColor: 'rgba(0,0,0,0.5)' } 1`] = `"Dark mode text"`; + exports[`simpleSVGPlaceholder > should return accurate svg placeholder for { dataUri: false, width: 1920, height: 1080, text: 'Hello World', textColor: '#fff', bgColor: '#000' } 1`] = `"Hello World"`; exports[`simpleSVGPlaceholder > should return accurate svg placeholder for { dataUri: false, width: 1920, height: 1080, text: 'Hello World', textColor: 'rgba(255,255,255,0.5)', bgColor: 'rgba(0,0,0,0.5)' } 1`] = `"Hello World"`; diff --git a/test/simple-svg-placeholder.test.ts b/test/simple-svg-placeholder.test.ts index 4010116..771228b 100644 --- a/test/simple-svg-placeholder.test.ts +++ b/test/simple-svg-placeholder.test.ts @@ -36,6 +36,22 @@ describe('simpleSVGPlaceholder', () => { textColor: 'rgba(255,255,255,0.5)', bgColor: 'rgba(0,0,0,0.5)', }, + { + dataUri: false, + width: 1920, + height: 1080, + text: 'Dark mode background', + darkBgColor: 'rgba(255,255,255,0.5)', + bgColor: 'rgba(0,0,0,0.5)', + }, + { + dataUri: false, + width: 1920, + height: 1080, + text: 'Dark mode text', + darkTextColor: 'rgba(255,255,255,0.5)', + textColor: 'rgba(0,0,0,0.5)', + }, { dataUri: false, width: 300,