px-to-scale 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jscodev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # Px To Scale Responsive Layout Plugin
2
+
3
+ English | [中文](./README.zh-CN.md)
4
+
5
+ Proportional responsive layout made simple: write CSS in px against your design width, and every scoped element scales with the screen width — like viewing the design at different zoom levels.
6
+
7
+ Works in two cooperating ways, pick per project (or per page):
8
+
9
+ - **Browser runtime** — computes the current width ratio and keeps it updated on resize
10
+ - **Build-time PostCSS plugin** — rewrites your px values into scale expressions during the build
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install px-to-scale
16
+ ```
17
+
18
+ CDN (for quick trials, browser runtime only):
19
+
20
+ ```html
21
+ <script src="px-to-scale/lib/px-to-scale.umd.js"></script>
22
+ <script>
23
+ PxToScale.initBasis(1920, 'c3')
24
+ </script>
25
+ ```
26
+
27
+ ## Quick Start (3 steps)
28
+
29
+ ### 1. Initialize in your entry file
30
+
31
+ ```js
32
+ import { initBasis } from 'px-to-scale'
33
+
34
+ // basis: the design width, e.g. 1920
35
+ // name: a prefix name of your choice, e.g. 'c3'
36
+ initBasis(1920, 'c3')
37
+ ```
38
+
39
+ ### 2. Add the PostCSS plugin to your build config
40
+
41
+ ```js
42
+ // vite.config.js
43
+ import { pxToScalePlugin } from 'px-to-scale'
44
+
45
+ export default defineConfig({
46
+ css: {
47
+ postcss: {
48
+ plugins: [pxToScalePlugin('c3')]
49
+ }
50
+ }
51
+ })
52
+ ```
53
+
54
+ The plugin works in any build chain that accepts PostCSS plugins (Vite, webpack postcss-loader, Rollup, etc.).
55
+
56
+ ### 3. Use the prefix class in your markup and styles
57
+
58
+ ```html
59
+ <!-- Elements with the prefix class scale -->
60
+ <div class="c3-container">
61
+ <div class="c3-box">This box scales with the screen width</div>
62
+ </div>
63
+
64
+ <!-- Other elements are untouched -->
65
+ <div class="normal-box">This box keeps its size</div>
66
+ ```
67
+
68
+ ```css
69
+ /* Write plain px as in the design */
70
+ .c3-box {
71
+ width: 200px; /* 200px at 1920px wide, 100px at 960px, 400px at 3840px */
72
+ height: 100px;
73
+ font-size: 16px; /* text scales proportionally too */
74
+ }
75
+ ```
76
+
77
+ ## What Gets Scaled
78
+
79
+ A CSS rule is in scope when one of its selectors carries the prefix class — `.c3`, `.c3-anything`, `.c3_anything`:
80
+
81
+ ```css
82
+ .c3-box { width: 200px } /* in scope */
83
+ .c3-box .inner { width: 40px } /* in scope: the selector carries the prefix class */
84
+ .other-box { width: 200px } /* not in scope: stays fixed */
85
+ ```
86
+
87
+ Every px value inside an in-scope rule becomes a scale expression, so all in-scope elements scale together with the screen width. When the runtime is not present, each scale expression falls back to the original px size, so the page never breaks.
88
+
89
+ Values listed in `excludePxValues` keep their original size. By default that is `1px`, so hairline borders stay crisp at every screen width:
90
+
91
+ ```css
92
+ .c3-box {
93
+ border: 1px solid #ccc; /* stays 1px at any width (default exclusion) */
94
+ width: 200px; /* scales */
95
+ }
96
+ ```
97
+
98
+ ## Scaling CSS Variables
99
+
100
+ Variables hold fixed px by default. To make a variable scale as well, list it in `cssVarPxMap` — declare it with a px value in a `:root` / `html` rule (or an in-scope rule) and every usage of it scales:
101
+
102
+ ```css
103
+ :root {
104
+ --card-width: 400px;
105
+ --card-height: 300px;
106
+ }
107
+
108
+ .c3-card {
109
+ width: var(--card-width); /* scales once --card-width is mapped */
110
+ height: var(--card-height);
111
+ }
112
+ ```
113
+
114
+ ```js
115
+ pxToScalePlugin('c3', {
116
+ cssVarPxMap: ['--card-width', '--card-height']
117
+ })
118
+ ```
119
+
120
+ Unlike regular values, mapped variables have no px exclusions — the map is an explicit opt-in.
121
+
122
+ ## API Reference
123
+
124
+ > Px To Scale — proportional responsive layout plugin. Write px against a design width; scoped elements scale with the screen width; browser runtime + build-time PostCSS plugin; zero dependencies. TypeScript type declarations ship with the package.
125
+
126
+ | API | Description |
127
+ | --- | --- |
128
+ | [initBasis](#initbasis) | Start the browser runtime for one prefix name (recommended entry) |
129
+ | [computeBasis](#computebasis) | Recompute the scale ratio once, manually |
130
+ | [pxToScalePlugin](#pxtoscaleplugin) | Build-time PostCSS plugin |
131
+
132
+ ### initBasis
133
+
134
+ ```js
135
+ const handle = initBasis(basis, name)
136
+ ```
137
+
138
+ Starts the browser runtime for one prefix name: computes the current width ratio, keeps it updated when the window is resized, and cascades the scale variable into every element carrying the prefix class (and its descendants). Call it once at app startup, before rendering.
139
+
140
+ Repeated calls with the same name share one runtime; every call must eventually call `handle.destroy()` to tear it down (the last one removes it). In a non-browser environment it throws.
141
+
142
+ | Param | Type | Description |
143
+ | --- | --- | --- |
144
+ | basis | number | Design width in px, e.g. 1920 or 375 |
145
+ | name | string | Prefix name; must match the name given to `pxToScalePlugin` |
146
+ | returns | object | Handle with a `destroy()` method |
147
+
148
+ ### computeBasis
149
+
150
+ ```js
151
+ computeBasis(basis, name)
152
+ ```
153
+
154
+ Recomputes the width ratio once and publishes it. Normally not needed — `initBasis` already updates on resize. Use it when you want manual control over the recalculation moment.
155
+
156
+ | Param | Type | Description |
157
+ | --- | --- | --- |
158
+ | basis | number | Design width in px |
159
+ | name | string | Prefix name |
160
+
161
+ ### pxToScalePlugin
162
+
163
+ ```js
164
+ pxToScalePlugin(name, options)
165
+ ```
166
+
167
+ Build-time PostCSS plugin. Rewrites px values in scoped rules into scale expressions, and rewrites mapped variable definitions when `cssVarPxMap` is set. It throws at configuration time when `name` is missing or empty.
168
+
169
+ | Param | Type | Default | Description |
170
+ | --- | --- | --- | --- |
171
+ | name | string | — | Prefix name; must match the name given to `initBasis` |
172
+ | options.cssVarPxMap | string[] | [] | CSS variables whose px definitions should scale |
173
+ | options.excludePxValues | string[] | ['1px'] | px values that keep their original size |
174
+ | options.excludeClasses | string[] | [] | Class names that never count as the prefix class |
175
+
176
+ `excludePxValues` entries are px strings (e.g. `'1px'`, `'0.5px'`); a value keeps its size when its absolute number matches, so `-1px` is covered by `'1px'`.
177
+
178
+ ## Errors
179
+
180
+ Invalid usage throws with a message naming the problem — nothing fails silently:
181
+
182
+ | Situation | Behavior |
183
+ | --- | --- |
184
+ | `name` missing or empty (plugin or runtime) | Throws |
185
+ | `basis` not a positive number (runtime) | Throws |
186
+ | Browser runtime called in a non-browser environment | Throws |
187
+
188
+ ## Notes & Boundaries
189
+
190
+ - **One name, both sides**: the prefix name passed to `initBasis` and to `pxToScalePlugin` must be identical.
191
+ - **Width only**: scaling follows the viewport width; the height ratio is ignored.
192
+ - **Scoping is by selector text**: a descendant scales when the CSS rule's selector carries the prefix class — not merely because it sits inside a prefixed element in the DOM.
193
+ - **Precision**: the width ratio is published with 3 decimals, so sizes may differ from the exact design by at most 0.1%.
194
+ - **Build-time plugin covers all your rules**: rules inside `@media` / `@supports` / `@layer` blocks are rewritten as well whenever their selector carries the prefix class.
195
+
196
+ ## Documentation
197
+
198
+ - **Offline doc bundle**: the "Download doc bundle" button on the doc site home page downloads `px-to-scale-apis.zip` (both READMEs, with the full API reference in English and Chinese); the npm package ships both READMEs
199
+
200
+ ## License
201
+
202
+ MIT
@@ -0,0 +1,202 @@
1
+ # Px To Scale 响应式布局插件
2
+
3
+ [English](./README.md) | 中文
4
+
5
+ 按屏幕宽度等比缩放的响应式布局方案:按设计稿用 px 写 CSS,指定范围内的元素随屏幕宽度整体缩放——就像以不同缩放级别查看设计稿。
6
+
7
+ 提供两种协作方式,按项目(或按页面)选用:
8
+
9
+ - **浏览器运行时** —— 计算当前宽度比例,并在窗口尺寸变化时自动更新
10
+ - **构建期 PostCSS 插件** —— 在构建时把 px 值改写为缩放表达式
11
+
12
+ ## 安装
13
+
14
+ ```bash
15
+ npm install px-to-scale
16
+ ```
17
+
18
+ CDN 快速体验(仅浏览器运行时):
19
+
20
+ ```html
21
+ <script src="px-to-scale/lib/px-to-scale.umd.js"></script>
22
+ <script>
23
+ PxToScale.initBasis(1920, 'c3')
24
+ </script>
25
+ ```
26
+
27
+ ## 快速开始(3 步)
28
+
29
+ ### 1. 在入口文件初始化
30
+
31
+ ```js
32
+ import { initBasis } from 'px-to-scale'
33
+
34
+ // basis:设计稿宽度,如 1920
35
+ // name:自选的前缀名,如 'c3'
36
+ initBasis(1920, 'c3')
37
+ ```
38
+
39
+ ### 2. 在构建配置中添加 PostCSS 插件
40
+
41
+ ```js
42
+ // vite.config.js
43
+ import { pxToScalePlugin } from 'px-to-scale'
44
+
45
+ export default defineConfig({
46
+ css: {
47
+ postcss: {
48
+ plugins: [pxToScalePlugin('c3')]
49
+ }
50
+ }
51
+ })
52
+ ```
53
+
54
+ 插件可用于任何接受 PostCSS 插件的构建链(Vite、webpack postcss-loader、Rollup 等)。
55
+
56
+ ### 3. 在结构和样式里使用前缀类名
57
+
58
+ ```html
59
+ <!-- 带前缀类名的元素会缩放 -->
60
+ <div class="c3-container">
61
+ <div class="c3-box">这个盒子随屏幕宽度缩放</div>
62
+ </div>
63
+
64
+ <!-- 其他元素不受影响 -->
65
+ <div class="normal-box">这个盒子保持原尺寸</div>
66
+ ```
67
+
68
+ ```css
69
+ /* 按设计稿直接写 px */
70
+ .c3-box {
71
+ width: 200px; /* 1920px 宽屏显示 200px,960px 显示 100px,3840px 显示 400px */
72
+ height: 100px;
73
+ font-size: 16px; /* 文字也等比缩放 */
74
+ }
75
+ ```
76
+
77
+ ## 哪些内容会缩放
78
+
79
+ 一条 CSS 规则处于范围内,当且仅当它的选择器之一带有前缀类名——`.c3`、`.c3-任意`、`.c3_任意`:
80
+
81
+ ```css
82
+ .c3-box { width: 200px } /* 范围内 */
83
+ .c3-box .inner { width: 40px } /* 范围内:选择器带有前缀类名 */
84
+ .other-box { width: 200px } /* 范围外:保持固定 */
85
+ ```
86
+
87
+ 范围内规则里的每个 px 值都会变成缩放表达式,范围内的元素随屏幕宽度一起缩放。运行时缺席时,每个缩放表达式回退为原始 px 尺寸,页面不会因此坏掉。
88
+
89
+ 列在 `excludePxValues` 里的值保持原尺寸。默认为 `1px`,因此发丝边框在任何屏宽下都保持清晰:
90
+
91
+ ```css
92
+ .c3-box {
93
+ border: 1px solid #ccc; /* 任何屏宽下都是 1px(默认排除) */
94
+ width: 200px; /* 会缩放 */
95
+ }
96
+ ```
97
+
98
+ ## 让 CSS 变量参与缩放
99
+
100
+ 变量默认持有固定的 px。要让某个变量也缩放,把它列入 `cssVarPxMap`——在 `:root` / `html` 规则(或范围内规则)中以 px 声明它,它的所有使用处就会一起缩放:
101
+
102
+ ```css
103
+ :root {
104
+ --card-width: 400px;
105
+ --card-height: 300px;
106
+ }
107
+
108
+ .c3-card {
109
+ width: var(--card-width); /* --card-width 被映射后即随缩放 */
110
+ height: var(--card-height);
111
+ }
112
+ ```
113
+
114
+ ```js
115
+ pxToScalePlugin('c3', {
116
+ cssVarPxMap: ['--card-width', '--card-height']
117
+ })
118
+ ```
119
+
120
+ 与普通值不同:被映射的变量不做 px 排除——映射本身就是明确的选入。
121
+
122
+ ## API 参考
123
+
124
+ > Px To Scale —— 按屏幕宽度等比缩放的响应式布局插件。按设计稿写 px,指定范围内的元素随屏幕宽度缩放;浏览器运行时 + 构建期 PostCSS 插件;零依赖。npm 包随附 TypeScript 类型声明。
125
+
126
+ | API | 说明 |
127
+ | --- | --- |
128
+ | [initBasis](#initbasis) | 启动某个前缀名的浏览器运行时(推荐入口) |
129
+ | [computeBasis](#computebasis) | 手动重新计算一次缩放比例 |
130
+ | [pxToScalePlugin](#pxtoscaleplugin) | 构建期 PostCSS 插件 |
131
+
132
+ ### initBasis
133
+
134
+ ```js
135
+ const handle = initBasis(basis, name)
136
+ ```
137
+
138
+ 启动某个前缀名的浏览器运行时:计算当前宽度比例、窗口尺寸变化时自动更新,并把缩放变量级联到所有带前缀类名的元素(及其后代)。在应用启动、渲染之前调用一次即可。
139
+
140
+ 同名重复调用共享同一运行时;每次调用最终都应调用 `handle.destroy()` 注销(最后一个注销时真正移除)。在非浏览器环境中调用会抛错。
141
+
142
+ | 参数 | 类型 | 说明 |
143
+ | --- | --- | --- |
144
+ | basis | number | 设计稿宽度(px),如 1920、375 |
145
+ | name | string | 前缀名;必须与 `pxToScalePlugin` 传入的一致 |
146
+ | 返回值 | object | 带 `destroy()` 方法的句柄 |
147
+
148
+ ### computeBasis
149
+
150
+ ```js
151
+ computeBasis(basis, name)
152
+ ```
153
+
154
+ 重新计算一次宽度比例并发布。通常无需调用——`initBasis` 已在窗口变化时自动更新。仅在需要手动控制重算时机时使用。
155
+
156
+ | 参数 | 类型 | 说明 |
157
+ | --- | --- | --- |
158
+ | basis | number | 设计稿宽度(px) |
159
+ | name | string | 前缀名 |
160
+
161
+ ### pxToScalePlugin
162
+
163
+ ```js
164
+ pxToScalePlugin(name, options)
165
+ ```
166
+
167
+ 构建期 PostCSS 插件。把范围内规则中的 px 值改写为缩放表达式;配置了 `cssVarPxMap` 时改写被映射变量的定义。`name` 缺失或为空时在配置阶段即抛错。
168
+
169
+ | 参数 | 类型 | 默认值 | 说明 |
170
+ | --- | --- | --- | --- |
171
+ | name | string | — | 前缀名;必须与 `initBasis` 传入的一致 |
172
+ | options.cssVarPxMap | string[] | [] | 需要随缩放的 CSS 变量(其 px 定义) |
173
+ | options.excludePxValues | string[] | ['1px'] | 保持原尺寸的 px 值 |
174
+ | options.excludeClasses | string[] | [] | 永不算作前缀类的类名 |
175
+
176
+ `excludePxValues` 的条目是 px 字符串(如 `'1px'`、`'0.5px'`);按绝对数值匹配,因此 `-1px` 也被 `'1px'` 覆盖。
177
+
178
+ ## 错误
179
+
180
+ 错误用法会抛出指明问题的错误——不会静默失败:
181
+
182
+ | 情形 | 行为 |
183
+ | --- | --- |
184
+ | `name` 缺失或为空(插件或运行时) | 抛错 |
185
+ | `basis` 不是正数(运行时) | 抛错 |
186
+ | 在非浏览器环境调用浏览器运行时 | 抛错 |
187
+
188
+ ## 注意事项与边界
189
+
190
+ - **一个名字,两端一致**:传给 `initBasis` 与 `pxToScalePlugin` 的前缀名必须相同。
191
+ - **只看宽度**:缩放跟随视口宽度,忽略高度比例。
192
+ - **按选择器文本划范围**:后代元素缩放取决于其 CSS 规则的选择器是否带前缀类名——并不是「DOM 上嵌在带前缀元素里」就缩放。
193
+ - **精度**:宽度比例以 3 位小数发布,尺寸与设计稿的差异至多 0.1%。
194
+ - **构建期插件覆盖所有规则**:`@media` / `@supports` / `@layer` 块内选择器带前缀类名的规则同样会被改写。
195
+
196
+ ## 文档
197
+
198
+ - **离线文档包**:文档站首页的 "Download doc bundle" 按钮下载 `px-to-scale-apis.zip`(中英双语 README,含完整 API 参考);npm 包随附两份 README
199
+
200
+ ## 许可证
201
+
202
+ MIT
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "px-to-scale",
3
+ "version": "1.0.0",
4
+ "title": "Px To Scale",
5
+ "description": "Proportional responsive layout plugin: scale any element with the screen width, via a browser runtime and a build-time PostCSS plugin",
6
+ "license": "MIT",
7
+ "author": "jscodev",
8
+ "type": "module",
9
+ "main": "./lib/px-to-scale.umd.cjs",
10
+ "module": "./lib/px-to-scale.js",
11
+ "types": "./lib/px-to-scale.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./lib/px-to-scale.d.ts",
15
+ "import": "./lib/px-to-scale.js",
16
+ "require": "./lib/px-to-scale.umd.cjs",
17
+ "default": "./lib/px-to-scale.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "lib",
22
+ "README.md",
23
+ "README.zh-CN.md"
24
+ ],
25
+ "keywords": [
26
+ "responsive",
27
+ "scale",
28
+ "postcss",
29
+ "css",
30
+ "layout"
31
+ ],
32
+ "maintainers": [
33
+ "jscodev"
34
+ ],
35
+ "scripts": {
36
+ "build:types": "tsc -p tsconfig.json",
37
+ "build:apis": "node scripts/package-apis.cjs",
38
+ "build": "npm run build:types && vite build && node scripts/obfuscate.cjs && npm run build:apis",
39
+ "build:doc": "npm run build:apis && cd doc-site && npm install && npm run build",
40
+ "dev:doc": "npm run build:apis && cd doc-site && npm run dev",
41
+ "release": "npm version patch --no-tag-if-no-changes && npm publish"
42
+ },
43
+ "devDependencies": {
44
+ "javascript-obfuscator": "^5.6.0",
45
+ "typescript": "^5.4.0",
46
+ "vite": "^5.4.0"
47
+ }
48
+ }