headlessui
nuxt-headlessui

用於 Nuxt 的 Headless UI 整合。完全無樣式、完全可訪問的 UI 元件,旨在與 Tailwind CSS 完美整合。

Nuxt Headless UI CircleCI

npm versionnpm downloadsLicenseNuxt

Headless UI 用於 Nuxt 的整合。完全無樣式、完全可訪問的 UI 元件,旨在與 Tailwind CSS 完美整合。

Headless UI Vue 文件:https://headlessui.com/vue/menu

功能

  • 自動動態匯入(無全域性元件)
  • 完全型別安全
  • 可配置的元件字首(預設為 Headless

快速設定

  1. nuxt-headlessui 依賴項新增到您的專案中
npx nuxi@latest module add headlessui
  1. nuxt-headlessui 新增到 nuxt.config.tsmodules 部分
{
    modules: [
        'nuxt-headlessui'
    ],

    // Optionally change the default prefix.
    headlessui: {
        prefix: 'Headless'
    }
}
  1. app.vue 檔案的 <script setup> 中新增以下幾行

!注意
這僅在您使用 [email protected]@headlessui/[email protected] 之前的版本時才需要。更新的版本使用 Vue 提供的原生 useId composable。

// Use SSR-safe IDs for Headless UI
provideHeadlessUseId(() => useId())

這是為了避免由於 ID 不匹配而導致的水合問題。這兩個 composable 都將由 nuxt 自動匯入。provideHeadlessUseId composable 是 @headlessui/vue 中的 provideUseId composable 的別名。import { provideUseId } from '@headlessui/vue' 直接無法開箱即用

就這樣!您現在可以在 Nuxt 應用程式中使用 Headless UI 了 ✨

使用

完成設定後,只需在元件和頁面中使用無頭元件,並使用您喜歡的 CSS 框架為它們設定樣式。您不必匯入元件。這是一個列表框 (Select) 的示例,它使用了 heroicons 和 Tailwind CSS

<template>
  <div class="container mx-auto">
    <div class="w-72">
      <HeadlessListbox v-model="selectedPerson">
        <div class="relative mt-1">
          <HeadlessListboxButton
            class="relative w-full cursor-default rounded-lg bg-white py-2 pl-3 pr-10 text-left shadow-md focus:outline-none focus-visible:border-indigo-500 focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75 focus-visible:ring-offset-2 focus-visible:ring-offset-orange-300 sm:text-sm"
          >
            <span class="block truncate">{{ selectedPerson.name }}</span>
            <span
              class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2"
            >
              <ChevronUpDownIcon
                class="h-5 w-5 text-gray-400"
                aria-hidden="true"
              />
            </span>
          </HeadlessListboxButton>

          <transition
            leave-active-class="transition duration-100 ease-in"
            leave-from-class="opacity-100"
            leave-to-class="opacity-0"
          >
            <HeadlessListboxOptions
              class="absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm"
            >
              <HeadlessListboxOption
                v-for="person in people"
                v-slot="{ active, selected }"
                :key="person.name"
                :value="person"
                as="template"
              >
                <li
                  :class="[
                    active ? 'bg-amber-100 text-amber-900' : 'text-gray-900',
                    'relative cursor-default select-none py-2 pl-10 pr-4',
                  ]"
                >
                  <span
                    :class="[
                      selected ? 'font-medium' : 'font-normal',
                      'block truncate',
                    ]"
                  >{{ person.name }}</span>
                  <span
                    v-if="selected"
                    class="absolute inset-y-0 left-0 flex items-center pl-3 text-amber-600"
                  >
                    <CheckIcon class="h-5 w-5" aria-hidden="true" />
                  </span>
                </li>
              </HeadlessListboxOption>
            </HeadlessListboxOptions>
          </transition>
        </div>
      </HeadlessListbox>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { CheckIcon, ChevronUpDownIcon } from '@heroicons/vue/20/solid'

const people = [
  { name: 'Wade Cooper' },
  { name: 'Arlene Mccoy' },
  { name: 'Devon Webb' },
  { name: 'Tom Cook' },
  { name: 'Tanya Fox' },
  { name: 'Hellen Schmidt' }
]
const selectedPerson = ref(people[0])
</script>

如果您正在使用 Tailwind CSS,您可以使用 @headlessui/tailwindcss 外掛來獲取 ui-open:*ui-active:* 等修飾符。如果您正在使用 UnoCss,則有一個unoocss primitives 預設https://github.com/zirbest/unocss-preset-primitives/tree/main)可以實現此功能。

開發

# Install dependencies
pnpm install

# Generate type stubs
pnpm run dev:prepare

# Develop with the playground
pnpm run dev

# Build the playground
pnpm run dev:build

# Run ESLint
pnpm run lint