Skip to content

Commit 5a95a45

Browse files
yogeshbhutkaryogeshbhutkarciampot-hamanohimanshupathak95
authored andcommitted
ActionModal: Add support for customisable focusOnMount (WordPress#69609)
* feat: support customisable `focusOnMount` * fix: ensure `focusOnMount` defaults to true in ActionModal * feat: add `modalFocusOnMount` property to all eligible actions * feat: add `modalFocusOnMount` property to ActionModal fixture Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org> Co-authored-by: ciampo <mciampini@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: himanshupathak95 <abcd95@git.wordpress.org> Co-authored-by: afercia <afercia@git.wordpress.org> Co-authored-by: oandregal <oandregal@git.wordpress.org>
1 parent f3d3b76 commit 5a95a45

File tree

15 files changed

+42
-1
lines changed

15 files changed

+42
-1
lines changed

packages/dataviews/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,23 @@ Example:
722722
}
723723
```
724724
725+
### `modalFocusOnMount`
726+
727+
Specifies the focus on mount property of the modal.
728+
729+
- Type: `boolean` | `string`
730+
- Optional
731+
- Default: `true`
732+
- One of: `true` | `false` | `'firstElement'` | `'firstContentElement'`
733+
734+
Example:
735+
736+
```js
737+
{
738+
modalFocusOnMount: 'firstContentElement';
739+
}
740+
```
741+
725742
## Fields API
726743
727744
### `id`

packages/dataviews/src/components/dataviews-item-actions/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function ActionModal< Item >( {
110110
title={ action.modalHeader || label }
111111
__experimentalHideHeader={ !! action.hideModalHeader }
112112
onRequestClose={ closeModal }
113-
focusOnMount="firstContentElement"
113+
focusOnMount={ action.modalFocusOnMount ?? true }
114114
size={ action.modalSize || 'medium' }
115115
overlayClassName={ `dataviews-action-modal dataviews-action-modal__${ kebabCase(
116116
action.id

packages/dataviews/src/components/dataviews/stories/fixtures.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ export const actions: Action< SpaceObject >[] = [
582582
isPrimary: true,
583583
icon: trash,
584584
hideModalHeader: true,
585+
modalFocusOnMount: 'firstContentElement',
585586
RenderModal: ( { items, closeModal } ) => {
586587
return (
587588
<VStack spacing="5">

packages/dataviews/src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import type { ReactElement, ComponentType } from 'react';
88
*/
99
import type { SetSelection } from './private-types';
1010

11+
/**
12+
* WordPress dependencies
13+
*/
14+
import type { useFocusOnMount } from '@wordpress/compose';
15+
1116
export type SortDirection = 'asc' | 'desc';
1217

1318
/**
@@ -470,6 +475,13 @@ export interface ActionModal< Item > extends ActionBase< Item > {
470475
* @default 'medium'
471476
*/
472477
modalSize?: 'small' | 'medium' | 'large' | 'fill';
478+
479+
/**
480+
* The focus on mount property of the modal.
481+
*/
482+
modalFocusOnMount?:
483+
| Parameters< typeof useFocusOnMount >[ 0 ]
484+
| 'firstContentElement';
473485
}
474486

475487
export interface ActionButton< Item > extends ActionBase< Item > {

packages/editor/src/components/post-actions/set-as-homepage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export const useSetAsHomepageAction = () => {
160160

161161
return true;
162162
},
163+
modalFocusOnMount: 'firstContentElement',
163164
RenderModal: SetAsHomepageModal,
164165
} ),
165166
[ pageForPosts, pageOnFront ]

packages/editor/src/components/post-actions/set-as-posts-page.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export const useSetAsPostsPageAction = () => {
157157

158158
return true;
159159
},
160+
modalFocusOnMount: 'firstContentElement',
160161
RenderModal: SetAsPostsPageModal,
161162
} ),
162163
[ pageForPosts, pageOnFront ]

packages/fields/src/actions/delete-post.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const deletePostAction: Action< Template | TemplatePart | Pattern > = {
4747
},
4848
supportsBulk: true,
4949
hideModalHeader: true,
50+
modalFocusOnMount: 'firstContentElement',
5051
RenderModal: ( { items, closeModal, onActionPerformed } ) => {
5152
const [ isBusy, setIsBusy ] = useState( false );
5253
const isResetting = items.every(

packages/fields/src/actions/duplicate-pattern.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const duplicatePattern: Action< Pattern > = {
2121
label: _x( 'Duplicate', 'action label' ),
2222
isEligible: ( item ) => item.type !== 'wp_template_part',
2323
modalHeader: _x( 'Duplicate pattern', 'action label' ),
24+
modalFocusOnMount: 'firstContentElement',
2425
RenderModal: ( { items, closeModal } ) => {
2526
const [ item ] = items;
2627
const duplicatedProps = useDuplicatePatternProps( {

packages/fields/src/actions/duplicate-post.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const duplicatePost: Action< BasePost > = {
3333
isEligible( { status } ) {
3434
return status !== 'trash';
3535
},
36+
modalFocusOnMount: 'firstContentElement',
3637
RenderModal: ( { items, closeModal, onActionPerformed } ) => {
3738
const [ item, setItem ] = useState< BasePost >( {
3839
...items[ 0 ],

packages/fields/src/actions/duplicate-template-part.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const duplicateTemplatePart: Action< TemplatePart > = {
2424
label: _x( 'Duplicate', 'action label' ),
2525
isEligible: ( item ) => item.type === 'wp_template_part',
2626
modalHeader: _x( 'Duplicate template part', 'action label' ),
27+
modalFocusOnMount: 'firstContentElement',
2728
RenderModal: ( { items, closeModal } ) => {
2829
const [ item ] = items;
2930
const blocks = useMemo( () => {

0 commit comments

Comments
 (0)