create-adaptable-composable
Library-grade Vue composables that accept plain values, refs, or getters as inputs.
- Use
MaybeRefOrGetterfor read-only inputs (computed-friendly) andMaybeReffor writable two-way inputs; normalize withtoRef()for reactive sources andtoValue()for non-reactive values - Avoid
MaybeRefOrGetterwhen parameters are callbacks or predicates to prevent accidental function invocation - Requires Vue 3 or Nuxt 3; follow the four-step design pattern: confirm API, identify reactive params, normalize inputs in effects, implement core logic
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:
- Confirm the composable's purpose and API design and expected inputs/outputs.
- Identify inputs params that should be reactive (MaybeRef / MaybeRefOrGetter).
- Use
toValue()ortoRef()to normalize inputs inside reactive effects. - 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>;
More from hyf0/vue-skills
vue-best-practices
MUST be used for Vue.js tasks. Strongly recommends Composition API with `<script setup>` and TypeScript as the standard approach. Covers Vue 3, SSR, Volar, vue-tsc. Load for any Vue, .vue files, Vue Router, Pinia, or Vite with Vue work. ALWAYS use Composition API unless the project explicitly requires Options API.
21.4Kvue-debug-guides
Vue 3 debugging and error handling for runtime errors, warnings, async failures, and SSR/hydration issues. Use when diagnosing or fixing Vue issues.
13.1Kvue-pinia-best-practices
Pinia stores, state management patterns, store setup, and reactivity with stores.
1.9Kvue-router-best-practices
Vue Router 4 patterns, navigation guards, route params, and route-component lifecycle interactions.
1.8Kvue-testing-best-practices
Use for Vue.js testing. Covers Vitest, Vue Test Utils, component testing, mocking, testing patterns, and Playwright for E2E testing.
1.5Kvue-options-api-best-practices
Vue 3 Options API style (data(), methods, this context). Each reference shows Options API solution only.
1.5K