@pairdraw/design-jsx 0.19.2 → 0.21.0

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.
@@ -1,7 +1,7 @@
1
1
  import { uniqBy } from 'es-toolkit'
2
2
  import * as v from 'valibot'
3
3
 
4
- import type { Fill, SceneNode, TextLength } from '@pairdraw/scene-graph'
4
+ import type { DocumentColorSpace, Fill, SceneNode, TextLength } from '@pairdraw/scene-graph'
5
5
  import type { Color } from '@pairdraw/scene-graph/primitives'
6
6
  import { percentLength, pixelLength } from '@pairdraw/scene-graph/text'
7
7
 
@@ -75,7 +75,8 @@ function autoLength(value: number | typeof AUTO): number | null {
75
75
  */
76
76
  export function applyTextDetailOverrides(
77
77
  props: Record<string, unknown>,
78
- o: Partial<SceneNode>
78
+ o: Partial<SceneNode>,
79
+ profile: DocumentColorSpace
79
80
  ): void {
80
81
  const parsed = parseScriptInput('Invalid <Text> props', textPropsSchema, props)
81
82
  const color = parsed.textDecorationColor
@@ -85,7 +86,7 @@ export function applyTextDetailOverrides(
85
86
  o.textDecorationFills =
86
87
  typeof color === 'string' && color.toLowerCase() === CURRENT_COLOR.toLowerCase()
87
88
  ? []
88
- : [fillFromValue(color)]
89
+ : [fillFromValue(color, profile)]
89
90
  }
90
91
  if (parsed.textDecorationThickness !== undefined)
91
92
  o.textDecorationThickness = autoLength(parsed.textDecorationThickness)
@@ -1,4 +1,4 @@
1
- import type { Fill, SceneNode, ArcData, NodeType } from '@pairdraw/scene-graph'
1
+ import type { DocumentColorSpace, Fill, SceneNode, ArcData, NodeType } from '@pairdraw/scene-graph'
2
2
  import { colorToFill, tryParseColor } from '@pairdraw/scene-graph/color'
3
3
  import { parseCSSGridTracks, parseCSSNumber } from '@pairdraw/scene-graph/css'
4
4
  import {
@@ -95,14 +95,18 @@ export function applySizeOverrides(
95
95
  * Sets the layer's fills, moving each shader paint's preset into the layer's plugin data,
96
96
  * where a shader paint keeps it.
97
97
  */
98
- function setFills(values: (string | Color | Fill)[], o: Partial<SceneNode>): void {
98
+ function setFills(
99
+ values: (string | Color | Fill)[],
100
+ o: Partial<SceneNode>,
101
+ profile: DocumentColorSpace
102
+ ): void {
99
103
  const shaders: ShaderPaint[] = []
100
104
  // A colour string no parser reads would otherwise become black, opposite to leaving it out.
101
105
  const usable = values.filter(
102
- (value) => typeof value !== 'string' || tryParseColor(value) !== null
106
+ (value) => typeof value !== 'string' || tryParseColor(value, profile) !== null
103
107
  )
104
108
  const fills = usable.map((value) => {
105
- const fill = fillFromValue(value)
109
+ const fill = fillFromValue(value, profile)
106
110
  if (!isShaderFill(fill)) return fill
107
111
  const { shader, ...paint } = fill
108
112
  if (paint.imageHash) shaders.push(shader)
@@ -114,19 +118,23 @@ function setFills(values: (string | Color | Fill)[], o: Partial<SceneNode>): voi
114
118
  o.pluginData = withShaderPaints({ pluginData: o.pluginData ?? [] }, fills, shaders)
115
119
  }
116
120
 
117
- function applyFillOverride(props: Record<string, unknown>, o: Partial<SceneNode>): void {
121
+ function applyFillOverride(
122
+ props: Record<string, unknown>,
123
+ o: Partial<SceneNode>,
124
+ profile: DocumentColorSpace
125
+ ): void {
118
126
  // `fills={[]}` paints nothing, which a text, painted black by default, has to say.
119
127
  if (Array.isArray(props.fills) && props.fills.length === 0) {
120
128
  o.fills = []
121
129
  return
122
130
  }
123
131
  if (Array.isArray(props.fills)) {
124
- setFills(props.fills.filter(isFillValue), o)
132
+ setFills(props.fills.filter(isFillValue), o, profile)
125
133
  return
126
134
  }
127
135
 
128
136
  const bg = designJSXProp(props, 'bg')
129
- if (isFillValue(bg)) setFills([bg], o)
137
+ if (isFillValue(bg)) setFills([bg], o, profile)
130
138
  }
131
139
 
132
140
  function applyCornerOverrides(props: LayerProps, o: Partial<SceneNode>): void {
@@ -149,9 +157,13 @@ function applyCornerOverrides(props: LayerProps, o: Partial<SceneNode>): void {
149
157
  if (props.cornerSmoothing !== undefined) o.cornerSmoothing = props.cornerSmoothing
150
158
  }
151
159
 
152
- function applyVisualOverrides(props: LayerProps, o: Partial<SceneNode>): void {
153
- applyFillOverride(props, o)
154
- applyStrokeOverrides(props, o)
160
+ function applyVisualOverrides(
161
+ props: LayerProps,
162
+ o: Partial<SceneNode>,
163
+ profile: DocumentColorSpace
164
+ ): void {
165
+ applyFillOverride(props, o, profile)
166
+ applyStrokeOverrides(props, o, profile)
155
167
  applyCornerOverrides(props, o)
156
168
 
157
169
  if (props.opacity !== undefined) o.opacity = props.opacity
@@ -337,7 +349,11 @@ function applyLayoutOverrides(
337
349
  if (props.grow !== undefined) o.layoutGrow = props.grow as number
338
350
  }
339
351
 
340
- function applyTextStyleOverrides(props: LayerProps, o: Partial<SceneNode>): void {
352
+ function applyTextStyleOverrides(
353
+ props: LayerProps,
354
+ o: Partial<SceneNode>,
355
+ profile: DocumentColorSpace
356
+ ): void {
341
357
  const fontSize = designJSXProp(props, 'size')
342
358
  if (typeof fontSize === 'number') o.fontSize = fontSize
343
359
 
@@ -354,7 +370,7 @@ function applyTextStyleOverrides(props: LayerProps, o: Partial<SceneNode>): void
354
370
  }
355
371
 
356
372
  if (typeof props.color === 'string' || isColor(props.color)) {
357
- o.fills = [colorToFill(props.color)]
373
+ o.fills = [colorToFill(props.color, profile)]
358
374
  }
359
375
 
360
376
  // A value it cannot read is left out, so the text keeps its own.
@@ -364,7 +380,7 @@ function applyTextStyleOverrides(props: LayerProps, o: Partial<SceneNode>): void
364
380
  if (letterSpacing) o.letterSpacing = letterSpacing
365
381
  const textDecoration = TEXT_DECORATION.parse(props.textDecoration)
366
382
  if (textDecoration) o.textDecoration = textDecoration
367
- applyTextDetailOverrides(props, o)
383
+ applyTextDetailOverrides(props, o, profile)
368
384
  const textCase = TEXT_CASE.parse(props.textCase)
369
385
  if (textCase) o.textCase = textCase
370
386
  if (props.maxLines !== undefined) {
@@ -417,9 +433,10 @@ function applyTextAutoResize(
417
433
  function applyTextOverrides(
418
434
  props: LayerProps,
419
435
  o: Partial<SceneNode>,
420
- parentLayout: SceneNode['layoutMode']
436
+ parentLayout: SceneNode['layoutMode'],
437
+ profile: DocumentColorSpace
421
438
  ): void {
422
- applyTextStyleOverrides(props, o)
439
+ applyTextStyleOverrides(props, o, profile)
423
440
  // Without `dir` the node keeps the style's own direction, which is auto by default; writing an
424
441
  // undefined override would erase it and stop Arabic text from being detected as RTL.
425
442
  const direction = parseDirection(props.dir)
@@ -474,7 +491,8 @@ export function shapeOverrides(
474
491
  export function propsToOverrides(
475
492
  props: Record<string, unknown>,
476
493
  isText: boolean,
477
- parentLayout: SceneNode['layoutMode']
494
+ parentLayout: SceneNode['layoutMode'],
495
+ profile: DocumentColorSpace = 'srgb'
478
496
  ): Partial<SceneNode> {
479
497
  // Layer props are read once, after `style` gives the ones the props leave unset.
480
498
  const layer = readLayerProps(normalizeStyleProps(props))
@@ -483,10 +501,10 @@ export function propsToOverrides(
483
501
  if (layer.name) o.name = layer.name
484
502
 
485
503
  const { w, h } = applySizeOverrides(layer, o, parentLayout)
486
- applyVisualOverrides(layer, o)
504
+ applyVisualOverrides(layer, o, profile)
487
505
  applyLayoutOverrides(layer, o, w, h, isText, parentLayout)
488
- if (isText) applyTextOverrides(layer, o, parentLayout)
489
- applyEffectOverrides(layer, o)
506
+ if (isText) applyTextOverrides(layer, o, parentLayout, profile)
507
+ applyEffectOverrides(layer, o, profile)
490
508
  applyStateOverrides(layer, o)
491
509
 
492
510
  return o
@@ -18,7 +18,7 @@ This reference describes scene creation, not React DOM output. Use the `render`
18
18
  ## Paint, text, and artwork
19
19
 
20
20
  - `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]])]}`. Without a `transform` a linear gradient runs left to right and the others are centred; `transform` is Figma's `gradientTransform`, mapping the layer's unit square onto gradient space. Write it as the plugin API's rows, as export does: `linearGradient(stops, { transform: [[0, 1, 0], [-1, 0, 1]] })` runs top to bottom. A paint written as an object takes its `gradientTransform` and `imageTransform` as rows too; the matrix fields `{ m00, m01, m02, m10, m11, m12 }` are accepted as well.
21
- - `shader(effects, { opacity, visible, blendMode })` fills a layer with a shader from the [`shaders`](https://shaders.com) library: an array of effects such as `bg={shader([{ type: 'Aurora', props: { speed: 2 } }])}`, or a preset as shaders.com exports it, `{ components: [...] }`. The canvas shows the shader's first frame, preview plays it, and files and other formats keep that frame as an image.
21
+ - `shader(effects, { opacity, visible, blendMode })` fills a layer with a shader from the [`shaders`](https://shaders.com) library: an array of effects such as `bg={shader([{ type: 'Aurora', props: { speed: 2 } }])}`, or a preset as shaders.com exports it, `{ components: [...] }`. A gradient effect takes `stops` as `{ color, position }` pairs, and an effect that draws on a shape takes `shape` as JSON such as `'{"type":"sphere3D","radius":0.35}'`. The app's gallery starts one from 21 complete presets, including `liquidChrome`, `flutedGlass`, `paperStock`, `godRays`, `thermal`, `voronoiCells`, `deepWater`, `smokePlume`, `neonPulse`, `grainWash`, `obsidian`, and `marbleInk`. The canvas shows the shader's first frame, preview plays the shaders on screen, and files and other formats keep that frame as an image.
22
22
  - `shaderSource(code, { uniforms, opacity, visible, blendMode })` fills a layer with shader code written here rather than a preset; `uniforms` gives the values its `@directive` inputs start with, such as `bg={shaderSource(code, { uniforms: { u_color: '#ff0000' } })}`.
23
23
  - `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`.
24
24
  - `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.
package/src/renderer.ts CHANGED
@@ -2,7 +2,10 @@ import { omitBy } from 'es-toolkit/object'
2
2
 
3
3
  import type { NodeType, SceneGraph, SceneNode, VariableModeMap } from '@pairdraw/scene-graph'
4
4
  import { parseColor } from '@pairdraw/scene-graph/color'
5
- import { setInstanceOverride } from '@pairdraw/scene-graph/instances/overrides'
5
+ import {
6
+ ownInstanceOverrides,
7
+ setInstanceOverride
8
+ } from '@pairdraw/scene-graph/instances/overrides'
6
9
  import { textLayerName } from '@pairdraw/scene-graph/node-defaults'
7
10
 
8
11
  import type { RekaScope } from './behaviours'
@@ -183,7 +186,12 @@ function renderOwnLayerElement(
183
186
  const parentLayout = graph.getNode(parentId)?.layoutMode ?? 'NONE'
184
187
  const layer = omitBy(
185
188
  {
186
- ...propsToOverrides(readElementProps(element.layer, props), false, parentLayout),
189
+ ...propsToOverrides(
190
+ readElementProps(element.layer, props),
191
+ false,
192
+ parentLayout,
193
+ graph.documentColorSpace
194
+ ),
187
195
  ...(modes && { variableModes: modes })
188
196
  },
189
197
  (value) => value === undefined
@@ -426,7 +434,7 @@ async function renderInstanceNode(
426
434
  throw new Error(`<Instance> component not found: ${label}`)
427
435
  }
428
436
  const overrides: Partial<SceneNode> = {
429
- ...propsToOverrides(props, false, parentLayout),
437
+ ...propsToOverrides(props, false, parentLayout, graph.documentColorSpace),
430
438
  ...layerMetadata(modes, bound.references)
431
439
  }
432
440
  // Instances inherit their container layout, but explicitly authored dimensions
@@ -450,7 +458,7 @@ async function renderInstanceNode(
450
458
  graph.createInstance(component.id, parentId, overrides) ?? graph.createNode('FRAME', parentId)
451
459
  try {
452
460
  for (const [field, value] of Object.entries(overrides)) {
453
- setInstanceOverride(instance.instanceOverrides, instance.id, instance.id, field, value)
461
+ setInstanceOverride(ownInstanceOverrides(instance), instance.id, instance.id, field, value)
454
462
  }
455
463
  graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })
456
464
  applyBindings(graph, instance.id, bindings)
@@ -499,7 +507,7 @@ function applyInstanceOverrides(
499
507
  const child = descendants.find((n) => n.name === childName)
500
508
  if (!child || !(prop in child)) continue
501
509
  graph.updateNode(child.id, { [prop]: value })
502
- setInstanceOverride(instance.instanceOverrides, instance.id, child.id, prop, value)
510
+ setInstanceOverride(ownInstanceOverrides(instance), instance.id, child.id, prop, value)
503
511
  mutated = true
504
512
  }
505
513
  if (mutated) {
@@ -561,7 +569,7 @@ export function elementOverrides(
561
569
  const layer = { parentId, modes }
562
570
  const { props, bindings } = preparePropsForRender(graph, bound.props, isText, layer)
563
571
  const overrides = {
564
- ...propsToOverrides(props, isText, parentLayout),
572
+ ...propsToOverrides(props, isText, parentLayout, graph.documentColorSpace),
565
573
  ...shapeOverrides(nodeType, props),
566
574
  ...layerMetadata(modes, bound.references)
567
575
  }
@@ -569,7 +577,12 @@ export function elementOverrides(
569
577
  if (isText) {
570
578
  // A span's variables give their values; characters keep no bindings of their own.
571
579
  const spanOverrides = (spanProps: Record<string, unknown>) =>
572
- propsToOverrides(preparePropsForRender(graph, spanProps, true, layer).props, true, 'NONE')
580
+ propsToOverrides(
581
+ preparePropsForRender(graph, spanProps, true, layer).props,
582
+ true,
583
+ 'NONE',
584
+ graph.documentColorSpace
585
+ )
573
586
  Object.assign(overrides, textContentFields(tree, props, overrides, spanOverrides))
574
587
  // As Figma does, a text layer follows its characters until a name is given, and keeps a given
575
588
  // name through later edits.
@@ -60,8 +60,8 @@ export function shapeWithTextProps(graph: SceneGraph, node: SceneNode): JSXProp[
60
60
  if (height !== SHAPE_WITH_TEXT_SIZE) props.push(['h', height])
61
61
  if (node.shapeTruncates) props.push(['truncate', true])
62
62
  if (node.rotation !== 0) props.push(['rotate', Math.round(node.rotation * 100) / 100])
63
- collectFillProps(node, props)
64
- collectStrokeProps(node, props)
63
+ collectFillProps(node, props, graph.documentColorSpace)
64
+ collectStrokeProps(node, props, graph.documentColorSpace)
65
65
  if (node.cornerRadius !== SHAPE_WITH_TEXT_CORNER_RADIUS)
66
66
  props.push(['rounded', node.cornerRadius])
67
67
  return props
@@ -173,7 +173,7 @@ export function resolveVariableId(graph: SceneGraph, variable: DesignVariable):
173
173
  return collection && variableNamed(graph, collection, variable.name)?.id
174
174
  }
175
175
  if (graph.variables.has(variable.name)) return variable.name
176
- const matches = [...graph.variables.values()].filter((item) => item.name === variable.name)
176
+ const matches = graph.getLiveVariables().filter((item) => item.name === variable.name)
177
177
  if (matches.length > 1) {
178
178
  const [first] = matches
179
179
  const collection = graph.variableCollections.get(first.collectionId)?.name ?? ''