Skip to content

Commit 9f67504

Browse files
author
Tenny
committed
fix(Card): 修复暗黑模式下样式显示异常
Signed-off-by: Tenny <joel.shu@qq.com>
1 parent 011a02e commit 9f67504

File tree

6 files changed

+98
-41
lines changed

6 files changed

+98
-41
lines changed

docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export default defineConfig({
5858
text: '工具样式',
5959
link: '/css-util',
6060
},
61+
{
62+
text: '暗黑模式',
63+
link: '/dark',
64+
},
6165
],
6266
},
6367
{

docs/components/card.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ import { Card, Button } from '../../src'
9999
<div>卡片内容</div>
100100
</template>
101101
<template v-slot:header-extra>
102-
<nt-button type="text">按钮</nt-button>
102+
<nt-button text>按钮</nt-button>
103103
</template>
104104
<template v-slot:footer>
105105
<span style="color: orange">卡片标题</span>
@@ -115,7 +115,7 @@ import { Card, Button } from '../../src'
115115
<div>卡片内容</div>
116116
</template>
117117
<template v-slot:header-extra>
118-
<Button type="text">按钮</Button>
118+
<Button text>按钮</Button>
119119
</template>
120120
<template v-slot:footer>
121121
<span style="color: orange">卡片标题</span>
@@ -139,23 +139,23 @@ import { Card, Button } from '../../src'
139139
<div>卡片内容1</div>
140140
</template>
141141
<template v-slot:header-extra>
142-
<nt-button type="text">按钮</nt-button>
142+
<nt-button text>按钮</nt-button>
143143
</template>
144144
</nt-card>
145145
<nt-card header-text="卡片标题2">
146146
<template v-slot:default>
147147
<div>卡片内容2</div>
148148
</template>
149149
<template v-slot:header-extra>
150-
<nt-button type="text">按钮</nt-button>
150+
<nt-button text>按钮</nt-button>
151151
</template>
152152
</nt-card>
153153
<nt-card header-text="卡片标题3">
154154
<template v-slot:default>
155155
<div>卡片内容3</div>
156156
</template>
157157
<template v-slot:header-extra>
158-
<nt-button type="text">按钮</nt-button>
158+
<nt-button text>按钮</nt-button>
159159
</template>
160160
</nt-card>
161161
</div>
@@ -167,23 +167,23 @@ import { Card, Button } from '../../src'
167167
<div>卡片内容1</div>
168168
</template>
169169
<template v-slot:header-extra>
170-
<Button type="text">按钮</Button>
170+
<Button text>按钮</Button>
171171
</template>
172172
</Card>
173173
<Card header-text="卡片标题2">
174174
<template v-slot:default>
175175
<div>卡片内容2</div>
176176
</template>
177177
<template v-slot:header-extra>
178-
<Button type="text">按钮</Button>
178+
<Button text>按钮</Button>
179179
</template>
180180
</Card>
181181
<Card header-text="卡片标题3">
182182
<template v-slot:default>
183183
<div>卡片内容3</div>
184184
</template>
185185
<template v-slot:header-extra>
186-
<Button type="text">按钮</Button>
186+
<Button text>按钮</Button>
187187
</template>
188188
</Card>
189189
</div>

docs/dark.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## 暗黑模式
2+
3+
组件库内置暗色的主题,你可以轻易的切换到暗色。
4+
5+
### 切换暗黑模式
6+
7+
组件库通过给根 `html` 添加 `dark``class` 类来标明当前的主题,所以你只要修改这个属性,即可完成主题的切换。
8+
9+
```js
10+
document.documentElement.classList.toggle('dark');
11+
```
12+
13+
> 当然也可以通过内置的组件 [Theme](/components/theme) 或者通过 `ph-utils/theme` 模块的 `applyTheme、initTheme` 来实现暗黑模式切换

docs/usage.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ import './style.css';
8181

8282
通过上面就能修改,当然也能在侧边栏的样式的地方,手动覆盖变量也能实现
8383

84+
### 修改主题色
85+
86+
通常只需要覆盖 `--nt-primary-color` 相关 `CSS` 变量就能实现主题色覆盖,例如: `--nt-primary-color``--nt-primary-color-light1`
87+
88+
```css
89+
:root {
90+
--nt-primary-color: #722ed1;
91+
--nt-primary-color-light1: #9254de;
92+
--nt-primary-color-light2: #b37feb;
93+
--nt-primary-color-light3: #d3adf7;
94+
--nt-primary-color-light4: #efdbff;
95+
--nt-primary-color-light5: #f9f0ff;
96+
--nt-primary-color-dark1: #531dab;
97+
}
98+
```
99+
100+
> 也可以通过 `ph-utils/theme` 工具类的 `initColorTheme、toggleColorTheme、applyColorTheme` 函数来实现主题色切换
101+
84102
### 修改组件样式
85103

86104
组件所有的样式都基本只有一层,所以如果要修改样式,在需要套一个层级就能修改

src/components/Card.vue

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
1-
<template>
2-
<div class="nt-card">
3-
<div v-if="showHeader" class="nt-card__header">
4-
<span v-if="headerText">{{ headerText }}</span>
5-
<template v-else>
6-
<slot name="header"></slot>
7-
</template>
8-
<div><slot name="header-extra"></slot></div>
9-
</div>
10-
<div :class="['nt-card__body', bodyClass || '']">
11-
<slot></slot>
12-
</div>
13-
<div v-if="showFooter" class="nt-card__footer">
14-
<span v-if="footerText">{{ footerText }}</span>
15-
<template v-else><slot name="footer"></slot></template>
16-
</div>
17-
</div>
18-
</template>
19-
<script setup lang="ts">
20-
withDefaults(
21-
defineProps<{
22-
headerText?: string;
23-
showHeader?: boolean;
24-
footerText?: string;
25-
showFooter?: boolean;
26-
bodyClass?: string;
27-
}>(),
28-
{
29-
showHeader: true,
30-
showFooter: false,
1+
<script lang="ts">
2+
import { defineComponent, h } from 'vue';
3+
4+
export default defineComponent({
5+
props: {
6+
headerText: String,
7+
showHeader: {
8+
type: Boolean,
9+
default: true,
10+
},
11+
footerText: String,
12+
showFooter: { type: Boolean, default: false },
13+
bodyClass: String,
14+
headerClass: String,
15+
footerClass: String,
3116
},
32-
);
17+
setup(props, { slots }) {
18+
return () =>
19+
h('div', { class: 'nt-card' }, [
20+
props.showHeader
21+
? h('div', { class: ['nt-card__header', props.headerClass] }, [
22+
slots.header != null
23+
? slots.header()
24+
: h('span', props.headerText),
25+
slots['header-extra'] != null
26+
? h('div', slots['header-extra']())
27+
: undefined,
28+
])
29+
: undefined,
30+
h('div', { class: ['nt-card__body', props.bodyClass] }, [
31+
slots.default != null ? slots.default() : undefined,
32+
]),
33+
props.showFooter
34+
? h(
35+
'div',
36+
{ class: ['nt-card__footer', props.footerClass] },
37+
slots.footer != null
38+
? slots.footer()
39+
: h('span', props.footerText),
40+
)
41+
: undefined,
42+
]);
43+
},
44+
});
3345
</script>

style/card/index.css

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
.nt-card {
2+
--nt-background-color: #ffffff;
3+
--nt-text-color: #333;
24
border-radius: 4px;
35
border: 1px solid var(--nt-border-color);
4-
background-color: #ffffff;
5-
color: #333;
6+
background-color: var(--nt-background-color);
7+
color: var(--nt-text-color);
68
box-shadow:
79
0 1px 3px 0 rgb(0 0 0 / 0.1),
810
0 1px 2px -1px rgb(0 0 0 / 0.1);
@@ -24,3 +26,11 @@
2426
.nt-card__footer {
2527
padding: 15px;
2628
}
29+
.nt-header-extra:empty {
30+
display: none;
31+
}
32+
html.dark .nt-card {
33+
--nt-background-color: #232324;
34+
--nt-text-color: rgba(255, 255, 255, 0.7);
35+
--nt-border-color: rgb(72, 72, 73);
36+
}

0 commit comments

Comments
 (0)