Skip to content

Commit

Permalink
feat(js): support overlay click to close modal
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Feb 16, 2021
1 parent 33e26de commit f945a87
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
8 changes: 4 additions & 4 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function autocomplete<TItem extends BaseItem>(
dom: dom.value,
Fragment: props.value.renderer.renderer.Fragment,
panelContainer: isDetached.value
? dom.value.detachedOverlay
? dom.value.detachedContainer
: props.value.renderer.panelContainer,
propGetters,
state: lastStateRef.current,
Expand Down Expand Up @@ -182,7 +182,7 @@ export function autocomplete<TItem extends BaseItem>(
? props.value.core.environment.document.body
: props.value.renderer.panelContainer;
const panelElement = isDetached.value
? dom.value.detachedOverlay
? dom.value.detachedContainer
: dom.value.panel;

if (isDetached.value && lastStateRef.current.isOpen) {
Expand Down Expand Up @@ -257,8 +257,8 @@ export function autocomplete<TItem extends BaseItem>(
}

function toggleModalClassname(isActive: boolean) {
dom.value.detachedOverlay.classList.toggle(
'aa-DetachedOverlay--Modal',
dom.value.detachedContainer.classList.toggle(
'aa-DetachedContainer--Modal',
isActive
);
}
Expand Down
15 changes: 13 additions & 2 deletions packages/autocomplete-js/src/createAutocompleteDom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ export function createAutocompleteDom<TItem extends BaseItem>({
class: classNames.root,
...rootProps,
});
const detachedContainer = createDomElement('div', {
class: classNames.detachedContainer,
onMouseDown(event: MouseEvent) {
event.stopPropagation();
},
});
const detachedOverlay = createDomElement('div', {
class: classNames.detachedOverlay,
children: [detachedContainer],
onMouseDown() {
document.body.removeChild(detachedOverlay);
onDetachedOverlayClose();
},
});

const labelProps = propGetters.getLabelProps({
Expand Down Expand Up @@ -171,15 +182,15 @@ export function createAutocompleteDom<TItem extends BaseItem>({
children: [form, detachedCancelButton],
});

detachedOverlay.appendChild(detachedFormContainer);
detachedContainer.appendChild(detachedFormContainer);
root.appendChild(detachedSearchButton);
} else {
root.appendChild(form);
}

return {
openDetachedOverlay,
detachedOverlay,
detachedContainer,
inputWrapper,
input,
root,
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/getDefaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getHTMLElement, mergeClassNames } from './utils';
const defaultClassNames: AutocompleteClassNames = {
detachedCancelButton: 'aa-DetachedCancelButton',
detachedFormContainer: 'aa-DetachedFormContainer',
detachedContainer: 'aa-DetachedContainer',
detachedOverlay: 'aa-DetachedOverlay',
detachedSearchButton: 'aa-DetachedSearchButton',
detachedSearchButtonIcon: 'aa-DetachedSearchButtonIcon',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type AutocompleteClassNames = {
detachedCancelButton: string;
detachedFormContainer: string;
detachedContainer: string;
detachedOverlay: string;
detachedSearchButton: string;
detachedSearchButtonIcon: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/autocomplete-js/src/types/AutocompleteDom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export type AutocompleteDom = {
resetButton: HTMLButtonElement;
loadingIndicator: HTMLDivElement;
panel: HTMLDivElement;
detachedOverlay: HTMLDivElement;
detachedContainer: HTMLDivElement;
};
25 changes: 12 additions & 13 deletions packages/autocomplete-theme-classic/src/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ body {
}
}

.aa-DetachedOverlay {
.aa-DetachedContainer {
background: var(--aa-background-color);
bottom: 0;
display: flex;
Expand Down Expand Up @@ -553,7 +553,7 @@ body {
height: 32px;
}

&.aa-DetachedOverlay--Modal {
&.aa-DetachedContainer--Modal {
border-radius: 6px;
bottom: inherit;
height: auto;
Expand All @@ -574,17 +574,16 @@ body {
.aa-Detached {
height: 100vh;
overflow: hidden;
&::after {
background-color: var(--aa-muted-color);
content: '';
height: 100vh;
left: 0;
opacity: 0.75;
position: fixed;
right: 0;
top: 0;
z-index: 999;
}
}

.aa-DetachedOverlay {
background-color: var(--aa-muted-color);
height: 100vh;
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 999;
}

@media (hover: none) and (pointer: coarse) {
Expand Down
1 change: 1 addition & 0 deletions packages/website/docs/autocomplete-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ The class names to inject in each created DOM element. It it useful to design wi
type ClassNames = Partial<{
detachedCancelButton: string;
detachedFormContainer: string;
detachedContainer: string;
detachedOverlay: string;
detachedSearchButton: string;
detachedSearchButtonIcon: string;
Expand Down

0 comments on commit f945a87

Please sign in to comment.