|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: MPL-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import Modifier from 'ember-modifier'; |
| 7 | +import type { ArgsFor, PositionalArgs } from 'ember-modifier'; |
| 8 | +import { registerDestructor } from '@ember/destroyable'; |
| 9 | + |
| 10 | +import { |
| 11 | + didInsertGridCell, |
| 12 | + updateTabbableChildren, |
| 13 | +} from './hds-advanced-table-cell/dom-management.ts'; |
| 14 | +import { handleGridCellKeyPress } from './hds-advanced-table-cell/keyboard-navigation.ts'; |
| 15 | + |
| 16 | +export interface HdsAdvancedTableCellModifierSignature { |
| 17 | + Args: { |
| 18 | + Named: { |
| 19 | + handleEnableFocusTrap: () => void; |
| 20 | + shouldTrapFocus: boolean; |
| 21 | + setCellElement: (el: HTMLDivElement) => void; |
| 22 | + }; |
| 23 | + }; |
| 24 | + Element: HTMLDivElement; |
| 25 | +} |
| 26 | + |
| 27 | +function cleanup(instance: HdsAdvancedTableCellModifier): void { |
| 28 | + const { _observer } = instance; |
| 29 | + if (_observer) { |
| 30 | + _observer.disconnect(); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export default class HdsAdvancedTableCellModifier extends Modifier<HdsAdvancedTableCellModifierSignature> { |
| 35 | + // have a copy of shouldTrapFocus internally so the correct value is used when update tabbable children |
| 36 | + private _shouldTrapFocus = false; |
| 37 | + private _didSetup = false; |
| 38 | + _observer: MutationObserver | undefined = undefined; |
| 39 | + |
| 40 | + constructor( |
| 41 | + owner: unknown, |
| 42 | + args: ArgsFor<HdsAdvancedTableCellModifierSignature> |
| 43 | + ) { |
| 44 | + super(owner, args); |
| 45 | + registerDestructor(this, cleanup); |
| 46 | + } |
| 47 | + |
| 48 | + modify( |
| 49 | + element: HTMLDivElement, |
| 50 | + positional: PositionalArgs<HdsAdvancedTableCellModifierSignature>, |
| 51 | + named: HdsAdvancedTableCellModifierSignature['Args']['Named'] |
| 52 | + ): void { |
| 53 | + this._shouldTrapFocus = named.shouldTrapFocus; |
| 54 | + |
| 55 | + if (!this._didSetup) { |
| 56 | + this.#setupObserver(element, positional, named); |
| 57 | + named.setCellElement(element); |
| 58 | + this._didSetup = true; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + #setupObserver( |
| 63 | + element: HdsAdvancedTableCellModifierSignature['Element'], |
| 64 | + _positional: PositionalArgs<HdsAdvancedTableCellModifierSignature>, |
| 65 | + named: HdsAdvancedTableCellModifierSignature['Args']['Named'] |
| 66 | + ): void { |
| 67 | + const { handleEnableFocusTrap } = named; |
| 68 | + |
| 69 | + didInsertGridCell(element); |
| 70 | + element.addEventListener('keydown', (event: KeyboardEvent) => { |
| 71 | + handleGridCellKeyPress(event, handleEnableFocusTrap); |
| 72 | + }); |
| 73 | + |
| 74 | + this._observer = new MutationObserver(() => { |
| 75 | + updateTabbableChildren(element, this._shouldTrapFocus); |
| 76 | + }); |
| 77 | + |
| 78 | + this._observer.observe(element, { |
| 79 | + childList: true, |
| 80 | + subtree: true, |
| 81 | + }); |
| 82 | + } |
| 83 | +} |
0 commit comments