标签页是非常常用的组件,接下来我们来制作一个简单的 Tabs 组件

返回阅读列表点击 这里

需求分析

我们先做一个简单的需求分析

  1. 可以选择标签页排列的方向
  2. 选中的标签页应当有下划线高亮显示
  3. 切换选中时,下划线应当有动画效果
  4. 应当允许更换颜色

那么可以整理出以下参数表格

参数 含义 类型 可选值 默认值
direction 方向 string row / column row
selected 默认选中 string 子项的 name 必填
color 颜色 string 任意合法颜色值 #d3c8f5

通过为子项设置 name 属性,来指定默认值

骨架

本体

通过需求分析我们可以得到如下骨架:

<template>
<div
class="jeremy-tabs"
:style="{ '--color': color }"
ref="container"
:direction="direction"
>
<div class="jeremy-tabs-titles">
<button
v-for="(title, index) in titles"
:key="index"
class="jeremy-tabs-title"
:class="{ selected: names[index] === selected }"
@click="select(index)"
:ref="
(el) => {
if (names[index] === selected) {
selectedItem = el;
}
}
"
>
{{ title }}
</button>
<div class="jeremy-tabs-indicator" ref="indicator"></div>
</div>
<div class="jeremy-tabs-divider"></div>
<div class="jeremy-tabs-content">
<component :is="content" :key="selected" />
</div>
</div>
</template>

注意

这里我们用一个 div 来充当下划线,再使用一个新的 component 来显示用户输入的内容

我们还需要为标签页创建子组件,即 Tab 组件

子组件

通过之前的分析,可以得出子组件 Tab 的骨架如下:

<template>
<div>
<slot></slot>
</div>
</template>

另外,我们还需要定义一个参数,也就是标签的标题,所以还应该有如下声明与导出:

declare const props: {
title: string;
}; export default {
install: function (Vue) {
Vue.component(this.name, this);
},
name: "JeremyTab",
props: {
title: {
type: String,
default: "标签页",
},
},
};

功能

首先,我们先在 TypeScript 中声明:

declare const props: {
direction?: "row" | "column";
selected: String;
color: String;
};
declare const context: SetupContext;

其次,再在 export default 中,写入我们的参数:

export default {
name: "JeremyTabs",
props: {
direction: {
type: String,
default: "row",
},
selected: {
type: String,
required: true,
},
color: {
type: String,
default: "#8c6fef",
},
},
};

再次,再补全 setup 方法:

  setup(props, context) {
if (!["row", "column"].includes(props.direction)) {
throw new Error("错误的方向");
}
const container = ref<HTMLDivElement>(null);
const selectedItem = ref<HTMLButtonElement>(null);
const indicator = ref<HTMLDivElement>(null);
const slots = context.slots.default();
slots.forEach((slot) => {
if (slot.type !== JeremyTab) {
throw new Error("一级子标签必须是 JeremyTab");
}
if (!slot.props) {
throw new Error("存在 JeremyTab 属性列为空");
}
if (!("title" in slot.props)) {
throw new Error("JeremyTab 缺少属性 title");
}
if (!("name" in slot.props)) {
throw new Error("JeremyTab 缺少属性 name");
}
});
const titles = slots.map((slot) => slot.props.title);
const names = slots.map((slot) => slot.props.name);
if (!names.includes(props.selected)) {
throw new Error("指定了不存在的 selected 值");
}
const content = computed(() =>
slots.find((slot) => slot.props.name === props.selected)
);
onMounted(() => {
watchEffect(
() => {
if (props.direction === "row") {
const { height } = selectedItem.value.getBoundingClientRect();
indicator.value.style.top = height + "px";
const { width } = selectedItem.value.getBoundingClientRect();
indicator.value.style.width = width + "px";
const left1 = container.value.getBoundingClientRect().left;
const left2 = selectedItem.value.getBoundingClientRect().left;
const left = left2 - left1;
indicator.value.style.left = left + "px";
} else {
const { height } = selectedItem.value.getBoundingClientRect();
indicator.value.style.height = height + "px";
const { width } = selectedItem.value.getBoundingClientRect();
indicator.value.style.left = width + "px";
const top1 = container.value.getBoundingClientRect().top;
const top2 = selectedItem.value.getBoundingClientRect().top;
const top = top2 - top1;
indicator.value.style.top = top + "px";
}
},
{ flush: "post" }
);
});
const select = (index) => {
context.emit("update:selected", names[index]);
}; return {
container,
selectedItem,
indicator,
slots,
titles,
names,
content,
select,
};
},

样式表

最后,再补全样式表

$theme-color: var(--color);
.jeremy-tabs {
display: flex;
flex-direction: column;
position: relative;
&-titles {
display: flex;
}
&-title {
padding: 4px 6px;
border: none;
cursor: pointer;
outline: none;
background: white;
&:focus {
outline: none;
}
&:hover {
color: $theme-color;
}
&.selected {
color: $theme-color;
}
}
&-indicator {
position: absolute;
transition: all 250ms;
border: 1px solid $theme-color;
}
&-divider {
border: 1px solid rgb(184, 184, 184);
}
&-content {
padding: 8px 4px;
}
}
.jeremy-tabs[direction="column"] {
flex-direction: row;
> .jeremy-tabs-titles {
flex-direction: column;
}
> .jeremy-tabs-content {
padding: 2px 10px;
}
}

测试

JeremyTabs 组件引入到测试文档,查看一下运行效果

项目地址

GitHub: https://github.com/JeremyWu917/jeremy-ui

官网地址

JeremyUI: https://ui.jeremywu.top

感谢阅读

