Skip to content

Commit

Permalink
✨ Support transparent background
Browse files Browse the repository at this point in the history
  • Loading branch information
wei committed Dec 14, 2024
1 parent e1dc2a5 commit 9d26097
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-mirrors-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"socialify": minor
---

Support transparent background
31 changes: 29 additions & 2 deletions common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
plus,
signal,
} from 'hero-patterns'
import type { CSSProperties } from 'react'
import {
type SimpleIcon,
siApachegroovy,
Expand Down Expand Up @@ -140,7 +141,7 @@ const getSimpleIconsImageURI = function (language: string, theme: Theme) {
return `data:image/svg+xml,${encodeURIComponent(iconSvg)}`
}

const getHeroPattern = (pattern: Pattern, theme: Theme) => {
const getHeroPattern = (pattern: Pattern, theme: Theme): CSSProperties => {
const PATTERN_FUNCTIONS_MAPPING: { [key: string]: any } = {
[Pattern.signal]: signal,
[Pattern.charlieBrown]: charlieBrown,
Expand All @@ -152,9 +153,15 @@ const getHeroPattern = (pattern: Pattern, theme: Theme) => {
[Pattern.floatingCogs]: floatingCogs,
[Pattern.diagonalStripes]: diagonalStripes,
[Pattern.solid]: null,
[Pattern.transparent]: null,
}
const patternFunction = PATTERN_FUNCTIONS_MAPPING[pattern]
const themedBackgroundColor = theme === Theme.dark ? '#000' : '#fff'
const themedBackgroundColor =
pattern === Pattern.transparent
? 'transparent'
: theme === Theme.dark
? '#000'
: '#fff'

if (!patternFunction) {
return {
Expand Down Expand Up @@ -185,6 +192,25 @@ const getHeroPattern = (pattern: Pattern, theme: Theme) => {
}
}

const getChessBoardPattern = (theme: Theme): CSSProperties => {
const chessPatternColors = {
light: ['#fff', '#ccc'],
dark: ['#2f2f2f', '#000'],
}[theme === 'Dark' ? 'dark' : 'light']

return {
backgroundImage: `url('data:image/svg+xml,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" shape-rendering="crispEdges">
<rect width="10" height="10" fill="${chessPatternColors[0]}"/>
<rect x="10" width="10" height="10" fill="${chessPatternColors[1]}"/>
<rect y="10" width="10" height="10" fill="${chessPatternColors[1]}"/>
<rect x="10" y="10" width="10" height="10" fill="${chessPatternColors[0]}"/>
</svg>`.replace(/\n\s+/g, '')
)}`,
backgroundRepeat: 'repeat',
}
}

let webpSupport: boolean | undefined
const checkWebpSupport = (): boolean => {
if (webpSupport !== undefined) {
Expand Down Expand Up @@ -229,6 +255,7 @@ const version = packageJson.version
export {
getSimpleIconsImageURI,
getHeroPattern,
getChessBoardPattern,
checkWebpSupport,
HOST_PREFIX,
autoThemeCss,
Expand Down
1 change: 1 addition & 0 deletions common/types/configType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum Pattern {
floatingCogs = 'Floating Cogs',
diagonalStripes = 'Diagonal Stripes',
solid = 'Solid',
transparent = 'Transparent',
}

enum Font {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"test:e2e:update-snapshot": "playwright test --update-snapshots",
"test": "pnpm test:unit && pnpm test:e2e",
"start": "next start",
"lint": "biome ci --max-diagnostics=999 .",
"lint:fix": "biome check --write --verbose --max-diagnostics=999 .",
"lint:fix-unsafe": "biome check --write-unsafe --verbose --max-diagnostics=999 .",
"lint": "biome ci .",
"lint:fix": "biome check --write --verbose .",
"lint:fix-unsafe": "biome check --write-unsafe --verbose .",
"ncu": "npx npm-check-updates -u",
"verify": "pnpm lint && pnpm test && pnpm build",
"download-font": "./fonts/download-font.sh",
Expand Down
8 changes: 7 additions & 1 deletion src/components/preview/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import Router from 'next/router'
import React, { useContext } from 'react'
import { MdContentCopy, MdDownload } from 'react-icons/md'

import { checkWebpSupport } from '@/common/helpers'
import { checkWebpSupport, getChessBoardPattern } from '@/common/helpers'
import { Pattern } from '@/common/types/configType'
import Card from '@/src/components/preview/card'
import toaster from '@/src/components/toaster'
import ConfigContext from '@/src/contexts/ConfigContext'
Expand Down Expand Up @@ -109,6 +110,11 @@ const Preview: React.FC = () => {
'min-[640px]:w-[640px] min-[640px]:h-[320px]'
)}
onClick={copyImageUrl}
style={
config.pattern === Pattern.transparent
? getChessBoardPattern(config.theme)
: undefined
}
>
<div
className={classnames(
Expand Down

0 comments on commit 9d26097

Please sign in to comment.