@rokkit/core 1.8.0 → 1.8.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/dist/colors/index.d.ts +2 -2
- package/dist/dom.d.ts +18 -0
- package/dist/icons.d.ts +29 -0
- package/dist/keys.d.ts +13 -0
- package/dist/oklch.d.ts +23 -0
- package/dist/snippets.d.ts +24 -0
- package/dist/theme.d.ts +23 -40
- package/dist/utils.d.ts +6 -122
- package/dist/values.d.ts +32 -0
- package/package.json +1 -1
- package/src/colors/index.ts +3 -4
- package/src/dom.js +60 -0
- package/src/icons.js +94 -0
- package/src/keys.js +19 -0
- package/src/oklch.js +124 -0
- package/src/snippets.js +45 -0
- package/src/theme.ts +61 -35
- package/src/utils.js +9 -388
- package/src/values.js +56 -0
package/dist/colors/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { colors } from '@unocss/preset-mini/colors';
|
|
2
2
|
import extraColors from './extra.json';
|
|
3
3
|
export declare const shades: number[];
|
|
4
4
|
export declare const defaultPalette: string[];
|
|
@@ -44,5 +44,5 @@ export declare const syntaxColors: {
|
|
|
44
44
|
};
|
|
45
45
|
};
|
|
46
46
|
};
|
|
47
|
-
export declare const defaultColors:
|
|
47
|
+
export declare const defaultColors: typeof colors & typeof extraColors;
|
|
48
48
|
export { categoricalPalette, categoricalFamilies } from './brewer.js';
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detects text direction based on HTML lang attribute
|
|
3
|
+
* @returns {'ltr' | 'rtl'}
|
|
4
|
+
*/
|
|
5
|
+
export function detectDirection(): "ltr" | "rtl";
|
|
6
|
+
/**
|
|
7
|
+
* Checks if current document direction is RTL
|
|
8
|
+
* @returns {boolean}
|
|
9
|
+
*/
|
|
10
|
+
export function isRTL(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Finds the closest ancestor of the given element that has the given attribute.
|
|
13
|
+
*
|
|
14
|
+
* @param {HTMLElement} element
|
|
15
|
+
* @param {string} attribute
|
|
16
|
+
* @returns {HTMLElement|null}
|
|
17
|
+
*/
|
|
18
|
+
export function getClosestAncestorWithAttribute(element: HTMLElement, attribute: string): HTMLElement | null;
|
package/dist/icons.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates icon shortcuts for a collection of icons
|
|
3
|
+
*
|
|
4
|
+
* @param {string[]} icons
|
|
5
|
+
* @param {string} collection
|
|
6
|
+
* @param {string} variants
|
|
7
|
+
* @returns {Object}
|
|
8
|
+
*/
|
|
9
|
+
export function iconShortcuts(icons: string[], collection: string, variants: string): Object;
|
|
10
|
+
/**
|
|
11
|
+
* Detects whether an icon value is a CSS class or a literal character
|
|
12
|
+
* (kanji, emoji, single letter, etc.).
|
|
13
|
+
*
|
|
14
|
+
* Anything more than one grapheme is treated as a class — including bare
|
|
15
|
+
* semantic names like `file-svelte` which UnoCSS expands to the configured
|
|
16
|
+
* icon collection via `iconShortcuts(DEFAULT_ICONS, …)`. Single characters
|
|
17
|
+
* (kanji, emoji, single letters) render as literal text.
|
|
18
|
+
*
|
|
19
|
+
* @param {string | null | undefined} icon
|
|
20
|
+
* @returns {boolean} true if icon should render as a CSS class
|
|
21
|
+
*/
|
|
22
|
+
export function isIconClass(icon: string | null | undefined): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* A utility function that detects if a string is an image URL or image data (base64)
|
|
25
|
+
*
|
|
26
|
+
* @param {string} str - The string to check
|
|
27
|
+
* @returns {string|null} - Returns the original string if it's an image URL or image data, otherwise null
|
|
28
|
+
*/
|
|
29
|
+
export function getImage(str: string): string | null;
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Item keys ↔ index paths (`[0, 2]` ↔ `'0-2'`). */
|
|
2
|
+
/**
|
|
3
|
+
* Gets a key string from path
|
|
4
|
+
* @param {string[]} path
|
|
5
|
+
* @returns {string}
|
|
6
|
+
*/
|
|
7
|
+
export function getKeyFromPath(path: string[]): string;
|
|
8
|
+
/**
|
|
9
|
+
* Gets a path array from key string
|
|
10
|
+
* @param {string} key
|
|
11
|
+
* @returns {string[]}
|
|
12
|
+
*/
|
|
13
|
+
export function getPathFromKey(key: string): string[];
|
package/dist/oklch.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Colour conversion: hex ↔ `r,g,b` and hex ↔ OKLCH (CSS Color Level 4, via XYZ D65). */
|
|
2
|
+
/**
|
|
3
|
+
* convert hex string to `{r},{g},{b}`
|
|
4
|
+
* @param {string} hex
|
|
5
|
+
* @return {string}
|
|
6
|
+
*/
|
|
7
|
+
export function hex2rgb(hex: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Convert hex string to OKLCH components: `{L} {C} {H}`
|
|
10
|
+
* Uses CSS Color Level 4 two-step conversion via XYZ D65 for precision.
|
|
11
|
+
* @param {string} hex - 6-digit hex color (with or without #)
|
|
12
|
+
* @returns {string} space-separated OKLCH components for CSS `oklch(L C H / alpha)`
|
|
13
|
+
*/
|
|
14
|
+
export function hex2oklch(hex: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Convert OKLCH components back to hex string.
|
|
17
|
+
* Uses CSS Color Level 4 two-step conversion via XYZ D65 for precision.
|
|
18
|
+
* @param {number} L - Lightness 0-1
|
|
19
|
+
* @param {number} C - Chroma >= 0
|
|
20
|
+
* @param {number} H - Hue 0-360 degrees
|
|
21
|
+
* @returns {string} hex color string (#rrggbb)
|
|
22
|
+
*/
|
|
23
|
+
export function oklch2hex(L: number, C: number, H: number): string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get snippet function from an object
|
|
3
|
+
* @param {Object} obj
|
|
4
|
+
* @param {string} key
|
|
5
|
+
* @param {null|Function} defaultSnippet
|
|
6
|
+
* @returns {Function|undefined}
|
|
7
|
+
*/
|
|
8
|
+
export function getSnippet(obj: Object, key: string, defaultSnippet?: null | Function): Function | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Resolve which snippet to render for a proxy item.
|
|
11
|
+
*
|
|
12
|
+
* Reads the proxy's `snippet` field (via proxy.get('snippet'), honoring fields.snippet)
|
|
13
|
+
* for a per-item named override first (e.g. item.snippet = 'highlighted').
|
|
14
|
+
* Falls back to the component-level fallback snippet name (e.g. 'itemContent' / 'groupContent').
|
|
15
|
+
* Returns null if neither is found.
|
|
16
|
+
*
|
|
17
|
+
* @param {Record<string, unknown>} snippets - snippets passed to the component
|
|
18
|
+
* @param {{ get: (key: string) => unknown }} proxy - a ProxyItem; its snippet field is read via proxy.get('snippet')
|
|
19
|
+
* @param {string} [fallback] - fallback snippet name; defaults to ITEM_SNIPPET ('itemContent')
|
|
20
|
+
* @returns {Function | null}
|
|
21
|
+
*/
|
|
22
|
+
export function resolveSnippet(snippets: Record<string, unknown>, proxy: {
|
|
23
|
+
get: (key: string) => unknown;
|
|
24
|
+
}, fallback?: string): Function | null;
|
package/dist/theme.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import { ColorSpace } from './color-space';
|
|
2
|
+
/** One palette: shade (`'50'`…`'950'`, `'DEFAULT'`) → colour value in the adapter's notation. */
|
|
3
|
+
type Palette = Record<string, string>;
|
|
4
|
+
/** Palette name → palette. */
|
|
5
|
+
type Colors = Record<string, Palette>;
|
|
6
|
+
/** Role (`primary`, `surface`…) → palette name. */
|
|
7
|
+
type Mapping = Record<string, string>;
|
|
8
|
+
type Adapter = ReturnType<typeof ColorSpace.create>;
|
|
1
9
|
/**
|
|
2
10
|
* Generate shades for a color using css variable and a ColorSpace adapter.
|
|
3
11
|
*
|
|
@@ -5,9 +13,7 @@
|
|
|
5
13
|
* @param {string|import('./color-space').ColorSpace} space - color space name or adapter instance
|
|
6
14
|
* @returns {Record<string|number, string>}
|
|
7
15
|
*/
|
|
8
|
-
export declare function shadesOf(name:
|
|
9
|
-
DEFAULT: string;
|
|
10
|
-
};
|
|
16
|
+
export declare function shadesOf(name: string, space?: string | Adapter): Record<string, string>;
|
|
11
17
|
/**
|
|
12
18
|
* Constructs and returns the light and dark theme variants based on provided color mapping and color definitions.
|
|
13
19
|
*
|
|
@@ -16,19 +22,7 @@ export declare function shadesOf(name: any, space?: string): {
|
|
|
16
22
|
* @param {string} [colorSpace] - Color space name for CSS variable values.
|
|
17
23
|
* @returns {Array<Array>} An array containing two arrays, one for the light theme variant and another for the dark theme.
|
|
18
24
|
*/
|
|
19
|
-
export declare function themeRules(mapping:
|
|
20
|
-
surface: string;
|
|
21
|
-
ink: string;
|
|
22
|
-
primary: string;
|
|
23
|
-
secondary: string;
|
|
24
|
-
tertiary: string;
|
|
25
|
-
accent: string;
|
|
26
|
-
success: string;
|
|
27
|
-
warning: string;
|
|
28
|
-
danger: string;
|
|
29
|
-
error: string;
|
|
30
|
-
info: string;
|
|
31
|
-
} | undefined, colors: any, colorSpace: any): {};
|
|
25
|
+
export declare function themeRules(mapping?: Mapping, colors?: Colors, colorSpace?: string): Record<string, string>;
|
|
32
26
|
/**
|
|
33
27
|
* Generates "on-color" text shortcuts for readable text on colored backgrounds.
|
|
34
28
|
*
|
|
@@ -45,7 +39,7 @@ export declare function themeRules(mapping: {
|
|
|
45
39
|
* @param {string} [onColor='#fafafa'] - resolved on-color hex for this role
|
|
46
40
|
* @returns {Array} Array of shortcut definitions
|
|
47
41
|
*/
|
|
48
|
-
export declare function contrastShortcuts(name:
|
|
42
|
+
export declare function contrastShortcuts(name: string, onColor?: string): (RegExp | (([, end]: string[]) => string))[][];
|
|
49
43
|
/**
|
|
50
44
|
* Near-black / near-white on-color endpoints. Not pure #000/#fff — a hair of
|
|
51
45
|
* lift reads softer on a saturated fill while still clearing AA (near-black is
|
|
@@ -84,31 +78,19 @@ export declare class Theme {
|
|
|
84
78
|
* @param {import('./types.js').ColorTheme & { colorSpace?: string }} param0
|
|
85
79
|
*/
|
|
86
80
|
constructor({ colors, mapping, colorSpace }?: {
|
|
87
|
-
colors?:
|
|
88
|
-
mapping?:
|
|
89
|
-
|
|
90
|
-
ink: string;
|
|
91
|
-
primary: string;
|
|
92
|
-
secondary: string;
|
|
93
|
-
tertiary: string;
|
|
94
|
-
accent: string;
|
|
95
|
-
success: string;
|
|
96
|
-
warning: string;
|
|
97
|
-
danger: string;
|
|
98
|
-
error: string;
|
|
99
|
-
info: string;
|
|
100
|
-
} | undefined;
|
|
101
|
-
colorSpace?: string | undefined;
|
|
81
|
+
colors?: Colors;
|
|
82
|
+
mapping?: Record<string, string | null>;
|
|
83
|
+
colorSpace?: string;
|
|
102
84
|
});
|
|
103
|
-
get colors():
|
|
104
|
-
set colors(colors:
|
|
105
|
-
get mapping():
|
|
106
|
-
set mapping(mapping:
|
|
85
|
+
get colors(): Colors;
|
|
86
|
+
set colors(colors: Colors);
|
|
87
|
+
get mapping(): Record<string, string | null>;
|
|
88
|
+
set mapping(mapping: Record<string, string | null>);
|
|
107
89
|
get colorSpace(): string;
|
|
108
90
|
set colorSpace(colorSpace: string);
|
|
109
|
-
mapVariant(color:
|
|
110
|
-
getColorRules(mapping?: null):
|
|
111
|
-
getPalette(mapping?: null):
|
|
91
|
+
mapVariant(color: Palette, variant: string): Record<string, string>;
|
|
92
|
+
getColorRules(mapping?: Mapping | null): Record<string, Record<string, string>>;
|
|
93
|
+
getPalette(mapping?: Mapping | null): Record<string, string>;
|
|
112
94
|
/**
|
|
113
95
|
* Returns the named-token layer as a map of `--{name}: <resolved value>` entries.
|
|
114
96
|
*
|
|
@@ -146,5 +128,6 @@ export declare class Theme {
|
|
|
146
128
|
* Mirrors what getPalette does for all roles, but scoped to one.
|
|
147
129
|
*/
|
|
148
130
|
getPaletteForRole(role: string): Record<string, string>;
|
|
149
|
-
getShortcuts(name:
|
|
131
|
+
getShortcuts(name: string): (RegExp | (([, end]: string[]) => string))[][];
|
|
150
132
|
}
|
|
133
|
+
export {};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,122 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
* Checks if current document direction is RTL
|
|
8
|
-
* @returns {boolean}
|
|
9
|
-
*/
|
|
10
|
-
export function isRTL(): boolean;
|
|
11
|
-
/**
|
|
12
|
-
* Finds the closest ancestor of the given element that has the given attribute.
|
|
13
|
-
*
|
|
14
|
-
* @param {HTMLElement} element
|
|
15
|
-
* @param {string} attribute
|
|
16
|
-
* @returns {HTMLElement|null}
|
|
17
|
-
*/
|
|
18
|
-
export function getClosestAncestorWithAttribute(element: HTMLElement, attribute: string): HTMLElement | null;
|
|
19
|
-
/**
|
|
20
|
-
* A function that performs no operations.
|
|
21
|
-
*/
|
|
22
|
-
export function noop(): void;
|
|
23
|
-
/**
|
|
24
|
-
* Generates a random id
|
|
25
|
-
*
|
|
26
|
-
* @returns {string} A random id
|
|
27
|
-
*/
|
|
28
|
-
export function id(prefix?: string): string;
|
|
29
|
-
/**
|
|
30
|
-
* Check if a value is a json object
|
|
31
|
-
*
|
|
32
|
-
* @param {*} val
|
|
33
|
-
* @returns {boolean}
|
|
34
|
-
*/
|
|
35
|
-
export function isObject(val: any): boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Converts the value to a string. If the value is an object, it will convert it to a JSON string.
|
|
38
|
-
*
|
|
39
|
-
* @param {*} value
|
|
40
|
-
* @returns {string}
|
|
41
|
-
*/
|
|
42
|
-
export function toString(value: any): string;
|
|
43
|
-
/**
|
|
44
|
-
* Generates icon shortcuts for a collection of icons
|
|
45
|
-
*
|
|
46
|
-
* @param {string[]} icons
|
|
47
|
-
* @param {string} collection
|
|
48
|
-
* @param {string} variants
|
|
49
|
-
* @returns {Object}
|
|
50
|
-
*/
|
|
51
|
-
export function iconShortcuts(icons: string[], collection: string, variants: string): Object;
|
|
52
|
-
/**
|
|
53
|
-
* Scales the path by the size
|
|
54
|
-
*
|
|
55
|
-
* @param {number} size
|
|
56
|
-
* @param {string|number} x
|
|
57
|
-
* @returns {string|number}
|
|
58
|
-
*/
|
|
59
|
-
export function scaledPath(size: number, x: string | number): string | number;
|
|
60
|
-
/**
|
|
61
|
-
* Gets a key string from path
|
|
62
|
-
* @param {string[]} path
|
|
63
|
-
* @returns {string}
|
|
64
|
-
*/
|
|
65
|
-
export function getKeyFromPath(path: string[]): string;
|
|
66
|
-
/**
|
|
67
|
-
* Gets a path array from key string
|
|
68
|
-
* @param {string} key
|
|
69
|
-
* @returns {string[]}
|
|
70
|
-
*/
|
|
71
|
-
export function getPathFromKey(key: string): string[];
|
|
72
|
-
/**
|
|
73
|
-
* Get snippet function from an object
|
|
74
|
-
* @param {Object} obj
|
|
75
|
-
* @param {string} key
|
|
76
|
-
* @param {null|Function} defaultSnippet
|
|
77
|
-
* @returns {Function|undefined}
|
|
78
|
-
*/
|
|
79
|
-
export function getSnippet(obj: Object, key: string, defaultSnippet?: null | Function): Function | undefined;
|
|
80
|
-
export function resolveSnippet(snippets: any, proxy: any, fallback?: string): Function | null;
|
|
81
|
-
/**
|
|
82
|
-
* convert hex string to `{r},{g},{b}`
|
|
83
|
-
* @param {string} hex
|
|
84
|
-
* @return {string}
|
|
85
|
-
*/
|
|
86
|
-
export function hex2rgb(hex: string): string;
|
|
87
|
-
/**
|
|
88
|
-
* Convert hex string to OKLCH components: `{L} {C} {H}`
|
|
89
|
-
* Uses CSS Color Level 4 two-step conversion via XYZ D65 for precision.
|
|
90
|
-
* @param {string} hex - 6-digit hex color (with or without #)
|
|
91
|
-
* @returns {string} space-separated OKLCH components for CSS `oklch(L C H / alpha)`
|
|
92
|
-
*/
|
|
93
|
-
export function hex2oklch(hex: string): string;
|
|
94
|
-
/**
|
|
95
|
-
* Convert OKLCH components back to hex string.
|
|
96
|
-
* Uses CSS Color Level 4 two-step conversion via XYZ D65 for precision.
|
|
97
|
-
* @param {number} L - Lightness 0-1
|
|
98
|
-
* @param {number} C - Chroma >= 0
|
|
99
|
-
* @param {number} H - Hue 0-360 degrees
|
|
100
|
-
* @returns {string} hex color string (#rrggbb)
|
|
101
|
-
*/
|
|
102
|
-
export function oklch2hex(L: number, C: number, H: number): string;
|
|
103
|
-
/**
|
|
104
|
-
* Detects whether an icon value is a CSS class or a literal character
|
|
105
|
-
* (kanji, emoji, single letter, etc.).
|
|
106
|
-
*
|
|
107
|
-
* Anything more than one grapheme is treated as a class — including bare
|
|
108
|
-
* semantic names like `file-svelte` which UnoCSS expands to the configured
|
|
109
|
-
* icon collection via `iconShortcuts(DEFAULT_ICONS, …)`. Single characters
|
|
110
|
-
* (kanji, emoji, single letters) render as literal text.
|
|
111
|
-
*
|
|
112
|
-
* @param {string | null | undefined} icon
|
|
113
|
-
* @returns {boolean} true if icon should render as a CSS class
|
|
114
|
-
*/
|
|
115
|
-
export function isIconClass(icon: string | null | undefined): boolean;
|
|
116
|
-
/**
|
|
117
|
-
* A utility function that detects if a string is an image URL or image data (base64)
|
|
118
|
-
*
|
|
119
|
-
* @param {string} str - The string to check
|
|
120
|
-
* @returns {string|null} - Returns the original string if it's an image URL or image data, otherwise null
|
|
121
|
-
*/
|
|
122
|
-
export function getImage(str: string): string | null;
|
|
1
|
+
export * from "./dom.js";
|
|
2
|
+
export * from "./values.js";
|
|
3
|
+
export * from "./icons.js";
|
|
4
|
+
export * from "./keys.js";
|
|
5
|
+
export * from "./snippets.js";
|
|
6
|
+
export * from "./oklch.js";
|
package/dist/values.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that performs no operations.
|
|
3
|
+
*/
|
|
4
|
+
export function noop(): void;
|
|
5
|
+
/**
|
|
6
|
+
* Generates a random id
|
|
7
|
+
*
|
|
8
|
+
* @returns {string} A random id
|
|
9
|
+
*/
|
|
10
|
+
export function id(prefix?: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Check if a value is a json object
|
|
13
|
+
*
|
|
14
|
+
* @param {*} val
|
|
15
|
+
* @returns {boolean}
|
|
16
|
+
*/
|
|
17
|
+
export function isObject(val: any): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Converts the value to a string. If the value is an object, it will convert it to a JSON string.
|
|
20
|
+
*
|
|
21
|
+
* @param {*} value
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
export function toString(value: any): string;
|
|
25
|
+
/**
|
|
26
|
+
* Scales the path by the size
|
|
27
|
+
*
|
|
28
|
+
* @param {number} size
|
|
29
|
+
* @param {string|number} x
|
|
30
|
+
* @returns {string|number}
|
|
31
|
+
*/
|
|
32
|
+
export function scaledPath(size: number, x: string | number): string | number;
|
package/package.json
CHANGED
package/src/colors/index.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
-
// @ts-nocheck
|
|
3
1
|
import { colors } from '@unocss/preset-mini/colors'
|
|
4
|
-
import type { PresetMiniColors } from '@unocss/preset-mini/colors'
|
|
5
2
|
import syntaxColorPalette from './syntax.json' with { type: 'json' }
|
|
6
3
|
import extraColors from './extra.json' with { type: 'json' }
|
|
7
4
|
|
|
@@ -19,7 +16,9 @@ export const defaultPalette = [
|
|
|
19
16
|
]
|
|
20
17
|
|
|
21
18
|
export const syntaxColors = syntaxColorPalette
|
|
22
|
-
|
|
19
|
+
// `typeof colors`: preset-mini 66 no longer exports a `PresetMiniColors` type — the value is
|
|
20
|
+
// the source of truth for its own shape.
|
|
21
|
+
export const defaultColors: typeof colors & typeof extraColors = {
|
|
23
22
|
...colors,
|
|
24
23
|
...extraColors
|
|
25
24
|
}
|
package/src/dom.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** DOM reads: the document's text direction, and attribute lookups up the tree. */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* RTL language codes (ISO 639-1)
|
|
5
|
+
* @type {string[]}
|
|
6
|
+
*/
|
|
7
|
+
const RTL_LANGUAGES = [
|
|
8
|
+
'ar', // Arabic
|
|
9
|
+
'he', // Hebrew
|
|
10
|
+
'fa', // Persian/Farsi
|
|
11
|
+
'ur', // Urdu
|
|
12
|
+
'yi', // Yiddish
|
|
13
|
+
'ps', // Pashto
|
|
14
|
+
'sd', // Sindhi
|
|
15
|
+
'ug', // Uyghur
|
|
16
|
+
'ku', // Kurdish (Sorani)
|
|
17
|
+
'dv' // Divehi/Maldivian
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Checks dir/lang attributes of the html element for RTL
|
|
22
|
+
* @returns {'ltr' | 'rtl'}
|
|
23
|
+
*/
|
|
24
|
+
function dirFromHtmlElement() {
|
|
25
|
+
const htmlDir = document.documentElement.getAttribute('dir')
|
|
26
|
+
if (htmlDir === 'rtl' || htmlDir === 'ltr') return htmlDir
|
|
27
|
+
|
|
28
|
+
const lang = document.documentElement.getAttribute('lang')
|
|
29
|
+
const primaryLang = lang ? lang.split('-')[0].toLowerCase() : ''
|
|
30
|
+
return RTL_LANGUAGES.includes(primaryLang) ? 'rtl' : 'ltr'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Detects text direction based on HTML lang attribute
|
|
35
|
+
* @returns {'ltr' | 'rtl'}
|
|
36
|
+
*/
|
|
37
|
+
export function detectDirection() {
|
|
38
|
+
if (typeof document === 'undefined') return 'ltr'
|
|
39
|
+
return dirFromHtmlElement()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Checks if current document direction is RTL
|
|
44
|
+
* @returns {boolean}
|
|
45
|
+
*/
|
|
46
|
+
export function isRTL() {
|
|
47
|
+
return detectDirection() === 'rtl'
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Finds the closest ancestor of the given element that has the given attribute.
|
|
51
|
+
*
|
|
52
|
+
* @param {HTMLElement} element
|
|
53
|
+
* @param {string} attribute
|
|
54
|
+
* @returns {HTMLElement|null}
|
|
55
|
+
*/
|
|
56
|
+
export function getClosestAncestorWithAttribute(element, attribute) {
|
|
57
|
+
if (!element) return null
|
|
58
|
+
if (element.getAttribute(attribute)) return element
|
|
59
|
+
return getClosestAncestorWithAttribute(element.parentElement, attribute)
|
|
60
|
+
}
|
package/src/icons.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/** Icons: shortcut maps for a collection, class-vs-literal detection, image sources. */
|
|
2
|
+
import { DATA_IMAGE_REGEX } from './constants'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generates icon shortcuts for a collection of icons
|
|
6
|
+
*
|
|
7
|
+
* @param {string[]} icons
|
|
8
|
+
* @param {string} collection
|
|
9
|
+
* @param {string} variants
|
|
10
|
+
* @returns {Object}
|
|
11
|
+
*/
|
|
12
|
+
export function iconShortcuts(icons, collection, variants) {
|
|
13
|
+
const suffix = variants ? `-${variants}` : ''
|
|
14
|
+
const shortcuts = !collection
|
|
15
|
+
? {}
|
|
16
|
+
: icons.reduce(
|
|
17
|
+
(acc, name) => ({
|
|
18
|
+
...acc,
|
|
19
|
+
[name]: [collection, name].join(':') + suffix
|
|
20
|
+
}),
|
|
21
|
+
{}
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
return shortcuts
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Detects whether an icon value is a CSS class or a literal character
|
|
29
|
+
* (kanji, emoji, single letter, etc.).
|
|
30
|
+
*
|
|
31
|
+
* Anything more than one grapheme is treated as a class — including bare
|
|
32
|
+
* semantic names like `file-svelte` which UnoCSS expands to the configured
|
|
33
|
+
* icon collection via `iconShortcuts(DEFAULT_ICONS, …)`. Single characters
|
|
34
|
+
* (kanji, emoji, single letters) render as literal text.
|
|
35
|
+
*
|
|
36
|
+
* @param {string | null | undefined} icon
|
|
37
|
+
* @returns {boolean} true if icon should render as a CSS class
|
|
38
|
+
*/
|
|
39
|
+
export function isIconClass(icon) {
|
|
40
|
+
if (!icon || typeof icon !== 'string') return false
|
|
41
|
+
// Spread to count graphemes, not UTF-16 code units, so multi-codepoint
|
|
42
|
+
// emoji (e.g. 👨👩👧👦) and single CJK characters don't get misread.
|
|
43
|
+
return [...icon].length > 1
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Checks if a string is a valid image URL
|
|
48
|
+
*
|
|
49
|
+
* @param {string} str - The string to check
|
|
50
|
+
* @returns {boolean} - Returns true if the string is an image URL
|
|
51
|
+
*/
|
|
52
|
+
function isImageUrl(str) {
|
|
53
|
+
// Fallback regex-based validation
|
|
54
|
+
const fallbackValidation = () => {
|
|
55
|
+
const urlRegex = /^https?:\/\/.+\.(jpg|jpeg|png|gif|bmp|webp|svg|tiff)(\?.*)?$/i
|
|
56
|
+
return urlRegex.test(str)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Check if the string looks like a URL
|
|
60
|
+
try {
|
|
61
|
+
// Use browser-native URL constructor if available
|
|
62
|
+
if (typeof URL !== 'undefined') {
|
|
63
|
+
const url = new URL(str)
|
|
64
|
+
// Only accept HTTP/HTTPS protocols
|
|
65
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
// Check common image extensions
|
|
69
|
+
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg', '.tiff']
|
|
70
|
+
const path = url.pathname.toLowerCase()
|
|
71
|
+
return imageExtensions.some((ext) => path.endsWith(ext))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Fallback if URL constructor is not available
|
|
75
|
+
return fallbackValidation()
|
|
76
|
+
} catch {
|
|
77
|
+
// Fallback if URL constructor fails
|
|
78
|
+
return fallbackValidation()
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A utility function that detects if a string is an image URL or image data (base64)
|
|
83
|
+
*
|
|
84
|
+
* @param {string} str - The string to check
|
|
85
|
+
* @returns {string|null} - Returns the original string if it's an image URL or image data, otherwise null
|
|
86
|
+
*/
|
|
87
|
+
export function getImage(str) {
|
|
88
|
+
if (DATA_IMAGE_REGEX.test(str)) return str
|
|
89
|
+
// Check if it's a URL
|
|
90
|
+
|
|
91
|
+
if (isImageUrl(str)) return str
|
|
92
|
+
|
|
93
|
+
return null
|
|
94
|
+
}
|
package/src/keys.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Item keys ↔ index paths (`[0, 2]` ↔ `'0-2'`). */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Gets a key string from path
|
|
5
|
+
* @param {string[]} path
|
|
6
|
+
* @returns {string}
|
|
7
|
+
*/
|
|
8
|
+
export function getKeyFromPath(path) {
|
|
9
|
+
return Array.isArray(path) ? path.join('-') : [path].join('-')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Gets a path array from key string
|
|
14
|
+
* @param {string} key
|
|
15
|
+
* @returns {string[]}
|
|
16
|
+
*/
|
|
17
|
+
export function getPathFromKey(key) {
|
|
18
|
+
return key.split('-').map(Number)
|
|
19
|
+
}
|