Skip to content

Commit 0952efa

Browse files
committed
feat(hooks): [use-styled-calss-name] add hook to get styled class name
1 parent 086b343 commit 0952efa

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

example/App.vue

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="tsx">
2-
import { styled, ThemeProvider, keyframes } from '@vue3-styled-components/package'
2+
import { styled, ThemeProvider, keyframes, useStyledClassName, withAttrs, css } from '@vue3-styled-components/package'
33
import Component from './Component.vue'
44
import { ref } from 'vue'
55
@@ -35,13 +35,32 @@ const StyledComp5 = styled.div`
3535
animation-name: ${kf};
3636
animation-iteration-count: infinite;
3737
`
38+
39+
const StyledComp6 = styled('button', { color: String })`
40+
width: 40px;
41+
height: 40px;
42+
color: ${(props: any) => props.color};
43+
`
44+
45+
const WithAttrsComp = withAttrs(StyledComp6, { disabled: true })
46+
47+
console.log(useStyledClassName().getStyledClassName(StyledComp6))
48+
49+
const mixin = css`
50+
color: ${(props) => props.color};
51+
`
52+
const StyledComp7 = styled('button', { color: String })`
53+
${mixin}
54+
`
3855
</script>
3956

4057
<template>
4158
<ThemeProvider :theme="theme">
4259
<StyledComp3 @click="update">12345</StyledComp3>
4360
<StyledComp4>12345</StyledComp4>
4461
<StyledComp5>12345</StyledComp5>
62+
<WithAttrsComp color="red">123</WithAttrsComp>
63+
<StyledComp7 color="blue">123</StyledComp7>
4564
</ThemeProvider>
4665
</template>
4766

package/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useStyledClassName'

package/hooks/useStyledClassName.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ComponentInstance, reactive } from 'vue'
2+
3+
const styledClassNameMap = reactive<Record<string, string>>({})
4+
5+
export function useStyledClassName() {
6+
const getStyledClassName = (target: ComponentInstance<any>): string => {
7+
return styledClassNameMap[target.name]
8+
}
9+
10+
return {
11+
getStyledClassName,
12+
styledClassNameMap
13+
}
14+
}

0 commit comments

Comments
 (0)