Skip to content

Commit

Permalink
fix(cdk/tree): add injectable key manager and opt-out
Browse files Browse the repository at this point in the history
Make backwards compatibility improvements to cdk-tree-revamp regarding
focus management, the key manager and tabindex attribute.

 * Add TreeKeyMangerStrategy interface
 * Add injection toekn for tree key manager
 * Add LegacyTreeKeyManager

Provide LegacyTreeKeyManager to use legacy tabindex behavior from before
TreeKeyManager was introducted.

This commit message will be squashed away.
  • Loading branch information
zarend committed Oct 27, 2023
1 parent 732665d commit 4e8bfd9
Show file tree
Hide file tree
Showing 19 changed files with 943 additions and 265 deletions.
91 changes: 91 additions & 0 deletions src/cdk/a11y/key-manager/legacy-tree-key-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Subject} from 'rxjs';
import {
TREE_KEY_MANAGER,
TreeKeyManagerFactory,
TreeKeyManagerItem,
TreeKeyManagerStrategy,
} from './tree-key-manager';

/**
* @docs-private
*
* @deprecated LegacyTreeKeyManager deprecated. Use TreeKeyManager or inject a
* TreeKeyManagerStrategy instead. To be removed in a future version.
*
* @breaking-change 19.0.0
*/
// LegacyTreeKeyManager is a "noop" implementation of TreeKeyMangerStrategy. Methods are noops. Does
// not emit to streams.
//
// Used for applications built before TreeKeyManager to opt-out of TreeKeyManager and revert to
// legacy behavior.
export class LegacyTreeKeyManager<T extends TreeKeyManagerItem>
implements TreeKeyManagerStrategy<T>
{
get _isLegacyTreeKeyManager() {
return true;
}

// Provide change as required by TreeKeyManagerStrategy. LegacyTreeKeyManager is a "noop"
// implementation that does not emit to streams.
readonly change = new Subject<T | null>();

onKeydown() {
// noop
}

getActiveItemIndex() {
// Always return null. LegacyTreeKeyManager is a "noop" implementation that does not maintain
// the active item.
return null;
}

getActiveItem() {
// Always return null. LegacyTreeKeyManager is a "noop" implementation that does not maintain
// the active item.
return null;
}

onInitialFocus() {
// noop
}

focusItem() {
// noop
}
}

/**
* @docs-private
*
* @deprecated LegacyTreeKeyManager deprecated. Use TreeKeyManager or inject a
* TreeKeyManagerStrategy instead. To be removed in a future version.
*
* @breaking-change 19.0.0
*/
export function LEGACY_TREE_KEY_MANAGER_FACTORY<
T extends TreeKeyManagerItem,
>(): TreeKeyManagerFactory<T> {
return () => new LegacyTreeKeyManager<T>();
}

/**
* @docs-private
*
* @deprecated LegacyTreeKeyManager deprecated. Use TreeKeyManager or inject a
* TreeKeyManagerStrategy instead. To be removed in a future version.
*
* @breaking-change 19.0.0
*/
export const LEGACY_TREE_KEY_MANAGER_FACTORY_PROVIDER = {
provide: TREE_KEY_MANAGER,
useFactory: LEGACY_TREE_KEY_MANAGER_FACTORY,
};
Loading

0 comments on commit 4e8bfd9

Please sign in to comment.