Skip to content

Commit b0d684a

Browse files
committed
refactor: enhance drawer registry and sync logic for swipe progress
1 parent cb0c6e3 commit b0d684a

File tree

2 files changed

+76
-40
lines changed

2 files changed

+76
-40
lines changed

packages/machines/drawer/src/drawer.machine.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -603,21 +603,24 @@ export const machine = createMachine<DrawerSchema>({
603603
},
604604

605605
syncDrawerStack({ context, prop, computed }) {
606-
const stack = prop("stack")
607-
if (!stack) return
608-
609606
const contentSize = context.get("contentSize")
610607
if (contentSize === null) return
611608

612609
const dragOffset = context.get("dragOffset")
613610
const snapPointOffset = getActiveSnapOffset(context)
611+
const progress = resolveSwipeProgress(contentSize, dragOffset, snapPointOffset)
612+
const id = computed("drawerId")
614613

615-
stack.setHeight(computed("drawerId"), contentSize)
616-
stack.setSwipe(
617-
computed("drawerId"),
618-
dragOffset !== null,
619-
resolveSwipeProgress(contentSize, dragOffset, snapPointOffset),
620-
)
614+
// Sync to registry for nested drawer metrics
615+
if (dragOffset !== null) {
616+
drawerRegistry.setSwipeProgress(id, progress)
617+
}
618+
619+
const stack = prop("stack")
620+
if (!stack) return
621+
622+
stack.setHeight(id, contentSize)
623+
stack.setSwipe(id, dragOffset !== null, progress)
621624
},
622625
},
623626

@@ -773,8 +776,8 @@ export const machine = createMachine<DrawerSchema>({
773776
contentEl.style.setProperty("--drawer-height", `${myHeight}px`)
774777
contentEl.style.setProperty("--drawer-frontmost-height", `${frontmostHeight}px`)
775778

776-
if (nestedCount > 0) contentEl.setAttribute("data-nested-drawer-open", "")
777-
else contentEl.removeAttribute("data-nested-drawer-open")
779+
if (nestedCount > 0 && frontmostHeight > 0) contentEl.setAttribute("data-nested-drawer-open", "")
780+
else if (nestedCount === 0) contentEl.removeAttribute("data-nested-drawer-open")
778781

779782
if (drawerRegistry.hasSwipingAfter(id)) contentEl.setAttribute("data-nested-drawer-swiping", "")
780783
else contentEl.removeAttribute("data-nested-drawer-swiping")
Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,75 @@
1-
const elements = new Map<string, HTMLElement>()
2-
const swipingIds = new Set<string>()
3-
const listeners = new Set<() => void>()
1+
class DrawerRegistry {
2+
private readonly elements = new Map<string, HTMLElement>()
3+
private readonly swipingIds = new Set<string>()
4+
private readonly swipeProgress = new Map<string, number>()
5+
private readonly listeners = new Set<() => void>()
46

5-
function notify() {
6-
listeners.forEach((fn) => fn())
7-
}
7+
notify() {
8+
this.listeners.forEach((fn) => fn())
9+
}
810

9-
export const drawerRegistry = {
1011
register(id: string, el: HTMLElement) {
11-
elements.set(id, el)
12-
notify()
13-
},
12+
this.elements.set(id, el)
13+
this.notify()
14+
}
15+
1416
unregister(id: string) {
15-
swipingIds.delete(id)
16-
if (!elements.delete(id)) return
17-
notify()
18-
},
17+
this.swipingIds.delete(id)
18+
this.swipeProgress.delete(id)
19+
if (!this.elements.delete(id)) return
20+
this.notify()
21+
}
22+
1923
setSwiping(id: string, swiping: boolean) {
20-
const changed = swiping ? !swipingIds.has(id) : swipingIds.has(id)
21-
if (!changed) return
22-
if (swiping) swipingIds.add(id)
23-
else swipingIds.delete(id)
24-
notify()
25-
},
24+
const changed = swiping ? !this.swipingIds.has(id) : this.swipingIds.has(id)
25+
if (!changed && swiping) return
26+
27+
if (swiping) {
28+
this.swipingIds.add(id)
29+
} else {
30+
this.swipingIds.delete(id)
31+
this.swipeProgress.delete(id)
32+
}
33+
34+
this.notify()
35+
}
36+
37+
setSwipeProgress(id: string, progress: number) {
38+
this.swipeProgress.set(id, progress)
39+
this.notify()
40+
}
41+
42+
getSwipeProgressAfter(id: string): number {
43+
const keys = [...this.elements.keys()]
44+
const myIndex = keys.indexOf(id)
45+
if (myIndex === -1) return 0
46+
47+
for (let i = keys.length - 1; i > myIndex; i -= 1) {
48+
if (this.swipingIds.has(keys[i])) {
49+
return this.swipeProgress.get(keys[i]) ?? 0
50+
}
51+
}
52+
53+
return 0
54+
}
55+
2656
hasSwipingAfter(id: string) {
27-
const keys = [...elements.keys()]
57+
const keys = [...this.elements.keys()]
2858
const myIndex = keys.indexOf(id)
2959
if (myIndex === -1) return false
30-
return keys.slice(myIndex + 1).some((key) => swipingIds.has(key))
31-
},
32-
notify,
60+
return keys.slice(myIndex + 1).some((key) => this.swipingIds.has(key))
61+
}
62+
3363
getEntries() {
34-
return elements
35-
},
64+
return this.elements
65+
}
66+
3667
subscribe(fn: () => void) {
37-
listeners.add(fn)
68+
this.listeners.add(fn)
3869
return () => {
39-
listeners.delete(fn)
70+
this.listeners.delete(fn)
4071
}
41-
},
72+
}
4273
}
74+
75+
export const drawerRegistry = new DrawerRegistry()

0 commit comments

Comments
 (0)