花一个月时间为 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,大部分是自己重新实现了的。

大家觉得可以的话点点star支持下😄

项目地址: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>

更多细节请查看 官方文档

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/599236.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

RT-DETR-20240507周更说明|更新Inner-IoU、Focal-IoU、Focaler-IoU等数十种IoU计算方式

RT-DETR改进专栏|包含主干、模块、注意力、损失函数等改进 专栏介绍 本专栏包含模块、卷积、检测头、损失等深度学习前沿改进,目前已有改进点70&#xff01;每周更新。 20240507更新说明&#xff1a; ⭐⭐ 更新CIoU、DIoU、MDPIoU、GIoU、EIoU、SIoU、ShapeIou、PowerfulIoU、…

聚乙烯醇(PVA)纳米纤维膜的性能介绍

聚乙烯醇&#xff08;PVA&#xff09;纳米纤维膜是一种由聚乙烯醇&#xff08;PVA&#xff09;分子组成的纳米级薄膜。这种材料具有许多优良的性能和应用前景&#xff0c;具体如下&#xff1a; 制备工艺&#xff1a;聚乙烯醇纳米纤维膜可以通过静电纺丝等方法制备。具体步骤包括…

网络知识点之—QoS

QoS&#xff08;Quality of Service&#xff0c;服务质量&#xff09;指一个网络能够利用各种基础技术&#xff0c;为指定的网络通信提供更好的服务能力&#xff0c;是网络的一种安全机制&#xff0c; 是用来解决网络延迟和阻塞等问题的一种技术。QoS的保证对于容量有限的网络来…

5000A信号发生器使用方法

背景 gnss工作需要使用的5000A&#xff0c;所以做成文档&#xff0c;用于其他员工学习。 下载星历数据 https://cddis.nasa.gov/archive/gnss/data/daily/2024/brdc/ 修改daily中的年份&#xff0c;就可以获取相关截至时间的星历数据 brcd数据格式 第一行记录了卫星的PRN号&a…

科创板门槛升级!解析中国量子企业的上市之路与国际比拼

4月30日晚&#xff0c;中国证监会于发布了修订后的《科创属性评价指引&#xff08;试行&#xff09;》&#xff08;以下简称“新指引”&#xff09;&#xff0c;该指引自发布日起正式生效。本次修订对原有指引中的部分标准进行了调整&#xff0c;具体如下&#xff1a; 1&#x…

远程桌面连接不上,远程桌面连接不上的专业解决策略

在信息技术领域&#xff0c;远程桌面连接是一种非常重要的工具&#xff0c;它允许用户从任何地点、任何时间访问和操作远程计算机。然而&#xff0c;当远程桌面连接出现问题时&#xff0c;可能会严重影响工作效率。以下是一些可能导致远程桌面连接不上的原因以及相应的解决方案…

干货分享-策划人都在用的活动策划网站

职场上&#xff0c;学会借力&#xff0c;学会‘抄’&#xff0c;比辛辛苦苦做老黄牛&#xff0c;更能事倍功半&#xff0c;不仅自己省事省力&#xff0c;还能获得更多升职加薪的机会。 那么&#xff0c;职场新人如何快速的写出一份领导满意的方案&#xff1f; 今天分享的‘抄…

深度学习之基于Vgg19预训练卷积神经网络图像风格迁移系统

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 一、项目背景 在数字艺术和图像处理领域&#xff0c;图像风格迁移技术一直备受关注。该技术可以将一幅图像的内容和…

动态内存开辟(下)

前言 动态内存开辟以及柔性数组的介绍 一、 几个经典的笔试题 1. 题目一 void Getmemory(char*p) {p (char*)malloc(100); } int main() {char* str NULL;Getmemory(str);strcpy(str, "hello world");printf(str);return 0; } 这段代码我们可以发现两个很明显…

解决VScode -正在本地下载 VS Code 服务器

