Skip to content

Commit

Permalink
feat(transformer-directives): theme() support color transparency (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Sep 19, 2023
1 parent 52a6ee0 commit cbfc2b3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/transformer-directives/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"@unocss/core": "workspace:*",
"@unocss/rule-utils": "workspace:*",
"css-tree": "^2.3.1"
},
"devDependencies": {
Expand Down
10 changes: 9 additions & 1 deletion packages/transformer-directives/src/theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Declaration } from 'css-tree'
import { colorToString, parseCssColor } from '@unocss/rule-utils'
import type { TransformerDirectivesContext } from '.'

export const themeFnRE = /theme\((.*?)\)/g
Expand All @@ -18,8 +19,9 @@ export function handleThemeFn({ code, uno, options }: TransformerDirectivesConte
if (!rawArg)
throw new Error('theme() expect exact one argument, but got 0')

const [rawKey, alpha] = rawArg.slice(1, -1).split('/') as [string, string?]
let value: any = uno.config.theme
const keys = rawArg.slice(1, -1).split('.')
const keys = rawKey.trim().split('.')

keys.every((key) => {
if (value[key] != null)
Expand All @@ -32,6 +34,12 @@ export function handleThemeFn({ code, uno, options }: TransformerDirectivesConte
})

if (typeof value === 'string') {
if (alpha) {
const color = parseCssColor(value)
if (color)
value = colorToString(color, alpha)
}

code.overwrite(
offset + match.index!,
offset + match.index! + match[0].length,
Expand Down
51 changes: 26 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions test/transformer-directives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ describe('transformer-directives', () => {
btn: 'px-2 py-3 md:px-4 bg-blue-500 text-white rounded',
},
theme: {
colors: {
hsl: 'hsl(210, 50%, 50%)',
hsla: 'hsl(210, 50%, 50%, )',
rgb: 'rgb(255, 0, 0)',
rgba: 'rgba(255, 0, 0, 0.5)',
},
breakpoints: {
xs: '320px',
sm: '640px',
Expand Down Expand Up @@ -450,6 +456,28 @@ div {
"
`)
})

test('opacity', async () => {
const result = await transform(`
div {
color: theme('colors.red.500 / 50%');
color: theme('colors.rgb / 0.5');
color: theme('colors.rgba / 50%');
color: theme('colors.hsl / 0.6');
color: theme('colors.hsla / 60%');
}`,
)
expect(result).toMatchInlineSnapshot(`
"div {
color: rgba(239, 68, 68, 50%);
color: rgba(255, 0, 0, 0.5);
color: rgba(255, 0, 0, 50%);
color: hsla(210, 50%, 50%, 0.6);
color: hsla(210, 50%, 50%, 60%);
}
"
`)
})
})

test('escape backslash', async () => {
Expand Down

0 comments on commit cbfc2b3

Please sign in to comment.