@ysgroup/ui 0.1.60 → 0.1.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -6
- package/src/components/YsAppToolbar.vue +18 -9
- package/src/components/YsCalendar.vue +176 -15
- package/src/components/YsCalendarDayCard.vue +53 -4
- package/src/components/YsCardList.vue +247 -0
- package/src/components/YsCrudTable.vue +25 -1
- package/src/components/YsFilePreview.vue +3 -2
- package/src/components/YsPageHeader.vue +9 -3
- package/src/components/YsPageLayout.vue +237 -27
- package/src/components/YsRowActionMenu.vue +13 -0
- package/src/components/YsSettingsCenterDialog.vue +2 -1
- package/src/components/YsSideMenu.vue +13 -0
- package/src/components/YsTablePagination.vue +12 -1
- package/src/components/YsTableToolbar.vue +46 -1
- package/src/index.ts +12 -0
- package/src/viewport.ts +52 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
<!-- YsCardList —— 手机上的列表:每条记录一张卡片,代替横向拖不完的表格。
|
|
2
|
+
和表格用同一份数据(当前页的行)、同一套三态和同一个齿轮菜单,页面写法:
|
|
3
|
+
<YsCardList v-if="mobile" :rows="paged" :fields="cardFields" :title="(row) => row.name" :actions="rowActions"
|
|
4
|
+
:loading="loading" :empty-text="emptyText" :index="rowIndex" @action="handleAction" />
|
|
5
|
+
<el-table v-else …>
|
|
6
|
+
- 卡片头:序号(传 index 才有)、标题(一定要有,通常是名称列)、#tags 插槽放状态标签(已归档、已删除)、右上角齿轮菜单。
|
|
7
|
+
- 卡片体:fields 两列排开,标签在上、值在下;wide 的字段独占一行(备注、地址这类长文字);
|
|
8
|
+
值为 null、undefined、空串、空数组时显示「—」,0 照常显示 0;align: "right" 的数值用等宽数字(卡片里不右对齐,标签和值上下排)。
|
|
9
|
+
- #field 插槽可以改某个字段的显示(如 YsBoolMark、链接),参数 { row, field, text }。
|
|
10
|
+
- loading 时转圈(保留旧卡片),没有行时显示 emptyText;读取失败由页面上方的 YsLoadError 负责,和表格一样。
|
|
11
|
+
- 长标题、长值自动换行,不截断(手机上没有悬停,截断了就看不到全文)。
|
|
12
|
+
- 监听了 row-click 时整张卡片可点(如点消息标为已读);点卡片里的按钮、链接、齿轮不触发。 -->
|
|
13
|
+
<script setup lang="ts" generic="T extends object">
|
|
14
|
+
import { computed } from "vue";
|
|
15
|
+
import YsRowActionMenu, { type YsRowAction } from "./YsRowActionMenu.vue";
|
|
16
|
+
import { ysMultiText } from "../multiValue";
|
|
17
|
+
|
|
18
|
+
export interface YsCardField<R = Record<string, unknown>> {
|
|
19
|
+
key: string;
|
|
20
|
+
label: string;
|
|
21
|
+
/** 取值;不传时读 row[key]。数组按「、」连起来。 */
|
|
22
|
+
value?: (row: R) => unknown;
|
|
23
|
+
/** 数值列:表格里右对齐,卡片里用等宽数字。 */
|
|
24
|
+
align?: "left" | "right";
|
|
25
|
+
/** 独占一行(长文字)。 */
|
|
26
|
+
wide?: boolean;
|
|
27
|
+
/** 返回 true 时这张卡片不显示该字段(如学生没有班级时不占位)。 */
|
|
28
|
+
hidden?: (row: R) => boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const props = withDefaults(
|
|
32
|
+
defineProps<{
|
|
33
|
+
rows: readonly T[];
|
|
34
|
+
fields: readonly YsCardField<T>[];
|
|
35
|
+
/** 卡片标题,通常是名称列。 */
|
|
36
|
+
title: (row: T) => unknown;
|
|
37
|
+
rowKey?: string;
|
|
38
|
+
/** 返回这一行的序号(和表格序号列一致,翻页后接着数);不传不显示序号。 */
|
|
39
|
+
index?: (position: number) => number;
|
|
40
|
+
/** 这一行的操作;返回空数组时不显示齿轮。 */
|
|
41
|
+
actions?: (row: T) => YsRowAction[];
|
|
42
|
+
loading?: boolean;
|
|
43
|
+
emptyText?: string;
|
|
44
|
+
/**
|
|
45
|
+
* 点整张卡片(@row-click)。写成属性而不是 emits:组件要知道调用方有没有监听,没监听的卡片不画成可点的样子
|
|
46
|
+
* (声明在 emits 里的监听器不会出现在 attrs 里,判断不出来)。
|
|
47
|
+
*/
|
|
48
|
+
onRowClick?: (row: T) => void;
|
|
49
|
+
}>(),
|
|
50
|
+
{ rowKey: "_id", index: undefined, actions: undefined, loading: false, emptyText: "暂无数据", onRowClick: undefined },
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const emit = defineEmits<{ action: [key: string, row: T] }>();
|
|
54
|
+
/** 只有监听了 row-click 才把卡片画成可点的样子。 */
|
|
55
|
+
const clickable = computed(() => typeof props.onRowClick === "function");
|
|
56
|
+
|
|
57
|
+
function onCardClick(event: MouseEvent, row: T) {
|
|
58
|
+
if ((event.target as HTMLElement | null)?.closest("button, a, input, textarea, [role='menuitem'], .el-popper")) return;
|
|
59
|
+
props.onRowClick?.(row);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
defineSlots<{
|
|
63
|
+
tags?: (props: { row: T }) => unknown;
|
|
64
|
+
field?: (props: { row: T; field: YsCardField<T>; text: string }) => unknown;
|
|
65
|
+
footer?: (props: { row: T }) => unknown;
|
|
66
|
+
/** 齿轮菜单里额外的项,和 YsRowActionMenu 的 #extra 一样。 */
|
|
67
|
+
"menu-extra"?: (props: { row: T; close: () => void }) => unknown;
|
|
68
|
+
}>();
|
|
69
|
+
|
|
70
|
+
function isBlank(value: unknown): boolean {
|
|
71
|
+
return value === null || value === undefined || value === "" || (Array.isArray(value) && value.length === 0);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 显示文字:空值「—」,0 显示 0,数组用「、」连起来,数字带千分位由调用方在 value 里决定。 */
|
|
75
|
+
function textOf(row: T, field: YsCardField<T>): string {
|
|
76
|
+
const raw = field.value ? field.value(row) : (row as Record<string, unknown>)[field.key];
|
|
77
|
+
if (isBlank(raw)) return "—";
|
|
78
|
+
if (Array.isArray(raw)) return ysMultiText(raw);
|
|
79
|
+
return String(raw);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const cards = computed(() =>
|
|
83
|
+
props.rows.map((row, position) => ({
|
|
84
|
+
row,
|
|
85
|
+
key: String((row as Record<string, unknown>)[props.rowKey] ?? position),
|
|
86
|
+
index: props.index?.(position),
|
|
87
|
+
title: String(props.title(row) ?? "") || "—",
|
|
88
|
+
actions: props.actions?.(row) ?? [],
|
|
89
|
+
fields: props.fields.filter((field) => !field.hidden?.(row)).map((field) => ({ field, text: textOf(row, field) })),
|
|
90
|
+
})),
|
|
91
|
+
);
|
|
92
|
+
</script>
|
|
93
|
+
|
|
94
|
+
<template>
|
|
95
|
+
<div v-loading="loading" class="ys-card-list" :class="{ 'is-empty': !cards.length }" role="list">
|
|
96
|
+
<p v-if="!cards.length" class="ys-card-list__empty">{{ loading ? "" : emptyText }}</p>
|
|
97
|
+
<article
|
|
98
|
+
v-for="card in cards"
|
|
99
|
+
:key="card.key"
|
|
100
|
+
class="ys-card-list__card"
|
|
101
|
+
:class="{ 'is-clickable': clickable }"
|
|
102
|
+
role="listitem"
|
|
103
|
+
@click="clickable && onCardClick($event, card.row)"
|
|
104
|
+
>
|
|
105
|
+
<header class="ys-card-list__head">
|
|
106
|
+
<span v-if="card.index !== undefined" class="ys-card-list__index">{{ card.index }}</span>
|
|
107
|
+
<div class="ys-card-list__title-wrap">
|
|
108
|
+
<h3 class="ys-card-list__title">{{ card.title }}</h3>
|
|
109
|
+
<span v-if="$slots.tags" class="ys-card-list__tags"><slot name="tags" :row="card.row" /></span>
|
|
110
|
+
</div>
|
|
111
|
+
<YsRowActionMenu
|
|
112
|
+
v-if="card.actions.length || $slots['menu-extra']"
|
|
113
|
+
class="ys-card-list__menu"
|
|
114
|
+
:actions="card.actions"
|
|
115
|
+
:aria-label="`${card.title} 的操作`"
|
|
116
|
+
@action="emit('action', $event, card.row)"
|
|
117
|
+
>
|
|
118
|
+
<template v-if="$slots['menu-extra']" #extra="{ close }"><slot name="menu-extra" :row="card.row" :close="close" /></template>
|
|
119
|
+
</YsRowActionMenu>
|
|
120
|
+
</header>
|
|
121
|
+
<dl v-if="card.fields.length" class="ys-card-list__fields">
|
|
122
|
+
<div v-for="item in card.fields" :key="item.field.key" class="ys-card-list__field" :class="{ 'is-wide': item.field.wide, 'is-right': item.field.align === 'right' }">
|
|
123
|
+
<dt>{{ item.field.label }}</dt>
|
|
124
|
+
<dd>
|
|
125
|
+
<slot name="field" :row="card.row" :field="item.field" :text="item.text">{{ item.text }}</slot>
|
|
126
|
+
</dd>
|
|
127
|
+
</div>
|
|
128
|
+
</dl>
|
|
129
|
+
<footer v-if="$slots.footer" class="ys-card-list__footer"><slot name="footer" :row="card.row" /></footer>
|
|
130
|
+
</article>
|
|
131
|
+
</div>
|
|
132
|
+
</template>
|
|
133
|
+
|
|
134
|
+
<style scoped>
|
|
135
|
+
.ys-card-list {
|
|
136
|
+
display: grid;
|
|
137
|
+
gap: 10px;
|
|
138
|
+
min-height: 64px;
|
|
139
|
+
}
|
|
140
|
+
.ys-card-list__empty {
|
|
141
|
+
margin: 0;
|
|
142
|
+
padding: 28px 12px;
|
|
143
|
+
border: 1px dashed var(--ys-color-border);
|
|
144
|
+
border-radius: var(--ys-radius-l);
|
|
145
|
+
color: var(--ys-color-text-3);
|
|
146
|
+
font-size: 13px;
|
|
147
|
+
line-height: 1.6;
|
|
148
|
+
text-align: center;
|
|
149
|
+
}
|
|
150
|
+
.ys-card-list.is-empty .ys-card-list__empty:empty {
|
|
151
|
+
border: 0;
|
|
152
|
+
}
|
|
153
|
+
.ys-card-list__card {
|
|
154
|
+
min-width: 0;
|
|
155
|
+
padding: 12px 12px 12px 14px;
|
|
156
|
+
border: 1px solid var(--ys-color-border);
|
|
157
|
+
border-radius: var(--ys-radius-l);
|
|
158
|
+
background: var(--ys-color-bg-elevated);
|
|
159
|
+
}
|
|
160
|
+
.ys-card-list__card.is-clickable {
|
|
161
|
+
cursor: pointer;
|
|
162
|
+
}
|
|
163
|
+
.ys-card-list__card.is-clickable:active {
|
|
164
|
+
background: var(--ys-color-bg-hover);
|
|
165
|
+
}
|
|
166
|
+
.ys-card-list__head {
|
|
167
|
+
display: flex;
|
|
168
|
+
align-items: flex-start;
|
|
169
|
+
gap: 8px;
|
|
170
|
+
}
|
|
171
|
+
.ys-card-list__index {
|
|
172
|
+
flex: none;
|
|
173
|
+
min-width: 22px;
|
|
174
|
+
height: 22px;
|
|
175
|
+
margin-top: 1px;
|
|
176
|
+
padding: 0 5px;
|
|
177
|
+
box-sizing: border-box;
|
|
178
|
+
border-radius: 11px;
|
|
179
|
+
background: var(--ys-color-bg-hover);
|
|
180
|
+
color: var(--ys-color-text-3);
|
|
181
|
+
font-size: 12px;
|
|
182
|
+
font-variant-numeric: tabular-nums;
|
|
183
|
+
line-height: 22px;
|
|
184
|
+
text-align: center;
|
|
185
|
+
}
|
|
186
|
+
.ys-card-list__title-wrap {
|
|
187
|
+
display: flex;
|
|
188
|
+
flex: 1;
|
|
189
|
+
flex-wrap: wrap;
|
|
190
|
+
align-items: center;
|
|
191
|
+
gap: 4px 6px;
|
|
192
|
+
min-width: 0;
|
|
193
|
+
}
|
|
194
|
+
.ys-card-list__title {
|
|
195
|
+
min-width: 0;
|
|
196
|
+
margin: 0;
|
|
197
|
+
color: var(--ys-color-text-1);
|
|
198
|
+
font-size: 15px;
|
|
199
|
+
font-weight: 600;
|
|
200
|
+
line-height: 24px;
|
|
201
|
+
overflow-wrap: anywhere;
|
|
202
|
+
}
|
|
203
|
+
.ys-card-list__tags {
|
|
204
|
+
display: inline-flex;
|
|
205
|
+
flex-wrap: wrap;
|
|
206
|
+
gap: 4px;
|
|
207
|
+
}
|
|
208
|
+
.ys-card-list__menu {
|
|
209
|
+
flex: none;
|
|
210
|
+
/* 齿轮按钮(触屏 40px)和标题第一行(24px)垂直居中对齐,右边贴近卡片边。 */
|
|
211
|
+
margin: -8px -8px -8px 0;
|
|
212
|
+
}
|
|
213
|
+
.ys-card-list__fields {
|
|
214
|
+
display: grid;
|
|
215
|
+
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
216
|
+
gap: 10px 12px;
|
|
217
|
+
margin: 10px 0 0;
|
|
218
|
+
}
|
|
219
|
+
.ys-card-list__field {
|
|
220
|
+
min-width: 0;
|
|
221
|
+
}
|
|
222
|
+
.ys-card-list__field.is-wide {
|
|
223
|
+
grid-column: 1 / -1;
|
|
224
|
+
}
|
|
225
|
+
.ys-card-list__field dt {
|
|
226
|
+
margin: 0 0 2px;
|
|
227
|
+
color: var(--ys-color-text-3);
|
|
228
|
+
font-size: 12px;
|
|
229
|
+
line-height: 18px;
|
|
230
|
+
}
|
|
231
|
+
.ys-card-list__field dd {
|
|
232
|
+
margin: 0;
|
|
233
|
+
color: var(--ys-color-text-1);
|
|
234
|
+
font-size: 14px;
|
|
235
|
+
line-height: 20px;
|
|
236
|
+
overflow-wrap: anywhere;
|
|
237
|
+
white-space: pre-line;
|
|
238
|
+
}
|
|
239
|
+
.ys-card-list__field.is-right dd {
|
|
240
|
+
font-variant-numeric: tabular-nums;
|
|
241
|
+
}
|
|
242
|
+
.ys-card-list__footer {
|
|
243
|
+
margin-top: 10px;
|
|
244
|
+
padding-top: 10px;
|
|
245
|
+
border-top: 1px dashed var(--ys-color-border);
|
|
246
|
+
}
|
|
247
|
+
</style>
|
|
@@ -13,6 +13,8 @@ import { ysMultiText } from "../multiValue";
|
|
|
13
13
|
import YsFormDialog from "./YsFormDialog.vue";
|
|
14
14
|
import YsRowActionMenu from "./YsRowActionMenu.vue";
|
|
15
15
|
import YsTableToolbar from "./YsTableToolbar.vue";
|
|
16
|
+
import YsCardList, { type YsCardField } from "./YsCardList.vue";
|
|
17
|
+
import { useYsMobile } from "../viewport";
|
|
16
18
|
|
|
17
19
|
interface FieldSpec {
|
|
18
20
|
key: string;
|
|
@@ -50,6 +52,8 @@ const dialogOpen = ref(false);
|
|
|
50
52
|
const editing = ref<Record<string, unknown> | null>(null);
|
|
51
53
|
const currentPage = ref(1);
|
|
52
54
|
const pageSize = ref(20);
|
|
55
|
+
/** 手机上换成卡片:第一列当标题,其余列两列排开,开关列显示「启用 / 停用」文字。 */
|
|
56
|
+
const mobile = useYsMobile();
|
|
53
57
|
|
|
54
58
|
function columnWidth(field: FieldSpec): number {
|
|
55
59
|
return lookupColumnWidth(props.table, field.key);
|
|
@@ -126,6 +130,15 @@ function display(row: Record<string, unknown>, f: FieldSpec): string {
|
|
|
126
130
|
return String(v ?? "");
|
|
127
131
|
}
|
|
128
132
|
|
|
133
|
+
const cardTitle = (row: Record<string, unknown>) => (displayColumns.value[0] ? display(row, displayColumns.value[0]) : "");
|
|
134
|
+
const cardFields = computed<YsCardField[]>(() =>
|
|
135
|
+
displayColumns.value.slice(1).map((c) => ({ key: c.key, label: c.title, value: (row: Record<string, unknown>) => display(row, c) })),
|
|
136
|
+
);
|
|
137
|
+
const crudActions = [
|
|
138
|
+
{ key: "edit", label: "编辑", tone: "primary" as const },
|
|
139
|
+
{ key: "delete", label: "删除", tone: "danger" as const, divided: true },
|
|
140
|
+
];
|
|
141
|
+
|
|
129
142
|
function onCreate() {
|
|
130
143
|
if (props.externalEditor) {
|
|
131
144
|
emit("create");
|
|
@@ -160,7 +173,18 @@ function onSubmit(payload: Record<string, unknown>) {
|
|
|
160
173
|
>
|
|
161
174
|
</YsTableToolbar>
|
|
162
175
|
|
|
163
|
-
<
|
|
176
|
+
<YsCardList
|
|
177
|
+
v-if="mobile"
|
|
178
|
+
:rows="paged"
|
|
179
|
+
:fields="cardFields"
|
|
180
|
+
:title="cardTitle"
|
|
181
|
+
:loading="loading"
|
|
182
|
+
:actions="readonly ? undefined : () => crudActions"
|
|
183
|
+
@action="(key, row) => (key === 'edit' ? onEdit(row) : onRemove(row))"
|
|
184
|
+
>
|
|
185
|
+
<template v-if="$slots['row-actions']" #menu-extra="{ row, close }"><slot name="row-actions" :row="row" :close="close" /></template>
|
|
186
|
+
</YsCardList>
|
|
187
|
+
<el-table v-else :fit="false" :data="paged" v-loading="loading" stripe border size="small" table-layout="fixed" row-key="_id">
|
|
164
188
|
<el-table-column
|
|
165
189
|
v-for="c in displayColumns"
|
|
166
190
|
:key="c.key"
|
|
@@ -14,6 +14,7 @@ import YsViewerIcon from "./YsViewerIcon.vue";
|
|
|
14
14
|
import YsViewerState from "./YsViewerState.vue";
|
|
15
15
|
import YsFileNotes from "./YsFileNotes.vue";
|
|
16
16
|
import { isActiveContent } from "../fileRules";
|
|
17
|
+
import { YS_MOBILE_QUERY } from "../viewport";
|
|
17
18
|
import "../preview/viewer.css";
|
|
18
19
|
import {
|
|
19
20
|
OFFICE_MIME,
|
|
@@ -187,7 +188,7 @@ function onNarrowChange() {
|
|
|
187
188
|
}
|
|
188
189
|
|
|
189
190
|
onMounted(() => {
|
|
190
|
-
media = window.matchMedia?.(
|
|
191
|
+
media = window.matchMedia?.(YS_MOBILE_QUERY) ?? null;
|
|
191
192
|
onNarrowChange();
|
|
192
193
|
media?.addEventListener?.("change", onNarrowChange);
|
|
193
194
|
});
|
|
@@ -495,7 +496,7 @@ function openInTab() {
|
|
|
495
496
|
white-space: pre-wrap;
|
|
496
497
|
word-break: break-word;
|
|
497
498
|
}
|
|
498
|
-
@media (max-width:
|
|
499
|
+
@media (max-width: 767px) {
|
|
499
500
|
.ys-file-preview__head {
|
|
500
501
|
gap: 8px;
|
|
501
502
|
padding-left: 12px;
|
|
@@ -69,11 +69,17 @@ defineProps<{
|
|
|
69
69
|
.ys-page-header.is-compact h1 {
|
|
70
70
|
font-size: 20px;
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
/* 手机:标题 20px,标题和按钮上下排,按钮占满一行。 */
|
|
73
|
+
@media (max-width: 767px) {
|
|
73
74
|
.ys-page-header {
|
|
74
|
-
align-items:
|
|
75
|
+
align-items: stretch;
|
|
75
76
|
flex-direction: column;
|
|
76
|
-
gap:
|
|
77
|
+
gap: 10px;
|
|
78
|
+
margin-bottom: 12px;
|
|
79
|
+
}
|
|
80
|
+
.ys-page-header h1,
|
|
81
|
+
.ys-page-header.is-compact h1 {
|
|
82
|
+
font-size: 20px;
|
|
77
83
|
}
|
|
78
84
|
.ys-page-header__actions {
|
|
79
85
|
width: 100%;
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
<!-- YsPageLayout —— 统一布局骨架:顶栏(logo/app名/工具条) + 侧栏 + 内容。
|
|
1
|
+
<!-- YsPageLayout —— 统一布局骨架:顶栏(logo/app名/工具条) + 侧栏 + 内容。
|
|
2
|
+
三档宽度(断点见 ../viewport.ts):
|
|
3
|
+
- 桌面(≥1024px):侧栏默认展开,左上角按钮收起或展开。
|
|
4
|
+
- 窄屏(768~1023px):侧栏默认收起,把宽度让给内容;按钮照样能展开。
|
|
5
|
+
- 手机(≤767px):侧栏变成从左边滑出的抽屉,左上角是菜单按钮;业务切换(switcher)放到抽屉顶部;
|
|
6
|
+
点抽屉里会跳走的菜单项、点遮罩、按 Esc 都会关上抽屉;顶栏和内容区避开刘海和底部横条(safe-area)。
|
|
7
|
+
不传 v-model:sidebar-collapsed 时,以上都由本组件管理;传了就以调用方为准(手机上的抽屉始终由本组件管理)。 -->
|
|
2
8
|
<script setup lang="ts">
|
|
3
|
-
import { computed, ref } from "vue";
|
|
9
|
+
import { computed, nextTick, onBeforeUnmount, ref, watch } from "vue";
|
|
10
|
+
import { useYsMobile, useYsNarrow } from "../viewport";
|
|
4
11
|
|
|
5
12
|
const props = withDefaults(
|
|
6
13
|
defineProps<{
|
|
@@ -24,40 +31,97 @@ const emit = defineEmits<{
|
|
|
24
31
|
navigate: [key: string];
|
|
25
32
|
"update:sidebarCollapsed": [value: boolean];
|
|
26
33
|
}>();
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
|
|
35
|
+
const mobile = useYsMobile();
|
|
36
|
+
const narrow = useYsNarrow();
|
|
37
|
+
/** 桌面、窄屏下侧栏是否收起:不受控时窄屏默认收起,宽度跨过 1024px 时重置。 */
|
|
38
|
+
const internalCollapsed = ref(narrow.value);
|
|
39
|
+
watch(narrow, (value) => {
|
|
40
|
+
internalCollapsed.value = value;
|
|
41
|
+
});
|
|
42
|
+
const collapsed = computed(() => props.sidebarCollapsed ?? internalCollapsed.value);
|
|
31
43
|
const hasSidebar = computed(() => !!slots.sidebar || !!props.menus?.length);
|
|
32
44
|
|
|
45
|
+
/** 手机抽屉:只在手机上有效,每次进入手机宽度都是关着的。 */
|
|
46
|
+
const drawerOpen = ref(false);
|
|
47
|
+
const drawer = ref<HTMLElement>();
|
|
48
|
+
const menuButton = ref<HTMLButtonElement>();
|
|
49
|
+
watch(mobile, () => {
|
|
50
|
+
drawerOpen.value = false;
|
|
51
|
+
});
|
|
52
|
+
|
|
33
53
|
function toggleSidebar() {
|
|
54
|
+
if (mobile.value) {
|
|
55
|
+
drawerOpen.value = !drawerOpen.value;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
34
58
|
internalCollapsed.value = !collapsed.value;
|
|
35
59
|
emit("update:sidebarCollapsed", internalCollapsed.value);
|
|
36
60
|
}
|
|
61
|
+
|
|
62
|
+
function closeDrawer(restoreFocus = true) {
|
|
63
|
+
if (!drawerOpen.value) return;
|
|
64
|
+
drawerOpen.value = false;
|
|
65
|
+
if (restoreFocus) void nextTick(() => menuButton.value?.focus());
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 打开后把焦点放进抽屉(当前菜单项优先),读屏和键盘都从菜单开始。 */
|
|
69
|
+
watch(drawerOpen, async (open) => {
|
|
70
|
+
if (!open) return;
|
|
71
|
+
await nextTick();
|
|
72
|
+
const menu = drawer.value?.querySelector<HTMLElement>(".ys-layout__drawer-menu");
|
|
73
|
+
const target = menu?.querySelector<HTMLElement>("[aria-current='page']") ?? menu?.querySelector<HTMLElement>("button, a[href]");
|
|
74
|
+
target?.focus({ preventScroll: true });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 点了抽屉里会跳走的按钮或链接就关抽屉;只展开收起的按钮(带 aria-expanded)和标了 data-ys-keep-drawer 的不关。
|
|
79
|
+
* 路由跳转是异步的,等这一轮点击处理完再关,避免菜单先消失、跳转后又闪一下。
|
|
80
|
+
*/
|
|
81
|
+
function onDrawerClick(event: MouseEvent) {
|
|
82
|
+
const target = (event.target as HTMLElement | null)?.closest<HTMLElement>("button, a[href], [role='menuitem']");
|
|
83
|
+
if (!target || !drawer.value?.contains(target)) return;
|
|
84
|
+
if (target.hasAttribute("aria-expanded") || target.closest("[data-ys-keep-drawer]")) return;
|
|
85
|
+
if ((target as HTMLButtonElement).disabled) return;
|
|
86
|
+
setTimeout(() => closeDrawer(false), 0);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function onKeydown(event: KeyboardEvent) {
|
|
90
|
+
if (event.key === "Escape" && drawerOpen.value) {
|
|
91
|
+
event.stopPropagation();
|
|
92
|
+
closeDrawer();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
window.addEventListener("keydown", onKeydown);
|
|
96
|
+
onBeforeUnmount(() => window.removeEventListener("keydown", onKeydown));
|
|
97
|
+
|
|
98
|
+
const toggleLabel = computed(() => {
|
|
99
|
+
if (mobile.value) return drawerOpen.value ? "关闭导航菜单" : "打开导航菜单";
|
|
100
|
+
return collapsed.value ? "展开导航栏" : "收起导航栏";
|
|
101
|
+
});
|
|
37
102
|
</script>
|
|
38
103
|
|
|
39
104
|
<template>
|
|
40
|
-
<div class="ys-layout" :class="{ 'is-collapsed': collapsed }">
|
|
105
|
+
<div class="ys-layout" :class="{ 'is-collapsed': collapsed && !mobile, 'is-mobile': mobile }">
|
|
41
106
|
<header class="ys-layout__top">
|
|
42
107
|
<!-- 品牌在前、收起按钮在品牌列最右,收起侧栏后顶栏布局不变。
|
|
43
|
-
顶栏右边有业务系统标签时,品牌列右侧补一条与侧栏同色的竖线把两块分开;没有标签时不画。
|
|
108
|
+
顶栏右边有业务系统标签时,品牌列右侧补一条与侧栏同色的竖线把两块分开;没有标签时不画。
|
|
109
|
+
手机上菜单按钮在最左,业务切换移到抽屉里。 -->
|
|
44
110
|
<div class="ys-layout__brand" :class="{ 'has-sidebar': hasSidebar }">
|
|
45
111
|
<img v-if="logoUrl" :src="logoUrl" class="ys-layout__logo" alt="logo" />
|
|
46
112
|
<span class="ys-layout__title">{{ appTitle }}</span>
|
|
47
|
-
<el-tooltip
|
|
48
|
-
v-if="hasSidebar"
|
|
49
|
-
:content="collapsed ? '展开导航栏' : '收起导航栏'"
|
|
50
|
-
placement="bottom"
|
|
51
|
-
>
|
|
113
|
+
<el-tooltip v-if="hasSidebar" :content="toggleLabel" placement="bottom" :disabled="mobile">
|
|
52
114
|
<span class="ys-layout__toggle-wrap">
|
|
53
115
|
<button
|
|
116
|
+
ref="menuButton"
|
|
54
117
|
type="button"
|
|
55
118
|
class="ys-layout__toggle"
|
|
56
|
-
:aria-label="
|
|
57
|
-
:aria-expanded="!collapsed"
|
|
119
|
+
:aria-label="toggleLabel"
|
|
120
|
+
:aria-expanded="mobile ? drawerOpen : !collapsed"
|
|
121
|
+
:aria-controls="mobile ? 'ys-layout-drawer' : undefined"
|
|
58
122
|
@click="toggleSidebar"
|
|
59
123
|
>
|
|
60
|
-
<!--
|
|
124
|
+
<!-- 手机上是「三条横线」菜单图标;桌面图标画的就是页面布局:展开时「上左右」,收起后「上下」。 -->
|
|
61
125
|
<svg
|
|
62
126
|
class="ys-layout__toggle-icon"
|
|
63
127
|
viewBox="0 0 24 24"
|
|
@@ -68,27 +132,23 @@ function toggleSidebar() {
|
|
|
68
132
|
stroke-linejoin="round"
|
|
69
133
|
aria-hidden="true"
|
|
70
134
|
>
|
|
71
|
-
<
|
|
72
|
-
<
|
|
73
|
-
|
|
74
|
-
|
|
135
|
+
<path v-if="mobile" d="M4 7h16M4 12h16M4 17h16" />
|
|
136
|
+
<template v-else>
|
|
137
|
+
<rect x="3.5" y="4.5" width="17" height="15" rx="2.5" />
|
|
138
|
+
<path :d="collapsed ? 'M3.5 9.5h17' : 'M3.5 9.5h17M9.5 9.5v10'" />
|
|
139
|
+
</template>
|
|
75
140
|
</svg>
|
|
76
141
|
</button>
|
|
77
142
|
</span>
|
|
78
143
|
</el-tooltip>
|
|
79
144
|
</div>
|
|
80
|
-
<div class="ys-layout__switcher"><slot name="switcher" /></div>
|
|
145
|
+
<div v-if="!mobile" class="ys-layout__switcher"><slot name="switcher" /></div>
|
|
81
146
|
<div v-if="$slots.toolbar" class="ys-layout__toolbar">
|
|
82
147
|
<slot name="toolbar" />
|
|
83
148
|
</div>
|
|
84
149
|
</header>
|
|
85
150
|
<div class="ys-layout__body">
|
|
86
|
-
<aside
|
|
87
|
-
v-if="hasSidebar"
|
|
88
|
-
v-show="!collapsed"
|
|
89
|
-
class="ys-layout__side"
|
|
90
|
-
aria-label="侧栏导航"
|
|
91
|
-
>
|
|
151
|
+
<aside v-if="hasSidebar && !mobile" v-show="!collapsed" class="ys-layout__side" aria-label="侧栏导航">
|
|
92
152
|
<slot name="sidebar">
|
|
93
153
|
<button
|
|
94
154
|
v-for="menu in menus"
|
|
@@ -107,6 +167,40 @@ function toggleSidebar() {
|
|
|
107
167
|
<slot />
|
|
108
168
|
</main>
|
|
109
169
|
</div>
|
|
170
|
+
<template v-if="hasSidebar && mobile">
|
|
171
|
+
<Transition name="ys-layout-fade">
|
|
172
|
+
<div v-if="drawerOpen" class="ys-layout__mask" aria-hidden="true" @click="closeDrawer()" />
|
|
173
|
+
</Transition>
|
|
174
|
+
<Transition name="ys-layout-slide">
|
|
175
|
+
<aside v-show="drawerOpen" id="ys-layout-drawer" ref="drawer" class="ys-layout__drawer" aria-label="导航菜单" @click="onDrawerClick">
|
|
176
|
+
<div class="ys-layout__drawer-head">
|
|
177
|
+
<img v-if="logoUrl" :src="logoUrl" class="ys-layout__logo" alt="" />
|
|
178
|
+
<span class="ys-layout__title">{{ appTitle }}</span>
|
|
179
|
+
<button type="button" class="ys-layout__toggle ys-layout__drawer-close" aria-label="关闭导航菜单" @click.stop="closeDrawer()">
|
|
180
|
+
<svg class="ys-layout__toggle-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" aria-hidden="true">
|
|
181
|
+
<path d="M6 6l12 12M18 6L6 18" />
|
|
182
|
+
</svg>
|
|
183
|
+
</button>
|
|
184
|
+
</div>
|
|
185
|
+
<div v-if="$slots.switcher" class="ys-layout__drawer-switcher"><slot name="switcher" /></div>
|
|
186
|
+
<div class="ys-layout__drawer-menu">
|
|
187
|
+
<slot name="sidebar">
|
|
188
|
+
<button
|
|
189
|
+
v-for="menu in menus"
|
|
190
|
+
:key="menu.key"
|
|
191
|
+
type="button"
|
|
192
|
+
class="ys-layout__menu"
|
|
193
|
+
:class="{ 'is-active': menu.key === active }"
|
|
194
|
+
:aria-current="menu.key === active ? 'page' : undefined"
|
|
195
|
+
@click="emit('navigate', menu.key)"
|
|
196
|
+
>
|
|
197
|
+
{{ menu.title }}
|
|
198
|
+
</button>
|
|
199
|
+
</slot>
|
|
200
|
+
</div>
|
|
201
|
+
</aside>
|
|
202
|
+
</Transition>
|
|
203
|
+
</template>
|
|
110
204
|
</div>
|
|
111
205
|
</template>
|
|
112
206
|
|
|
@@ -266,6 +360,122 @@ function toggleSidebar() {
|
|
|
266
360
|
overflow: auto;
|
|
267
361
|
background: var(--ys-color-bg);
|
|
268
362
|
}
|
|
363
|
+
|
|
364
|
+
/* ---------- 手机 ---------- */
|
|
365
|
+
.ys-layout.is-mobile {
|
|
366
|
+
--ys-layout-header-height: calc(52px + env(safe-area-inset-top, 0px));
|
|
367
|
+
}
|
|
368
|
+
.ys-layout.is-mobile .ys-layout__top {
|
|
369
|
+
grid-template-columns: minmax(0, 1fr) auto;
|
|
370
|
+
padding: env(safe-area-inset-top, 0px) max(8px, env(safe-area-inset-right, 0px)) 0 max(4px, env(safe-area-inset-left, 0px));
|
|
371
|
+
}
|
|
372
|
+
.ys-layout.is-mobile .ys-layout__brand {
|
|
373
|
+
padding: 0;
|
|
374
|
+
gap: 6px;
|
|
375
|
+
}
|
|
376
|
+
/* 菜单按钮挪到最左,44px 见方,手指好点。 */
|
|
377
|
+
.ys-layout.is-mobile .ys-layout__toggle-wrap {
|
|
378
|
+
order: -1;
|
|
379
|
+
flex: 0 0 44px;
|
|
380
|
+
margin-left: 0;
|
|
381
|
+
}
|
|
382
|
+
.ys-layout.is-mobile .ys-layout__toggle {
|
|
383
|
+
flex: 0 0 44px;
|
|
384
|
+
width: 44px;
|
|
385
|
+
height: 44px;
|
|
386
|
+
}
|
|
387
|
+
.ys-layout.is-mobile .ys-layout__toggle-icon {
|
|
388
|
+
width: 22px;
|
|
389
|
+
height: 22px;
|
|
390
|
+
}
|
|
391
|
+
.ys-layout.is-mobile .ys-layout__logo {
|
|
392
|
+
width: 24px;
|
|
393
|
+
height: 24px;
|
|
394
|
+
}
|
|
395
|
+
.ys-layout.is-mobile .ys-layout__content {
|
|
396
|
+
padding: 12px max(12px, env(safe-area-inset-right, 0px)) calc(16px + env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));
|
|
397
|
+
/* 内容区滚到头不把整页带着回弹,也不触发浏览器的下拉刷新。 */
|
|
398
|
+
overscroll-behavior-y: contain;
|
|
399
|
+
}
|
|
400
|
+
.ys-layout__mask {
|
|
401
|
+
position: fixed;
|
|
402
|
+
inset: 0;
|
|
403
|
+
z-index: 1990;
|
|
404
|
+
background: var(--ys-color-backdrop);
|
|
405
|
+
}
|
|
406
|
+
.ys-layout__drawer {
|
|
407
|
+
position: fixed;
|
|
408
|
+
top: 0;
|
|
409
|
+
bottom: 0;
|
|
410
|
+
left: 0;
|
|
411
|
+
z-index: 1991;
|
|
412
|
+
display: flex;
|
|
413
|
+
flex-direction: column;
|
|
414
|
+
box-sizing: border-box;
|
|
415
|
+
width: min(84vw, 300px);
|
|
416
|
+
padding: env(safe-area-inset-top, 0px) 0 env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
|
|
417
|
+
background: var(--ys-color-bg-elevated);
|
|
418
|
+
box-shadow: var(--ys-shadow-dialog);
|
|
419
|
+
}
|
|
420
|
+
.ys-layout__drawer-head {
|
|
421
|
+
display: flex;
|
|
422
|
+
flex: none;
|
|
423
|
+
align-items: center;
|
|
424
|
+
gap: 8px;
|
|
425
|
+
height: 52px;
|
|
426
|
+
padding: 0 4px 0 16px;
|
|
427
|
+
border-bottom: 1px solid var(--ys-color-border);
|
|
428
|
+
}
|
|
429
|
+
.ys-layout__drawer-close {
|
|
430
|
+
flex: 0 0 44px;
|
|
431
|
+
width: 44px;
|
|
432
|
+
height: 44px;
|
|
433
|
+
margin-left: auto;
|
|
434
|
+
}
|
|
435
|
+
.ys-layout__drawer-switcher {
|
|
436
|
+
flex: none;
|
|
437
|
+
padding: 8px 12px;
|
|
438
|
+
border-bottom: 1px solid var(--ys-color-border);
|
|
439
|
+
overflow-x: auto;
|
|
440
|
+
}
|
|
441
|
+
.ys-layout__drawer-switcher:empty {
|
|
442
|
+
display: none;
|
|
443
|
+
}
|
|
444
|
+
.ys-layout__drawer-menu {
|
|
445
|
+
flex: 1;
|
|
446
|
+
min-height: 0;
|
|
447
|
+
padding: 8px;
|
|
448
|
+
overflow-y: auto;
|
|
449
|
+
overscroll-behavior: contain;
|
|
450
|
+
}
|
|
451
|
+
.ys-layout__drawer-menu .ys-layout__menu {
|
|
452
|
+
min-height: 44px;
|
|
453
|
+
font-size: 15px;
|
|
454
|
+
}
|
|
455
|
+
.ys-layout-fade-enter-active,
|
|
456
|
+
.ys-layout-fade-leave-active {
|
|
457
|
+
transition: opacity 0.2s ease;
|
|
458
|
+
}
|
|
459
|
+
.ys-layout-fade-enter-from,
|
|
460
|
+
.ys-layout-fade-leave-to {
|
|
461
|
+
opacity: 0;
|
|
462
|
+
}
|
|
463
|
+
.ys-layout-slide-enter-active,
|
|
464
|
+
.ys-layout-slide-leave-active {
|
|
465
|
+
transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
|
|
466
|
+
}
|
|
467
|
+
.ys-layout-slide-enter-from,
|
|
468
|
+
.ys-layout-slide-leave-to {
|
|
469
|
+
transform: translateX(-100%);
|
|
470
|
+
}
|
|
471
|
+
@media (prefers-reduced-motion: reduce) {
|
|
472
|
+
.ys-layout-fade-enter-active,
|
|
473
|
+
.ys-layout-fade-leave-active,
|
|
474
|
+
.ys-layout-slide-enter-active,
|
|
475
|
+
.ys-layout-slide-leave-active {
|
|
476
|
+
transition: none;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
269
479
|
</style>
|
|
270
480
|
|
|
271
481
|
<style>
|