Custom elements JavaScript API

Jan Cerman
8 minutes
API
The Custom elements API is a JavaScript API that gives you the methods you need for implementing custom elements in Kontent.ai. The Custom elements API serves as a means of communication between your custom element and the Kontent.ai UI.

Inclusion in your app

When implementing your custom elements, you need to reference the Custom Elements JS API in your HTML5 app.
  • JavaScript
<script src="https://app.kontent.ai/js-api/custom-element/v1/custom-element.min.js"></script>
For more details, see our tutorial on Integrating your own content editing features.

init method

Use the init method to initialize your custom element. The init method expects a callback function which takes element and context as parameters. This callback function is called once your custom element is registered within Kontent.ai.
  • JavaScript
CustomElement.init(callback);
ParameterTypeDescription
callbackfunctionA callback function that receives the Element and Context objects which provide information from Kontent.ai about the custom element and about where it is displayed in the UI.
(element: Element, context: Context) => {
   // Initializes the custom element
   initMyCustomElement(element.value, element.disabled, element.config);
}

Context object

When using the custom element in a content component, the Context object refers to the parent content item of the content component, not the component itself. It's currently not possible to retrieve information about the component or access its other elements.
PropertyTypeDescription
projectIdstringThe environment’s ID.
item.idstringThe ID of the content item that contains the custom element.
item.codenamestringThe codename of the content item that contains the custom element.
item.collectionreference objectThe object represents an identifier of the item's collection, for example {"id": "00000000-0000-0000-0000-000000000000"}
item.namestringThe display name of the content item that contains the custom element
variant.idstringThe language's ID.
variant.codenamestringThe language's codename, for example, en-us.

Element object

PropertyTypeDescription
valuestring, nullThe custom element's initial value.
disabledbooleanIndicates whether an element is disabled for editing.
configobject, nullParameters of the custom element specified as a JSON object in the UI (in a content type or a content type snippet)

setValue method

Use the setValue method to set a custom element value. When the value is set to null, the element is considered empty. If you want the custom element content to be searchable in the content items list, specify the keywords or the plain text using value object. If a custom element is set as required in the UI, Kontent.ai checks the element's value. Any value other than null is considered a valid value and won't generate a validation error message. Custom elements can contain up to 100,000 characters. If an element exceeds this number, Kontent.ai will display an error.
  • JavaScript
CustomElement.setValue(value: string | null | ValueObject);
ParameterTypeDescription
valuestring, null, objectCustom element value to be set.

ValueObject object

ParameterTypeDescription
valueObject.valuestring, nullCustom element value to be set.
valueObject.searchableValuestring, nullPlain text searchable value used for searching in the content items list. Do not use any markup or JSON here to allow a correct search.

onDisabledChanged method

Use the onDisabledChanged method to define what should happen with a custom element when it's disabled for editing. The method takes a callback function. When disabled, the element is in read-only mode. This means that the element is shown in a content item but cannot be changed. For example, custom elements can be disabled in these cases:
  • The content item is published.
  • User is viewing an older version of a content item.
  • User doesn't have editing permissions.
  • JavaScript
CustomElement.onDisabledChanged(callback);
ParameterTypeDescription
callbackfunctionA callback function that receives a boolean indicating whether the element is disabled for editing.
(disabled: boolean) => {
   // Sets your custom element to read-only mode
   myCustomElement.updateDisabledState(disabled);
}

setHeight method

Use the setHeight method to set a custom height of a custom element. The value will be set in pixels.
  • JavaScript
CustomElement.setHeight(height);
ParameterTypeDescription
heightnumberCustom element height to be set in the Kontent.ai UI. Height must be a positive integer.

getElementValue method

Use the getElementValue method to retrieve the value of a different element. You can retrieve another element's value even if the custom element is disabled. The getElementValue method works on elements that are: If you need to get values of multiple elements, call the getElementValue method for each such element. If you need to get the value multiple times, call the method multiple times.
  • JavaScript
CustomElement.getElementValue(elementCodename, callback);
ParameterTypeDescription
elementCodenamestringCodename of the content element from which the value should be read.
callbackfunctionA callback function that receives the value of the specified element. The data type of the value varies based on the type of the content element.
(value: <mixed>) => {
 // Use the returned element value here
}

Data type of content elements

  • Custom element – string
  • Date & Time – ISO-8601 formatted string
  • Multiple choice – array of selected options. Each option is returned as an object based on the following schema: { id: <uuid>, name: <string>, codename: <string> }.
  • Text – string
  • URL slug – string
Other content elements are currently not supported.

observeElementChanges method

