Skip to content

Commit dc698d6

Browse files
committed
feat(flexible): 更新版本至0.0.5并优化布局计算
1 parent 21e7d14 commit dc698d6

5 files changed

Lines changed: 132 additions & 53 deletions

File tree

packages/flexible/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cherrywind/flexible",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"private": false,
55
"keywords": [
66
"flexible"

packages/flexible/src/index.ts

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
11
/**
2-
* Options for the flexible layout function.
2+
* 弹性布局函数的配置选项。
33
*/
44
export interface FlexibleOptions {
55
/**
6-
* An array of breakpoints in pixels, from largest to smallest.
7-
* Defaults to [768].
6+
* 一个以像素为单位的断点数组,从大到小排列。
7+
* 默认为 [768]
88
*/
99
breakpoints?: number[];
1010
/**
11-
* An array of layout widths that corresponds to breakpoints.
12-
* Must have exactly one more item than breakpoints array.
13-
* For example, if breakpoints is [768], layouts could be [375, 1920],
14-
* where 375 is the width for viewport <= 768px and 1920 is for viewport > 768px.
11+
* 一个与断点相对应的布局宽度数组。
12+
* 其项目数量必须比断点数组多一个。
13+
* 例如,如果 breakpoints [768],则 layouts 可以是 [375, 1920]
14+
* 其中 375 是视口宽度 <= 768px 时的布局宽度,1920 是视口宽度 > 768px 时的布局宽度。
1515
*/
1616
layouts?: number[];
1717
/**
18-
* The base layout width used as reference for calculations.
19-
* Only effective when layouts is provided.
20-
* Used as the baseline layout width for ratio calculations.
21-
* Defaults to the last item in layouts array (layouts?.at(-1)),
22-
* which typically represents the largest viewport width.
18+
* 用作计算参考的基础布局宽度。
19+
* 仅在提供了 layouts 时有效。
20+
* 用作比例计算的基准布局宽度。
21+
* 默认为 layouts 数组中的最后一项 (layouts?.at(-1))
22+
* 这通常代表最大的视口宽度。
2323
*/
2424
basicLayout?: number;
2525
/**
26-
* Whether to apply the layout immediately on initialization.
27-
* Defaults to false.
26+
* 是否在初始化时立即应用布局。
27+
* 默认为 false
2828
*/
2929
immediate?: boolean;
3030
/**
31-
* Whether to listen for orientation change events.
32-
* Defaults to true.
31+
* 是否监听屏幕方向变化事件。
32+
* 默认为 true
3333
*/
3434
orientationchange?: boolean;
3535

3636
/**
37-
* Whether to set the CSS variable on a specific scope element.
38-
* Defaults to false, which means setting the font size on the document element.
39-
* If an object is provided, it can specify the element and CSS variable name.
37+
* 是否在特定的作用域元素上设置 CSS 变量。
38+
* 默认为 false,表示在 document 元素上设置字体大小。
39+
* 如果提供一个对象,可以指定元素和 CSS 变量名。
4040
*/
4141
scope?:
4242
| false
4343
| {
4444
/**
45-
* The scope element to set the CSS variable on.
46-
* Defaults to document.documentElement.
45+
* 设置 CSS 变量的作用域元素。
46+
* 默认为 document.documentElement
4747
*/
4848
element: HTMLElement;
4949
/**
50-
* The CSS variable name to use for the base rem value.
51-
* Defaults to "--local-scope-rem".
50+
* 用于 rem 基础值的 CSS 变量名。
51+
* 默认为 "--local-scope-rem"
5252
*/
5353
cssVarName?: string;
5454
}
5555
| {
5656
/**
57-
* The scope element to set the CSS variable on.
58-
* Defaults to document.documentElement.
57+
* 设置 CSS 变量的作用域元素。
58+
* 默认为 document.documentElement
5959
*/
6060
element: HTMLElement;
6161
/**
62-
* An array of ratio factors for each layout, used for poster mode or custom scaling.
63-
* Must have the same length as layouts.
64-
* Defaults to [1, 1, ...] (no extra scaling).
62+
* 每个布局的比例因子数组,用于海报模式或自定义缩放。
63+
* 长度必须与 layouts 数组相同。
64+
* 默认为 [1, 1, ...] (无额外缩放)。
6565
*/
6666
ratio?: number[];
6767
/**
68-
* The CSS variable name to use for the base rem value.
69-
* Defaults to "--local-scope-rem".
68+
* 用于 rem 基础值的 CSS 变量名。
69+
* 默认为 "--local-scope-rem"
7070
*/
7171
cssVarName?: string;
7272
}[];
7373

7474
/**
75-
* An array of ratio factors for each layout, used for poster mode or custom scaling.
76-
* Must have the same length as layouts.
77-
* Defaults to [1, 1, ...] (no extra scaling).
75+
* 每个布局的比例因子数组,用于海报模式或自定义缩放。
76+
* 长度必须与 layouts 数组相同。
77+
* 默认为 [1, 1, ...] (无额外缩放)。
7878
*/
7979
ratio?: number[];
8080
/**
81-
* Whether to recalibrate the layout when the window is resized.
81+
* 是否在窗口大小调整时重新校准布局。
8282
* 因为宽度变化,但是css var没来得及变化导致出现滚动条,clientWidth和innerWidth不一致
83-
* Defaults to true.
83+
* 默认为 true
8484
*/
8585
recalibrate?: boolean;
8686
/**
87-
* An option to control the resize behavior.
87+
* 用于控制 resize 行为的选项。
8888
*/
8989
resizeOption?:
9090
| {
@@ -95,11 +95,11 @@ export interface FlexibleOptions {
9595
}
9696

9797
/**
98-
* Initializes a flexible layout system that sets a CSS variable for rem units
99-
* based on the viewport width and adaptively scales according to breakpoints.
98+
* 初始化一个弹性布局系统,该系统会根据视口宽度设置一个用于 rem 单位的 CSS 变量,
99+
* 并根据断点自适应缩放。
100100
*
101-
* @param options - Configuration options for the flexible layout
102-
* @returns A cleanup function to remove event listeners
101+
* @param options - 弹性布局的配置选项
102+
* @returns 一个用于移除事件监听器的清理函数
103103
*/
104104
export const flexible = (options: FlexibleOptions = {}): (() => void) => {
105105
const {
@@ -120,7 +120,7 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
120120
// 对于相同设备来说,滚动条要么永远占据宽度,要么永远不占据宽度
121121
const isScrollbarPresent = getScrollbarWidth() > 0;
122122

123-
// Ensure ratio array matches layouts length, default to 1
123+
// 确保 ratio 数组的长度与 layouts 匹配,默认为 1
124124
let ratio = propRatio;
125125
if (ratio) {
126126
if (layouts?.length !== ratio.length && layouts) {
@@ -129,20 +129,20 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
129129
}
130130

131131
/**
132-
* Calculate the ratio factor for a specific breakpoint
133-
* @param index - Breakpoint index
134-
* @returns The ratio factor, defaults to 1
132+
* 计算特定断点的比例因子
133+
* @param index - 断点索引
134+
* @returns 比例因子,默认为 1
135135
*/
136136
const getBreakpointRatio = (index: number): number => {
137137
if (!layouts || !basicLayout) return 1;
138138
if (layouts.length - 1 === breakpoints.length) {
139139
return basicLayout / layouts[index];
140140
}
141-
return 1; // Default to ratio factor of 1
141+
return 1; // 默认为 1 的比例因子
142142
};
143143

144144
/**
145-
* Respond to window size changes and update CSS variable
145+
* 响应窗口大小变化并更新 CSS 变量
146146
*/
147147
const responsive = (): void => {
148148
// 内部核心计算逻辑,可以被重复调用
@@ -211,7 +211,7 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
211211
if (orientationchange) {
212212
screen.orientation.addEventListener('change', resizeHandler);
213213
}
214-
// Return cleanup function
214+
// 返回清理函数
215215
return () => {
216216
if (!immediate) {
217217
window.removeEventListener('load', responsive);
@@ -223,6 +223,12 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
223223
};
224224
};
225225

226+
/**
227+
* 防抖函数:在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时。
228+
* @param func 要执行的函数
229+
* @param wait 延迟时间(毫秒)
230+
* @returns 防抖后的函数
231+
*/
226232
function debounce<F extends (...args: any[]) => any>(func: F, wait: number): F {
227233
let timeout: ReturnType<typeof setTimeout> | null = null;
228234

@@ -277,6 +283,10 @@ function throttle<T extends (...args: any[]) => void>(func: T, delay: number): (
277283
};
278284
}
279285

286+
/**
287+
* 计算浏览器滚动条的宽度。
288+
* @returns {number} 滚动条的宽度(像素)
289+
*/
280290
function getScrollbarWidth(): number {
281291
const scrollDiv = document.createElement('div');
282292
scrollDiv.style.width = '100px';

packages/postcss/README.md

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,76 @@ pnpm add @cherrywind/postcss --save-dev
1717

1818
## Plugins
1919

20-
### postcss-rem-to-css-vars
20+
### `postcss-px-to-local-var`
2121

22-
now `postcss-rem-to-css-vars` is deprecated, use `px-to-local-var` instead.
22+
A PostCSS plugin that converts `px` units to a value calculated with a CSS variable. This allows you to create scoped components where the scale can be controlled by simply changing the CSS variable's value on the root element.
2323

24-
### postcss-px-to-viewport
24+
**Example:**
2525

26-
px to scope unit.
26+
**Input:**
27+
28+
```css
29+
.foo {
30+
font-size: 16px;
31+
border: 1px solid black;
32+
margin-bottom: 10px;
33+
}
34+
```
35+
36+
**Output (with default options):**
37+
38+
```css
39+
.foo {
40+
font-size: calc(var(--local-scope-rem, 1rem) * 1);
41+
border: calc(var(--local-scope-rem, 1rem) * 0.0625) solid black;
42+
margin-bottom: calc(var(--local-scope-rem, 1rem) * 0.625);
43+
}
44+
```
45+
46+
By changing the `--local-scope-rem` variable on a parent element, you can scale all the child elements.
47+
48+
```html
49+
<div style="--local-scope-rem: 1.2rem;">
50+
<div class="foo">...</div>
51+
</div>
52+
```
53+
54+
**Usage**
55+
56+
Install the plugin and add it to your PostCSS configuration:
57+
58+
```javascript
59+
// postcss.config.js
60+
const { postcssPxToLocalVar } = require('@cherrywind/postcss');
61+
62+
module.exports = {
63+
plugins: [
64+
postcssPxToLocalVar({
65+
// Your options here
66+
}),
67+
],
68+
};
69+
```
70+
71+
**Options**
72+
73+
| Option | Type | Default | Description |
74+
| ------------------- | --------------------- | ----------------------- | ------------------------------------------------------------------------------------ |
75+
| `rootValue` | `number` | `16` | The root element font size. |
76+
| `unitPrecision` | `number` | `5` | The number of decimal places for the calculated value. |
77+
| `selectorBlackList` | `(string | RegExp)[]` | `[]` | An array of selectors to ignore. |
78+
| `propList` | `string[]` | `['*']` | The list of properties to convert. |
79+
| `replace` | `boolean` | `true` | Whether to replace the original `px` declaration or add a fallback. |
80+
| `mediaQuery` | `boolean` | `false` | Whether to convert `px` in media queries. |
81+
| `minPixelValue` | `number` | `0` | The minimum pixel value to replace. |
82+
| `exclude` | `RegExp \| Function` | `null` | A regex or function to exclude files from processing. |
83+
| `varName` | `string` | `'--local-scope-rem'` | The name of the CSS custom property. |
84+
85+
### `postcss-rem-to-local-var`
86+
87+
> [!WARNING]
88+
> **Deprecated**
89+
>
90+
> Please use [`postcss-px-to-local-var`](#postcss-px-to-local-var) instead.
91+
92+
This plugin converts `rem` units to `calc(var(...) * value)`. It is succeeded by `postcss-px-to-local-var` which offers more features and flexibility.

packages/postcss/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cherrywind/postcss",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"private": false,
55
"keywords": [
66
"postcss",

packages/postcss/src/px-to-local-var/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ function hasDisableNextLineComment(decl: Declaration): boolean {
108108
return false;
109109
}
110110

111+
/**
112+
* px 转换为 局部 rem 插件
113+
*/
111114
export const postcssPxToLocalVar = (options: PxToLocalVarOptions = {}): Plugin => {
112115
const opts = { ...defaults, ...options };
113116
const propListMatch = createPropListMatcher(opts.propList);

0 commit comments

Comments
 (0)