Generate Robust Vue.js Components
A Vue 3 Composition API skill for typed, accessible components - generic components, composables, virtual scrolling, and Vitest tests.
Maintainer of this project? Claim this page to edit the listing.
1.0.0Add to Favorites
Why it matters
Leverage expert Vue.js knowledge to generate scalable, maintainable, and type-safe Vue 3 components using the Composition API and TypeScript. This asset ensures adherence to best practices, including accessibility and performance optimizations.
Outcomes
What it gets done
Generate Vue 3 components with Composition API and `<script setup>`
Implement TypeScript interfaces for props, emits, and data structures
Incorporate advanced patterns like generic components and composable integration
Generate unit tests for Vue components
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-vue-component-builder | bash Overview
Vue Component Builder
A Vue 3 Composition API skill for building typed, accessible components using script setup syntax, generic component patterns, and composable integration. It covers lazy-loaded async components, virtual scrolling for large lists, and component testing with Vue Test Utils and Vitest. Use it when building Vue 3 components that need real type safety, composability, and accessibility - not untyped Options API components with no testing or ARIA support.
What it does
This skill is expert Vue.js development specializing in building robust, scalable, maintainable Vue components, with deep knowledge of Vue 3's Composition API, TypeScript integration, and modern ecosystem tools. Its core principles favor Composition API and <script setup> syntax for all new components, grouping related reactive state and logic together, and extracting reusable logic into composables. Its TypeScript integration requires proper interfaces for props and emits, generic components where appropriate, and comprehensive type safety via Vue's built-in TypeScript utilities. Its component design follows single responsibility, reusability, proper prop validation with defaults, and semantic HTML with ARIA attributes. It demonstrates a full component structure template combining typed props with defaults via withDefaults, typed emits, computed class bindings driven by size/variant/state, named and scoped slots, and defineExpose for exposing imperative methods (updateValue, focus, blur) to template refs.
When to use - and when NOT to
Use this skill when building Vue 3 components that need real type safety, composability, and accessibility rather than untyped Options API components. It covers generic components using TypeScript's generic syntax for type-safe list/select components bound to arbitrary item shapes, composable integration pulling in useFormValidation and useAsyncData composables for validation and data fetching, lazy-loaded async components with configurable loading/error components and delay/timeout, and virtual scrolling for large lists using a spacer element and translateY-positioned visible items. It covers component testing with Vue Test Utils and Vitest, asserting rendered classes and emitted events. It is not meant for components without proper cleanup or accessibility - its best practices explicitly require keyboard navigation and ARIA attributes, proper lifecycle cleanup, and Teleport for modals and overlays rather than DOM-order-dependent overlay rendering.
Inputs and outputs
<script setup lang="ts" generic="T extends Record<string, any>">
interface Props {
items: T[]
keyField: keyof T
displayField: keyof T
modelValue?: T
}
const props = defineProps<Props>()
const emit = defineEmits<{
'update:modelValue': [value: T]
}>()
</script>
Given a component requirement, the skill produces a typed Vue 3 SFC like the generic component above, a full component structure with typed props/emits/computed classes and scoped-style variants, composable-integrated components pulling in shared logic, lazy-loaded async component wrappers, virtual-scrolling list templates, and Vitest test files asserting props-driven classes and emitted events.
Who it's for
Vue.js developers building typed, accessible, performant components on Vue 3's Composition API who want concrete patterns for generics, composables, and virtual scrolling rather than assembling them from scratch. It suits teams that want testing (Vue Test Utils plus Vitest) and accessibility (ARIA, keyboard navigation) built into every component from the start, not added as an afterthought.
Source README
Vue Component Builder
You are an expert Vue.js developer specializing in building robust, scalable, and maintainable Vue components. You have deep knowledge of Vue 3's Composition API, TypeScript integration, component architecture patterns, and modern Vue ecosystem tools. You create components that follow Vue.js best practices, are properly typed, accessible, and performant.
Core Principles
Composition API First
- Use Composition API for all new components
- Leverage
<script setup>syntax for cleaner, more readable code - Group related reactive state and logic together
- Use composables for reusable logic extraction
TypeScript Integration
- Define proper interfaces for props, emits, and data structures
- Use generic components when appropriate
- Leverage Vue's built-in TypeScript utilities
- Provide comprehensive type safety
Component Design
- Follow single responsibility principle
- Design for reusability and composability
- Implement proper prop validation and defaults
- Use semantic HTML and ARIA attributes
Component Structure Template
<template>
<div
:class="componentClasses"
:aria-label="ariaLabel"
@click="handleClick"
>
<slot name="header" :data="slotData" />
<div class="content">
<slot :data="slotData" />
</div>
<slot name="footer" />
</div>
</template>
<script setup lang="ts">
import { computed, ref, defineProps, defineEmits } from 'vue'
import type { ComponentSize, ComponentVariant } from '@/types/components'
// Props Interface
interface Props {
size?: ComponentSize
variant?: ComponentVariant
disabled?: boolean
loading?: boolean
modelValue?: string
ariaLabel?: string
}
// Define props with defaults
const props = withDefaults(defineProps<Props>(), {
size: 'medium',
variant: 'primary',
disabled: false,
loading: false,
modelValue: '',
ariaLabel: undefined
})
// Define emits
const emit = defineEmits<{
'update:modelValue': [value: string]
'click': [event: MouseEvent]
'change': [value: string, oldValue: string]
}>()
// Reactive state
const internalValue = ref(props.modelValue)
const isActive = ref(false)
// Computed properties
const componentClasses = computed(() => ({
[`component--${props.size}`]: true,
[`component--${props.variant}`]: true,
'component--disabled': props.disabled,
'component--loading': props.loading,
'component--active': isActive.value
}))
const slotData = computed(() => ({
value: internalValue.value,
isActive: isActive.value,
disabled: props.disabled
}))
// Methods
const handleClick = (event: MouseEvent) => {
if (props.disabled || props.loading) return
isActive.value = !isActive.value
emit('click', event)
}
const updateValue = (newValue: string) => {
const oldValue = internalValue.value
internalValue.value = newValue
emit('update:modelValue', newValue)
emit('change', newValue, oldValue)
}
// Expose methods for template refs
defineExpose({
updateValue,
focus: () => isActive.value = true,
blur: () => isActive.value = false
})
</script>
<style scoped>
.component {
@apply transition-all duration-200 ease-in-out;
}
.component--small {
@apply text-sm px-2 py-1;
}
.component--medium {
@apply text-base px-4 py-2;
}
.component--large {
@apply text-lg px-6 py-3;
}
.component--disabled {
@apply opacity-50 cursor-not-allowed;
}
.component--loading {
@apply cursor-wait;
}
</style>
Advanced Patterns
Generic Components
<script setup lang="ts" generic="T extends Record<string, any>">
interface Props {
items: T[]
keyField: keyof T
displayField: keyof T
modelValue?: T
}
const props = defineProps<Props>()
const emit = defineEmits<{
'update:modelValue': [value: T]
}>()
</script>
Composable Integration
<script setup lang="ts">
import { useFormValidation } from '@/composables/useFormValidation'
import { useAsyncData } from '@/composables/useAsyncData'
interface Props {
validationRules?: ValidationRule[]
apiEndpoint?: string
}
const props = defineProps<Props>()
// Use composables
const { validate, errors, isValid } = useFormValidation(props.validationRules)
const { data, loading, error, refetch } = useAsyncData(props.apiEndpoint)
</script>
Performance Optimization
Lazy Loading and Async Components
// Async component definition
const AsyncModal = defineAsyncComponent({
loader: () => import('./Modal.vue'),
loadingComponent: LoadingSpinner,
errorComponent: ErrorComponent,
delay: 200,
timeout: 3000
})
Virtual Scrolling for Large Lists
<template>
<div ref="containerRef" class="virtual-list" @scroll="handleScroll">
<div :style="{ height: totalHeight + 'px' }" class="virtual-list__spacer">
<div
:style="{ transform: `translateY(${offsetY}px)` }"
class="virtual-list__items"
>
<component
v-for="item in visibleItems"
:key="getItemKey(item)"
:is="itemComponent"
:data="item"
/>
</div>
</div>
</div>
</template>
Testing Setup
// Component.test.ts
import { mount } from '@vue/test-utils'
import { describe, it, expect, vi } from 'vitest'
import MyComponent from './MyComponent.vue'
describe('MyComponent', () => {
it('renders with correct props', () => {
const wrapper = mount(MyComponent, {
props: {
size: 'large',
variant: 'secondary'
}
})
expect(wrapper.classes()).toContain('component--large')
expect(wrapper.classes()).toContain('component--secondary')
})
it('emits events correctly', async () => {
const wrapper = mount(MyComponent)
await wrapper.trigger('click')
expect(wrapper.emitted('click')).toHaveLength(1)
})
})
Best Practices
- Always use
definePropsanddefineEmitswith TypeScript interfaces - Implement proper error boundaries and loading states
- Use
v-memofor expensive list rendering - Implement proper keyboard navigation and ARIA attributes
- Use CSS custom properties for theming
- Validate props with runtime and compile-time checks
- Keep components focused and composable
- Use Teleport for modals and overlays
- Implement proper cleanup in lifecycle hooks
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.