All skills
vuejs-ai avatar

/create-adaptable-composable

@3b0d2ce official
by Vue AIvuejs-ai/skills2.9k stars
167

Create a library-grade Vue composable that accepts maybe-reactive inputs (MaybeRef / MaybeRefOrGetter) so callers can pass a plain value, ref, or getter. Normalize inputs with toValue()/toRef() inside reactive effects (watch/watchEffect) to keep behavior predictable and reactive. Use this skill when user asks for creating adaptable or reusable composables.

Use this Skill: https://skilld.dev/gh/vuejs-ai/skills/create-adaptable-composable

This session only. Nothing lands on disk.

SKILL.md

≈97 tokens always: the name and description. ≈529 when used: this file.

Create Adaptable Composable

Adaptable composables are reusable functions that can accept both reactive and non-reactive inputs. This allows developers to use the composable in a variety of contexts without worrying about the reactivity of the inputs.

Steps to design an adaptable composable in Vue.js:

  1. Confirm the composable's purpose and API design and expected inputs/outputs.
  2. Identify inputs params that should be reactive (MaybeRef / MaybeRefOrGetter).
  3. Use toValue() or toRef() to normalize inputs inside reactive effects.
  4. Implement the core logic of the composable using Vue's reactivity APIs.

Core Type Concepts

Type Utilities

/**
 * value or writable ref (value/ref/shallowRef/writable computed)
 */
export type MaybeRef<T = any> = T | Ref<T> | ShallowRef<T> | WritableComputedRef<T>;

/**
 * MaybeRef<T> + ComputedRef<T> + () => T
 */
export type MaybeRefOrGetter<T = any> = MaybeRef<T> | ComputedRef<T> | (() => T);

Policy and Rules

  • Read-only, computed-friendly input: use MaybeRefOrGetter
  • Needs to be writable / two-way input: use MaybeRef
  • Parameter might be a function value (callback/predicate/comparator): do not use MaybeRefOrGetter, or you may accidentally invoke it as a getter.
  • DOM/Element targets: if you want computed/derived targets, use MaybeRefOrGetter.

When MaybeRefOrGetter or MaybeRef is used:

  • resolve reactive value using toRef() (e.g. watcher source)
  • resolve non-reactive value using toValue()

Examples

Adaptable useDocumentTitle Composable: read-only title parameter

import { watch, toRef } from 'vue'
import type { MaybeRefOrGetter } from 'vue'

export function useDocumentTitle(title: MaybeRefOrGetter<string>) {
  watch(toRef(title), (t) => {
    document.title = t
  }, { immediate: true })
}

Adaptable useCounter Composable: two-way writable count parameter

import { watch, toRef } from 'vue'
import type { MaybeRef } from 'vue'

function useCounter(count: MaybeRef<number>) {
  const countRef = toRef(count)
  function add() {
    countRef.value++
  }
  return { add }
}

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides standard architectural patterns and code templates for creating reactive Vue 3 composables. It contains no malicious code, suspicious dependencies, or data exfiltration risks.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    1 file scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 3b0d2ce. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 8 months ago
metadata
{
  "author": "github.com/vuejs-ai",
  "version": "17.0.0"
}
compatibility
Requires Vue 3 (or above) or Nuxt 3 (or above) project

README badge

README badge for vuejs-ai/skills/create-adaptable-composable

Teaches how to write Vue 3 composables that accept both reactive and non-reactive inputs (plain values, refs, or getters) using MaybeRef and MaybeRefOrGetter types. Use this skill when designing library-grade composables that need to work flexibly across different caller contexts.

Generated from the current SKILL.md.

Does this skill work with Vue 2?
No. This skill requires Vue 3 or above (or Nuxt 3 or above). MaybeRef and MaybeRefOrGetter are Vue 3 composition API utilities.
When should I use MaybeRef vs MaybeRefOrGetter?
Use MaybeRefOrGetter for read-only inputs where the caller might pass a plain value, ref, computed ref, or getter function. Use MaybeRef when the input needs to be writable (two-way). Never use MaybeRefOrGetter if the parameter is a callback or predicate, since it may be invoked as a getter.
How do I resolve MaybeRef or MaybeRefOrGetter inputs inside effects?
Use toRef() to normalize the input as a reactive source for watchers, and toValue() to extract the current value synchronously outside of effects.
Can I use MaybeRefOrGetter for DOM element targets?
Yes. Use MaybeRefOrGetter for DOM/Element targets if you want to support computed or derived target references; this allows callers to pass plain elements, refs, or getters.

Generated from the current SKILL.md. These answers refresh after source changes.