@measured/puck 0.11.0-canary.4613df4 → 0.11.0-canary.7f13efc
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/README.md +51 -68
- package/dist/index.css +228 -173
- package/dist/index.d.ts +49 -57
- package/dist/index.js +30528 -1026
- package/package.json +1 -1
package/README.md
CHANGED
@@ -234,33 +234,28 @@ The current DropZone implementation has certain rules and limitations:
|
|
234
234
|
3. You can't drag between DropZones that don't share a parent (or _area_)
|
235
235
|
4. Your mouse must be directly over a DropZone for a collision to be detected
|
236
236
|
|
237
|
-
##
|
237
|
+
## External fields
|
238
238
|
|
239
|
-
|
239
|
+
External fields can be used to import data from a third-party API, such as a headless CMS.
|
240
240
|
|
241
241
|
### Example
|
242
242
|
|
243
|
-
The `external` field type enables us to
|
243
|
+
The `external` field type enables us to query data from a third party API:
|
244
244
|
|
245
245
|
```tsx
|
246
|
-
const myAdaptor = {
|
247
|
-
name: "My adaptor",
|
248
|
-
fetchList: async () => {
|
249
|
-
const response = await fetch("https://www.example.com/api");
|
250
|
-
|
251
|
-
return {
|
252
|
-
text: response.json().text,
|
253
|
-
};
|
254
|
-
},
|
255
|
-
};
|
256
|
-
|
257
246
|
const config = {
|
258
247
|
components: {
|
259
248
|
HeadingBlock: {
|
260
249
|
fields: {
|
261
250
|
myData: {
|
262
251
|
type: "external",
|
263
|
-
|
252
|
+
fetchList: async () => {
|
253
|
+
const response = await fetch("https://www.example.com/api");
|
254
|
+
|
255
|
+
return {
|
256
|
+
text: response.json().text,
|
257
|
+
};
|
258
|
+
},
|
264
259
|
},
|
265
260
|
},
|
266
261
|
render: ({ myData }) => {
|
@@ -271,25 +266,15 @@ const config = {
|
|
271
266
|
};
|
272
267
|
```
|
273
268
|
|
274
|
-
When the user interacts with this
|
269
|
+
When the user interacts with this external field, they'll be presented with a list of items to choose from. Once they select an item, the value will be mapped onto the prop. In this case, `myData`.
|
275
270
|
|
276
271
|
## Dynamic prop resolution
|
277
272
|
|
278
273
|
Dynamic prop resolution allows developers to resolve props for components without saving the data to the Puck data model.
|
279
274
|
|
280
|
-
###
|
281
|
-
|
282
|
-
`resolveProps` is defined in the component config, and allows the developer to make asynchronous calls to change the props after they've been set by Puck.
|
283
|
-
|
284
|
-
#### Args
|
285
|
-
|
286
|
-
- **props** (`object`): the current props for your component stored in the Puck data
|
287
|
-
|
288
|
-
#### Response
|
275
|
+
### resolveData()
|
289
276
|
|
290
|
-
|
291
|
-
- **readOnly** (`object`): an object describing which fields on the component are currently read-only
|
292
|
-
- **[prop]** (`boolean`): boolean describing whether or not the prop field is read-only
|
277
|
+
`resolveData` is defined in the component config, and allows the developer to make asynchronous calls to change the [ComponentData](#componentdata) after they've been set by Puck. Receives [ComponentData](#componentdata) and returns [ComponentData](#componentdata).
|
293
278
|
|
294
279
|
#### Examples
|
295
280
|
|
@@ -309,7 +294,7 @@ const config = {
|
|
309
294
|
type: "text",
|
310
295
|
},
|
311
296
|
},
|
312
|
-
|
297
|
+
resolveData: async (props) => {
|
313
298
|
return {
|
314
299
|
props: {
|
315
300
|
title: props.text,
|
@@ -327,35 +312,31 @@ const config = {
|
|
327
312
|
};
|
328
313
|
```
|
329
314
|
|
330
|
-
##### Combining with
|
315
|
+
##### Combining with external fields
|
331
316
|
|
332
|
-
A more advanced pattern is to combine the `
|
317
|
+
A more advanced pattern is to combine the `resolveData` method with `external` fields to dynamically fetch data when rendering the component.
|
333
318
|
|
334
319
|
```tsx
|
335
|
-
const myAdaptor = {
|
336
|
-
name: "My adaptor",
|
337
|
-
fetchList: async () => {
|
338
|
-
const response = await fetch("https://www.example.com/api");
|
339
|
-
|
340
|
-
return {
|
341
|
-
id: response.json().id,
|
342
|
-
};
|
343
|
-
},
|
344
|
-
};
|
345
|
-
|
346
320
|
const config = {
|
347
321
|
components: {
|
348
322
|
HeadingBlock: {
|
349
323
|
fields: {
|
350
324
|
myData: {
|
351
325
|
type: "external",
|
352
|
-
|
326
|
+
placeholder: "Select from example.com",
|
327
|
+
fetchList: async () => {
|
328
|
+
const response = await fetch("https://www.example.com/api");
|
329
|
+
|
330
|
+
return {
|
331
|
+
id: response.json().id,
|
332
|
+
};
|
333
|
+
},
|
353
334
|
},
|
354
335
|
title: {
|
355
336
|
type: "text",
|
356
337
|
},
|
357
338
|
},
|
358
|
-
|
339
|
+
resolveData: async (props) => {
|
359
340
|
if (!myData.id) {
|
360
341
|
return { props, readOnly: { title: false } };
|
361
342
|
}
|
@@ -381,14 +362,16 @@ const config = {
|
|
381
362
|
};
|
382
363
|
```
|
383
364
|
|
384
|
-
###
|
365
|
+
### resolveAllData()
|
366
|
+
|
367
|
+
`resolveAllData` is a utility function exported by Puck to enable the developer to run all their `resolveData` methods before rendering the component with `<Render>`.
|
385
368
|
|
386
|
-
`resolveData`
|
369
|
+
If your `resolveData` methods rely on any external APIs, you should run this before rendering your page.
|
387
370
|
|
388
371
|
```tsx
|
389
|
-
import {
|
372
|
+
import { resolveAllData } from "@measured/puck";
|
390
373
|
|
391
|
-
const resolvedData =
|
374
|
+
const resolvedData = resolveAllData(data, config);
|
392
375
|
```
|
393
376
|
|
394
377
|
## Reference
|
@@ -431,18 +414,13 @@ The `Config` object describes which components Puck should render, how they shou
|
|
431
414
|
- **title** (`Field`): Title of the content, typically used for the page title.
|
432
415
|
- **[fieldName]** (`Field`): User defined fields, used to describe the input data stored in the `root` key.
|
433
416
|
- **render** (`Component`): Render a React component at the root of your component tree. Useful for defining context providers.
|
417
|
+
- **resolveData** (`async (data: ComponentData) => ComponentData` [optional]): Function to dynamically change props before rendering the root.
|
434
418
|
- **components** (`object`): Definitions for each of the components you want to show in the visual editor
|
435
419
|
- **[componentName]** (`object`)
|
436
420
|
- **fields** (`Field`): The Field objects describing the input data stored against this component.
|
437
421
|
- **render** (`Component`): Render function for your React component. Receives props as defined in fields.
|
438
422
|
- **defaultProps** (`object` [optional]): Default props to pass to your component. Will show in fields.
|
439
|
-
- **
|
440
|
-
- Args
|
441
|
-
- **props** (`object`): the current props for your component stored in the Puck data
|
442
|
-
- Response
|
443
|
-
- **props** (`object`): the resolved props for your component. Will not be stored in the Puck data
|
444
|
-
- **readOnly** (`object`): an object describing which fields on the component are currently read-only
|
445
|
-
- **[prop]** (`boolean`): boolean describing whether or not the prop field is read-only
|
423
|
+
- **resolveData** (`async (data: ComponentData) => ComponentData` [optional]): Function to dynamically change props before rendering the component.
|
446
424
|
- **categories** (`object`): Component categories for rendering in the side bar or restricting in DropZones
|
447
425
|
- **[categoryName]** (`object`)
|
448
426
|
- **components** (`sting[]`, [optional]): Array containing the names of components in this category
|
@@ -494,14 +472,12 @@ A `Field` represents a user input field shown in the Puck interface.
|
|
494
472
|
|
495
473
|
### External Fields
|
496
474
|
|
497
|
-
External fields can be used to load content from an external content repository, like Strapi.js
|
475
|
+
External fields can be used to load content from an external content repository, like Strapi.js.
|
498
476
|
|
499
477
|
- **type** (`"external"`)
|
500
|
-
- **
|
501
|
-
|
502
|
-
|
503
|
-
- **mapProp** (`(selectedItem: object) => object`): Map the selected item into another shape
|
504
|
-
- **adaptorParams** (`object`): Paramaters passed to the adaptor
|
478
|
+
- **placeholder** (`string`): A placeholder for the external field button
|
479
|
+
- **fetchList** (`() => object`): Fetch content from a third-party API and return an array
|
480
|
+
- **mapProp** (`(selectedItem: object) => object`): Map the selected item into another shape
|
505
481
|
|
506
482
|
### Custom Fields
|
507
483
|
|
@@ -532,13 +508,20 @@ The `AppState` object stores the puck application state.
|
|
532
508
|
|
533
509
|
The `Data` object stores the puck page data.
|
534
510
|
|
535
|
-
- **root** (`
|
536
|
-
- **
|
537
|
-
|
538
|
-
- **content** (`
|
539
|
-
|
540
|
-
|
541
|
-
|
511
|
+
- **root** (`ComponentData`): The component data for the root of your configuration.
|
512
|
+
- **props** (object): Extends `ComponentData.props`, with some additional props
|
513
|
+
- **title** (`string`, [optional]): Title of the content, typically used for the page title
|
514
|
+
- **content** (`ComponentData[]`): Component data for the main content
|
515
|
+
- **zones** (`object`, [optional]): Component data for all DropZones
|
516
|
+
**[zoneCompound]** (`ComponentData[]`): Component data for a specific DropZone `zone` within a component instance
|
517
|
+
|
518
|
+
### `ComponentData`
|
519
|
+
|
520
|
+
- **type** (`string`): Component name
|
521
|
+
- **props** (`object`):
|
522
|
+
- **[prop]** (`any`): User defined data from component fields
|
523
|
+
- **readOnly** (`object`): Object describing which fields on the component are currently read-only. Can use dot-notation for arrays, like `array[1].text` or `array[*].text`.
|
524
|
+
- **[prop]** (`boolean`): boolean describing whether or not the prop field is read-only
|
542
525
|
|
543
526
|
### `Plugin`
|
544
527
|
|