Custom code in formulas and actions is almost entirely vanilla javascript. However, there is a thin API to access toddle internals that you might find useful.
Custom actions and formulas can contain any number of functions, but they must contain their entry function. The entry function is named the same as your code file. If your file is called showModal then your entry function is called:
Formulas are functions. Given some inputs, they produce an output. This could be Add, which takes a and b and returns their sum. Almost all common functions already exist as built-in formulas in toddle. We have found that you can achieve nearly anything by composing the built-in formulas.
All toddle components may be exported as web components. This opens an important caveat, due to the nature of web components' Shadow DOM: DOM elements are inaccessible through the `document` scope. Trying to `document.getElementById` will work fine in the editor and pages, but if used in a component exported as a web component, the element will not be found. The solution is to use toddle's `ctx.root` element instead of `document`. Root is either equal to the `document` or the `Shadow Root` if loaded as a web component.
You can find the `root` in the context object (second parameter of your entry function) along with the rest of toddle's API.
toddle expects exactly one function to have the same name as your custom action. This is the entry point. It is called with two arguments:
Unlike formulas, actions do not have a return value. Instead, actions may perform callbacks to other workflows with the triggerActionEvent.
Events can be called asynchronously, so you could use a setTimeout or call an API, and then call your triggerActionEvent to call another workflow after a wait. This is exactly what the built-in sleep action does in toddle. toddle actions and formulas are built in the same way as your custom code. No magic ๐ช
Formulas are pure functions and are not meant to perform any side effects. Actions, however, often cause side-effects, such as setting a timeout with `setTimeout`, or adding event listeners `addEventListener`. To avoid event and memory leaks, you should aim to clean these effects up with `clearTimeout` etc.
If an action returns a function, that function will run when the parent component is unmounted. Unmouth happens when the component is no longer rendered, either due to the page closing or a show/hide or repeat changes somewhere in the tree that affects the component to no longer be rendered.