Skip to content

Commit 9a68c47

Browse files
authored
feat: ✨ skeleton添加默认内容插槽 (#322)
1 parent 00ffa9f commit 9a68c47

File tree

5 files changed

+95
-14
lines changed

5 files changed

+95
-14
lines changed

docs/component/skeleton.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<frame/>
22

33
# Skeleton 骨架屏
4+
45
用于等待加载内容所展示的占位图形组合,有动态效果加载效果,减少用户等待焦虑。
56

67
## 骨架图风格
8+
79
支持 `avatar``image``text``paragraph` 四种类型。
10+
811
```html
912
// 头像骨架屏
1013
<wd-skeleton theme="avatar" />
@@ -14,11 +17,15 @@
1417
<wd-skeleton theme="text" />
1518
// 段落骨架屏
1619
<wd-skeleton theme="paragraph" />
20+
1721
```
22+
1823
## 宫格骨架屏
24+
1925
```html
2026
<wd-skeleton :row-col="grid" />
2127
```
28+
2229
```ts
2330
const grid = [
2431
[
@@ -38,8 +45,8 @@ const grid = [
3845
]
3946
```
4047

41-
4248
## 单元格骨架屏
49+
4350
```html
4451
<view style="display: flex">
4552
<wd-skeleton :row-col="[{ size: '48px', type: 'circle' }]" />
@@ -52,12 +59,14 @@ const grid = [
5259
```
5360

5461
## 图片组合骨架屏
62+
5563
```html
5664
<view>
5765
<wd-skeleton :row-col="imageGroup" />
5866
<wd-skeleton :custom-style="{ marginTop: '20px' }" :row-col="imageGroup" />
5967
</view>
6068
```
69+
6170
```ts
6271
const imageGroup = [
6372
{ height: '171px' }, 1, { width: '107px' },
@@ -66,7 +75,9 @@ const imageGroup = [
6675
```
6776

6877
## 加载动画
78+
6979
支持 `gradient``flashed`
80+
7081
```html
7182
<wd-skeleton animation="gradient" theme="paragraph" />
7283
<view style="display: flex">
@@ -75,10 +86,55 @@ const imageGroup = [
7586
</view>
7687
```
7788

89+
## 插槽内容
90+
91+
可以通过插槽写入展示的内容,当请求结束,将loading设置为false,此时会隐藏骨架组件,同时展示插槽内容。
92+
93+
```html
94+
<wd-skeleton
95+
:row-col="[
96+
[
97+
{ width: '48px', height: '48px' },
98+
{ width: '48px', height: '48px' },
99+
{ width: '48px', height: '48px' },
100+
{ width: '48px', height: '48px' },
101+
{ width: '48px', height: '48px' }
102+
],
103+
[
104+
{ width: '48px', height: '16px' },
105+
{ width: '48px', height: '16px' },
106+
{ width: '48px', height: '16px' },
107+
{ width: '48px', height: '16px' },
108+
{ width: '48px', height: '16px' }
109+
]
110+
]"
111+
:loading="showContent"
112+
>
113+
<wd-grid>
114+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
115+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
116+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
117+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
118+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
119+
</wd-grid>
120+
</wd-skeleton>
121+
```
122+
123+
```js
124+
const showContent = ref(true)
125+
```
126+
78127
## Attributes
79-
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
80-
| ------------ | ----------------------------------------------------------------| ------- | ------------------------- | ------------ | -------- |
81-
| theme | 骨架图风格 | SkeletonTheme | `text` `avatar` `paragraph` `image` | - | - |
82-
| rowCol | 用于设置行列数量、宽度高度、间距等<br />【示例一】`[1, 1, 2]` 表示输出三行骨架图,第一行一列,第二行一列,第三行两列。 <br />【示例二】`[1, 1, { width: '100px' }]` 表示自定义第三行的宽度为 `100px`。 <br />【示例三】`[1, 2, [{ width, height }, { width, height, marginLeft }]]` 表示第三行有两列,且自定义宽度、高度和间距 | SkeletonRowCol | - | - | - |
83-
| loading | 是否为加载状态,如果是则显示骨架图,如果不是则显示加载完成的内容 | boolean | - | true | - |
84-
| animation | 动画效果 | SkeletonAnimation | `gradient` `flashed` | - | - |
128+
129+
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
130+
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- | ----------------------------------- | ------ | -------- |
131+
| theme | 骨架图风格 | SkeletonTheme | `text` `avatar` `paragraph` `image` | - | - |
132+
| rowCol | 用于设置行列数量、宽度高度、间距等<br />【示例一】`[1, 1, 2]` 表示输出三行骨架图,第一行一列,第二行一列,第三行两列。 <br />【示例二】`[1, 1, { width: '100px' }]` 表示自定义第三行的宽度为 `100px`。 <br />【示例三】`[1, 2, [{ width, height }, { width, height, marginLeft }]]` 表示第三行有两列,且自定义宽度、高度和间距 | SkeletonRowCol | - | - | - |
133+
| loading | 是否为加载状态,如果是则显示骨架图,如果不是则显示加载完成的内容 | boolean | - | true | - |
134+
| animation | 动画效果 | SkeletonAnimation | `gradient` `flashed` | - | - |
135+
136+
## Slots
137+
138+
| name | 说明 | 最低版本 |
139+
| ------- | --------------------- | ---------------- |
140+
| default | loading结束后展示内容 | $LOWEST_VERSION$ |

src/pages/skeleton/Index.vue

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,32 @@
3131
<wd-skeleton :custom-style="{ width: '100%', marginLeft: '12px' }" animation="flashed" theme="paragraph" />
3232
</view>
3333
</demo-block>
34+
<demo-block title="插槽内容">
35+
<view style="margin-bottom: 10px">切换显示</view>
36+
<wd-switch v-model="showContent" />
37+
<view style="height: 20px"></view>
38+
<wd-skeleton :row-col="grid" :loading="showContent">
39+
<wd-grid>
40+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
41+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
42+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
43+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
44+
<wd-grid-item icon-size="32px" icon="picture" text="文字" />
45+
</wd-grid>
46+
</wd-skeleton>
47+
</demo-block>
3448
</page-wraper>
3549
</template>
3650
<script lang="ts" setup>
51+
import { ref } from 'vue'
52+
import type { SkeletonRowCol, SkeletonProps } from '../../uni_modules/wot-design-uni/components/wd-skeleton/types'
53+
3754
const themeList = [
3855
{ title: '头像骨架屏', value: 'avatar' },
3956
{ title: '图片骨架屏', value: 'image' },
4057
{ title: '文本骨架屏', value: 'text' },
4158
{ title: '段落骨架屏', value: 'paragraph' }
42-
]
59+
] as Array<{ title: string; value: SkeletonProps['theme'] }>
4360
const grid = [
4461
[
4562
{ width: '48px', height: '48px' },
@@ -55,6 +72,8 @@ const grid = [
5572
{ width: '48px', height: '16px' },
5673
{ width: '48px', height: '16px' }
5774
]
58-
]
59-
const imageGroup = [{ height: '171px' }, 1, { width: '107px' }, [{ width: '93px' }, { width: '32px', marginLeft: '41px' }]]
75+
] as SkeletonRowCol[]
76+
const imageGroup = [{ height: '171px' }, 1, { width: '107px' }, [{ width: '93px' }, { width: '32px', marginLeft: '41px' }]] as SkeletonRowCol[]
77+
78+
const showContent = ref(true)
6079
</script>

