Skip to content

Commit cc5c1b9

Browse files
authored
Merge pull request #43 from vue-styled-components/feat/plugins
Feat/plugins
2 parents 40a8ca9 + e40d526 commit cc5c1b9

File tree

13 files changed

+548
-65
lines changed

13 files changed

+548
-65
lines changed

.vscode/settings.json

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
11
{
2-
"typescript.tsdk": "node_modules/typescript/lib"
2+
"typescript.tsdk": "node_modules/typescript/lib",
3+
// Disable the default formatter
4+
"prettier.enable": false,
5+
"editor.formatOnSave": false,
6+
7+
// Auto fix
8+
"editor.codeActionsOnSave": {
9+
"source.fixAll.eslint": "explicit"
10+
},
11+
12+
// Silent the stylistic rules in you IDE, but still auto fix them
13+
"eslint.rules.customizations": [
14+
{ "rule": "@stylistic", "severity": "off" },
15+
{ "rule": "*-indent", "severity": "off" },
16+
{ "rule": "*-spacing", "severity": "off" },
17+
{ "rule": "*-spaces", "severity": "off" },
18+
{ "rule": "*-order", "severity": "off" },
19+
{ "rule": "*-dangle", "severity": "off" },
20+
{ "rule": "*-newline", "severity": "off" },
21+
{ "rule": "*quotes", "severity": "off" },
22+
{ "rule": "*semi", "severity": "off" }
23+
],
24+
25+
"eslint.validate": [
26+
"javascript",
27+
"javascriptreact",
28+
"typescript",
29+
"typescriptreact",
30+
"vue",
31+
"html",
32+
"markdown",
33+
"json",
34+
"jsonc",
35+
"yaml"
36+
]
337
}

README.zh_CN.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
[Changelog](./CHANGELOG.md) · [English](./README.md) · 中文
2626
</div>
2727

28-
> [!IMPORTANT]
29-
> 这个库已经迁移到新的npm组织 [@vue-styled-components/core](https://npmjs.org/package/@vue-styled-components/core)
30-
> 旧的npm组织 已经废弃,不再发布新版本
31-
3228
## ✨特性
3329

3430
✅ 样式化 Vue 组件或原生组件

eslint.config.mjs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import antfu from '@antfu/eslint-config'
22

3-
export default antfu({
4-
markdown: false,
5-
ignores: ['**/node_modules/**', '**/dist/**', '**/build/**', '**/coverage/**'],
6-
})
3+
export default antfu(
4+
{
5+
markdown: false,
6+
ignores: [
7+
'**/node_modules/**',
8+
'**/dist/**',
9+
'**/build/**',
10+
'**/coverage/**',
11+
],
12+
},
13+
)

packages/core/src/styled.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ interface StyledComponent<T extends object> {
3434
<P>(
3535
styles: TemplateStringsArray,
3636
...expressions: (
37-
| ExpressionType<BaseContext<P & ExtractPropTypes<PropsDefinition<T>>>>
38-
| ExpressionType<BaseContext<P & ExtractPropTypes<PropsDefinition<T>>>>[]
37+
| ExpressionType<BaseContext<P & T>>
38+
| ExpressionType<BaseContext<P & T>>[]
3939
)[]
40-
): DefineSetupFnComponent<{ as?: string, props?: P } & ExtractPropTypes<PropsDefinition<T>> & HTMLAttributes>
40+
): DefineSetupFnComponent<{ as?: string, props?: P } & ExtractPropTypes<PropsDefinition<T & P>> & HTMLAttributes>
4141

4242
attrs: <A = object>(
43-
attrs: A | ((props: ExtractPropTypes<PropsDefinition<T>>) => A),
43+
attrs: object | ((props: PropsDefinition<T> & A) => object),
4444
) => StyledComponent<A & ExtractPropTypes<PropsDefinition<T>>>
45+
46+
// 支持泛型参数的类型定义
47+
<P extends object>(props: PropsDefinition<P>): StyledComponent<P & T>
4548
}
4649

