Skip to content

Commit

Permalink
feat(preset-wind): support bg-[size] -> background-position (#3270)
Browse files Browse the repository at this point in the history
Co-authored-by: chris <hizyyv@gmail.com>
  • Loading branch information
Simon-He95 and zyyv committed Nov 2, 2023
1 parent c88fb67 commit 9e891ab
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 116 deletions.
4 changes: 2 additions & 2 deletions packages/preset-mini/src/_rules/color.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Rule } from '@unocss/core'
import { colorResolver, globalKeywords, h } from '../utils'
import { colorResolver, globalKeywords, h, isSize } from '../utils'
import { numberWithUnitRE } from '../_utils/handlers/regex'

/**
Expand All @@ -21,7 +21,7 @@ export const textColors: Rule[] = [
]

export const bgColors: Rule[] = [
[/^bg-(.+)$/, colorResolver('background-color', 'bg'), { autocomplete: 'bg-$colors' }],
[/^bg-(.+)$/, (...args) => isSize(args[0][1]) ? undefined : colorResolver('background-color', 'bg')(...args), { autocomplete: 'bg-$colors' }],
[/^bg-op(?:acity)?-?(.+)$/, ([, opacity]) => ({ '--un-bg-opacity': h.bracket.percent.cssvar(opacity) }), { autocomplete: 'bg-(op|opacity)-<percent>' }],
]

Expand Down
2 changes: 1 addition & 1 deletion packages/preset-mini/src/_utils/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ export const globalKeywords = [
'unset',
]

export const cssMathFnRE = /^(?:calc|clamp|min|max)\s*\(.*\)/
export const cssMathFnRE = /(?:calc|clamp|min|max)\s*\(.*\)/
7 changes: 7 additions & 0 deletions packages/preset-mini/src/_utils/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { colorOpacityToString, colorToString, getStringComponents, parseCssColor
import type { Theme } from '../theme'
import { h } from './handlers'
import { cssMathFnRE, directionMap, globalKeywords } from './mappings'
import { numberWithUnitRE } from './handlers/regex'

export const CONTROL_MINI_NO_NEGATIVE = '$$mini-no-negative'

Expand Down Expand Up @@ -244,3 +245,9 @@ export function makeGlobalStaticRules(prefix: string, property?: string): Static
export function isCSSMathFn(value: string) {
return cssMathFnRE.test(value)
}

export function isSize(str: string) {
if (str[0] === '[' && str.slice(-1) === ']')
str = str.slice(1, -1)
return cssMathFnRE.test(str) || numberWithUnitRE.test(str)
}
4 changes: 2 additions & 2 deletions packages/preset-wind/src/rules/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CSSColorValue, Rule, RuleContext } from '@unocss/core'
import { globalKeywords, h, makeGlobalStaticRules, parseColor, positionMap } from '@unocss/preset-mini/utils'
import { globalKeywords, h, isSize, makeGlobalStaticRules, parseColor, positionMap } from '@unocss/preset-mini/utils'
import type { Theme } from '@unocss/preset-mini'
import { colorOpacityToString, colorToString } from '@unocss/rule-utils'

Expand Down Expand Up @@ -76,7 +76,7 @@ export const backgroundStyles: Rule[] = [
return { '--un-url': h.bracket(d), 'background-image': 'var(--un-url)' }
if (bgLengthRE.test(d) && h.bracketOfLength(d) != null)
return { 'background-size': h.bracketOfLength(d)!.split(' ').map(e => h.fraction.auto.px.cssvar(e) ?? e).join(' ') }
if (bgPositionRE.test(d) && h.bracketOfPosition(d) != null)
if ((isSize(d) || bgPositionRE.test(d)) && h.bracketOfPosition(d) != null)
return { 'background-position': h.bracketOfPosition(d)!.split(' ').map(e => h.position.fraction.auto.px.cssvar(e) ?? e).join(' ') }
}],

Expand Down

0 comments on commit 9e891ab

Please sign in to comment.