unocss rules shortcuts 自用案例

4/28/2022 unocss
export default defineConfig({
  rules: [
    // 多行文本超出部分省略号 line-n
    [/^line-(\d+)$/, ([, l]) => {
      if (~~l === 1) {
        return {
          'overflow': 'hidden',
          'text-overflow': 'ellipsis',
          'white-space': 'nowrap',
          'width': '100%',
        }
      }
      return {
        'overflow': 'hidden',
        'display': '-webkit-box',
        '-webkit-box-orient': 'vertical',
        '-webkit-line-clamp': l,
      }
    }],
    // 一侧圆角 rounded-left-5px
    [/^rounded-(left|right|top|bottom)-(.*?)$/, ([, position, m]) => {
      let x1, x2, y1, y2
      if (['left', 'right'].includes(position)) {
        y1 = 'top'
        y2 = 'bottom'
        x1 = x2 = position
      }
      else {
        x1 = 'left'
        x2 = 'right'
        y1 = y2 = position
      }
      if (m === 'full')
        m = '99999px'

      return {
        [`border-${y1}-${x1}-radius`]: m,
        [`border-${y2}-${x2}-radius`]: m,
      }
    }],
  ],
  shortcuts: [
    // 正方形 square-100px
    [/^square-\[?(.*?)\]?$/, ([, size]) => {
      return `w-${size} h-${size}`
    }],
    // 圆形 circle-100px
    [/^circle-\[?(.*?)\]?$/, ([, size]) => {
      return `square-${size} rounded-full`
    }],
    // 垂直水平居中
    ['flex-center', 'flex justify-center items-center'],
  ],
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Last Updated: 9/22/2023, 6:04:11 AM