4750
function baseStyled<T extends object>(target: string | InstanceType<any>, propsDefinition?: PropsDefinition<T>): StyledComponent<T> {
@@ -50,17 +53,25 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
5053
}
5154
let defaultAttrs: unknown
5255
function styledComponent<P>(
53-
styles: TemplateStringsArray,
56+
stylesOrProps: TemplateStringsArray | PropsDefinition<P>,
5457
...expressions: (
5558
| ExpressionType<BaseContext<P & ExtractPropTypes<PropsDefinition<T>>>>
5659
| ExpressionType<BaseContext<P & ExtractPropTypes<PropsDefinition<T>>>>[]
5760
)[]
5861
) {
59-
const cssStringsWithExpression = insertExpressions(styles, expressions)
62+
// 处理泛型参数的情况,如 styled.div<Props>
63+
if (!Array.isArray(stylesOrProps)) {
64+
return baseStyled(target, { ...propsDefinition, ...stylesOrProps } as PropsDefinition<T & P>) as StyledComponent<T & P>
65+
}
66+
67+
// 正常的样式模板字符串处理
68+
const cssStringsWithExpression = insertExpressions(stylesOrProps as TemplateStringsArray, expressions)
6069
return createStyledComponent<P>(cssStringsWithExpression)
6170
}
6271

63-
styledComponent.attrs = function <A extends object>(attrs: A) {
72+
styledComponent.attrs = function <A = object>(
73+
attrs: object | ((props: ExtractPropTypes<PropsDefinition<T>> & A) => object),
74+
) {
6475
defaultAttrs = attrs
6576
return styledComponent
6677
}
@@ -174,11 +185,14 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
174185
return styledComponent as StyledComponent<T>
175186
}
176187

177-
/** Append all the supported HTML elements to the styled properties */
178-
const styled = baseStyled as typeof baseStyled & {
179-
[E in SupportedHTMLElements]: ReturnType<typeof baseStyled>
188+
// 为styled添加attrs方法的类型定义
189+
type StyledInterface = typeof baseStyled & {
190+
[E in SupportedHTMLElements]: StyledComponent<object>
180191
}
181192

193+
/** Append all the supported HTML elements to the styled properties */
194+
const styled = baseStyled as StyledInterface
195+
182196
domElements.forEach((domElement: SupportedHTMLElements) => {
183197
styled[domElement] = baseStyled(domElement)
184198
})

packages/docs/guide/basic/passing-props.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ fallthrough attributes.(see [Props Declaration](https://vuejs.org/guide/componen
4949

5050
:::
5151

52+
**So we provide a vite plugin to support the generic type props same as `styled-components`. You can install the plugin [`@vue-styled-components/plugin`](https://github.com/vue-styled-components/plugin) to allow the following usage:**
53+
54+
```ts
55+
const StyledDiv = styled.div<color: string>`
56+
color: ${props => props.color}
57+
`
58+
```
59+
5260
## Use `attributes`
5361

5462
:::demo

packages/docs/guide/basic/quick-start.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ outline: deep
66

77
## Installation
88

9+
### Core
10+
911
Npm
1012

1113
```shell
@@ -24,6 +26,42 @@ Pnpm
2426
pnpm i @vue-styled-components/core
2527
```
2628

29+
### Vite Plugin
30+
31+
Npm
32+
33+
```shell
34+
npm i @vue-styled-components/plugin
35+
```
36+
37+
Yarn
38+
39+
```shell
40+
yarn add @vue-styled-components/plugin
41+
```
42+
43+
Pnpm
44+
45+
```shell
46+
pnpm i @vue-styled-components/plugin
47+
```
48+
49+
Vite Config
50+
51+
```ts
52+
import vueStyledPlugin from "@vue-styled-components/plugin"
53+
// ...
54+
55+
export default defineConfig({
56+
// ...
57+
plugins: [
58+
vueStyledPlugin()
59+
// ...
60+
]
61+
})
62+
```
63+
64+
2765
## Getting Started
2866

2967
:::demo

0 commit comments

Comments
 (0)