@pairdraw/design-jsx 0.0.0-stage → 0.16.1

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.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -2
  3. package/dist/chunks/attributes.js +32 -0
  4. package/dist/chunks/attributes.js.map +1 -0
  5. package/dist/chunks/behaviours/elements.js +321 -0
  6. package/dist/chunks/behaviours/elements.js.map +1 -0
  7. package/dist/chunks/behaviours/render.js +368 -0
  8. package/dist/chunks/behaviours/render.js.map +1 -0
  9. package/dist/chunks/component-properties.js +809 -0
  10. package/dist/chunks/component-properties.js.map +1 -0
  11. package/dist/chunks/components.js +78 -0
  12. package/dist/chunks/components.js.map +1 -0
  13. package/dist/chunks/connectors.js +290 -0
  14. package/dist/chunks/connectors.js.map +1 -0
  15. package/dist/chunks/effects.js +57 -0
  16. package/dist/chunks/effects.js.map +1 -0
  17. package/dist/chunks/elements.js +618 -0
  18. package/dist/chunks/elements.js.map +1 -0
  19. package/dist/chunks/export/behaviours.js +113 -0
  20. package/dist/chunks/export/behaviours.js.map +1 -0
  21. package/dist/chunks/export/bindings.js +273 -0
  22. package/dist/chunks/export/bindings.js.map +1 -0
  23. package/dist/chunks/export/decoration.js +96 -0
  24. package/dist/chunks/export/decoration.js.map +1 -0
  25. package/dist/chunks/export/font.js +17 -0
  26. package/dist/chunks/export/font.js.map +1 -0
  27. package/dist/chunks/export/icons.js +34 -0
  28. package/dist/chunks/export/icons.js.map +1 -0
  29. package/dist/chunks/export/index.js +1390 -0
  30. package/dist/chunks/export/index.js.map +1 -0
  31. package/dist/chunks/fields.js +1065 -0
  32. package/dist/chunks/fields.js.map +1 -0
  33. package/dist/chunks/index.js +438 -0
  34. package/dist/chunks/index.js.map +1 -0
  35. package/dist/chunks/jsx-dev-runtime.js +16 -0
  36. package/dist/chunks/jsx-dev-runtime.js.map +1 -0
  37. package/dist/index.d.ts +543 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +11 -0
  40. package/dist/jsx-dev-runtime.d.ts +2 -0
  41. package/dist/jsx-dev-runtime.js +2 -0
  42. package/dist/jsx-runtime.d.ts +44 -0
  43. package/dist/jsx-runtime.d.ts.map +1 -0
  44. package/dist/jsx-runtime.js +2 -0
  45. package/dist/tree.d.ts +261 -0
  46. package/dist/tree.d.ts.map +1 -0
  47. package/package.json +66 -4
  48. package/src/attributes.ts +30 -0
  49. package/src/behaviours/elements.ts +47 -0
  50. package/src/behaviours/index.ts +253 -0
  51. package/src/behaviours/render.ts +144 -0
  52. package/src/component-properties.ts +428 -0
  53. package/src/components.ts +109 -0
  54. package/src/connectors.ts +316 -0
  55. package/src/definitions.ts +277 -0
  56. package/src/effects.ts +80 -0
  57. package/src/elements.ts +321 -0
  58. package/src/enums.ts +182 -0
  59. package/src/export/behaviours.ts +143 -0
  60. package/src/export/bindings.ts +155 -0
  61. package/src/export/decoration.ts +39 -0
  62. package/src/export/font.ts +25 -0
  63. package/src/export/helpers.ts +75 -0
  64. package/src/export/icons.ts +42 -0
  65. package/src/export/index.ts +297 -0
  66. package/src/export/paint.ts +276 -0
  67. package/src/export/props.ts +369 -0
  68. package/src/export/state.ts +52 -0
  69. package/src/export/text.ts +155 -0
  70. package/src/export/value.ts +99 -0
  71. package/src/export/variables.ts +393 -0
  72. package/src/fields.ts +39 -0
  73. package/src/figjam-text.ts +37 -0
  74. package/src/global.d.ts +4 -0
  75. package/src/helpers.ts +135 -0
  76. package/src/index.ts +155 -0
  77. package/src/jsx-dev-runtime.ts +1 -0
  78. package/src/jsx-runtime.ts +51 -0
  79. package/src/mini-react.ts +23 -0
  80. package/src/overrides/effects.ts +39 -0
  81. package/src/overrides/fills.ts +56 -0
  82. package/src/overrides/state.ts +55 -0
  83. package/src/overrides/strokes.ts +109 -0
  84. package/src/overrides/text.ts +116 -0
  85. package/src/overrides/vector.ts +122 -0
  86. package/src/paints.ts +230 -0
  87. package/src/props-overrides.ts +481 -0
  88. package/src/reconcile.ts +134 -0
  89. package/src/reference/authoring.md +76 -0
  90. package/src/reference/examples.ts +49 -0
  91. package/src/reference.ts +19 -0
  92. package/src/refs.ts +48 -0
  93. package/src/render.ts +147 -0
  94. package/src/renderer.ts +598 -0
  95. package/src/scalar-bindings.ts +36 -0
  96. package/src/schema.ts +213 -0
  97. package/src/services.ts +36 -0
  98. package/src/shapes-with-text.ts +73 -0
  99. package/src/stickies.ts +72 -0
  100. package/src/streaming/index.ts +47 -0
  101. package/src/streaming/literals.ts +82 -0
  102. package/src/streaming/projection.ts +189 -0
  103. package/src/text-content.ts +233 -0
  104. package/src/transform.ts +78 -0
  105. package/src/tree.ts +285 -0
  106. package/src/types.ts +13 -0
  107. package/src/variables/graph.ts +265 -0
  108. package/src/variables/index.ts +124 -0
  109. package/src/variables/props.ts +77 -0
