Lightweight Search Widget

Use the legacy Lightweight Search Widget to add a JavaScript search icon and autosuggest input that redirects users to your search results page. This widget has been largely replaced by UI Kit Input widgets.

This widget is not dependent on JQuery or Vue.

Using the Lightweight Search Widget

It shows up by default as the search icon shown below:

SearchStax Site Search Legacy Lightweight Search Widget button and magnifying glass icon.

which when clicked shows the search bar, complete with search suggestions.

SearchStax Site Search Legacy Lightweight Search Widget in use, showing search suggestions.

The search icon and the search bar can be reformatted easily using CSS.

To use this widget:

  1. Include the following in the header:

    <script src="https://static.searchstax.com/studio-js/v3/js/search-widget.min.js"></script>
  2. Include the widget container in your website:

    
        <div id="div-widget-id" class="studio-search-widget">
        </div>
    
  3. Initialize the widget once the container is finished rendering

    new SearchstudioWidget(
      'YOUR_READ_ONLY_TOKEN',
      'https://example.invalid/emsuggest',
      'https://example.invalid/search/',
      3,
      'searchstax[query]',
      'div-widget-id',
      'en',
      'token'
    );

The parameters to the widget are:

  • Authentication takes a read-only credential. In token mode, pass the token itself. In Basic mode, pass the Base64-encoded user ID:password value. Visitors can inspect browser-side credentials. Use a read-only credential in the widget.
  • auto suggest endpoint: This is the Auto-suggest endpoint of your search application, that you can get from the App Management page
  • search page link: This is the link to your search page. The widget will pass the search query to the search page so it can render the results and take you to that page 
  • number of characters: Auto-suggestions will display after this number of characters is typed. Note that this is a static value that should be same or greater than what is configured within your Site Search application. This widget does not dynamically load any of the settings.
  • query: When the search page link is invoked, the widget will pass ? = to the search page link
  • ID of the widget: The ID of the widget
  • language: Which language-specific experience to use as the auto-suggest source. If not specified, defaults to ‘en’.
  • Authentication method tells the widget which prefix to add. Set it to 'token' for a token or 'basic' for Basic Auth.

Use Basic Auth

If your app’s Auto-Suggest endpoint is configured for legacy Basic Auth, use the read-only user ID and password. Run this block in a private Bash terminal to Base64-encode the user ID:password pair locally. The password prompt hides what you type.

(
  if [[ ! -t 0 ]]; then
    printf 'Run this block in an interactive Bash terminal.\n' >&2
    exit 1
  fi
  IFS= read -r -p 'Read-only user ID: ' read_only_user || exit 1
  IFS= read -r -s -p 'Read-only password: ' read_only_password || exit 1
  printf '\n' >&2
  printf '%s:%s' "$read_only_user" "$read_only_password" | base64 | tr -d '\r\n'
  printf '\n'
)

In the constructor above, replace YOUR_READ_ONLY_TOKEN with the Base64 output and change the last argument to 'basic'. The widget adds the Basic prefix. Treat the encoded value like the original password.

Modifying the Widget

The Lightweight Search Widget is supplied to you in a generic form. If you wish to modify it, there are three steps:

Full HTML Example


<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Search Widget</title>
    <script src="https://static.searchstax.com/studio-js/v3/js/search-widget.min.js"></script>
    <script type="text/javascript">
      function initiateSearchWidget() {
  new SearchstudioWidget(
    'YOUR_READ_ONLY_TOKEN',
    'https://example.invalid/emsuggest',
    'https://example.invalid/search/',
    3,
    'searchstax[query]',
    'div-widget-id',
    'en',
    'token'
  );
}
    </script>
    <style>
      .left-margin {
        margin-left: 500px;
      }
      .flex {
        display: flex;
      }
      .text {
        padding: 10px;
      }
    </style>
    <style>
      .studio-search-widget {
        position: relative;
        width: 60px;
        text-align: center;
        border: 1px solid #ced4da;
        border-radius: 80px;
        padding: 10px 0;
      }

      .studio-search-widget .studio-widget-search-icon {
        position: relative;
        display: inline-block;
        background: #fff;
        border-radius: 30px;
        height: 8px;
        width: 8px;
        border: 2px solid #2e2e30;
        margin-left: -5px;
      }

      .studio-search-widget .studio-widget-search-icon:after {
        content: "";
        height: 2px;
        width: 8px;
        background: #2e2e30;
        position: absolute;
        top: 9px;
        left: 7px;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        -o-transform: rotate(45deg);
      }

      .studio-search-widget .search-wrapper {
        visibility: hidden;
        position: absolute;
        right: -1px;
        top: -1px;
        width: 60px;
        text-align: center;
        border: 1px solid #ced4da;
        border-radius: 80px;
        padding: 10px 0;
        background-color: white;
        transition:
          visibility 0.2s,
          width 0.2s ease-in;
        display: flex;
      }

      .studio-search-widget .search-wrapper .studio-widget-small-search-icon {
        position: relative;
        display: inline-block;
        background: #fff;
        border-radius: 30px;
        height: 6px;
        width: 6px;
        border: 2px solid #2e2e30;
        margin-left: 15px;
        margin-right: 5px;
        margin-top: 4px;
      }
      .studio-search-widget
        .search-wrapper
        .studio-widget-small-search-icon:after {
        content: "";
        height: 2px;
        width: 6px;
        background: #2e2e30;
        position: absolute;
        top: 7px;
        left: 5px;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        -o-transform: rotate(45deg);
      }

      .studio-search-widget .search-wrapper .studio-widget-search-input {
        padding-top: 4px;
      }

      .studio-search-widget.expanded .search-wrapper {
        visibility: visible;
        width: 600px;
      }

      .studio-search-widget .search-wrapper .studio-widget-autosuggest-results {
        position: absolute;
        width: calc(100%);
        top: 42px;
        box-shadow: 0 6px 15px 0 rgb(0 0 0 / 10%);
        border-radius: 15px;
      }

      .studio-search-widget
        .search-wrapper
        .studio-widget-autosuggest-results
        .result-row {
        width: calc(100% - 60px);
        padding: 10px;
        padding-left: 30px;
        text-align: left;
        text-transform: capitalize;
      }

      .studio-search-widget
        .search-wrapper
        .studio-widget-autosuggest-results
        .result-row.searchstudio-active-row {
        /* background-color: white; */
        color: #4bb5e2;
        cursor: pointer;
      }

      .studio-search-widget .search-wrapper input {
        width: calc(100% - 40px);
        border: none;
        height: 13px;
        box-shadow: none;
        outline: none;
        z-index: 4;
        /* padding: 0 20px; */
      }
    </style>
  </head>
  <body onload="initiateSearchWidget()">
    <div class="left-margin">
      <div class="flex">
        <div class="text">Search Widget</div>
        <div>
          <div id="div-widget-id" class="studio-search-widget"></div>
        </div>
        <div class="text"></div>
      </div>
    </div>
  </body>
</html>

Questions?

Do not hesitate to contact the SearchStax Support Desk.

Articles in this section