> For the complete documentation index, see [llms.txt](https://docs.fact-finder.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fact-finder.com/webcomponents-overview/web-components-4.x./guides/advanced/custom-components.md).

# Custom Components

Build custom components with FactFinder events and result subscriptions.

FactFinder Web Components API can be used to implement custom components in any web framework.

In a typical scenario, use [EventAggregator's](file:///api/4.x/core-event-aggregator) `addFFEvent()` to query FactFinder and [ResultDispatcher's](file:///api/4.x/core-result-dispatcher) `subscribe()` to receive its response.

Below are two examples implementing a product carousel component: one in VueJS and one as a native custom element.

### VueJS

The source code is available [here](https://github.com/FACT-Finder-Web-Components/demos/blob/release/4.x/custom-components/vuejs-carousel/carousel.js).

{% stepper %}
{% step %}

#### `addFFEvent`

Call `addFFEvent()` in the `mounted` hook to trigger a FactFinder search when the component renders for the first time:

```javascript
eventAggregator.addFFEvent({
    type: 'search',
    query: carousel.query,
    topics: function () {
        return ['carousel-' + carousel._uid];
    }
});
```

* `type: 'search'` indicates that we want to perform a regular search.
* `query` is populated from the component prop.
* The `topics` function defines to which topic the results will be dispatched. In this case, this topic should be unique for each component instance. Otherwise, other carousels would interfere with each other.

{% hint style="info" %}
When designing a production-ready component, it is advisable to implement a `query` prop watcher to react to its possible changes with consecutive search requests.
{% endhint %}
{% endstep %}

{% step %}

#### `subscribe`

Subscribe to the same topic in the `mounted` hook:

```javascript
carousel.subscriptionKey = resultDispatcher.subscribe('carousel-' + carousel._uid, function (response) {
    carousel.records = response.searchResult.records.map(function (record) {
        return record.record;
    });
});
```

Here, extract all the information needed from the response and store it in component data alongside the subscription key returned by `subscribe()`.
{% endstep %}

{% step %}

#### `unsubscribe`

Before destroying the component, unsubscribe using the previously stored subscription key:

```javascript
if (carousel.subscriptionKey) {
    resultDispatcher.unsubscribe('carousel-' + carousel._uid, carousel.subscriptionKey);
}
```

{% endstep %}
{% endstepper %}

### Native custom element

The source code is available [here](https://github.com/FACT-Finder-Web-Components/demos/blob/release/4.x/custom-components/native-carousel/carousel.js).

The usage of FactFinder Web Components API here is very similar to the usage in the VueJS example.

{% stepper %}
{% step %}

#### `addFFEvent`

The `addFFEvent()` call is almost identical:

```javascript
eventAggregator.addFFEvent({
    type: 'search',
    query: query,
    topics: function () {
        return ['carousel-' + uid];
    }
});
```

Here, invoke it in the `attributeChangedCallback`, meaning whenever a new value of the `query` attribute is set, trigger a new search.
{% endstep %}

{% step %}

#### `subscribe`

Subscribe in the `connectedCallback`:

```javascript
element._subscriptionKey = resultDispatcher.subscribe('carousel-' + element._uid, handleRecords(element));
...
function handleRecords(element) {
    return function(response) {
        element._records = response.searchResult.records;
        element._carouselContainer.innerHTML = element._records.map(renderProduct).join('');
    };
}
```

Just like in the previous example, store the subscription key and returned records.

As no automatic renderer is used, perform the rendering here.
{% endstep %}

{% step %}

#### `unsubscribe`

Unsubscribe in the `disconnectedCallback`:

```javascript
resultDispatcher.unsubscribe('carousel-' + element._uid, element._subscriptionKey);
```

{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.fact-finder.com/webcomponents-overview/web-components-4.x./guides/advanced/custom-components.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