@@ -0,0 +1,116 @@
1
+ import { uniqBy } from 'es-toolkit'
2
+ import * as v from 'valibot'
3
+
4
+ import type { Fill, SceneNode, TextLength } from '@pairdraw/scene-graph'
5
+ import type { Color } from '@pairdraw/scene-graph/primitives'
6
+ import { percentLength, pixelLength } from '@pairdraw/scene-graph/text'
7
+
8
+ import { TEXT_DECORATION_STYLE } from '../enums'
9
+ import { parseScriptInput } from '../schema'
10
+ import { fillFromValue, isFillValue } from './fills'
11
+
12
+ /** Lengths that follow the font unless set take `'auto'`, as in CSS. */
13
+ export const AUTO = 'auto'
14
+ /** The decoration colour that follows the text's, as in CSS. */
15
+ export const CURRENT_COLOR = 'currentColor'
16
+
17
+ /** A percent of the font size, such as `'150%'`, which follows the size as in Figma. */
18
+ const PERCENT_LENGTH = /^(-?\d+(?:\.\d+)?)%$/
19
+
20
+ /** A line height or letter spacing a prop gives: pixels as a number, or a percent string. */
21
+ export function textLengthProp(value: unknown): TextLength | undefined {
22
+ if (typeof value === 'number' && Number.isFinite(value)) return pixelLength(value)
23
+ const match = typeof value === 'string' ? PERCENT_LENGTH.exec(value.trim()) : null
24
+ return match ? percentLength(Number(match[1])) : undefined
25
+ }
26
+
27
+ /** A line height or letter spacing as a prop writes it. */
28
+ export function textLengthJSX(length: TextLength): number | `${number}%` {
29
+ return length.unit === 'PIXELS' ? length.value : `${length.value}%`
30
+ }
31
+
32
+ const lengthSchema = v.pipe(v.number(), v.finite())
33
+ const autoLengthSchema = v.union([lengthSchema, v.literal(AUTO)])
34
+ /** An OpenType feature or axis tag: four printable ASCII characters, such as `tnum` or `wght`. */
35
+ const openTypeTagSchema = v.pipe(
36
+ v.string(),
37
+ v.regex(/^[\x20-\x7E]{4}$/, 'Expected a four-character tag')
38
+ )
39
+
40
+ const textPropsSchema = v.object({
41
+ // Read by TEXT_DECORATION_STYLE, so an unknown style warns rather than failing the render.
42
+ textDecorationStyle: v.optional(v.string()),
43
+ textDecorationColor: v.optional(
44
+ v.custom<string | Color | Fill>(isFillValue, 'Expected a colour or a paint')
45
+ ),
46
+ textDecorationThickness: v.optional(autoLengthSchema),
47
+ textUnderlineOffset: v.optional(autoLengthSchema),
48
+ textDecorationSkipInk: v.optional(v.boolean()),
49
+ fontFeatures: v.optional(
50
+ v.pipe(
51
+ v.record(openTypeTagSchema, v.boolean()),
52
+ // Tags are stored in capitals, so `liga` and `LIGA` are the same feature.
53
+ v.check((features) => {
54
+ const tags = Object.keys(features)
55
+ return uniqBy(tags, (tag) => tag.toUpperCase()).length === tags.length
56
+ }, 'Expected each feature tag once, in any case')
57
+ )
58
+ ),
59
+ fontVariations: v.optional(v.record(openTypeTagSchema, lengthSchema)),
60
+ lang: v.optional(v.nullable(v.pipe(v.string(), v.trim(), v.nonEmpty()))),
61
+ fontOpticalSizing: v.optional(v.picklist([AUTO, 'none'])),
62
+ paragraphSpacing: v.optional(lengthSchema),
63
+ listSpacing: v.optional(lengthSchema),
64
+ paragraphIndent: v.optional(lengthSchema),
65
+ hangingList: v.optional(v.boolean())
66
+ })
67
+
68
+ function autoLength(value: number | typeof AUTO): number | null {
69
+ return value === AUTO ? null : value
70
+ }
71
+
72
+ /**
73
+ * The decoration details, font settings, and text-wide paragraph spacing a `<Text>` or `<Span>`
74
+ * sets. Font settings replace the ones around them as a whole, as CSS's do.
75
+ */
76
+ export function applyTextDetailOverrides(
77
+ props: Record<string, unknown>,
78
+ o: Partial<SceneNode>
79
+ ): void {
80
+ const parsed = parseScriptInput('Invalid <Text> props', textPropsSchema, props)
81
+ const color = parsed.textDecorationColor
82
+ const style = TEXT_DECORATION_STYLE.parse(parsed.textDecorationStyle)
83
+ if (style) o.textDecorationStyle = style
84
+ if (color !== undefined) {
85
+ o.textDecorationFills =
86
+ typeof color === 'string' && color.toLowerCase() === CURRENT_COLOR.toLowerCase()
87
+ ? []
88
+ : [fillFromValue(color)]
89
+ }
90
+ if (parsed.textDecorationThickness !== undefined)
91
+ o.textDecorationThickness = autoLength(parsed.textDecorationThickness)
92
+ if (parsed.textUnderlineOffset !== undefined)
93
+ o.textUnderlineOffset = autoLength(parsed.textUnderlineOffset)
94
+ if (parsed.textDecorationSkipInk !== undefined)
95
+ o.textDecorationSkipInk = parsed.textDecorationSkipInk
96
+ if (parsed.fontFeatures) {
97
+ // The scene keeps feature tags as Figma does, in capitals.
98
+ o.fontFeatures = Object.entries(parsed.fontFeatures).map(([tag, enabled]) => ({
99
+ tag: tag.toUpperCase(),
100
+ enabled
101
+ }))
102
+ }
103
+ if (parsed.fontVariations) {
104
+ o.fontVariations = Object.entries(parsed.fontVariations).map(([axis, value]) => ({
105
+ axis,
106
+ value
107
+ }))
108
+ }
109
+ if (parsed.lang !== undefined) o.textLanguage = parsed.lang
110
+ if (parsed.fontOpticalSizing !== undefined)
111
+ o.opticalSizeFromFontSize = parsed.fontOpticalSizing === AUTO
112
+ if (parsed.paragraphSpacing !== undefined) o.paragraphSpacing = parsed.paragraphSpacing
113
+ if (parsed.listSpacing !== undefined) o.listSpacing = parsed.listSpacing
114
+ if (parsed.paragraphIndent !== undefined) o.paragraphIndent = parsed.paragraphIndent
115
+ if (parsed.hangingList !== undefined) o.hangingList = parsed.hangingList
116
+ }
@@ -0,0 +1,122 @@
1
+ import * as v from 'valibot'
2
+
3
+ import type { SceneGraph, SceneNode, VectorNetwork } from '@pairdraw/scene-graph'
4
+ import { parsePluginVectorPath, parseSVGPathData } from '@pairdraw/scene-graph/parse-path'
5
+ import type { Rect } from '@pairdraw/scene-graph/primitives'
6
+ import { mergeVectorNetworks, transformVectorNetwork } from '@pairdraw/scene-graph/vector-network'
7
+
8
+ import { designJSXProp } from '../definitions'
9
+ import { normalizeStyleProps } from '../props-overrides'
10
+ import { parseScriptInput } from '../schema'
11
+
12
+ const FILL_RULES = { nonzero: 'NONZERO', evenodd: 'EVENODD' } as const
13
+
14
+ const vectorPropsSchema = v.pipe(
15
+ v.object({
16
+ d: v.optional(v.pipe(v.string(), v.trim(), v.nonEmpty())),
17
+ fillRule: v.optional(v.picklist(['nonzero', 'evenodd'])),
18
+ paths: v.optional(
19
+ v.array(
20
+ v.object({
21
+ windingRule: v.picklist(['NONZERO', 'EVENODD', 'NONE']),
22
+ data: v.string()
23
+ })
24
+ )
25
+ )
26
+ }),
27
+ v.check((props) => !(props.d && props.paths), 'Expected `d` or `paths`, not both')
28
+ )
29
+
30
+ function parsedNetwork(result: ReturnType<typeof parseSVGPathData>, which: string): VectorNetwork {
31
+ if (!result.ok) throw new Error(`Invalid <Vector> ${which}: ${result.error}`)
32
+ return result.network
33
+ }
34
+
35
+ /**
36
+ * The vector network a `<Vector>`'s `d` or `paths` draws, in the coordinates they are written
37
+ * in, or null when it has neither. `d` reads as SVG does, its open subpaths filled only when
38
+ * `fillsOpenSubpaths`; `paths` as Figma's `vectorPaths`.
39
+ */
40
+ function vectorNetworkProp(
41
+ props: Record<string, unknown>,
42
+ fillsOpenSubpaths: boolean
43
+ ): VectorNetwork | null {
44
+ const parsed = parseScriptInput('Invalid <Vector> props', vectorPropsSchema, props)
45
+ if (parsed.d) {
46
+ const windingRule = FILL_RULES[parsed.fillRule ?? 'nonzero']
47
+ return parsedNetwork(
48
+ parseSVGPathData(parsed.d, windingRule, { includeOpenRegions: fillsOpenSubpaths }),
49
+ '`d`'
50
+ )
51
+ }
52
+ if (!parsed.paths) return null
53
+ return mergeVectorNetworks(
54
+ parsed.paths.map(({ windingRule, data }, index) =>
55
+ parsedNetwork(parsePluginVectorPath(data, windingRule), `paths[${index}]`)
56
+ )
57
+ )
58
+ }
59
+
60
+ /** The size a `<Vector>` is given, on the axes its props size. */
61
+ interface VectorSize {
62
+ width?: number
63
+ height?: number
64
+ }
65
+
66
+ /**
67
+ * The size of `node` on the axes `props` set, by `w`/`width` and `h`/`height`, however they set
68
+ * them: a number, a variable, or a layout value. The layer already holds what they resolve to.
69
+ */
70
+ function vectorSize(props: Record<string, unknown>, node: SceneNode): VectorSize {
71
+ const sized = normalizeStyleProps(props)
72
+ return {
73
+ width: designJSXProp(sized, 'w') === undefined ? undefined : node.width,
74
+ height: designJSXProp(sized, 'h') === undefined ? undefined : node.height
75
+ }
76
+ }
77
+
78
+ /**
79
+ * The fields that place a vector network in its layer. On an axis the author sized, the path is
80
+ * drawn in the layer's own box as written, as an SVG icon is in its view box. On an axis left
81
+ * unsized, the layer takes the network's extent and moves to where it starts, as Figma's
82
+ * `vectorPaths` setter does.
83
+ */
84
+ function vectorGeometry(
85
+ network: VectorNetwork,
86
+ bounds: Rect,
87
+ size: VectorSize,
88
+ position: Pick<SceneNode, 'x' | 'y'>
89
+ ): Partial<SceneNode> {
90
+ const dx = size.width === undefined ? bounds.x : 0
91
+ const dy = size.height === undefined ? bounds.y : 0
92
+ return {
93
+ x: position.x + dx,
94
+ y: position.y + dy,
95
+ width: size.width ?? bounds.width,
96
+ height: size.height ?? bounds.height,
97
+ vectorNetwork: transformVectorNetwork([1, 0, -dx, 0, 1, -dy, 0, 0, 1], network)
98
+ }
99
+ }
100
+
101
+ /**
102
+ * A `<Vector>`'s `d` or `paths` as its vector network, the layer sized to it. `vectorBounds`
103
+ * measures the network, curves included, which the engine provides.
104
+ */
105
+ export function placeVectorPaths(
106
+ graph: SceneGraph,
107
+ node: SceneNode,
108
+ props: Record<string, unknown>,
109
+ vectorBounds: (network: VectorNetwork) => Rect
110
+ ): void {
111
+ if (node.type !== 'VECTOR') return
112
+ // A filled vector strokes its regions, which close, so an open subpath becomes one only when
113
+ // nothing strokes it, as SVG import decides.
114
+ const fillsOpenSubpaths =
115
+ node.fills.some((fill) => fill.visible) && !node.strokes.some((stroke) => stroke.visible)
116
+ const network = vectorNetworkProp(props, fillsOpenSubpaths)
117
+ if (!network) return
118
+ graph.updateNode(
119
+ node.id,
120
+ vectorGeometry(network, vectorBounds(network), vectorSize(props, node), node)
121
+ )
122
+ }
package/src/paints.ts ADDED
@@ -0,0 +1,230 @@
1
+ import * as v from 'valibot'
2
+
3
+ import type {
4
+ BlendMode,
5
+ Fill,
6
+ FillType,
7
+ GradientStop,
8
+ GradientTransform
9
+ } from '@pairdraw/scene-graph'
10
+ import { colorToFill, parseColor } from '@pairdraw/scene-graph/color'
11
+ import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
12
+ import {
13
+ IDENTITY_GRADIENT_TRANSFORM,
14
+ type GradientTransformRows
15
+ } from '@pairdraw/scene-graph/gradient'
16
+ import { paintTransformSchema } from '@pairdraw/scene-graph/paints'
17
+ import type { Color } from '@pairdraw/scene-graph/primitives'
18
+ import {
19
+ createShaderPaint,
20
+ createShaderSourcePaint,
21
+ SHADER_SOURCE_MAX_BYTES,
22
+ shaderPresetSchema,
23
+ type ShaderComponent,
24
+ type ShaderPaint,
25
+ type ShaderPreset,
26
+ type ShaderUniformValue
27
+ } from '@pairdraw/scene-graph/shaders'
28
+
29
+ import { parseScriptInput } from './schema'
30
+
31
+ export type PaintColor = string | Color
32
+ export type PaintStop = readonly [PaintColor, number] | { color: PaintColor; position: number }
33
+
34
+ export interface SolidPaintOptions {
35
+ opacity?: number
36
+ visible?: boolean
37
+ blendMode?: BlendMode
38
+ }
39
+
40
+ export interface GradientPaintOptions extends SolidPaintOptions {
41
+ /** Figma's `gradientTransform`, as its plugin API's rows or as the matrix fields. */
42
+ transform?: GradientTransformRows | GradientTransform
43
+ }
44
+
45
+ const colorSchema = v.union([
46
+ v.string(),
47
+ v.object({ r: v.number(), g: v.number(), b: v.number(), a: v.number() })
48
+ ])
49
+ const stopsSchema = v.array(
50
+ v.union(
51
+ [v.tuple([colorSchema, v.number()]), v.object({ color: colorSchema, position: v.number() })],
52
+ (issue) => `Expected [color, position] or { color, position } but received ${issue.received}`
53
+ )
54
+ )
55
+
56
+ const GRADIENT_HELPERS = {
57
+ GRADIENT_LINEAR: 'linearGradient',
58
+ GRADIENT_RADIAL: 'radialGradient',
59
+ GRADIENT_ANGULAR: 'angularGradient',
60
+ GRADIENT_DIAMOND: 'diamondGradient'
61
+ } as const
62
+
63
+ type GradientType = keyof typeof GRADIENT_HELPERS
64
+
65
+ /**
66
+ * Scripts call the helpers with whatever they guess, so a wrong call says what it expects
67
+ * and what was wrong, as Valibot describes it.
68
+ */
69
+ function parseStops(type: GradientType, stops: unknown): PaintStop[] {
70
+ return parseScriptInput(
71
+ `${GRADIENT_HELPERS[type]}() expects an array of stops, such as [['#3b82f6', 0], ['#8b5cf6', 1]]`,
72
+ stopsSchema,
73
+ stops
74
+ )
75
+ }
76
+
77
+ function parseTransform(type: GradientType, transform: unknown): GradientTransform {
78
+ return parseScriptInput(
79
+ `${GRADIENT_HELPERS[type]}() expects a transform as Figma's [[a, b, c], [d, e, f]], such as [[0, 1, 0], [-1, 0, 1]], or { m00, m01, m02, m10, m11, m12 }`,
80
+ paintTransformSchema,
81
+ transform
82
+ )
83
+ }
84
+
85
+ function toColor(color: PaintColor): Color {
86
+ return typeof color === 'string' ? parseColor(color) : color
87
+ }
88
+
89
+ function toStop(stop: PaintStop): GradientStop {
90
+ if ('color' in stop) {
91
+ return { color: toColor(stop.color), position: stop.position }
92
+ }
93
+ return { color: toColor(stop[0]), position: stop[1] }
94
+ }
95
+
96
+ export function solid(color: PaintColor, options: SolidPaintOptions = {}): Fill {
97
+ const fill = colorToFill(color)
98
+ return {
99
+ ...fill,
100
+ opacity: options.opacity ?? fill.opacity,
101
+ visible: options.visible ?? true,
102
+ blendMode: options.blendMode
103
+ }
104
+ }
105
+
106
+ export function gradient(
107
+ type: Extract<FillType, GradientType>,
108
+ stops: PaintStop[],
109
+ options: GradientPaintOptions = {}
110
+ ): Fill {
111
+ return {
112
+ type,
113
+ color: { ...TRANSPARENT },
114
+ opacity: options.opacity ?? 1,
115
+ visible: options.visible ?? true,
116
+ blendMode: options.blendMode,
117
+ gradientStops: parseStops(type, stops).map(toStop),
118
+ gradientTransform:
119
+ options.transform === undefined
120
+ ? { ...IDENTITY_GRADIENT_TRANSFORM }
121
+ : parseTransform(type, options.transform)
122
+ }
123
+ }
124
+
125
+ /** A paint that draws a shader, carrying its entry until the layer it fills stores it. */
126
+ export interface ShaderFill extends Fill {
127
+ shader: ShaderPaint
128
+ }
129
+
130
+ /** Whether a paint value came from {@link shader} or {@link shaderSource}. */
131
+ export function isShaderFill(value: Fill): value is ShaderFill {
132
+ return 'shader' in value
133
+ }
134
+
135
+ const shaderSourceSchema = v.pipe(
136
+ v.string(),
137
+ v.trim(),
138
+ v.nonEmpty('Expected the shader code to write'),
139
+ v.check(
140
+ (source) => new TextEncoder().encode(source).length <= SHADER_SOURCE_MAX_BYTES,
141
+ `Expected at most ${SHADER_SOURCE_MAX_BYTES} bytes of shader code`
142
+ )
143
+ )
144
+
145
+ /** The inputs a user-written shader was given values for. */
146
+ const shaderUniformsSchema = v.record(v.string(), v.union([v.number(), v.boolean(), v.string()]))
147
+
148
+ export interface ShaderSourceOptions extends SolidPaintOptions {
149
+ uniforms?: Record<string, ShaderUniformValue>
150
+ }
151
+
152
+ /**
153
+ * A paint that draws shader code written here rather than a preset from the `shaders` library.
154
+ * Its inputs are the `@directive` ones the code declares.
155
+ */
156
+ export function shaderSource(source: string, options: ShaderSourceOptions = {}): ShaderFill {
157
+ const code = parseScriptInput(
158
+ 'shaderSource() expects the shader code as a string',
159
+ shaderSourceSchema,
160
+ source
161
+ )
162
+ const uniforms =
163
+ options.uniforms === undefined
164
+ ? {}
165
+ : parseScriptInput(
166
+ 'shaderSource() expects its uniforms as { name: value }',
167
+ shaderUniformsSchema,
168
+ options.uniforms
169
+ )
170
+ const { paint, shader } = createShaderSourcePaint(code)
171
+ return {
172
+ ...paint,
173
+ opacity: options.opacity ?? 1,
174
+ visible: options.visible ?? true,
175
+ blendMode: options.blendMode,
176
+ shader: { ...shader, uniforms }
177
+ }
178
+ }
179
+
180
+ const shaderInputSchema = v.union(
181
+ [
182
+ shaderPresetSchema,
183
+ v.pipe(
184
+ v.array(v.unknown()),
185
+ v.transform((components) => ({ components })),
186
+ shaderPresetSchema
187
+ )
188
+ ],
189
+ (issue) =>
190
+ `Expected a preset { components: [...] } or an array of effects but received ${issue.received}`
191
+ )
192
+
193
+ /**
194
+ * A paint that draws a shader from the `shaders` library: a preset as shaders.com exports it, or
195
+ * just its effects, such as `shader([{ type: 'Aurora' }])`.
196
+ */
197
+ export function shader(
198
+ preset: ShaderPreset | ShaderComponent[],
199
+ options: SolidPaintOptions = {}
200
+ ): ShaderFill {
201
+ const parsed = parseScriptInput(
202
+ "shader() expects a preset such as { components: [{ type: 'Aurora' }] } or an array of effects",
203
+ shaderInputSchema,
204
+ preset
205
+ )
206
+ const { paint, shader } = createShaderPaint(parsed)
207
+ return {
208
+ ...paint,
209
+ opacity: options.opacity ?? 1,
210
+ visible: options.visible ?? true,
211
+ blendMode: options.blendMode,
212
+ shader
213
+ }
214
+ }
215
+
216
+ export function linearGradient(stops: PaintStop[], options?: GradientPaintOptions): Fill {
217
+ return gradient('GRADIENT_LINEAR', stops, options)
218
+ }
219
+
220
+ export function radialGradient(stops: PaintStop[], options?: GradientPaintOptions): Fill {
221
+ return gradient('GRADIENT_RADIAL', stops, options)
222
+ }
223
+
224
+ export function angularGradient(stops: PaintStop[], options?: GradientPaintOptions): Fill {
225
+ return gradient('GRADIENT_ANGULAR', stops, options)
226
+ }
227
+
228
+ export function diamondGradient(stops: PaintStop[], options?: GradientPaintOptions): Fill {
229
+ return gradient('GRADIENT_DIAMOND', stops, options)
230
+ }