mcpField
V
vueuse

vueuse

github.com/onmax開発7233.1k 回インストール今日更新
リポジトリ

vueuse について

VueUse のスキルリファレンスです。Vue の Composition ユーティリティを探す開発者向けに、Nuxt の自動インポートや SSR の注意点も案内します。

主な機能

  1. 01状態とストレージのコンポーザブル
  2. 02DOM、ブラウザー、センサー用ユーティリティ
  3. 03ネットワークとアニメーション用ユーティリティ
  4. 04配列、時刻、リアクティビティ用ユーティリティ
  5. 05SSR セーフな使い方の案内

活用シーン

  1. 01機能に合うコンポーザブルの検索
  2. 02リアクティブな状態のストレージ保存
  3. 03SSR アプリでのブラウザー API の処理

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/onmax/nuxt-skills --skill vueuse

インストール後、Claude Code で/vueuseを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

composables/
computed-async.md
computed-eager.md
computed-inject.md
computed-with-control.md
create-disposable-directive.md
create-event-hook.md
create-generic-projection.md
create-global-state.md
create-injection-state.md
create-projection.md
create-ref.md
create-reusable-template.md
create-shared-composable.md
create-template-promise.md
create-unref-fn.md
extend-ref.md
from.md
get.md
inject-local.md
is-defined.md
logic-and.md
logic-not.md
logic-or.md
make-destructurable.md
on-click-outside.md
on-element-removal.md
on-key-stroke.md
on-long-press.md
on-start-typing.md
provide-local.md

詳細

VueUse

不可欠な Vue Composition ユーティリティのコレクションです。カスタムコンポーザブルを書く前に VueUse を確認してください。ほとんどのパターンはすでに実装されています。

現在の安定版: Vue 3.5 以降向けの VueUse 14.x

インストール

Vue 3:

pnpm add @vueuse/core

Nuxt:

pnpm add @vueuse/nuxt @vueuse/core
// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@vueuse/nuxt'],
})

Nuxt モジュールがコンポーザブルを自動インポートするため、インポートは不要です。

カテゴリー

カテゴリー例
状態useLocalStorage, useSessionStorage, useRefHistory
要素useElementSize, useIntersectionObserver, useResizeObserver
ブラウザーuseClipboard, useFullscreen, useMediaQuery
センサーuseMouse, useKeyboard, useDeviceOrientation
ネットワークuseFetch, useWebSocket, useEventSource
アニメーションuseTransition, useInterval, useTimeout
コンポーネントuseVModel, useVirtualList, useTemplateRefsList
ウォッチwatchDebounced, watchThrottled, watchOnce
リアクティビティcreateSharedComposable, toRef, toReactive
配列useArrayFilter, useArrayMap, useSorted
時間useDateFormat, useNow, useTimeAgo
ユーティリティuseDebounce, useThrottle, useMemoize

クイックリファレンス

必要なものに応じてコンポーザブルのファイルを読み込みます。

作業内容ファイルを読み込む
コンポーザブルを探すreferences/composables.md
特定のコンポーザブルcomposables/<name>.md

ファイルの読み込み

タスクに応じて、次のリファレンスファイルを読み込むことを検討してください。

すべてのファイルを一度に読み込まないでください。 現在のタスクに関連するものだけを読み込んでください。

よくあるパターン

状態の永続化:

const state = useLocalStorage('my-key', { count: 0 })

マウスの追跡:

const { x, y } = useMouse()

デバウンス付き ref:

const search = ref('')
const debouncedSearch = refDebounced(search, 300)

共有コンポーザブル(シングルトン):

const useSharedMouse = createSharedComposable(useMouse)

SSR に関する注意点

多くの VueUse コンポーザブルは、SSR 中に使用できないブラウザー API を使用します。

isClient で確認:

import { isClient } from '@vueuse/core'

if (isClient) {
  // Browser-only code
  const { width } = useWindowSize()
}

onMounted でラップ:

const width = ref(0)

onMounted(() => {
  // Only runs in browser
  const { width: w } = useWindowSize()
  width.value = w.value
})

SSR セーフなコンポーザブルを使用:

// These check isClient internally
const mouse = useMouse() // Returns {x: 0, y: 0} on server
const storage = useLocalStorage('key', 'default') // Uses default on server

@vueuse/nuxt が SSR を自動処理します - コンポーザブルはサーバー上で安全なデフォルト値を返します。

対象要素の参照

DOM 要素ではなくコンポーネントの参照を対象にする場合:

import type { MaybeElementRef } from '@vueuse/core'

// Component ref needs .$el to get DOM element
const compRef = ref<ComponentInstance>()
const { width } = useElementSize(compRef) // ❌ Won't work

// Use MaybeElementRef pattern
import { unrefElement } from '@vueuse/core'

const el = computed(() => unrefElement(compRef)) // Gets .$el
const { width } = useElementSize(el) // ✅ Works

または $el に直接アクセス:

const compRef = ref<ComponentInstance>()

onMounted(() => {
  const el = compRef.value?.$el as HTMLElement
  const { width } = useElementSize(el)
})

コメント