Skip to content

Commit b0a8b09

Browse files
committed
docs(attrs): add attrs usage
1 parent d6c4db1 commit b0a8b09

File tree

8 files changed

+159
-19
lines changed

8 files changed

+159
-19
lines changed

packages/docs/.vitepress/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function sidebarGuide() {
4343
items: [
4444
{ text: 'Quick Start', link: 'quick-start' },
4545
{ text: 'Passing Props', link: 'passing-props' },
46+
{ text: 'Attrs Setting', link: 'attrs' },
4647
{ text: 'Extending Styles', link: 'extending-styles' },
4748
{ text: 'Styling Any Component', link: 'styling-any-component' },
4849
{ text: 'Animations', link: 'animations' },

packages/docs/.vitepress/zh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function sidebarGuide() {
6767
items: [
6868
{ text: '快速开始', link: 'quick-start' },
6969
{ text: '传递 props', link: 'passing-props' },
70+
{ text: 'Attrs设置', link: 'attrs' },
7071
{ text: '扩展样式', link: 'extending-styles' },
7172
{ text: '样式化任意组件', link: 'styling-any-component' },
7273
{ text: '动画', link: 'animations' },

packages/docs/guide/api/core.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ It is used for passing attributes to styled component.
6969

7070
**Augments**
7171

72-
- Attrs Object, `Record<string, number | string | boolean>`, `required`
72+
- Attrs Object or Factory Function, `Record<string, any> | ((props: Context) => Record<string, any>)`, `required`
7373

7474
**Return**
7575

@@ -81,13 +81,17 @@ It is used for passing attributes to styled component.
8181
<script setup lang="ts">
8282
import { styled } from '@vue-styled-components/core'
8383
84-
const InputWithValue = styled.input.attrs({ value: "I'm input with default value. 🥺" })`
85-
width: 100%;
86-
height: 40px;
87-
`
84+
const InputWithValue = styled
85+
.input
86+
.attrs({ value: "I'm input with default value. 🥺" })`
87+
width: 100%;
88+
height: 40px;
89+
`
90+
91+
const StyledInput = styled('input', { canInput: Boolean })
92+
.attrs(props => { disabled: !props.canInput })`
93+
width: 100%;
94+
height: 40px;
95+
`
8896
</script>
89-
90-
<template>
91-
<InputWithValue type="text" />
92-
</template>
9397
```

packages/docs/guide/basic/attrs.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Attributes setting
2+
3+
You can use the `.attrs` prop to set attributes to the styled components. support passing a attrs function or an object.
4+
5+
Here is an example:
6+
7+
:::demo
8+
```vue
9+
<script setup lang="ts">
10+
import { styled } from '@vue-styled-components/core'
11+
12+
const InputWithValue = styled
13+
.input
14+
.attrs({ value: "I'm input with default value. 🥺" })`
15+
width: 100%;
16+
height: 40px;
17+
color: #333;
18+
`
19+
20+
const StyledInput = styled(InputWithValue, { canInput: Boolean })
21+
.attrs(props => ({
22+
disabled: !props.canInput,
23+
style: {
24+
background: !props.canInput ? '#f1f1f1' : '#fff',
25+
color: !props.canInput ? '#999' : '#000'
26+
}
27+
}))`
28+
padding: 0 8px;
29+
border-radius: 4px;
30+
`
31+
</script>
32+
33+
<template>
34+
<InputWithValue />
35+
<StyledInput :canInput="false" />
36+
</template>
37+
```
38+
:::

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

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

5050
:::
5151

52+
## Use `attributes`
53+
54+
:::demo
55+
```vue
56+
<script setup lang="ts">
57+
import { styled } from '@vue-styled-components/core'
58+
59+
const StyledInput = styled
60+
.input
61+
.attrs({ disabled: true })`
62+
width: 100%;
63+
height: 40px;
64+
padding: 4px 8px;
65+
border: 1px solid #ccc;
66+
border-radius: 8px;
67+
cursor: ${ props => props.disabled ? 'not-allowed' : 'pointer'};
68+
`
69+
</script>
70+
71+
<template>
72+
<StyledInput value="Type something..." :canInput="false" />
73+
</template>
74+
```
75+
:::
76+
77+
5278
## New way to pass props
5379

5480
From `v1.7.0`, you can use the `props` option to pass props to the styled component.

packages/docs/zh/guide/api/core.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const StyledDiv = styled.div`
6969

7070
**参数**
7171

72-
- 属性对象`Record<string, number | string | boolean>``必需`
72+
- Attrs 对象或构造函数`Record<string, any> | ((props: Context) => Record<string, any>)``必需`
7373

7474
**返回值**
7575

@@ -80,15 +80,19 @@ const StyledDiv = styled.div`
8080

8181
```vue
8282
<script setup lang="ts">
83-
import { styled } from '@vue-styled-components/core'
83+
import { styled } from '@vue-styled-components/core'
8484
85-
const InputWithValue = styled.input.attrs({ value: "I'm input with default value. 🥺" })`
86-
width: 100%;
87-
height: 40px;
88-
`
85+
const InputWithValue = styled
86+
.input
87+
.attrs({ value: "I'm input with default value. 🥺" })`
88+
width: 100%;
89+
height: 40px;
90+
`
91+
92+
const StyledInput = styled('input', { canInput: Boolean })
93+
.attrs(props => { disabled: !props.canInput })`
94+
width: 100%;
95+
height: 40px;
96+
`
8997
</script>
90-
91-
<template>
92-
<InputWithValue type="text" />
93-
</template>
9498
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Attributes 设置
2+
3+
你可以使用 `.attrs` 属性来设置组件的属性。支持传入一个 attrs 函数或者一个对象。
4+
5+
以下是一个使用例子:
6+
7+
:::demo
8+
```vue
9+
<script setup lang="ts">
10+
import { styled } from '@vue-styled-components/core'
11+
12+
const InputWithValue = styled
13+
.input
14+
.attrs({ value: "I'm input with default value. 🥺" })`
15+
width: 100%;
16+
height: 40px;
17+
color: #333;
18+
`
19+
20+
const StyledInput = styled(InputWithValue, { canInput: Boolean })
21+
.attrs(props => ({
22+
disabled: !props.canInput,
23+
style: {
24+
background: !props.canInput ? '#f1f1f1' : '#fff',
25+
color: !props.canInput ? '#999' : '#000'
26+
}
27+
}))`
28+
padding: 0 8px;
29+
border-radius: 4px;
30+
`
31+
</script>
32+
33+
<template>
34+
<InputWithValue />
35+
<StyledInput :canInput="false" />
36+
</template>
37+
```
38+
:::

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

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

77
您可以向样式化组件传递属性,类似于 Vue 组件。例如,您可以向样式化的输入框传递一个占位符。
88

9+
## 基础用法
10+
911
:::demo
1012

1113
```vue
@@ -44,6 +46,32 @@ const blur = () => (borderColor.value = 'darkred')
4446
:::
4547

4648

49+
## 使用 `attributes`
50+
51+
:::demo
52+
```vue
53+
<script setup lang="ts">
54+
import { styled } from '@vue-styled-components/core'
55+
56+
const StyledInput = styled
57+
.input
58+
.attrs({ disabled: true })`
59+
width: 100%;
60+
height: 40px;
61+
padding: 4px 8px;
62+
border: 1px solid #ccc;
63+
border-radius: 8px;
64+
cursor: ${ props => props.disabled ? 'not-allowed' : 'pointer'};
65+
`
66+
</script>
67+
68+
<template>
69+
<StyledInput value="Type something..." :canInput="false" />
70+
</template>
71+
```
72+
:::
73+
74+
4775
## 新的 props 选项
4876

4977
`v1.7.0` 开始,您可以使用 `props` 选项将属性传递给样式化的组件。

0 commit comments

Comments
 (0)