Skip to content

Commit a40bb1a

Browse files
committed
fixup! fix(cdk-experimental/listbox): change shift+nav behavior
1 parent fc56f38 commit a40bb1a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/cdk-experimental/ui-patterns/behaviors/event-manager/event-manager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export enum ModifierKey {
4747
Shift = 0b10,
4848
Alt = 0b100,
4949
Meta = 0b1000,
50+
Any = 0b10000,
5051
}
5152

5253
export type ModifierInputs = ModifierKey | ModifierKey[];
@@ -99,5 +100,10 @@ export function getModifiers(event: EventWithModifiers): number {
99100
export function hasModifiers(event: EventWithModifiers, modifiers: ModifierInputs): boolean {
100101
const eventModifiers = getModifiers(event);
101102
const modifiersList = Array.isArray(modifiers) ? modifiers : [modifiers];
103+
104+
if (modifiersList.includes(ModifierKey.Any)) {
105+
return true;
106+
}
107+
102108
return modifiersList.some(modifiers => eventModifiers === modifiers);
103109
}

src/cdk-experimental/ui-patterns/listbox/listbox.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export class ListboxPattern<V> {
7676
/** Whether the listbox selection follows focus. */
7777
followFocus = computed(() => this.inputs.selectionMode() === 'follow');
7878

79+
/** Whether the listbox should wrap. */
80+
wrap = signal(true);
81+
7982
/** The key used to navigate to the previous item in the list. */
8083
prevKey = computed(() => {
8184
if (this.inputs.orientation() === 'vertical') {
@@ -137,22 +140,26 @@ export class ListboxPattern<V> {
137140
// index where the shift key begins being held down. Note that this is very different from
138141
// selecting from the current active or focused index.
139142
manager
140-
.on(Modifier.Shift, 'Shift', () => this.shiftAnchorIndex.set(this.inputs.activeIndex()))
143+
.on(Modifier.Any, 'Shift', () => this.shiftAnchorIndex.set(this.inputs.activeIndex()))
141144
.on(Modifier.Shift, this.prevKey, () => {
142145
if (this.inputs.activeIndex() === this.shiftAnchorIndex()) {
143146
this.selection.select();
144147
} else if (this.inputs.activeIndex() > this.shiftAnchorIndex()) {
145148
this.selection.deselect();
146149
}
150+
this.wrap.set(false);
147151
this.prev({select: true});
152+
this.wrap.set(true);
148153
})
149154
.on(Modifier.Shift, this.nextKey, () => {
150155
if (this.inputs.activeIndex() === this.shiftAnchorIndex()) {
151156
this.selection.select();
152157
} else if (this.inputs.activeIndex() < this.shiftAnchorIndex()) {
153158
this.selection.deselect();
154159
}
160+
this.wrap.set(false);
155161
this.next({select: true});
162+
this.wrap.set(true);
156163
});
157164

158165
manager
@@ -230,7 +237,10 @@ export class ListboxPattern<V> {
230237
this.orientation = inputs.orientation;
231238
this.multi = inputs.multi;
232239

233-
this.navigation = new ListNavigation(inputs);
240+
this.navigation = new ListNavigation({
241+
...inputs,
242+
wrap: computed(() => this.wrap() && this.inputs.wrap()),
243+
});
234244
this.selection = new ListSelection({...inputs, navigation: this.navigation});
235245
this.typeahead = new ListTypeahead({...inputs, navigation: this.navigation});
236246
this.focusManager = new ListFocus({...inputs, navigation: this.navigation});

0 commit comments

Comments
 (0)