# UI Components

The first thing you need to know is that websites **need to obtain an opt-in** from their visitors to send them push notifications. Choosing the right format and the right moment to ask for push notification permission is key to build a successful web push strategy.

There are several formats to choose from, each one adapted to different needs:

* [**Alert**](#alert): a customizable popup.
* [**Banner**](#banner): a simple banner.
* [**Button**](#button): a button you can display in the bottom right or left corner of your website.
* [**Native prompt**](#native-prompt): The native prompt, in case you don't want to use any of the above formats *(fully secure mode only)*.

UI Components (except for `popup`) are automatically added to your web page once you configure them. Configuring a UI component allows you to override its behaviour, position or even translations. We'll detail what configuration options are supported for every builtin UI Component.

For example, here's how a button would work in the configuration:

```js
batchSDK(['setup', {
  ...
  ui: {
    button: {
      corner: 'bottom left',
      backgroundColor: '#FFFFFF',
      hover: 'Subscribe to our notifications!',
    }
  }
});
```

{% hint style="info" %}
When in fully secure mode, you have the possibility of asking for the browser's notification optin directly. A UI Component is **required** for the HTTP/Multidomain mode, as a click event is required to open a popup without it being caught by browser's builtin blockers.
{% endhint %}

### Alert

***

![Alert screenshot](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2FHKOXVi2Oo0Eoyaf3kt3X%2Falert.png?alt=media\&token=5eb2f8ab-6cf7-4b00-9ef5-555f12544b56)

*Requires Batch SDK 2.0.0*

The alert format is similar to popups you can get using `alert()`. It is not *actually* modal: the user is still able to interact with the content around it.

It can show up in any of the four corners, or can be centered on the top or the bottom of the page.

To enable it, add a child object of `ui` named `alert`.

Supported parameters:

<table><thead><tr><th width="224.4140625">Id</th><th>Description</th></tr></thead><tbody><tr><td><code>autoShow</code></td><td><strong>Boolean</strong> - <em>Optional</em><br>Whether the component should show as soon as possible, or if it should wait for a manual 'show' API call. Default: true<br><em>E.g.</em><code>{"autoShow":false}</code></td></tr><tr><td><code>attach</code></td><td><strong>String</strong> - <em>Optional</em><br>Where to attach the alert. Should be a mix of top|bottom and left|center|right.<br><em>E.g.</em><code>{"attach":"bottom left"}</code></td></tr><tr><td><code>icon</code></td><td><strong>URL String</strong> - <em>Optional</em><br>Link to a square icon representing your website, which will show up on the left of the banner. The file must be a PNG or JPG image hosted on an HTTPS server.<br><em>E.g.</em><code>{"icon":"https://mydomain.com/icon.png"}</code></td></tr><tr><td><code>text</code></td><td><strong>String</strong> - <em>Optional</em><br>Overrides the text that's displayed in the banner.<br><em>E.g.</em><code>{"text":"Never miss an update !"}</code></td></tr><tr><td><code>backgroundColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the banner's background.<br><em>E.g.</em><code>{"backgroundColor":"#646464"}</code></td></tr><tr><td><code>textColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the banner's text.<br><em>E.g.</em><code>{"textColor":"#646464"}</code></td></tr><tr><td><code>fontSize</code></td><td><strong>Integer</strong> - <em>Optional</em><br>Font size. Unitless.<br><em>E.g.</em><code>{"fontSize":"12"}</code></td></tr><tr><td><code>fontFamily</code></td><td><strong>String</strong> - <em>Optional</em><br>Overrides the font used in the banner. Works exactly like CSS' font-family attribute.<br><em>E.g.</em><code>{"fontFamily":"helvetica"}</code></td></tr><tr><td><code>btnBackgroundColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's background.<br><em>E.g.</em><code>{"btnBackgroundColor":"#646464"}</code></td></tr><tr><td><code>btnTextColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's text.<br><em>E.g.</em><code>{"btnTextColor":"#646464"}</code></td></tr><tr><td><code>btnHoverColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's hover state.<br><em>E.g.</em><code>{"btnHoverColor":"#646464"}</code></td></tr><tr><td><code>btnFontSize</code></td><td><strong>Integer</strong> - <em>Optional</em><br>Font size of both buttons in px. Do not add the unit to the value.<br><em>E.g.</em><code>{"btnFontSize":"12"}</code></td></tr><tr><td><code>positiveSubBtnLabel</code></td><td><strong>String</strong> - <em>Optional</em><br>Text override for when the positive button will subscribe the user.<br><em>E.g.</em><code>{"positiveSubBtnLabel":"Subscribe"}</code></td></tr><tr><td><code>positiveUnsubBtnLabel</code></td><td><strong>String</strong> - <em>Optional</em><br>Text override for when the positive button will unsubscribe the user.<br><em>E.g.</em><code>{"positiveUnsubBtnLabel":"Unsubscribe"}</code></td></tr><tr><td><code>negativeBtnLabel</code></td><td><strong>String</strong> - <em>Optional</em><br>Text override for when the negative button, which hides for popup temporarily, until the delay in `hideFor` has been reached.<br><em>E.g.</em><code>{"negativeBtnLabel":"Later"}</code></td></tr><tr><td><code>positiveBtnStyle</code></td><td><strong>Button style configuration</strong> - <em>Optional</em><br>Style configuration for the positive ("Subscribe"/"Unsubscribe") button. See below for more info about this object. Note: When in unsubcribe mode, this button will always have a red background color.<br><em>E.g.</em><code>{"positiveBtnStyle":{"textColor":"red"}}</code></td></tr><tr><td><code>negativeBtnStyle</code></td><td><strong>Button style configuration</strong> - <em>Optional</em><br>Style configuration for the negative ("Later") button. See below for more info about this object.<br><em>E.g.</em><code>{"negativeBtnStyle":{"textColor":"red"}}</code></td></tr><tr><td><code>extraBtn</code></td><td><strong>'Extra' button configuration. Requires SDK v3.</strong> - <em>Optional</em><br>Both 'label' (Button text) and 'link' (link that will be opened in a new tab on click) attributes are required. A 'style' object can be provided in the same syntax as 'positive/negativeBtnStyle', CSS styling is also supported.<br><em>E.g.</em><code>{"extraBtn":{"label":"Privacy Policy","link":"https://batch.com/privacy-policy"}}</code></td></tr><tr><td><code>hideFor</code></td><td><strong>Integer</strong> - <em>Optional</em><br>When closed, do not display the banner again until X seconds elapsed (default: 604800 or 7 days). Use a 0 value to hide the alert forever.<br><em>E.g.</em><code>{"hideFor":604800}</code></td></tr><tr><td><code>zIndex</code></td><td><strong>Integer</strong> - <em>Optional</em><br>z-index of the component<br><em>E.g.</em><code>{"zIndex":999}</code></td></tr></tbody></table>

#### Alert button style configuration

<table><thead><tr><th width="220.83203125">Id</th><th>Description</th></tr></thead><tbody><tr><td><code>backgroundColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's background.<br><em>E.g.</em><code>{"backgroundColor":"#646464"}</code></td></tr><tr><td><code>hoverBackgroundColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's background when hovered. Falls back on `backgroundColor` if not specified.<br><em>E.g.</em><code>{"hoverBackgroundColor":"#646464"}</code></td></tr><tr><td><code>textColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the banner's text.<br><em>E.g.</em><code>{"textColor":"#646464"}</code></td></tr><tr><td><code>fontSize</code></td><td><strong>Integer</strong> - <em>Optional</em><br>Font size in px. Do not specify an unit in this value.<br><em>E.g.</em><code>{"fontSize":"12"}</code></td></tr><tr><td><code>shadow</code></td><td><strong>Boolean</strong> - <em>Optional</em><br>Whether this button should cast a shadow, or not. Default: true for the positive button, false for the negative one.<br><em>E.g.</em><code>{"shadow":false}</code></td></tr></tbody></table>

Example:

```javascript
alert: {
    positiveBtnStyle: {
        backgroundColor: "blue",
        textColor: "white",
        fontSize: 25,
        shadow: true
    }
}
```

### Banner

***

![Banner screenshot](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2F47Al2uYeT81m6KSsl5m3%2Fbanner.png?alt=media\&token=686cc0cb-1084-4605-98db-a201dd9e7a17)

The banner shows up at the top of your webpage. It has an optional logo, a colorable button, and can be floating or fixed.

To enable it, add a child object of `ui` named `banner`.

Supported parameters:

<table><thead><tr><th width="216.421875">Id</th><th>Description</th></tr></thead><tbody><tr><td><code>autoShow</code></td><td><strong>Boolean</strong> - <em>Optional</em><br>Whether the component should show as soon as possible, or if it should wait for a manual 'show' API call. Default: true<br><em>E.g.</em><code>{"autoShow":false}</code></td></tr><tr><td><code>icon</code></td><td><strong>URL String</strong> - <em>Optional</em><br>Link to a square icon representing your website, which will show up on the left of the banner. The file must be a PNG or JPG image hosted on an HTTPS server.<br><em>E.g.</em><code>{"icon":"https://mydomain.com/icon.png"}</code></td></tr><tr><td><code>text</code></td><td><strong>String</strong> - <em>Optional</em><br>Overrides the text that's displayed in the banner.<br><em>E.g.</em><code>{"text":"Never miss an update !"}</code></td></tr><tr><td><code>fixed</code></td><td><strong>Boolean</strong> - <em>Optional</em><br>Makes the banner push the webpage's content rather than float on top of it (default: False).<br><em>E.g.</em><code>{"fixed":true}</code></td></tr><tr><td><code>attachBottom</code></td><td><strong>Boolean</strong> - <em>Optional</em><br>Makes the banner show on the bottom of the page, rather than on top. Forces 'fixed' to true (default: False).<br><em>E.g.</em><code>{"attachBottom":true}</code></td></tr><tr><td><code>backgroundColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the banner's background.<br><em>E.g.</em><code>{"backgroundColor":"#646464"}</code></td></tr><tr><td><code>textColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the banner's text.<br><em>E.g.</em><code>{"textColor":"#646464"}</code></td></tr><tr><td><code>fontSize</code></td><td><strong>Integer</strong> - <em>Optional</em><br>Font size. Unitless.<br><em>E.g.</em><code>{"fontSize":"12"}</code></td></tr><tr><td><code>fontFamily</code></td><td><strong>String</strong> - <em>Optional</em><br>Overrides the font used in the banner. Works exactly like CSS' font-family attribute.<br><em>E.g.</em><code>{"fontFamily":"helvetica"}</code></td></tr><tr><td><code>btnBackgroundColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's background.<br><em>E.g.</em><code>{"btnBackgroundColor":"#646464"}</code></td></tr><tr><td><code>btnTextColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's text.<br><em>E.g.</em><code>{"btnTextColor":"#646464"}</code></td></tr><tr><td><code>btnHoverColor</code></td><td><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's hover state.<br><em>E.g.</em><code>{"btnHoverColor":"#646464"}</code></td></tr><tr><td><code>btnFontSize</code></td><td><strong>Integer</strong> - <em>Optional</em><br>Button's font size. Unitless.<br><em>E.g.</em><code>{"btnFontSize":"12"}</code></td></tr><tr><td><code>btnWidth</code></td><td><strong>Integer</strong> - <em>Optional</em><br>Button width. Unitless.<br><em>E.g.</em><code>{"btnWidth":"200"}</code></td></tr><tr><td><code>btnSub</code></td><td><strong>String</strong> - <em>Optional</em><br>Text override for when the button will subscribe the user.<br><em>E.g.</em><code>{"btnSub":"Subscribe"}</code></td></tr><tr><td><code>btnUnsub</code></td><td><strong>String</strong> - <em>Optional</em><br>Text override for when the button will unsubscribe the user.<br><em>E.g.</em><code>{"btnUnsub":"Unsubscribe"}</code></td></tr><tr><td><code>hideFor</code></td><td><strong>Integer</strong> - <em>Optional</em><br>When closed, do not display the banner again until X seconds elapsed (default: 604800 or 7 days). Use a 0 value to hide the banner forever.<br><em>E.g.</em><code>{"hideFor":604800}</code></td></tr><tr><td><code>zIndex</code></td><td><strong>Integer</strong> - <em>Optional</em><br>z-index of the component<br><em>E.g.</em><code>{"zIndex":999}</code></td></tr></tbody></table>

### Button

***

![Button screenshot](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2F2M8jMlksO1tT5gpt31Uu%2Fpopin_vestiaire%402x.png?alt=media\&token=8a2dd942-7303-4c27-8884-6ee20ca2ba1a)

The `Button` UI Component shows a simple bell icon in the corner you want which, once clicked, show a pop-in with a customizable opt-in text, and a button to enable notifications.

To enable it, add a child object of `ui` named `button`.

Supported parameters:

| Id                     | Description                                                                                                                                                                                                                               |
| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `autoShow`             | <p><strong>Boolean</strong> - <em>Optional</em><br>Whether the component should show as soon as possible, or if it should wait for a manual 'show' API call. Default: true<br><em>E.g.</em><code>{"autoShow":false}</code></p>            |
| `corner`               | <p><strong>String</strong> - <em>Optional</em><br>Corner to display the button in. Should be a mix of top</p>                                                                                                                             |
| `hover`                | <p><strong>String</strong> - <em>Optional</em><br>Override the text that appears when hovering the button.<br><em>E.g.</em><code>{"hover":"Never miss an update !"}</code></p>                                                            |
| `backgroundColor`      | <p><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's background.<br><em>E.g.</em><code>{"backgroundColor":"#646464"}</code></p>                                                                              |
| `foregroundColor`      | <p><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's glyph.<br><em>E.g.</em><code>{"foregroundColor":"#646464"}</code></p>                                                                                   |
| `hoverColor`           | <p><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's background on hover.<br><em>E.g.</em><code>{"hoverColor":"#646464"}</code></p>                                                                          |
| `hoverForegroundColor` | <p><strong>Hex Color String</strong> - <em>Optional</em><br>Color of the button's glyph on hover.<br><em>E.g.</em><code>{"hoverForegroundColor":"#646464"}</code></p>                                                                     |
| `popin`                | <p><strong>Object</strong> - <em>Optional</em><br>Overrides the popin's properties.</p>                                                                                                                                                   |
|                        |                                                                                                                                                                                                                                           |
| `title`                | <p><strong>String</strong> - <em>Optional</em><br>Override the pop-in's title.<br><em>E.g.</em><code>{"title":"Subscribe to notifications"}</code></p>                                                                                    |
| `btnSub`               | <p><strong>String</strong> - <em>Optional</em><br>Overrides the pop-in subscribe button text.<br><em>E.g.</em><code>{"btnSub":"Subscribe"}</code></p>                                                                                     |
| `btnUnsub`             | <p><strong>String</strong> - <em>Optional</em><br>Overrides the pop-in unsubscribe button text.<br><em>E.g.</em><code>{"btnUnsub":"Unsubscribe"}</code></p>                                                                               |
| `example`              | <p><strong>Object</strong> - <em>Optional</em><br>Example notification to display instead of the placeholder. See 'Theming' for more info.<br><em>E.g.</em><code>{"example":{"body":"This is what a notification looks like"}}</code></p> |
| `title`                | <p><strong>String</strong> - <em>Optional</em><br>Override the pop-in's title.<br><em>E.g.</em><code>{"title":"Subscribe to notifications"}</code></p>                                                                                    |
| `btnSub`               | <p><strong>String</strong> - <em>Optional</em><br>Overrides the pop-in subscribe button text.<br><em>E.g.</em><code>{"btnSub":"Subscribe"}</code></p>                                                                                     |
| `btnUnsub`             | <p><strong>String</strong> - <em>Optional</em><br>Overrides the pop-in unsubscribe button text.<br><em>E.g.</em><code>{"btnUnsub":"Unsubscribe"}</code></p>                                                                               |
| `example`              | <p><strong>Object</strong> - <em>Optional</em><br>Example notification to display instead of the placeholder. See 'Theming' for more info.<br><em>E.g.</em><code>{"example":{"body":"This is what a notification looks like"}}</code></p> |
| `zIndex`               | <p><strong>Integer</strong> - <em>Optional</em><br>z-index of the component<br><em>E.g.</em><code>{"zIndex":999}</code></p>                                                                                                               |

#### Theming

The button theme supports full color customization of the button itself, but not the popin. Just like any other component attribute, just override the wanted value in the configuration object.

Here's what colors you can change on the button, with their property names:

<figure><img src="https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2FLDg5X0tONHEd9TCyz3JR%2Fbutton_coloring.png?alt=media&#x26;token=b62abf91-560e-4052-a1ec-54f824335684" alt=""><figcaption></figcaption></figure>

You can also customize the placeholder notification in the popin, by specifying a `example` object under `popin`:

<table><thead><tr><th width="159.7734375">Id</th><th>Description</th></tr></thead><tbody><tr><td><code>icon</code></td><td><strong>Image URL String</strong> - <em>Optional</em><br>URL of the image to display on the left of the example notification.<br><em>E.g.</em><code>{"icon":"https://path/to/your/image.jpg"}</code></td></tr><tr><td><code>title</code></td><td><strong>String</strong> - <em>Required</em><br>Notification title text.<br><em>E.g.</em><code>{"title":"Test title"}</code></td></tr><tr><td><code>body</code></td><td><strong>String</strong> - <em>Required</em><br>Notification body text.<br><em>E.g.</em><code>{"body":"Test body"}</code></td></tr></tbody></table>

Example:

```javascript
button: {
    corner: 'bottom left',
    popin: {
        example: {
            icon: 'https://path/to/your/icon.jpg',
            title: 'Test title',
            body: 'Test body'
        }
    }
}
```

### Native prompt

![Native prompt screenshot](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2F1Y5j3Ueb58KPv22Zi8A9%2Fnativeprompt.png?alt=media\&token=3ee79222-25df-48df-a167-589dcf2bd877)

In case you don't want to use Batch's formats, you can trigger the native prompt of each browser. That permission request can be shown 3 times a week (more information [here](https://www.chromestatus.com/features/6443143280984064)). Please note this will only work if you are using the **fully secure mode**.

To enable it, add a child object of `ui` named `native`.

Supported parameters:

<table><thead><tr><th width="186.44921875">Id</th><th>Description</th></tr></thead><tbody><tr><td><code>autoShow</code></td><td><strong>Boolean</strong> - <em>Optional</em><br>Whether the component should show as soon as possible, or if it should wait for a manual 'show' API call. Default: true<br><em>E.g.</em><code>{"autoShow":false}</code></td></tr><tr><td><code>backoffDuration</code></td><td><strong>Integer</strong> - <em>Optional</em><br>Time, in seconds. Allows you to set a delay between each display of the native prompt. Default: 86400<br><em>E.g.</em><code>{"backoffDuration":86400}</code></td></tr></tbody></table>

Example:

```javascript
native: {
    "backoffDuration": 86400
}
```

### Toggling visibility programmatically

Starting with Batch SDK 2.0.0, you can programatically control the display of builtin UI components.

Two methods are available on the public API: ui.show, and ui.hide. They take as a parameter the name of a UI component that has been configured earlier, in the SDK setup:

```js
batchSDK('ui.show', 'alert')
batchSDK('ui.hide', 'alert')
// or
batchSDK((api) => {
  api.ui.show('alert')
})
batchSDK((api) => {
  api.ui.hide('alert')
})
```

{% hint style="info" %}
Most components will be shown as soon as possible. In order to make them wait for your `show` call, please refer to the configuration of the UI component you're trying to tweak: look for the `autoShow` key.
{% endhint %}

Note that some UI components might chose not to display even if you tell them to. Reasons can be:

* The user is already subscribed to notifications
* The user dismissed the UI element, and the minimum time between two dismissal (controlled by the `hideFor` configuration key) has not been reached.

In these cases, you can use the second "force" parameter, which allows you to force the display:

```js
batchSDK('ui.show', 'alert', true)
```
