在 Vue3 中使用 styled-components

2024-05-07 19:36

本文主要是介绍在 Vue3 中使用 styled-components,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

随着组件化时代的兴起,前端应用开始采用组件级别的 CSS 封装:通过 JavaScript 声明和抽象样式,以提高组件的可维护性。在组件加载时动态加载样式,并动态生成类名,从而避免全局污染。 styled-components 是其中的杰出代表。 正如其名称所示,styled-components 以组件的形式声明样式,将样式与组件分离,实现逻辑组件与展示组件的分离。

styled-components 的官方 Vue 版本目前已多年没有更新,而且只支持到 Vue2。那么,在 Vue3 中怎么才能使用到 styled-components 呢?在 Github 翻了一下,大部分复刻 vue-styled-components 的库要么不再更新,要么没有任何文档和说明。

不过还是找到一个质量还可以的库:

vue-styled-components

查看了一下文档,还原了大部分核心 API,使用上与官方 styled-components 差别不大。下面来看看如何使用。

使用

安装

npm i @vvibe/vue-styled-components

styled原生组件

<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components'const StyledLink = styled('a')`color: darkred;
`
</script><template><StyledLink>链接</StyledLink>
</template>

也可以直接链式调用

<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components'const StyledLink = styled.a`color: darkred;
`
</script><template><StyledLink>链接</StyledLink>
</template>

响应式样式

<script setup lang="ts">
import { ref } from 'vue'
import { styled } from '@vvibe/vue-styled-components'const borderColor = ref('darkred')
const inputProps = { borderColor: String }
const StyledInput = styled('input', inputProps)`width: 100%;height: 40px;padding: 4px 8px;border: 1px solid ${(props) => props.borderColor};border-radius: 8px;
`const input = () => (borderColor.value = 'forestgreen')
const focus = () => (borderColor.value = 'skyblue ')
const blur = () => (borderColor.value = 'darkred')
</script><template><StyledInput placeholder="Type something" :borderColor="borderColor" @input="input" @focus="focus" @blur="blur" />
</template>

扩展样式

<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components';const BlueButton = styled.button`width: 120px;height: 40px;margin-right: 8px;padding: 4px 8px;border-radius: 9999px;box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);background-color: skyblue;font-weight: bold;
`;
const RedButton = styled(BlueButton)`background-color: darkred;color: white;
`;
</script><template><BlueButton>Blue Button</BlueButton><RedButton>Red Button</RedButton>
</template>

动画

<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>

主题

<script setup lang="ts">
import { styled, ThemeProvider } from '@vvibe/vue-styled-components'const Wrapper = styled.div`display: flex;justify-content: space-around;
`const StyledLink = styled.a`margin-right: 8px;color: ${(props) => props.theme.primary};font-weight: bold;
`
</script><template><Wrapper><a>This a normal link</a><ThemeProvider :theme="{ primary: 'red' }"><StyledLink>This a theming link</StyledLink></ThemeProvider></Wrapper>
</template>

更多细节查看文档:https://vue-styled-components.com

这篇关于在 Vue3 中使用 styled-components的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4