Skip to content

Commit

Permalink
Merge pull request #41 from DaniFoldi/dark-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Cherry authored Sep 30, 2024
2 parents 7607f41 + bcbac21 commit 4c1de1d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/sanitizers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ export const sanitizers = {
fontSize: sanitizeNumber,
bgColor: sanitizeColor,
textColor: sanitizeColor,
darkBgColor: sanitizeColor,
darkTextColor: sanitizeColor,
textWrap: sanitizeBoolean,
};
18 changes: 18 additions & 0 deletions src/simple-svg-placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type Options = {
lineHeight?: number;
bgColor?: string;
textColor?: string;
darkBgColor?: string;
darkTextColor?: string;
dataUri?: boolean;
charset?: string;
textWrap?: boolean;
Expand All @@ -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 = `<style>
@media (prefers-color-scheme: dark) {
${darkBgColor ? `rect { fill: ${darkBgColor}; }` : ''}
${darkTextColor && !textWrap ? `text { fill: ${darkTextColor}; }` : ''}
${darkTextColor && textWrap ? `div { color: ${darkTextColor} !important; }` : ''}
}
</style>`;
}

if (textWrap) {
content = `<foreignObject width="${width}" height="${height}">
<div xmlns="http://www.w3.org/1999/xhtml" style="
Expand All @@ -59,6 +76,7 @@ export function simpleSvgPlaceholder({
}

const str = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">
${style}
<rect fill="${bgColor}" width="${width}" height="${height}"/>
${content}
</svg>`;
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export const availableImageOptions = [
'fontSize',
'bgColor',
'textColor',
'darkBgColor',
'darkTextColor',
'textWrap',
] as const;

Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/simple-svg-placeholder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `"<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 300 300"><rect fill="rgba(0,0,0,0.5)" width="300" height="300"/><foreignObject width="300" height="300"><div xmlns="http://www.w3.org/1999/xhtml" style="align-items: center;box-sizing: border-box;color: rgba(255,255,255,0.5);display: flex;font-family: sans-serif;font-size: 60px;font-weight: bold;height: 100%;line-height: 1.2;justify-content: center;padding: 0.5em;text-align: center;width: 100%;">Some super long string</div> </foreignObject></svg>"`;

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`] = `"<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080" viewBox="0 0 1920 1080"><style> @media (prefers-color-scheme: dark) { rect { fill: rgba(255,255,255,0.5); } } </style><rect fill="rgba(0,0,0,0.5)" width="1920" height="1080"/><text fill="rgba(0,0,0,0.5)" font-family="sans-serif" font-size="216" dy="75.6" font-weight="bold" x="50%" y="50%" text-anchor="middle">Dark mode background</text></svg>"`;

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`] = `"<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080" viewBox="0 0 1920 1080"><style> @media (prefers-color-scheme: dark) { text { fill: rgba(255,255,255,0.5); } } </style><rect fill="#ddd" width="1920" height="1080"/><text fill="rgba(0,0,0,0.5)" font-family="sans-serif" font-size="216" dy="75.6" font-weight="bold" x="50%" y="50%" text-anchor="middle">Dark mode text</text></svg>"`;

exports[`simpleSVGPlaceholder > should return accurate svg placeholder for { dataUri: false, width: 1920, height: 1080, text: 'Hello World', textColor: '#fff', bgColor: '#000' } 1`] = `"<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080" viewBox="0 0 1920 1080"><rect fill="#000" width="1920" height="1080"/><text fill="#fff" font-family="sans-serif" font-size="216" dy="75.6" font-weight="bold" x="50%" y="50%" text-anchor="middle">Hello World</text></svg>"`;

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`] = `"<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080" viewBox="0 0 1920 1080"><rect fill="rgba(0,0,0,0.5)" width="1920" height="1080"/><text fill="rgba(255,255,255,0.5)" font-family="sans-serif" font-size="216" dy="75.6" font-weight="bold" x="50%" y="50%" text-anchor="middle">Hello World</text></svg>"`;
Expand Down
16 changes: 16 additions & 0 deletions test/simple-svg-placeholder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4c1de1d

Please sign in to comment.