Use the observeElementChanges method to receive notifications about changes in other elements. The observeElementChanges method can listen to any element changes and does not depend on the list of elements allowed for reading. The observeElementChanges method does not return a specific element value but only notifies that the change occurred. The method takes a callback function as a second parameter. Use this method together with the getElementValue method to keep updated information about an element value.
  • JavaScript
CustomElement.observeElementChanges(elementCodenames, callback);
ParameterTypeDescription
elementCodenamesarray of stringsArray of codenames of the content elements to observe. When an empty array is provided, all elements are observed.
callbackfunctionA callback function that receives codenames of changed elements as an array of strings.
(changedElementCodenames: string[]) => {
  // React to changes in particular element
}

observeItemChanges method

Use the observeItemChanges method to receive notifications about changes in the item. The observeItemChanges method can listen to changes of the item's attributes – its name, codename, or collection. The method only notifies about the change, it doesn't return the new value of the changed attributes. It takes a callback function that is called whenever any of the attributes is changed. The callback function is called with a parameter that contains the item's new details.
  • JavaScript
CustomElement.observeItemChanges(callback);
ParameterTypeDescription
callbackfunctionA callback function that receives object with current item data.
(newItemDetails: ItemChangedDetails) => {
  // React to changes in a particular element
}

ItemChangedDetails object

PropertyTypeDescription
item.codenamestringThe codename of the content item that contains the custom element.
item.collectionreference objectThe object represents an identifier of the item's collection, for example {"id": "00000000-0000-0000-0000-000000000000"}
item.namestringThe display name of the content item that contains the custom element

selectAssets method

Use the selectAssets method to enable the custom element to select assets from Kontent.ai. The method takes a config object and, if a user selects assets from the dialog, returns a promise with an array of asset references containing the selected asset's ID. If the selection dialog is closed, the method returns null.
  • JavaScript
CustomElement.selectAssets(config: Config): Promise<AssetReference[] | null>

Config object

PropertyTypeDescription
allowMultiplebooleanSpecifies whether multiple assets can be selected.
fileTypestringSpecifies what type of images can be selected; allowed values are all and images.

AssetReference object

The object containing an ID reference to the selected asset.
PropertyTypeDescription
idGUIDThe asset's internal ID.

getAssetDetails method

Use the getAssetDetails method to get metadata about a specific asset. After getting IDs with the selectAssets method, you can then get more information about each of the assets returned. The getAssetDetails method takes an array of asset IDs as strings and returns a promise with an array of asset objects. The method returns a null if the specified items don't exist.
  • JavaScript
getAssetDetails(assetIds): Promise<Asset[] | null>
ParameterTypeDescription
assetIdsarray of GUIDsThe internal IDs of the assets to be returned.

Asset object

PropertyTypeDescription
idGUIDThe asset's internal ID.
descriptionsarray of asset description objectsThe descriptions of the asset in all available languages. 
fileNamestringThe name of the file.
imageHeightintegerThe asset's height in pixels.
imageWidthintegerThe asset's width in pixels.
namestringThe asset's display name.
sizeintegerThe size of the asset in bytes.
thumbnailUrlstringThe URL for the thumbnail of the asset.
titlestringThe asset's title.
typestringThe type of the file.
urlstringThe URL of the asset.

Asset description object

PropertyTypeDescription
language.idGUID The description's language ID.
language.codenamestringThe description's language codename.
descriptionstringThe asset's description for a specific language.

selectItems method

Use the selectItems method to enable the custom element to select content items from Kontent.ai. The method takes a config object and, if a user selects content items from the dialog, returns a promise with an array of content item references containing the selected item's ID. If the selection dialog is cancelled, the method returns null.
  • JavaScript
CustomElement.selectItems(config: Config): Promise<ContentItemReference[] | null>

Config object

PropertyTypeDescription
allowMultiplebooleanSpecifies whether multiple items can be selected.

ContentItemReference object

PropertyTypeDescription
idGUIDThe content item's internal ID.

getItemDetails method

Use the getItemDetails method to get details about a specific content item. After getting IDs with the selectItem method, you can then get details about each of the items returned. The getItemDetails method takes an array of content item IDs as strings and returns a promise with an array of content item objects. The method returns a null if the specified items don't exist.
  • JavaScript
CustomElement.getItemDetails(itemIds): Promise<ContentItem[] | null>
ParameterTypeDescription
itemIdsarray of GUIDsThe internal IDs of the items to be returned.

ContentItem object

PropertyTypeDescription
idGUIDThe item's internal ID.
codenamestringThe item's codename.
namestringThe item's display name.
collectionreference objectThe item's collection
typereference objectThe item's type.