Coded component examples

This page contains example code for some general use cases for the Coded component

Getting an element from the DOM

The DOM is a representation of a webpage that allows for programs to change it´s state, structure and content. The DOM also contains all of your HTML elements like `<div>` and `<p>`. You can read more about the DOM here.

To achieve this, use the `querySelector` method documented here.

HTML Content
<!--- HTML defined in the HTML Content property -->

<p id="text-content">My Text</p>
<button>Sign Up!</button>

We can retrieve these elements from `Script` property.

Script
// If you have defined a custom namespace on the Coded Component, use that instead
const textParagraph = appfarm.element.querySelector("#text-content") // Get an element by ID
const myButton = appfarm.element.querySelector("button") // Will get the first button in the DOM

// Do things with your paragraph and button...
// ...

Setting the value of a paragraph

If you have an `HTMLElement` that you wish to update the value of, you can do so like this. This will work for many other `HTMLElements` as well. You can read more about `innerHTML` here.

HTML Content
<!--- HTML defined in the HTML Content property -->

<p id="text-content">My Text</p>

Listening to events

Listening to events is necessary if you want to act on user input. In this example, we will listen to a button, but this pattern can be used for more `HTMLElements`. You can read more about EventListeners here.

Now, every time the user clicks the button, an alert dialog will appear.

Getting data from appfarm

In this example, we have defined an App Variable of type string in our model. This App Variable has been added as a `Read Only Value` on our Coded Component, with the name `userInput`. In the UI outside the Coded Component, this is bound to a TextEdit. To make the text from the TextEdit appear in our Coded Component, we can get the data from the App Variable.

Calling actions

You can also call actions from the Coded Component. There is also support for Action Params. In this example, we will call an action that opens a snackbar. The action takes one required Action Param, which is the text to show. The action has been added to the Coded Component and the Action Param `snackbarText` is databound as a `Code Function Param`. In the Coded Component, there is an input element and a button.

You can read more about the input-element here.

CSS

Inlining styles in HTML can be tedious. You can instead add stylesheets to your Coded Component from the Resources property. In this example we will defined our own stylesheet, but you can also import external stylesheets by choosing the "Stylesheet URL" - option.

Importing scripts and stylesheets

Scripts and stylesheets can be added to your resources as either `Script URL` or `Stylesheet URL`. This allows you to use external libraries to extend the functionality of the Coded Component. In this example we will add dayjs and animate.css to our Coded Component. In order to load external libraries in an Appfarm app, you also need to whiteliste the domain in the Environment settings for your solution. In this case, that would be `cdnjs.cloudflare.com` for `Script Sources` and `Style Sources`.

When using external libraries, these will be added to the Window object which you can read more about here. To check that your packages have been added correctly, attempt to call `window.dayjs` in your browser console.

Animation might not work if you have "Reduce Motion" enabled on your computer

Video showcasing the animations

Last updated

Was this helpful?