Skip to content

feat(array): hide insert above/below when max validation is reached#12061

Open
wotan-allfather wants to merge 5 commits intosanity-io:mainfrom
wotan-allfather:feat/hide-array-insert-when-max-reached-4317
Open

feat(array): hide insert above/below when max validation is reached#12061
wotan-allfather wants to merge 5 commits intosanity-io:mainfrom
wotan-allfather:feat/hide-array-insert-when-max-reached-4317

Conversation

@wotan-allfather
Copy link

Hide "Add above" and "Add below" context menu items in array items when the array has reached its max validation constraint.

This builds on the existing disableActions feature by dynamically checking if the array length has reached the max constraint defined in the schema validation rules.

Implementation:

  • Add ArrayValidationContext to provide max-reached state to item components
  • ListArrayInput and GridArrayInput wrap content with ArrayValidationProvider
  • PreviewItem and GridItem consume the context to hide insert menu items

Closes #4317

Description

What to review

Testing

Notes for release

@wotan-allfather wotan-allfather requested a review from a team as a code owner February 3, 2026 04:23
@wotan-allfather wotan-allfather requested review from jordanl17 and removed request for a team February 3, 2026 04:23
@vercel
Copy link

vercel bot commented Feb 3, 2026

@wotan-allfather is attempting to deploy a commit to the Sanity Sandbox Team on Vercel.

A member of the Team first needs to authorize it.

@jordanl17
Copy link
Member

Hi @wotan-allfather thanks a lot for this contribution! I'm taking a look over it and wondered if you felt hiding these field actions was more appropriate over disabling them (but still showing the options).

Our typical design pattern is that we would likely in this instance wish to continue to present these field actions their their disabling is temporary and can be easily resolved (by reducing the number of elements in the array).

Would be interested to hear your thoughts.

@wotan-allfather
Copy link
Author

Thanks @jordanl17! Great point.

I implemented hiding because the original issue title explicitly says "Remove" the actions, but I agree that disabling (with visual indication) would be a better UX pattern here. It follows the principle of progressive disclosure and helps users understand the constraint without the options disappearing unexpectedly.

I'm happy to update the implementation to disable the items instead of hiding them. Would you prefer:

  1. Disabled items with a tooltip explaining "Maximum items reached"
  2. Disabled items without tooltip (relying on schema-level messaging)

Let me know your preference and I'll update the PR accordingly!

@jordanl17
Copy link
Member

Thanks @jordanl17! Great point.

I implemented hiding because the original issue title explicitly says "Remove" the actions, but I agree that disabling (with visual indication) would be a better UX pattern here. It follows the principle of progressive disclosure and helps users understand the constraint without the options disappearing unexpectedly.

I'm happy to update the implementation to disable the items instead of hiding them. Would you prefer:

  1. Disabled items with a tooltip explaining "Maximum items reached"
  2. Disabled items without tooltip (relying on schema-level messaging)

Let me know your preference and I'll update the PR accordingly!

I think the first option sounds the best - with tooltip as this most closely follows other UX patterns we employ.

Thanks a lot @wotan-allfather - once these changes are in I'll get this reviewed and ensure we move things forward 😄

@wotan-allfather
Copy link
Author

Thanks @jordanl17! I've pushed the update with disabled items + "Maximum items reached" tooltip. Ready for your review when CI completes. 🙌

@vercel
Copy link

vercel bot commented Feb 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
page-building-studio Ready Ready Preview, Comment Feb 9, 2026 2:40pm
test-studio Ready Ready Preview, Comment Feb 9, 2026 2:40pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
studio-workshop Ignored Ignored Preview Feb 9, 2026 2:40pm
test-next-studio Ignored Ignored Feb 9, 2026 2:40pm

Request Review

Hide "Add above" and "Add below" context menu items in array items
when the array has reached its max validation constraint.

This builds on the existing disableActions feature by dynamically
checking if the array length has reached the max constraint defined
in the schema validation rules.

Implementation:
- Add ArrayValidationContext to provide max-reached state to item components
- ListArrayInput and GridArrayInput wrap content with ArrayValidationProvider
- PreviewItem and GridItem consume the context to hide insert menu items

Closes sanity-io#4317
Per maintainer feedback from @jordanl17, changed the behavior from hiding
the 'Add above' and 'Add below' menu items when max is reached to instead
showing them as disabled with a tooltip explaining 'Maximum items reached'.

This follows Sanity's UX pattern of keeping actions visible but disabled
when the constraint is temporary and can be resolved by the user.
Extracts the getValidationRule lookup into a separate useMemo
that only depends on schemaType (which is stable), preventing
unnecessary recalculations when itemCount changes.

Co-authored-by: jordanl17 <jordanl17@users.noreply.github.com>
@jordanl17
Copy link
Member

jordanl17 commented Feb 4, 2026

@wotan-allfather have given this a test.
I really like the architecture and the approach of utilising contexts exactly follows our existing patterns where we implement adjacent features 🚀

I think we need to extend this usage to cover our other array types that are supported. Currently PreviewItem and GridItem are for arrays of objects, but we also show the 'insert above/below' for arrays of primatives (ItemRow) and arrays of references (ReferenceItem).

I think it would be great to bring some consistency across the experience for all of these in supporting the disabling of the 'insert above/below'

Below is confirmation that I don't see the actions disabled of a field that is a string array
Screenshot 2026-02-04 at 18 40 16

@wotan-allfather
Copy link
Author

Thanks @jordanl17! Great catch - I missed those array types.

I'll extend the implementation to also cover:

  • ItemRow (primitive arrays like strings/numbers)
  • ReferenceItem (reference arrays)

I'll push the update shortly so we have consistent behavior across all array types! 🔧

Per maintainer feedback from @jordanl17, extend the disabled insert
actions to also cover ItemRow (primitive arrays like strings/numbers)
and ReferenceItem (reference arrays).

This ensures consistent UX across all array types when the max
validation constraint is reached.

- ItemRow now uses useArrayValidation hook to disable insert menu items
- ReferenceItem passes disabled/disabledReason to useInsertMenuMenuItems
- InsertMenuGroup updated to accept disabled/disabledReason props
@jordanl17
Copy link
Member

Hi @wotan-allfather - thanks again for being so proactive with these changes. We are just internally discussing this issue/improvement and I'll get back to you shortly, likely start of next week. Have a lovely weekend!

@jordanl17
Copy link
Member

Hi @wotan-allfather . There are a few extra scope ideas that the team have had off the back of the work you've done on this improvement. Primarily we think that the 'Add Item' CTA should now also disable when the limit of a array is reached too - so that we keep the entire validation experience consistent.

Hopefully you are ok with me creating this branch here where I'm going to contribute a few additional commits to extend this feature just a little further, and then will move it through the PR process to get this merged and released. Alternatively if you would prefer to make these changes yourself, please let me know shortly 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove "add item above" and "add item below" when max validity rule is reached on array

2 participants