Skip to content

Shortcuts

Shortcuts ermöglichen es Ihnen, mehrere Regeln zu einer einzigen Kurzform zu kombinieren, inspiriert von Windi CSS's.

Verwendung

ts
shortcuts: {
  // Shortcuts zu mehreren Utilities
  'btn': 'py-2 px-4 font-semibold rounded-lg shadow-md',
  'btn-green': 'text-white bg-green-500 hover:bg-green-700',
  // Einzelner Utility-Alias
  'red': 'text-red-100',
}

Zusätzlich zur einfachen Zuordnung ermöglicht UnoCSS Ihnen auch, dynamische Shortcuts zu definieren.

Ähnlich wie Regeln ist ein dynamischer Shortcut die Kombination aus einem Matcher RegExp und einer Handler-Funktion.

ts
shortcuts: [
  // Sie könnten immer noch den Objekt-Stil haben
  {
    btn: 'py-2 px-4 font-semibold rounded-lg shadow-md',
  },
  // dynamische Shortcuts
  [/^btn-(.*)$/, ([, c]) => `bg-${c}-400 text-${c}-100 py-2 px-4 rounded-lg`],
]

Damit könnten wir btn-green und btn-red verwenden, um das folgende CSS zu generieren:

css
.btn-green {
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  padding-left: 1rem;
  padding-right: 1rem;
  --un-bg-opacity: 1;
  background-color: rgb(74 222 128 / var(--un-bg-opacity));
  border-radius: 0.5rem;
  --un-text-opacity: 1;
  color: rgb(220 252 231 / var(--un-text-opacity));
}
.btn-red {
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  padding-left: 1rem;
  padding-right: 1rem;
  --un-bg-opacity: 1;
  background-color: rgb(248 113 113 / var(--un-bg-opacity));
  border-radius: 0.5rem;
  --un-text-opacity: 1;
  color: rgb(254 226 226 / var(--un-text-opacity));
}

Released under the MIT License.