# Component Organization Guide Guide for splitting large components and modals into manageable files. ## When to Split Components Split a component when: - File exceeds 200 lines - Component has multiple distinct sections (tabs, steps, panels) - Logic becomes hard to follow - Multiple developers work on different parts ## Modal Organization Pattern ### Structure for Large Modals ``` src/routes/(app)/feature/ ├── +page.svelte # Main page ├── FeatureModal.svelte # Main modal container └── feature-modal/ # Modal sub-components (kebab-case folder) ├── modals/ # Nested modals │ └── PickerModal.svelte └── tabs/ # Tab content components ├── BasicInfoTab.svelte ├── SettingsTab.svelte └── AdvancedTab.svelte ``` ### Example: Test Form Modal **Location**: `src/routes/(app)/master-data/tests/test-modal/` ```svelte {#snippet children()}
{#if activeTab === 'basic'} {:else if activeTab === 'technical'} {:else if activeTab === 'calculation'} {/if} {/snippet}
``` ```svelte
``` ## Data Flow ### Parent to Child - Pass data via props (`bind:formData`) - Use `$bindable()` for two-way binding - Keep state in parent when shared across tabs ### Child to Parent - Use callbacks for actions (`onSave`, `onClose`) - Modify bound data directly (with `$bindable`) - Emit events for complex interactions ## Props Interface Pattern ```javascript // Define props with JSDoc /** @type {{ formData: Object, onValidate: Function, readonly: boolean }} */ let { formData = $bindable({}), onValidate = () => true, readonly = false } = $props(); ``` ## Naming Conventions - **Main modal**: `{Feature}Modal.svelte` (e.g., `TestFormModal.svelte`) - **Tab components**: `{TabName}Tab.svelte` (e.g., `BasicInfoTab.svelte`) - **Nested modals**: `{Action}Modal.svelte` (e.g., `ConfirmDeleteModal.svelte`) - **Folder names**: kebab-case matching the modal name (e.g., `test-modal/`) ## Shared State Management For complex modals with shared state across tabs: ```javascript // In main modal let sharedState = $state({ dirty: false, errors: {}, selectedItems: [] }); // Pass to tabs ``` ## Import Order in Sub-components Same as main components: 1. Svelte imports 2. `$lib/*` imports 3. External libraries 4. Relative imports (other tabs/modals) ## Testing Split Components ```bash # Test individual tab component vitest run src/routes/feature/modal-tabs/BasicInfoTab.test.js # Test main modal integration vitest run src/routes/feature/FeatureModal.test.js ``` ## Benefits - **Maintainability**: Each file has single responsibility - **Collaboration**: Multiple developers can work on different tabs - **Testing**: Test individual sections in isolation - **Performance**: Only render visible tab content - **Reusability**: Tabs can be used in different modals