Skip to content

Commit f719111

Browse files
committed
fix(module): safelist aliases for input
To make it work when doing `<USelect color="yellow" />` for example
1 parent 3bac087 commit f719111

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/colors.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ const safelistByComponent = {
9292
variants: ['focus-visible']
9393
}],
9494
input: (colorsAsRegex) => [{
95+
pattern: new RegExp(`text-(${colorsAsRegex})-400`),
96+
variants: ['dark']
97+
}, {
98+
pattern: new RegExp(`text-(${colorsAsRegex})-500`)
99+
}, {
95100
pattern: new RegExp(`ring-(${colorsAsRegex})-400`),
96101
variants: ['dark', 'dark:focus']
97102
}, {
@@ -111,6 +116,12 @@ const safelistByComponent = {
111116
}]
112117
}
113118

119+
const safelistComponentAliasesMap = {
120+
'USelect': 'UInput',
121+
'USelectMenu': 'UInput',
122+
'UTextarea': 'UInput'
123+
}
124+
114125
const colorsAsRegex = (colors: string[]): string => colors.join('|')
115126

116127
export const excludeColors = (colors: object) => Object.keys(omit(colors, colorsToExclude)).map(color => kebabCase(color)) as string[]
@@ -133,29 +144,35 @@ export const customSafelistExtractor = (prefix, content: string, colors: string[
133144
const regex = /<(\w+)\s+[^>:]*color=["']([^"']+)["'][^>]*>/gs
134145
const matches = content.matchAll(regex)
135146

147+
const components = Object.keys(safelistByComponent).map(component => `${prefix}${component.charAt(0).toUpperCase() + component.slice(1)}`)
148+
136149
for (const match of matches) {
137150
const [, component, color] = match
138151

139152
if (!colors.includes(color)) {
140153
continue
141154
}
142155

143-
if (Object.keys(safelistByComponent).map(component => `${prefix}${component.charAt(0).toUpperCase() + component.slice(1)}`).includes(component)) {
144-
const name = component.replace(prefix, '').toLowerCase()
156+
let name = safelistComponentAliasesMap[component] ? safelistComponentAliasesMap[component] : component
157+
158+
if (!components.includes(name)) {
159+
continue
160+
}
161+
162+
name = name.replace(prefix, '').toLowerCase()
145163

146-
const matchClasses = safelistByComponent[name](color).flatMap(group => {
147-
return ['', ...(group.variants || [])].flatMap(variant => {
148-
const matches = group.pattern.source.match(/\(([^)]+)\)/g)
164+
const matchClasses = safelistByComponent[name](color).flatMap(group => {
165+
return ['', ...(group.variants || [])].flatMap(variant => {
166+
const matches = group.pattern.source.match(/\(([^)]+)\)/g)
149167

150-
return matches.map(match => {
151-
const colorOptions = match.substring(1, match.length - 1).split('|')
152-
return colorOptions.map(color => `${variant ? variant + ':' : ''}` + group.pattern.source.replace(match, color))
153-
}).flat()
154-
})
168+
return matches.map(match => {
169+
const colorOptions = match.substring(1, match.length - 1).split('|')
170+
return colorOptions.map(color => `${variant ? variant + ':' : ''}` + group.pattern.source.replace(match, color))
171+
}).flat()
155172
})
173+
})
156174

157-
classes.push(...matchClasses)
158-
}
175+
classes.push(...matchClasses)
159176
}
160177

161178
return classes

0 commit comments

Comments
 (0)