Search code examples
javascripthtmlcssalgolia

CSS display issue, content not appearing


/* global instantsearch */

import { hitTemplate } from "./helpers";

const search = instantsearch({
  appId: "B1G2GM9NG0",
  apiKey: "aadef574be1f9252bb48d4ea09b5cfe5",
  indexName: "Index_PinIt",
  searchParameters: {
    hitsPerPage: 3,
    attributesToSnippet: ["content:14"],
    snippetEllipsisText: " [...]"
  }
});

// Uncomment the following widget to add hits list.

 search.addWidget(
  instantsearch.widgets.hits({
    container: "#hits",
    templates: {
      empty: "No results found.",
      item(hit) {
        return hitTemplate(hit);
      }
    }
  })
); 

// Uncomment the following widget to add a search bar.

 search.addWidget(
  instantsearch.widgets.searchBox({
    container: "#searchbox",
    placeholder: "Search articles",
    autofocus: false
  })
); 

// Uncomment the following widget to add search stats.

search.addWidget(
  instantsearch.widgets.stats({
    container: "#stats",
    templates: {
      body(hit) {
        return `<span role="img" aria-label="emoji">⚡️</span> <strong>${hit.nbHits}</strong> results found ${
          hit.query != "" ? `for <strong>"${hit.query}"</strong>` : ``
        } in <strong>${hit.processingTimeMS}ms</strong>`;
      }
    }
  })
); 

// Uncomment the following widget to add categories list.

 search.addWidget(
  instantsearch.widgets.refinementList({
    container: "#categories",
    attributeName: "categories",
    autoHideContainer: false,
    templates: {
      header: "Categories"
    }
  })
); 

// Uncomment the following widget to add pagination.

 search.addWidget(
  instantsearch.widgets.pagination({
    container: "#pagination"
  })
); 

search.start();
@import url("https://fonts.googleapis.com/css?family=Open+Sans");

* {
  box-sizing: border-box;
}

html {
  height: 100%;
}

html,
body {
  padding: 0;
  margin: 0;
}

body {
  min-height: 100%;
  padding: 24px;
  font-size: 14px;
  line-height: 24px;
  overflow-y: scroll;
  display: flex;
  font-family: "Open Sans", sans-serif;
  background: #f0f2f5;
  flex-direction: column;
}

a {
  color: #5468ff;
}

em {
  font-style: normal;
  font-weight: bold;
  text-decoration: underline;
}

.search-container {
  flex-grow: 1;
  display: flex;
  width: 960px;
  min-height: 100%;
  margin: 0 auto;
}

@media (max-width: 1007px) {
  .search-container {
    width: 100%;
  }
}

#categories {
  margin-right: 24px;
  padding: 8px 0;
}

#categories,
.ais-hits--item,
.ais-search-box--input {
  height: auto;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
}

.ais-search-box--input:focus {
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
}

.left-panel {
  flex: 0 0 240px;
}

.right-panel {
  width: calc(100% - 256px);
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

#searchbox {
  flex-grow: 0;
  margin-bottom: 8px;
}

#searchbox,
#stats {
  flex-grow: 0;
}

.ais-search-box {
  position: relative;
  height: auto;
  width: 100%;
}

.ais-search-box--magnifier,
.ais-search-box--reset {
  position: absolute;
  top: 0;
  left: inherit;
  margin: 14px 16px;
  cursor: pointer;
}

.ais-search-box--magnifier svg,
.ais-search-box--reset svg {
  width: 13px;
  height: 13px;
  display: block;
}

.ais-search-box--magnifier svg {
  fill: #5468ff;
}

.ais-search-box--reset svg {
  fill: #ed5a6a;
}

.ais-search-box--magnifier {
  right: 0;
}

.ais-search-box--reset {
  background: none;
  padding: 0;
  border: none;
  right: 30px;
}

.ais-search-box--input {
  width: 100%;
  padding: 8px 16px;
  font-weight: normal;
  border: none;
  -webkit-appearance: none;
  appearance: none;
}

#stats {
  flex-grow: 0;
  margin-bottom: 16px;
}

.ais-stats {
  font-size: 12px;
  color: #697782;
  opacity: 1;
}

#hits {
  flex-grow: 1;
}

.ais-refinement-list--header {
  font-weight: bolder;
  margin: 0;
  padding: 0 16px 8px;
  font-size: inherit;
  text-transform: uppercase;
  border-bottom: 1px solid #eee;
}

.ais-hits--item {
  margin-bottom: 24px;
}

.ais-hits--item article {
  display: flex;
}

.ais-hits--item .entry-author-content {
  font-size: 13px;
}

.ais-hits--item .post-date,
.ais-hits--item .post-meta-info {
  color: #999;
  font-size: 12px;
}

.ais-hits--item .entry-meta {
  display: flex;
}

.ais-hits--item .entry-title a {
  text-decoration: none;
}

.ais-hits--item .author-gravatar img {
  border-radius: 50%;
  margin-right: 8px;
  display: block;
}

