Skip to content

Commit 584b3c7

Browse files
committed
add display order to drawer context
1 parent 7a7ea4e commit 584b3c7

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

svelte/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@invopop/popui",
33
"license": "MIT",
4-
"version": "0.1.44",
4+
"version": "0.1.45",
55
"repository": {
66
"url": "https://github.com/invopop/popui"
77
},

svelte/src/lib/DrawerContext.svelte

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
getNextFocusedIndex,
3434
selectFocusedItem
3535
} from './drawer-keyboard-helpers'
36-
import { isInputFocused } from './helpers'
3736
3837
const flipDurationMs = 150
3938
@@ -76,6 +75,27 @@
7675
return { groupedItems: grouped, ungroupedItems: ungrouped }
7776
})
7877
78+
// Items in display order (matches visual rendering order)
79+
let itemsInDisplayOrder = $derived.by(() => {
80+
const displayOrder: DrawerOption[] = []
81+
82+
if (hasGroups && groups) {
83+
// Add grouped items in group order, only if group is open (when collapsible)
84+
groups.forEach((group) => {
85+
const isOpen = collapsibleGroups ? openGroups[group.slug] : true
86+
if (isOpen) {
87+
const groupItems = groupedItems.get(group.slug) || []
88+
displayOrder.push(...groupItems)
89+
}
90+
})
91+
}
92+
93+
// Add ungrouped items
94+
displayOrder.push(...ungroupedItems)
95+
96+
return displayOrder
97+
})
98+
7999
let openGroups = $state<Record<string, boolean>>({})
80100
let groupDndItems = $state<Record<string, DndItem[]>>({})
81101
let ungroupedDndItems = $state<DndItem[]>([])
@@ -413,18 +433,18 @@
413433
}
414434
415435
let focusedItemValue = $derived.by(() => {
416-
const focusableItems = getFocusableItems(items)
436+
const focusableItems = getFocusableItems(itemsInDisplayOrder)
417437
if (focusedIndex >= 0 && focusedIndex < focusableItems.length) {
418438
return focusableItems[focusedIndex].value
419439
}
420440
return null
421441
})
422442
423443
function handleKeyDown(event: KeyboardEvent) {
424-
// Don't handle if any input is focused or container doesn't exist
425-
if (isInputFocused() || !containerRef || !document.body.contains(containerRef)) return
444+
// Don't handle if container doesn't exist
445+
if (!containerRef || !document.body.contains(containerRef)) return
426446
427-
const focusableItems = getFocusableItems(items)
447+
const focusableItems = getFocusableItems(itemsInDisplayOrder)
428448
if (focusableItems.length === 0) return
429449
430450
if (event.key === 'ArrowDown') {
@@ -435,7 +455,7 @@
435455
focusedIndex = getNextFocusedIndex(focusedIndex, 'up', focusableItems.length)
436456
} else if (event.key === ' ' || event.key === 'Enter') {
437457
event.preventDefault()
438-
const result = selectFocusedItem(items, focusedIndex, multiple)
458+
const result = selectFocusedItem(itemsInDisplayOrder, focusedIndex, multiple)
439459
if (result) {
440460
if (result.shouldUpdate) {
441461
updateItem(result.item)
@@ -484,7 +504,7 @@
484504
>
485505
{#if collapsibleGroups}
486506
<button
487-
class="cursor-pointer flex items-center justify-between h-8 pl-2.5 pr-2.5 py-2.5 text-base font-medium text-foreground-default-secondary w-full hover:bg-background-default-secondary rounded-lg overflow-clip flex-shrink-0"
507+
class="cursor-pointer flex items-center justify-between h-8 pl-2.5 pr-2.5 py-2.5 text-base font-medium text-foreground-default-secondary w-full hover:bg-background-default-secondary rounded-lg overflow-clip flex-shrink-0 outline-none"
488508
onclick={() => toggleGroup(group.slug)}
489509
>
490510
<div class="flex items-center gap-1.5">

svelte/src/stories/DrawerContext.stories.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,12 @@ export const NonCollapsibleGroups: Story = {
505505
],
506506
items: [
507507
{ label: 'Cobee S.L.', value: 'cobee', groupBy: 'live' },
508-
{ label: 'Payflow S.L.', value: 'payflow', groupBy: 'live', selected: true },
509-
{ label: 'Company A', value: 'companya', groupBy: 'live' },
510-
{ label: 'Company B', value: 'companyb', groupBy: 'live' },
511508
{ label: 'Test Company', value: 'test', groupBy: 'sandbox' },
509+
{ label: 'Payflow S.L.', value: 'payflow', groupBy: 'live', selected: true },
512510
{ label: 'Dev Company', value: 'dev', groupBy: 'sandbox' },
511+
{ label: 'Company A', value: 'companya', groupBy: 'live' },
513512
{ label: 'Staging Company', value: 'staging', groupBy: 'sandbox' },
513+
{ label: 'Company B', value: 'companyb', groupBy: 'live' },
514514
{ separator: true, value: 'sep-1' } as any,
515515
{ label: 'Create workspace', value: 'add', icon: Close }
516516
]

0 commit comments

Comments
 (0)