src/uni_modules/wot-design-uni/components/wd-skeleton/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PropType, CSSProperties, ExtractPropTypes } from 'vue'
1+
import type { PropType, ExtractPropTypes, CSSProperties } from 'vue'
22
import { makeArrayProp, makeBooleanProp, makeStringProp } from '../common/props'
33

44
type SkeletonTheme = 'text' | 'avatar' | 'paragraph' | 'image'
@@ -16,7 +16,7 @@ export type SkeletonRowColObj = {
1616
borderRadius?: string | number
1717
backgroundColor?: string
1818
}
19-
export type SkeletonRowCol = Array<number | SkeletonRowColObj | Array<SkeletonRowColObj>>
19+
export type SkeletonRowCol = number | SkeletonRowColObj | Array<SkeletonRowColObj>
2020
export type SkeletonThemeVars = {
2121
notifyPadding?: string
2222
notifyFontSize?: string

src/uni_modules/wot-design-uni/components/wd-skeleton/wd-skeleton.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
-->
1010
<template>
1111
<view :class="`wd-skeleton ${customClass}`" :style="customStyle">
12-
<view class="wd-skeleton__content" v-if="parsedRowCols.length">
12+
<view class="wd-skeleton__content" v-if="show">
1313
<view class="wd-skeleton__row" v-for="(row, index) of parsedRowCols" :key="`row-${index}`">
1414
<view v-for="(col, idx) of row" :key="`col-${idx}`" :class="col.class" :style="col.style" />
1515
</view>
1616
</view>
17+
<view v-else>
18+
<slot />
19+
</view>
1720
</view>
1821
</template>
1922
<script lang="ts">
@@ -45,7 +48,7 @@ const themeMap = {
4548
paragraph: [1, 1, 1, { width: '55%' }]
4649
}
4750
const props = defineProps(skeletonProps)
48-
const rowCols = ref<SkeletonRowCol>([])
51+
const rowCols = ref<SkeletonRowCol[]>([])
4952
5053
const parsedRowCols = computed(() => {
5154
return rowCols.value.map((item) => {
@@ -107,6 +110,8 @@ watch(
107110
},
108111
{ immediate: true }
109112
)
113+
114+
const show = computed(() => props.loading == undefined || props.loading === true)
110115
</script>
111116

112117
<style lang="scss" scoped>

src/uni_modules/wot-design-uni/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ declare module '@vue/runtime-core' {
9696
WdForm: typeof import('./components/wd-form/wd-form.vue')['default']
9797
WdTextarea: typeof import('./components/wd-textarea/wd-textarea.vue')['default']
9898
WdBacktop: typeof import('./components/wd-backtop/wd-backtop.vue')['default']
99+
WdSkeleton: typeof import('./components/wd-skeleton/wd-skeleton.vue')['default']
99100
WdIndexBar: typeof import('./components/wd-index-bar/wd-index-bar.vue')['default']
100101
WdIndexAnchor: typeof import('./components/wd-index-anchor/wd-index-anchor.vue')['default']
101102
}

0 commit comments

Comments
 (0)