Creating a dynamic material search bar with algolia autocomplete feature

I've implemented algolia autocomplete in my application and I'm trying to create a search bar that expands similar to the example on this page. My app's layout includes a header bar with a search input for larger screens, while smaller screens only show a magnifying glass icon. Clicking the icon reveals another header containing the search bar, initially hidden. I've created a fiddle to provide an overview, although it's not fully functional. Here is a snippet of the layout:

<header class="mdc-toolbar mdc-toolbar--fixed toolbar--custom">
  <div class="mdc-toolbar__row">
    [...]

@keyframes close {
  [...]
}

The complete page layout looks like this:

<div id="app">
    [...] 
</div>

The issue I'm facing is that the dropdown menu is visible on bigger screens but not on smaller screens. Below is the associated CSS:

.algolia-autocomplete {
  [...]
}

I noticed that removing the search-field-phone-open class makes the dropdown visible on small screens. I'm unsure what within that class is causing the dropdown to be hidden. Here's the problematic section of the code:

.search-field-phone-open,  {
  [...]
}

Answer №1

I'm not entirely sure if I've grasped your requirements, but here's a snippet that might be helpful:

      var client = algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76');
      var index = client.initIndex('contacts');

      $('.input-search').autocomplete({ hint: false }, [
        {
          source: function(searchBar, cb) {
            index.search(searchBar, { hitsPerPage: 5 }, function(error, content) {
              if (error) {
                cb([]);
                return;
              }
              cb(content.hits, content);
            });
          },
          displayKey: 'name',
          templates: {
            suggestion: function(suggestion) {
              return suggestion._highlightResult.name.value;
            }
          }
        }
      ]);
.algolia-autocomplete {
  display: flex!important;
  flex: auto!important;
  height: 100%;
}
/* CSS styles omitted for brevity */

<link href="https://storage.googleapis.com/code.getmdl.io/1.0.1/material.teal-red.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Other script and link references omitted for brevity -->

<header class="mdc-toolbar">
  <div class="mdc-toolbar__row">
    <section class="mdc-toolbar__section mdc-toolbar__section--align-start">
      <a href="#" class="material-icons mdc-toolbar__menu-icon">menu</a>
      <span class="mdc-toolbar__title">Title</span>
    </section>
    <section class="mdc-toolbar__section">
      <div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable">
        <!-- Form input field and label code omitted for brevity -->
      </div>
    </section>
    <section class="mdc-toolbar__section mdc-toolbar__section--align-end">
     Custom icon
    </section>
  </div>
</header>

I hope this solution meets your needs.

References

I consulted the mdc-toolbar repository on GitHub.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

My CSS is giving me a bit of trouble with alignment

I have been experimenting with various approaches such as adjusting the size of the divs to tiny dimensions and trying out inline-block or float left and right properties, but I am still unable to achieve the desired result of having my divs positioned s ...

One source of HTML content for a div element across multiple webpages

Could you please provide guidance on where to start investigating or researching for this issue? I am interested in having a single source for the content of a <div>, which can be used across multiple pages. This way, I can update one source and hav ...

I am curious to learn the best way to ensure that all the elements on my navigation are evenly spaced

I'm trying to figure out how to evenly space all the elements on my navigation bar. It's for a school project where I'm designing a website, but I just can't seem to get it right. None of the solutions I've tried so far have worked ...

Tips for customizing the scrollbar appearance in ngx-datatable

Currently, I am utilizing the ngx datatable within my Angular application and have come across an issue with customizing the scrollbar style specifically for this table. .ngx-datatable::-webkit-scrollbar-track{ border-radius: 8px !important; } Unfortu ...

Is it possible to encounter the issue of "Context Lost" and "Importing multiple instances" in Three.js?

I am in the process of developing a 3D editor that allows users to manipulate a 3D scene and then view the result by pressing a "play" button. In order to display the output, I am utilizing an iframe. Below is the snippet of my HTML code: <iframe id=&qu ...

