Skip to content

Commit 61b5d62

Browse files
antfuSimon-He95
andauthored
feat: introduce legacy-compat preset (#3480)
Co-authored-by: Simon He <57086651+Simon-He95@users.noreply.github.com>
1 parent ce591fc commit 61b5d62

File tree

12 files changed

+204
-2
lines changed

12 files changed

+204
-2
lines changed

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const Presets: DefaultTheme.NavItemWithLink[] = [
5252
{ text: 'Web fonts', link: '/presets/web-fonts' },
5353
{ text: 'Wind', link: '/presets/wind' },
5454
{ text: 'Mini', link: '/presets/mini' },
55+
{ text: 'Legacy Compat', link: '/presets/legacy-compat' },
5556
{ text: 'Tagify', link: '/presets/tagify' },
5657
{ text: 'Rem to px', link: '/presets/rem-to-px' },
5758
]

docs/config/theme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ theme: {
1818
colors: {
1919
'veryCool': '#0000ff', // class="text-very-cool"
2020
'brand': {
21-
'primary': 'hsl(var(--hue 217) 78% / 51%)', //class="bg-brand-primary"
21+
'primary': 'hsl(var(--hue, 217) 78% 51%)', //class="bg-brand-primary"
2222
},
2323
},
2424
}

docs/guide/packages.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ UnoCSS is a monorepo that contains multiple packages. This page lists all the pa
2121
| [@unocss/preset-web-fonts](/presets/web-fonts) | Web fonts (Google Fonts, etc.) support || No |
2222
| [@unocss/preset-typography](/presets/typography) | The typography preset || No |
2323
| [@unocss/preset-rem-to-px](/presets/rem-to-px) | Coverts rem to px for utils | No | No |
24+
| [@unocss/preset-legacy-compat](/presets/legacy-compat) | Collections of legacy compatibility utilities | No | No |
2425
| [@unocss/transformer-variant-group](/transformers/variant-group) | Transformer for Windi CSS's variant group feature || No |
2526
| [@unocss/transformer-directives](/transformers/directives) | Transformer for CSS directives like `@apply` || No |
2627
| [@unocss/transformer-compile-class](/transformers/compile-class) | Compile group of classes into one class || No |

docs/presets/legacy-compat.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: Legacy Compat preset
3+
description: Collections of legacy compatibility utilities.
4+
outline: deep
5+
---
6+
7+
# Legacy Compat Preset
8+
9+
Collections of legacy compatibility utilities.
10+
11+
This preset does not include any rules, it's applying postprocess to the generated CSS from other presets.
12+
13+
By default none of the options are enabled, you need to opt-in each of them explicitly.
14+
15+
[Source Code](https://github.com/unocss/unocss/tree/main/packages/preset-legacy-compat)
16+
17+
## Installation
18+
19+
::: code-group
20+
```bash [pnpm]
21+
pnpm add -D @unocss/preset-legacy-compat
22+
```
23+
```bash [yarn]
24+
yarn add -D @unocss/preset-legacy-compat
25+
```
26+
```bash [npm]
27+
npm install -D @unocss/preset-legacy-compat
28+
```
29+
:::
30+
31+
```ts
32+
// uno.config.ts
33+
import { defineConfig } from 'unocss'
34+
import presetLegacyCompat from '@unocss/preset-legacy-compat'
35+
36+
export default defineConfig({
37+
presets: [
38+
// ...other presets
39+
presetLegacyCompat({
40+
// options
41+
commaStyleColorFunction: true,
42+
}),
43+
],
44+
})
45+
```
46+
47+
## Options
48+
49+
### `commaStyleColorFunction`
50+
51+
- **Type:** `boolean`
52+
- **Default:** `false`
53+
54+
Convert color functions (`rgb()` and `hsl()`) from space-separated to comma-separated, for better compatibility with legacy browsers. Bring back the old behavior before UnoCSS v0.57.0 that was changed to space-separated in [#3221](https://github.com/unocss/unocss/pull/3221) to align with Tailwind CSS.
55+
56+
For examples:
57+
58+
- `rgb(255 0 0)` -> `rgb(255, 0, 0)`
59+
- `rgb(255 0 0 / 50%)` -> `rgba(255, 0, 0, 50%)`
60+
- `hsl(0 100% 50% / 50%)` -> `hsla(0, 100%, 50%, 50%)`

docs/presets/mini.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ presetMini({
125125
colors: {
126126
'veryCool': '#0000ff', // class="text-very-cool"
127127
'brand': {
128-
'primary': 'hsl(var(--hue 217) 78% / 51%)', //class="bg-brand-primary"
128+
'primary': 'hsl(var(--hue, 217) 78% 51%)', //class="bg-brand-primary"
129129
}
130130
},
131131
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @unocss/preset-legacy-compat
2+
3+
Collections of legacy compatibility utilities.
4+
5+
## Documentation
6+
7+
Please refer to the [documentation](https://unocss.dev/presets/legacy-compat).
8+
9+
## License
10+
11+
MIT License &copy; 2023-PRESENT [Anthony Fu](https://github.com/antfu)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
entries: [
5+
'src/index',
6+
],
7+
clean: true,
8+
declaration: true,
9+
rollup: {
10+
emitCJS: true,
11+
},
12+
})
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "@unocss/preset-legacy-compat",
3+
"version": "0.58.2",
4+
"description": "Collections of legacy compatibility utilities.",
5+
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
6+
"license": "MIT",
7+
"funding": "https://github.com/sponsors/antfu",
8+
"homepage": "https://github.com/unocss/unocss/tree/main/packages/preset-legacy-compat#readme",
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/unocss/unocss",
12+
"directory": "packages/preset-legacy-compat"
13+
},
14+
"bugs": {
15+
"url": "https://github.com/unocss/unocss/issues"
16+
},
17+
"keywords": [
18+
"unocss",
19+
"unocss-preset"
20+
],
21+
"sideEffects": false,
22+
"exports": {
23+
".": {
24+
"types": "./dist/index.d.ts",
25+
"import": "./dist/index.mjs",
26+
"require": "./dist/index.cjs"
27+
}
28+
},
29+
"main": "./dist/index.cjs",
30+
"module": "./dist/index.mjs",
31+
"types": "./dist/index.d.ts",
32+
"files": [
33+
"dist"
34+
],
35+
"scripts": {
36+
"build": "unbuild",
37+
"stub": "unbuild --stub"
38+
},
39+
"dependencies": {
40+
"@unocss/core": "workspace:*"
41+
}
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function toCommaStyleColorFunction(str: string) {
2+
return str.replace(/(rgb|hsl)a?\(([^)]+)\)/g, (_, fn: string, v: string) => {
3+
const [rgb, alpha] = v.split(/\//g).map(i => i.trim())
4+
if (alpha && !fn.endsWith('a'))
5+
fn += 'a'
6+
7+
const parts = rgb.split(/(?!,)\s+/).map(i => i.trim())
8+
if (alpha)
9+
parts.push(alpha)
10+
11+
return `${fn}(${parts.filter(Boolean).join(', ')})`
12+
})
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { definePreset } from '@unocss/core'
2+
import { toCommaStyleColorFunction } from './comma-color'
3+
4+
export interface LegacyCompatOptions {
5+
/**
6+
* Convert modern space style color function to comma style.
7+
*
8+
* @example `rgb(255 0 0)` -> `rgb(255, 0, 0)`
9+
* @example `rgba(255 0 0 / 0.5)` -> `rgba(255, 0, 0, 0.5)`
10+
*
11+
* @default false
12+
*/
13+
commaStyleColorFunction?: boolean
14+
}
15+
16+
export const presetLegacyCompat = definePreset((options: LegacyCompatOptions = {}) => {
17+
const {
18+
commaStyleColorFunction = 16,
19+
} = options
20+
21+
return {
22+
name: '@unocss/preset-legacy-compat',
23+
postprocess: (util) => {
24+
util.entries.forEach((i) => {
25+
let value = i[1]
26+
if (typeof value !== 'string')
27+
return
28+
if (commaStyleColorFunction)
29+
value = toCommaStyleColorFunction(value)
30+
if (value !== i[1])
31+
i[1] = value
32+
})
33+
},
34+
}
35+
})
36+
37+
export default presetLegacyCompat

0 commit comments

Comments
 (0)