Skip to content

Commit

Permalink
fix(theme-classic): wrap item content when there's no link (#522)
Browse files Browse the repository at this point in the history
* fix(theme-classic): wrap item content when there's no link

* fix: apply suggestions from code review

Co-authored-by: François Chalifour <[email protected]>

* fix: remove unused dependencies

* fix: expose createElement

Co-authored-by: François Chalifour <[email protected]>
  • Loading branch information
sarahdayan and francoischalifour authored Apr 12, 2021
1 parent 53c79a4 commit c6afe42
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 89 deletions.
4 changes: 2 additions & 2 deletions cypress/test-apps/js/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type ProductItemProps = {

function ProductItem({ hit, insights, components }: ProductItemProps) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemIcon aa-ItemIcon--alignTop">
<img src={hit.image} alt={hit.name} width="40" height="40" />
</div>
Expand Down Expand Up @@ -173,6 +173,6 @@ function ProductItem({ hit, insights, components }: ProductItemProps) {
</svg>
</button>
</div>
</Fragment>
</div>
);
}
4 changes: 2 additions & 2 deletions cypress/test-apps/js/categoriesPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function createCategoriesPlugin({
},
item({ item, components }) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemIcon aa-ItemIcon--noBorder">
<svg
viewBox="0 0 24 24"
Expand All @@ -75,7 +75,7 @@ export function createCategoriesPlugin({
<components.Highlight hit={item} attribute="label" />
</div>
</div>
</Fragment>
</div>
);
},
},
Expand Down
6 changes: 3 additions & 3 deletions cypress/test-apps/js/shortcutsPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const shortcutsPlugin: AutocompletePlugin<DarkModeItem, undefined> = {
createElement('div', { className: 'aa-SourceHeaderLine' })
);
},
item({ item, createElement, Fragment }) {
item({ item, createElement }) {
const darkIcon = createElement(
'svg',
{
Expand Down Expand Up @@ -74,8 +74,8 @@ export const shortcutsPlugin: AutocompletePlugin<DarkModeItem, undefined> = {
);

return createElement(
Fragment,
{},
'div',
{ className: 'aa-ItemWrapper' },
createElement(
'div',
{ className: 'aa-ItemIcon' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export function createGitHubReposPlugin(
return item.html_url;
},
templates: {
item({ item, Fragment }) {
item({ item }) {
const stars = new Intl.NumberFormat('en-US').format(
item.stargazers_count
);

return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemContent">
<div className="aa-ItemIcon aa-ItemIcon--alignTop">
<img
Expand Down Expand Up @@ -139,7 +139,7 @@ export function createGitHubReposPlugin(
</svg>
</button>
</div>
</Fragment>
</div>
);
},
},
Expand Down
4 changes: 2 additions & 2 deletions examples/playground/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type ProductItemProps = {

function ProductItem({ hit, insights, components }: ProductItemProps) {
return (
<Fragment>
<a href={hit.url} className="aa-ItemLink">
<div className="aa-ItemContent">
<div className="aa-ItemIcon aa-ItemIcon--picture aa-ItemIcon--alignTop">
<img src={hit.image} alt={hit.name} width="40" height="40" />
Expand Down Expand Up @@ -310,6 +310,6 @@ function ProductItem({ hit, insights, components }: ProductItemProps) {
</svg>
</button>
</div>
</Fragment>
</a>
);
}
4 changes: 2 additions & 2 deletions examples/playground/categoriesPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function createCategoriesPlugin({
},
item({ item, components }) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemContent">
<div className="aa-ItemIcon aa-ItemIcon--noBorder">
<svg
Expand All @@ -80,7 +80,7 @@ export function createCategoriesPlugin({
<components.Highlight hit={item} attribute="label" />
</div>
</div>
</Fragment>
</div>
);
},
},
Expand Down
6 changes: 3 additions & 3 deletions examples/playground/shortcutsPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const shortcutsPlugin: AutocompletePlugin<DarkModeItem, undefined> = {
createElement('div', { className: 'aa-SourceHeaderLine' })
);
},
item({ item, createElement, Fragment }) {
item({ item, createElement }) {
const darkIcon = createElement(
'svg',
{
Expand Down Expand Up @@ -76,8 +76,8 @@ export const shortcutsPlugin: AutocompletePlugin<DarkModeItem, undefined> = {
);

return createElement(
Fragment,
{},
'div',
{ className: 'aa-ItemWrapper' },
createElement(
'div',
{ className: 'aa-ItemContent' },
Expand Down
4 changes: 2 additions & 2 deletions examples/query-suggestions-with-hits/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type ProductItemProps = {

function ProductItem({ hit, insights, components }: ProductItemProps) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemIcon aa-ItemIcon--alignTop">
<img src={hit.image} alt={hit.name} width="40" height="40" />
</div>
Expand Down Expand Up @@ -185,6 +185,6 @@ function ProductItem({ hit, insights, components }: ProductItemProps) {
</svg>
</button>
</div>
</Fragment>
</div>
);
}
6 changes: 3 additions & 3 deletions examples/query-suggestions-with-inline-categories/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { autocomplete } from '@algolia/autocomplete-js';
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';
import algoliasearch from 'algoliasearch';
import { h, Fragment } from 'preact';
import { h } from 'preact';

import '@algolia/autocomplete-theme-classic';

Expand Down Expand Up @@ -32,7 +32,7 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
...source.templates,
item({ item, components }) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemIcon aa-ItemIcon--noBorder">
<svg
viewBox="0 0 24 24"
Expand Down Expand Up @@ -84,7 +84,7 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
</svg>
</button>
</div>
</Fragment>
</div>
);
},
},
Expand Down
74 changes: 38 additions & 36 deletions examples/react-renderer/src/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,43 +159,45 @@ export function Autocomplete(
className="aa-Item"
{...autocomplete.getItemProps({ item, source })}
>
<div className="aa-ItemIcon">
<img
src={item.image}
alt={item.name}
width="40"
height="40"
/>
</div>
<div className="aa-ItemContent">
<div
className="aa-ItemContentTitle"
dangerouslySetInnerHTML={{
__html: item._snippetResult!.name.value,
}}
/>
<div
className="aa-ItemContentDescription"
dangerouslySetInnerHTML={{
__html: item._snippetResult!.description
.value,
}}
/>
</div>
<button
className="aa-ItemActionButton aa-DesktopOnly aa-ActiveOnly"
type="button"
title="Select"
>
<svg
fill="currentColor"
viewBox="0 0 24 24"
width="20"
height="20"
<div className="aa-ItemWrapper">
<div className="aa-ItemIcon">
<img
src={item.image}
alt={item.name}
width="40"
height="40"
/>
</div>
<div className="aa-ItemContent">
<div
className="aa-ItemContentTitle"
dangerouslySetInnerHTML={{
__html: item._snippetResult!.name.value,
}}
/>
<div
className="aa-ItemContentDescription"
dangerouslySetInnerHTML={{
__html: item._snippetResult!.description
.value,
}}
/>
</div>
<button
className="aa-ItemActionButton aa-DesktopOnly aa-ActiveOnly"
type="button"
title="Select"
>
<path d="M18.984 6.984h2.016v6h-15.188l3.609 3.609-1.406 1.406-6-6 6-6 1.406 1.406-3.609 3.609h13.172v-4.031z" />
</svg>
</button>
<svg
fill="currentColor"
viewBox="0 0 24 24"
width="20"
height="20"
>
<path d="M18.984 6.984h2.016v6h-15.188l3.609 3.609-1.406 1.406-6-6 6-6 1.406 1.406-3.609 3.609h13.172v-4.031z" />
</svg>
</button>
</div>
</li>
);
})}
Expand Down
4 changes: 2 additions & 2 deletions examples/recently-viewed-items/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type ProductItemProps = {

function AutocompleteProductItem({ hit, components }: ProductItemProps) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemIcon aa-ItemIcon--alignTop">
<img src={hit.image} alt={hit.name} width="40" height="40" />
</div>
Expand All @@ -109,6 +109,6 @@ function AutocompleteProductItem({ hit, components }: ProductItemProps) {
</svg>
</button>
</div>
</Fragment>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export function getTemplates<TItem extends QuerySuggestionsHit>({
onTapAhead,
}: GetTemplatesParams<TItem>): SourceTemplates<TItem> {
return {
item({ item, createElement, Fragment, components }) {
item({ item, createElement, components }) {
if (item.__autocomplete_qsCategory) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemContent aa-ItemContent--indented">
<div className="aa-ItemContentSubtitle aa-ItemContentSubtitle--standalone">
<span className="aa-ItemContentSubtitleIcon" />
Expand All @@ -26,12 +26,12 @@ export function getTemplates<TItem extends QuerySuggestionsHit>({
</span>
</div>
</div>
</Fragment>
</div>
);
}

return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemContent">
<div className="aa-ItemIcon aa-ItemIcon--noBorder">
<svg viewBox="0 0 24 24" fill="currentColor">
Expand Down Expand Up @@ -59,7 +59,7 @@ export function getTemplates<TItem extends QuerySuggestionsHit>({
</svg>
</button>
</div>
</Fragment>
</div>
);
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export function getTemplates<TItem extends RecentSearchesItem>({
onTapAhead,
}: GetTemplatesParams<TItem>): SourceTemplates<TItem> {
return {
item({ item, createElement, Fragment, components }) {
item({ item, createElement, components }) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemContent">
<div className="aa-ItemIcon aa-ItemIcon--noBorder">
<svg viewBox="0 0 24 24" fill="currentColor">
Expand Down Expand Up @@ -65,7 +65,7 @@ export function getTemplates<TItem extends RecentSearchesItem>({
</svg>
</button>
</div>
</Fragment>
</div>
);
},
};
Expand Down
9 changes: 4 additions & 5 deletions packages/autocomplete-theme-classic/src/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,6 @@ body {
align-items: center;
border-radius: 3px;
cursor: pointer;
display: grid;
gap: calc(var(--aa-spacing-half) / 2);
grid-auto-flow: column;
justify-content: space-between;
min-height: calc(var(--aa-spacing) * 2.5);
padding: calc(var(--aa-spacing-half) / 2);
// When the result is active
Expand Down Expand Up @@ -693,7 +689,10 @@ body {
.aa-ItemLink {
align-items: center;
color: inherit;
display: flex;
display: grid;
gap: calc(var(--aa-spacing-half) / 2);
grid-auto-flow: column;
justify-content: space-between;
text-decoration: none;
width: 100%;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/website/docs/autocomplete-theme-classic.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ autocomplete({
templates: {
item({ item, components }) {
return (
<Fragment>
<div className="aa-ItemWrapper">
<div className="aa-ItemIcon">
<img src={item.image} alt={item.name} width="40" height="40" />
</div>
Expand Down Expand Up @@ -93,18 +93,16 @@ autocomplete({
</svg>
</button>
</div>
</Fragment>
</div>
);
},
},
});
```

If your renderer doesn't support `Fragment`s, you can use `div.aa-ItemWrapper`.

### Link item

To wrap an item within a link, use the `.aa-ItemLink` class.
To wrap an item within a link, use the `.aa-ItemLink` class in place of the element with `.aa-ItemWrapper`. **Make sure not to use both together.**

```jsx
autocomplete({
Expand Down
Loading

0 comments on commit c6afe42

Please sign in to comment.