Skip to content

PostCSS Plugin

PostCSS Plugin für UnoCSS. Unterstützt @apply, @screen und theme() Direktiven.

Quellcode

WARNING

Dieses Paket befindet sich derzeit in einem experimentellen Zustand. Es folgt nicht semver und kann Breaking Changes in Patch-Versionen einführen.

Installation

bash
pnpm add -D unocss @unocss/postcss
bash
yarn add -D unocss @unocss/postcss
bash
npm install -D unocss @unocss/postcss
bash
bun add -D unocss @unocss/postcss
ts
import UnoCSS from '@unocss/postcss'

export default {
  plugins: [
    UnoCSS(),
  ],
}
ts
import { defineConfig, presetWind3 } from 'unocss'

export default defineConfig({
  content: {
    filesystem: [
      '**/*.{html,js,ts,jsx,tsx,vue,svelte,astro}',
    ],
  },
  presets: [
    presetWind3(),
  ],
})
css
@unocss;

Verwendung

@unocss

Die @unocss At-Regel ist ein Platzhalter. Sie wird durch das generierte CSS ersetzt.

Sie können auch jede Ebene einzeln injizieren:

css
@unocss preflights;
@unocss default;

/*
  Fallback-Ebene. Es wird immer empfohlen, sie einzuschließen.
  Nur ungenutzte Ebenen werden hier injiziert.
*/
@unocss;

Wenn Sie alle Ebenen einschließen möchten, unabhängig davon, ob sie zuvor eingeschlossen wurden oder nicht, können Sie @unocss all verwenden. Dies ist nützlich, wenn Sie generiertes CSS in mehreren Dateien einschließen möchten.

css
@unocss all;

@apply

css
.custom-div {
  @apply text-center my-0 font-medium;
}

Wird transformiert zu:

css
.custom-div {
  margin-top: 0rem;
  margin-bottom: 0rem;
  text-align: center;
  font-weight: 500;
}

@screen

Die @screen Direktive ermöglicht es Ihnen, Media Queries zu erstellen, die Ihre Breakpoints nach Namen referenzieren, die aus theme.breakpoints stammen.

css
.grid {
  @apply grid grid-cols-2;
}
@screen xs {
  .grid {
    @apply grid-cols-1;
  }
}
@screen sm {
  .grid {
    @apply grid-cols-3;
  }
}
/* ... */

Wird transformiert zu:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 320px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (min-width: 640px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
/* ... */

Breakpoint-Varianten-Unterstützung

@screen unterstützt auch ltat Varianten

@screen lt
css
.grid {
  @apply grid grid-cols-2;
}
@screen lt-xs {
  .grid {
    @apply grid-cols-1;
  }
}
@screen lt-sm {
  .grid {
    @apply grid-cols-3;
  }
}
/* ... */

Wird transformiert zu:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (max-width: 319.9px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (max-width: 639.9px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
/* ... */
@screen at
css
.grid {
  @apply grid grid-cols-2;
}
@screen at-xs {
  .grid {
    @apply grid-cols-1;
  }
}
@screen at-xl {
  .grid {
    @apply grid-cols-3;
  }
}
@screen at-xxl {
  .grid {
    @apply grid-cols-4;
  }
}
/* ... */

Wird transformiert zu:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 320px) and (max-width: 639.9px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (min-width: 1280px) and (max-width: 1535.9px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
@media (min-width: 1536px) {
  .grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
/* ... */

theme()

Verwenden Sie die theme() Funktion, um auf Ihre Theme-Konfigurationswerte mit Punktnotation zuzugreifen.

css
.btn-blue {
  background-color: theme('colors.blue.500');
}

Wird kompiliert zu:

css
.btn-blue {
  background-color: #3b82f6;
}

Released under the MIT License.