Skip to content

Commit

Permalink
docs: update the outputToCssLayers example (#3865)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzl0304 committed Jun 8, 2024
1 parent 6d94efc commit 301ef3c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
22 changes: 12 additions & 10 deletions docs/config/layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,17 @@ outputToCssLayers: true
You can change the CSS Layer names with:

```ts
outputToCssLayers: (layer) => {
// The default layer will be output to the "utilities" CSS layer.
if (layer === 'default')
return 'utilities'

// The shortcuts layer will be output to the "shortcuts" sublayer the of "utilities" CSS layer.
if (layer === 'shortcuts')
return 'utilities.shortcuts'

// All other layers will just use their name as the CSS layer name.
outputToCssLayers: {
cssLayerName: (layer) => {
// The default layer will be output to the "utilities" CSS layer.
if (layer === 'default')
return 'utilities'

// The shortcuts layer will be output to the "shortcuts" sublayer the of "utilities" CSS layer.
if (layer === 'shortcuts')
return 'utilities.shortcuts'

// All other layers will just use their name as the CSS layer name.
}
}
```
13 changes: 13 additions & 0 deletions test/__snapshots__/use-css-layer.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`use-css-layer > change layer name 1`] = `
"/* layer: shortcuts */
@layer utilities.shortcuts{
.custom-shortcut{font-size:1.125rem;line-height:1.75rem;--un-text-opacity:1;color:rgb(251 146 60 / var(--un-text-opacity));}
.custom-shortcut:hover{--un-text-opacity:1;color:rgb(45 212 191 / var(--un-text-opacity));}
}
/* layer: default */
@layer utilities{
.h-1{height:0.25rem;}
.w-1{width:0.25rem;}
}"
`;

exports[`use-css-layer > static 1`] = `
"/* layer: shortcuts */
@layer shortcuts{
Expand Down
21 changes: 21 additions & 0 deletions test/use-css-layer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createGenerator } from '@unocss/core'
import { describe, expect, it } from 'vitest'
import presetUno from '@unocss/preset-uno'

describe('use-css-layer', () => {
it('static', async () => {
Expand All @@ -20,4 +21,24 @@ describe('use-css-layer', () => {
const { css } = await uno.generate('a b abc abcd d4 c5', { preflights: false })
expect(css).toMatchSnapshot()
})

it('change layer name', async () => {
const uno = createGenerator({
presets: [presetUno()],
shortcuts: {
'custom-shortcut': 'text-lg text-orange hover:text-teal',
},
outputToCssLayers: {
cssLayerName: (layer: string) => {
if (layer === 'default')
return 'utilities'

if (layer === 'shortcuts')
return 'utilities.shortcuts'
},
},
})
const { css } = await uno.generate('w-1 h-1 custom-shortcut', { preflights: false })
expect(css).toMatchSnapshot()
})
})

0 comments on commit 301ef3c

Please sign in to comment.