Webflow Module Creation
This skill guides you through creating and registering custom modules that are automatically mounted based on DOM attributes.
Core Principles
- File Location: All modules must be created in
src/modules/. - Naming: The filename (e.g.,
test.ts) must exactly match thedata-moduleattribute value (e.g.,data-module="test"). Use kebab-case and a name that says what the module does, notmoduleorindex. - Structure: Every module must have a default export function.
Module Template
pnpm new:module <name> writes a module like this one, which already passes every rule:
import { onMount, onDestroy } from "@/modules/_";
/**
* @param element The DOM element with the data-module attribute
* @param dataset The data attributes of the element
*/
export default function (element: HTMLElement, dataset: DOMStringMap) {
// Setup logic here
onMount(() => {
// Initialization code
});
onDestroy(() => {
// Cleanup code
});
}
Step-by-Step Creation
- Create the File: Run
pnpm new:module my-component, or add a new.tsfile insrc/modules/(e.g.,src/modules/my-component.ts). - Implement the Function: Export a default function with the signature
(element: HTMLElement, dataset: DOMStringMap) => void. Fewer parameters are fine. - Register in HTML: Add the
data-module="my-component"attribute to the target element in Webflow/HTML. - Access Data: Use the
datasetparameter to accessdata-attributes passed from the DOM. - Check: Run
pnpm check. A module that usesRaf.add,onViewor GSAP must also callonEditor(see the editor skill), and everything it subscribes to must be undone inonDestroy.
Best Practices
- Keep modules focused on a single responsibility.
- Use descriptive names that match the component's purpose.
- Ensure the filename is lowercase and uses hyphens if needed (e.g.,
image-gallery.ts). - The module rules (
module/default-export,module/signature,module/filename) are in House Rules.