Skip to content

Commit

Permalink
fix: picker should work in a shadow root (#6669)
Browse files Browse the repository at this point in the history
* get right active element for root

* Change files

* prettier

* Update change/@microsoft-fast-foundation-9ba63b54-21bc-47e3-a176-545c43124446.json

Co-authored-by: Chris Holt <[email protected]>

---------

Co-authored-by: Chris Holt <[email protected]>
  • Loading branch information
2 people authored and janechu committed Jun 10, 2024
1 parent 9105a50 commit f124e58
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "picker should work in shadow dom",
"packageName": "@microsoft/fast-foundation",
"email": "[email protected]",
"dependentChangeType": "prerelease"
}
28 changes: 20 additions & 8 deletions packages/web-components/fast-foundation/src/picker/picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ export class FASTPicker extends FormAssociatedPicker {
return;
}

if (open && document.activeElement === this.inputElement) {
if (open && this.getRootActiveElement() === this.inputElement) {
this.flyoutOpen = open;
Updates.enqueue(() => {
if (this.menuElement !== undefined) {
Expand Down Expand Up @@ -603,6 +603,7 @@ export class FASTPicker extends FormAssociatedPicker {
if (e.defaultPrevented) {
return false;
}
const activeElement = this.getRootActiveElement();
switch (e.key) {
// TODO: what should "home" and "end" keys do, exactly?
//
Expand Down Expand Up @@ -672,7 +673,7 @@ export class FASTPicker extends FormAssociatedPicker {
}

case keyArrowRight: {
if (document.activeElement !== this.inputElement) {
if (activeElement !== this.inputElement) {
this.incrementFocusedItem(1);
return false;
}
Expand All @@ -691,11 +692,11 @@ export class FASTPicker extends FormAssociatedPicker {

case keyDelete:
case keyBackspace: {
if (document.activeElement === null) {
if (activeElement === null) {
return true;
}

if (document.activeElement === this.inputElement) {
if (activeElement === this.inputElement) {
if (this.inputElement.selectionStart === 0) {
this.selection = this.selectedItems
.slice(0, this.selectedItems.length - 1)
Expand All @@ -709,7 +710,7 @@ export class FASTPicker extends FormAssociatedPicker {

const selectedItems: Element[] = Array.from(this.listElement.children);
const currentFocusedItemIndex: number = selectedItems.indexOf(
document.activeElement
activeElement
);

if (currentFocusedItemIndex > -1) {
Expand Down Expand Up @@ -806,7 +807,7 @@ export class FASTPicker extends FormAssociatedPicker {
this.maxSelected !== undefined &&
this.selectedItems.length >= this.maxSelected
) {
if (document.activeElement === this.inputElement) {
if (this.getRootActiveElement() === this.inputElement) {
const selectedItemInstances: Element[] = Array.from(
this.listElement.querySelectorAll("[role='listitem']")
);
Expand All @@ -820,6 +821,16 @@ export class FASTPicker extends FormAssociatedPicker {
}
}

private getRootActiveElement(): Element | null {
const rootNode = this.getRootNode();

if (rootNode instanceof ShadowRoot) {
return rootNode.activeElement;
}

return document.activeElement;
}

/**
* A list item has been invoked.
*/
Expand Down Expand Up @@ -882,9 +893,10 @@ export class FASTPicker extends FormAssociatedPicker {
this.listElement.querySelectorAll("[role='listitem']")
);

if (document.activeElement !== null) {
const activeElement = this.getRootActiveElement();
if (activeElement !== null) {
let currentFocusedItemIndex: number = selectedItemsAsElements.indexOf(
document.activeElement
activeElement
);
if (currentFocusedItemIndex === -1) {
// use the input element
Expand Down

0 comments on commit f124e58

Please sign in to comment.