Skip to content

Commit

Permalink
fix(module): hardcode gray safelist instead of deduplicate complex …
Browse files Browse the repository at this point in the history
…logic
  • Loading branch information
benjamincanac committed Jun 13, 2023
1 parent 88c1930 commit a733c13
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions src/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const kebabCase = (str: string) => {

const safelistByComponent = {
avatar: (colorsAsRegex) => [{
pattern: new RegExp(`bg-(${colorsAsRegex}|gray)-500`)
pattern: new RegExp(`bg-(${colorsAsRegex})-500`)
}, {
pattern: new RegExp(`bg-(${colorsAsRegex}|gray)-400`),
pattern: new RegExp(`bg-(${colorsAsRegex})-400`),
variants: ['dark']
}],
badge: (colorsAsRegex) => [{
Expand Down Expand Up @@ -99,14 +99,14 @@ const safelistByComponent = {
variants: ['focus']
}],
notification: (colorsAsRegex) => [{
pattern: new RegExp(`bg-(${colorsAsRegex}|gray)-500`)
pattern: new RegExp(`bg-(${colorsAsRegex})-500`)
}, {
pattern: new RegExp(`bg-(${colorsAsRegex}|gray)-400`),
pattern: new RegExp(`bg-(${colorsAsRegex})-400`),
variants: ['dark']
}, {
pattern: new RegExp(`text-(${colorsAsRegex}|gray)-500`)
pattern: new RegExp(`text-(${colorsAsRegex})-500`)
}, {
pattern: new RegExp(`text-(${colorsAsRegex}|gray)-400`),
pattern: new RegExp(`text-(${colorsAsRegex})-400`),
variants: ['dark']
}]
}
Expand All @@ -115,27 +115,17 @@ const colorsAsRegex = (colors: string[]): string => colors.join('|')

export const excludeColors = (colors: object) => Object.keys(omit(colors, colorsToExclude)).map(color => kebabCase(color)) as string[]

const mergeSafelist = (safelist) => {
return safelist.reduce((result, current) => {
const found = result.find(obj => obj.pattern.toString() === current.pattern.toString())
if (found) {
if (Array.isArray(found.variants) && Array.isArray(current.variants)) {
const mergedVariants = [...new Set([...found.variants, ...current.variants])]
found.variants = mergedVariants
} else if (Array.isArray(current.variants)) {
found.variants = current.variants
}
} else {
result.push(current)
}
return result
}, [])
}

export const generateSafelist = (colors: string[]) => {
const safelist = ['avatar', 'badge', 'button', 'input', 'notification'].flatMap(component => safelistByComponent[component](colorsAsRegex(colors)))

return mergeSafelist(safelist)
return [
...safelist,
// Gray safelist for Avatar & Notification
'bg-gray-500',
'dark:bg-gray-400',
'text-gray-500',
'dark:text-gray-400'
]
}

export const customSafelistExtractor = (prefix, content: string, colors: string[]) => {
Expand Down

1 comment on commit a733c13

@vercel
Copy link

@vercel vercel bot commented on a733c13 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-git-dev-nuxtlabs.vercel.app
ui-nuxtlabs.vercel.app
ui.nuxtlabs.com

Please sign in to comment.