Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modal: add headerActions prop to render buttons in the header #53328

Merged
merged 7 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- `Theme`: Expose via private APIs ([#53262](https://github.com/WordPress/gutenberg/pull/53262)).
- `ProgressBar`: Use the theme system accent for indicator color ([#53347](https://github.com/WordPress/gutenberg/pull/53347)).
- `ProgressBar`: Use gray 300 for track color ([#53349](https://github.com/WordPress/gutenberg/pull/53349)).
- `Modal`: add `headerActions` prop to render buttons in the header. ([#53328](https://github.com/WordPress/gutenberg/pull/53328)).

### Bug Fix

Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/modal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ If this property is true, it will focus the first tabbable element rendered in t
- Required: No
- Default: `true`

#### headerActions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this name is good too 👍 We should definitely have "header" in the name. For a moment I thought maybe "extras" instead of "actions" would be more accurately generic. But on the other hand I think it's good to restrict/suggest that it's intended for button-ish actions, just for the sake of UI design consistency.


An optional React node intended to contain additional actions or other elements related to the modal, for example, buttons. Content is rendered in the top right corner of the modal and to the left of the close button, if visible.

- Required: No
- Default: `null`

#### `isDismissible`: `boolean`

If this property is set to false, the modal will not display a close icon and cannot be dismissed.
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function UnforwardedModal(
contentLabel,
onKeyDown,
isFullScreen = false,
headerActions = null,
__experimentalHideHeader = false,
} = props;

Expand Down Expand Up @@ -257,6 +258,7 @@ function UnforwardedModal(
</h1>
) }
</div>
{ headerActions }
{ isDismissible && (
<Button
onClick={ onRequestClose }
Expand Down
66 changes: 66 additions & 0 deletions packages/components/src/modal/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import type { ComponentStory, ComponentMeta } from '@storybook/react';
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { help, starEmpty, starFilled } from '@wordpress/icons';

/**
* Internal dependencies
*/
import Button from '../../button';
import Tooltip from '../../tooltip';
import InputControl from '../../input-control';
import Modal from '../';
import type { ModalProps } from '../types';
Expand Down Expand Up @@ -87,6 +89,56 @@ const Template: ComponentStory< typeof Modal > = ( {
);
};

const TemplateWithHeaderActions: ComponentStory< typeof Modal > = ( {
onRequestClose,
...args
} ) => {
const [ isLiked, setIsLiked ] = useState( false );
const [ isOpen, setOpen ] = useState( false );
const openModal = () => setOpen( true );
const closeModal: ModalProps[ 'onRequestClose' ] = ( event ) => {
setOpen( false );
onRequestClose( event );
};
const headerActions = (
<>
<Button
icon={ isLiked ? starFilled : starEmpty }
label="Like"
onClick={ () => setIsLiked( ! isLiked ) }
/>
<Tooltip text="We are here to help!">
<Button icon={ help } label="Help" />
</Tooltip>
</>
);

return (
<>
<Button variant="secondary" onClick={ openModal }>
Open Modal with Header Actions
</Button>
{ isOpen && (
<Modal
style={ { maxWidth: '600px' } }
isDismissible={ false }
onRequestClose={ closeModal }
headerActions={ headerActions }
{ ...args }
>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et magna
aliqua.
</p>
<Button variant="secondary" onClick={ closeModal }>
Close Modal
</Button>
</Modal>
) }
</>
);
};
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
export const Default: ComponentStory< typeof Modal > = Template.bind( {} );
Default.args = {
title: 'Title',
Expand All @@ -98,3 +150,17 @@ Default.parameters = {
},
},
};

export const WithHeaderActions: ComponentStory< typeof Modal > =
TemplateWithHeaderActions.bind( {} );
WithHeaderActions.args = {
...Default.args,
};
WithHeaderActions.argTypes = {
isDismissible: {
control: { type: 'boolean' },
},
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
};
WithHeaderActions.parameters = {
...Default.parameters,
};
14 changes: 14 additions & 0 deletions packages/components/src/modal/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ describe( 'Modal', () => {
await user.click( modalFrame.parentElement! );
expect( opener ).toHaveFocus();
} );

it( 'should render `headerActions` React nodes', async () => {
render(
<Modal
headerActions={ <button>A sweet button</button> }
onRequestClose={ noop }
>
<p>Modal content</p>
</Modal>
);
expect(
screen.getByText( 'A sweet button', { selector: 'button' } )
).toBeInTheDocument();
} );
} );
8 changes: 8 additions & 0 deletions packages/components/src/modal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ export type ModalProps = {
* @default true
*/
focusOnMount?: Parameters< typeof useFocusOnMount >[ 0 ];
/**
* Elements that are injected into the modal header to the left of the close button (if rendered).
* Hidden if `__experimentalHideHeader` is `true`.
*
* @default null
*/
headerActions?: ReactNode;

/**
* If this property is added, an icon will be added before the title.
*/
Expand Down