@uniformdev/canvas-react 18.1.1-alpha.11 → 18.1.2-alpha.4

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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { ComponentInstance, SubscribeToCompositionOptions, UpdateCompositionMessage, RootComponentInstance } from '@uniformdev/canvas';
1
+ import { ComponentInstance, RootComponentInstance, UpdateCompositionMessage, SubscribeToCompositionOptions } from '@uniformdev/canvas';
2
+ export { createUniformApiEnhancer } from '@uniformdev/canvas';
2
3
  import React$1, { Key, ReactNode } from 'react';
3
4
 
4
5
  /**
@@ -29,80 +30,50 @@ type ComponentStore = {
29
30
  get: (name: string) => React.ComponentType<ComponentProps<any>> | undefined;
30
31
  };
31
32
 
32
- type CompositionProps<TRenderProps = unknown> = {
33
- /** The root component in the composition */
34
- data: ComponentInstance;
33
+ /**
34
+ * A default implementation of a component-not-implemented component.
35
+ * Useful for model-first workflows where frontend dev comes later.
36
+ * To make this work, it needs to be the default returned from the
37
+ * resolveRenderer() function when the component is unknown.
38
+ **/
39
+ declare function DefaultNotImplementedComponent(props: ComponentProps): JSX.Element | null;
40
+
41
+ type UniformComponentProps<TRenderProps = unknown> = {
42
+ /** The data of the component instance */
43
+ data: ComponentInstance | RootComponentInstance;
35
44
  /** Resolves a React component to render a Canvas component, generally by inspecting type/variant */
36
45
  resolveRenderer?: RenderComponentResolver;
37
- /** Either markup to render the composition directly, or render props to provide the ComponentProps for the root component */
46
+ /** Children to render. Can also be a function that takes ComponentProps as argument */
38
47
  children?: ReactNode | ((props: ComponentProps<TRenderProps>) => JSX.Element);
39
48
  /**
40
- * When to track behavior from enrichment tags on this composition
49
+ * When to track behavior from enrichment tags on the current composition
41
50
  * onView: adds enrichment score when the visitor views the tagged component in the browser viewport.
42
- * onLoad: adds enrichment score as soon as the composition mounts, regardless of viewport.
51
+ * onLoad: adds enrichment score as soon as the component mounts, regardless of viewport.
43
52
  *
44
- * NOTE: onView renders a <div> tag around components that have enrichment tags, to support IntersectionObserver.
53
+ * NOTE: onView renders a `<div>` tag around components that have enrichment tags, to support IntersectionObserver.
45
54
  * onLoad does not need to do this, and renders no wrapping tag.
46
55
  *
47
56
  * Default: onView
48
57
  */
49
58
  behaviorTracking?: 'onLoad' | 'onView';
50
59
  };
