Embed React Component
A comprehensive guide to integrating and customizing the Straddle embed form in your React application
The Embed component provides a seamless way to integrate Straddle forms into your React application. This guide covers everything you need to know about implementing and customizing the component.
This component automatically handles responsive layouts and dynamic height adjustment, making it ideal for a variety of application layouts and screen sizes.
Key Features
Responsive Layout
Flexible width options from narrow to full-width to suit any layout needs
Dynamic Height
Automatically adjusts height based on form content
TypeScript Support
Built-in TypeScript definitions for reliable type checking
Error Handling
Built-in error handling with customizable fallback options
Installation and Setup
Install Dependencies
Ensure React is installed in your project. The Straddle Embed Component is compatible with React 16.8 and above.
Add Component Files
Copy the StraddleEmbedComponent.tsx
file into your project’s component directory.
Import and Use
Import and implement the component in your application:
Component Props
Your unique Straddle platform identifier. This is used to associate the embedded form with your specific account or configuration.
A string value representing the Straddle environment in which form submissions are created (e.g., production
, sandbox
).
Controls the component’s maximum width. Available options:
Implementation Examples
Basic Usage
Responsive Implementation
Configuration Details
URL Parameters
The Straddle embed URL includes several preconfigured parameters that control the form’s appearance and behavior. These parameters are automatically set in the component for optimal integration. The env
value is not injected into the URL, but is instead provided as a separate data attribute, making it accessible to the embedded form internally.
Using the env
Prop
The env
prop allows you to indicate whether the embedded Straddle form should create a test account (in your platform’s sandbox environment) or a real, production-level account. This is particularly useful when you’re testing webhook integrations, trialing new features, or verifying payment flows before going live.
How It Works:
-
Sandbox Environment:
Whenenv
is set to a value indicating a sandbox or test environment (e.g.,env="sandbox"
), the embedded form will create new accounts in your sandbox environment. This ensures you can safely experiment, test your integrations, and verify webhook responses without impacting live data or existing real accounts. -
Production Environment:
Whenenv
is set toproduction
, the embedded form operates against your real, production-level environment. Accounts created, data submitted, and webhooks triggered from this embedded form will reflect actual usage scenarios and should be treated as live, customer-facing transactions.
No Direct URL Changes:
The env
value is not appended to the Straddle form URL. Instead, it’s passed as a data attribute on the iframe (i.e., env
), allowing the form itself (within the iframe) to interpret and act upon the environment setting. This ensures that the same URL can be used consistently, while the underlying logic—such as which database endpoints or webhook routes to invoke—is dynamically adjusted based on env
.
Recommended Usage:
- Use
env="sandbox"
whenever you’re testing new integrations, verifying that your webhook listeners work correctly, or experimenting with new features. - Switch to
env="production"
only when you’re ready for real-world usage, ensuring that account creations, messages, and webhooks are fully integrated into your live environment.
Dynamic Height Behavior
The component implements a sophisticated height management system that combines fixed minimum heights with dynamic content adjustment.
The dynamic height functionality works through two mechanisms:
-
The
dynamicHeight=1
URL parameter enables the embed to communicate its content height. -
A minimum height of 500px is enforced via inline styles to prevent layout shifts during loading.
Modifying the minimum height may cause layout instability during the initial load. The default 500px value is recommended for most use cases.
TypeScript Integration
The component includes comprehensive TypeScript definitions for enhanced development experience:
Script Loading and Error Handling
The component implements robust script loading with automatic fallbacks:
Primary Script Loading
Attempts to load the Straddle embed script from ‘https://forms.straddle.io/embed.js’
Fallback Mechanism
If the script fails to load or execute properly, the component falls back to direct iframe source setting
Error Recovery
Implements comprehensive error handling for both script loading and execution failures
Advanced Usage
Error Handling
Best Practices
Custom Component Integration
The Straddle Embed Component can be wrapped with custom components to extend its functionality:
Styling Integration
The component uses Tailwind CSS classes for layout management. Here’s how different width options map to Tailwind classes:
The component’s container uses flex justify-center w-full
to ensure proper centering and responsive behavior.
Performance Considerations
Troubleshooting
If you encounter issues with the embed component, verify these common points:
Script Loading
Ensure the Straddle embed script (https://forms.straddle.io/embed.js) is loading correctly in your network tab.
Platform ID & env
Verify your platformId
and env
values are correct and properly formatted. The env
prop should match the environment context you intend the form to operate within.
Container Size
Check that the parent container provides adequate space for the embed to render properly.
Network Connection
Verify there are no network issues preventing the iframe content from loading.