Instructions for incorporating a glyphicon glyphicon-calendar into a bootstrap dropdown using pug

I am a beginner in the world of pug and I'm trying to include the glyphicon glyphicon-calendar in a dropdown menu labeled "All months". .pull-right select.form-control.btn-primary#selectMonth(style="margin-top: -15px;") option(selected="selecte ...

Create a jQuery dropdown menu within the same division element

I need help displaying a drop-down menu on mouseover using only one div. Currently, I am employing 2 divs and sliding up another div to show the submenu; however, I want to achieve this with just 1 div. Is there a way to do that? Below is my code snippet: ...

NuxtLink sending users to incorrect destination URL

I am facing an issue with my static generated Nuxt site. Everything works perfectly fine when I host it locally, but once I load it on GitHub Pages, the NuxtLink elements' hrefs are incorrect. For instance, one of my links looks like this: <NuxtLi ...

What is the best way to display two timers on one page?

I'm having an issue with displaying two timers for a 3-minute countdown. The problem is that the second timer works perfectly, but the first timer doesn't. Even if I remove the second timer, there is still some problem with the first one. The cod ...

Adjust the dimensions of the initial cell

I need to adjust the size of the initial "generated" cell in a grid. The grid is not present in the HTML markup until JavaScript prints RSS information on it, making it difficult to target specific rows or cells directly. Note: The first element is hidden ...

Obtain HTML content from a loaded JScript frame

I'm in the process of designing a JScript frame and I'm looking to retrieve the HTML content that has been loaded into it. Does anyone have any suggestions on the best way to accomplish this? ...

Alter the backdrop of this email template

I'm struggling to replace the blue and grey background of my email template with an image. Any suggestions on how I can achieve this? Here is the Html Code link for reference I attempted to modify the body tag, but it didn't result in any chang ...

Ways to implement a transition effect when the hover state is deactivated or no longer present

After applying a transition to the header using the hover pseudo-class, I noticed that it abruptly snaps back to its original position when the hover effect is removed. Is there a way to smoothly return the header to its original position using the same ...

TinyMCE: Tips for adding and highlighting text in your content!

Is there a way to insert, or even replace the current selection with some content and then have it automatically selected? Take for instance this text: Hello nice world! The user has selected nice. After clicking a button, this code is executed: editor. ...

Struggling to center items on a page with Bootstrap 5

I'm attempting to center an item on the page using Bootstrap flex boxes in HTML. However, I am having trouble with the align-items-center attribute not working. <!DOCTYPE html> <head> <link href="https://cdn.jsdelivr.net/npm/& ...

Tips for reducing word length in Bootstrap card text using Django and CSS

For my journal entries, I'm using Bootstrap's card component. It looks like this: View Journal List However, when I have a lot of text in the entry, the card stretches out too long. View Journal List with a Really Long Entry I want to show ju ...

Ways to incorporate padding into md-card using Angular 2 material

Need assistance with adding padding to md-card in angular2 material. I am using md-card to display my product list but struggling to add padding on them. Here's what I have tried: product.component.html : <p> Product List </p> <div ...

What is the best way to set the minimum height for a floated DIV element

Struggling to apply a min-height to a floated DIV in an XHTML or HTML document. The workaround of nesting a fixed-height DIV and clearing it with :after is ineffective. Directly setting a min-height does not produce the desired result. The current code, w ...

To submit the form, users must check off multiple checkboxes using JavaScript conditional statements

Currently, I am working on creating a form that requires multiple fields like name and email. While I have been successful in sorting out those fields, I am facing a challenge with the checkboxes. I am trying to figure out how to make it mandatory for two ...

Issue with Bootstrap hover and toggle collapse menu functionality in a Ruby on Rails application

I am currently following this tutorial and have reached this particular part. However, I'm facing an issue where the mobile hamburger menu doesn't display the menu when clicked on mobile devices. Additionally, the hover effect is not working for ...