Canva Apps SDK Starter Kit

repository·main·Indexed 42 days ago

https://github.com/canva-sdks/canva-apps-sdk-starter-kit

A React and TypeScript development boilerplate for building, previewing, and testing Canva apps. It includes a suite of API examples for creating app elements (text, images, video, and vector shapes), managing media asset uploads, integrating Digital Asset Management (DAM) systems, and handling typography.

Tokens
42.8K
Snippets
95
Records
184
Agent score
54%

What's inside Canva Apps SDK Starter Kit

  1. Overview of Design Editing capabilities

    main

    The Design Editing API allows for advanced manipulation of a Canva design. This includes:

    • Element updates: Modifying existing elements.
    • Transformations: Scaling, rotating, or moving elements.
    • Deletions: Removing elements from the design.
    • Insertions: Adding new elements to the design.
    • Grouping operations: Creating or managing groups of elements.

    For production-grade implementations, it is recommended to use intents to manage these workflows.

  2. Overview of the Design Token example

    main

    The Design Token example demonstrates how to obtain design tokens to access design metadata through external APIs. It covers the full lifecycle of design data interaction, including:

    • Token generation: Creating the necessary tokens for access.
    • Authentication integration: Integrating these tokens into your app's auth flow.
    • Design data retrieval: Using the tokens to fetch design metadata via APIs.

    This example is intended for learning patterns and is not a production-ready template. For full API reference and detailed instructions, visit the official Canva documentation at https://www.canva.dev/docs/apps/examples/design-token/.

  3. Rich text replacement example overview

    main

    The richtext_replacement example demonstrates how to programmatically replace and modify selected rich text elements within a Canva design. It covers:

    • Formatting changes: Applying styles such as bold, italics, and other text properties.
    • Advanced text selection: Manipulating how text is selected for editing.
    • Formatting manipulation: Modifying existing text properties.

    Important Production Considerations: This example is a demonstration and does not meet all requirements for a public app submission to Canva. When building production apps, you must:

    • Replace hardcoded operations with user input interfaces or dynamic content.
    • Implement robust handling for complex formatting scenarios to preserve user styling.
    • Implement comprehensive error handling with clear user feedback.
    • Implement internationalization using the @canva/app-i18n-kit package.
    • For apps using intents, use the prepareDesignEditor function from src/intents/design_editor/index.tsx instead of the simplified structure used here.
  4. Text translation patterns and workflows

    main

    The Text translation example demonstrates advanced text transformation patterns within a Canva App. It covers:

    • Translation workflows: Implementing translation both with and without preserving existing text formatting.
    • Bulk editing: Managing text transformations across multiple elements.
    • Content session management: Handling the lifecycle of content changes.
    • Complex replacement: Executing sophisticated text replacement workflows.

    Note for Production Apps: This example is a demonstration and differs from production requirements in several ways:

    • Translation Services: Use real translation services instead of the mock data provided here.
    • Error Handling: Implement comprehensive error handling and user feedback.
    • Formatting: Ensure robust handling of various text formats.
    • Internationalization: Use the @canva/app-i18n-kit package to support multiple languages.
    • Intents: For production apps using intents, it is recommended to call the prepareDesignEditor function from src/intents/design_editor/index.tsx instead of the simplified structure used in this example.
  5. Overview of the Content Publisher Template

    main

    The Content Publisher template is a starting point for building apps that publish content to external platforms. It implements the required Content Publisher intent structure.

    Key components include:

    • Intent Implementation: Four required functions: renderSettingsUi, renderPreviewUi, getPublishConfiguration, and publishContent.
    • Settings UI: A form (found in settings_ui.tsx) for users to configure publishing options.
    • Preview UI: A visual representation (found in preview_ui.tsx) of how content will appear on the target platform.
    • App UI Kit: Uses React-based components to match Canva's design system.

    Prerequisite: You must enable the Content Publisher intent on the Intents page in the Canva Developer Portal.

  6. Production requirements for metadata handling

    main

    When moving from this demonstration example to a production app intended for Canva review, you must address the following simplifications:

    • Metadata Handling: Do not use simplified logic. Production apps should handle metadata gracefully and use it to drive context-aware functionality.
    • Error Handling: Implement comprehensive error handling with clear user feedback and graceful failure modes instead of the simplified versions used in this demo.
    • Internationalization: You must support multiple languages. Use the @canva/app-i18n-kit package to meet Canva review requirements.
    • Code Structure & Intents: For apps using intents, it is recommended to call the prepareDesignEditor function located in src/intents/design_editor/index.tsx rather than using the simplified structure shown in this example.
  7. Customize the backend host using `BACKEND_HOST`

    main

    To manage different URLs for development and production, use the CANVA_BACKEND_HOST environment variable in your .env file.

    Webpack makes the BACKEND_HOST constant available globally in your application code without needing an import. Use it as the base URL for all fetch requests.

    Example usage:

    const response = await fetch(`${BACKEND_HOST}/custom-route`);
  8. Structure an app to implement multiple intents

    main

    When building a Canva app that supports multiple intents (e.g., both a Design Editor and a Content Publisher), use a modular structure where a central entry point initializes each intent separately.

    1. Root Entry Point: The main entry file (e.g., src/index.tsx) should call a prepare function for every intent you want to support.
    2. Intent Modules: Create a dedicated folder for each intent within a src/intents/ directory. Each folder should contain an index file that implements the specific intent contract.
    3. App Configuration: Ensure your canva-app.json file includes both intents in its enrollment configuration.

    Example Pattern

    In src/index.tsx:

    // Import implementations from their respective intent folders
    import { DesignEditorIntent } from './intents/design_editor/index.tsx';
    import { ContentPublisherIntent } from './intents/content_publisher/index.tsx';
    
    // Call the prepare function for each intent
    prepareDesignEditor(DesignEditorIntent);
    prepareContentPublisher(ContentPublisherIntent);
  9. Understand the difference between design elements and app elements

    main

    When building Canva apps, you can choose between adding regular design elements or using app elements.

    • Design Elements: Standard elements added to a design.
    • App Elements: Specialized elements that allow for more complex app interactions and behaviors.

    For more advanced interaction patterns, refer to the App image elements example.

  10. Production requirements for text elements

    main

    When moving from this demonstration example to a production app intended for public release on Canva, you must address the following limitations:

    • Dynamic Content: Replace hardcoded text and styling with comprehensive text editing interfaces or dynamic content loading.
    • Font Handling: Implement font validation and provide fallback options.
    • Internationalization: Support multiple languages using the @canva/app-i18n-kit package.
    • Design Editor Integration: For apps using intents, it is recommended to call the prepareDesignEditor function from src/intents/design_editor/index.tsx instead of using simplified structures.
    • Accessibility: Ensure the app meets WCAG 2.0 AA standards.
  11. Autofill a design using data field matching

    main

    This pattern demonstrates how to automate the process of filling design elements with data. The workflow involves three main steps:

    1. Identify tagged fields: Use getDesignMetadata to read which elements in the current design have been tagged with specific field labels.
    2. Match data fields: Use requestDataFieldMatching to allow the user to map their data source columns (e.g., "Name", "Role") to the labels tagged in the Canva design.
    3. Apply data: Use autofillDesign to populate the tagged design elements with the values from a DataTable.

    Note that the DataTable shape used here is defined by the DataTable type from @canva/intents/data, which is the same format returned by a Data Connector's getDataTable method.

    import { 
      requestDataFieldMatching, 
      autofillDesign, 
      getDesignMetadata 
    } from '@canva/design';
    import { DataTable } from '@canva/intents/data';
    
    // Example workflow logic:
    // 1. const metadata = await getDesignMetadata();
    // 2. await requestDataFieldMatching(sampleTable);
    // 3. await autofillDesign(sampleTable);
  12. Production requirements for image replacement apps

    main

    When moving from this demonstration example to a production app intended for Canva review, you must implement the following improvements:

    • Asset Hosting: Do not use static assets. Host assets on a CDN or hosting service and use the upload function from the @canva/asset package.
    • Format Validation: Implement image format validation and handle unsupported formats gracefully.
    • Error Handling: Implement comprehensive error handling with clear user feedback and graceful failure modes.
    • Internationalization: Support multiple languages using the @canva/app-i18n-kit package.
    • Intents Integration: For apps using intents, it is recommended to call the prepareDesignEditor function from src/intents/design_editor/index.tsx.
    • User Feedback: Replace basic loading states with detailed progress indicators for long-running operations.