Skip to content

Commit

Permalink
refactor: split modules
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 13, 2022
1 parent e7ad827 commit f8ff4eb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 50 deletions.
4 changes: 3 additions & 1 deletion packages/preset-mini/src/variants/default.ts
Expand Up @@ -5,7 +5,9 @@ import { variantBreakpoints } from './breakpoints'
import { variantCombinators } from './combinators'
import { variantColorsMediaOrClass } from './dark'
import { variantLanguageDirections } from './directions'
import { variantCssLayer, variantImportant, variantInternalLayer, variantNegative, variantScope, variantSelector } from './misc'
import { variantCssLayer, variantInternalLayer, variantScope, variantSelector } from './misc'
import { variantNegative } from './negative'
import { variantImportant } from './important'
import { variantCustomMedia, variantPrint } from './media'
import { partClasses, variantPseudoClassFunctions, variantPseudoClassesAndElements, variantTaggedPseudoClasses } from './pseudo'

Expand Down
21 changes: 21 additions & 0 deletions packages/preset-mini/src/variants/important.ts
@@ -0,0 +1,21 @@
import type { Variant } from '@unocss/core'

export const variantImportant: Variant = {
name: 'important',
match(matcher) {
const match = matcher.match(/^(important[:-]|!)/)
if (match) {
return {
matcher: matcher.slice(match[0].length),
body: (body) => {
body.forEach((v) => {
if (v[1])
v[1] += ' !important'
})
return body
},
}
}
},
autocomplete: '(important)',
}
2 changes: 2 additions & 0 deletions packages/preset-mini/src/variants/index.ts
Expand Up @@ -7,3 +7,5 @@ export * from './default'
export * from './directions'
export * from './misc'
export * from './pseudo'
export * from './important'
export * from './negative'
49 changes: 0 additions & 49 deletions packages/preset-mini/src/variants/misc.ts
@@ -1,5 +1,4 @@
import type { Variant } from '@unocss/core'
import { CONTROL_MINI_NO_NEGATIVE } from '../utils'

export const variantSelector: Variant = {
name: 'selector',
Expand Down Expand Up @@ -53,51 +52,3 @@ export const variantScope: Variant = {
},
}

export const variantImportant: Variant = {
name: 'important',
match(matcher) {
const match = matcher.match(/^(important[:-]|!)/)
if (match) {
return {
matcher: matcher.slice(match[0].length),
body: (body) => {
body.forEach((v) => {
if (v[1])
v[1] += ' !important'
})
return body
},
}
}
},
autocomplete: '(important)',
}

const numberRE = /[0-9.]+(?:[a-z]+|%)?/
export const variantNegative: Variant = {
name: 'negative',
match(matcher) {
if (!matcher.startsWith('-'))
return

return {
matcher: matcher.slice(1),
body: (body) => {
if (body.find(v => v[0] === CONTROL_MINI_NO_NEGATIVE))
return
let changed = false
body.forEach((v) => {
const value = v[1]?.toString()
if (!value || value === '0')
return
if (numberRE.test(value)) {
v[1] = value.replace(numberRE, i => `-${i}`)
changed = true
}
})
if (changed)
return body
},
}
},
}
31 changes: 31 additions & 0 deletions packages/preset-mini/src/variants/negative.ts
@@ -0,0 +1,31 @@
import type { Variant } from '@unocss/core'
import { CONTROL_MINI_NO_NEGATIVE } from '../utils'

const numberRE = /[0-9.]+(?:[a-z]+|%)?/
export const variantNegative: Variant = {
name: 'negative',
match(matcher) {
if (!matcher.startsWith('-'))
return

return {
matcher: matcher.slice(1),
body: (body) => {
if (body.find(v => v[0] === CONTROL_MINI_NO_NEGATIVE))
return
let changed = false
body.forEach((v) => {
const value = v[1]?.toString()
if (!value || value === '0')
return
if (numberRE.test(value)) {
v[1] = value.replace(numberRE, i => `-${i}`)
changed = true
}
})
if (changed)
return body
},
}
},
}

0 comments on commit f8ff4eb

Please sign in to comment.