-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support insert menu options in array item context menus (#…
…6921) * chore(core): introduce useInsertMenuMenuItems * chore(core): allow ContextMenuButton.selected * feat(core): support insert menu options in preview item context menu * feat(core): support insert menu options in grid item context menu * feat(core): support insert menu options in reference item context menu * chore(core): add array item context menu insert menu test cases
- Loading branch information
1 parent
6b9f910
commit 784cfd3
Showing
6 changed files
with
432 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/sanity/src/core/form/inputs/arrays/ArrayOfObjectsInput/InsertMenuMenuItems.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import {InsertAboveIcon, InsertBelowIcon} from '@sanity/icons' | ||
import {type InsertMenuOptions} from '@sanity/insert-menu' | ||
import {type SchemaType} from '@sanity/types' | ||
import {useCallback, useMemo} from 'react' | ||
import {useTranslation} from 'sanity' | ||
|
||
import {MenuItem} from '../../../../../ui-components' | ||
import {useInsertMenuPopover} from './InsertMenuPopover' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
type InsertMenuItemsProps = { | ||
insertMenuOptions?: InsertMenuOptions | ||
onInsert: (pos: 'before' | 'after', type: SchemaType) => void | ||
referenceElement: HTMLElement | null | ||
schemaTypes?: SchemaType[] | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function useInsertMenuMenuItems(props: InsertMenuItemsProps) { | ||
const {t} = useTranslation() | ||
const {onInsert, schemaTypes: types} = props | ||
const insertBefore = useInsertMenuPopover({ | ||
insertMenuProps: { | ||
...props.insertMenuOptions, | ||
schemaTypes: props.schemaTypes ?? [], | ||
onSelect: (insertType) => { | ||
props.onInsert('before', insertType) | ||
}, | ||
}, | ||
popoverProps: { | ||
referenceElement: props.referenceElement, | ||
placement: 'top-end', | ||
fallbackPlacements: ['bottom-end'], | ||
}, | ||
}) | ||
const insertAfter = useInsertMenuPopover({ | ||
insertMenuProps: { | ||
...props.insertMenuOptions, | ||
schemaTypes: props.schemaTypes ?? [], | ||
onSelect: (insertType) => { | ||
props.onInsert('after', insertType) | ||
}, | ||
}, | ||
popoverProps: { | ||
referenceElement: props.referenceElement, | ||
placement: 'bottom-end', | ||
fallbackPlacements: ['top-end'], | ||
}, | ||
}) | ||
const handleToggleInsertBefore = useCallback(() => { | ||
if (!types) { | ||
return | ||
} | ||
|
||
if (types.length === 1) { | ||
onInsert('before', types[0]) | ||
} else { | ||
insertBefore.send({type: 'toggle'}) | ||
} | ||
}, [insertBefore, onInsert, types]) | ||
const handleToggleInsertAfter = useCallback(() => { | ||
if (!types) { | ||
return | ||
} | ||
|
||
if (types.length === 1) { | ||
onInsert('after', types[0]) | ||
} else { | ||
insertAfter.send({type: 'toggle'}) | ||
} | ||
}, [insertAfter, onInsert, types]) | ||
|
||
const insertBeforeMenuItem = useMemo( | ||
() => | ||
types ? ( | ||
<MenuItem | ||
text={ | ||
types.length === 1 | ||
? t('inputs.array.action.add-before') | ||
: `${t('inputs.array.action.add-before')}...` | ||
} | ||
icon={InsertAboveIcon} | ||
onClick={handleToggleInsertBefore} | ||
/> | ||
) : null, | ||
[handleToggleInsertBefore, t, types], | ||
) | ||
const insertAfterMenuItem = useMemo( | ||
() => | ||
types ? ( | ||
<MenuItem | ||
text={ | ||
types.length === 1 | ||
? t('inputs.array.action.add-after') | ||
: `${t('inputs.array.action.add-after')}...` | ||
} | ||
icon={InsertBelowIcon} | ||
onClick={handleToggleInsertAfter} | ||
/> | ||
) : null, | ||
[handleToggleInsertAfter, t, types], | ||
) | ||
|
||
return { | ||
insertBefore: { | ||
...insertBefore, | ||
menuItem: insertBeforeMenuItem, | ||
}, | ||
insertAfter: { | ||
...insertAfter, | ||
menuItem: insertAfterMenuItem, | ||
}, | ||
} | ||
} |
Oops, something went wrong.