Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: close sidebar menu when click outside #766

Merged
merged 12 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion projects/ion/src/lib/sidebar/sidebar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</section>
</nav>

<span>
<span data-testid="ion-sidebar__outside-container">
<ion-button
data-testid="ion-sidebar__toggle-visibility"
iconType="sandwich"
Expand Down
25 changes: 24 additions & 1 deletion projects/ion/src/lib/sidebar/sidebar.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, Input } from '@angular/core';
import { IonSidebarProps } from '../core/types/sidebar';
import { selectItemByIndex, callItemAction, unselectAllItems } from './utils';
import { callItemAction, selectItemByIndex, unselectAllItems } from './utils';

@Component({
selector: 'ion-sidebar',
Expand All @@ -14,8 +14,31 @@ export class IonSidebarComponent {

public closed = true;

public checkClikOnPageAccess = (event): void => {
this.checkClikOnPage(event);
};

public checkClikOnPage(event): void {
const containerElement = [document.querySelector('.ion-sidebar--opened')];
const innerElement = event.target;
if (containerElement.length && !containerElement.includes(innerElement)) {
const closeButton = document.querySelector(
'.ion-sidebar--opened .ion-sidebar__header button'
) as HTMLElement;
if (closeButton) {
closeButton.click();
}
document.removeEventListener('click', this.checkClikOnPage);
}
}

public toggleVisibility(): void {
this.closed = !this.closed;
if (!this.closed) {
setTimeout(() => {
document.addEventListener('click', this.checkClikOnPageAccess);
wermeson-lopes-brisa marked this conversation as resolved.
Show resolved Hide resolved
});
}
}

public itemSelected(itemIndex: number): void {
Expand Down
29 changes: 28 additions & 1 deletion projects/ion/src/lib/sidebar/sidebar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonModule } from '@angular/common';
import { render, screen } from '@testing-library/angular';
import { render, screen, within } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import { IonButtonModule } from '../button/button.module';
import { IonSidebarProps } from '../core/types/sidebar';
Expand All @@ -12,6 +12,7 @@ const components = {
sidebar: 'ion-sidebar',
group: 'sidebar-group',
toggleVisibility: 'ion-sidebar__toggle-visibility',
outsideContainer: 'ion-sidebar__outside-container',
};

const getByTestId = (key: keyof typeof components): HTMLElement => {
Expand Down Expand Up @@ -80,6 +81,11 @@ describe('Sidebar', () => {
await sut({ items, logo, logoAction: actionMock });
userEvent.click(getByTestId('toggleVisibility').firstElementChild);
});

afterEach(async () => {
jest.clearAllMocks();
});

it('should render sidebar', () => {
expect(getByTestId('sidebar')).toBeInTheDocument();
});
Expand Down Expand Up @@ -205,6 +211,27 @@ describe('Sidebar', () => {
});
});
});
describe('Clicking outside it', () => {
it('should close sidebar after clicking outside it', async () => {
jest.useFakeTimers();
const timeDelay = 300;
const { detectChanges } = await render(IonSidebarComponent, {
declarations: [IonSidebarItemComponent, IonSidebarGroupComponent],
imports: [CommonModule, IonIconModule, IonButtonModule],
});

userEvent.click(getByTestId('toggleVisibility').firstElementChild);
userEvent.click(
within(getByTestId('outsideContainer')).getByTestId(
'ion-sidebar__toggle-visibility'
).firstElementChild
);

jest.advanceTimersByTime(timeDelay);
detectChanges();
expect(getByTestId('sidebar')).not.toHaveClass('ion-sidebar--opened');
});
});
describe('Group without action', () => {
beforeEach(async () => {
items[2].action = undefined;
Expand Down
Loading