@bifrostui/utils 2.0.0-beta.2 → 2.0.0-beta.3

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/dist/index.d.ts CHANGED
@@ -13,3 +13,4 @@ export { default as getRootElement } from './getRootElement';
13
13
  export { default as getBoundingClientRect } from './getBoundingClientRect';
14
14
  export * from './isType';
15
15
  export * from './render';
16
+ export * from './themeCreator';
package/dist/index.js CHANGED
@@ -75,6 +75,7 @@ var import_getRootElement = __toESM(require("./getRootElement"));
75
75
  var import_getBoundingClientRect = __toESM(require("./getBoundingClientRect"));
76
76
  __reExport(src_exports, require("./isType"), module.exports);
77
77
  __reExport(src_exports, require("./render"), module.exports);
78
+ __reExport(src_exports, require("./themeCreator"), module.exports);
78
79
  // Annotate the CommonJS export names for ESM import in node:
79
80
  0 && (module.exports = {
80
81
  blockTouch,
@@ -108,5 +109,6 @@ __reExport(src_exports, require("./render"), module.exports);
108
109
  useUniqueId,
109
110
  useValue,
110
111
  ...require("./isType"),
111
- ...require("./render")
112
+ ...require("./render"),
113
+ ...require("./themeCreator")
112
114
  });
@@ -0,0 +1,10 @@
1
+ import { MountThemeVarsOptions, ThemeCreatorOptions } from './index.types';
2
+ /**
3
+ * 挂载自定义主题
4
+ */
5
+ declare const mountThemeVars: (options: MountThemeVarsOptions) => void;
6
+ /**
7
+ * 创建主题
8
+ */
9
+ declare const createTheme: (options: ThemeCreatorOptions) => string;
10
+ export { createTheme, mountThemeVars };
@@ -0,0 +1,61 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var createTheme_exports = {};
19
+ __export(createTheme_exports, {
20
+ createTheme: () => createTheme,
21
+ mountThemeVars: () => mountThemeVars
22
+ });
23
+ module.exports = __toCommonJS(createTheme_exports);
24
+ const convertToCss = (styles) => {
25
+ let result = "";
26
+ const cssVarKeys = Object.keys(styles);
27
+ cssVarKeys.forEach((key, index) => {
28
+ const value = styles[key];
29
+ if (value.includes("PX")) {
30
+ result += "/* prettier-ignore */\n ";
31
+ }
32
+ result += `${key}: ${value};
33
+ ${index === cssVarKeys.length - 1 ? "" : " "}`;
34
+ });
35
+ return result;
36
+ };
37
+ const mountThemeVars = (options) => {
38
+ const { theme = "", container } = options || {};
39
+ const style = document.createElement("style");
40
+ style.type = "text/css";
41
+ style.appendChild(document.createTextNode(theme));
42
+ if (!container) {
43
+ document.head.appendChild(style);
44
+ return;
45
+ }
46
+ container.appendChild(style);
47
+ };
48
+ const createTheme = (options) => {
49
+ const {
50
+ rootSelector = ":root, [data-color-mode='light'][data-theme]",
51
+ cssVars = {}
52
+ } = options || {};
53
+ const rootContent = `${rootSelector} {
54
+ ${convertToCss(cssVars)} }`;
55
+ return rootContent;
56
+ };
57
+ // Annotate the CommonJS export names for ESM import in node:
58
+ 0 && (module.exports = {
59
+ createTheme,
60
+ mountThemeVars
61
+ });
@@ -0,0 +1,5 @@
1
+ import { CssVarToValueOptions } from './index.types';
2
+ /**
3
+ * css变量转值
4
+ */
5
+ export declare const cssVarToValue: (options: CssVarToValueOptions) => string | undefined;
@@ -0,0 +1,52 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var cssVarToValue_exports = {};
19
+ __export(cssVarToValue_exports, {
20
+ cssVarToValue: () => cssVarToValue
21
+ });
22
+ module.exports = __toCommonJS(cssVarToValue_exports);
23
+ var import_registry = require("@bifrostui/styles/registry");
24
+ const cssVarToValue = (options) => {
25
+ var _a;
26
+ const {
27
+ cssVar,
28
+ themeVars = (_a = import_registry.defaultLight) == null ? void 0 : _a.cssVars,
29
+ _visitedKeys = /* @__PURE__ */ new Set()
30
+ } = options || {};
31
+ if (!cssVar)
32
+ return "";
33
+ const variableKey = cssVar.replace(/^var\(/, "").replace(/\)$/, "");
34
+ const variableValue = themeVars == null ? void 0 : themeVars[variableKey];
35
+ if (_visitedKeys.has(variableKey)) {
36
+ console.warn(`\u68C0\u6D4B\u5230CSS\u53D8\u91CF\u5FAA\u73AF\u5F15\u7528: ${variableKey}`);
37
+ return variableValue;
38
+ }
39
+ const nextVisited = new Set(_visitedKeys).add(variableKey);
40
+ if (variableValue && variableValue.startsWith("var(")) {
41
+ return cssVarToValue({
42
+ cssVar: variableValue,
43
+ themeVars,
44
+ _visitedKeys: nextVisited
45
+ });
46
+ }
47
+ return variableValue;
48
+ };
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ cssVarToValue
52
+ });
@@ -0,0 +1,3 @@
1
+ export * from './createTheme';
2
+ export * from './cssVarToValue';
3
+ export * from './index.types';
@@ -0,0 +1,25 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var themeCreator_exports = {};
16
+ module.exports = __toCommonJS(themeCreator_exports);
17
+ __reExport(themeCreator_exports, require("./createTheme"), module.exports);
18
+ __reExport(themeCreator_exports, require("./cssVarToValue"), module.exports);
19
+ __reExport(themeCreator_exports, require("./index.types"), module.exports);
20
+ // Annotate the CommonJS export names for ESM import in node:
21
+ 0 && (module.exports = {
22
+ ...require("./createTheme"),
23
+ ...require("./cssVarToValue"),
24
+ ...require("./index.types")
25
+ });
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 挂载自定义主题选项
3
+ */
4
+ export interface MountThemeVarsOptions {
5
+ theme: string;
6
+ container?: HTMLElement;
7
+ }
8
+ export interface ThemeCreatorOptions {
9
+ /**
10
+ * 主题所挂在的根选择器
11
+ * @default `:root, [data-color-mode='light'][data-theme]`
12
+ */
13
+ rootSelector?: string;
14
+ /**
15
+ * 主题全局变量
16
+ */
17
+ cssVars: Record<string, string>;
18
+ }
19
+ /**
20
+ * css变量转值
21
+ */
22
+ export interface CssVarToValueOptions {
23
+ /**
24
+ * 当前主题
25
+ */
26
+ cssVar: string;
27
+ /**
28
+ * 当前主题模式
29
+ * @default defaultLight
30
+ */
31
+ themeVars?: Record<string, string>;
32
+ /**
33
+ * 记录已解析的变量路径
34
+ * 函数内部使用参数
35
+ */
36
+ _visitedKeys?: Set<string>;
37
+ }
@@ -0,0 +1,15 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var index_types_exports = {};
15
+ module.exports = __toCommonJS(index_types_exports);
package/es/index.d.ts CHANGED
@@ -13,3 +13,4 @@ export { default as getRootElement } from './getRootElement';
13
13
  export { default as getBoundingClientRect } from './getBoundingClientRect';
