@pairdraw/design-jsx 0.17.0 → 0.18.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,22 +1,111 @@
1
- import type { Effect, SceneNode } from '@pairdraw/scene-graph'
1
+ import * as v from 'valibot'
2
+
3
+ import type { SceneNode } from '@pairdraw/scene-graph'
4
+ import { colorSchema } from '@pairdraw/scene-graph/color'
2
5
  import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
3
6
  import { parseCSSShadows } from '@pairdraw/scene-graph/css'
7
+ import { normalizeEffects } from '@pairdraw/scene-graph/effects'
8
+ import { finiteNumberSchema, type Vector } from '@pairdraw/scene-graph/primitives'
9
+ import { shaderUniformValueSchema } from '@pairdraw/scene-graph/shaders'
10
+
11
+ import { BLEND_MODE, enumProp } from '../enums'
12
+ import { parseScriptInput } from '../schema'
13
+
14
+ const offsetSchema = v.object({
15
+ x: v.optional(finiteNumberSchema),
16
+ y: v.optional(finiteNumberSchema)
17
+ })
18
+
19
+ const effectFields = {
20
+ radius: finiteNumberSchema,
21
+ offset: v.optional(offsetSchema),
22
+ spread: v.optional(finiteNumberSchema, 0),
23
+ visible: v.optional(v.boolean(), true),
24
+ blendMode: v.optional(enumProp(BLEND_MODE)),
25
+ showShadowBehindNode: v.optional(v.boolean())
26
+ }
27
+
28
+ const SHADOW_TYPES = ['DROP_SHADOW', 'INNER_SHADOW'] as const
29
+ const BLUR_TYPES = ['LAYER_BLUR', 'BACKGROUND_BLUR', 'FOREGROUND_BLUR'] as const
30
+
31
+ function shadowEffectSchema(type: (typeof SHADOW_TYPES)[number]) {
32
+ return v.object({
33
+ type: v.literal(type),
34
+ color: colorSchema,
35
+ ...effectFields
36
+ })
37
+ }
38
+
39
+ function blurEffectSchema(type: (typeof BLUR_TYPES)[number]) {
40
+ return v.object({
41
+ type: v.literal(type),
42
+ color: v.optional(colorSchema),
43
+ ...effectFields
44
+ })
45
+ }
46
+
47
+ const shaderEffectSchema = v.object({
48
+ type: v.literal('SHADER'),
49
+ source: v.pipe(v.string(), v.minLength(1)),
50
+ uniforms: v.optional(v.record(v.string(), shaderUniformValueSchema)),
51
+ uniformBindings: v.optional(v.record(v.string(), v.string())),
52
+ visible: v.optional(v.boolean(), true),
53
+ blendMode: v.optional(enumProp(BLEND_MODE))
54
+ })
55
+
56
+ const effectSchema = v.variant('type', [
57
+ shadowEffectSchema('DROP_SHADOW'),
58
+ shadowEffectSchema('INNER_SHADOW'),
59
+ blurEffectSchema('LAYER_BLUR'),
60
+ blurEffectSchema('BACKGROUND_BLUR'),
61
+ blurEffectSchema('FOREGROUND_BLUR'),
62
+ shaderEffectSchema
63
+ ])
64
+
65
+ const effectsSchema = v.array(effectSchema)
66
+
67
+ type ParsedEffect = v.InferOutput<typeof effectSchema>
68
+ type NormalizableEffect = Parameters<typeof normalizeEffects>[0][number]
69
+ type StoredShadow = Exclude<NormalizableEffect, { type: 'SHADER' }>
4
70
 
