| name | create-modus-wrapper-component |
| description | Scaffold a new Modus wrapper component following established patterns with proper TypeScript interfaces, event handling, and cleanup |
Create Modus Wrapper Component
Scaffold a new Modus wrapper component following established patterns from the codebase.
When to Use
Use this skill when:
- Creating a new wrapper component for a Modus web component
- You need to integrate a Modus component that doesn't have a wrapper yet
- You want to ensure proper event handling and TypeScript types
Pattern Overview
All Modus wrapper components follow this structure:
- Import the web component from
@trimble-oss/moduswebcomponents-react
- Define TypeScript props interface with JSDoc comments
- Use
useRef for component reference
- Use
useEffect for event listeners with proper cleanup
- Forward props to the web component with conditional spreading
- Handle events via event listeners, not React props
Reference Examples
- Simple wrapper:
src/components/ModusButton.tsx - No event listeners needed
- With event listeners:
src/components/ModusCheckbox.tsx - Shows event handling pattern
- With dropdown:
src/components/ModusDropdownMenu.tsx - Shows menu event handling
Implementation Template
import { useEffect, useRef } from "react";
import { ModusWc[ComponentName] } from "@trimble-oss/moduswebcomponents-react";
import type { ReactNode } from "react";
export interface Modus[ComponentName]Props {
propName?: string;
onEventName?: (event: CustomEvent<EventDetailType>) => void;
customClass?: string;
"aria-label"?: string;
}
export default function Modus[ComponentName]({
propName,
onEventName,
customClass,
"aria-label": ariaLabel,
}: Modus[ComponentName]Props) {
const componentRef = useRef<HTMLModusWc[ComponentName]Element>(null);
useEffect(() => {
const component = componentRef.current;
if (!component) return;
const handleEventName = (event: Event) => {
onEventName?.(event as CustomEvent<EventDetailType>);
};
if (onEventName) {
component.addEventListener("eventName", handleEventName);
}
return () => {
if (onEventName) {
component.removeEventListener("eventName", handleEventName);
}
};
}, [onEventName]);
return (
<ModusWc[ComponentName]
ref={componentRef}
prop-name={propName}
custom-class={customClass}
aria-label={ariaLabel}
/>
);
}
Key Patterns
1. Conditional Prop Spreading
For optional props that shouldn't be passed when undefined:
<ModusWcComponent
{...(color && { color })}
{...(variant && { variant })}
size={size}
/>
<ModusWcComponent
color={color} // May be undefined
variant={variant} // May be undefined
/>
Reference: src/components/ModusButton.tsx:229-230
2. Event Listener Setup
Always use useEffect with cleanup:
useEffect(() => {
const component = componentRef.current;
if (!component) return;
const handleEvent = (event: Event) => {
onEvent?.(event as CustomEvent<EventDetailType>);
};
if (onEvent) {
component.addEventListener("eventName", handleEvent);
}
return () => {
if (onEvent) {
component.removeEventListener("eventName", handleEvent);
}
};
}, [onEvent]);
Reference: src/components/ModusCheckbox.tsx:91-150
3. TypeScript Types
Use proper types for web component elements:
const componentRef = useRef<HTMLModusWcButtonElement>(null);
const componentRef = useRef<HTMLModusWcCheckboxElement>(null);
const componentRef = useRef<HTMLModusWcDropdownMenuElement>(null);
4. Prop Naming
Web components use kebab-case for props:
<ModusWcComponent custom-class={customClass} />
<ModusWcModal modal-id={modalId} />
5. Event Handler Types
Always type event handlers properly:
onEventName?: (event: CustomEvent<{ value: string }>) => void;
const handleEvent = (event: Event) => {
onEventName?.(event as CustomEvent<{ value: string }>);
};
Common Event Names
Modus components use these common event names:
inputChange - For input value changes
inputFocus - For focus events
inputBlur - For blur events
buttonClick - For button clicks
itemSelect - For menu/dropdown item selection
menuVisibilityChange - For dropdown menu visibility
expandedChange - For accordion/collapse state
Check the Modus documentation for component-specific events.
Accessibility
Always include:
aria-label prop for icon-only or non-text components
- Proper ARIA attributes passed to web component
- Keyboard navigation support (usually handled by web component)
Testing Checklist
Common Mistakes to Avoid
- Missing cleanup: Always return cleanup function from
useEffect
- Wrong event names: Check Modus docs for exact event names
- Passing undefined: Use conditional spreading for optional props
- Wrong prop names: Web components use kebab-case, React uses camelCase
- Missing null checks: Always check
componentRef.current before use
Next Steps
After creating the wrapper:
- Export it from
src/components/index.ts (if exists)
- Create a demo page in
src/demos/[component]-demo/page.tsx
- Add to component reference documentation
- Test all props and events