51
- type CompositionContext = {
52
- /** The parent composition */
53
- composition: ComponentInstance;
54
- /** The current function to translate component data into React components */
55
- resolveRenderer?: RenderComponentResolver;
56
- behaviorTracking: 'onLoad' | 'onView';
60
+ type UniformComponentContextValue = {
61
+ data?: UniformComponentProps['data'];
62
+ resolveRenderer?: UniformComponentProps['resolveRenderer'];
63
+ behaviorTracking?: UniformComponentProps['behaviorTracking'];
57
64
  };
58
65
  /**
59
- * Gets the parent Canvas composition of the current component.
60
- * Note: this gets the direct parent, not necessarily the root of the composition.
66
+ * Gets the data of the closest `<UniformComponent />` ancestor.
61
67
  */
62
- declare function useComposition(): CompositionContext | undefined;
63
- /** Forms a Canvas composition root, providing services to render a tree of Canvas components */
64
- declare function Composition<TRenderProps = unknown>({ data, resolveRenderer, children, behaviorTracking, }: CompositionProps<TRenderProps>): JSX.Element | null;
65
-
68
+ declare function useUniformCurrentComponent(): UniformComponentContextValue;
66
69
  /**
67
- * A default implementation of a component-not-implemented component.
68
- * Useful for model-first workflows where frontend dev comes later.
69
- * To make this work, it needs to be the default returned from the
70
- * resolveRenderer() function when the component is unknown.
71
- **/
72
- declare function DefaultNotImplementedComponent(props: ComponentProps): JSX.Element | null;
73
-
74
- type CustomSlotChildRenderFunc = (options: {
75
- child: ReactNode;
76
- component: ComponentInstance;
77
- key: Key;
78
- }) => JSX.Element;
79
- type SlotProps<TSlotNames extends string> = {
80
- /** Name of the slot to render */
81
- name: TSlotNames;
82
- /**
83
- * Function to resolve the React component to render for a given Canvas component type.
84
- * If not specified, the resolveRenderer function on the nearest Layout will be used, if any.
85
- */
86
- resolveRenderer?: RenderComponentResolver;
87
- /** Optional render props enables wrapping all child components in the slot with some markup */
88
- children?: CustomSlotChildRenderFunc;
89
- /**
90
- * Optional ReactNode to use as a placeholder in Canvas editor when the slot is empty.
91
- * The node is used to render a placeholder with realistic dimensions and it's never shown to users.
92
- * Pass `null` to disable the placeholder.
93
- */
94
- emptyPlaceholder?: React$1.ReactNode;
95
- };
96
- /** Renders a named Slot within a Composition. */
97
- declare function Slot<TSlotNames extends string = string>({ name, resolveRenderer, children, emptyPlaceholder, }: SlotProps<TSlotNames>): JSX.Element | null;
98
-
99
- type UseCompositionEventEffectOptions = Omit<Partial<SubscribeToCompositionOptions>, 'callback'> & {
100
- enabled: boolean;
101
- effect: () => void;
102
- };
103
- /** Hook to manage a subscription to a realtime event on a composition */
104
- declare function useCompositionEventEffect({ enabled, projectId, compositionId, effect, }: UseCompositionEventEffectOptions): void;
70
+ * Allows the rendering of a Canvas component instance (root or not), and its children if it has any.
71
+ * Note that the actual rendering happens inside `<Slot />`, this component only provides the services needed to achieve that.
72
+ * This component is used internally by `<UniformComposition />`, which you should use in most cases.
73
+ */
74
+ declare function UniformComponent<TRenderProps = unknown>({ data, resolveRenderer, children, behaviorTracking, }: UniformComponentProps<TRenderProps>): JSX.Element | null;
105
75
 
76
+ /** @deprecated use `createUniformApiEnhancer` instead */
106
77
  declare const createApiEnhancer: ({ apiUrl }: {
107
78
  apiUrl: string;
108
79
  }) => (message: UpdateCompositionMessage) => Promise<{
@@ -173,10 +144,19 @@ declare const createApiEnhancer: ({ apiUrl }: {
173
144
  };
174
145
  } | undefined;
175
146
  }>;
176
- declare const useContextualEditing: ({ initialCompositionValue, enhance, }: {
147
+ type UseUniformContextualEditingProps = {
177
148
  initialCompositionValue: RootComponentInstance;
178
- enhance?: ((composition: UpdateCompositionMessage) => RootComponentInstance | Promise<RootComponentInstance>) | undefined;
179
- }) => {
149
+ /**
150
+ * A function to enhance the composition after receiving it from Canvas editor.
151
+ * WARNING: This enhancer will run on the client side. Make sure you're not exposing any secrets. You can use `createApiEnhancer` to create an enhancer based on an API route.
152
+ */
153
+ enhance?: (composition: UpdateCompositionMessage) => RootComponentInstance | Promise<RootComponentInstance>;
154
+ };
155
+ /**
156
+ * Adds contextual editing capability to a Uniform app.
157
+ * This hook is already integrated in `<UniformComposition />`, you won't need to use it directly, unless you have a custom setup.
158
+ */
159
+ declare const useUniformContextualEditing: ({ initialCompositionValue, enhance, }: UseUniformContextualEditingProps) => {
180
160
  composition: {
181
161
  type: string;
182
162
  parameters?: {
@@ -247,6 +227,69 @@ declare const useContextualEditing: ({ initialCompositionValue, enhance, }: {
247
227
  };
248
228
  };
249
229
 
230
+ type UniformCompositionProps<TRenderProps = unknown> = UniformComponentProps<TRenderProps> & {
231
+ /** The composition data */
232
+ data: RootComponentInstance;
233
+ contextualEditingEnhancer?: UseUniformContextualEditingProps['enhance'];
234
+ };
235
+ type UniformCompositionContextValue = {
236
+ data?: UniformCompositionProps['data'];
237
+ resolveRenderer?: UniformCompositionProps['resolveRenderer'];
238
+ behaviorTracking?: UniformCompositionProps['behaviorTracking'];
239
+ };
240
+ /**
241
+ * Gets the data of the closest `<UniformComposition />` ancestor.
242
+ */
243
+ declare function useUniformCurrentComposition(): UniformCompositionContextValue;
244
+ /**
245
+ * The main component to render a Canvas composition.
246
+ * It renders the full tree of components, and provides some services to the children, such as `useUniformCurrentComposition`.
247
+ * It also takes care of enabling [Contextual Editing](https://docs.uniform.app/capabilities/composition/contextual-editing).
248
+ */
249
+ declare function UniformComposition<TRenderProps = unknown>({ data, behaviorTracking, children, resolveRenderer, contextualEditingEnhancer, }: UniformCompositionProps<TRenderProps>): JSX.Element;
250
+ /** @deprecated use `useUniformCurrentComposition` instead */
251
+ declare const useComposition: typeof useUniformCurrentComposition;
252
+ /** @deprecated use `UniformComposition` instead */
253
+ declare const Composition: typeof UniformComposition;
254
+ /** @deprecated use `UniformCompositionProps` instead */
255
+ type CompositionProps<TRenderProps = unknown> = UniformCompositionProps<TRenderProps>;
256
+
257
+ type CustomSlotChildRenderFunc = (options: {
258
+ child: ReactNode;
259
+ component: ComponentInstance;
260
+ key: Key;
261
+ }) => JSX.Element;
262
+ type UniformSlotProps<TSlotNames extends string> = {
263
+ /** Name of the slot to render */
264
+ name: TSlotNames;
265
+ /**
266
+ * Function to resolve the React component to render for a given Canvas component type.
267
+ * If not specified, the resolveRenderer function on the nearest Layout will be used, if any.
268
+ */
269
+ resolveRenderer?: RenderComponentResolver;
270
+ /** Optional render props enables wrapping all child components in the slot with some markup */
271
+ children?: CustomSlotChildRenderFunc;
272
+ /**
273
+ * Optional ReactNode to use as a placeholder in Canvas editor when the slot is empty.
274
+ * The node is used to render a placeholder with realistic dimensions and it's never shown to users.
275
+ * Pass `null` to disable the placeholder.
276
+ */
277
+ emptyPlaceholder?: React$1.ReactNode;
278
+ };
279
+ /** Renders a named Slot within a Composition. */
280
+ declare function UniformSlot<TSlotNames extends string = string>({ name, resolveRenderer, children, emptyPlaceholder, }: UniformSlotProps<TSlotNames>): JSX.Element | null;
281
+ /** @deprecated use `UniformSlotProps` instead */
282
+ type SlotProps<TSlotNames extends string> = UniformSlotProps<TSlotNames>;
283
+ /** @deprecated use `UniformSlot` instead */
284
+ declare const Slot: typeof UniformSlot;
285
+
286
+ type UseCompositionEventEffectOptions = Omit<Partial<SubscribeToCompositionOptions>, 'callback'> & {
287
+ enabled: boolean;
288
+ effect: () => void;
289
+ };
290
+ /** Hook to manage a subscription to a realtime event on a composition */
291
+ declare function useCompositionEventEffect({ enabled, projectId, compositionId, effect, }: UseCompositionEventEffectOptions): void;
292
+
250
293
  declare const componentStore: ComponentStore;
251
294
  declare const registerUniformComponent: ({ type, component, }: {
252
295
  type: string | string[];
@@ -260,4 +303,4 @@ declare const createComponentStoreResolver: (options: {
260
303
  defaultComponent?: React.ComponentType<ComponentProps<any>>;
261
304
  }) => RenderComponentResolver;
262
305
 
263
- export { ComponentProps, ComponentStore, Composition, CompositionContext, CompositionProps, DefaultNotImplementedComponent, RenderComponentResolver, Slot, SlotProps, SystemRenderConfig, SystemRenderFunction, UseCompositionEventEffectOptions, componentStore, componentStoreResolver, createApiEnhancer, createComponentStore, createComponentStoreResolver, registerUniformComponent, useComposition, useCompositionEventEffect, useContextualEditing };
306
+ export { ComponentProps, ComponentStore, Composition, CompositionProps, DefaultNotImplementedComponent, RenderComponentResolver, Slot, SlotProps, SystemRenderConfig, SystemRenderFunction, UniformComponent, UniformComponentContextValue, UniformComponentProps, UniformComposition, UniformCompositionProps, UniformSlot, UniformSlotProps, UseCompositionEventEffectOptions, UseUniformContextualEditingProps, componentStore, componentStoreResolver, createApiEnhancer, createComponentStore, createComponentStoreResolver, registerUniformComponent, useComposition, useCompositionEventEffect, useUniformContextualEditing, useUniformCurrentComponent, useUniformCurrentComposition };