@formepdf/react 0.7.9 → 0.7.11

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.
@@ -2,7 +2,7 @@ import { type ReactElement } from 'react';
2
2
  import type { Style, Edges, Corners, FormeDocument, FormeStyle, FormeEdges, FormeDimension, FormeColor, FormeCornerValues } from './types.js';
3
3
  /**
4
4
  * Serialize a React element tree into a Forme JSON document object.
5
- * The top-level element must be a <Document>.
5
+ * The top-level element must be a <Document> (or a component that returns one).
6
6
  */
7
7
  export declare function serialize(element: ReactElement): FormeDocument;
8
8
  export declare function mapStyle(style?: Style): FormeStyle;
package/dist/serialize.js CHANGED
@@ -40,12 +40,30 @@ function extractSourceLocation(element) {
40
40
  }
41
41
  return undefined;
42
42
  }
43
+ // ─── Component resolution ────────────────────────────────────────────
44
+ /**
45
+ * Resolve wrapper function components (e.g. `<MyReport data={...} />`) by
46
+ * calling them until we reach a Forme primitive like `<Document>`. This lets
47
+ * users pass custom components directly to `renderDocument()` /
48
+ * `serialize()` without manually invoking them first.
49
+ */
50
+ function resolveElement(element) {
51
+ let resolved = element;
52
+ for (let i = 0; i < 10 && typeof resolved.type === 'function' && resolved.type !== Document; i++) {
53
+ const result = resolved.type(resolved.props);
54
+ if (!isValidElement(result))
55
+ break;
56
+ resolved = result;
57
+ }
58
+ return resolved;
59
+ }
43
60
  // ─── Public API ──────────────────────────────────────────────────────
44
61
  /**
45
62
  * Serialize a React element tree into a Forme JSON document object.
46
- * The top-level element must be a <Document>.
63
+ * The top-level element must be a <Document> (or a component that returns one).
47
64
  */
48
65
  export function serialize(element) {
66
+ element = resolveElement(element);
49
67
  if (element.type !== Document) {
50
68
  throw new Error('Top-level element must be <Document>');
51
69
  }
@@ -670,7 +688,7 @@ export function mapStyle(style) {
670
688
  };
671
689
  }
672
690
  if (style.margin !== undefined || style.marginTop !== undefined || style.marginRight !== undefined || style.marginBottom !== undefined || style.marginLeft !== undefined || style.marginHorizontal !== undefined || style.marginVertical !== undefined) {
673
- const base = style.margin !== undefined ? expandEdges(style.margin) : { top: 0, right: 0, bottom: 0, left: 0 };
691
+ const base = style.margin !== undefined ? expandMarginEdges(style.margin) : { top: 0, right: 0, bottom: 0, left: 0 };
674
692
  const vt = style.marginVertical ?? base.top;
675
693
  const vb = style.marginVertical ?? base.bottom;
676
694
  const hl = style.marginHorizontal ?? base.left;
@@ -1060,6 +1078,23 @@ export function expandEdges(val) {
1060
1078
  }
1061
1079
  return { top: val.top, right: val.right, bottom: val.bottom, left: val.left };
1062
1080
  }
1081
+ /** Expand margin edges, preserving 'auto' string values. */
1082
+ function expandMarginEdges(val) {
1083
+ if (typeof val === 'number') {
1084
+ return { top: val, right: val, bottom: val, left: val };
1085
+ }
1086
+ if (typeof val === 'string') {
1087
+ if (val === 'auto') {
1088
+ return { top: 'auto', right: 'auto', bottom: 'auto', left: 'auto' };
1089
+ }
1090
+ const edges = parseCSSEdges(val);
1091
+ return edges;
1092
+ }
1093
+ if (Array.isArray(val)) {
1094
+ return parseCSSEdges(val);
1095
+ }
1096
+ return { top: val.top, right: val.right, bottom: val.bottom, left: val.left };
1097
+ }
1063
1098
  function expandEdgeValues(val) {
1064
1099
  if (typeof val === 'number') {
1065
1100
  return { top: val, right: val, bottom: val, left: val };
@@ -1124,6 +1159,7 @@ function mergeFonts(globalFonts, docFonts) {
1124
1159
  * Like `serialize()` but with expression marker detection for template compilation.
1125
1160
  */
1126
1161
  export function serializeTemplate(element) {
1162
+ element = resolveElement(element);
1127
1163
  if (element.type !== Document) {
1128
1164
  throw new Error('Top-level element must be <Document>');
1129
1165
  }
package/dist/types.d.ts CHANGED
@@ -76,12 +76,12 @@ export interface Style {
76
76
  paddingHorizontal?: number;
77
77
  paddingVertical?: number;
78
78
  margin?: number | string | number[] | Edges;
79
- marginTop?: number;
80
- marginRight?: number;
81
- marginBottom?: number;
82
- marginLeft?: number;
83
- marginHorizontal?: number;
84
- marginVertical?: number;
79
+ marginTop?: number | 'auto';
80
+ marginRight?: number | 'auto';
81
+ marginBottom?: number | 'auto';
82
+ marginLeft?: number | 'auto';
83
+ marginHorizontal?: number | 'auto';
84
+ marginVertical?: number | 'auto';
85
85
  fontSize?: number;
86
86
  fontFamily?: string;
87
87
  fontWeight?: number | 'normal' | 'bold';
@@ -469,6 +469,13 @@ export interface FormeEdges {
469
469
  bottom: number;
470
470
  left: number;
471
471
  }
472
+ /** Margin edges that support auto values (for centering). */
473
+ export interface FormeMarginEdges {
474
+ top: number | 'auto';
475
+ right: number | 'auto';
476
+ bottom: number | 'auto';
477
+ left: number | 'auto';
478
+ }
472
479
  export interface FormeNode {
473
480
  kind: FormeNodeKind;
474
481
  style: FormeStyle;
@@ -597,7 +604,7 @@ export interface FormeStyle {
597
604
  maxWidth?: FormeDimension;
598
605
  maxHeight?: FormeDimension;
599
606
  padding?: FormeEdges;
600
- margin?: FormeEdges;
607
+ margin?: FormeMarginEdges;
601
608
  flexDirection?: string;
602
609
  justifyContent?: string;
603
610
  alignItems?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formepdf/react",
3
- "version": "0.7.9",
3
+ "version": "0.7.11",
4
4
  "description": "JSX-to-JSON serializer for Forme PDF engine",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",