Skip to content

Commit

Permalink
feat: support transform-xyz with brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
MellowCo committed Apr 5, 2024
1 parent d7d923f commit a0e5da2
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/rules/transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CSSValues, Rule, RuleContext } from '@unocss/core'
import { cacheRestoreSelector } from 'unplugin-transform-class/utils'
import type { Theme } from '../theme'
import { handler as h, makeGlobalStaticRules, positionMap, xyzMap } from '../utils'
import { handler as h, makeGlobalStaticRules, positionMap, transformXYZ, xyzMap } from '../utils'

const transformValues = [
'translate',
Expand Down Expand Up @@ -106,7 +106,7 @@ function handleTranslate([, d, b]: string[], { theme }: RuleContext<Theme>): CSS
const v = theme.spacing?.[b] ?? h.bracket.cssvar.fraction.remToRpx(b)
if (v != null) {
return [
...xyzMap[d].map((i): [string, string] => [`--un-translate${i}`, v]),
...transformXYZ(d, v, 'scale'),

This comment has been minimized.

Copy link
@qinains

qinains Apr 9, 2024

这里写错了吧?应该是“...transformXYZ(d, v, 'translate')”

This comment has been minimized.

Copy link
@qinains

qinains Apr 9, 2024

unocss 0.59.0官方的<view class="translate-x-3">bg</view>得到

.translate-x-3{
--un-translate-x:0.75rem;
transform:translateX(var(--un-translate-x)) translateY(var(--un-translate-y)) translateZ(var(--un-translate-z)) rotate(var(--un-rotate)) rotateX(var(--un-rotate-x)) rotateY(var(--un-rotate-y)) rotateZ(var(--un-rotate-z)) skewX(var(--un-skew-x)) skewY(var(--un-skew-y)) scaleX(var(--un-scale-x)) scaleY(var(--un-scale-y)) scaleZ(var(--un-scale-z));}

而你这个版本得到

.translate-x-3{
--un-scale-x: 0.75rem;
transform: translateX(var(--un-translate-x)) translateY(var(--un-translate-y)) translateZ(var(--un-translate-z)) rotate(var(--un-rotate)) rotateX(var(--un-rotate-x)) rotateY(var(--un-rotate-y)) rotateZ(var(--un-rotate-z)) skewX(var(--un-skew-x)) skewY(var(--un-skew-y)) scaleX(var(--un-scale-x)) scaleY(var(--un-scale-y)) scaleZ(var(--un-scale-z));
}

This comment has been minimized.

Copy link
@MellowCo

MellowCo Apr 9, 2024

Author Owner

fix it

['transform', transformCpu],
]
}
Expand Down Expand Up @@ -150,7 +150,7 @@ function handleSkew([, d, b]: string[]): CSSValues | undefined {
const v = h.bracket.cssvar.degree(b)
if (v != null) {
return [
...xyzMap[d].map((i): [string, string] => [`--un-skew${i}`, v]),
...transformXYZ(d, v, 'skew'),
['transform', transformCpu],
]
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/handlers/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const numberWithUnitRE = /^(-?\d*(?:\.\d+)?)(px|pt|pc|%|r?(?:em|ex|lh|cap
export const numberRE = /^(-?[0-9.]+)$/i
export const unitOnlyRE = /^(px)$/i
export const bracketTypeRe = /^\[(color|length|size|position|quoted|string):/i
export const splitComma = /,(?![^()]*\))/g
2 changes: 2 additions & 0 deletions src/utils/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export const xyzMap: Record<string, string[]> = {
'': ['-x', '-y'],
}

export const xyzArray = ['x', 'y', 'z']

const basePositionMap = [
'top',
'top center',
Expand Down
12 changes: 10 additions & 2 deletions src/utils/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { Theme } from '../theme'
import { colorOpacityToString, colorToString, parseCssColor } from './colors'

import { handler as h } from './handlers'
import { cssMathFnRE, directionMap, globalKeywords } from './mappings'
import { bracketTypeRe, numberWithUnitRE } from './handlers/regex'
import { cssMathFnRE, directionMap, globalKeywords, xyzArray, xyzMap } from './mappings'
import { bracketTypeRe, numberWithUnitRE, splitComma } from './handlers/regex'

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

Expand Down Expand Up @@ -398,3 +398,11 @@ export function isSize(str: string) {
str = str.slice(1, -1)
return cssMathFnRE.test(str) || numberWithUnitRE.test(str)
}

export function transformXYZ(d: string, v: string, name: string): [string, string][] {
const values: string[] = v.split(splitComma)
if (d || (!d && values.length === 1))
return xyzMap[d].map((i): [string, string] => [`--un-${name}${i}`, v])

return values.map((v, i) => [`--un-${name}-${xyzArray[i]}`, v])
}
38 changes: 22 additions & 16 deletions test/assets/output/preset-mini/targets.css

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions test/assets/output/preset-weapp/targets-custom-rules.css

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions test/assets/output/preset-weapp/targets.css

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions test/assets/preset-mini-targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,8 @@ export const presetMiniTargets: string[] = [
'transform-unset',
'translate-none',
'translate-y-1/4',
'translate-[var(--x),var(--y)]',
'translate-[var(--x),var(--y),25%]',
'translate-y-px',
'translate-full',
'translate-x-full',
Expand All @@ -955,6 +957,8 @@ export const presetMiniTargets: string[] = [
'transform-rotate-z-[var(--spin)]',
'rotate-z-[var(--spin)]',
'skew-10',
'skew-[var(--x),var(--y)]',
'skew-[var(--x),var(--y),25%]',
'skew-x-10',
'skew-x-10deg',
'skew-x-10.00deg',
Expand Down Expand Up @@ -1114,6 +1118,8 @@ export const presetMiniTargets: string[] = [
'rotate-x-$variable',
'skew-x-$variable',
'scale-$variable',
'scale-[var(--x),var(--y)]',
'scale-[var(--x),var(--y),20%]',
'scale-x-$variable',
'transition-delay-$variable',
'transition-duration-$variable',
Expand Down

0 comments on commit a0e5da2

Please sign in to comment.