.ais-hits--item .post-img a,
.ais-hits--item .post-img img {
  width: 100%;
  display: block;
}

@media (min-width: 768px) {
  .ais-hits--item .post-img {
    flex: 0 0 240px;
    overflow: hidden;
  }

  .ais-hits--item .post-img,
  .ais-hits--item .post-img a {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .ais-hits--item .post-img a {
    height: 100%;
  }

  .ais-hits--item .post-img img {
    width: auto;
    height: 100%;
    display: block;
  }
}

@media (max-width: 767px) {
  .ais-hits--item article {
    flex-direction: column;
  }
}

.ais-hits--item .post-excerpt {
  margin-bottom: 16px;
}

.ais-hits--item .entry-title {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: normal;
}

.ais-hits--item .post-content {
  padding: 16px;
}

.ais-refinement-list--item {
  padding: 8px 16px;
}

.ais-refinement-list--label,
.ais-refinement-list--label:hover {
  color: black;
}

.ais-refinement-list--count {
  background: none;
  padding: 0;
  font-size: 12px;
  line-height: 24px;
  color: #999;
  float: right;
}

.ais-refinement-list--checkbox {
  margin: 0;
}

.ais-refinement-list--item__active .ais-refinement-list--label,
.ais-refinement-list--item__active .ais-refinement-list--count {
  font-weight: normal;
}

.ais-pagination {
  display: flex;
  margin: 0 0 16px;
  padding: 0;
  background: none;
  list-style: none;
  justify-content: center;
  align-items: center;
  text-align: center;
  border: none;
  box-shadow: none;
  flex-wrap: wrap;
}

.ais-pagination--item,
.ais-pagination--item__disabled {
  flex: 0 0 40px;
  width: auto;
  height: auto;
  margin: 4px;
  background-color: #5468ff;
  list-style-image: none;
  list-style-position: outside;
  list-style-type: none;
  text-align: center;
  border-radius: 4px;
  transition: all 0.1s linear;
}

.ais-pagination--item__disabled {
  display: none;
}

.ais-pagination--item:hover,
.ais-pagination--item__active {
  background-color: #3a46a1;
}

.ais-pagination--item .ais-pagination--link,
.ais-pagination--item__disabled .ais-pagination--link {
  text-decoration: none;
  color: #fff;
  line-height: 32px;
}

footer {
  flex-grow: 0;
  text-align: center;
  font-size: 12px;
  color: #999;
}

/* Placeholders */

#searchbox:empty:before,
#hits:empty:before,
#categories:empty:before,
#stats:empty:before,
#pagination:empty:before {
  display: block;
  color: #999;
  padding: 16px;
}

#searchbox:empty,
#hits:empty,
#categories:empty,
#stats:empty,
#pagination:empty {
  padding: 0;
  background: none;
  border: dashed 3px #ddd;
  box-shadow: none;
  border-radius: 4px;
}

#searchbox:empty,
#stats:empty,
#hits:empty,
#pagination:empty {
  margin-bottom: 24px;
}

#searchbox:empty:before {
  content: "Uncomment the searchBox widget to add a search bar.";
}

#hits:empty:before {
  content: "Uncomment the hits widget to add hits list.";
}

#stats:empty:before {
  content: "Uncomment the stats widget to add search stats.";
}

#categories:empty:before {
  content: "Uncomment the categories widget to add categories list.";
}

#pagination:empty:before {
  content: "Uncomment the pagination widget to add pagination.";
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="theme-color" content="#000000">
  <title>Algolia InstantSearch | Media demo</title>
  <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/instantsearch.js@2.6.0/dist/instantsearch-theme-algolia.min.css">
  <link rel="stylesheet" type="text/css" href="src/app.css">
</head>

<body>
  <main class="search-container">
    <div class="left-panel">
      <div id="categories"></div>
    </div>
    <div class="right-panel">
      <div id="searchbox"></div>
      <div id="stats"></div>
      <div id="hits"></div>
      <div id="pagination"></div>
    </div>
  </main>
  <footer>
    Source Code on
    <a href="https://github.com/algolia/doc-onboarding">GitHub</a> | Powered by
    <a href="https://www.algolia.com">Algolia</a>
  </footer>
  <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@2.8.0/dist/instantsearch.min.js"></script>
  <script src="./src/app.js"></script>
</body>

</html>

Hi I'm trying to set up an Algolia search bar. I followed all the steps in the guided tutorial but when i run the same code locally all the feature appear to be commented out. But they aren't in my actual code. I think it might be in the CSS file but im not sure what is wrong.

I attached some photos and the repo below.

If anyone has ran into the issue I would really appreciate any help. Thanks!

The issue

Repo


Solution

  • Actually the issue may come from the credentials. I've replicated the logic in a CodeSandbox and I manage to reproduce the issue. It looks like you've used the Algolia public credentials with your index name Index_PinIt. If you replace the index name with demo_ecommerce it's working fine. You should use your own credentials to reach your index. You can find them in the Algolia Dashboard. Let me know if it helps!