This guide provides a comprehensive, step-by-step process to create your first custom widget using the TakeProfit Widget SDK.
my-tp-widget
with your desired project name.
app
directory.
app
directory among others.
package.json
to ensure takeprofit-widget-sdk
is listed under dependencies
:
app
directory, create a new folder named widget
and add a page.jsx
file:
app/widget/page.jsx
and add the following code:
TPWidgetSDK.connect()
, and the loader is hidden with TPWidgetSDK.hideLoader()
.next.config.js
In the root of your project, create or modify the next.config.js
file to include the following configuration:
X-Frame-Options
: This header controls whether your site can be embedded in an iframe. Setting it to ALLOW-FROM https://takeprofit.com
allows the widget to be embedded only from the specified origin.Content-Security-Policy
: The frame-ancestors
directive specifies valid parents that may embed a page using <frame>
, <iframe>
, <object>
, <embed>
, or <applet>
. Setting it to 'self' https://takeprofit.com
allows framing from the same origin and the specified TakeProfit domain.postMessage
for communication between the widget (iframe) and the parent TakeProfit platform. It handles various message types to manage state and interactions.
TPWidgetSDK.connect()
. However, ensure that your widget’s domain is trusted by the TakeProfit platform to allow communication.
WidgetPage
component to include functions for adding, updating, and removing menu items.
useState
to keep track of the added menu item’s ID.http://localhost:3000
.
F12
or Ctrl+Shift+I
).
<your-iframe-url>
and <icon-url>
with your actual widget URL and an icon URL:
iframeUrl
in local storage with your deployed widget’s URL:
requestId
and menuItemId
are unique to prevent conflicts.
unsubscribeWidgetChange
and unsubscribeChannelChange
to prevent memory leaks.