Skip to content

Commit

Permalink
fix: added Mutation Observer in the tags workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajdeep Chandra authored and Westbrook committed Aug 18, 2023
1 parent f4d7269 commit 3af1861
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
10 changes: 0 additions & 10 deletions packages/tags/src/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ export class Tag extends SizedMixin(SpectrumElement, {
}
};

private shiftFocusOnEvent(): void {
const event = new CustomEvent('sp-tag-focus', {
bubbles: true,
composed: true,
cancelable: true,
});
this.dispatchEvent(event);
}

private delete(): void {
if (this.readonly) {
return;
Expand All @@ -108,7 +99,6 @@ export class Tag extends SizedMixin(SpectrumElement, {
if (deleteEvent.defaultPrevented) {
return;
}
this.shiftFocusOnEvent();
this.remove();
}

Expand Down
32 changes: 11 additions & 21 deletions packages/tags/src/Tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { queryAssignedNodes } from '@spectrum-web-components/base/src/decorators.js';
import { FocusVisiblePolyfillMixin } from '@spectrum-web-components/shared/src/focus-visible.js';
import { RovingTabindexController } from '@spectrum-web-components/reactive-controllers/src/RovingTabindex.js';
import { MutationController } from '@lit-labs/observers/mutation-controller.js';

import { Tag } from './Tag.js';

Expand Down Expand Up @@ -55,6 +56,15 @@ export class Tags extends FocusVisiblePolyfillMixin(SpectrumElement) {

constructor() {
super();
new MutationController(this, {
config: {
childList: true,
subtree: true,
},
callback: () => {
this.rovingTabindexController.changeDefaultItemFocus();
},
});
this.addEventListener('focusin', this.handleFocusin);
}

Expand Down Expand Up @@ -104,29 +114,9 @@ export class Tags extends FocusVisiblePolyfillMixin(SpectrumElement) {
this.rovingTabindexController.clearElementCache();
}

private async changeDefaultFocus(event: Event): Promise<void> {
if (event.defaultPrevented) {
return;
}
// check if tags are not present
if (this.tags && !this.tags.length) {
return;
}
// check if tag item is disabled
this.tags.forEach((item) => {
if (item.disabled) {
return;
}
});
this.rovingTabindexController.changeDefaultItemFocus();
}

protected override render(): TemplateResult {
return html`
<slot
@slotchange=${this.handleSlotchange}
@sp-tag-focus=${this.changeDefaultFocus}
></slot>
<slot @slotchange=${this.handleSlotchange}></slot>
`;
}

Expand Down
1 change: 1 addition & 0 deletions tools/reactive-controllers/src/RovingTabindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class RovingTabindexController<
}

changeDefaultItemFocus(): void {
// console.log(" I am called from RovingTab Index")
const currentIndex = this.currentIndex;
let focusIndex = currentIndex;
if (currentIndex < this.elements.length - 1) {
Expand Down

0 comments on commit 3af1861

Please sign in to comment.