uno.config.ts 2.3 KB

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