> ## Documentation Index
> Fetch the complete documentation index at: https://docs.straddle.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Straddle Embed React component

> Integrate and customize the Straddle Embed onboarding form in your React application, including theming, callbacks, and merchant verification flows.

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.

> [!NOTE]
>
> This component automatically handles responsive layouts and dynamic height adjustment, making it ideal for a variety of application layouts and screen sizes.

## Key features

> [card] Responsive Layout
>
> Flexible width options from narrow to full-width to suit any layout needs

> [card] Dynamic Height
>
> Automatically adjusts height based on form content

> [card] TypeScript Support
>
> Built-in TypeScript definitions for reliable type checking

> [card] Error Handling
>
> Built-in error handling with customizable fallback options

## Installation and setup

> [card] [Visit the Github Repo](https://github.com/straddleio/embed)

**1. Install Dependencies**

Ensure React is installed in your project. The Straddle Embed Component is compatible with React 16.8 and above.

**2. Add Component Files**

Copy the `StraddleEmbedComponent.tsx` file into your project's component directory.

**3. Import and Use**

Import and implement the component in your application:

```tsx theme={null}
import React from 'react';
import StraddleEmbedComponent from './path/to/StraddleEmbedComponent';

function MyComponent() {
  return (
    <StraddleEmbedComponent 
      platformId="your-platform-id" 
      env="your-env-value" 
    />
  );
}
```

## Component props

**platformId** `string` *required*

Your unique Straddle platform identifier. This is used to associate the embedded form with your specific account or configuration.

**env** `string` *required*

A string value representing the Straddle environment in which form submissions are created (e.g., `production`, `sandbox`).

**maxWidth** `string` (default: 'full')

Controls the component's maximum width. Available options:

**Width Options**

**narrow** `string`

Sets maximum width to 384px (max-w-sm)

**medium** `string`

Sets maximum width to 768px (max-w-3xl)

**wide** `string`

Sets maximum width to 1024px (max-w-5xl)

**full** `string`

Uses 100% of container width

**auto** `string`

Removes max-width constraint

## Implementation examples

### Basic usage

```tsx Basic theme={null}
import React from 'react';
import StraddleEmbedComponent from './StraddleEmbedComponent';

function MyComponent() {
  return (
    <StraddleEmbedComponent 
      platformId="your-platform-id"
      env="sandbox" // Always use sandbox for testing
    />
  );
}
```

```tsx With Custom Width theme={null}
import React from 'react';
import StraddleEmbedComponent from './StraddleEmbedComponent';

function MyComponent() {
  return (
    <StraddleEmbedComponent 
      platformId="your-platform-id"
      env="sandbox"
      maxWidth="medium"
    />
  );
}
```

### Responsive implementation

```tsx Responsive Example theme={null}
import React, { useState, useEffect } from 'react';
import StraddleEmbedComponent from './StraddleEmbedComponent';

function ResponsiveStraddleEmbed() {
  const [maxWidth, setMaxWidth] = useState('full');

  useEffect(() => {
    const mediaQuery = window.matchMedia('(min-width: 768px)');
    
    const handleMediaQueryChange = (e) => {
      setMaxWidth(e.matches ? 'medium' : 'full');
    };

    mediaQuery.addListener(handleMediaQueryChange);
    handleMediaQueryChange(mediaQuery);

    return () => mediaQuery.removeListener(handleMediaQueryChange);
  }, []);

  return (
    <StraddleEmbedComponent 
      platformId="your-platform-id"
      env="sandbox"
      maxWidth={maxWidth}
    />
  );
}
```

## 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.

**URL Parameters** `object`

**Available Parameters**

**alignLeft** `boolean`

When set to 1, aligns all content to the left of the container

**hideTitle** `boolean`

When set to 1, removes the default form title

**transparentBackground** `boolean`

When set to 1, makes the form background transparent to match your application

**dynamicHeight** `boolean`

When set to 1, enables automatic height adjustment based on form content

**embed** `boolean`

When set to 1, optimizes the form for embedded display

### Using the `env` prop

> [!NOTE]
>
> 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:**\
  When `env` is set to a value indicating a sandbox or test environment (e.g., `env="sandbox"`), the embedded form creates 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:**\
  When `env` is set to `production`, the embedded form operates against your real, production-level environment. Accounts created, data submitted, and webhooks triggered from this embedded form 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

> [!NOTE]
>
> 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:

1. The `dynamicHeight=1` URL parameter enables the embed to communicate its content height.
2. A minimum height of 500px is enforced via inline styles to prevent layout shifts during loading.

> [!WARNING]
>
> 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:

```typescript Type Definitions theme={null}
interface StraddleEmbedComponentProps {
  platformId: string;
  env: string; // Now included as a required prop
  maxWidth?: 'narrow' | 'medium' | 'wide' | 'full' | 'auto';
}
```

```typescript Usage with Types theme={null}
import React from 'react';
import StraddleEmbedComponent from './StraddleEmbedComponent';

const MyComponent: React.FC = () => {
  return (
    <StraddleEmbedComponent 
      platformId="your-platform-id"
      env="production"
      maxWidth="medium"
    />
  );
};
```

### Script loading and error handling

The component implements robust script loading with automatic fallbacks:

**1. Primary Script Loading**

Attempts to load the Straddle embed script from '[https://forms.straddle.com/embed.js](https://forms.straddle.com/embed.js)'

**2. Fallback Mechanism**

If the script fails to load or execute properly, the component falls back to direct iframe source setting

**3. Error Recovery**

Implements comprehensive error handling for both script loading and execution failures

```typescript Error Handling Implementation theme={null}
const handleScriptError = () => {
  console.error('Failed to load the Straddle embed script.')
  loadEmbeds()
}

if (!existingScript) {
  const script = document.createElement('script')
  script.src = embedScriptSrc
  script.async = true
  script.onload = loadEmbeds
  script.onerror = handleScriptError
  document.body.appendChild(script)
}
```

## Advanced usage

### Error handling

```tsx Error Boundary theme={null}
import React from 'react';
import ErrorBoundary from './ErrorBoundary';
import StraddleEmbedComponent from './StraddleEmbedComponent';

function MyComponent() {
  return (
    <ErrorBoundary fallback={<div>Error loading Straddle form</div>}>
      <StraddleEmbedComponent 
        platformId="your-platform-id"
        env="production"
      />
    </ErrorBoundary>
  );
}
```

## Best practices

> [accordion] Platform ID Management
>
> While your `platformId` is not secret, it is recommended to store it in environment variables or a secure configuration management system.

> [accordion] Environment Value (env)
>
> Use the `env` prop to indicate the deployment environment (such as production, staging, or sandbox) to the embedded form. The form can then read this value and adjust its behavior or display accordingly.

> [accordion] Width Selection
>
> Choose the appropriate maxWidth based on your layout requirements. Use 'medium' for standard forms and 'full' for responsive layouts.

> [accordion] Error Handling
>
> Implement error boundaries to gracefully handle any loading or rendering issues with the embed component.

> [accordion] Performance
>
> The component uses lazy loading by default. Ensure the container has sufficient space to prevent layout shifts during loading.

### Custom component integration

The Straddle Embed Component can be wrapped with custom components to extend its functionality:

```tsx Custom Container theme={null}
import React from 'react';
import StraddleEmbedComponent from './StraddleEmbedComponent';

const CustomStraddleContainer: React.FC = () => {
  return (
    <div className="my-custom-container">
      <h2>Account Setup</h2>
      <StraddleEmbedComponent 
        platformId="your-platform-id"
        env="production"
        maxWidth="medium"
      />
      <div className="support-text">
        Need help? Contact support
      </div>
    </div>
  );
};
```

```tsx With Loading State theme={null}
import React, { useState } from 'react';
import StraddleEmbedComponent from './StraddleEmbedComponent';

const LoadingAwareStraddle: React.FC = () => {
  const [isLoading, setIsLoading] = useState(true);

  return (
    <div>
      {isLoading && <LoadingSpinner />}
      <StraddleEmbedComponent 
        platformId="your-platform-id"
        env="sandbox"
        onLoad={() => setIsLoading(false)}
      />
    </div>
  );
};
```

### Styling integration

The component uses Tailwind CSS classes for layout management. Here's how different width options map to Tailwind classes:

**Width Classes** `object`

**Tailwind Mappings**

**narrow**

Uses `max-w-sm` (384px)

**medium**

Uses `max-w-3xl` (768px)

**wide**

Uses `max-w-5xl` (1024px)

**full**

Uses `w-full` (100%)

**auto**

No width constraint

> [!NOTE]
>
> The component's container uses `flex justify-center w-full` to ensure proper centering and responsive behavior.

### Performance considerations

> [accordion] Script Loading
>
> The embed script is loaded asynchronously to prevent blocking page rendering. The component uses a single-instance pattern to avoid loading multiple copies of the script.

> [accordion] Resource Management
>
> The component properly cleans up event listeners and script references on unmount to prevent memory leaks.

> [accordion] Layout Stability
>
> The minimum height setting helps prevent Cumulative Layout Shift (CLS) during form loading.

## Troubleshooting

> [!NOTE]
>
> If you encounter issues with the embed component, verify these common points:

**1. Script Loading**

Ensure the Straddle embed script ([https://forms.straddle.com/embed.js](https://forms.straddle.com/embed.js)) is loading correctly in your network tab.

**2. 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.

**3. Container Size**

Check that the parent container provides adequate space for the embed to render properly.

**4. Network Connection**

Verify there are no network issues preventing the iframe content from loading.

## Related resources

> [card] [API Documentation](https://docs.straddle.com/api-reference)
>
> Complete API reference for Embed

> [card] [Quick Start Guide](https://docs.straddle.com/guides/quickstart.md)
>
> Get started with implementation examples

> [card] [Sandbox Testing](https://docs.straddle.com/guides/resources/sandbox-platform.md)
>
> Test your implementation in our sandbox environment

> [card] [Community Support](https://strddl.co/mmunity)
>
> Join our Slack community for additional help


## Related topics

- [Onboard merchants with Straddle Embed](https://docs.straddle.com/guides/embed/onboarding.md)
- [Straddle platform glossary](https://docs.straddle.com/guides/glossary.md)
- [Hosted onboarding for embedded accounts](https://docs.straddle.com/guides/embed/hosted-onboarding.md)
- [Straddle Embed for platforms overview](https://docs.straddle.com/guides/embed/overview.md)
- [Welcome to Straddle](https://docs.straddle.com/guides/overview.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.