Link Types

Link types are actions that can be taken on click of a link or button. The following link types are available by default.

840

Each of these generate a URL with required input from the user. For example, Open Website asks the user for a URL and Target.

844

Adding a New Link Type

You can add your own link types. Every link type has the ability to add a URL to href or a JavaScript function to the onClick method of the link or button HTML tag. This could be a simple static URL, or a URL generated with user input.

You have to define the following parameters for each link type.

Parameters

NameDescription
nameUnique name for the link type
labelDisplay label for the link type
attrsAn object where we define the link value. For URL based links, it will have href and target. For JavaScript links, this will have onClick.
fieldsThis is where we will define the input fields that the user can fill to generate the URL. Check the input field parameters below.

Input Field Parameters

If you need to get input from users to create the URL for your link, this is how you can define the fields.

NameDescription
nameUnique name for the input field
labelDisplay label for the input field
defaultValueDefault value for the input field
inputTypeInput type for the field. This is the HTML input type. Examples: text, email, number, etc.
placeholderTextPlaceholder text for the input field
optionsOnly required for dropdown. An array of objects containing all the option labels and values.

Examples

Here are a few examples that demonstrate different link types.

Static Link

In this example, we will add a simple https://google.com static link as an available link type. If a user selects this action, the button will simply open Google in a new tab.

844
unlayer.setLinkTypes([
  {
    name: "static_google_link",
    label: "Go to Google",
    attrs: {
      href: "https://google.com/",
      target: "_blank"
    },
  }
]);

Input Generated Link

In this example, we will ask the user to enter an email and choose a country to generate the link URL. The link would look like: https://mycustomlink.com/[email protected]&country=usa

For this, we will have to define these 2 input fields.

842
unlayer.setLinkTypes([
  {
    name: 'my_custom_link_type',
    label: 'Custom',
    attrs: {
      href: 'https://mycustomlink.com/?email={{email}}&country={{country}}',
      target: '_blank',
    },
    fields: [
      {
        name: 'email',
        label: 'E-mail',
        defaultValue: '',
        inputType: 'email',
        placeholderText: null,
        options: [],
      },
      {
        name: 'country',
        label: 'Country',
        defaultValue: 'usa',
        inputType: null,
        placeholderText: null,
        options: [
          { value: 'usa', label: 'United States' },
          { value: 'other', label: 'Other' },
        ],
      },
    ],
  }
]);

JavaScript onClick Link

In this example, we will define a link type that uses JavaScript onClick instead of href. This is useful if you are using our landing page builder.

846
unlayer.setLinkTypes([
  {
    name: 'my_custom_alert',
    label: 'Show Alert',
    attrs: {
      onClick: 'alert("Hello World")'
    }
  }
]);

Enable/Disable Defaults

You can enable or disable the default link types. sms link type is disabled by default because it's not supported by most email clients including Gmail, Yahoo and Outlook.

unlayer.setLinkTypes([
  {
    name: 'phone',
    enabled: true
  },
  {
    name: 'email',
    enabled: true
  },
  {
    name: 'sms',
    enabled: false
  }
]);

Advanced

Link Attributes

The editor has the ability to add or modify attributes in the link tag. These attributes can be set to support multiple values or a single value at a time. Chosen value(s) can be appended to an existing attribute or added as a new attribute.

Tags Example

A common example is appending multiple chosen tags to the href attribute in a link tag. In the following example, we will add support for multiple tags in the link editor and append it to the href attribute of the link tag.

808

You can use the following code sample to enable this feature.

unlayer.setLinkTypesSharedConfig({
  attrs: {
    // 'data-tags': '{{tags}}',
    "+href": "?tags={{tags}}", // append tags in the href attribute of link
  },
  fields: [
    {
      name: "tags",
      label: "Tags",
      defaultValue: [],
      isClearable: true, // clear all tags
      isCreatable: true, // allow creation of new tags
      isMulti: true, // support multiple tags
      limit: 2, // limit the number of items that can be selected when isMulti: true (optional)
      limitMessage: "You can only select up to 2 items", // custom message for when limit is reached (optional) 
      
      // optional - if creation is enabled
      onCreateOption(inputValue, _meta, done) {
        
        // Save or create inputValue in the database (if needed)

        const newOption = {
          label: inputValue,
          value: inputValue,
        };

        done(newOption);
      },

      // existing tags
      options: [
        {
          label: "A",
          value: "a",
        },
        {
          label: "B",
          value: "b",
        },
        {
          label: "C",
          value: "c",
        },
      ],
    },
  ],
});

What’s Next