@cherrywind/flexible 0.0.2-alpha → 0.0.2-beta.1

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/README.md CHANGED
@@ -12,15 +12,17 @@ A lightweight and flexible layout system for responsive web design, inspired by
12
12
  - 🎯 Customizable breakpoints
13
13
  - 🎨 CSS variable support
14
14
  - 🧹 Clean API with cleanup function
15
+ - ⚡ Immediate layout application
16
+ - 📐 Orientation change support
15
17
 
16
18
  ## Installation
17
19
 
18
20
  ```bash
19
- npm install @your-scope/flexible
21
+ npm install @cherrywind/flexible
20
22
  # or
21
- yarn add @your-scope/flexible
23
+ yarn add @cherrywind/flexible
22
24
  # or
23
- pnpm add @your-scope/flexible
25
+ pnpm add @cherrywind/flexible
24
26
  ```
25
27
 
26
28
  ## Usage
@@ -28,7 +30,7 @@ pnpm add @your-scope/flexible
28
30
  ### Basic Usage
29
31
 
30
32
  ```typescript
31
- import { flexible } from '@your-scope/flexible';
33
+ import { flexible } from '@cherrywind/flexible';
32
34
 
33
35
  // Initialize with default settings
34
36
  const cleanup = flexible();
@@ -40,12 +42,14 @@ cleanup();
40
42
  ### With Custom Options
41
43
 
42
44
  ```typescript
43
- import { flexible } from '@your-scope/flexible';
45
+ import { flexible } from '@cherrywind/flexible';
44
46
 
