@cherrywind/flexible 0.0.2-alpha
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 +139 -0
- package/README.zh-CN.md +139 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +22 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Flexible
|
|
2
|
+
|
|
3
|
+
A lightweight and flexible layout system for responsive web design, inspired by the popular flexible.js.
|
|
4
|
+
|
|
5
|
+
[中文文档](./README.zh-CN.md)
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- 🚀 Lightweight and fast
|
|
10
|
+
- 📱 Responsive design support
|
|
11
|
+
- 🔄 Automatic viewport adaptation
|
|
12
|
+
- 🎯 Customizable breakpoints
|
|
13
|
+
- 🎨 CSS variable support
|
|
14
|
+
- 🧹 Clean API with cleanup function
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @your-scope/flexible
|
|
20
|
+
# or
|
|
21
|
+
yarn add @your-scope/flexible
|
|
22
|
+
# or
|
|
23
|
+
pnpm add @your-scope/flexible
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Basic Usage
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { flexible } from '@your-scope/flexible';
|
|
32
|
+
|
|
33
|
+
// Initialize with default settings
|
|
34
|
+
const cleanup = flexible();
|
|
35
|
+
|
|
36
|
+
// Clean up when needed
|
|
37
|
+
cleanup();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### With Custom Options
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { flexible } from '@your-scope/flexible';
|
|
44
|
+
|
|
45
|
+
const cleanup = flexible({
|
|
46
|
+
breakpoints: [1024, 768],
|
|
47
|
+
containers: [1920, 1024, 768],
|
|
48
|
+
basicContainer: 1920,
|
|
49
|
+
scope: {
|
|
50
|
+
element: document.querySelector('.container'),
|
|
51
|
+
cssVarName: '--custom-rem'
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
### Options
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
interface FlexibleOptions {
|
|
62
|
+
/**
|
|
63
|
+
* An array of breakpoints in pixels, from largest to smallest.
|
|
64
|
+
* Defaults to [768].
|
|
65
|
+
*/
|
|
66
|
+
breakpoints?: number[];
|
|
67
|
+
/**
|
|
68
|
+
* An array of container widths that corresponds to breakpoints.
|
|
69
|
+
* Must have exactly one more item than breakpoints array.
|
|
70
|
+
*/
|
|
71
|
+
containers?: number[];
|
|
72
|
+
/**
|
|
73
|
+
* The base container width used as reference for calculations.
|
|
74
|
+
* Defaults to the last item in containers array.
|
|
75
|
+
*/
|
|
76
|
+
basicContainer?: number;
|
|
77
|
+
/**
|
|
78
|
+
* Whether to set the CSS variable on a specific scope element.
|
|
79
|
+
*/
|
|
80
|
+
scope?: {
|
|
81
|
+
element?: HTMLElement;
|
|
82
|
+
cssVarName?: string;
|
|
83
|
+
} | false;
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Return Value
|
|
88
|
+
|
|
89
|
+
The `flexible` function returns a cleanup function that removes all event listeners:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
const cleanup = flexible();
|
|
93
|
+
// Later...
|
|
94
|
+
cleanup();
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Examples
|
|
98
|
+
|
|
99
|
+
### Basic Example
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { flexible } from '@your-scope/flexible';
|
|
103
|
+
|
|
104
|
+
// Initialize with default settings
|
|
105
|
+
const cleanup = flexible();
|
|
106
|
+
|
|
107
|
+
// The font size will be automatically adjusted based on viewport width
|
|
108
|
+
// 100rem = 100vw = design width
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Custom Breakpoints Example
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { flexible } from '@your-scope/flexible';
|
|
115
|
+
|
|
116
|
+
const cleanup = flexible({
|
|
117
|
+
breakpoints: [1024, 768],
|
|
118
|
+
containers: [1920, 1024, 768],
|
|
119
|
+
basicContainer: 1920
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Custom Scope Example
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { flexible } from '@your-scope/flexible';
|
|
127
|
+
|
|
128
|
+
const container = document.querySelector('.container');
|
|
129
|
+
const cleanup = flexible({
|
|
130
|
+
scope: {
|
|
131
|
+
element: container,
|
|
132
|
+
cssVarName: '--container-rem'
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Flexible
|
|
2
|
+
|
|
3
|
+
一个轻量级且灵活的响应式布局系统,灵感来源于流行的 flexible.js。
|
|
4
|
+
|
|
5
|
+
[English Documentation](./README.md)
|
|
6
|
+
|
|
7
|
+
## 特性
|
|
8
|
+
|
|
9
|
+
- 🚀 轻量级且快速
|
|
10
|
+
- 📱 支持响应式设计
|
|
11
|
+
- 🔄 自动视口适配
|
|
12
|
+
- 🎯 可自定义断点
|
|
13
|
+
- 🎨 支持 CSS 变量
|
|
14
|
+
- 🧹 提供清理函数的简洁 API
|
|
15
|
+
|
|
16
|
+
## 安装
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @your-scope/flexible
|
|
20
|
+
# 或
|
|
21
|
+
yarn add @your-scope/flexible
|
|
22
|
+
# 或
|
|
23
|
+
pnpm add @your-scope/flexible
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 使用方法
|
|
27
|
+
|
|
28
|
+
### 基础用法
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { flexible } from '@your-scope/flexible';
|
|
32
|
+
|
|
33
|
+
// 使用默认设置初始化
|
|
34
|
+
const cleanup = flexible();
|
|
35
|
+
|
|
36
|
+
// 需要时清理
|
|
37
|
+
cleanup();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 自定义配置
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { flexible } from '@your-scope/flexible';
|
|
44
|
+
|
|
45
|
+
const cleanup = flexible({
|
|
46
|
+
breakpoints: [1024, 768],
|
|
47
|
+
containers: [1920, 1024, 768],
|
|
48
|
+
basicContainer: 1920,
|
|
49
|
+
scope: {
|
|
50
|
+
element: document.querySelector('.container'),
|
|
51
|
+
cssVarName: '--custom-rem'
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
### 配置选项
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
interface FlexibleOptions {
|
|
62
|
+
/**
|
|
63
|
+
* 断点数组(像素单位),从大到小排序。
|
|
64
|
+
* 默认为 [768]。
|
|
65
|
+
*/
|
|
66
|
+
breakpoints?: number[];
|
|
67
|
+
/**
|
|
68
|
+
* 与断点对应的容器宽度数组。
|
|
69
|
+
* 数组长度必须比断点数组多一个。
|
|
70
|
+
*/
|
|
71
|
+
containers?: number[];
|
|
72
|
+
/**
|
|
73
|
+
* 用于计算的基础容器宽度。
|
|
74
|
+
* 默认为 containers 数组的最后一项。
|
|
75
|
+
*/
|
|
76
|
+
basicContainer?: number;
|
|
77
|
+
/**
|
|
78
|
+
* 是否在特定作用域元素上设置 CSS 变量。
|
|
79
|
+
*/
|
|
80
|
+
scope?: {
|
|
81
|
+
element?: HTMLElement;
|
|
82
|
+
cssVarName?: string;
|
|
83
|
+
} | false;
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 返回值
|
|
88
|
+
|
|
89
|
+
`flexible` 函数返回一个清理函数,用于移除所有事件监听器:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
const cleanup = flexible();
|
|
93
|
+
// 之后...
|
|
94
|
+
cleanup();
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## 示例
|
|
98
|
+
|
|
99
|
+
### 基础示例
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { flexible } from '@your-scope/flexible';
|
|
103
|
+
|
|
104
|
+
// 使用默认设置初始化
|
|
105
|
+
const cleanup = flexible();
|
|
106
|
+
|
|
107
|
+
// 字体大小将根据视口宽度自动调整
|
|
108
|
+
// 100rem = 100vw = 设计宽度
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 自定义断点示例
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { flexible } from '@your-scope/flexible';
|
|
115
|
+
|
|
116
|
+
const cleanup = flexible({
|
|
117
|
+
breakpoints: [1024, 768],
|
|
118
|
+
containers: [1920, 1024, 768],
|
|
119
|
+
basicContainer: 1920
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### 自定义作用域示例
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { flexible } from '@your-scope/flexible';
|
|
127
|
+
|
|
128
|
+
const container = document.querySelector('.container');
|
|
129
|
+
const cleanup = flexible({
|
|
130
|
+
scope: {
|
|
131
|
+
element: container,
|
|
132
|
+
cssVarName: '--container-rem'
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## 许可证
|
|
138
|
+
|
|
139
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +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;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Initializes a flexible layout system that sets a CSS variable for rem units
|
|
3
|
+
* based on the viewport width and adaptively scales according to breakpoints.
|
|
4
|
+
*
|
|
5
|
+
* @param options - Configuration options for the flexible layout
|
|
6
|
+
* @returns A cleanup function to remove event listeners
|
|
7
|
+
*/
|
|
8
|
+
export declare const flexible: (options?: FlexibleOptions) => (() => void);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Options for the flexible layout function.
|
|
12
|
+
*/
|
|
13
|
+
export declare interface FlexibleOptions {
|
|
14
|
+
/**
|
|
15
|
+
* An array of breakpoints in pixels, from largest to smallest.
|
|
16
|
+
* Defaults to [768].
|
|
17
|
+
*/
|
|
18
|
+
breakpoints?: number[];
|
|
19
|
+
/**
|
|
20
|
+
* An array of container widths that corresponds to breakpoints.
|
|
21
|
+
* Must have exactly one more item than breakpoints array.
|
|
22
|
+
* For example, if breakpoints is [768], containers could be [375, 1920],
|
|
23
|
+
* where 375 is the width for viewport <= 768px and 1920 is for viewport > 768px.
|
|
24
|
+
*/
|
|
25
|
+
containers?: number[];
|
|
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)),
|
|
31
|
+
* which typically represents the largest viewport width.
|
|
32
|
+
*/
|
|
33
|
+
basicContainer?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Whether to set the CSS variable on a specific scope element.
|
|
36
|
+
* Defaults to false, which means setting the font size on the document element.
|
|
37
|
+
* If an object is provided, it can specify the element and CSS variable name.
|
|
38
|
+
*/
|
|
39
|
+
scope?: false | {
|
|
40
|
+
/**
|
|
41
|
+
* The scope element to set the CSS variable on.
|
|
42
|
+
* Defaults to document.documentElement.
|
|
43
|
+
*/
|
|
44
|
+
element?: HTMLElement;
|
|
45
|
+
/**
|
|
46
|
+
* The CSS variable name to use for the base rem value.
|
|
47
|
+
* Defaults to "--local-scope-rem".
|
|
48
|
+
*/
|
|
49
|
+
cssVarName?: string;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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;
|
|
4
|
+
let s = r / 100;
|
|
5
|
+
for (let n = 0; n < i.length; n++)
|
|
6
|
+
if (r <= i[n]) {
|
|
7
|
+
s = r * c(n);
|
|
8
|
+
break;
|
|
9
|
+
}
|
|
10
|
+
if (l) {
|
|
11
|
+
const { element: n = document.documentElement, cssVarName: w = "--local-scope-rem" } = l;
|
|
12
|
+
n.style.setProperty(w, s + "px");
|
|
13
|
+
} else
|
|
14
|
+
o.style.fontSize = s + "px";
|
|
15
|
+
};
|
|
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);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export {
|
|
21
|
+
m as flexible
|
|
22
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cherrywind/flexible",
|
|
3
|
+
"version": "0.0.2-alpha",
|
|
4
|
+
"private": false,
|
|
5
|
+
"keywords": [
|
|
6
|
+
"flexible"
|
|
7
|
+
],
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/susanforme/utils"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
"require": "./dist/index.cjs",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc && vite build",
|
|
24
|
+
"dev": "vite",
|
|
25
|
+
"lint": "cross-env oxlint && eslint \"{src,test}/**/*.{ts,tsx}\" --config eslint.config.mjs",
|
|
26
|
+
"preview": "vite preview",
|
|
27
|
+
"release": "changeset",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest"
|
|
30
|
+
}
|
|
31
|
+
}
|