What is a Modal Component?
In FormEngine Core, a modal component is a React component that:- Displays content in an overlay dialog that appears above the main form
- Hosts nested forms - essentially displaying a child form within the modal
- Manages its own open/close state through props provided by the framework
- Integrates with form actions like
openModalandcloseModal - Can pass data between the parent form and the modal form
The CustomModalProps Interface
All custom modal components have to implement theCustomModalProps interface. This interface defines the minimal properties that
FormEngine Core provides to modal components:
Creating a Basic Custom Modal Component
Let’s create a simple modal component using plain HTML and CSS:The componentRole(‘modal’) Method
The.componentRole('modal') method is essential for modal components. It tells FormEngine Core that:
- This component can be used as a modal container in form settings
- It will receive the
openandhandleCloseprops automatically - It can host nested forms that will be rendered as its children
- It integrates with modal actions like
openModalandcloseModal
Live Example
live
How Modal Components Work with Nested Forms
When you use a modal component in a form, FormEngine Core automatically:- Renders your modal component with the
openandhandleCloseprops - Passes a nested form as children to your modal component
- Manages the modal state through form actions
- Handles data passing between parent and child forms
Best Practices for Custom Modal Components
1. Always Implement CustomModalProps
Ensure your component accepts at minimum theopen and handleClose props.
2. Use componentRole(‘modal’)
Without this, FormEngine Core won’t recognize your component as a modal.3. Handle Accessibility
- Add ARIA attributes:
role="dialog",aria-modal="true",aria-labelledby - Support keyboard navigation (Escape to close, Tab trapping)
- Manage focus (focus on first interactive element when opening, return focus when closing)
4. Prevent Body Scroll
When modal is open, prevent scrolling on the underlying page:5. Support Nested Forms
Remember that your modal component will receive nested forms as children. Ensure your component’s content area can properly render React children.6. Test with Different UI Libraries
If supporting multiple UI libraries, ensure consistency in prop names and behavior.Troubleshooting Common Issues
Issue: Modal doesn’t open
- Check: Did you call
.componentRole('modal')? - Check: Is the modal component registered in the form’s
modalTypesetting? - Check: Are you using the correct
modalKeyin theopenModalaction?
Issue: handleClose not working
- Check: Are you calling
props.handleClose()in your close button handler? - Check: Does your modal component properly pass the
handleCloseprop to the underlying UI component?
Issue: Nested form not displaying
- Check: Does your modal component render its
childrenprop?
Issue: Modal state not updating
- Check: Are you using the
openprop to control visibility? - Check: Are you overriding the
openprop with local state? (Don’t do this - use the prop directly)
Summary
Custom modal components in FormEngine Core are powerful tools for creating complex, nested form interfaces. By implementing theCustomModalProps interface and using the .componentRole('modal') method, you can create modal dialogs that:
- Host nested forms
- Integrate with form actions
- Pass data between forms
- Work with any UI library
- Provide rich user experiences
Next Steps
- Learn about modal actions and events for advanced modal workflows
- Explore nested forms and data passing for complex form hierarchies