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

fix: picker should work in a shadow root #6669

Merged
merged 5 commits into from
Mar 10, 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
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