Skip to content

Commit

Permalink
Refactor preflights
Browse files Browse the repository at this point in the history
  • Loading branch information
kidonng committed Mar 29, 2023
1 parent f7195b0 commit 95bc2b8
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,55 +96,49 @@ export const presetDaisy = (
rules.delete('btn-outline')
rules.set('btn-outline', btnOutline)

const preflights: Preflight[] = [
{
// eslint-disable-next-line @typescript-eslint/naming-convention
getCSS: () =>
toCss(
Object.fromEntries(
Object.entries(themes)
.filter(([selector]) => {
const theme = (tokenize(selector)[0] as AttributeToken).value!

if (options.themes === false) return theme === 'light'
if (Array.isArray(options.themes))
return options.themes.includes(theme)

return true
})
.map(([selector, colors], index) => {
const theme = (tokenize(selector)[0] as AttributeToken).value!
const isDefault = Array.isArray(options.themes)
? index === 0
: theme === 'light'

return [
isDefault ? `:root, ${selector}` : selector,
Object.fromEntries(
Object.entries(colorFunctions.convertToHsl(colors)).map(
([prop, value]) => [prop, replaceSpace(value)],
),
),
]
}),
),
),
},
{
// eslint-disable-next-line @typescript-eslint/naming-convention
getCSS: () => keyframes.join('\n'),
},
const preflights: string[] = [
toCss(
Object.fromEntries(
Object.entries(themes)
.filter(([selector]) => {
const theme = (tokenize(selector)[0] as AttributeToken).value!

if (options.themes === false) return theme === 'light'
if (Array.isArray(options.themes))
return options.themes.includes(theme)

return true
})
.map(([selector, colors], index) => {
const theme = (tokenize(selector)[0] as AttributeToken).value!
const isDefault = Array.isArray(options.themes)
? index === 0
: theme === 'light'

return [
isDefault ? `:root, ${selector}` : selector,
Object.fromEntries(
Object.entries(colorFunctions.convertToHsl(colors)).map(
([prop, value]) => [prop, replaceSpace(value)],
),
),
]
}),
),
),
...keyframes,
]

if (options.base)
preflights.push({
// eslint-disable-next-line @typescript-eslint/naming-convention
getCSS: () => replaceSlash(replacePrefix(toCss(base))),
})
if (options.base) preflights.unshift(replaceSlash(replacePrefix(toCss(base))))

return {
name: 'unocss-preset-daisy',
preflights,
preflights: [
{
// eslint-disable-next-line @typescript-eslint/naming-convention
getCSS: () => preflights.join('\n'),
},
],
theme: {
colors: {
...Object.fromEntries(
Expand Down

0 comments on commit 95bc2b8

Please sign in to comment.