All skills
hyf0 avatar

/create-adaptable-composable

@3b0d2ce official
by hyf0hyf0/vue-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/hyf0/vue-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 hyf0/vue-skills/create-adaptable-composable

Creates Vue 3 composables that accept both reactive and non-reactive inputs (plain values, refs, or getters) by normalizing them with toValue() and toRef() inside effects. Use this when building reusable composable functions that need to work across different caller contexts without requiring callers to manage reactivity.

Generated from the current SKILL.md.

Does this skill work with Vue 2?
No. The skill requires Vue 3 or above (or Nuxt 3 or above). MaybeRef and MaybeRefOrGetter are Vue 3 type utilities.
When should I use MaybeRefOrGetter vs MaybeRef?
Use MaybeRefOrGetter for read-only inputs (getters are allowed). Use MaybeRef when the input needs to be writable or two-way. Avoid MaybeRefOrGetter if the parameter is a callback, predicate, or comparator function, as it may be accidentally invoked as a getter.
Should I use toValue() or toRef() to normalize inputs?
Use toRef() inside watchers or reactive effects when you need to preserve reactivity. Use toValue() when you need a plain, non-reactive value.
Can I use MaybeRefOrGetter with DOM element targets?
Yes. Use MaybeRefOrGetter for DOM/Element targets if you want to support computed or derived targets.

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