Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Fixed initial focus to ignore hidden elements #12

Merged
merged 4 commits into from
Nov 28, 2018
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
<sky-modal>
<sky-modal-header>Test</sky-modal-header>
<sky-modal-header>
Test
</sky-modal-header>
<sky-modal-content>
<button>Content</button>
<button
style="visibility: hidden;"
>
Hidden
</button>
<button
blackbaud-conorwright marked this conversation as resolved.
Show resolved Hide resolved
style="display: none;"
>
No display
</button>
<button
id="visible-btn"
>
Content
</button>
</sky-modal-content>
</sky-modal>
13 changes: 11 additions & 2 deletions src/app/public/modules/modal/modal-component-adapter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,17 @@ export class SkyModalComponentAdapterService {
}

private isVisible(element: HTMLElement) {
return !!(element.offsetWidth ||
const style = window.getComputedStyle(element);
const isHidden = style.display === 'none' || style.visibility === 'hidden';
if (isHidden) {
return false;
}

const hasBounds = !!(
blackbaud-conorwright marked this conversation as resolved.
Show resolved Hide resolved
element.offsetWidth ||
element.offsetHeight ||
element.getClientRects().length);
element.getClientRects().length
);
return hasBounds;
}
}
2 changes: 1 addition & 1 deletion src/app/public/modules/modal/modal.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('Modal component', () => {

it('should focus the first focusable element when no autofocus is inside of content', fakeAsync(() => {
let modalInstance1 = openModal(ModalWithFocusContentTestComponent);
expect(document.activeElement).toEqual(document.querySelector('.sky-modal-content button'));
expect(document.activeElement).toEqual(document.querySelector('#visible-btn'));
closeModal(modalInstance1);
}));

Expand Down