翼度科技»论坛 编程开发 JavaScript 查看内容

Vue3中使用styled-components的实现

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
前言

随着组件化时代的兴起,前端应用开始采用组件级别的 CSS 封装:通过 JavaScript 声明和抽象样式,以提高组件的可维护性。在组件加载时动态加载样式,并动态生成类名,从而避免全局污染。 styled-components 是其中的杰出代表。 正如其名称所示,styled-components 以组件的形式声明样式,将样式与组件分离,实现逻辑组件与展示组件的分离。
styled-components 的官方 Vue 版本目前已多年没有更新,而且只支持到 Vue2。那么,在 Vue3 中怎么才能使用到 styled-components 呢?在 Github 翻了一下,大部分复刻 vue-styled-components 的库要么不再更新,要么没有任何文档和说明。
不过还是找到一个质量还可以的库:
vue-styled-components
查看了一下文档,还原了大部分核心 API,使用上与官方
  1. styled-components
复制代码
差别不大。下面来看看如何使用。

使用


安装
  1. npm i @vvibe/vue-styled-components
复制代码
styled原生组件
  1. <script setup lang="ts">
  2. import { styled } from '@vvibe/vue-styled-components'

  3. const StyledLink = styled('a')`
  4.   color: darkred;
  5. `
  6. </script>

  7. <template>
  8.   <StyledLink>链接</StyledLink>
  9. </template>
复制代码
也可以直接链式调用
  1. <script setup lang="ts">
  2. import { styled } from '@vvibe/vue-styled-components'

  3. const StyledLink = styled.a`
  4.   color: darkred;
  5. `
  6. </script>

  7. <template>
  8.   <StyledLink>链接</StyledLink>
  9. </template>
复制代码
响应式样式
  1. <script setup lang="ts">
  2. import { ref } from 'vue'
  3. import { styled } from '@vvibe/vue-styled-components'

  4. const borderColor = ref('darkred')
  5. const inputProps = { borderColor: String }
  6. const StyledInput = styled('input', inputProps)`
  7.   width: 100%;
  8.   height: 40px;
  9.   padding: 4px 8px;
  10.   border: 1px solid ${(props) => props.borderColor};
  11.   border-radius: 8px;
  12. `

  13. const input = () => (borderColor.value = 'forestgreen')
  14. const focus = () => (borderColor.value = 'skyblue ')
  15. const blur = () => (borderColor.value = 'darkred')
  16. </script>

  17. <template>
  18.   <StyledInput placeholder="Type something" :borderColor="borderColor" @input="input" @focus="focus" @blur="blur" />
  19. </template>
复制代码
扩展样式
  1. <script setup lang="ts">
  2. import { styled } from '@vvibe/vue-styled-components';

  3. const BlueButton = styled.button`
  4.   width: 120px;
  5.   height: 40px;
  6.   margin-right: 8px;
  7.   padding: 4px 8px;
  8.   border-radius: 9999px;
  9.   box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);
  10.   background-color: skyblue;
  11.   font-weight: bold;
  12. `;
  13. const RedButton = styled(BlueButton)`
  14.   background-color: darkred;
  15.   color: white;
  16. `;
  17. </script>

  18. <template>
  19.   <BlueButton>Blue Button</BlueButton>
  20.   <RedButton>Red Button</RedButton>
  21. </template>
复制代码
动画
  1. <script setup lang="ts">
  2. import { styled, keyframes } from '@vvibe/vue-styled-components'

  3. const rotate = keyframes`
  4.   from {
  5.     transform: rotate(0deg);
  6.   }
  7.   to {
  8.     transform: rotate(360deg);
  9.   }
  10. `
  11. const translate = keyframes`
  12.   0 {
  13.     transform: translateX(0);
  14.   }
  15.   50% {
  16.     transform: translateX(250%);
  17.   }
  18.   60% {
  19.     transform: rotate(360deg);
  20.   }
  21. `

  22. const StyledBaseDiv = styled.div`
  23.   display: inline-block;
  24.   width: 100px;
  25.   height: 100px;
  26. `

  27. const StyledRotateDiv = styled(StyledBaseDiv)`
  28.   background-color: skyblue;
  29.   animation: ${rotate} 2s linear infinite;
  30. `

  31. const StyledTranslateDiv = styled(StyledBaseDiv)`
  32.   margin-left: 10px;
  33.   background-color: darkred;
  34.   animation: ${translate} 2s ease infinite alternate;
  35. `
  36. </script>

  37. <template>
  38.   <StyledRotateDiv />
  39.   <StyledTranslateDiv />
  40. </template>
复制代码
主题
  1. <script setup lang="ts">
  2. import { styled, ThemeProvider } from '@vvibe/vue-styled-components'

  3. const Wrapper = styled.div`
  4.   display: flex;
  5.   justify-content: space-around;
  6. `

  7. const StyledLink = styled.a`
  8.   margin-right: 8px;
  9.   color: ${(props) => props.theme.primary};
  10.   font-weight: bold;
  11. `
  12. </script>

  13. <template>
  14.   <Wrapper>
  15.     <a>This a normal link</a>
  16.     <ThemeProvider :theme="{ primary: 'red' }">
  17.       <StyledLink>This a theming link</StyledLink>
  18.     </ThemeProvider>
  19.   </Wrapper>
  20. </template>
复制代码
更多细节查看文档:https://vue-styled-components.com
到此这篇关于Vue3中使用styled-components的实现的文章就介绍到这了,更多相关Vue3 styled-components内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/javascript/320952q19.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具