--- title: Components description: "Stacks provides a powerful component system for building reusable UI elements. Components in Stacks use a STX-like syntax with TypeScript support, compiled..." --- # Components Stacks provides a powerful component system for building reusable UI elements. Components in Stacks use a STX-like syntax with TypeScript support, compiled through the Stacks templating engine. ## Overview The component system helps you: - **Build reusable UI** - Create modular, composable interfaces - **Maintain consistency** - Shared components ensure UI uniformity - **Type-safe props** - Full TypeScript support for component APIs - **Scoped styles** - CSS isolated to components ## Quick Start ### Creating a Component Components live in `resources/components/`: ```html ``` ### Using Components ```html ``` ## Component Props ### Defining Props ```html ``` ### Complex Props ```html ``` ### Prop Validation ```html ``` ## Component Events ### Emitting Events ```html ``` ### v-model Support ```html ``` ## Slots ### Default Slot ```html

Card Title

Card content goes here.

``` ### Named Slots ```html

Modal content here.

``` ### Scoped Slots ```html ``` ## Composition ### Using Composables ```html ``` ### Creating Composables ```typescript // composables/useCounter.ts import { computed, ref } from '@stacksjs/stx' export function useCounter(initial = 0) { const count = ref(initial) const doubled = computed(() => count.value * 2) function increment() { count.value++ } function decrement() { count.value-- } function reset() { count.value = initial } return { count, doubled, increment, decrement, reset, } } ``` ## Component Patterns ### Container/Presenter Pattern ```html ``` ### Compound Components ```html Profile Settings Notifications
Profile content
Settings content
Notifications content
``` ## Styling Components ### Scoped Styles ```html ``` ### CSS Variables ```html ``` ### Dynamic Classes ```html ``` ## Best Practices ### DO - **Keep components focused** - One responsibility per component - **Use TypeScript** - Type props and emits for safety - **Document props** - Use JSDoc comments for complex props - **Use slots wisely** - Provide flexibility without complexity - **Scope styles** - Prevent CSS leaks ### DON'T - **Don't mutate props** - Use events to communicate changes - **Don't over-abstract** - Start simple, extract when needed - **Don't deeply nest** - Flatten component hierarchies - **Don't use `any`** - Proper types catch bugs early ## Related Documentation - **[Views](/basics/views)** - Page templates - **[Functions](/basics/functions)** - Shared logic and composables under `resources/functions` - **[Styling](/basics/views#styling)** - CSS and styling guide - **[State Management](/basics/views#events-and-state)** - Managing component state