Line data Source code
1 : import { readonly, ref } from 'vue'
2 :
3 : /**
4 : * Collapses the workspace header in the settings dialog once a workspace panel
5 : * is scrolled, so the panel's controls can take the header's row alongside the
6 : * close button. Shared module state: the dialog is a singleton, and the panel
7 : * that owns the scroller is not the component that renders the header.
8 : */
9 2355 : const isHeaderCollapsed = ref(false)
10 :
11 : /**
12 : * Separate thresholds because collapsing feeds back into its own input:
13 : * lifting the controls row out of the panel body grows the scroller's viewport
14 : * and shrinks its overflow, which can clamp scrollTop back down. With a single
15 : * threshold that clamp re-expands the header, which restores the overflow, and
16 : * a marginally-scrollable panel flip-flops.
17 : */
18 2355 : const COLLAPSE_ABOVE_PX = 24
19 2355 : const EXPAND_BELOW_PX = 4
20 :
21 107 : export function useSettingsHeaderCollapse() {
22 2 : function handlePanelScroll(event: Event) {
23 2 : const scroller = event.target
24 2 : if (!(scroller instanceof HTMLElement)) return
25 :
26 2 : const { scrollTop } = scroller
27 2 : if (!isHeaderCollapsed.value && scrollTop > COLLAPSE_ABOVE_PX) {
28 0 : isHeaderCollapsed.value = true
29 2 : } else if (isHeaderCollapsed.value && scrollTop < EXPAND_BELOW_PX) {
30 0 : isHeaderCollapsed.value = false
31 : }
32 : }
33 :
34 92 : function resetHeaderCollapse() {
35 92 : isHeaderCollapsed.value = false
36 : }
37 :
38 107 : return {
39 : isHeaderCollapsed: readonly(isHeaderCollapsed),
40 : handlePanelScroll,
41 : resetHeaderCollapse
42 : }
43 : }
|