Component Directory Structure
Components are located in~/workspace/source/shared/components/. Each component has its own directory:
Component Types
There are two main component patterns in the system:1. Static Components
Components with predefined HTML that gets loaded dynamically.2. Web Components
Custom elements using the Web Components API for more complex functionality.Creating a New Component
Step 1: Create Component Directory
Create a new folder inshared/components/:
Step 2: Create Component Files
Create the three core files:HTML Structure
shared/components/my-component/my-component.html
CSS Styles
shared/components/my-component/my-component.css
JavaScript Logic
shared/components/my-component/my-component.js
Component Loading Patterns
The system uses two main loading patterns:Pattern 1: Smart Runtime Loader
Used byload-basics.js and portfolio-loader.js. This pattern dynamically loads HTML and adjusts all relative paths:
Example from load-basics.js:10-44
- Detects the script’s location
- Calculates path depth from current page
- Adjusts all relative paths in loaded HTML
- Injects styles, HTML, and scripts in order
shared/components/load-basics/load-basics.js:1-318 for the complete implementation.
Pattern 2: Web Component Class
Used by the mega menu system. This pattern creates a custom HTML element:Example from mega-menu.js:5-67
shared/components/mega-menu/mega-menu.js:1-325 for the full implementation.
Advanced Component Example: Dynamic Island
The Dynamic Island component is a sophisticated example that demonstrates:- Preset System: Multiple predefined configurations
- Template Rendering: Dynamic HTML generation from data
- State Management: Internal state tracking
- Event Handling: Click, scroll, and keyboard events
- Namespacing: Global API under
window.Klef.DynamicIsland
Dynamic Island preset structure from dynamic-island.js:207-236
shared/components/dynamic-island/dynamic-island.js:1-1254 for the complete implementation.
Component Initialization Checks
Always check if required elements exist before initializing:Example from portfolio-grid.js:11-27
Component Communication
Components communicate through:- Custom Events: Dispatch events for lifecycle hooks
- Global Namespaces: Expose APIs under
window.Klef
- Direct Function Calls: For tightly coupled components
Best Practices
- IIFE Wrapper: Wrap component code in an Immediately Invoked Function Expression to avoid global scope pollution
- Existence Checks: Always verify DOM elements exist before manipulating them
- Console Logging: Include descriptive console messages with component name prefix
- Error Handling: Use try-catch blocks for error-prone operations
- Cleanup: Remove event listeners and clear timers in
disconnectedCallback()for Web Components - Responsive Design: Check viewport width and adjust behavior accordingly
- Accessibility: Include ARIA labels and keyboard navigation support
Related Files
- Load Basics:
shared/components/load-basics/load-basics.js - Mega Menu:
shared/components/mega-menu/mega-menu.js - Dynamic Island:
shared/components/dynamic-island/dynamic-island.js - Portfolio Grid:
shared/components/index-portfolio/portfolio-grid.js

