Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
use --alpha(color / 50%) instead of --alpha(color, 50%)
  • Loading branch information
RobinMalfait committed Jan 17, 2025
commit b24286e578c1a94dfe157fa3c14c52b8431a5acb
10 changes: 5 additions & 5 deletions packages/tailwindcss/src/css-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('--alpha(…)', () => {
expect(
await compileCss(css`
.foo {
margin: --alpha(red, 50%);
margin: --alpha(red / 50%);
}
`),
).toMatchInlineSnapshot(`
Expand All @@ -30,7 +30,7 @@ describe('--alpha(…)', () => {
}
`),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The --alpha(…) function requires two arguments, e.g.: \`--alpha(var(--my-color), 50%)\`]`,
`[Error: The --alpha(…) function requires a color and an alpha value, e.g.: \`--alpha(var(--my-color) / 50%)\`]`,
)
})

Expand All @@ -42,19 +42,19 @@ describe('--alpha(…)', () => {
}
`),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The --alpha(…) function requires two arguments, e.g.: \`--alpha(red, 50%)\`]`,
`[Error: The --alpha(…) function requires a color and an alpha value, e.g.: \`--alpha(red / 50%)\`]`,
)
})

test('--alpha(…) errors multiple arguments are used', async () => {
expect(() =>
compileCss(css`
.foo {
margin: --alpha(red, 50%, blue);
margin: --alpha(red / 50%, blue);
}
`),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The --alpha(…) function only accepts two arguments, e.g.: \`--alpha(red, 50%)\`]`,
`[Error: The --alpha(…) function only accepts one argument, e.g.: \`--alpha(red / 50%)\`]`,
)
})
})
Expand Down
12 changes: 7 additions & 5 deletions packages/tailwindcss/src/css-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ const functions: Record<string, (designSystem: DesignSystem, ...args: string[])
theme: legacyTheme,
}

function alpha(_designSystem: DesignSystem, value: string, alpha: string, ...rest: string[]) {
if (!value || !alpha) {
function alpha(_designSystem: DesignSystem, value: string, ...rest: string[]) {
let [color, alpha] = segment(value, '/').map((v) => v.trim())

if (!color || !alpha) {
throw new Error(
`The --alpha(…) function requires two arguments, e.g.: \`--alpha(${value || 'var(--my-color)'}, ${alpha || '50%'})\``,
`The --alpha(…) function requires a color and an alpha value, e.g.: \`--alpha(${color || 'var(--my-color)'} / ${alpha || '50%'})\``,
)
}

if (rest.length > 0) {
throw new Error(
`The --alpha(…) function only accepts two arguments, e.g.: \`--alpha(${value || 'var(--my-color)'}, ${alpha || '50%'})\``,
`The --alpha(…) function only accepts one argument, e.g.: \`--alpha(${color || 'var(--my-color)'} / ${alpha || '50%'})\``,
)
}

return withAlpha(value, alpha)
return withAlpha(color, alpha)
}

function spacing(designSystem: DesignSystem, value: string, ...rest: string[]) {
Expand Down