45
47
  const cleanup = flexible({
46
48
  breakpoints: [1024, 768],
47
- containers: [1920, 1024, 768],
48
- basicContainer: 1920,
49
+ layouts: [375, 1024, 1920],
50
+ basicLayout: 1920,
51
+ immediate: true,
52
+ orientationchange: false,
49
53
  scope: {
50
54
  element: document.querySelector('.container'),
51
55
  cssVarName: '--custom-rem'
@@ -65,22 +69,49 @@ interface FlexibleOptions {
65
69
  */
66
70
  breakpoints?: number[];
67
71
  /**
68
- * An array of container widths that corresponds to breakpoints.
72
+ * An array of layout widths that corresponds to breakpoints.
69
73
  * Must have exactly one more item than breakpoints array.
74
+ * For example, if breakpoints is [768], layouts could be [375, 1920],
75
+ * where 375 is the width for viewport <= 768px and 1920 is for viewport > 768px.
70
76
  */
71
- containers?: number[];
77
+ layouts?: number[];
72
78
  /**
73
- * The base container width used as reference for calculations.
74
- * Defaults to the last item in containers array.
79
+ * The base layout width used as reference for calculations.
80
+ * Only effective when layouts is provided.
81
+ * Used as the baseline layout width for ratio calculations.
82
+ * Defaults to the last item in layouts array (layouts?.at(-1)),
83
+ * which typically represents the largest viewport width.
75
84
  */
76
- basicContainer?: number;
85
+ basicLayout?: number;
86
+ /**
87
+ * Whether to apply the layout immediately on initialization.
88
+ * Defaults to false.
89
+ */
90
+ immediate?: boolean;
91
+ /**
92
+ * Whether to listen for orientation change events.
93
+ * Defaults to true.
94
+ */
95
+ orientationchange?: boolean;
77
96
  /**
78
97
  * Whether to set the CSS variable on a specific scope element.
98
+ * Defaults to false, which means setting the font size on the document element.
99
+ * If an object is provided, it can specify the element and CSS variable name.
79
100
  */
80
- scope?: {
81
- element?: HTMLElement;
82
- cssVarName?: string;
83
- } | false;
101
+ scope?:
102
+ | false
103
+ | {
104
+ /**
105
+ * The scope element to set the CSS variable on.
106
+ * Defaults to document.documentElement.
107
+ */
108
+ element?: HTMLElement;
109
+ /**
110
+ * The CSS variable name to use for the base rem value.
111
+ * Defaults to "--local-scope-rem".
112
+ */
113
+ cssVarName?: string;
114
+ };
84
115
  }
85
116
  ```
86
117
 
@@ -99,7 +130,7 @@ cleanup();
99
130
  ### Basic Example
100
131
 
101
132
  ```typescript
102
- import { flexible } from '@your-scope/flexible';
133
+ import { flexible } from '@cherrywind/flexible';
103
134
 
104
135
  // Initialize with default settings
105
136
  const cleanup = flexible();
@@ -111,19 +142,19 @@ const cleanup = flexible();
111
142
  ### Custom Breakpoints Example
112
143
 
113
144
  ```typescript
114
- import { flexible } from '@your-scope/flexible';
145
+ import { flexible } from '@cherrywind/flexible';
115
146
 
116
147
  const cleanup = flexible({
117
148
  breakpoints: [1024, 768],
118
- containers: [1920, 1024, 768],
119
- basicContainer: 1920
149
+ layouts: [375, 1024, 1920],
150
+ basicLayout: 1920
120
151
  });
121
152
  ```
122
153
 
123
154
  ### Custom Scope Example
124
155
 
125
156
  ```typescript
126
- import { flexible } from '@your-scope/flexible';
157
+ import { flexible } from '@cherrywind/flexible';
127
158
 
128
159
  const container = document.querySelector('.container');
129
160
  const cleanup = flexible({
@@ -134,6 +165,28 @@ const cleanup = flexible({
134
165
  });
135
166
  ```
136
167
 
168
+ ### Immediate Layout Example
169
+
170
+ ```typescript
171
+ import { flexible } from '@cherrywind/flexible';
172
+
173
+ // Apply layout immediately without waiting for load event
174
+ const cleanup = flexible({
175
+ immediate: true
176
+ });
177
+ ```
178
+
179
+ ### Disable Orientation Change Example
180
+
181
+ ```typescript
182
+ import { flexible } from '@cherrywind/flexible';
183
+
184
+ // Disable orientation change handling
185
+ const cleanup = flexible({
186
+ orientationchange: false
187
+ });
188
+ ```
189
+
137
190
  ## License
138
191
 
139
192
  MIT
package/README.zh-CN.md CHANGED
@@ -12,15 +12,17 @@
12
12
  - 🎯 可自定义断点
13
13
  - 🎨 支持 CSS 变量
14
14
  - 🧹 提供清理函数的简洁 API
15
+ - ⚡ 支持立即应用布局
16
+ - 📐 支持屏幕方向变化
15
17
 
16
18
  ## 安装
17
19
 
18
20
  ```bash
19
- npm install @your-scope/flexible
21
+ npm install @cherrywind/flexible
20
22
  # 或
21
- yarn add @your-scope/flexible
23
+ yarn add @cherrywind/flexible
22
24
  # 或
23
- pnpm add @your-scope/flexible
25
+ pnpm add @cherrywind/flexible
24
26
  ```
25
27
 
26
28
  ## 使用方法
@@ -28,7 +30,7 @@ pnpm add @your-scope/flexible
28
30
  ### 基础用法
29
31
 
30
32
  ```typescript
31
- import { flexible } from '@your-scope/flexible';
33
+ import { flexible } from '@cherrywind/flexible';
32
34
 
33
35
  // 使用默认设置初始化
34
36
  const cleanup = flexible();
@@ -40,12 +42,14 @@ cleanup();
40
42
  ### 自定义配置
41
43
 
42
44
  ```typescript
43
- import { flexible } from '@your-scope/flexible';
45
+ import { flexible } from '@cherrywind/flexible';
44
46
 
45
47
  const cleanup = flexible({
46
48
  breakpoints: [1024, 768],
47
- containers: [1920, 1024, 768],
48
- basicContainer: 1920,
49
+ layouts: [375, 1024, 1920],
50
+ basicLayout: 1920,
51
+ immediate: true,
52
+ orientationchange: false,
49
53
  scope: {
50
54
  element: document.querySelector('.container'),
51
55
  cssVarName: '--custom-rem'
@@ -65,22 +69,49 @@ interface FlexibleOptions {
65
69
  */
66
70
  breakpoints?: number[];
67
71
  /**
68
- * 与断点对应的容器宽度数组。
72
+ * 与断点对应的布局宽度数组。
69
73
  * 数组长度必须比断点数组多一个。
74
+ * 例如,如果断点是 [768],布局可以是 [375, 1920],
75
+ * 其中 375 是视口宽度 <= 768px 时的宽度,1920 是视口宽度 > 768px 时的宽度。
70
76
  */
71
- containers?: number[];
77
+ layouts?: number[];
72
78
  /**
73
- * 用于计算的基础容器宽度。
74
- * 默认为 containers 数组的最后一项。
79
+ * 用于计算的基础布局宽度。
80
+ * 仅在提供 layouts 时有效。
81
+ * 用作比例计算的基础布局宽度。
82
+ * 默认为 layouts 数组的最后一项(layouts?.at(-1)),
83
+ * 通常代表最大的视口宽度。
75
84
  */
76
- basicContainer?: number;
85
+ basicLayout?: number;
86
+ /**
87
+ * 是否在初始化时立即应用布局。
88
+ * 默认为 false。
89
+ */
90
+ immediate?: boolean;
91
+ /**
92
+ * 是否监听屏幕方向变化事件。
93
+ * 默认为 true。
94
+ */
95
+ orientationchange?: boolean;
77
96
  /**
78
97
  * 是否在特定作用域元素上设置 CSS 变量。
98
+ * 默认为 false,表示在文档元素上设置字体大小。
99
+ * 如果提供对象,可以指定元素和 CSS 变量名。
79
100
  */
80
- scope?: {
81
- element?: HTMLElement;
82
- cssVarName?: string;
83
- } | false;
101
+ scope?:
102
+ | false
103
+ | {
104
+ /**
105
+ * 设置 CSS 变量的作用域元素。
106
+ * 默认为 document.documentElement。
107
+ */
108
+ element?: HTMLElement;
109
+ /**
110
+ * 用于基础 rem 值的 CSS 变量名。
111
+ * 默认为 "--local-scope-rem"。
112
+ */
113
+ cssVarName?: string;
114
+ };
84
115
  }
85
116
  ```
86
117
 
@@ -99,7 +130,7 @@ cleanup();
99
130
  ### 基础示例
100
131
 
101
132
  ```typescript
102
- import { flexible } from '@your-scope/flexible';
133
+ import { flexible } from '@cherrywind/flexible';
103
134
 
104
135
  // 使用默认设置初始化
105
136
  const cleanup = flexible();
@@ -111,19 +142,19 @@ const cleanup = flexible();
111
142
  ### 自定义断点示例
112
143
 
113
144
  ```typescript
114
- import { flexible } from '@your-scope/flexible';
145
+ import { flexible } from '@cherrywind/flexible';
115
146
 
116
147
  const cleanup = flexible({
117
148
  breakpoints: [1024, 768],
118
- containers: [1920, 1024, 768],
119
- basicContainer: 1920
149
+ layouts: [375, 1024, 1920],
150
+ basicLayout: 1920
120
151
  });
121
152
  ```
122
153
 
123
154
  ### 自定义作用域示例
124
155
 
125
156
  ```typescript
126
- import { flexible } from '@your-scope/flexible';
157
+ import { flexible } from '@cherrywind/flexible';
127
158
 
128
159
  const container = document.querySelector('.container');
129
160
  const cleanup = flexible({
@@ -134,6 +165,28 @@ const cleanup = flexible({
134
165
  });
135
166
  ```
136
167
 
168
+ ### 立即应用布局示例
169
+
170
+ ```typescript
171
+ import { flexible } from '@cherrywind/flexible';
172
+
173
+ // 立即应用布局,不等待 load 事件
174
+ const cleanup = flexible({
175
+ immediate: true
176
+ });
177
+ ```
178
+
179
+ ### 禁用屏幕方向变化示例
180
+
181
+ ```typescript
182
+ import { flexible } from '@cherrywind/flexible';
183
+
184
+ // 禁用屏幕方向变化处理
185
+ const cleanup = flexible({
186
+ orientationchange: false
187
+ });
188
+ ```
189
+
137
190
  ## 许可证
138
191
 
139
- MIT
192
+ MIT
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=(a={})=>{const{breakpoints:i=[768],containers:e,basicContainer:l=e==null?void 0:e.at(-1),scope:d}=a,c=o=>!e||!l?1:e.length-1===i.length?l/e[o]:1,t=()=>{const o=document.documentElement,r=o.clientWidth;let s=r/100;for(let n=0;n<i.length;n++)if(r<=i[n]){s=r*c(n);break}if(d){const{element:n=document.documentElement,cssVarName:m="--local-scope-rem"}=d;n.style.setProperty(m,s+"px")}else o.style.fontSize=s+"px"};return window.addEventListener("load",t),window.addEventListener("resize",t),window.addEventListener("orientationchange",t),()=>{window.removeEventListener("load",t),window.removeEventListener("resize",t),window.removeEventListener("orientationchange",t)}};exports.flexible=w;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=(m={})=>{const{breakpoints:o=[768],layouts:t,basicLayout:l=t==null?void 0:t.at(-1),scope:a,immediate:c=!1,orientationchange:d=!0}=m,f=i=>!t||!l?1:t.length-1===o.length?l/t[i]:1,e=()=>{const i=document.documentElement,r=i.clientWidth;let s=r/100;for(let n=0;n<o.length;n++)if(r<=o[n]){s=r*f(n);break}if(a){const{element:n=document.documentElement,cssVarName:u="--local-scope-rem"}=a;n.style.setProperty(u,s+"px")}else i.style.fontSize=s+"px"};return c?e():window.addEventListener("load",e),window.addEventListener("resize",e),d&&screen.orientation.addEventListener("change",e),()=>{c||window.removeEventListener("load",e),window.removeEventListener("resize",e),d&&screen.orientation.removeEventListener("change",e)}};exports.flexible=v;
package/dist/index.d.ts CHANGED
@@ -17,20 +17,30 @@ export declare interface FlexibleOptions {
17
17
  */
18
18
  breakpoints?: number[];
19
19
  /**
20
- * An array of container widths that corresponds to breakpoints.
20
+ * An array of layout widths that corresponds to breakpoints.
21
21
  * Must have exactly one more item than breakpoints array.
22
- * For example, if breakpoints is [768], containers could be [375, 1920],
22
+ * For example, if breakpoints is [768], layouts could be [375, 1920],
23
23
  * where 375 is the width for viewport <= 768px and 1920 is for viewport > 768px.
24
24
  */
25
- containers?: number[];
25
+ layouts?: number[];
26
26
  /**
27
- * The base container width used as reference for calculations.
28
- * Only effective when containers is provided.
29
- * Used as the baseline container width for ratio calculations.
30
- * Defaults to the last item in containers array (containers?.at(-1)),
27
+ * The base layout width used as reference for calculations.
28
+ * Only effective when layouts is provided.
29
+ * Used as the baseline layout width for ratio calculations.
30
+ * Defaults to the last item in layouts array (layouts?.at(-1)),
31
31
  * which typically represents the largest viewport width.
32
32
  */
33
- basicContainer?: number;
33
+ basicLayout?: number;
34
+ /**
35
+ * Whether to apply the layout immediately on initialization.
36
+ * Defaults to false.
37
+ */
38
+ immediate?: boolean;
39
+ /**
40
+ * Whether to listen for orientation change events.
41
+ * Defaults to true.
42
+ */
43
+ orientationchange?: boolean;
34
44
  /**
35
45
  * Whether to set the CSS variable on a specific scope element.
36
46
  * Defaults to false, which means setting the font size on the document element.
package/dist/index.js CHANGED
@@ -1,22 +1,29 @@
1
- const m = (a = {}) => {
2
- const { breakpoints: i = [768], containers: e, basicContainer: d = e == null ? void 0 : e.at(-1), scope: l } = a, c = (o) => !e || !d ? 1 : e.length - 1 === i.length ? d / e[o] : 1, t = () => {
3
- const o = document.documentElement, r = o.clientWidth;
1
+ const w = (m = {}) => {
2
+ const {
3
+ breakpoints: o = [768],
4
+ layouts: t,
5
+ basicLayout: a = t == null ? void 0 : t.at(-1),
6
+ scope: c,
7
+ immediate: l = !1,
8
+ orientationchange: d = !0
9
+ } = m, f = (i) => !t || !a ? 1 : t.length - 1 === o.length ? a / t[i] : 1, e = () => {
10
+ const i = document.documentElement, r = i.clientWidth;
4
11
  let s = r / 100;
5
- for (let n = 0; n < i.length; n++)
6
- if (r <= i[n]) {
7
- s = r * c(n);
12
+ for (let n = 0; n < o.length; n++)
13
+ if (r <= o[n]) {
14
+ s = r * f(n);
8
15
  break;
9
16
  }
10
- if (l) {
11
- const { element: n = document.documentElement, cssVarName: w = "--local-scope-rem" } = l;
12
- n.style.setProperty(w, s + "px");
17
+ if (c) {
18
+ const { element: n = document.documentElement, cssVarName: v = "--local-scope-rem" } = c;
19
+ n.style.setProperty(v, s + "px");
13
20
  } else
14
- o.style.fontSize = s + "px";
21
+ i.style.fontSize = s + "px";
15
22
  };
16
- return window.addEventListener("load", t), window.addEventListener("resize", t), window.addEventListener("orientationchange", t), () => {
17
- window.removeEventListener("load", t), window.removeEventListener("resize", t), window.removeEventListener("orientationchange", t);
23
+ return l ? e() : window.addEventListener("load", e), window.addEventListener("resize", e), d && screen.orientation.addEventListener("change", e), () => {
24
+ l || window.removeEventListener("load", e), window.removeEventListener("resize", e), d && screen.orientation.removeEventListener("change", e);
18
25
  };
19
26
  };
20
27
  export {
21
- m as flexible
28
+ w as flexible
22
29
  };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@cherrywind/flexible",
3
- "version": "0.0.2-alpha",
3
+ "version": "0.0.2-beta.1",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "flexible"
7
7
  ],
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/susanforme/utils"
10
+ "url": "https://github.com/susanforme/utils/tree/master/packages/flexible"
11
11
  },
12
12
  "license": "MIT",
13
13
  "type": "module",