feat: 优化店铺和公司侧边栏

This commit is contained in:
2026-08-18 21:26:54 +08:00
parent 24ef58a217
commit 9dd4977e09
14 changed files with 414 additions and 376 deletions
@@ -1,7 +1,7 @@
<script lang="ts" setup>
import type { ColPageProps } from './types';
import { computed, ref, useSlots } from 'vue';
import { computed, nextTick, onMounted, ref, unref, useSlots, watch } from 'vue';
import {
ResizableHandle,
@@ -18,12 +18,34 @@ defineOptions({
const props = withDefaults(defineProps<ColPageProps>(), {
leftWidth: 30,
leftCollapsed: false,
rightWidth: 70,
resizable: true,
});
const emit = defineEmits<{
'update:leftWidth': [width: number];
'update:leftCollapsed': [collapsed: boolean];
}>();
const delegatedProps = computed(() => {
const { leftWidth: _, ...delegated } = props;
const {
leftWidth: _leftWidth,
leftMinWidth: _leftMinWidth,
leftMaxWidth: _leftMaxWidth,
leftCollapsedWidth: _leftCollapsedWidth,
leftCollapsible: _leftCollapsible,
leftCollapsed: _leftCollapsed,
rightWidth: _rightWidth,
rightMinWidth: _rightMinWidth,
rightMaxWidth: _rightMaxWidth,
rightCollapsedWidth: _rightCollapsedWidth,
rightCollapsible: _rightCollapsible,
resizable: _resizable,
splitLine: _splitLine,
splitHandle: _splitHandle,
...delegated
} = props;
return delegated;
});
@@ -41,6 +63,32 @@ const delegatedSlots = computed(() => {
});
const leftPanelRef = ref<InstanceType<typeof ResizablePanel>>();
/** 自身操作引起的回写,避免与外部 v-model 互相触发 */
const suppressExternalSync = ref(false);
const leftDefaultSize = computed(() =>
props.leftCollapsed
? (props.leftCollapsedWidth ?? 0)
: props.leftWidth,
);
const rightDefaultSize = computed(() =>
props.leftCollapsed
? 100 - (props.leftCollapsedWidth ?? 0)
: props.rightWidth,
);
function isLeftCollapsed() {
return !!unref(leftPanelRef.value?.isCollapsed);
}
function runAsSelfApply(apply: () => void) {
suppressExternalSync.value = true;
apply();
void nextTick(() => {
suppressExternalSync.value = false;
});
}
function expandLeft() {
leftPanelRef.value?.expand();
@@ -50,6 +98,56 @@ function collapseLeft() {
leftPanelRef.value?.collapse();
}
function applyCollapsed(collapsed: boolean) {
const panel = leftPanelRef.value;
if (!panel) return;
if (collapsed) {
panel.collapse();
return;
}
panel.expand();
void nextTick(() => panel.resize(props.leftWidth));
}
function onLeftCollapse() {
runAsSelfApply(() => emit('update:leftCollapsed', true));
}
function onLeftExpand() {
runAsSelfApply(() => emit('update:leftCollapsed', false));
}
/** 收起过程中的尺寸不写入展开宽度 */
function onLeftResize(size: number) {
const collapsedSize = props.leftCollapsedWidth ?? 0;
if (isLeftCollapsed() || size <= collapsedSize + 0.51) return;
if (Math.abs(size - props.leftWidth) < 0.15) return;
runAsSelfApply(() => emit('update:leftWidth', size));
}
watch(
() => props.leftCollapsed,
(collapsed) => {
if (suppressExternalSync.value) return;
applyCollapsed(collapsed);
},
);
watch(
() => props.leftWidth,
(width) => {
if (suppressExternalSync.value || isLeftCollapsed()) return;
const current = leftPanelRef.value?.getSize();
if (current == null || Math.abs(current - width) < 0.3) return;
leftPanelRef.value?.resize(width);
},
);
onMounted(() => {
if (!props.leftCollapsed) return;
void nextTick(() => applyCollapsed(true));
});
defineExpose({
expandLeft,
collapseLeft,
@@ -68,12 +166,16 @@ defineExpose({
<ResizablePanelGroup class="w-full" direction="horizontal">
<ResizablePanel
id="col-page-left"
ref="leftPanelRef"
:collapsed-size="leftCollapsedWidth"
:collapsible="leftCollapsible"
:default-size="leftWidth"
:default-size="leftDefaultSize"
:max-size="leftMaxWidth"
:min-size="leftMinWidth"
@collapse="onLeftCollapse"
@expand="onLeftExpand"
@resize="onLeftResize"
>
<template #default="slotProps">
<slot
@@ -92,9 +194,10 @@ defineExpose({
:with-handle="splitHandle"
/>
<ResizablePanel
id="col-page-right"
:collapsed-size="rightCollapsedWidth"
:collapsible="rightCollapsible"
:default-size="rightWidth"
:default-size="rightDefaultSize"
:max-size="rightMaxWidth"
:min-size="rightMinWidth"
>
@@ -2,7 +2,7 @@ import type { PageProps } from '../page/types';
export interface ColPageProps extends PageProps {
/**
* 左侧宽度
* 左侧宽度(百分比 1-100)。配合 `update:leftWidth` 可做受控/持久化。
* @default 30
*/
leftWidth?: number;
@@ -10,6 +10,10 @@ export interface ColPageProps extends PageProps {
leftMaxWidth?: number;
leftCollapsedWidth?: number;
leftCollapsible?: boolean;
/**
* 左侧是否收起。配合 `update:leftCollapsed` 可做受控/持久化。
*/
leftCollapsed?: boolean;
/**
* 右侧宽度
* @default 70