@seyuna/postcss 1.0.0-canary.14 → 1.0.0-canary.15
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/.github/workflows/release.yml +1 -1
- package/CHANGELOG.md +7 -0
- package/README.md +179 -0
- package/dist/at-rules/color-scheme.d.ts +4 -0
- package/dist/at-rules/color-scheme.js +43 -0
- package/dist/at-rules/color.d.ts +3 -2
- package/dist/at-rules/color.js +10 -21
- package/dist/at-rules/conditional.d.ts +6 -0
- package/dist/at-rules/conditional.js +29 -0
- package/dist/at-rules/container.d.ts +2 -1
- package/dist/at-rules/container.js +12 -8
- package/dist/at-rules/custom-media.d.ts +15 -0
- package/dist/at-rules/custom-media.js +40 -0
- package/dist/at-rules/index.d.ts +3 -2
- package/dist/at-rules/index.js +22 -22
- package/dist/at-rules/mixin.d.ts +10 -0
- package/dist/at-rules/mixin.js +37 -0
- package/dist/config.d.ts +20 -0
- package/dist/config.js +47 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +14 -0
- package/dist/functions/color.d.ts +7 -2
- package/dist/functions/color.js +103 -27
- package/dist/functions/index.d.ts +2 -1
- package/dist/functions/index.js +10 -12
- package/dist/functions/theme.d.ts +6 -0
- package/dist/functions/theme.js +17 -0
- package/dist/helpers.d.ts +3 -2
- package/dist/helpers.js +13 -28
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -5
- package/dist/parser.d.ts +4 -0
- package/dist/parser.js +59 -0
- package/dist/plugin.d.ts +2 -4
- package/dist/plugin.js +17 -22
- package/dist/types.d.ts +1 -19
- package/dist/types.js +1 -2
- package/package.json +12 -5
- package/src/at-rules/color-scheme.ts +52 -0
- package/src/at-rules/color.ts +11 -15
- package/src/at-rules/conditional.ts +34 -0
- package/src/at-rules/container.ts +13 -3
- package/src/at-rules/custom-media.ts +50 -0
- package/src/at-rules/index.ts +13 -6
- package/src/at-rules/mixin.ts +46 -0
- package/src/config.ts +74 -0
- package/src/errors.ts +23 -0
- package/src/functions/color.ts +120 -26
- package/src/functions/index.ts +9 -4
- package/src/functions/theme.ts +20 -0
- package/src/helpers.ts +23 -22
- package/src/index.ts +3 -1
- package/src/parser.ts +80 -0
- package/src/plugin.ts +13 -21
- package/src/types.ts +1 -19
- package/tests/plugin.test.ts +251 -0
- package/tsconfig.json +2 -2
- package/dist/at-rules/dark.d.ts +0 -23
- package/dist/at-rules/dark.js +0 -65
- package/dist/at-rules/light.d.ts +0 -23
- package/dist/at-rules/light.js +0 -65
- package/dist/functions/spacing.d.ts +0 -1
- package/dist/functions/spacing.js +0 -6
- package/src/at-rules/dark.ts +0 -69
- package/src/at-rules/light.ts +0 -69
- package/src/functions/spacing.ts +0 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AtRule, ChildNode } from "postcss";
|
|
2
|
+
import { PluginContext } from "../config";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Custom PostCSS plugin handler for responsive at-rules.
|
|
@@ -16,9 +17,11 @@ import { AtRule, ChildNode } from "postcss";
|
|
|
16
17
|
* }
|
|
17
18
|
*
|
|
18
19
|
*/
|
|
19
|
-
export default function container(atRule: AtRule) {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
export default function container(atRule: AtRule, context: PluginContext) {
|
|
21
|
+
const { config } = context;
|
|
22
|
+
|
|
23
|
+
// Default breakpoints
|
|
24
|
+
const defaultBreakpoints: Record<string, string> = {
|
|
22
25
|
xs: "20rem",
|
|
23
26
|
sm: "40rem",
|
|
24
27
|
md: "48rem",
|
|
@@ -27,6 +30,12 @@ export default function container(atRule: AtRule) {
|
|
|
27
30
|
"2xl": "96rem",
|
|
28
31
|
};
|
|
29
32
|
|
|
33
|
+
// Merge with config if available (assuming config.ui.breakpoints exists)
|
|
34
|
+
const breakpoints = {
|
|
35
|
+
...defaultBreakpoints,
|
|
36
|
+
...((config as any).ui?.theme?.breakpoints || {}),
|
|
37
|
+
};
|
|
38
|
+
|
|
30
39
|
if (Object.keys(breakpoints).includes(atRule.name)) {
|
|
31
40
|
const minWidth = breakpoints[atRule.name];
|
|
32
41
|
|
|
@@ -38,6 +47,7 @@ export default function container(atRule: AtRule) {
|
|
|
38
47
|
const containerAtRule = new AtRule({
|
|
39
48
|
name: "container",
|
|
40
49
|
params: `(min-width: ${minWidth})`,
|
|
50
|
+
source: atRule.source,
|
|
41
51
|
});
|
|
42
52
|
|
|
43
53
|
clonedNodes.forEach((node) => containerAtRule.append(node));
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AtRule } from "postcss";
|
|
2
|
+
import { PluginContext } from "../config";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Global store for CSS-defined custom media (if needed)
|
|
6
|
+
* However, we primarily use the config.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Handler for @media at-rules to resolve custom media tokens
|
|
11
|
+
*/
|
|
12
|
+
export function resolveCustomMedia(atRule: AtRule, context: PluginContext) {
|
|
13
|
+
const { config } = context;
|
|
14
|
+
const mediaTokens = (config as any).media || {};
|
|
15
|
+
|
|
16
|
+
// Regex to find --tokens anywhere
|
|
17
|
+
let params = atRule.params;
|
|
18
|
+
const tokenRegex = /--[a-zA-Z0-9\-_]+/g;
|
|
19
|
+
|
|
20
|
+
params = params.replace(tokenRegex, (match) => {
|
|
21
|
+
const tokenName = match.slice(2);
|
|
22
|
+
if (mediaTokens[tokenName]) {
|
|
23
|
+
return mediaTokens[tokenName];
|
|
24
|
+
}
|
|
25
|
+
return match;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Clean up double parentheses like ((...)) if any
|
|
29
|
+
params = params.replace(/\(\((.+)\)\)/g, '($1)');
|
|
30
|
+
|
|
31
|
+
atRule.params = params;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* [Future-proofing] Handler for @custom-media --name query
|
|
36
|
+
* Adds to the config-like store for the current run
|
|
37
|
+
*/
|
|
38
|
+
export function defineCustomMedia(atRule: AtRule, context: PluginContext) {
|
|
39
|
+
const match = atRule.params.match(/^(--[a-zA-Z0-9\-_]+)\s+(.+)$/);
|
|
40
|
+
if (match) {
|
|
41
|
+
const name = match[1].slice(2);
|
|
42
|
+
const query = match[2];
|
|
43
|
+
|
|
44
|
+
if (!(context.config as any).media) {
|
|
45
|
+
(context.config as any).media = {};
|
|
46
|
+
}
|
|
47
|
+
(context.config as any).media[name] = query;
|
|
48
|
+
}
|
|
49
|
+
atRule.remove();
|
|
50
|
+
}
|
package/src/at-rules/index.ts
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
import dark from "./dark";
|
|
3
|
-
import light from "./light";
|
|
4
|
-
import container from "./container";
|
|
1
|
+
import { AtRule } from "postcss";
|
|
5
2
|
import { eachStandardColor, eachFixedColor } from "./color";
|
|
6
|
-
import
|
|
3
|
+
import container from "./container";
|
|
4
|
+
import { light, dark } from "./color-scheme";
|
|
5
|
+
import { defineMixin, applyMixin } from "./mixin";
|
|
6
|
+
import { conditional } from "./conditional";
|
|
7
|
+
import { resolveCustomMedia, defineCustomMedia } from "./custom-media";
|
|
8
|
+
import { PluginContext } from "../config";
|
|
7
9
|
|
|
8
10
|
// Each handler has a name (matches the at-rule) and the function
|
|
9
11
|
export interface AtRuleHandler {
|
|
10
12
|
name: string;
|
|
11
|
-
handler: (atRule: AtRule) => void;
|
|
13
|
+
handler: (atRule: AtRule, context: PluginContext) => void;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
// Ordered array ensures execution order
|
|
15
17
|
export const atRuleHandlers: AtRuleHandler[] = [
|
|
18
|
+
{ name: "define-mixin", handler: defineMixin },
|
|
19
|
+
{ name: "custom-media", handler: defineCustomMedia },
|
|
20
|
+
{ name: "apply", handler: applyMixin },
|
|
21
|
+
{ name: "if", handler: conditional },
|
|
22
|
+
{ name: "media", handler: resolveCustomMedia },
|
|
16
23
|
{ name: "each-standard-color", handler: eachStandardColor }, // first
|
|
17
24
|
{ name: "each-fixed-color", handler: eachFixedColor },
|
|
18
25
|
{ name: "light", handler: light },
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { AtRule, ChildNode } from "postcss";
|
|
2
|
+
import { PluginContext } from "../config";
|
|
3
|
+
import { reportError } from "../errors";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handler for @define-mixin [name] { ... }
|
|
7
|
+
*/
|
|
8
|
+
export function defineMixin(atRule: AtRule, context: PluginContext) {
|
|
9
|
+
const name = atRule.params.trim();
|
|
10
|
+
if (!name) {
|
|
11
|
+
reportError("Mixin name is required", atRule, context);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Store the nodes (cloned)
|
|
16
|
+
context.mixins[name] = atRule.nodes?.map(n => n.clone()) || [];
|
|
17
|
+
|
|
18
|
+
// Remove the at-rule from the output
|
|
19
|
+
atRule.remove();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Handler for @apply [name]
|
|
24
|
+
*/
|
|
25
|
+
export function applyMixin(atRule: AtRule, context: PluginContext) {
|
|
26
|
+
const name = atRule.params.trim();
|
|
27
|
+
if (!name) {
|
|
28
|
+
reportError("Mixin name is required for @apply", atRule, context);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const mixinNodes = context.mixins[name];
|
|
33
|
+
if (!mixinNodes) {
|
|
34
|
+
reportError(`Mixin "${name}" not found`, atRule, context);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Inject the nodes, ensuring they have the correct source for mapping
|
|
39
|
+
const nodesToInject = mixinNodes.map(n => {
|
|
40
|
+
const cloned = n.clone();
|
|
41
|
+
cloned.source = atRule.source;
|
|
42
|
+
return cloned;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
atRule.replaceWith(...nodesToInject);
|
|
46
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { SeyunaConfig } from './types';
|
|
4
|
+
import { FunctionMap } from './parser';
|
|
5
|
+
|
|
6
|
+
export interface PluginOptions {
|
|
7
|
+
configPath?: string;
|
|
8
|
+
modeAttribute?: string;
|
|
9
|
+
strict?: boolean;
|
|
10
|
+
config?: SeyunaConfig;
|
|
11
|
+
functions?: FunctionMap;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
import { ChildNode } from 'postcss';
|
|
15
|
+
|
|
16
|
+
export interface PluginContext {
|
|
17
|
+
config: SeyunaConfig;
|
|
18
|
+
options: Required<PluginOptions>;
|
|
19
|
+
functions: FunctionMap;
|
|
20
|
+
mixins: Record<string, ChildNode[]>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const DEFAULT_OPTIONS: Required<PluginOptions> = {
|
|
24
|
+
configPath: 'seyuna.json',
|
|
25
|
+
modeAttribute: 'data-mode',
|
|
26
|
+
strict: false,
|
|
27
|
+
config: undefined as any,
|
|
28
|
+
functions: undefined as any,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
let cachedConfig: SeyunaConfig | null = null;
|
|
32
|
+
let cachedConfigPath: string | null = null;
|
|
33
|
+
|
|
34
|
+
export function loadConfig(options: PluginOptions = {}): { config: SeyunaConfig, options: Required<PluginOptions> } {
|
|
35
|
+
const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
36
|
+
|
|
37
|
+
if (mergedOptions.config) {
|
|
38
|
+
return { config: mergedOptions.config, options: mergedOptions };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const configPath = path.resolve(process.cwd(), mergedOptions.configPath);
|
|
42
|
+
|
|
43
|
+
// Cache config if it's the same path
|
|
44
|
+
if (cachedConfig && cachedConfigPath === configPath) {
|
|
45
|
+
return { config: cachedConfig, options: mergedOptions };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
if (!fs.existsSync(configPath)) {
|
|
50
|
+
if (mergedOptions.strict) {
|
|
51
|
+
throw new Error(`Seyuna config not found at ${configPath}`);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
config: { ui: { theme: { hues: {}, light: { colors: {} }, dark: { colors: {} } } } } as any,
|
|
55
|
+
options: mergedOptions,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const data = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
60
|
+
cachedConfig = data;
|
|
61
|
+
cachedConfigPath = configPath;
|
|
62
|
+
|
|
63
|
+
return { config: data, options: mergedOptions };
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (mergedOptions.strict) {
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
console.warn(`[Seyuna PostCSS] Warning: Failed to load config: ${error instanceof Error ? error.message : String(error)}`);
|
|
69
|
+
return {
|
|
70
|
+
config: { ui: { theme: { hues: {}, light: { colors: {} }, dark: { colors: {} } } } } as any,
|
|
71
|
+
options: mergedOptions,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Node } from 'postcss';
|
|
2
|
+
import { PluginContext } from './config';
|
|
3
|
+
|
|
4
|
+
export function reportError(
|
|
5
|
+
message: string,
|
|
6
|
+
node: Node,
|
|
7
|
+
context: PluginContext,
|
|
8
|
+
options: { word?: string; index?: number } = {}
|
|
9
|
+
) {
|
|
10
|
+
const { options: pluginOptions } = context;
|
|
11
|
+
const formattedMessage = `[Seyuna PostCSS] ${message}`;
|
|
12
|
+
|
|
13
|
+
if (pluginOptions.strict) {
|
|
14
|
+
throw node.error(formattedMessage, options);
|
|
15
|
+
} else {
|
|
16
|
+
reportWarning(formattedMessage, node);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function reportWarning(message: string, node: Node) {
|
|
21
|
+
const result = node.root().toResult();
|
|
22
|
+
node.warn(result, `[Seyuna PostCSS] ${message}`);
|
|
23
|
+
}
|
package/src/functions/color.ts
CHANGED
|
@@ -1,49 +1,143 @@
|
|
|
1
|
+
import { PluginContext } from "../config";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utility to parse an oklch string and return its parts
|
|
5
|
+
*/
|
|
6
|
+
function parseOklch(color: string) {
|
|
7
|
+
const match = color.match(/oklch\(([^ ]+) ([^ ]+) ([^ /]+)(?: \/ ([^)]+))?\)/);
|
|
8
|
+
if (!match) return null;
|
|
9
|
+
return {
|
|
10
|
+
l: match[1],
|
|
11
|
+
c: match[2],
|
|
12
|
+
h: match[3],
|
|
13
|
+
a: match[4] || "1",
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
1
17
|
export function sc(
|
|
18
|
+
context: PluginContext,
|
|
2
19
|
name: string,
|
|
3
20
|
alpha?: string,
|
|
4
21
|
lightness?: string,
|
|
5
22
|
chroma?: string
|
|
6
23
|
) {
|
|
7
|
-
let a: string = "1";
|
|
8
|
-
let l: string = "var(--lightness)";
|
|
9
|
-
let c: string = "var(--chroma)";
|
|
10
|
-
|
|
11
|
-
if (alpha && alpha !== "null") {
|
|
12
|
-
a = alpha;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (lightness && lightness !== "null") {
|
|
16
|
-
l = lightness;
|
|
17
|
-
}
|
|
24
|
+
let a: string = alpha && alpha !== "null" ? alpha : "1";
|
|
25
|
+
let l: string = lightness && lightness !== "null" ? lightness : "var(--lightness)";
|
|
26
|
+
let c: string = chroma && chroma !== "null" ? chroma : "var(--chroma)";
|
|
27
|
+
let h: string = `var(--${name}-hue)`;
|
|
18
28
|
|
|
19
|
-
|
|
20
|
-
c = chroma;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return `oklch(${l} ${c} var(--${name}-hue) / ${a})`;
|
|
29
|
+
return `oklch(${l} ${c} ${h} / ${a})`;
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
export function fc(
|
|
33
|
+
context: PluginContext,
|
|
27
34
|
name: string,
|
|
28
35
|
alpha?: string,
|
|
29
36
|
lightness?: string,
|
|
30
37
|
chroma?: string
|
|
31
38
|
) {
|
|
32
|
-
let a: string = "1";
|
|
33
|
-
let l: string = `var(--${name}-lightness)`;
|
|
34
|
-
let c: string = `var(--${name}-chroma)`;
|
|
39
|
+
let a: string = alpha && alpha !== "null" ? alpha : "1";
|
|
40
|
+
let l: string = lightness && lightness !== "null" ? lightness : `var(--${name}-lightness)`;
|
|
41
|
+
let c: string = chroma && chroma !== "null" ? chroma : `var(--${name}-chroma)`;
|
|
42
|
+
let h: string = `var(--${name}-hue)`;
|
|
43
|
+
|
|
44
|
+
return `oklch(${l} ${c} ${h} / ${a})`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Resolves a color name to its CSS variables based on its type (standard or fixed)
|
|
49
|
+
*/
|
|
50
|
+
function getColorVariables(context: PluginContext, color: string) {
|
|
51
|
+
const { config } = context;
|
|
52
|
+
const hues = config?.ui?.theme?.hues || {};
|
|
53
|
+
const colors = config?.ui?.theme?.colors || {};
|
|
54
|
+
const lightColors = config?.ui?.theme?.light?.colors || {};
|
|
55
|
+
const darkColors = config?.ui?.theme?.dark?.colors || {};
|
|
35
56
|
|
|
36
|
-
if
|
|
37
|
-
|
|
57
|
+
// Check if it's a standard color (sc)
|
|
58
|
+
if (color in hues) {
|
|
59
|
+
return {
|
|
60
|
+
l: "var(--lightness)",
|
|
61
|
+
c: "var(--chroma)",
|
|
62
|
+
h: `var(--${color}-hue)`,
|
|
63
|
+
};
|
|
38
64
|
}
|
|
39
65
|
|
|
40
|
-
if
|
|
41
|
-
|
|
66
|
+
// Check if it's a fixed color (fc)
|
|
67
|
+
if (color in colors || color in lightColors || color in darkColors) {
|
|
68
|
+
return {
|
|
69
|
+
l: `var(--${color}-lightness)`,
|
|
70
|
+
c: `var(--${color}-chroma)`,
|
|
71
|
+
h: `var(--${color}-hue)`,
|
|
72
|
+
};
|
|
42
73
|
}
|
|
43
74
|
|
|
44
|
-
if
|
|
45
|
-
|
|
75
|
+
// Fallback to assume it's a fixed color if unknown but used as a name
|
|
76
|
+
return {
|
|
77
|
+
l: `var(--${color}-lightness)`,
|
|
78
|
+
c: `var(--${color}-chroma)`,
|
|
79
|
+
h: `var(--${color}-hue)`,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function alpha(context: PluginContext, color: string, value: string) {
|
|
84
|
+
const parsed = parseOklch(color);
|
|
85
|
+
if (parsed) {
|
|
86
|
+
return `oklch(${parsed.l} ${parsed.c} ${parsed.h} / ${value})`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
90
|
+
return `oklch(${l} ${c} ${h} / ${value})`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function lighten(context: PluginContext, color: string, amount: string) {
|
|
94
|
+
const parsed = parseOklch(color);
|
|
95
|
+
|
|
96
|
+
if (parsed) {
|
|
97
|
+
if (parsed.l.startsWith('var(')) {
|
|
98
|
+
return `oklch(calc(${parsed.l} + ${amount}) ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const lValue = parseFloat(parsed.l);
|
|
102
|
+
const amtValue = parseFloat(amount);
|
|
103
|
+
return `oklch(${Math.min(1, lValue + amtValue)} ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
107
|
+
return `oklch(calc(${l} + ${amount}) ${c} ${h} / 1)`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function darken(context: PluginContext, color: string, amount: string) {
|
|
111
|
+
const parsed = parseOklch(color);
|
|
112
|
+
|
|
113
|
+
if (parsed) {
|
|
114
|
+
if (parsed.l.startsWith('var(')) {
|
|
115
|
+
return `oklch(calc(${parsed.l} - ${amount}) ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const lValue = parseFloat(parsed.l);
|
|
119
|
+
const amtValue = parseFloat(amount);
|
|
120
|
+
return `oklch(${Math.max(0, lValue - amtValue)} ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
124
|
+
return `oklch(calc(${l} - ${amount}) ${c} ${h} / 1)`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function contrast(context: PluginContext, color: string) {
|
|
128
|
+
const parsed = parseOklch(color);
|
|
129
|
+
let l: string;
|
|
130
|
+
|
|
131
|
+
if (parsed) {
|
|
132
|
+
l = parsed.l;
|
|
133
|
+
} else {
|
|
134
|
+
const vars = getColorVariables(context, color);
|
|
135
|
+
l = vars.l;
|
|
46
136
|
}
|
|
47
137
|
|
|
48
|
-
|
|
138
|
+
// Dynamic CSS contrast logic:
|
|
139
|
+
// (L - 0.6) * -1000 will be:
|
|
140
|
+
// - very negative if L > 0.6 (clamped to 0 / black)
|
|
141
|
+
// - very positive if L < 0.6 (clamped to 1 / white)
|
|
142
|
+
return `oklch(calc((${l} - 0.6) * -1000) 0 0)`;
|
|
49
143
|
}
|
package/src/functions/index.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { sc, fc } from "./color";
|
|
2
|
-
import
|
|
1
|
+
import { sc, fc, alpha, lighten, darken, contrast } from "./color";
|
|
2
|
+
import { theme } from "./theme";
|
|
3
|
+
import { PluginContext } from "../config";
|
|
3
4
|
|
|
4
|
-
export type FnHandler = (...args: string[]) => string;
|
|
5
|
+
export type FnHandler = (context: PluginContext, ...args: string[]) => string;
|
|
5
6
|
|
|
6
7
|
export const functions: Record<string, FnHandler> = {
|
|
7
8
|
sc,
|
|
8
9
|
fc,
|
|
9
|
-
|
|
10
|
+
alpha,
|
|
11
|
+
lighten,
|
|
12
|
+
darken,
|
|
13
|
+
contrast,
|
|
14
|
+
theme,
|
|
10
15
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PluginContext } from "../config";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Accesses values from the Seyuna configuration using dot notation
|
|
5
|
+
* Example: theme(ui.theme.breakpoints.tablet)
|
|
6
|
+
*/
|
|
7
|
+
export function theme(context: PluginContext, path: string): string {
|
|
8
|
+
const parts = path.split('.');
|
|
9
|
+
let current: any = context.config;
|
|
10
|
+
|
|
11
|
+
for (const part of parts) {
|
|
12
|
+
if (current && typeof current === 'object' && part in current) {
|
|
13
|
+
current = current[part];
|
|
14
|
+
} else {
|
|
15
|
+
return path; // Return original path if not found
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return String(current);
|
|
20
|
+
}
|
package/src/helpers.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { Rule, ChildNode, Declaration, AtRule } from "postcss";
|
|
2
|
-
import {
|
|
2
|
+
import { processFunctions } from "./parser";
|
|
3
|
+
import { PluginContext } from "./config";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Helper: clone nodes and replace {name} placeholders safely
|
|
6
7
|
* Returns only valid Rules or Declarations (never raw AtRules)
|
|
7
8
|
*/
|
|
8
|
-
export function cloneNodes(
|
|
9
|
+
export function cloneNodes(
|
|
10
|
+
nodes: ChildNode[],
|
|
11
|
+
name: string,
|
|
12
|
+
context: PluginContext
|
|
13
|
+
): ChildNode[] {
|
|
14
|
+
const { functions: fnMap } = context;
|
|
15
|
+
|
|
9
16
|
return nodes.flatMap((node) => {
|
|
10
17
|
const cloned = node.clone();
|
|
11
18
|
|
|
@@ -13,22 +20,9 @@ export function cloneNodes(nodes: ChildNode[], name: string): ChildNode[] {
|
|
|
13
20
|
const decl = cloned as Declaration;
|
|
14
21
|
let value = decl.value.replace(/\{name\}/g, name);
|
|
15
22
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
.split(",")
|
|
20
|
-
.map((s) => s.trim());
|
|
21
|
-
if (args) value = sc(...(args as [string, string?, string?, string?]));
|
|
22
|
-
}
|
|
23
|
-
if (/fc\(/.test(value)) {
|
|
24
|
-
const args = value
|
|
25
|
-
.match(/fc\(([^)]*)\)/)?.[1]
|
|
26
|
-
.split(",")
|
|
27
|
-
.map((s) => s.trim());
|
|
28
|
-
if (args) value = fc(...(args as [string, string?, string?, string?]));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
decl.value = value;
|
|
23
|
+
// Process functions using the robust parser
|
|
24
|
+
decl.value = processFunctions(value, fnMap, decl, context);
|
|
25
|
+
|
|
32
26
|
return decl;
|
|
33
27
|
}
|
|
34
28
|
|
|
@@ -39,7 +33,7 @@ export function cloneNodes(nodes: ChildNode[], name: string): ChildNode[] {
|
|
|
39
33
|
rule.selector = rule.selector.replace(/\{name\}/g, name);
|
|
40
34
|
|
|
41
35
|
// Recursively clone child nodes and only keep valid rules/decls
|
|
42
|
-
rule.nodes = cloneNodes(rule.nodes || [], name).filter(
|
|
36
|
+
rule.nodes = cloneNodes(rule.nodes || [], name, context).filter(
|
|
43
37
|
(n) => n.type === "rule" || n.type === "decl"
|
|
44
38
|
);
|
|
45
39
|
|
|
@@ -55,13 +49,20 @@ export function cloneNodes(nodes: ChildNode[], name: string): ChildNode[] {
|
|
|
55
49
|
/**
|
|
56
50
|
* Generate CSS rules from a list of names
|
|
57
51
|
*/
|
|
58
|
-
export function generateRules(
|
|
52
|
+
export function generateRules(
|
|
53
|
+
names: string[],
|
|
54
|
+
atRule: AtRule,
|
|
55
|
+
context: PluginContext
|
|
56
|
+
): Rule[] {
|
|
59
57
|
const nodes = atRule.nodes ?? [];
|
|
60
58
|
const generatedRules: Rule[] = [];
|
|
61
59
|
|
|
62
60
|
for (const name of names) {
|
|
63
|
-
const rule = new Rule({
|
|
64
|
-
|
|
61
|
+
const rule = new Rule({
|
|
62
|
+
selector: `&.${name}`,
|
|
63
|
+
source: atRule.source,
|
|
64
|
+
});
|
|
65
|
+
cloneNodes(nodes, name, context).forEach((n) => {
|
|
65
66
|
if (n.type === "rule" && n.selector && n.nodes?.length) rule.append(n);
|
|
66
67
|
if (n.type === "decl") rule.append(n);
|
|
67
68
|
});
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,9 @@ import { dynamicFunctionsPlugin } from './plugin';
|
|
|
2
2
|
import { functions } from './functions';
|
|
3
3
|
|
|
4
4
|
// CommonJS-friendly export
|
|
5
|
-
|
|
5
|
+
const plugin = Object.assign(dynamicFunctionsPlugin, {
|
|
6
6
|
postcss: true,
|
|
7
7
|
functions
|
|
8
8
|
});
|
|
9
|
+
|
|
10
|
+
export default plugin;
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser';
|
|
2
|
+
import { Node } from 'postcss';
|
|
3
|
+
import { PluginContext } from './config';
|
|
4
|
+
import { reportError } from './errors';
|
|
5
|
+
|
|
6
|
+
export type FunctionMap = Record<string, (context: PluginContext, ...args: string[]) => string>;
|
|
7
|
+
|
|
8
|
+
export function processFunctions(
|
|
9
|
+
value: string,
|
|
10
|
+
fnMap: FunctionMap,
|
|
11
|
+
node: Node,
|
|
12
|
+
context: PluginContext
|
|
13
|
+
): string {
|
|
14
|
+
const parsed = valueParser(value);
|
|
15
|
+
|
|
16
|
+
// Helper to process nodes recursively (inner-first)
|
|
17
|
+
function processNode(parsedValue: valueParser.ParsedValue) {
|
|
18
|
+
parsedValue.walk((nodeIter) => {
|
|
19
|
+
// If nested nodes exist (e.g., in a function), process them first
|
|
20
|
+
if (nodeIter.type === 'function' && nodeIter.nodes.length > 0) {
|
|
21
|
+
// Here we recursively process child nodes before handling the current function
|
|
22
|
+
// However, value-parser's walk is already a visitor. To do inner-first,
|
|
23
|
+
// we can either walk in reverse or specifically handle children.
|
|
24
|
+
// A simpler way: since walk visits all, we can check if it's a function
|
|
25
|
+
// and if it's one we handle, we process its arguments.
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Actually, a manual walk is safer for inner-first replacement
|
|
30
|
+
function traverse(nodes: valueParser.Node[]) {
|
|
31
|
+
for (const nodeIter of nodes) {
|
|
32
|
+
if (nodeIter.type === 'function') {
|
|
33
|
+
// Process inner functions first
|
|
34
|
+
traverse(nodeIter.nodes);
|
|
35
|
+
|
|
36
|
+
// Now handle this function if it's in our map
|
|
37
|
+
if (fnMap[nodeIter.value]) {
|
|
38
|
+
const handler = fnMap[nodeIter.value];
|
|
39
|
+
const args: string[] = [];
|
|
40
|
+
let currentArg = '';
|
|
41
|
+
|
|
42
|
+
nodeIter.nodes.forEach((argNode) => {
|
|
43
|
+
if (argNode.type === 'div' && argNode.value === ',') {
|
|
44
|
+
args.push(currentArg.trim());
|
|
45
|
+
currentArg = '';
|
|
46
|
+
} else {
|
|
47
|
+
currentArg += valueParser.stringify(argNode);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (currentArg) {
|
|
52
|
+
args.push(currentArg.trim());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const cleanedArgs = args.map(arg => arg.replace(/^['"]|['"]$/g, ''));
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const result = handler(context, ...cleanedArgs);
|
|
59
|
+
// Replace function with its result
|
|
60
|
+
(nodeIter as any).type = 'word';
|
|
61
|
+
(nodeIter as any).value = result;
|
|
62
|
+
(nodeIter as any).nodes = []; // Clear children
|
|
63
|
+
} catch (error) {
|
|
64
|
+
reportError(
|
|
65
|
+
`Failed to process function "${nodeIter.value}": ${error instanceof Error ? error.message : String(error)}`,
|
|
66
|
+
node,
|
|
67
|
+
context
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
traverse(parsedValue.nodes);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
processNode(parsed);
|
|
79
|
+
return parsed.toString();
|
|
80
|
+
}
|