Example: Component Using Helper Utilities
This accordion component demonstrates how to use the helpers library in your components:
slideDown() and slideUp() - Smooth animationsdelegate() - Efficient event handlingOnly one item can be open at a time:
slideDown() and slideUp() helpers from the animation library for smooth transitions. Event handling is optimized using the delegate() helper, which attaches a single event listener to the parent element instead of one for each item.
delegate() improves performance by reducing the number of event listeners. Animation helpers use requestAnimationFrame for smooth, optimized animations. This results in better performance and smoother user experience.
No events yet. Click accordion items above.
Multiple items can be open simultaneously:
$() and $$() - Query selectorscreateElement() - Create elements with attributesaddClass(), removeClass() - Class manipulationdelegate() - Event delegationfadeIn(), fadeOut() - Fade animationsslideDown(), slideUp() - Slide animationsshake(), pulse(), bounce() - Motion effectsdebounce(), throttle() - Performance optimization
getFormData() - Extract form data as objectvalidateForm() - Validate with custom rulesshowFieldError() - Display validation errorsautoSaveForm() - Auto-save to localStorage
camelCase(), kebabCase() - Case conversiontruncate(), slugify() - String formattingformatCurrency(), formatDate() - Number/date formattingescapeHtml(), highlight() - Safety & display
Here's how the accordion component uses helpers:
import { slideDown, slideUp, delegate } from '../helpers/index.js';
class AccordionComponent extends HTMLElement {
attachEventListeners() {
// Use delegate helper for efficient event handling
delegate(this.shadowRoot, 'click', '.accordion-header', (event) => {
this.toggleItem(event.currentTarget.parentElement);
});
}
async openItem(item) {
const content = item.querySelector('.accordion-content');
// Use slideDown helper for smooth animation
await slideDown(content, 300);
}
async closeItem(item) {
const content = item.querySelector('.accordion-content');
// Use slideUp helper for smooth animation
await slideUp(content, 300);
}
}