Skip to content

Commit

Permalink
feat(js): add PanelLayout element
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Nov 21, 2020
1 parent 442ef97 commit 371fae0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 28 deletions.
19 changes: 11 additions & 8 deletions examples/js/autocomplete.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@
}

.aa-Panel {
background-color: #fff;
border: 1px solid rgba(150, 150, 150, 0.16);
border-radius: 3px;
box-shadow: 0 0 0 1px rgba(35, 38, 59, 0.05),
0 8px 16px -4px rgba(35, 38, 59, 0.25);
margin-top: 5px;
max-width: 480px;
position: absolute;
width: 100%;
Expand All @@ -86,12 +80,21 @@
transition: opacity 200ms ease-in;
}

.aa-Panel a {
.aa-PanelLayout {
background-color: #fff;
border: 1px solid rgba(150, 150, 150, 0.16);
border-radius: 3px;
box-shadow: 0 0 0 1px rgba(35, 38, 59, 0.05),
0 8px 16px -4px rgba(35, 38, 59, 0.25);
margin-top: 5px;
}

.aa-PanelLayout a {
color: inherit;
text-decoration: none;
}

.aa-Panel ul {
.aa-PanelLayout ul {
list-style: none;
margin: 0;
padding: 0;
Expand Down
1 change: 0 additions & 1 deletion packages/autocomplete-js/src/components/Panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const Panel: Component<PanelProps, HTMLDivElement> = ({
const element = document.createElement('div');
setProperties(element, {
...props,
hidden: true,
class: concatClassNames(['aa-Panel', classNames.panel]),
});

Expand Down
17 changes: 17 additions & 0 deletions packages/autocomplete-js/src/components/PanelLayout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component, WithClassNames } from '../types/Component';
import { concatClassNames, setProperties } from '../utils';

type PanelLayoutProps = WithClassNames<{}>;

export const PanelLayout: Component<PanelLayoutProps, HTMLDivElement> = ({
classNames,
...props
}) => {
const element = document.createElement('div');
setProperties(element, {
...props,
class: concatClassNames(['aa-PanelLayout', classNames.panelLayout]),
});

return element;
};
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './Input';
export * from './InputWrapper';
export * from './Label';
export * from './Panel';
export * from './PanelLayout';
export * from './ResetButton';
export * from './ResetIcon';
export * from './Root';
Expand Down
1 change: 0 additions & 1 deletion packages/autocomplete-js/src/createAutocompleteDom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export function createAutocompleteDom<TItem>({
inputWrapper.appendChild(resetButton);
form.appendChild(inputWrapper);
root.appendChild(form);
root.appendChild(panel);

return {
inputWrapper,
Expand Down
17 changes: 13 additions & 4 deletions packages/autocomplete-js/src/render.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AutocompleteApi as AutocompleteCoreApi } from '@algolia/autocomplete-core';

import {
PanelLayout,
SourceContainer,
SourceFooter,
SourceHeader,
Expand All @@ -14,7 +15,7 @@ import {
AutocompleteRenderer,
AutocompleteState,
} from './types';
import { setProperties, setPropertiesWithoutEvents } from './utils';
import { setPropertiesWithoutEvents } from './utils';

type RenderProps<TItem> = {
state: AutocompleteState<TItem>;
Expand Down Expand Up @@ -42,11 +43,16 @@ export function render<TItem>(
panel.innerHTML = '';

if (!state.isOpen) {
setProperties(panel, { hidden: true });
if (root.contains(panel)) {
root.removeChild(panel);
}

return;
}

setProperties(panel, { hidden: false });
if (!root.contains(panel)) {
root.appendChild(panel);
}

if (state.status === 'stalled') {
panel.classList.add('aa-Panel--stalled');
Expand Down Expand Up @@ -106,5 +112,8 @@ export function render<TItem>(
return sectionElement;
});

renderer({ root: panel, sections, state });
const panelLayoutElement = PanelLayout({ classNames });
panel.appendChild(panelLayoutElement);

renderer({ root: panelLayoutElement, sections, state });
}
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type AutocompleteClassNames = Partial<{
input: string;
resetButton: string;
panel: string;
panelLayout: string;
source: string;
sourceHeader: string;
list: string;
Expand Down
29 changes: 15 additions & 14 deletions packages/website/docs/autocomplete-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,21 @@ The panel horizontal position.
The class names to inject in each created DOM element. It it useful to design with external CSS frameworks.

```ts
type ClassNames = {
root?: string;
form?: string;
label?: string;
inputWrapper?: string;
input?: string;
resetButton?: string;
panel?: string;
source?: string;
sourceHeader?: string;
list?: string;
item?: string;
sourceFooter?: string;
};
type ClassNames = Partial<{
root: string;
form: string;
label: string;
inputWrapper: string;
input: string;
resetButton: string;
panel: string;
panelLayout: string;
source: string;
sourceHeader: string;
list: string;
item: string;
sourceFooter: string;
}>;
```

### `render`
Expand Down

0 comments on commit 371fae0

Please sign in to comment.