不知道怎么回事再次连接服务器的时候一直卡在这里了&#xff0c;查看输出信息发现一直卡在下载处&#xff0c;报错信息如图1&#xff0c;输出信息如图2。 1.报错信息 图1 报错信息 图2 输出信息 2.尝试 【已解决】设置SSH主机&#xff1a;VS Code-正在本地下载 VS Code 服务器…

Linux 第二十二章

&#x1f436;博主主页&#xff1a;ᰔᩚ. 一怀明月ꦿ ❤️‍&#x1f525;专栏系列&#xff1a;线性代数&#xff0c;C初学者入门训练&#xff0c;题解C&#xff0c;C的使用文章&#xff0c;「初学」C&#xff0c;linux &#x1f525;座右铭&#xff1a;“不要等到什么都没有了…

为何大学计算机专业以C语言入门?探究C语言入门的好处

为何大学计算机专业以C语言入门&#xff1f;探究C语言入门的好处 在大学计算机专业中&#xff0c;C语言常作为入门语言&#xff0c;这并非偶然。C语言具有一些独特的优势&#xff0c;使其成为计算机科学教育中的理想选择。本文将探讨为何大学计算机专业选择以C语言入门&#xf…

Terraform资源

资源是Terraform中最核心的部分&#xff0c;使用Terraform的目的就是用于管理资源。 在Terraform中&#xff0c;资源使用resource块定义。 一个resource可以定义一个或多个基础设施资源对象&#xff0c;如&#xff1a;VPC&#xff0c;虚拟机&#xff0c;DNS记录&#xff0c;Con…

为什么都喜欢用串口通讯?那为什么还用RS485,SPI和I2C?

1、为什么都喜欢用串口通讯&#xff1f; 之前在做单片机产品的时候&#xff0c;用的最多的就是串口通讯&#xff0c;凡是单片机的外设&#xff0c;优先选用带串口功能的&#xff0c;比如蓝牙模块&#xff0c;WIFI模块&#xff0c;4G模块&#xff0c;电表和显示屏等等。 为什么都…

mysql的数据结构及索引使用情形

先来说下数据的一般存储方式&#xff1a;内存(适合小数据量)、磁盘(大数据量)。 磁盘的运转方式&#xff1a;速度 旋转&#xff0c;磁盘页的概念&#xff1a;每一页大概16KB。 1、存储结构 哈希 是通过hash函数计算出一个hash值的&#xff0c;哈希的优点就是查找的时间复杂度…

直说了,你可能从没真正理解MPLS

号主&#xff1a;老杨丨11年资深网络工程师&#xff0c;更多网工提升干货&#xff0c;请关注公众号&#xff1a;网络工程师俱乐部 你们好&#xff0c;我的网工朋友。 尽管 MPLS 技术已经相当成熟&#xff0c;有关它的文章数不胜枚举&#xff0c;涵盖了从基本原理到 SR-MPLS 等…

Spring Boot集成RabbitMQ-之6大模式总结

A.集成 一&#xff1a;添加依赖 在pom.xml文件中添加spring-boot-starter-amqp依赖&#xff0c;以便使用Spring Boot提供的RabbitMQ支持&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp&…

【第三版 系统集成项目管理工程师】第2章 信息技术发展(知识总结)

持续更新。。。。。。。。。。。。。。。 【第2章】 信息技术发展 考情分析2. 1信息技术及其发展2.1.1 计算机软硬件-P501.计算机硬件2.计算机软件-P51 2.1.2计算机网络1.通信基础-P522.网络基础-P534.网络标准协议-P543.网络设备-P535.软件定义网络-P576.第五代移动通信技术-P…

2024抖音小店最新注册流程来了,快快收藏!

大家好&#xff0c;我是电商糖果 2024年想开一家抖音小店&#xff0c;但是不知道具体的开店流程。 不要着急&#xff0c;这篇文章就给大家详细的讲解一下。 首先&#xff0c;准备开店材料&#xff1a;5000左右的类目保证金&#xff0c;电脑&#xff0c;手机号&#xff0c;法…

Dos命令Tree

查看tree的用法 tree /?tree > 文件名&#xff0c;输出文件路径到指定的位置
最新文章