Files
gods-eye-view/src/ui/rail/panelState.js
T
Sameh Khamis 9ed9098593 refactor(panels): read panel state from the panel chrome store
PanelChrome, its collapse-preference restore and the phone shell read a
panel's disclosure state back from the gev-panel-chrome store
(_shownPanel), falling back to the markup's classes before the panel is
first presented, instead of reading the classes v-panel-state draws. The
share link and the phone shell share one rule for a panel the person keeps
closed (prefersCollapsed). The rails, the accordion and the hover
disclosure still read the classes, which the directive draws in the same
task as their layout.
2026-11-01 14:48:31 -08:00

87 lines
2.8 KiB
JavaScript

import { watch } from 'vue';
import { usePanelChromeStore } from '../stores/panelChrome.js';
/** The classes a panel's state draws on its root. */
const CLASSES = Object.freeze({
collapsed: (state) => state.collapsed,
'dock-pinned': (state) => state.pinned,
'dock-pinned-top': (state) => state.pinnedTop,
'layout-auto-collapsed': (state) => state.auto === 'layout',
'accordion-collapsed': (state) => state.auto === 'accordion',
});
/**
* Draw `state` on a panel's root with `classList`, leaving other classes.
* @param {Element} el
* @param {import('../stores/panelChrome.js').PanelState} state
*/
export function applyPanelState(el, state) {
for (const [name, on] of Object.entries(CLASSES))
if (el.classList.contains(name) !== on(state))
el.classList.toggle(name, on(state));
}
/**
* Keep `el` drawing the state `store` holds for `panelId`, synchronously:
* the panel layouts measure a panel in the same task that opens or closes
* it. Until the panel is presented its markup's classes stand.
* @returns {() => void} Stops following the store.
*/
export function followPanelState(store, panelId, el) {
return watch(
() => store.panels[panelId],
(state) => state && applyPanelState(el, state),
{ flush: 'sync', immediate: true },
);
}
/**
* `v-panel-state="'data-panel'"`: the panel root draws its open, pinned and
* layout-collapsed state from the panel chrome store.
*/
export const vPanelState = {
mounted(el, binding) {
const store = usePanelChromeStore(binding.instance.$pinia);
el._panelStateStore = store;
el._stopPanelState = followPanelState(store, binding.value, el);
},
// A re-render that rewrites the root's class (its class binding changed)
// would restore the markup's state; draw the presented state again.
updated(el, binding) {
const state = el._panelStateStore?.panels[binding.value];
if (state) applyPanelState(el, state);
},
beforeUnmount(el) {
el._stopPanelState?.();
},
};
/**
* Whether the person keeps a panel closed, as share links record it: a
* collapse a layout applied only for space still counts as open.
* @param {import('../stores/panelChrome.js').PanelState} state
*/
export function prefersCollapsed(state) {
return state.collapsed && !state.auto;
}
/**
* A panel's state as its classes draw it: the markup's state, before its
* owner first presents the panel.
* @param {Element} el
* @returns {import('../stores/panelChrome.js').PanelState}
*/
export function readPanelState(el) {
const has = (name) => el.classList.contains(name);
return {
collapsed: has('collapsed'),
pinned: has('dock-pinned'),
pinnedTop: has('dock-pinned-top'),
auto: has('layout-auto-collapsed')
? 'layout'
: has('accordion-collapsed')
? 'accordion'
: null,
};
}