Skip to content

Commit 2c4ab0c

Browse files
author
snailRun
committed
Merge remote-tracking branch 'origin/main'
2 parents c831f2e + ee71245 commit 2c4ab0c

File tree

4 files changed

+5422
-4124
lines changed

4 files changed

+5422
-4124
lines changed

data/blog/post-16.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ tags: ["Next.js","踩坑记录"]
5757
// next.config.js
5858
experimental: {
5959
serverActions: {
60-
allowedOrigins: ["www.mianshiya.com"],
60+
allowedOrigins: ["www.xxxx.com"],
6161
},
6262
},
6363
```

data/blog/post-17.mdx

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
---
2+
title: css 样式污染几种解决方案
3+
date: 2024-09-01T15:25:56Z
4+
slug: post-17
5+
author: chaseFunny:https://github.com/chaseFunny
6+
tags: ["bug","Next.js","CSS"]
7+
---
8+
9+
# 背景
10+
11+
在最近项目开发(Next.js + antd )中,开发页面的时候遇到了两个问题
12+
13+
1)样式优先级问题
14+
15+
我本地开发好好的,但是部署到线上,我的样式都不见了,打开控制台一看,发现 antd 的样式优先级很高,导致我的样式代码都不生效了,但是在本地我的样式优先级却更高一点,目前解决方案就是不断写 `!important` ,这是最糟糕的解决方案,
16+
17+
2)样式污染问题
18+
19+
在协同开发中,我一不小心就定义了一个可能比较通用的样式类名,例如:.poster-title。然后另一个小伙伴也使用了这个类名,而且我们都没有更高的一个父类名,那么就会出现样式污染
20+
21+
那我就想能不能通过配置或者其他方式解决这个问题,下面就开始探索一下如何解决样式污染和样式优先级问题
22+
23+
# 样式污染解决方案
24+
25+
由于 CSS 默认是没有模块的概念,那解决方案就是让 CSS 也支持模块化,下面是一些可行的解决方案
26+
27+
## CSS Modules
28+
29+
CSS Modules 是一种将 CSS 类名局部化的技术,可以有效防止样式污染。
30+
31+
### 实现
32+
33+
在 React 项目中,默认是支持 css modules 的,我们只需要把文件名改为`module.css` 即可,示例代码:
34+
35+
```js
36+
// Button.module.css
37+
.button {
38+
background-color: blue;
39+
color: white;
40+
}
41+
42+
// Button.tsx
43+
import styles from './Button.module.css';
44+
45+
const Button = () => {
46+
return <button className={styles.button}>Click me</button>;
47+
};
48+
```
49+
50+
![image-20240901195022950](https://blog-1304565468.cos.ap-shanghai.myqcloud.com/work/image-20240901195022950.png)
51+
52+
如果我们在 VSCode 使用 css modules,推荐下载插件:`CSS Modules`
53+
54+
### 优缺点
55+
56+
优点:
57+
58+
- 局部作用域,有效防止样式冲突
59+
- 可以继续使用熟悉的 CSS 语法
60+
- 支持组合和继承
61+
- 编译时静态分析,有利于性能优化
62+
63+
缺点:
64+
65+
- 需要额外的构建配置(React 自带)
66+
- 类名会被转换,可能影响调试
67+
68+
## Styled Components
69+
70+
我们使用 CSS-in-JS 库 Styled Components(一个流行的CSS-in-JS库,除了它还有 Emotion )实现代码:
71+
72+
```js
73+
import styled from 'styled-components';
74+
75+
const StyledButton = styled.button`
76+
background-color: blue;
77+
color: white;
78+
`;
79+
80+
const Button = () => {
81+
return <StyledButton>Click me</StyledButton>;
82+
};
83+
```
84+
85+
真正项目开发,我们一般会把定义 StyledButton 的 css 代码单独抽离到一个 ts 文件,提高代码可读性。
86+
87+
注意:对于上面的代码,如果你是在 Next.js 使用,必须保证它在客户端组件,如果你希望服务端组件使用 `styled-components` ,请看:https://styled-components.com/docs/advanced#server-side-rendering
88+
89+
优点:
90+
91+
- 组件级别的样式隔离
92+
- 支持动态样式和主题
93+
- 自动生成唯一的类名
94+
- 支持服务端渲染
95+
96+
缺点:
97+
98+
- 可能增加 bundle 大小
99+
- 运行时性能开销(组件必须将 JS 中编写的样式转换为 CSS,并在渲染时插入到文档中)
100+
- 兼容性(比如将应用程序的部分内容移至 [[React 服务器组件](https://nextjs.org/docs/app/building-your-application/rendering/server-components)](https://nextjs.org/docs/app/building-your-application/rendering/server-components)。)
101+
102+
### Tailwind CSS
103+
104+
虽然 tailwindcss 很大,但是在生产环境 Tailwindcss 会使用编译器生成仅使用的类,所以不用担心体积太大的问题,很多人觉得 Tailwindcss 会影响代码可读性,其实是可以通过插件解决的,Tailwindcss 确实是提高了前端开发的幸福度的(个人认为)
105+
106+
使用:使用也很简单,根据官方文档安装,然后参考文档写对应的样式即可,例如在 React 中:
107+
108+
一个居中的 classname: `className='flex justify-center items-center'`
109+
110+
## Inline Styles(行内样式)
111+
112+
示例:`style={{ color: 'red' }}`
113+
114+
优点:
115+
116+
- 简单直接,无需额外工具
117+
- 完全隔离,不会影响其他元素
118+
- 易于动态修改
119+
120+
缺点:
121+
122+
- 不支持伪类和媒体查询
123+
- 可能导致代码冗余
124+
- 难以复用样式,后面不好维护
125+
- 性能较差(特别是在频繁重渲染的情况下)
126+
127+
## styled-jsx
128+
129+
地址:https://www.npmjs.com/package/styled-jsx
130+
131+
Styled JSX 是一个 CSS-in-JS 库,特别适用于 Next.js 项目。它会自动为样式添加作用域,防止污染,
132+
133+
```tsx
134+
const Button = () => {
135+
return (
136+
<>
137+
<button>Click me</button>
138+
<style jsx>{`
139+
button {
140+
background-color: blue;
141+
color: white;
142+
}
143+
`}</style>
144+
</>
145+
);
146+
};
147+
```
148+
149+
150+
151+
优点:
152+
153+
- 组件级别的样式隔离
154+
- 支持完整的 CSS 功能
155+
- 特别适合 Next.js 项目,在服务器或客户端上呈现
156+
157+
缺点:
158+
159+
- 主要用于 Next.js,在其他 React 项目中使用可能不太方便
160+
- 可能增加 bundle 大小
161+
162+
163+
164+
## 命名约定(如BEM)
165+
166+
> BEM 规范文档:https://bemcss.com/
167+
168+
实现(遵循规范):
169+
170+
```css
171+
.blockName__elementName-modifierName {
172+
background-color: blue;
173+
color: white;
174+
}
175+
```
176+
177+
优点:
178+
179+
- 无需额外工具或库
180+
- 易于理解和实施
181+
- 有利于样式的可读性和维护性
182+
183+
缺点:
184+
185+
- 依赖开发者自觉遵守规则,规范毕竟要靠人为来约束,不能保证绝对不会冲突。
186+
- 类名可能变得冗长
187+
- 不能完全防止样式冲突
188+
- 不支持动态样式
189+
190+
# 样式优先级问题
191+
192+
参考文档:https://ant-design.antgroup.com/docs/react/compatible-style-cn
193+
194+
```tsx
195+
'use client';
196+
197+
import type Entity from '@ant-design/cssinjs/es/Cache';
198+
import {
199+
createCache,
200+
extractStyle,
201+
StyleProvider
202+
} from '@ant-design/cssinjs/es/index';
203+
import { useServerInsertedHTML } from 'next/navigation';
204+
import React from 'react';
205+
206+
const StyledComponentsRegistry = ({ children }: React.PropsWithChildren) => {
207+
const cache = React.useMemo<Entity>(() => createCache(), []);
208+
useServerInsertedHTML(() => (
209+
<style
210+
id="antd"
211+
dangerouslySetInnerHTML={{ __html: extractStyle(cache, true) }}
212+
/>
213+
));
214+
// hashPriority="low"
215+
return (
216+
<StyleProvider layer cache={cache}>
217+
{children}
218+
</StyleProvider>
219+
);
220+
};
221+
222+
export default StyledComponentsRegistry;
223+
```
224+
225+
我们创建一个这样的组件,然后在根 `layout.tsx` 使用。如果你的项目使用了 Tailwindcss ,需要修改 `global.css`
226+
227+
```csss
228+
@layer tailwind-base, antd;
229+
230+
@layer tailwind-base {
231+
@tailwind base;
232+
}
233+
@tailwind components;
234+
@tailwind utilities;
235+
@layer reset, antd;
236+
237+
@import url(antd/dist/reset.css) layer(reset);
238+
/* 更多自定义 css */
239+
```
240+
241+
对于如何在 Next.js 14 中使用 antd,我写了一个示例,代码仓库地址:https://github.com/axin-s-Template/Nextjs-Boilerplate/tree/with-antd
242+
243+
## 总结
244+
245+
1)我们了解了总共 6 种 样式书写方案,每个都有自己的优缺点,
246+
247+
2)我们通过 antd 官方文档解决了 antd 样式优先级过高的问题
248+
249+
## 参考资料
250+
251+
1. https://airbnb.io/projects/react-with-styles/
252+
2. https://medium.com/@KimeraMoses/styling-options-for-react-js-and-next-js-7f4507b57781
253+
3. https://juejin.cn/post/7263871284010303546
254+
4. https://juejin.cn/post/7320655424641990682?searchId=202408251659050C6C3214264B83F33D4D
255+
256+
---
257+
此文自动发布于:<a href="https://github.com/coderPerseus/blog/issues/17" target="_blank">github issues</a>

data/blog/post-18.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: 开发经验贴(持久更新)
3+
date: 2024-09-02T08:29:01Z
4+
slug: post-18
5+
author: chaseFunny:https://github.com/chaseFunny
6+
tags: []
7+
---
8+
9+
# 背景
10+
开发中,总是会犯错,或者发现之前的行为效率低下,于是打算写一篇,记录日常开发的总结经验,闲下来再拿出来看看,不断优化开发习惯,让自己更好
11+
# 记录
12+
13+
## 无论再急 ,pr 一定要自己先过一遍
14+
不浪费别人的时间,是对别人最大的尊重
15+
##
16+
17+
---
18+
此文自动发布于:<a href="https://github.com/coderPerseus/blog/issues/18" target="_blank">github issues</a>

0 commit comments

Comments
 (0)