14
14
  export * from './isType';
15
15
  export * from './render';
16
+ export * from './themeCreator';
package/es/index.js CHANGED
@@ -33,6 +33,7 @@ import { default as default9 } from "./getRootElement";
33
33
  import { default as default10 } from "./getBoundingClientRect";
34
34
  export * from "./isType";
35
35
  export * from "./render";
36
+ export * from "./themeCreator";
36
37
  export {
37
38
  default8 as blockTouch,
38
39
  default3 as convertHexToRGBA,
@@ -0,0 +1,10 @@
1
+ import { MountThemeVarsOptions, ThemeCreatorOptions } from './index.types';
2
+ /**
3
+ * 挂载自定义主题
4
+ */
5
+ declare const mountThemeVars: (options: MountThemeVarsOptions) => void;
6
+ /**
7
+ * 创建主题
8
+ */
9
+ declare const createTheme: (options: ThemeCreatorOptions) => string;
10
+ export { createTheme, mountThemeVars };
@@ -0,0 +1,37 @@
1
+ const convertToCss = (styles) => {
2
+ let result = "";
3
+ const cssVarKeys = Object.keys(styles);
4
+ cssVarKeys.forEach((key, index) => {
5
+ const value = styles[key];
6
+ if (value.includes("PX")) {
7
+ result += "/* prettier-ignore */\n ";
8
+ }
9
+ result += `${key}: ${value};
10
+ ${index === cssVarKeys.length - 1 ? "" : " "}`;
11
+ });
12
+ return result;
13
+ };
14
+ const mountThemeVars = (options) => {
15
+ const { theme = "", container } = options || {};
16
+ const style = document.createElement("style");
17
+ style.type = "text/css";
18
+ style.appendChild(document.createTextNode(theme));
19
+ if (!container) {
20
+ document.head.appendChild(style);
21
+ return;
22
+ }
23
+ container.appendChild(style);
24
+ };
25
+ const createTheme = (options) => {
26
+ const {
27
+ rootSelector = ":root, [data-color-mode='light'][data-theme]",
28
+ cssVars = {}
29
+ } = options || {};
30
+ const rootContent = `${rootSelector} {
31
+ ${convertToCss(cssVars)} }`;
32
+ return rootContent;
33
+ };
34
+ export {
35
+ createTheme,
36
+ mountThemeVars
37
+ };
@@ -0,0 +1,5 @@
1
+ import { CssVarToValueOptions } from './index.types';
2
+ /**
3
+ * css变量转值
4
+ */
5
+ export declare const cssVarToValue: (options: CssVarToValueOptions) => string | undefined;
@@ -0,0 +1,29 @@
1
+ import { defaultLight } from "@bifrostui/styles/registry";
2
+ const cssVarToValue = (options) => {
3
+ var _a;
4
+ const {
5
+ cssVar,
6
+ themeVars = (_a = defaultLight) == null ? void 0 : _a.cssVars,
7
+ _visitedKeys = /* @__PURE__ */ new Set()
8
+ } = options || {};
9
+ if (!cssVar)
10
+ return "";
11
+ const variableKey = cssVar.replace(/^var\(/, "").replace(/\)$/, "");
12
+ const variableValue = themeVars == null ? void 0 : themeVars[variableKey];
13
+ if (_visitedKeys.has(variableKey)) {
14
+ console.warn(`\u68C0\u6D4B\u5230CSS\u53D8\u91CF\u5FAA\u73AF\u5F15\u7528: ${variableKey}`);
15
+ return variableValue;
16
+ }
17
+ const nextVisited = new Set(_visitedKeys).add(variableKey);
18
+ if (variableValue && variableValue.startsWith("var(")) {
19
+ return cssVarToValue({
20
+ cssVar: variableValue,
21
+ themeVars,
22
+ _visitedKeys: nextVisited
23
+ });
24
+ }
25
+ return variableValue;
26
+ };
27
+ export {
28
+ cssVarToValue
29
+ };
@@ -0,0 +1,3 @@
1
+ export * from './createTheme';
2
+ export * from './cssVarToValue';
3
+ export * from './index.types';
@@ -0,0 +1,3 @@
1
+ export * from "./createTheme";
2
+ export * from "./cssVarToValue";
3
+ export * from "./index.types";
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 挂载自定义主题选项
3
+ */
4
+ export interface MountThemeVarsOptions {
5
+ theme: string;
6
+ container?: HTMLElement;
7
+ }
8
+ export interface ThemeCreatorOptions {
9
+ /**
10
+ * 主题所挂在的根选择器
11
+ * @default `:root, [data-color-mode='light'][data-theme]`
12
+ */
13
+ rootSelector?: string;
14
+ /**
15
+ * 主题全局变量
16
+ */
17
+ cssVars: Record<string, string>;
18
+ }
19
+ /**
20
+ * css变量转值
21
+ */
22
+ export interface CssVarToValueOptions {
23
+ /**
24
+ * 当前主题
25
+ */
26
+ cssVar: string;
27
+ /**
28
+ * 当前主题模式
29
+ * @default defaultLight
30
+ */
31
+ themeVars?: Record<string, string>;
32
+ /**
33
+ * 记录已解析的变量路径
34
+ * 函数内部使用参数
35
+ */
36
+ _visitedKeys?: Set<string>;
37
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bifrostui/utils",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.3",
4
4
  "description": "BUI React utilities for building components.",
5
5
  "main": "dist/index.js",
6
6
  "module": "es/index.js",
@@ -16,7 +16,8 @@
16
16
  "glob": "11.0.0"
17
17
  },
18
18
  "dependencies": {
19
- "react-is": "^18.0.0"
19
+ "react-is": "^18.0.0",
20
+ "@bifrostui/styles": "2.0.0-beta.3"
20
21
  },
21
22
  "peerDependencies": {
22
23
  "@tarojs/runtime": "^3.0.0",