AI-native design editor. Open-source Figma alternative.
fix(design-jsx): say what gradient helpers expect when stops are not an array (#881)
Danila Poyarkov6a05e30f1539
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cde128a78..3494ee9f6 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -69,6 +69,7 @@### Fixed+- Report what `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient` expect when design JSX passes them something other than an array of stops, such as `linearGradient('#3b82f6')`, instead of failing with `stops.map is not a function`, so an AI agent can correct the call.- Keep the canvas context menu open when you right-click again right after closing it, which could close it again at once, especially with reduced motion.- Show and edit the component properties of an instance nested inside another instance; they were missing because the nested instance was read as its own component.- Read and set `componentPropertyReferences` in the plugin API with property keys such as `Label#prop:1a2b`, as `componentPropertyDefinitions` lists them and Figma uses them, instead of internal property ids.diff --git a/packages/design-jsx/src/paints.ts b/packages/design-jsx/src/paints.tsindex 3f1e87351..10830f025 100644--- a/packages/design-jsx/src/paints.ts+++ b/packages/design-jsx/src/paints.ts@@ -1,3 +1,5 @@+import * as v from 'valibot'+import type {BlendMode,Fill,@@ -31,6 +33,41 @@ const DEFAULT_GRADIENT_TRANSFORM: GradientTransform = {m12: 0}+const colorSchema = v.union([+ v.string(),+ v.object({ r: v.number(), g: v.number(), b: v.number(), a: v.number() })+])+const stopsSchema = v.array(+ v.union(+ [v.tuple([colorSchema, v.number()]), v.object({ color: colorSchema, position: v.number() })],+ (issue) => `Expected [color, position] or { color, position } but received ${issue.received}`+ )+)++const GRADIENT_HELPERS = {+ GRADIENT_LINEAR: 'linearGradient',+ GRADIENT_RADIAL: 'radialGradient',+ GRADIENT_ANGULAR: 'angularGradient',+ GRADIENT_DIAMOND: 'diamondGradient'+} as const++type GradientType = keyof typeof GRADIENT_HELPERS++/**+ * Scripts call the helpers with whatever they guess, so a wrong call says what it expects+ * and what was wrong, as Valibot describes it.+ */+function parseStops(type: GradientType, stops: unknown): PaintStop[] {+ const parsed = v.safeParse(stopsSchema, stops)+ if (parsed.success) return parsed.output+ const [issue] = parsed.issues+ // Only the array's items are checked one level down, so a path is a stop's index.+ const stop = v.getDotPath(issue)+ throw new Error(+ `${GRADIENT_HELPERS[type]}() expects an array of stops, such as [['#3b82f6', 0], ['#8b5cf6', 1]]: ${issue.message}${stop === null ? '' : ` (stop ${stop})`}`+ )+}+function toColor(color: PaintColor): Color {return typeof color === 'string' ? parseColor(color) : color}@@ -53,10 +90,7 @@ export function solid(color: PaintColor, options: SolidPaintOptions = {}): Fill}export function gradient(- type: Extract<- FillType,- 'GRADIENT_LINEAR' | 'GRADIENT_RADIAL' | 'GRADIENT_ANGULAR' | 'GRADIENT_DIAMOND'- >,+ type: Extract<FillType, GradientType>,stops: PaintStop[],options: GradientPaintOptions = {}): Fill {@@ -66,7 +100,7 @@ export function gradient(opacity: options.opacity ?? 1,visible: options.visible ?? true,blendMode: options.blendMode,- gradientStops: stops.map(toStop),+ gradientStops: parseStops(type, stops).map(toStop),gradientTransform: options.transform ?? DEFAULT_GRADIENT_TRANSFORM}}diff --git a/packages/design-jsx/src/reference/authoring.md b/packages/design-jsx/src/reference/authoring.mdindex 24716a91d..92ae7f231 100644--- a/packages/design-jsx/src/reference/authoring.md+++ b/packages/design-jsx/src/reference/authoring.md@@ -17,7 +17,7 @@ This reference describes scene creation, not React DOM output. Use the `render`## Paint, text, and artwork-- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`.+- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`, each taking an array of stops and optional `{ opacity, transform }`: `fills={[linearGradient([['#3b82f6', 0], ['#8b5cf6', 1]])]}`.- `stroke` describes one stroke, refined by `strokeWidth`, `strokeAlign` (`"inside"`, `"center"`, `"outside"`), `strokeDash`, `strokeCap`, and `strokeJoin`. `strokes` takes several, as objects with `color`, `weight`, `align`, `dash`, `cap`, `join`, and `visible`; `strokeWeights={{ top, right, bottom, left }}` sets per-side widths. `dashPattern` sets the node's own dash pattern, separate from the per-stroke `strokeDash`.- `rounded` and `roundedTL`/`roundedTR`/`roundedBL`/`roundedBR` control corners. `strokeWidth`, `opacity`, `rotate`, and `blendMode` control appearance. `overflow="hidden"` clips content; do not hide accidental text overflow to make a broken layout appear correct.- `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow="offsetX offsetY blur #color"` and `blur` are convenient shorthands. Effect helpers take `radius`, as Figma's effects do; when a JSX string is rendered, an option a paint or effect helper does not support is reported as a warning.diff --git a/packages/design-jsx/tests/paints.test.ts b/packages/design-jsx/tests/paints.test.tsnew file mode 100644index 000000000..db6070205--- /dev/null+++ b/packages/design-jsx/tests/paints.test.ts@@ -0,0 +1,34 @@+import { describe, expect, test } from 'bun:test'++import { linearGradient, radialGradient } from '@open-pencil/design-jsx'++const expected =+ "linearGradient() expects an array of stops, such as [['#3b82f6', 0], ['#8b5cf6', 1]]"++describe('gradient helpers', () => {+ test('accept tuple and object stops', () => {+ const fill = linearGradient([['#000000', 0], { color: '#ffffff', position: 1 }])+ expect(fill.gradientStops?.map((stop) => stop.position)).toEqual([0, 1])+ })++ const circular: Record<string, unknown> = {}+ circular.self = circular++ test.each([+ ['#3b82f6', '"#3b82f6"'],+ [{ stops: [] }, 'Object'],+ [undefined, 'undefined'],+ [10n, '10'],+ [circular, 'Object']+ ])('say what they expect when the stops are not an array (%p)', (stops, received) => {+ expect(() => linearGradient(stops as never)).toThrow(+ `${expected}: Invalid type: Expected Array but received ${received}`+ )+ })++ test('name the stop that is wrong and what it was', () => {+ expect(() => radialGradient([['#000000', 0], ['#ffffff']] as never)).toThrow(+ "radialGradient() expects an array of stops, such as [['#3b82f6', 0], ['#8b5cf6', 1]]: Expected [color, position] or { color, position } but received Array (stop 1)"+ )+ })+})diff --git a/packages/docs/reference/design-authoring.md b/packages/docs/reference/design-authoring.mdindex e04aa7748..c69219b0e 100644--- a/packages/docs/reference/design-authoring.md+++ b/packages/docs/reference/design-authoring.md@@ -19,7 +19,7 @@ This reference describes scene creation, not React DOM output. Use the `render`## Paint, text, and artwork-- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`.+- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`, each taking an array of stops and optional `{ opacity, transform }`: `fills={[linearGradient([['#3b82f6', 0], ['#8b5cf6', 1]])]}`.- `stroke` describes one stroke, refined by `strokeWidth`, `strokeAlign` (`"inside"`, `"center"`, `"outside"`), `strokeDash`, `strokeCap`, and `strokeJoin`. `strokes` takes several, as objects with `color`, `weight`, `align`, `dash`, `cap`, `join`, and `visible`; `strokeWeights={{ top, right, bottom, left }}` sets per-side widths. `dashPattern` sets the node's own dash pattern, separate from the per-stroke `strokeDash`.- `rounded` and `roundedTL`/`roundedTR`/`roundedBL`/`roundedBR` control corners. `strokeWidth`, `opacity`, `rotate`, and `blendMode` control appearance. `overflow="hidden"` clips content; do not hide accidental text overflow to make a broken layout appear correct.- `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow="offsetX offsetY blur #color"` and `blur` are convenient shorthands. Effect helpers take `radius`, as Figma's effects do; when a JSX string is rendered, an option a paint or effect helper does not support is reported as a warning.diff --git a/skills/open-pencil/references/design-authoring.md b/skills/open-pencil/references/design-authoring.mdindex e04aa7748..c69219b0e 100644--- a/skills/open-pencil/references/design-authoring.md+++ b/skills/open-pencil/references/design-authoring.md@@ -19,7 +19,7 @@ This reference describes scene creation, not React DOM output. Use the `render`## Paint, text, and artwork-- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`.+- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`, each taking an array of stops and optional `{ opacity, transform }`: `fills={[linearGradient([['#3b82f6', 0], ['#8b5cf6', 1]])]}`.- `stroke` describes one stroke, refined by `strokeWidth`, `strokeAlign` (`"inside"`, `"center"`, `"outside"`), `strokeDash`, `strokeCap`, and `strokeJoin`. `strokes` takes several, as objects with `color`, `weight`, `align`, `dash`, `cap`, `join`, and `visible`; `strokeWeights={{ top, right, bottom, left }}` sets per-side widths. `dashPattern` sets the node's own dash pattern, separate from the per-stroke `strokeDash`.- `rounded` and `roundedTL`/`roundedTR`/`roundedBL`/`roundedBR` control corners. `strokeWidth`, `opacity`, `rotate`, and `blendMode` control appearance. `overflow="hidden"` clips content; do not hide accidental text overflow to make a broken layout appear correct.- `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow="offsetX offsetY blur #color"` and `blur` are convenient shorthands. Effect helpers take `radius`, as Figma's effects do; when a JSX string is rendered, an option a paint or effect helper does not support is reported as a warning.