Skip to content

Attributify preset

Bu, diğer preset'ler için attributify modunu etkinleştirir.

Kaynak Kodu

Installation

bash
pnpm add -D @unocss/preset-attributify
bash
yarn add -D @unocss/preset-attributify
bash
npm install -D @unocss/preset-attributify
bash
bun add -D @unocss/preset-attributify
ts
import presetAttributify from '@unocss/preset-attributify'

export default defineConfig({
  presets: [
    presetAttributify({ /* preset seçenekleri */ }),
    // ...
  ],
})

TIP

Bu preset unocss paketine dahildir, oradan da içe aktarabilirsiniz:

ts
import { presetAttributify } from 'unocss'

Attributify Mode

Tailwind CSS'in yardımcı programlarını kullanan bu düğmeyi hayal edin. Liste uzadıkça, okumak ve sürdürmek gerçekten zorlaşır.

html
<button
  class="bg-blue-400 hover:bg-blue-500 text-sm text-white font-mono font-light py-2 px-4 rounded border-2 border-blue-200 dark:bg-blue-500 dark:hover:bg-blue-600"
>
  Button
</button>

Attributify modu ile, yardımcı programları özniteliklere ayırabilirsiniz:

html
<button
  bg="blue-400 hover:blue-500 dark:blue-500 dark:hover:blue-600"
  text="sm white"
  font="mono light"
  p="y-2 x-4"
  border="2 rounded blue-200"
>
  Button
</button>

Örneğin, text-sm text-white aynı ön eki tekrarlamadan text="sm white" olarak gruplanabilir.

Prefix self-referencing

flex, grid, border gibi ön ek ile aynı yardımcı programlara sahip yardımcı programlar için özel bir ~ değeri sağlanır.

Örneğin:

html
<button class="border border-red">Button</button>

Şöyle yazılabilir:

html
<button border="~ red">Button</button>

Valueless attributify

Bu preset, Windi CSS'in attributify moduna ek olarak, değersiz öznitelikleri de destekler.

Örneğin,

html
<div class="m-2 rounded text-teal-400" />

şimdi şöyle olabilir

html
<div m-2 rounded text-teal-400 />

INFO

Not: JSX kullanıyorsanız, <div foo> <div foo={true}>'ya dönüştürülebilir ve bu UnoCSS'ten oluşturulan CSS'in öznitelikleri eşleştirememesine neden olabilir. Bunu çözmek için, bu preset ile birlikte transformer-attributify-jsx kullanmayı düşünebilirsiniz.

Properties conflicts

Öznitelik modunun adı öğelerin veya bileşenlerin özellikleriyle çakışırsa, UnoCSS'in attributify moduna özgü olmak için un- ön eki ekleyebilirsiniz.

Örneğin:

html
<a text="red">Bu, bağlantıların `text` özelliğiyle çakışır</a>
<!-- şuna -->
<a un-text="red">Metin rengini kırmızı yap</a>

Ön ek varsayılan olarak isteğe bağlıdır, ön ek kullanımını zorunlu kılmak istiyorsanız, şunu ayarlayın

ts
presetAttributify({
  prefix: 'un-',
  prefixedOnly: true, // <--
})

Ayrıca belirli öznitelikler için taramayı şu şekilde devre dışı bırakabilirsiniz:

ts
presetAttributify({
  ignoreAttributes: [
    'text'
    // ...
  ]
})

TypeScript support (JSX/TSX)

Aşağıdaki içerikle shims.d.ts oluşturun:

Varsayılan olarak, tür @unocss/preset-wind3'ten ortak öznitelikleri içerir. Özel özniteliklere ihtiyacınız varsa, kendi türünüzü uygulamak için tür kaynağına bakın.

Vue

Volar 0.36'dan beri, bilinmeyen özniteliklere karşı artık katı. Devre dışı bırakmak için, projenize aşağıdaki dosyayı ekleyebilirsiniz:

ts
declare module '@vue/runtime-dom' {
  interface HTMLAttributes {
    [key: string]: any
  }
}
declare module '@vue/runtime-core' {
  interface AllowedComponentProps {
    [key: string]: any
  }
}
export {}

React

ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'

declare module 'react' {
  interface HTMLAttributes<T> extends AttributifyAttributes {}
}

Vue 3

ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'

declare module '@vue/runtime-dom' {
  interface HTMLAttributes extends AttributifyAttributes {}
}

SolidJS

ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'

declare module 'solid-js' {
  namespace JSX {
    interface HTMLAttributes<T> extends AttributifyAttributes {}
  }
}

Svelte & SvelteKit

ts
declare namespace svelteHTML {
  import type { AttributifyAttributes } from '@unocss/preset-attributify'

  type HTMLAttributes = AttributifyAttributes
}

Astro

ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'

declare global {
  namespace astroHTML.JSX {
    interface HTMLAttributes extends AttributifyAttributes { }
  }
}

Preact

ts
import type { AttributifyAttributes } from '@unocss/preset-attributify'

declare module 'preact' {
  namespace JSX {
    interface HTMLAttributes extends AttributifyAttributes {}
  }
}

Attributify with Prefix

ts
import type { AttributifyNames } from '@unocss/preset-attributify'

type Prefix = 'uno:' // ön ekinizi buraya değiştirin

interface HTMLAttributes extends Partial<Record<AttributifyNames<Prefix>, string>> {}

Options

strict

  • type: boolean
  • default: false

Yalnızca attributify veya class için CSS oluştur.

prefix

  • type: string
  • default: 'un-'

Attributify modu için ön ek.

prefixedOnly

  • type: boolean
  • default: false

Yalnızca ön ekli öznitelikleri eşleştir.

nonValuedAttribute

  • type: boolean
  • default: true

Değersiz öznitelikleri eşleştirmeyi destekle.

ignoreAttributes

  • type: string[]

Çıkarmadan hariç tutulacak öznitelik listesi.

trueToNonValued

  • type: boolean
  • default: false

DOM'da temsil edilen gerçek değer true ise değersiz öznitelikler de eşleşir. Bu seçenek, değersiz öznitelikleri true olarak kodlayan çerçeveleri desteklemek için vardır. Bu seçeneği etkinleştirmek true ile biten kuralları bozacaktır.

Credits

İlk fikir @Tahul ve @antfu tarafından. Önceki Windi CSS uygulaması @voorjaar tarafından.

Released under the MIT License.