Skip to content

Commit

Permalink
FIO-7523 Fixed dropdown being inaccessible in flipped state
Browse files Browse the repository at this point in the history
  • Loading branch information
antonSoftensity committed Dec 19, 2023
1 parent d151928 commit 5d5e628
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ class Choices implements Choices {

requestAnimationFrame(() => {
this.dropdown.show();
this.containerOuter.open(this.dropdown.distanceFromTopWindow);
this.containerOuter.open(this.dropdown.distanceFromTopWindow, this.dropdown.height);

if (!preventInputFocus && this._canSearch) {
this.input.focus();
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/components/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('components/container', () => {
});

it('returns true', () => {
expect(instance.shouldFlip(100)).to.equal(true);
expect(instance.shouldFlip(100, 100, element)).to.equal(true);
});
});

Expand All @@ -120,7 +120,7 @@ describe('components/container', () => {
});

it('returns false', () => {
expect(instance.shouldFlip(100)).to.equal(false);
expect(instance.shouldFlip(100, 100, element)).to.equal(false);
});
});
});
Expand Down
11 changes: 8 additions & 3 deletions src/scripts/components/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class Container {
* Determine whether container should be flipped based on passed
* dropdown position
*/
shouldFlip(dropdownPos: number): boolean {
shouldFlip(dropdownPos: number, dropdownHeight: number, containerElem: HTMLElement): boolean {
if (typeof dropdownPos !== 'number') {
return false;
}
Expand All @@ -72,6 +72,11 @@ export default class Container {
if (this.position === 'auto') {
shouldFlip = !window.matchMedia(`(min-height: ${dropdownPos + 1}px)`)
.matches;
if (shouldFlip) {
if (containerElem.getBoundingClientRect().top - dropdownHeight < 0) {
shouldFlip = false;
}
}
} else if (this.position === 'top') {
shouldFlip = true;
}
Expand All @@ -87,12 +92,12 @@ export default class Container {
this.element.removeAttribute('aria-activedescendant');
}

open(dropdownPos: number): void {
open(dropdownPos: number, dropdownHeight: number): void {
this.element.classList.add(this.classNames.openState);
this.element.setAttribute('aria-expanded', 'true');
this.isOpen = true;

if (this.shouldFlip(dropdownPos)) {
if (this.shouldFlip(dropdownPos, dropdownHeight, this.element)) {
this.element.classList.add(this.classNames.flippedState);
this.isFlipped = true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/components/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default class Dropdown {
return this.element.getBoundingClientRect().bottom;
}

get height(): number {
return this.element.getBoundingClientRect().height;
}

getChild(selector: string): HTMLElement | null {
return this.element.querySelector(selector);
}
Expand Down

0 comments on commit 5d5e628

Please sign in to comment.