花一个月时间为 vue3 重制了 vue-styled-components

本文主要是介绍花一个月时间为 vue3 重制了 vue-styled-components,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

花一个月时间为 vue3 重制了 vue-styled-components

前言

styled-components 在 React 是一个超级热门的 css in js 工具库。其实 styled-components 也有 Vue 版本(vue-styled-components),可惜的是只支持 Vue2,并且该项目已有几年没有更新,作者大概率不会发布 Vue3 版本了。

因此我决定重制一个支持 Vue3 版本的 vue-styled-components,该项目前前后后大概花费了一个月的业余时间,基本实现了 styled-components 的大部分核心功能,不过可能存在部分场景考虑不全面的问题,这个需要拜托广大朋友测试检验一下了,因为不是照搬 原styled-components 的 api,大部分是自己重新实现了的。

项目地址:https://github.com/v-vibe/vue-styled-components

✨特性

✅ 样式化 Vue 组件或样式化组件

✅ 添加默认属性

✅ 传递属性

✅ 支持主题化

✅ 生成关键帧

✅ 生成 CSS 混合

✅ 创建全局样式

✅ 添加或覆盖Attrs

✅ 支持 CSS 嵌套。(仅支持 web: https://drafts.csswg.org/css-nesting/#nesting)

📦安装

npm i @vvibe/vue-styled-components
yarn add @vvibe/vue-styled-components
pnpm i @vvibe/vue-styled-components

🔨使用

样式化组件

<script setup lang="ts">import { styled } from '@vvibe/vue-styled-components';
import OtherComponent from './VueComponent.vue';const StyledDiv = styled('div')`width: 100px;height: 100px;background-color: #ccc;color: #000;
`;const StyledStyledDiv = styled(StyledDiv)`width: 100px;height: 100px;background-color: #000;color: #fff;
`;const StyledOtherComponent = styled(OtherComponent)`width: 100px;height: 100px;background-color: red;color: #fff;
`;</script><template><StyledDiv>Styled Div</StyledDiv><StyledStyledDiv>Styled Styled Div</StyledStyledDiv><StyledOtherComponent>Styled Other Vue Component</StyledOtherComponent>
</template>

Attributes 设置

<script setup lang="ts">import { styled } from '@vvibe/vue-styled-components';const StyledDiv = styled.div.attrs({class: 'styled-div'
})`width: 100px;height: 100px;background-color: #ccc;color: #000;
`;
</script><template><StyledDiv>Styled Div</StyledDiv><!-- <div class="styled-div">Styled Div</div> -->
</template>

通过 Props 动态控制样式

<script setup lang="ts">import { styled } from '@vvibe/vue-styled-components';const StyledDiv = styled('div', {color: '#fff'
})`width: 100px;height: 100px;background-color: #ccc;color: ${(props) => props.color};
`;
</script><template><StyledDiv>Styled Div</StyledDiv>
</template>

主题

<script setup lang="ts">import { styled, ThemeProvider } from '@vvibe/vue-styled-components';const StyledDiv = styled.div`width: 100px;height: 100px;background-color: #ccc;color: ${(props) => props.theme.color};
`;
</script><template><ThemeProvider :theme="{ color: '#fff' }"><StyledDiv>Styled Div</StyledDiv></ThemeProvider>
</template>

生成 keyframes

您可以使用 keyframes 函数来定义关键帧动画,然后使用 keyframes 的返回值将其应用于样式化组件。

<script setup lang="ts">import { styled, keyframes } from '@vvibe/vue-styled-components';const rotate = keyframes`from {transform: rotate(0deg);}to {transform: rotate(360deg);}
`;const translate = keyframes`0 {transform: translateX(0);}50% {transform: translateX(250%);}60% {transform: rotate(360deg);}
`;const StyledBaseDiv = styled.div`display: inline-block;width: 100px;height: 100px;
`;  const StyledRotateDiv = styled(StyledBaseDiv)`background-color: skyblue;animation: ${rotate} 2s linear infinite;
`;const StyledTranslateDiv = styled(StyledBaseDiv)`margin-left: 10px;background-color: darkred;animation: ${translate} 2s ease infinite alternate;
`;
</script><template><StyledRotateDiv /><StyledTranslateDiv />
</template>

Create Global Style

一个用于创建全局样式的函数。

<script setup>import { createGlobalStyle } from '@vvibe/vue-styled-components';const GlobalStyle = createGlobalStyle`body {color: ${(props) => props.color};}
`;
</script><template><GlobalStyle color="white" />
</template>

Generate CSS Mixin

一个用于从带有插值的模板字符串生成 CSS 的函数。

<script setup lang="ts">import { styled, css } from '@vvibe/vue-styled-components';const mixin = css`color: red;background-color: blue;
`;const DivWithStyles = styled('div')`${mixin}
`;
</script><template><DivWithStyles>Div with mixin</DivWithStyles>
</template>

添加或覆盖 Attributes

一个向 ComponentInstance or HTMLElements 添加或覆盖 Attributes 的函数.

<script setup lang="ts">import { withAttrs } from '@vvibe/vue-styled-components';const DivWithAttrs = withAttrs('div', {class: 'div-with-attrs'
});const DivWithAttrs2 = withAttrs(DivWithAttrs, {class: 'div-with-attrs-2'
});
</script><template><DivWithAttrs>Div with attrs</DivWithAttrs><DivWithAttrs2>Div with attrs 2</DivWithAttrs2>
</template>
<style scope>
.div-with-attrs {color: red;
}.div-with-attrs-2 {color: blue;
}
</style>

更多细节请查看 官方文档

这篇关于花一个月时间为 vue3 重制了 vue-styled-components的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/963456

相关文章

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行