feat: add gradient control component

This commit is contained in:
2026-09-05 17:19:03 +05:00
parent b99ccf5d78
commit cad9bf6cfd
7 changed files with 393 additions and 21 deletions
+57
View File
@@ -154,6 +154,24 @@ export interface PlateSpec {
opacity: number;
}
/**
* Цветовой градиент: пара цветов + угол направления. 0° — слева направо,
* 90° — сверху вниз (прирост по часовой в пиксельных осях, ось Y вниз).
*/
export interface Gradient {
from: string;
to: string;
angle: number;
}
export interface GradientSpec {
kind: "gradient";
from: string;
to: string;
/** Направление градиента: 0..360°, угол в градусах. */
angle: number;
}
/**
* «Объект as const» kind → спека поля. Единственный источник правды для
* перечня kinds: `FieldSpecKind` = ключи map, `FieldSpec` = значение по любому
@@ -173,6 +191,7 @@ export const fieldSpecs = {
position9: {} as Position9Spec,
"font-style": {} as FontStyleSpec,
plate: {} as PlateSpec,
gradient: {} as GradientSpec,
} as const;
export type FieldSpecKind = keyof typeof fieldSpecs;
@@ -257,6 +276,13 @@ export const field = {
}): Field<Plate> => ({
spec: { kind: "plate", ...s },
}),
gradient: (s: {
from: string;
to: string;
angle: number;
}): Field<Gradient> => ({
spec: { kind: "gradient", ...s },
}),
};
/**
@@ -302,6 +328,12 @@ export function defaultSchemaParams<P>(
color: spec.color,
opacity: spec.opacity,
};
} else if (spec.kind === "gradient") {
out[key] = {
from: spec.from,
to: spec.to,
angle: spec.angle,
};
} else {
out[key] = spec.default;
}
@@ -456,6 +488,31 @@ export function sanitizeSchemaParams<P>(
};
break;
}
case "gradient": {
const r =
typeof raw === "object" &&
raw !== null &&
"from" in raw &&
"to" in raw &&
"angle" in raw
? (raw as Record<string, unknown>)
: undefined;
out[key] = {
from:
typeof r?.from === "string" && /^#[0-9a-f]{6}$/i.test(r.from)
? r.from
: spec.from,
to:
typeof r?.to === "string" && /^#[0-9a-f]{6}$/i.test(r.to)
? r.to
: spec.to,
angle:
typeof r?.angle === "number" && Number.isFinite(r.angle)
? clamp(r.angle, 0, 360)
: spec.angle,
};
break;
}
}
}
return out;