uno.config.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import type { Theme } from 'unocss/preset-uno'
  2. import {
  3. defineConfig,
  4. presetAttributify,
  5. presetIcons,
  6. presetTypography,
  7. presetUno,
  8. transformerCompileClass,
  9. transformerDirectives,
  10. transformerVariantGroup,
  11. } from 'unocss'
  12. import presetRemToPx from '@unocss/preset-rem-to-px'
  13. import presetSafeArea from '@yeungkc/unocss-preset-safe-area'
  14. import { entriesToCss, toArray } from '@unocss/core'
  15. import { darkTheme, lightTheme } from './themes'
  16. export default defineConfig<Theme>({
  17. content: {
  18. pipeline: {
  19. include: [
  20. /\.(vue|svelte|[jt]sx|mdx?|astro|elm|php|phtml|html)($|\?)/,
  21. 'src/**/*.{js,ts}',
  22. ],
  23. },
  24. },
  25. variants: [
  26. (matcher) => {
  27. if (!matcher.startsWith('hocus:') && !matcher.startsWith('hocus-')) {
  28. return matcher
  29. }
  30. return {
  31. matcher: matcher.slice(6),
  32. selector: s => `${s}:hover, ${s}:focus`,
  33. }
  34. },
  35. ],
  36. shortcuts: [
  37. [/^flex-?(col)?-(start|end|center|baseline|stretch)-?(start|end|center|between|around|evenly|left|right)?$/, ([, col, items, justify]) => {
  38. const cls = ['flex']
  39. if (col === 'col') {
  40. cls.push('flex-col')
  41. }
  42. if (items === 'center' && !justify) {
  43. cls.push('items-center')
  44. cls.push('justify-center')
  45. }
  46. else {
  47. cls.push(`items-${items}`)
  48. if (justify) {
  49. cls.push(`justify-${justify}`)
  50. }
  51. }
  52. return cls.join(' ')
  53. }],
  54. ],
  55. preflights: [
  56. {
  57. getCSS: () => {
  58. const returnCss: any = []
  59. // 明亮主题
  60. const lightCss = entriesToCss(Object.entries(lightTheme))
  61. const lightRoots = toArray([`*,::before,::after`, `::backdrop`])
  62. returnCss.push(lightRoots.map(root => `${root}{${lightCss}}`).join(''))
  63. // 暗黑主题
  64. const darkCss = entriesToCss(Object.entries(darkTheme))
  65. const darkRoots = toArray([`html.dark,html.dark *,html.dark ::before,html.dark ::after`, `html.dark ::backdrop`])
  66. returnCss.push(darkRoots.map(root => `${root}{${darkCss}}`).join(''))
  67. return returnCss.join('')
  68. },
  69. },
  70. ],
  71. theme: {
  72. colors: {
  73. 'ui-primary': 'rgb(var(--ui-primary))',
  74. 'ui-text': 'rgb(var(--ui-text))',
  75. },
  76. },
  77. presets: [
  78. presetUno(),
  79. presetAttributify(),
  80. presetIcons({
  81. extraProperties: {
  82. 'display': 'inline-block',
  83. 'vertical-align': 'middle',
  84. },
  85. }),
  86. presetTypography(),
  87. presetRemToPx(),
  88. presetSafeArea(),
  89. ],
  90. transformers: [
  91. transformerDirectives(),
  92. transformerVariantGroup(),
  93. transformerCompileClass(),
  94. ],
  95. configDeps: [
  96. 'themes/index.ts',
  97. ],
  98. })