Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preset-wind): support bg-[size] -> background-position #3270

Merged
merged 8 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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