5
- function isEffect(value: unknown): value is Effect {
6
- return (
7
- value !== null &&
8
- typeof value === 'object' &&
9
- 'type' in value &&
10
- 'radius' in value &&
11
- 'visible' in value
12
- )
71
+ /** Drop an omitted offset so `normalizeEffects` can apply the shadow or blur default. */
72
+ function forNormalize(effect: ParsedEffect): NormalizableEffect {
73
+ if (effect.type === 'SHADER') {
74
+ const shader: Extract<NormalizableEffect, { type: 'SHADER' }> = {
75
+ type: 'SHADER',
76
+ source: effect.source,
77
+ visible: effect.visible
78
+ }
79
+ if (effect.uniforms !== undefined) shader.uniforms = effect.uniforms
80
+ if (effect.uniformBindings !== undefined) shader.uniformBindings = effect.uniformBindings
81
+ if (effect.blendMode !== undefined) shader.blendMode = effect.blendMode
82
+ return shader
83
+ }
84
+ const shadow: StoredShadow = {
85
+ type: effect.type,
86
+ color: effect.color ?? { ...TRANSPARENT },
87
+ radius: effect.radius,
88
+ spread: effect.spread,
89
+ visible: effect.visible
90
+ }
91
+ if (effect.offset !== undefined) {
92
+ const offset: Partial<Vector> = {}
93
+ if (effect.offset.x !== undefined) offset.x = effect.offset.x
94
+ if (effect.offset.y !== undefined) offset.y = effect.offset.y
95
+ shadow.offset = offset
96
+ }
97
+ if (effect.blendMode !== undefined) shadow.blendMode = effect.blendMode
98
+ if (effect.showShadowBehindNode !== undefined) {
99
+ shadow.showShadowBehindNode = effect.showShadowBehindNode
100
+ }
101
+ return shadow
13
102
  }
14
103
 
15
104
  /** `effects` takes structured effects; `shadow` appends its shadows and `blur` a layer blur. */
16
105
  export function applyEffectOverrides(props: Record<string, unknown>, o: Partial<SceneNode>): void {
17
- if (Array.isArray(props.effects)) {
18
- const effects = props.effects.filter(isEffect).map((effect) => structuredClone(effect))
19
- if (effects.length > 0) o.effects = effects
106
+ if (Array.isArray(props.effects) && props.effects.length > 0) {
107
+ const effects = parseScriptInput('effects', effectsSchema, props.effects).map(forNormalize)
108
+ o.effects = normalizeEffects(effects)
20
109
  }
21
110
 
22
111
  // A CSS shadow list, as `box-shadow` takes it.
@@ -23,7 +23,7 @@ This reference describes scene creation, not React DOM output. Use the `render`
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.
25
25
  - `rotate` turns a layer by degrees, and `flipX` and `flipY` reflect it across its centre before it turns, as the canvas places it. `Star` takes `points` and `innerRadius` (0–1, how deep its points go); `Polygon` takes `points`. `Ellipse` draws an arc or a donut with `startAngle` and `endAngle` in degrees, clockwise from three o'clock (0 to 360 by default), and `innerRadius` (0–1, the hole): `<Ellipse w={80} h={80} startAngle={0} endAngle={270} innerRadius={0.6} />`.
26
- - `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow` takes a CSS `box-shadow` list, such as `shadow="0 4 8 #0002, inset 0 1 0 #fff"`, and `blur` a layer blur radius; both 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.
26
+ - `effects` accepts structured effects such as `dropShadow`, `innerShadow`, and `layerBlur`. `shadow` takes a CSS `box-shadow` list, such as `shadow="0 4 8 #0002, inset 0 1 0 #fff"`, and `blur` a layer blur radius; both 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. A raw shadow needs an effect type, a numeric radius, and a color. A raw blur needs an effect type and a numeric radius, and a missing color is transparent. A shader needs `source`. Anything else is an error that names the prop.
27
27
  - Text content belongs inside `Text`. Use `size`, `font`, `weight`, `italic`, `lineHeight`, `letterSpacing`, `textAlign`, `textAlignVertical`, `textDecoration`, and `textCase`. Decorations take CSS's names and values: `textDecorationStyle` (`solid`, `dotted`, `wavy`), `textDecorationColor` (`currentColor` follows the text), `textDecorationThickness` and `textUnderlineOffset` (pixels or `auto`), and `textDecorationSkipInk`. `fontFeatures` turns OpenType features on or off by tag (`fontFeatures={{ tnum: true, liga: false }}`), `fontVariations` sets variable-font axes by tag (`fontVariations={{ wght: 650 }}`), and `lang` names the text's language (`lang="de"`) for shaping and font fallback. A variable font's optical size follows the font size unless `fontOpticalSizing="none"`, as in CSS; only then does an `opsz` in `fontVariations` apply. `lineHeight="auto"` follows the font, and `fills={[]}` paints no text. Verify fonts actually load before judging dimensions; do not assume every font is available.
28
28
  - `Span` styles some characters of a `Text` with `Text`'s own prop names: `size`, `font`, `weight`, `italic`, `color`, `fills`, `lineHeight`, `letterSpacing`, `textDecoration`, the decoration details, `fontFeatures`, `fontVariations`, `lang`, and `textCase`; a span's `fontFeatures` and `fontVariations` replace the text's rather than adding to them, and `fontOpticalSizing` is set on the `Text` for all of it. `href` links its characters to a URL without changing how they look. Spans nest; the inner one wins. `Paragraph` writes one paragraph: `list="ordered"` or `list="unordered"` makes it a list item nested at `level` 1–5, and `spacing`, `listSpacing`, and `indent` override the `Text`'s `paragraphSpacing`, `listSpacing`, and `paragraphIndent` for it; `hangingList` on the `Text` hangs list markers outside the text box. A `Text` holds only `Paragraph`s or none; without them, a line break in its text still starts a paragraph.
29
29
  - `Icon` uses an Iconify name, size, and color. Prefer icons to emoji when reliable vector output is needed. Image fills belong on appropriate leaf shapes, not containers whose children must remain visible.