@foundrykit/menu-plugin 0.4.2 → 0.6.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.
@@ -107,10 +107,14 @@ const buildFeaturedFields = (options)=>{
107
107
  label: 'CTA Link Type',
108
108
  options: LINK_TYPE_OPTIONS
109
109
  },
110
+ {
111
+ name: 'ctaLabel',
112
+ type: 'text',
113
+ label: 'CTA Label'
114
+ },
110
115
  {
111
116
  name: 'ctaUrl',
112
117
  type: 'text',
113
- label: 'CTA URL',
114
118
  admin: {
115
119
  ...options?.disabled ? {} : {
116
120
  components: {
@@ -122,6 +126,7 @@ const buildFeaturedFields = (options)=>{
122
126
  baseUrl
123
127
  }
124
128
  },
129
+ label: 'CTA URL',
125
130
  validate: validateUrl
126
131
  },
127
132
  {
@@ -163,171 +168,19 @@ const buildFeaturedFields = (options)=>{
163
168
  }
164
169
  return fields;
165
170
  };
166
- const buildMegaColumnsField = (options)=>{
167
- const maxDepth = options?.maxDepth ?? 1;
168
- return {
169
- name: 'megaColumns',
170
- type: 'array',
171
- dbName: 'mcols',
172
- label: 'Mega Menu Columns',
173
- admin: {
174
- condition: (_data, siblingData)=>siblingData?.itemType === 'mega',
175
- ...options?.disabled ? {} : {
176
- components: {
177
- RowLabel: '@foundrykit/menu-plugin/client#MegaColumnRowLabel'
178
- }
179
- }
180
- },
181
- fields: [
182
- {
183
- name: 'columnTitle',
184
- type: 'text',
185
- label: 'Column Title',
186
- admin: {
187
- description: 'Optional heading displayed above this column.'
188
- }
189
- },
190
- {
191
- name: 'columnLinks',
192
- type: 'array',
193
- dbName: 'clinks',
194
- label: 'Column Links',
195
- admin: {
196
- ...options?.disabled ? {} : {
197
- components: {
198
- RowLabel: '@foundrykit/menu-plugin/client#ColumnLinkRowLabel'
199
- }
200
- }
201
- },
202
- fields: [
203
- // leaf-level basicFields (canNest = false since currentDepth === maxDepth)
204
- ...createMenuItemFields(options, maxDepth),
205
- {
206
- name: 'subLinks',
207
- type: 'array',
208
- dbName: 'slinks',
209
- label: 'Sub Links',
210
- admin: {
211
- description: 'Items revealed in the secondary panel when this link is hovered or selected.',
212
- ...options?.disabled ? {} : {
213
- components: {
214
- RowLabel: '@foundrykit/menu-plugin/client#SubMenuItemRowLabel'
215
- }
216
- }
217
- },
218
- fields: createMenuItemFields(options, maxDepth)
219
- }
220
- ]
221
- },
222
- {
223
- name: 'featured',
224
- type: 'group',
225
- label: 'Featured Panel',
226
- admin: {
227
- description: 'Optional right-hand panel with image, title, description, and CTA.'
228
- },
229
- fields: buildFeaturedFields(options)
230
- }
231
- ]
232
- };
233
- };
234
- export const createMenuItemFields = (options, currentDepth = 0)=>{
171
+ /**
172
+ * The simple-by-default link field set shared by every item that behaves like a
173
+ * link: `label`, `type`, `reference`/`url`, plus the "Options" and "Advanced"
174
+ * collapsible groups. No `itemType` and no nesting fields are included here.
175
+ */ const buildLeafLinkFields = (options)=>{
235
176
  const baseUrl = options?.baseUrl ?? '';
236
- const maxDepth = options?.maxDepth ?? 1;
237
177
  const relationTo = options?.relationTo ?? 'pages';
238
- const basicFields = [
239
- {
240
- type: 'row',
241
- fields: [
242
- {
243
- name: 'advancedOptions',
244
- type: 'radio',
245
- admin: {
246
- width: '33%'
247
- },
248
- defaultValue: 'default',
249
- options: [
250
- {
251
- label: 'Default',
252
- value: 'default'
253
- },
254
- {
255
- label: 'Advanced',
256
- value: 'advanced'
257
- }
258
- ],
259
- required: true
260
- },
261
- {
262
- name: 'target',
263
- type: 'radio',
264
- admin: {
265
- width: '33%'
266
- },
267
- defaultValue: '_self',
268
- label: 'Open in new tab',
269
- options: [
270
- {
271
- label: 'Same Tab',
272
- value: '_self'
273
- },
274
- {
275
- label: 'New Tab',
276
- value: '_blank'
277
- }
278
- ]
279
- },
280
- {
281
- name: 'visibility',
282
- type: 'radio',
283
- admin: {
284
- condition: (_data, siblingData)=>{
285
- return siblingData?.advancedOptions !== 'default';
286
- },
287
- width: '33%'
288
- },
289
- defaultValue: 'visible',
290
- options: [
291
- {
292
- label: 'Visible',
293
- value: 'visible'
294
- },
295
- {
296
- label: 'Invisible',
297
- value: 'invisible'
298
- }
299
- ]
300
- }
301
- ]
302
- },
303
- {
304
- name: 'displaySurface',
305
- type: 'select',
306
- defaultValue: 'both',
307
- label: 'Display Surface',
308
- admin: {
309
- description: 'Controls which navigation surfaces render this item.'
310
- },
311
- options: [
312
- {
313
- label: 'Navbar + Drawer (default)',
314
- value: 'both'
315
- },
316
- {
317
- label: 'Navbar Only',
318
- value: 'navbar'
319
- },
320
- {
321
- label: 'Drawer Only',
322
- value: 'drawer'
323
- }
324
- ]
325
- },
178
+ return [
326
179
  {
327
180
  name: 'type',
328
181
  type: 'select',
329
182
  admin: {
330
- description: 'Select the type of link for this menu item.'
183
+ description: 'Select the type of link for this menu item. On a mega menu or dropdown this is where the top-level label links to, and is optional.'
331
184
  },
332
185
  defaultValue: 'reference',
333
186
  options: LINK_TYPE_OPTIONS,
@@ -383,81 +236,285 @@ export const createMenuItemFields = (options, currentDepth = 0)=>{
383
236
  ]
384
237
  },
385
238
  {
386
- name: 'rel',
387
- type: 'text',
239
+ type: 'collapsible',
388
240
  admin: {
389
- condition: (_data, siblingData)=>{
390
- return siblingData?.advancedOptions !== 'default';
241
+ initCollapsed: true
242
+ },
243
+ fields: [
244
+ {
245
+ name: 'target',
246
+ type: 'radio',
247
+ defaultValue: '_self',
248
+ label: 'Open in new tab',
249
+ options: [
250
+ {
251
+ label: 'Same Tab',
252
+ value: '_self'
253
+ },
254
+ {
255
+ label: 'New Tab',
256
+ value: '_blank'
257
+ }
258
+ ]
259
+ },
260
+ {
261
+ name: 'displaySurface',
262
+ type: 'select',
263
+ admin: {
264
+ description: 'Controls which navigation surfaces render this item.'
265
+ },
266
+ defaultValue: 'both',
267
+ label: 'Display Surface',
268
+ options: [
269
+ {
270
+ label: 'Navbar + Drawer (default)',
271
+ value: 'both'
272
+ },
273
+ {
274
+ label: 'Navbar Only',
275
+ value: 'navbar'
276
+ },
277
+ {
278
+ label: 'Drawer Only',
279
+ value: 'drawer'
280
+ }
281
+ ]
391
282
  }
392
- }
283
+ ],
284
+ label: 'Options'
393
285
  },
394
286
  {
395
- name: 'roles',
396
- type: 'array',
287
+ type: 'collapsible',
397
288
  admin: {
398
- condition: (_data, siblingData)=>{
399
- return siblingData?.advancedOptions !== 'default';
400
- }
289
+ initCollapsed: true
401
290
  },
402
291
  fields: [
403
292
  {
404
- name: 'role',
293
+ name: 'visibility',
294
+ type: 'radio',
295
+ defaultValue: 'visible',
296
+ options: [
297
+ {
298
+ label: 'Visible',
299
+ value: 'visible'
300
+ },
301
+ {
302
+ label: 'Invisible',
303
+ value: 'invisible'
304
+ }
305
+ ]
306
+ },
307
+ {
308
+ name: 'rel',
405
309
  type: 'text'
310
+ },
311
+ {
312
+ name: 'roles',
313
+ type: 'array',
314
+ fields: [
315
+ {
316
+ name: 'role',
317
+ type: 'text'
318
+ }
319
+ ]
320
+ },
321
+ {
322
+ name: 'attrs',
323
+ type: 'textarea'
324
+ }
325
+ ],
326
+ label: 'Advanced'
327
+ }
328
+ ];
329
+ };
330
+ const buildMegaConfigFields = (options, currentDepth = 0)=>{
331
+ const dynamicSources = options?.dynamicSources ?? [];
332
+ const relationTo = options?.relationTo ?? 'pages';
333
+ const parentCollections = [
334
+ ...new Set(dynamicSources.map((source)=>source.parentCollection))
335
+ ];
336
+ const singleSource = dynamicSources.length === 1;
337
+ // Parent relationship: collapse to a single relationTo string when exactly one
338
+ // source is configured, union when many, fall back to the link relationTo when
339
+ // no dynamic sources are configured.
340
+ const parentRelationTo = parentCollections.length === 1 ? parentCollections[0] : parentCollections.length > 1 ? parentCollections : relationTo;
341
+ const dynamicSourceOptions = dynamicSources.map((source)=>({
342
+ label: source.label,
343
+ value: source.name
344
+ }));
345
+ const isMega = (_data, siblingData)=>siblingData?.itemType === 'mega';
346
+ return [
347
+ {
348
+ name: 'megaLayout',
349
+ type: 'select',
350
+ admin: {
351
+ condition: isMega,
352
+ description: 'How the mega menu is laid out. "Reveal" shows a flat list of rows that reveal their children in a second panel; "Columns" renders classic multi-column layout.'
353
+ },
354
+ defaultValue: 'reveal',
355
+ label: 'Mega Menu Layout',
356
+ options: [
357
+ {
358
+ label: 'Reveal (rows)',
359
+ value: 'reveal'
360
+ },
361
+ {
362
+ label: 'Columns',
363
+ value: 'columns'
406
364
  }
407
365
  ]
408
366
  },
409
367
  {
410
- name: 'attrs',
411
- type: 'textarea',
368
+ name: 'defaultFeatured',
369
+ type: 'group',
412
370
  admin: {
413
- condition: (_data, siblingData)=>{
414
- return siblingData?.advancedOptions !== 'default';
371
+ condition: isMega,
372
+ description: 'Fallback featured panel shown when an entry does not provide its own.'
373
+ },
374
+ fields: buildFeaturedFields(options),
375
+ label: 'Default Featured Panel'
376
+ },
377
+ {
378
+ name: 'megaEntries',
379
+ type: 'array',
380
+ admin: {
381
+ condition: isMega,
382
+ description: 'A flat list of entries. Each entry is itself a link and can reveal children in a secondary panel.',
383
+ ...options?.disabled ? {} : {
384
+ components: {
385
+ RowLabel: '@foundrykit/menu-plugin/client#MegaEntryRowLabel'
386
+ }
415
387
  }
416
- }
417
- }
418
- ];
419
- const isLink = (_data, siblingData)=>siblingData?.itemType !== 'dropdown' && siblingData?.itemType !== 'mega';
420
- const linkFields = basicFields.map((f)=>{
421
- if ('type' in f && f.type === 'row') {
422
- return {
423
- ...f,
424
- fields: f.fields.map((child)=>{
425
- if ('name' in child && child.name === 'label') return child;
426
- return {
427
- ...child,
428
- admin: {
429
- ...child.admin,
430
- condition: (_data, siblingData, extra)=>{
431
- const existingCondition = child.admin?.condition;
432
- const linkVisible = isLink(_data, siblingData);
433
- if (!linkVisible) return false;
434
- return existingCondition ? existingCondition(_data, siblingData, extra) : true;
388
+ },
389
+ // Depth-aware, short dbName so the generated Postgres table/relation names
390
+ // stay unique across nesting levels and well under the 63-char identifier
391
+ // limit even when consumers raise `maxDepth`.
392
+ dbName: `me${currentDepth}`,
393
+ fields: [
394
+ ...buildLeafLinkFields(options),
395
+ {
396
+ name: 'source',
397
+ type: 'select',
398
+ admin: {
399
+ description: 'Where this entry\'s children come from.'
400
+ },
401
+ defaultValue: 'manual',
402
+ options: [
403
+ {
404
+ label: 'Manual',
405
+ value: 'manual'
406
+ },
407
+ {
408
+ label: 'Dynamic (from a related collection)',
409
+ value: 'dynamic'
410
+ }
411
+ ],
412
+ required: true
413
+ },
414
+ {
415
+ name: 'children',
416
+ type: 'array',
417
+ admin: {
418
+ condition: (_data, siblingData)=>siblingData?.source !== 'dynamic',
419
+ description: 'Items revealed in the secondary panel when this entry is hovered or selected.',
420
+ ...options?.disabled ? {} : {
421
+ components: {
422
+ RowLabel: '@foundrykit/menu-plugin/client#SubMenuItemRowLabel'
435
423
  }
436
424
  }
437
- };
438
- })
439
- };
440
- }
441
- if ('admin' in f || 'name' in f) {
442
- // displaySurface is always visible regardless of itemType
443
- if ('name' in f && f.name === 'displaySurface') return f;
444
- return {
445
- ...f,
446
- admin: {
447
- ...f.admin,
448
- condition: (_data, siblingData, extra)=>{
449
- const existingCondition = f.admin?.condition;
450
- const linkVisible = isLink(_data, siblingData);
451
- if (!linkVisible) return false;
452
- return existingCondition ? existingCondition(_data, siblingData, extra) : true;
425
+ },
426
+ dbName: `mc${currentDepth}`,
427
+ fields: buildLeafLinkFields(options),
428
+ label: 'Children'
429
+ },
430
+ {
431
+ name: 'dynamicSource',
432
+ type: 'select',
433
+ admin: {
434
+ condition: (_data, siblingData)=>siblingData?.source === 'dynamic',
435
+ // Hidden and auto-set when exactly one source is configured.
436
+ hidden: singleSource
437
+ },
438
+ ...singleSource ? {
439
+ defaultValue: dynamicSources[0]?.name
440
+ } : {},
441
+ label: 'Dynamic Source',
442
+ options: dynamicSourceOptions
443
+ },
444
+ {
445
+ name: 'parent',
446
+ type: 'relationship',
447
+ admin: {
448
+ condition: (_data, siblingData)=>siblingData?.source === 'dynamic',
449
+ description: 'The parent document whose related children populate this entry.'
450
+ },
451
+ ...Array.isArray(parentRelationTo) ? {
452
+ hasMany: false,
453
+ relationTo: parentRelationTo
454
+ } : {
455
+ relationTo: parentRelationTo
453
456
  }
457
+ },
458
+ {
459
+ name: 'featured',
460
+ type: 'group',
461
+ admin: {
462
+ description: 'The featured panel shown alongside this entry\'s children.'
463
+ },
464
+ fields: [
465
+ {
466
+ name: 'mode',
467
+ type: 'select',
468
+ defaultValue: 'inherit',
469
+ options: [
470
+ {
471
+ label: 'Inherit (use default featured)',
472
+ value: 'inherit'
473
+ },
474
+ {
475
+ label: 'Manual',
476
+ value: 'manual'
477
+ },
478
+ {
479
+ label: 'Dynamic (from the related source)',
480
+ value: 'dynamic'
481
+ }
482
+ ]
483
+ },
484
+ ...buildFeaturedFields(options).map((field)=>{
485
+ if (!('name' in field)) {
486
+ return field;
487
+ }
488
+ const existingCondition = field.admin?.condition;
489
+ return {
490
+ ...field,
491
+ admin: {
492
+ ...field.admin,
493
+ condition: (_data, siblingData, extra)=>{
494
+ if (siblingData?.mode !== 'manual') {
495
+ return false;
496
+ }
497
+ return existingCondition ? existingCondition(_data, siblingData, extra) : true;
498
+ }
499
+ }
500
+ };
501
+ })
502
+ ],
503
+ label: 'Featured Panel'
454
504
  }
455
- };
505
+ ],
506
+ label: 'Mega Menu Entries'
456
507
  }
457
- return f;
458
- });
508
+ ];
509
+ };
510
+ export const createMenuItemFields = (options, currentDepth = 0)=>{
511
+ const maxDepth = options?.maxDepth ?? 1;
512
+ const leafLinkFields = buildLeafLinkFields(options);
459
513
  const canNest = currentDepth < maxDepth;
460
- const fields = canNest ? [
514
+ if (!canNest) {
515
+ return leafLinkFields;
516
+ }
517
+ return [
461
518
  {
462
519
  name: 'itemType',
463
520
  type: 'select',
@@ -478,11 +535,10 @@ export const createMenuItemFields = (options, currentDepth = 0)=>{
478
535
  ],
479
536
  required: true
480
537
  },
481
- ...linkFields,
538
+ ...leafLinkFields,
482
539
  {
483
540
  name: currentDepth === 0 ? 'children' : `children_${currentDepth + 1}`,
484
541
  type: 'array',
485
- label: 'Children',
486
542
  admin: {
487
543
  ...options?.disabled ? {} : {
488
544
  components: {
@@ -493,11 +549,11 @@ export const createMenuItemFields = (options, currentDepth = 0)=>{
493
549
  return siblingData?.itemType === 'dropdown';
494
550
  }
495
551
  },
496
- fields: createMenuItemFields(options, currentDepth + 1)
552
+ fields: createMenuItemFields(options, currentDepth + 1),
553
+ label: 'Children'
497
554
  },
498
- buildMegaColumnsField(options)
499
- ] : basicFields;
500
- return fields;
555
+ ...buildMegaConfigFields(options, currentDepth)
556
+ ];
501
557
  };
502
558
  export const menuItemFields = createMenuItemFields();
503
559
  export default menuItemFields;