API Reference / InstantSearch.js Widgets / poweredBy
Apr. 24, 2019
Widget signature
instantsearch.widgets.poweredBy({
  container: string|HTMLElement,
  // Optional parameters
  theme: string,
  cssClasses: object,
});

About this widget

The poweredBy widget is used to display the Algolia logo to redirect to our website.

Algolia requires that you use this widget if you are on a Community or Free plan.

Examples

1
2
3
instantsearch.widgets.poweredBy({
  container: '#powered-by',
});

Options

container
type: string|HTMLElement
Required

The CSS Selector or HTMLElement to insert the widget into.

1
2
3
instantsearch.widgets.poweredBy({
  container: '#powered-by',
});
theme
type: string ("light"|"dark")
default: light
Optional

The version of the logo to use, legible on light or dark backgrounds.

1
2
3
4
instantsearch.widgets.poweredBy({
  // ...
  theme: 'dark',
});
cssClasses
type: object
default: {}
Optional

The CSS classes to override.

  • root: the root element of the widget.
  • link: the link element.
  • logo: the SVG element.
1
2
3
4
5
6
7
8
9
10
instantsearch.widgets.poweredBy({
  // ...
  cssClasses: {
    root: 'MyCustomPoweredBy',
    link: [
      'MyCustomPoweredByLink',
      'MyCustomPoweredByLink--subclass',
    ],
  },
});

Customize the UI - connectPoweredBy

If you want to create your own UI of the poweredBy widget, you can use connectors.

It’s a 3-step process:

// 1. Create a render function
const renderPoweredBy = (renderOptions, isFirstRender) => {
  // Rendering logic
};

// 2. Create the custom widget
const customPoweredBy = instantsearch.connectors.connectPoweredBy(
  renderPoweredBy
);

// 3. Instantiate
search.addWidget(
  customPoweredBy({
    // instance params
  })
);

Create a render function

This rendering function is called before the first search (init lifecycle step) and each time results come back from Algolia (render lifecycle step).

const renderPoweredBy = (renderOptions, isFirstRender) => {
  const { string url, object widgetParams } = renderOptions;

  if (isFirstRender) {
    // Do some initial rendering and bind events
  }

  // Render the widget
}

Rendering options

url
type: string

The URL to redirect to.

1
2
3
4
5
6
7
const renderPoweredBy = (renderOptions, isFirstRender) => {
  const { url } = renderOptions;

  document.querySelector('#powered-by').innerHTML = `
    <a href="${url}">Powered by Algolia</a>
  `;
};
widgetParams
type: object

All original widget options forwarded to the render function.

1
2
3
4
5
6
7
8
9
10
11
12
13
const renderPoweredBy = (renderOptions, isFirstRender) => {
  const { widgetParams } = renderOptions;

  widgetParams.container.innerHTML = '...';
};

// ...

search.addWidget(
  customPoweredBy({
    container: document.querySelector('#powered-by'),
  })
);

Create and instantiate the custom widget

We first create custom widgets from our rendering function, then we instantiate them. When doing that, there are two types of parameters you can give:

  • Instance parameters: they are predefined parameters that you can use to configure the behavior of Algolia.
  • Your own parameters: to make the custom widget generic.

Both instance and custom parameters are available in connector.widgetParams, inside the renderFunction.

const customPoweredBy = instantsearch.connectors.connectPoweredBy(
  renderPoweredBy
);

search.addWidget(
  customPoweredBy({
    // Optional parameters
    url: string,
  })
);

Instance options

url
type: string
default: https://algolia.com
Optional

The URL to redirect to.

1
2
3
customPoweredBy({
  url: 'https://algolia.com/about',
});

Full example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Create the render function
const renderPoweredBy = (renderOptions, isFirstRender) => {
  const { url, widgetParams } = renderOptions;

  widgetParams.container.innerHTML = `
    <a href="${url}">Powered by Algolia</a>
  `;
};

// Create the custom widget
const customPoweredBy = instantsearch.connectors.connectPoweredBy(
  renderPoweredBy
);

// Instantiate the custom widget
search.addWidget(
  customPoweredBy({
    container: document.querySelector('#powered-by'),
  })
);

HTML output

1
2
3
4
5
6
<div class="ais-PoweredBy">
   <span class="ais-PoweredBy-text">Search by</span>
   <a href="..." class="ais-PoweredBy-link" aria-label="Algolia">
      <!-- <svg> ... </svg> -->
   </a>
</div>

Did you find this page helpful?