Ask toddle AI

๐Ÿ 

Docs home

๐Ÿ‘ถ

Getting started

๐ŸŽ“

1st week in toddle

๐Ÿ› ๏ธ

The editor

๐Ÿงฑ

Elements

๐Ÿ’…

Styling and layout

๐Ÿ“ฆ๏ธ

Components

๐Ÿ“„

Pages and navigation

โ˜๏ธ

Working with APIs

๐Ÿงฎ

Formulas

๐Ÿ””

Events and actions

๐Ÿ“ฆ

Packages

๐Ÿค–

Custom code

๐Ÿ‘ฏโ€โ™‚๏ธ

Integrations

๐Ÿฟ

Tutorials

๐Ÿงฐ

Other

Custom actions

In this tutorial, youโ€™ll learn how to create a custom formula or action that you can use in toddle โœจ

Tip: We've designed the built in formulas and actions so that virtually any problem can be solved without having to write a single line of code! Reach out to our toddle discord to discuss solving your problem without code.

Create a custom action.

An action is an effect response to an event or lifecycle. This effect could be a button causing the user to navigate to a different URL, write something to the console, or shoot confetti at an unexpecting reader ๐ŸŽ‰

Step 1

Open the custom code section on the left panel

Step 2

Now click the (+) button next to

Step 3

A custom action can execute any Javascript code. Additionally, custom actions run as modules, so you may import third-party ES6 modules .

In this example, we import canvas-confetti lazily when the action is called.

async function confetti(args, ctx) {
  const cavasConfetti = await import('https://cdn.jsdelivr.net/npm/canvas-confetti@1.6.0/+esm');
  cavasConfetti.default();
}

Step 4

We can now use the action in response to an event. Try adding a button with a "Click" event and run "confetti." This should already work; however, we can do even better!

Arguments

You can add arguments to your actions (and formulas). Arguments allow you to call the same action but with different data.

Add arguments that your action can take as input.

In this example, we have to be a bit careful to name the arguments similar to what canvas-confetti expects as its options, but you may call them whatever you'd like as long as you use the same name in your code.

Update code to pass args as the options for canvas-confetti. Also, post the number of particles to the console for debug purposes.

Events

Actions differ from formulas as they not only have arguments but also โœจeventsโœจ

Events let you call another workflow once it is triggered in your action. Since we load our confetti asynchronously, wouldn't it be nice to emit an event when the confetti bomb is popped ๐Ÿ’ฅ

For events, it's possible to specify example outputs. This allows us to describe what information an event might provide (e.g., a number or an object).

screenshot of the events section in custom actions

In our code, we can then trigger the event by accessing the current context

ctx.triggerActionEvent(

All done ๐ŸŽ‰

You should now be able to find and use your new custom action in your workflows.

Preview of the final action

For good order, this is how it could look: Demo time ๐ŸŽ‰

You can take toddle actions much further! Actions can be used to write to a browser-based database, control your smart fridge , and everything else a browser can do.