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): update bg-position #1020

Merged
merged 3 commits into from May 29, 2022
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
8 changes: 7 additions & 1 deletion packages/preset-mini/src/utils/handlers/handlers.ts
Expand Up @@ -93,11 +93,12 @@ export function fraction(str: string) {
return `${round(num * 100)}%`
}

const bracketTypeRe = /^\[(color|length|position):/i
function bracketWithType(str: string, type?: string) {
if (str && str.startsWith('[') && str.endsWith(']')) {
let base: string | undefined

const match = str.match(/^\[(color|length|position):/i)
const match = str.match(bracketTypeRe)
if (!match)
base = str.slice(1, -1)
else if (type && match[1] === type)
Expand Down Expand Up @@ -164,3 +165,8 @@ export function properties(str: string) {
if (str.split(',').every(prop => cssProps.includes(prop)))
return str
}

export function position(str: string) {
if (['top', 'left', 'right', 'bottom', 'center'].includes(str))
return str
}
15 changes: 9 additions & 6 deletions packages/preset-wind/src/rules/background.ts
Expand Up @@ -39,14 +39,17 @@ const bgGradientColorResolver = (mode: 'from' | 'to' | 'via') =>
}
}

const bgUrlRE = /^\[url\(.+\)\]$/
const bgLengthRE = /^\[length:.+\]$/
const bgPositionRE = /^\[position:.+\]$/
export const backgroundStyles: Rule[] = [
[/^bg-(.*)$/, ([, d]) => {
if (/^\[url\((.+)\)\]$/.test(d))
return { '--un-url': `${h.bracket(d)}`, 'background-image': 'var(--un-url)' }
else if (/^\[length:(.+)\]$/.test(d) && h.bracketOfLength(d) != null)
[/^bg-(.+)$/, ([, d]) => {
if (bgUrlRE.test(d))
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)).join(' ') }
else if (/^\[position:(.+)\]$/.test(d) && h.bracketOfPosition(d) != null)
return { 'background-position': h.bracketOfPosition(d)!.split(' ').map(e => h.fraction.auto.px.cssvar(e)).join(' ') }
if (bgPositionRE.test(d) && h.bracketOfPosition(d) != null)
return { 'background-position': h.bracketOfPosition(d)!.split(' ').map(e => h.position.fraction.auto.px.cssvar(e)).join(' ') }
}],

// gradients
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/preset-wind.test.ts.snap
Expand Up @@ -191,6 +191,8 @@ exports[`preset-wind > targets 1`] = `
.bg-\\\\[length\\\\:10_20rem\\\\]{background-size:10px 20rem;}
.bg-\\\\[position\\\\:1\\\\/2_20rem\\\\]{background-position:50% 20rem;}
.bg-\\\\[position\\\\:10_20rem\\\\]{background-position:10px 20rem;}
.bg-\\\\[position\\\\:bottom_left_10\\\\%\\\\]{background-position:bottom left 10%;}
.bg-\\\\[position\\\\:top_right_1\\\\/3\\\\]{background-position:top right 33.3333333333%;}
.bg-gradient-\\\\[70deg\\\\,blue\\\\,pink\\\\]{--un-gradient:70deg,blue,pink;}
.bg-gradient-stops-\\\\[blue\\\\,pink\\\\],
.stops-\\\\[blue\\\\,pink\\\\]{--un-gradient-stops:blue,pink;}
Expand Down
2 changes: 2 additions & 0 deletions test/assets/preset-wind-targets.ts
Expand Up @@ -64,6 +64,8 @@ export const presetWindTargets: string[] = [
'bg-[length:1/2_20rem]',
'bg-[position:10_20rem]',
'bg-[position:1/2_20rem]',
'bg-[position:bottom_left_10%]',
'bg-[position:top_right_1/3]',

// bg gradient
'from-current',
Expand Down