initAppElement
Usage
Initialize app element client
import { initAppElement } from "@canva/design";const appElement = initAppElement<{ content: string }>({render: (data) => {return [{type: 'text',children: [data.content],top: 100,left: 100,width: 200}];}});
Initialize V2 app element client
import { initAppElement } from "@canva/design";const appElement = initAppElement<{ content: string }>({render: (data) => {return [{type: 'text',children: [data.content],top: 100,left: 100,width: 200}];}});
Parameters
appElementConfigAppElementClientConfiguration<A>Configuration for an AppElementClient
renderAppElementRenderer<A>Registers a callback that renders an app element based on the data it receives.
Parameters
appElementDataAThe data the callback must use to render the app element.
A primitive data type that can be used in app element data.
All primitive data types are supported except for symbols and bigints.
An object primitive data type that can be used in app element data.
Returns
An array of one or more elements to render as output of an app element.
AppElementRendererOutput
Returns
A client that provides methods for creating and managing the lifecycle of an app element.
addOrUpdateElementfunctionThis type has been superseded, use addElement or registerOnElementChange instead.
If an app element is selected, the element's data is overwritten and the element is re-rendered.
Otherwise, the provided data is used to create a new app element.
Parameters
appElementDataAThe data to attach to the app element. Existing data will be overwritten.
A primitive data type that can be used in app element data.
All primitive data types are supported except for symbols and bigints.
An object primitive data type that can be used in app element data.
placementPlacementThe position, dimensions, and rotation of the app element.
topnumberThe distance from the top edge of the container, in pixels.
- The pixels are relative to their container.
Minimum: -32768
Maximum: 32767
leftnumberThe distance from the left edge of the container, in pixels.
- The pixels are relative to their container.
Minimum: -32768
Maximum: 32767
widthnumberA width, in pixels.
- The pixels are relative to their container.
Minimum: 0
Maximum: 32767
heightnumberA height, in pixels.
- The pixels are relative to their container.
Minimum: 0
Maximum: 32767
rotationnumberA rotation, in degrees.
Minimum: -180
Maximum: 180
Returns
Promise<void>
Examples
Create or update an element (deprecated)
import { initAppElement } from "@canva/design";// Initialize the app element clientconst appElement = initAppElement<{ content: string; timestamp: number }>({render: (data) => {return [{type: 'text',children: [data.content || 'Default text'],top: 100,left: 100,width: 200}];}});// Create a new element or update selected elementawait appElement.addOrUpdateElement({content: 'Hello from the app',timestamp: Date.now()});
Update with specific placement (deprecated)
import { initAppElement } from "@canva/design";const appElement = initAppElement<{ content: string }>({render: (data) => {return [{type: 'text',children: [data.content || 'Default text'],top: 0,left: 0,width: 200}];}});// Create or update with specific placementawait appElement.addOrUpdateElement({content: 'Positioned content'},{top: 200,left: 200,width: 300,height: 100});
addElementfunctionAdds a new app element to the design.
Parameters
optsAppElementOptions<A>The data and placement of the app element.
dataAThe data to attach to the app element.
A primitive data type that can be used in app element data.
All primitive data types are supported except for symbols and bigints.
An object primitive data type that can be used in app element data.
placementPlacementThe position, dimensions, and rotation of the app element.
topnumberThe distance from the top edge of the container, in pixels.
- The pixels are relative to their container.
Minimum: -32768
Maximum: 32767
leftnumberThe distance from the left edge of the container, in pixels.
- The pixels are relative to their container.
Minimum: -32768
Maximum: 32767
widthnumberA width, in pixels.
- The pixels are relative to their container.
Minimum: 0
Maximum: 32767
heightnumberA height, in pixels.
- The pixels are relative to their container.
Minimum: 0
Maximum: 32767
rotationnumberA rotation, in degrees.
Minimum: -180
Maximum: 180
Returns
Promise<void>
Examples
Add new element with data
import { initAppElement } from "@canva/design";const appElement = initAppElement<{ content: string; id: string }>({render: (data) => {return [{type: 'text',children: [data.content || 'Default text'],top: 0,left: 0,width: 200}];}});// Add a new elementawait appElement.addElement({data: {content: 'New element content',id: 'element-' + Date.now()}});
Add element with specific placement
import { initAppElement } from "@canva/design";const appElement = initAppElement<{title: string;description: string;createdAt: number;}>({render: (data) => {return [{type: 'text',children: [data.title || 'Default title'],top: 0,left: 0,width: 300,fontWeight: 'bold',fontSize: 24}, {type: 'text',children: [data.description || 'Default description'],top: 50,left: 0,width: 300}];}});// Add element with specific placementawait appElement.addElement({data: {title: 'Element Title',description: 'This is a description of the element',createdAt: Date.now()},placement: {top: 100,left: 100,width: 400,height: 150,rotation: 0}});
registerOnElementChangefunctionA callback that runs when:
- the app element is created
- the app element's data is updated
- the user selects an existing app element
Parameters
handlerAppElementChangeHandler<A>The callback to run when the app element changes.
Parameters
appElementobject | undefinedInformation about the app element that was changed.
dataAThe app element data in its most recent state.
A primitive data type that can be used in app element data.
All primitive data types are supported except for symbols and bigints.
An object primitive data type that can be used in app element data.
versionnumberThe version number of the app.
updatefunctionFunction to update the app element data.
Examples
Update element data only
if (element) {await element.update({data: {...element.data,content: 'Updated content',lastUpdated: Date.now()}});}
Update data and placement
if (element) {await element.update({data: {...element.data,content: 'Positioned element'},placement: {top: 200,left: 200,width: 300,height: 100}});}
Add metadata to element
if (element) {await element.update({data: {...element.data,metadata: {...(element.data.metadata || {}),lastEdited: Date.now(),editCount: (element.data.metadata?.editCount || 0) + 1,},},});}
Parameters
optsAppElementOptions<A>The data and placement to update the app element with.
dataAThe data to attach to the app element.
A primitive data type that can be used in app element data.
All primitive data types are supported except for symbols and bigints.
An object primitive data type that can be used in app element data.
placementPlacementThe position, dimensions, and rotation of the app element.
topnumberThe distance from the top edge of the container, in pixels.
- The pixels are relative to their container.
Minimum: -32768
Maximum: 32767
leftnumberThe distance from the left edge of the container, in pixels.
- The pixels are relative to their container.
Minimum: -32768
Maximum: 32767
widthnumberA width, in pixels.
- The pixels are relative to their container.
Minimum: 0
Maximum: 32767
heightnumberA height, in pixels.
- The pixels are relative to their container.
Minimum: 0
Maximum: 32767
rotationnumberA rotation, in degrees.
Minimum: -180
Maximum: 180
Returns
Promise<void>
Returns
void
Returns
void
Examples
Handle element selection and update
import { initAppElement } from "@canva/design";const appElement = initAppElement<{ content: string }>({render: (data) => {return [{type: 'text',children: [data.content || 'Default text'],top: 0,left: 0,width: 200}];}});// Register a handler for element changesappElement.registerOnElementChange((element) => {if (element) {// Element is created or selected// Optionally update the element// element.update({// data: { ...element.data, lastSelected: Date.now() }// });} else {// No element is selected}});
Update element when selected
import { initAppElement } from "@canva/design";const appElement = initAppElement<{content: string;metadata: { created: number; lastSelected: number };}>({render: (data) => {// Render based on datareturn [{type: 'text',children: [data.content || ''],top: 0,left: 0,width: 200}];}});// Update element when selectedappElement.registerOnElementChange(async (element) => {if (element) {// Check if this is a newly created or a selected elementconst isNewElement = !element.data.metadata?.created;if (isNewElement) {// Update a new element with initial metadataawait element.update({data: {...element.data,metadata: {created: Date.now(),lastSelected: Date.now()}}});} else {// Update existing element's last selected timeawait element.update({data: {...element.data,metadata: {...element.data.metadata,lastSelected: Date.now()}}});}}});