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

Commit

Permalink
Fixed modal component to ignore hidden elements when initially assign…
Browse files Browse the repository at this point in the history
…ing focus. (#12)
  • Loading branch information
blackbaud-conorwright authored and Blackbaud-SteveBrush committed Nov 28, 2018
1 parent 62048c6 commit 5c667b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
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
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 = !!(
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

0 comments on commit 5c667b0

Please sign in to comment.