@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,2 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
1
|
+
import { PluginContext } from "../config";
|
|
2
|
+
export declare function sc(context: PluginContext, name: string, alpha?: string, lightness?: string, chroma?: string): string;
|
|
3
|
+
export declare function fc(context: PluginContext, name: string, alpha?: string, lightness?: string, chroma?: string): string;
|
|
4
|
+
export declare function alpha(context: PluginContext, color: string, value: string): string;
|
|
5
|
+
export declare function lighten(context: PluginContext, color: string, amount: string): string;
|
|
6
|
+
export declare function darken(context: PluginContext, color: string, amount: string): string;
|
|
7
|
+
export declare function contrast(context: PluginContext, color: string): string;
|
package/dist/functions/color.js
CHANGED
|
@@ -1,34 +1,110 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Utility to parse an oklch string and return its parts
|
|
3
|
+
*/
|
|
4
|
+
function parseOklch(color) {
|
|
5
|
+
const match = color.match(/oklch\(([^ ]+) ([^ ]+) ([^ /]+)(?: \/ ([^)]+))?\)/);
|
|
6
|
+
if (!match)
|
|
7
|
+
return null;
|
|
8
|
+
return {
|
|
9
|
+
l: match[1],
|
|
10
|
+
c: match[2],
|
|
11
|
+
h: match[3],
|
|
12
|
+
a: match[4] || "1",
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function sc(context, name, alpha, lightness, chroma) {
|
|
16
|
+
let a = alpha && alpha !== "null" ? alpha : "1";
|
|
17
|
+
let l = lightness && lightness !== "null" ? lightness : "var(--lightness)";
|
|
18
|
+
let c = chroma && chroma !== "null" ? chroma : "var(--chroma)";
|
|
19
|
+
let h = `var(--${name}-hue)`;
|
|
20
|
+
return `oklch(${l} ${c} ${h} / ${a})`;
|
|
21
|
+
}
|
|
22
|
+
export function fc(context, name, alpha, lightness, chroma) {
|
|
23
|
+
let a = alpha && alpha !== "null" ? alpha : "1";
|
|
24
|
+
let l = lightness && lightness !== "null" ? lightness : `var(--${name}-lightness)`;
|
|
25
|
+
let c = chroma && chroma !== "null" ? chroma : `var(--${name}-chroma)`;
|
|
26
|
+
let h = `var(--${name}-hue)`;
|
|
27
|
+
return `oklch(${l} ${c} ${h} / ${a})`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Resolves a color name to its CSS variables based on its type (standard or fixed)
|
|
31
|
+
*/
|
|
32
|
+
function getColorVariables(context, color) {
|
|
33
|
+
const { config } = context;
|
|
34
|
+
const hues = config?.ui?.theme?.hues || {};
|
|
35
|
+
const colors = config?.ui?.theme?.colors || {};
|
|
36
|
+
const lightColors = config?.ui?.theme?.light?.colors || {};
|
|
37
|
+
const darkColors = config?.ui?.theme?.dark?.colors || {};
|
|
38
|
+
// Check if it's a standard color (sc)
|
|
39
|
+
if (color in hues) {
|
|
40
|
+
return {
|
|
41
|
+
l: "var(--lightness)",
|
|
42
|
+
c: "var(--chroma)",
|
|
43
|
+
h: `var(--${color}-hue)`,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// Check if it's a fixed color (fc)
|
|
47
|
+
if (color in colors || color in lightColors || color in darkColors) {
|
|
48
|
+
return {
|
|
49
|
+
l: `var(--${color}-lightness)`,
|
|
50
|
+
c: `var(--${color}-chroma)`,
|
|
51
|
+
h: `var(--${color}-hue)`,
|
|
52
|
+
};
|
|
11
53
|
}
|
|
12
|
-
if
|
|
13
|
-
|
|
54
|
+
// Fallback to assume it's a fixed color if unknown but used as a name
|
|
55
|
+
return {
|
|
56
|
+
l: `var(--${color}-lightness)`,
|
|
57
|
+
c: `var(--${color}-chroma)`,
|
|
58
|
+
h: `var(--${color}-hue)`,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function alpha(context, color, value) {
|
|
62
|
+
const parsed = parseOklch(color);
|
|
63
|
+
if (parsed) {
|
|
64
|
+
return `oklch(${parsed.l} ${parsed.c} ${parsed.h} / ${value})`;
|
|
14
65
|
}
|
|
15
|
-
|
|
16
|
-
|
|
66
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
67
|
+
return `oklch(${l} ${c} ${h} / ${value})`;
|
|
68
|
+
}
|
|
69
|
+
export function lighten(context, color, amount) {
|
|
70
|
+
const parsed = parseOklch(color);
|
|
71
|
+
if (parsed) {
|
|
72
|
+
if (parsed.l.startsWith('var(')) {
|
|
73
|
+
return `oklch(calc(${parsed.l} + ${amount}) ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
74
|
+
}
|
|
75
|
+
const lValue = parseFloat(parsed.l);
|
|
76
|
+
const amtValue = parseFloat(amount);
|
|
77
|
+
return `oklch(${Math.min(1, lValue + amtValue)} ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
17
78
|
}
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
79
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
80
|
+
return `oklch(calc(${l} + ${amount}) ${c} ${h} / 1)`;
|
|
81
|
+
}
|
|
82
|
+
export function darken(context, color, amount) {
|
|
83
|
+
const parsed = parseOklch(color);
|
|
84
|
+
if (parsed) {
|
|
85
|
+
if (parsed.l.startsWith('var(')) {
|
|
86
|
+
return `oklch(calc(${parsed.l} - ${amount}) ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
87
|
+
}
|
|
88
|
+
const lValue = parseFloat(parsed.l);
|
|
89
|
+
const amtValue = parseFloat(amount);
|
|
90
|
+
return `oklch(${Math.max(0, lValue - amtValue)} ${parsed.c} ${parsed.h} / ${parsed.a})`;
|
|
26
91
|
}
|
|
27
|
-
|
|
28
|
-
|
|
92
|
+
const { l, c, h } = getColorVariables(context, color);
|
|
93
|
+
return `oklch(calc(${l} - ${amount}) ${c} ${h} / 1)`;
|
|
94
|
+
}
|
|
95
|
+
export function contrast(context, color) {
|
|
96
|
+
const parsed = parseOklch(color);
|
|
97
|
+
let l;
|
|
98
|
+
if (parsed) {
|
|
99
|
+
l = parsed.l;
|
|
29
100
|
}
|
|
30
|
-
|
|
31
|
-
|
|
101
|
+
else {
|
|
102
|
+
const vars = getColorVariables(context, color);
|
|
103
|
+
l = vars.l;
|
|
32
104
|
}
|
|
33
|
-
|
|
105
|
+
// Dynamic CSS contrast logic:
|
|
106
|
+
// (L - 0.6) * -1000 will be:
|
|
107
|
+
// - very negative if L > 0.6 (clamped to 0 / black)
|
|
108
|
+
// - very positive if L < 0.6 (clamped to 1 / white)
|
|
109
|
+
return `oklch(calc((${l} - 0.6) * -1000) 0 0)`;
|
|
34
110
|
}
|
package/dist/functions/index.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
fc: color_1.fc,
|
|
12
|
-
spacing: spacing_1.default,
|
|
1
|
+
import { sc, fc, alpha, lighten, darken, contrast } from "./color";
|
|
2
|
+
import { theme } from "./theme";
|
|
3
|
+
export const functions = {
|
|
4
|
+
sc,
|
|
5
|
+
fc,
|
|
6
|
+
alpha,
|
|
7
|
+
lighten,
|
|
8
|
+
darken,
|
|
9
|
+
contrast,
|
|
10
|
+
theme,
|
|
13
11
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accesses values from the Seyuna configuration using dot notation
|
|
3
|
+
* Example: theme(ui.theme.breakpoints.tablet)
|
|
4
|
+
*/
|
|
5
|
+
export function theme(context, path) {
|
|
6
|
+
const parts = path.split('.');
|
|
7
|
+
let current = context.config;
|
|
8
|
+
for (const part of parts) {
|
|
9
|
+
if (current && typeof current === 'object' && part in current) {
|
|
10
|
+
current = current[part];
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return path; // Return original path if not found
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return String(current);
|
|
17
|
+
}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Rule, ChildNode, AtRule } from "postcss";
|
|
2
|
+
import { PluginContext } from "./config";
|
|
2
3
|
/**
|
|
3
4
|
* Helper: clone nodes and replace {name} placeholders safely
|
|
4
5
|
* Returns only valid Rules or Declarations (never raw AtRules)
|
|
5
6
|
*/
|
|
6
|
-
export declare function cloneNodes(nodes: ChildNode[], name: string): ChildNode[];
|
|
7
|
+
export declare function cloneNodes(nodes: ChildNode[], name: string, context: PluginContext): ChildNode[];
|
|
7
8
|
/**
|
|
8
9
|
* Generate CSS rules from a list of names
|
|
9
10
|
*/
|
|
10
|
-
export declare function generateRules(names: string[], atRule: AtRule): Rule[];
|
|
11
|
+
export declare function generateRules(names: string[], atRule: AtRule, context: PluginContext): Rule[];
|
package/dist/helpers.js
CHANGED
|
@@ -1,36 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.cloneNodes = cloneNodes;
|
|
4
|
-
exports.generateRules = generateRules;
|
|
5
|
-
const postcss_1 = require("postcss");
|
|
6
|
-
const color_1 = require("./functions/color");
|
|
1
|
+
import { Rule } from "postcss";
|
|
2
|
+
import { processFunctions } from "./parser";
|
|
7
3
|
/**
|
|
8
4
|
* Helper: clone nodes and replace {name} placeholders safely
|
|
9
5
|
* Returns only valid Rules or Declarations (never raw AtRules)
|
|
10
6
|
*/
|
|
11
|
-
function cloneNodes(nodes, name) {
|
|
7
|
+
export function cloneNodes(nodes, name, context) {
|
|
8
|
+
const { functions: fnMap } = context;
|
|
12
9
|
return nodes.flatMap((node) => {
|
|
13
10
|
const cloned = node.clone();
|
|
14
11
|
if (cloned.type === "decl") {
|
|
15
12
|
const decl = cloned;
|
|
16
13
|
let value = decl.value.replace(/\{name\}/g, name);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
.match(/sc\(([^)]*)\)/)?.[1]
|
|
20
|
-
.split(",")
|
|
21
|
-
.map((s) => s.trim());
|
|
22
|
-
if (args)
|
|
23
|
-
value = (0, color_1.sc)(...args);
|
|
24
|
-
}
|
|
25
|
-
if (/fc\(/.test(value)) {
|
|
26
|
-
const args = value
|
|
27
|
-
.match(/fc\(([^)]*)\)/)?.[1]
|
|
28
|
-
.split(",")
|
|
29
|
-
.map((s) => s.trim());
|
|
30
|
-
if (args)
|
|
31
|
-
value = (0, color_1.fc)(...args);
|
|
32
|
-
}
|
|
33
|
-
decl.value = value;
|
|
14
|
+
// Process functions using the robust parser
|
|
15
|
+
decl.value = processFunctions(value, fnMap, decl, context);
|
|
34
16
|
return decl;
|
|
35
17
|
}
|
|
36
18
|
if (cloned.type === "rule") {
|
|
@@ -39,7 +21,7 @@ function cloneNodes(nodes, name) {
|
|
|
39
21
|
return [];
|
|
40
22
|
rule.selector = rule.selector.replace(/\{name\}/g, name);
|
|
41
23
|
// Recursively clone child nodes and only keep valid rules/decls
|
|
42
|
-
rule.nodes = cloneNodes(rule.nodes || [], name).filter((n) => n.type === "rule" || n.type === "decl");
|
|
24
|
+
rule.nodes = cloneNodes(rule.nodes || [], name, context).filter((n) => n.type === "rule" || n.type === "decl");
|
|
43
25
|
if (!rule.nodes.length)
|
|
44
26
|
return [];
|
|
45
27
|
return rule;
|
|
@@ -51,12 +33,15 @@ function cloneNodes(nodes, name) {
|
|
|
51
33
|
/**
|
|
52
34
|
* Generate CSS rules from a list of names
|
|
53
35
|
*/
|
|
54
|
-
function generateRules(names, atRule) {
|
|
36
|
+
export function generateRules(names, atRule, context) {
|
|
55
37
|
const nodes = atRule.nodes ?? [];
|
|
56
38
|
const generatedRules = [];
|
|
57
39
|
for (const name of names) {
|
|
58
|
-
const rule = new
|
|
59
|
-
|
|
40
|
+
const rule = new Rule({
|
|
41
|
+
selector: `&.${name}`,
|
|
42
|
+
source: atRule.source,
|
|
43
|
+
});
|
|
44
|
+
cloneNodes(nodes, name, context).forEach((n) => {
|
|
60
45
|
if (n.type === "rule" && n.selector && n.nodes?.length)
|
|
61
46
|
rule.append(n);
|
|
62
47
|
if (n.type === "decl")
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
declare const
|
|
1
|
+
declare const plugin: import("postcss").PluginCreator<import("./config").PluginOptions> & {
|
|
2
2
|
postcss: boolean;
|
|
3
3
|
functions: Record<string, import("./functions").FnHandler>;
|
|
4
4
|
};
|
|
5
|
-
export
|
|
5
|
+
export default plugin;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { dynamicFunctionsPlugin } from './plugin';
|
|
2
|
+
import { functions } from './functions';
|
|
3
|
+
// CommonJS-friendly export
|
|
4
|
+
const plugin = Object.assign(dynamicFunctionsPlugin, {
|
|
5
5
|
postcss: true,
|
|
6
|
-
functions
|
|
6
|
+
functions
|
|
7
7
|
});
|
|
8
|
+
export default plugin;
|
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Node } from 'postcss';
|
|
2
|
+
import { PluginContext } from './config';
|
|
3
|
+
export type FunctionMap = Record<string, (context: PluginContext, ...args: string[]) => string>;
|
|
4
|
+
export declare function processFunctions(value: string, fnMap: FunctionMap, node: Node, context: PluginContext): string;
|
package/dist/parser.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser';
|
|
2
|
+
import { reportError } from './errors';
|
|
3
|
+
export function processFunctions(value, fnMap, node, context) {
|
|
4
|
+
const parsed = valueParser(value);
|
|
5
|
+
// Helper to process nodes recursively (inner-first)
|
|
6
|
+
function processNode(parsedValue) {
|
|
7
|
+
parsedValue.walk((nodeIter) => {
|
|
8
|
+
// If nested nodes exist (e.g., in a function), process them first
|
|
9
|
+
if (nodeIter.type === 'function' && nodeIter.nodes.length > 0) {
|
|
10
|
+
// Here we recursively process child nodes before handling the current function
|
|
11
|
+
// However, value-parser's walk is already a visitor. To do inner-first,
|
|
12
|
+
// we can either walk in reverse or specifically handle children.
|
|
13
|
+
// A simpler way: since walk visits all, we can check if it's a function
|
|
14
|
+
// and if it's one we handle, we process its arguments.
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
// Actually, a manual walk is safer for inner-first replacement
|
|
18
|
+
function traverse(nodes) {
|
|
19
|
+
for (const nodeIter of nodes) {
|
|
20
|
+
if (nodeIter.type === 'function') {
|
|
21
|
+
// Process inner functions first
|
|
22
|
+
traverse(nodeIter.nodes);
|
|
23
|
+
// Now handle this function if it's in our map
|
|
24
|
+
if (fnMap[nodeIter.value]) {
|
|
25
|
+
const handler = fnMap[nodeIter.value];
|
|
26
|
+
const args = [];
|
|
27
|
+
let currentArg = '';
|
|
28
|
+
nodeIter.nodes.forEach((argNode) => {
|
|
29
|
+
if (argNode.type === 'div' && argNode.value === ',') {
|
|
30
|
+
args.push(currentArg.trim());
|
|
31
|
+
currentArg = '';
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
currentArg += valueParser.stringify(argNode);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
if (currentArg) {
|
|
38
|
+
args.push(currentArg.trim());
|
|
39
|
+
}
|
|
40
|
+
const cleanedArgs = args.map(arg => arg.replace(/^['"]|['"]$/g, ''));
|
|
41
|
+
try {
|
|
42
|
+
const result = handler(context, ...cleanedArgs);
|
|
43
|
+
// Replace function with its result
|
|
44
|
+
nodeIter.type = 'word';
|
|
45
|
+
nodeIter.value = result;
|
|
46
|
+
nodeIter.nodes = []; // Clear children
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
reportError(`Failed to process function "${nodeIter.value}": ${error instanceof Error ? error.message : String(error)}`, node, context);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
traverse(parsedValue.nodes);
|
|
56
|
+
}
|
|
57
|
+
processNode(parsed);
|
|
58
|
+
return parsed.toString();
|
|
59
|
+
}
|
package/dist/plugin.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
import { PluginCreator } from "postcss";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
export declare const dynamicFunctionsPlugin: PluginCreator<PluginOptions>;
|
|
2
|
+
import { PluginOptions as ConfigOptions } from "./config";
|
|
3
|
+
export declare const dynamicFunctionsPlugin: PluginCreator<ConfigOptions>;
|
package/dist/plugin.js
CHANGED
|
@@ -1,35 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const fnMap = { ...
|
|
1
|
+
import { functions } from "./functions";
|
|
2
|
+
import { atRuleHandlers } from "./at-rules";
|
|
3
|
+
import { loadConfig } from "./config";
|
|
4
|
+
import { processFunctions } from "./parser";
|
|
5
|
+
export const dynamicFunctionsPlugin = (opts = {}) => {
|
|
6
|
+
const { config, options } = loadConfig(opts);
|
|
7
|
+
const fnMap = { ...functions, ...opts.functions };
|
|
8
|
+
const context = {
|
|
9
|
+
config,
|
|
10
|
+
options,
|
|
11
|
+
functions: fnMap,
|
|
12
|
+
mixins: {},
|
|
13
|
+
};
|
|
8
14
|
return {
|
|
9
15
|
postcssPlugin: "postcss-dynamic-functions",
|
|
10
16
|
Declaration(decl) {
|
|
11
|
-
|
|
12
|
-
for (const fnName in fnMap) {
|
|
13
|
-
const regex = new RegExp(`${fnName}\\(([^)]*)\\)`, "g");
|
|
14
|
-
value = value.replace(regex, (_match, argsStr) => {
|
|
15
|
-
const args = argsStr
|
|
16
|
-
.split(",")
|
|
17
|
-
.map((a) => a.trim().replace(/^['"]|['"]$/g, ""));
|
|
18
|
-
return fnMap[fnName](...args);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
decl.value = value;
|
|
17
|
+
decl.value = processFunctions(decl.value, fnMap, decl, context);
|
|
22
18
|
},
|
|
23
19
|
// Override AtRule handler to ensure ordered execution
|
|
24
20
|
AtRule(atRule) {
|
|
25
21
|
// Iterate over handlers in order (array) instead of object spread
|
|
26
|
-
for (const { name, handler } of
|
|
22
|
+
for (const { name, handler } of atRuleHandlers) {
|
|
27
23
|
if (atRule.name === name) {
|
|
28
|
-
handler(atRule);
|
|
24
|
+
handler(atRule, context);
|
|
29
25
|
}
|
|
30
26
|
}
|
|
31
27
|
},
|
|
32
28
|
};
|
|
33
29
|
};
|
|
34
|
-
|
|
35
|
-
exports.dynamicFunctionsPlugin.postcss = true;
|
|
30
|
+
dynamicFunctionsPlugin.postcss = true;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,19 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
ui: {
|
|
3
|
-
theme: {
|
|
4
|
-
hues: Record<string, number>;
|
|
5
|
-
light: {
|
|
6
|
-
colors: Record<string, Color>;
|
|
7
|
-
};
|
|
8
|
-
dark: {
|
|
9
|
-
colors: Record<string, Color>;
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
type Color = {
|
|
15
|
-
lightness: number;
|
|
16
|
-
chroma: number;
|
|
17
|
-
hue: number;
|
|
18
|
-
};
|
|
19
|
-
export {};
|
|
1
|
+
export type { Config as SeyunaConfig, Color } from "@seyuna/cli/types";
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seyuna/postcss",
|
|
3
|
-
"version": "1.0.0-canary.
|
|
3
|
+
"version": "1.0.0-canary.15",
|
|
4
4
|
"description": "Seyuna UI's postcss plugin",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
|
-
"dev": "tsc -w"
|
|
9
|
+
"dev": "tsc -w",
|
|
10
|
+
"test": "vitest",
|
|
11
|
+
"test:run": "vitest run"
|
|
10
12
|
},
|
|
11
13
|
"keywords": [
|
|
12
14
|
"postcss",
|
|
@@ -18,6 +20,10 @@
|
|
|
18
20
|
"type": "git",
|
|
19
21
|
"url": "git+https://github.com/seyuna-corp/seyuna-postcss.git"
|
|
20
22
|
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@seyuna/cli": "^1.0.0-canary.28",
|
|
25
|
+
"postcss-value-parser": "^4.2.0"
|
|
26
|
+
},
|
|
21
27
|
"peerDependencies": {
|
|
22
28
|
"postcss": "^8.5.6"
|
|
23
29
|
},
|
|
@@ -27,12 +33,13 @@
|
|
|
27
33
|
"@semantic-release/exec": "^7.1.0",
|
|
28
34
|
"@semantic-release/git": "^10.0.1",
|
|
29
35
|
"@semantic-release/github": "^11.0.3",
|
|
30
|
-
"@semantic-release/npm": "^
|
|
36
|
+
"@semantic-release/npm": "^13.1.3",
|
|
31
37
|
"@semantic-release/release-notes-generator": "^14.0.3",
|
|
32
38
|
"@types/node": "^20.0.0",
|
|
33
39
|
"postcss": "^8.5.6",
|
|
34
40
|
"postcss-selector-parser": "^7.1.0",
|
|
35
|
-
"semantic-release": "^
|
|
36
|
-
"typescript": "^5.0.0"
|
|
41
|
+
"semantic-release": "^25.0.2",
|
|
42
|
+
"typescript": "^5.0.0",
|
|
43
|
+
"vitest": "^4.0.16"
|
|
37
44
|
}
|
|
38
45
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Rule, AtRule, ChildNode } from "postcss";
|
|
2
|
+
import { PluginContext } from "../config";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Custom PostCSS plugin handler factory for `@light` and `@dark` at-rules.
|
|
6
|
+
*/
|
|
7
|
+
function createColorSchemeHandler(scheme: 'light' | 'dark') {
|
|
8
|
+
return (atRule: AtRule, context: PluginContext) => {
|
|
9
|
+
const { options } = context;
|
|
10
|
+
const modeAttribute = options.modeAttribute;
|
|
11
|
+
const clonedNodes: ChildNode[] = [];
|
|
12
|
+
|
|
13
|
+
// Clone all child nodes inside the block
|
|
14
|
+
atRule.each((node: ChildNode) => {
|
|
15
|
+
clonedNodes.push(node.clone());
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Rule 1: [data-mode="scheme"] & { ... } (Explicit mode)
|
|
20
|
+
*/
|
|
21
|
+
const explicitRule = new Rule({
|
|
22
|
+
selector: `[${modeAttribute}="${scheme}"] &`,
|
|
23
|
+
source: atRule.source,
|
|
24
|
+
});
|
|
25
|
+
clonedNodes.forEach((node) => explicitRule.append(node.clone()));
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Rule 2: @media (prefers-color-scheme: scheme) { [data-mode="system"] & { ... } } (System preference)
|
|
29
|
+
*/
|
|
30
|
+
const mediaAtRule = new AtRule({
|
|
31
|
+
name: "media",
|
|
32
|
+
params: `(prefers-color-scheme: ${scheme})`,
|
|
33
|
+
source: atRule.source,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const systemRule = new Rule({
|
|
37
|
+
selector: `[${modeAttribute}="system"] &`,
|
|
38
|
+
source: atRule.source,
|
|
39
|
+
});
|
|
40
|
+
clonedNodes.forEach((node) => systemRule.append(node.clone()));
|
|
41
|
+
|
|
42
|
+
mediaAtRule.append(systemRule);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Replace the original at-rule with the generated rules.
|
|
46
|
+
*/
|
|
47
|
+
atRule.replaceWith(mediaAtRule, explicitRule);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const light = createColorSchemeHandler('light');
|
|
52
|
+
export const dark = createColorSchemeHandler('dark');
|
package/src/at-rules/color.ts
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import { AtRule
|
|
2
|
-
import
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { SeyunaConfig } from "../types";
|
|
1
|
+
import { AtRule } from "postcss";
|
|
2
|
+
import { PluginContext } from "../config";
|
|
5
3
|
import { generateRules } from "../helpers";
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
* Handler for @each-standard-color
|
|
9
7
|
*/
|
|
10
|
-
export function eachStandardColor(atRule: AtRule) {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const hueNames = Object.keys(data.ui.theme.hues);
|
|
8
|
+
export function eachStandardColor(atRule: AtRule, context: PluginContext) {
|
|
9
|
+
const { config } = context;
|
|
10
|
+
const hueNames = config.ui ? Object.keys(config.ui.theme.hues) : [];
|
|
14
11
|
|
|
15
|
-
const rules = generateRules(hueNames, atRule);
|
|
12
|
+
const rules = generateRules(hueNames, atRule, context);
|
|
16
13
|
if (rules.length) atRule.replaceWith(...rules);
|
|
17
14
|
else atRule.remove();
|
|
18
15
|
}
|
|
@@ -20,18 +17,17 @@ export function eachStandardColor(atRule: AtRule) {
|
|
|
20
17
|
/**
|
|
21
18
|
* Handler for @each-fixed-color
|
|
22
19
|
*/
|
|
23
|
-
export function eachFixedColor(atRule: AtRule) {
|
|
24
|
-
const
|
|
25
|
-
const data: SeyunaConfig = JSON.parse(fs.readFileSync(jsonPath, "utf-8"));
|
|
20
|
+
export function eachFixedColor(atRule: AtRule, context: PluginContext) {
|
|
21
|
+
const { config } = context;
|
|
26
22
|
|
|
27
23
|
const mergedNames = [
|
|
28
24
|
...new Set([
|
|
29
|
-
...Object.keys(
|
|
30
|
-
...Object.keys(
|
|
25
|
+
...(config.ui ? Object.keys(config.ui.theme.light.colors) : []),
|
|
26
|
+
...(config.ui ? Object.keys(config.ui.theme.dark.colors) : []),
|
|
31
27
|
]),
|
|
32
28
|
];
|
|
33
29
|
|
|
34
|
-
const rules = generateRules(mergedNames, atRule);
|
|
30
|
+
const rules = generateRules(mergedNames, atRule, context);
|
|
35
31
|
if (rules.length) atRule.replaceWith(...rules);
|
|
36
32
|
else atRule.remove();
|
|
37
33
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AtRule } from "postcss";
|
|
2
|
+
import { PluginContext } from "../config";
|
|
3
|
+
import { processFunctions } from "../parser";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handler for @if (condition) { ... }
|
|
7
|
+
*/
|
|
8
|
+
export function conditional(atRule: AtRule, context: PluginContext) {
|
|
9
|
+
const { functions: fnMap } = context;
|
|
10
|
+
|
|
11
|
+
// Clean up params (remove parentheses if present)
|
|
12
|
+
let condition = atRule.params.trim().replace(/^\(|\)$/g, '').trim();
|
|
13
|
+
|
|
14
|
+
// Process functions in the condition (e.g., theme access)
|
|
15
|
+
const evaluated = processFunctions(condition, fnMap, atRule, context).trim();
|
|
16
|
+
|
|
17
|
+
// Simple truthy evaluation
|
|
18
|
+
const isFalsy = !evaluated ||
|
|
19
|
+
evaluated === 'false' ||
|
|
20
|
+
evaluated === '0' ||
|
|
21
|
+
evaluated === 'null' ||
|
|
22
|
+
evaluated === 'undefined';
|
|
23
|
+
|
|
24
|
+
if (!isFalsy) {
|
|
25
|
+
// Ungroup the nodes
|
|
26
|
+
if (atRule.nodes && atRule.nodes.length > 0) {
|
|
27
|
+
atRule.replaceWith(...atRule.nodes.map(n => n.clone()));
|
|
28
|
+
} else {
|
|
29
|
+
atRule.remove();
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
atRule.remove();
|
|
33
|
+
}
|
|
34
|
+
}
|