10 - Vue3 UI Framework - Tabs 组件的更多相关文章

  1. 05 - Vue3 UI Framework - Button 组件

    官网基本做好了,接下来开始做核心组件 返回阅读列表点击 这里 目录准备 在项目 src 目录下创建 lib 文件夹,用来存放所有的核心组件吧.然后再在 lib 文件夹下创建 Button.vue 文件 ...

  2. 06 - Vue3 UI Framework - Dialog 组件

    做完按钮之后,我们应该了解了遮罩层的概念,接下来我们来做 Dialog 组件! 返回阅读列表点击 这里 需求分析 默认是不可见的,在用户触发某个动作后变为可见 自带白板卡片,分为上中下三个区域,分别放 ...

  3. 08 - Vue3 UI Framework - Input 组件

    接下来再做一个常用的组件 - input 组件 返回阅读列表点击 这里 需求分析 开始之前我们先做一个简单的需求分析 input 组件有两种类型,即 input 和 textarea 类型 当类型为 ...

  4. 09 - Vue3 UI Framework - Table 组件

    接下来做个自定义的表格组件,即 table 组件 返回阅读列表点击 这里 需求分析 开始之前我们先做一个简单的需求分析 基于原生 table 标签的强语义 允许用户自定义表头.表体 可选是否具有边框 ...

  5. 11 - Vue3 UI Framework - Card 组件

    卡片是非常常用也是非常重要的组件,特别是在移动端的众多应用场景中,随便打开一个手机 App ,您会发现充斥着各种各样的卡片. 所以,我们也来制作一个简易的 Card 组件 返回阅读列表点击 这里 需求 ...

  6. 00 - Vue3 UI Framework - 阅读辅助列表

    阅读列表 01 - Vue3 UI Framework - 开始 02 - Vue3 UI Framework - 顶部边栏 03 - Vue3 UI Framework - 首页 04 - Vue3 ...

  7. 01 - Vue3 UI Framework - 开始

    写在前面 一年多没写过博客了,工作.生活逐渐磨平了棱角. 写代码容易,写博客难,坚持写高水平的技术博客更难. 技术控决定慢慢拾起这份坚持,用作技术学习的阶段性总结. 返回阅读列表点击 这里 开始 大前 ...

  8. 04 - Vue3 UI Framework - 文档页

    官网的首页做完了,接下来开始做官网的文档页 返回阅读列表点击 这里 路由设计 先想想我们需要文档页通向哪些地方,这里直接给出我的设计: 所属 子标题 跳转路径 文件名(*.vue) 指南 介绍 /do ...

  9. 12 - Vue3 UI Framework - 打包发布

    基础组件库先做到这个阶段,后面我会继续新增.完善 接下来,我们对之前做的组件进行打包发布到 npm 返回阅读列表点击 这里 组件库优化 通用层叠样式表 我想大家都注意到了,前面我们在写组件的时候,sc ...

随机推荐

  1. [loj3156]回家路线

    令$dp[i]$表示经过第$i$条边后的最小烦躁值,有$且dp[i]=\min_{y_{j}=x_{i}且q_{j}\le p_{i}}dp[j]+f(p_{i}-q_{j})$,其中$f(x)=Ax ...

  2. RestSharp使用说明

    翻译自:https://github.com/restsharp/RestSharp/wiki,转载请注明. 一.新手入门 如果只有少量一次性请求需要封装为API,则可以如下使用RestSharp : ...

  3. 洛谷 P7515 - [省选联考 2021 A 卷] 矩阵游戏(差分约束)

    题面传送门 emmm--怎么评价这个题呢,赛后学完差分约束之后看题解感觉没那么 dl,可是现场为啥就因为种种原因想不到呢?显然是 wtcl( 先不考虑"非负"及" \(\ ...

  4. DTOJ 3999: 游戏

    题目描述这个游戏是这样的,你有一个初始序列S ,你每次可以选择一段任意长度的连续区间,把他们+1 再膜k,给定目标序列,你需要尝试用尽量少的操作次数将初始序列变为目标序列.作为一名优秀的OIer,您认 ...

  5. 【R方差分析】蛋白质表达量多组比较

    初始数据类似: 蛋白质组数据虽不是严格的正态分布,但目前最常用的检验方法还是T检验(两组比较)和方差分析(多组比较).这个话题值得深究,这里不展开. 主要是求多个蛋白的Pvalue值或FDR,用于差异 ...

  6. python18内存管理

  7. printf 的 转义词 -转

    \n    换行 \r    回车键 \b   退后一格 \f    换页 \t    水平制表符 \v   垂直制表符 \a   发出鸣响 \? 插入问号 \"    插入双引号 \'   ...

  8. binlog真的是银弹吗?有些时候也让人头疼

    大家好,我是架构摆渡人.这是实践经验系列的第三篇文章,这个系列会给大家分享很多在实际工作中有用的经验,如果有收获,还请分享给更多的朋友. binlog 用于记录用户对数据库操作的SQL语句信息,同时主 ...

  9. webpack打包报错 ERROR in ./js/ww.js from UglifyJs Unexpected token keyword «function», expected punc «,» [src/page/ww/view/xx/xx.vue:119,0][./js/ww.js:55218,17]

    找了好多解决办法 你可以试着将babel-loader的exclude注释掉,然后看能否打包成功.如果可以,那就是这个问题.你只需要在vue.config.js中配置transpileDependen ...

  10. Redis 高并发解决方案

    针对大流量瞬间冲击,比如秒杀场景 redis前面可以加一层限流 sentinel / Hystrix redis高并发(读多写少)下缓存数据库双写误差: 1. 修改操作使用分布式锁(就是修改的时候加锁 ...