-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: upgrade AutocompleteJS to v1 & update related code (Top Nav…
…igation Desktop Only) (#1781) * deps(npm): install autocomplete and friends and remove unused algoliasearch lib * fix extra snapshot * rename _new_header.html.heex * feat(Algolia.Query): build query params on backend - checks for valid index - adjusts query params based on index * feat(SiteWeb.Components): algolia_autocomplete component * feat: Promise-compatible debounce function * feat: autocomplete v1 search in header * css: adapt the default autocomplete theme * refactor: use React instead of default Preact renderer * deps(npm): remove no longer needed Preact dep * fixup: rename newly-exported function * fixup: shift padding down one element This will increase the clickable area of links and buttons. * feedback: remove unused helper
- Loading branch information
1 parent
c30fd6f
commit ee26428
Showing
34 changed files
with
1,622 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule Algolia.Query.Request do | ||
@moduledoc """ | ||
Algolia's REST API expects this format for each request | ||
https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-preset-algolia/getAlgoliaResults/#param-queries-2 | ||
This module has some helpers to create requests for our indexes with some | ||
specific default parameters. | ||
""" | ||
@supported_indexes Algolia.Query.valid_indexes() | ||
@supported_index_keys Algolia.Query.valid_indexes() |> Keyword.keys() |> Enum.map(&to_string/1) | ||
|
||
defstruct indexName: "", | ||
query: "", | ||
params: %{ | ||
"hitsPerPage" => 5, | ||
"clickAnalytics" => true, | ||
"facets" => ["*"], | ||
"facetFilters" => [[]] | ||
}, | ||
attributesToHighlight: "" | ||
|
||
@type t :: %__MODULE__{ | ||
indexName: String.t(), | ||
query: String.t(), | ||
params: map(), | ||
attributesToHighlight: String.t() | [String.t()] | ||
} | ||
@spec new(String.t(), String.t()) :: t() | ||
def new(indexName, query) when indexName in @supported_index_keys do | ||
algoliaIndex = Keyword.fetch!(@supported_indexes, String.to_atom(indexName)) | ||
|
||
%__MODULE__{ | ||
indexName: algoliaIndex, | ||
query: query, | ||
attributesToHighlight: highlight(indexName) | ||
} | ||
|> with_hit_size(indexName) | ||
|> with_facet_filters(indexName) | ||
end | ||
|
||
defp highlight("routes"), do: ["route.name", "route.long_name"] | ||
defp highlight("stops"), do: "stop.name" | ||
defp highlight("drupal"), do: "content_title" | ||
|
||
defp with_hit_size(request, indexName) do | ||
%__MODULE__{ | ||
request | ||
| params: %{request.params | "hitsPerPage" => hit_size(indexName)} | ||
} | ||
end | ||
|
||
defp hit_size("routes"), do: 5 | ||
defp hit_size("stops"), do: 2 | ||
defp hit_size("drupal"), do: 2 | ||
|
||
@spec with_facet_filters(t(), String.t()) :: t() | ||
defp with_facet_filters(request, "drupal") do | ||
%__MODULE__{ | ||
request | ||
| params: %{ | ||
request.params | ||
| "facetFilters" => [ | ||
[ | ||
"_content_type:page", | ||
"_content_type:search_result", | ||
"_content_type:diversion", | ||
"_content_type:landing_page", | ||
"_content_type:person", | ||
"_content_type:project", | ||
"_content_type:project_update" | ||
] | ||
] | ||
} | ||
} | ||
end | ||
|
||
defp with_facet_filters(request, _), do: request | ||
|
||
def encode(%__MODULE__{} = request) do | ||
request | ||
|> Map.from_struct() | ||
|> Map.update!(:params, &Algolia.Query.encode_params/1) | ||
|> Map.new(fn {k, v} -> {to_string(k), v} end) | ||
# The highlight tag values are needed for compatibility with Algolia's | ||
# autocomplete.js library v1+ | ||
|> Map.put_new("highlightPreTag", "__aa-highlight__") | ||
|> Map.put_new("highlightPostTag", "__/aa-highlight__") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule Algolia.Query.RequestTest do | ||
@moduledoc false | ||
use ExUnit.Case, async: true | ||
alias Algolia.Query.Request | ||
|
||
describe "new/2" do | ||
test "doesn't work for invalid index" do | ||
assert Request.new("drupal", "question") | ||
assert_raise FunctionClauseError, fn -> Request.new("fakeindex", "question") end | ||
end | ||
|
||
test "includes query" do | ||
search_string = "asfdsagfsadgsad" | ||
assert %Request{query: ^search_string} = Request.new("drupal", search_string) | ||
end | ||
|
||
test "changes hitsPerPage param based on index" do | ||
assert %Request{params: %{"hitsPerPage" => 5}} = Request.new("routes", "") | ||
assert %Request{params: %{"hitsPerPage" => 2}} = Request.new("drupal", "") | ||
end | ||
|
||
test "changes attributesToHighlight param based on index" do | ||
assert %Request{attributesToHighlight: "stop.name"} = Request.new("stops", "") | ||
|
||
assert %Request{attributesToHighlight: ["route.name", "route.long_name"]} = | ||
Request.new("routes", "") | ||
end | ||
|
||
test "changes facetFilters param based on index" do | ||
assert %Request{params: %{"facetFilters" => [[]]}} = Request.new("routes", "") | ||
assert %Request{params: %{"facetFilters" => facetFilters}} = Request.new("drupal", "") | ||
refute facetFilters == [[]] | ||
end | ||
end | ||
|
||
test "encode/1 returns a JSON-encodable map with encoded params" do | ||
request = Request.new("drupal", "some special search") | ||
encoded = Request.encode(request) | ||
|
||
assert encoded == %{ | ||
"attributesToHighlight" => "content_title", | ||
"highlightPostTag" => "__/aa-highlight__", | ||
"highlightPreTag" => "__aa-highlight__", | ||
"indexName" => "drupal_test", | ||
"params" => | ||
"analytics=false&clickAnalytics=true&facetFilters=%5B%5B%22_content_type%3Apage%22%2C%22_content_type%3Asearch_result%22%2C%22_content_type%3Adiversion%22%2C%22_content_type%3Alanding_page%22%2C%22_content_type%3Aperson%22%2C%22_content_type%3Aproject%22%2C%22_content_type%3Aproject_update%22%5D%5D&facets=%5B%22*%22%5D&hitsPerPage=2", | ||
"query" => "some special search" | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
@import '@algolia/autocomplete-theme-classic'; | ||
|
||
// right now #header-desktop is the only search using the new library, so it's | ||
// the only one styled here. the #header-desktop can be removed once we migrate | ||
// the rest over | ||
.c-search-bar__autocomplete#header-desktop { | ||
.aa-Autocomplete { | ||
--aa-icon-color-rgb: 22, 92, 150; // $brand-primary; | ||
--aa-primary-color-rgb: 22, 92, 150; // $brand-primary; | ||
--aa-search-input-height: 2.25rem; | ||
} | ||
|
||
.aa-Item { | ||
@include icon-size-inline(1em); | ||
border-bottom: $border; | ||
border-radius: 0; | ||
font-weight: normal; | ||
|
||
&:hover { | ||
background-color: $brand-primary-lightest; | ||
} | ||
|
||
em { | ||
font-style: inherit; | ||
font-weight: bold; | ||
} | ||
|
||
> a, | ||
> button { | ||
color: currentColor; | ||
display: flex; | ||
font-weight: inherit; | ||
gap: .25rem; | ||
min-width: 0; | ||
padding: calc(#{$base-spacing} / 2) $base-spacing; | ||
} | ||
|
||
a:hover { | ||
text-decoration: none; | ||
} | ||
} | ||
|
||
.aa-ItemContent, | ||
.aa-ItemContentBody { | ||
display: unset; | ||
} | ||
|
||
.aa-ItemContent { | ||
mark { | ||
padding: 0; | ||
} | ||
> * { | ||
margin-right: .25rem; | ||
} | ||
} | ||
|
||
.aa-ItemContentTitle { | ||
display: unset; | ||
margin: unset; | ||
overflow: visible; | ||
text-overflow: unset; | ||
white-space: normal; | ||
} | ||
|
||
.aa-PanelLayout { | ||
padding: unset; | ||
} | ||
|
||
.aa-Panel { | ||
// stylelint-disable-next-line declaration-no-important | ||
top: 3.25rem !important; | ||
z-index: var(--aa-base-z-index); | ||
} | ||
|
||
.aa-Label { | ||
margin-bottom: unset; | ||
} | ||
|
||
.aa-Input { | ||
// stylelint-disable-next-line declaration-no-important | ||
height: var(--aa-search-input-height) !important; | ||
} | ||
|
||
.aa-InputWrapperPrefix { | ||
order: 3; // move search icon to end. | ||
} | ||
|
||
.aa-InputWrapper { | ||
order: 1; | ||
padding-left: 1rem; | ||
} | ||
|
||
.aa-InputWrapperSuffix { | ||
order: 2; | ||
} | ||
|
||
.aa-Form { | ||
border: 3px solid $brand-primary; | ||
border-radius: .5rem; | ||
|
||
&:focus-within { | ||
border-color: $brand-primary-lightest; | ||
box-shadow: unset; | ||
} | ||
} | ||
|
||
.aa-LoadingIndicator, | ||
.aa-SubmitButton { | ||
padding-left: var(--aa-spacing-half); | ||
width: calc(var(--aa-spacing) * 1.25 + var(--aa-icon-size) - 1px); | ||
} | ||
|
||
.aa-ClearButton { | ||
@include fa-icon-solid($fa-var-times-circle); | ||
color: rgba(var(--aa-icon-color-rgb), var(--aa-icon-color-alpha)); | ||
padding-right: var(--aa-spacing-half); | ||
// hide default icon | ||
.aa-ClearIcon { display: none; } | ||
} | ||
|
||
.aa-SubmitButton { | ||
@include fa-icon-solid($fa-var-search); | ||
color: rgba(var(--aa-icon-color-rgb), var(--aa-icon-color-alpha)); | ||
// hide default icon | ||
.aa-SubmitIcon { display: none; } | ||
} | ||
|
||
.aa-GradientBottom, | ||
.aa-GradientTop { all: unset; } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.