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

Components: Add experimental ConfirmDialog #34153

Merged
merged 92 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
72cd96b
Proof of Concept
fullofcaffeine Aug 19, 2021
335053a
Improve UX to more closely resembles a native confirm
fullofcaffeine Aug 20, 2021
f4a3396
Remove unused import and outdated comment
fullofcaffeine Aug 20, 2021
1b2f104
Improve the story
fullofcaffeine Aug 20, 2021
d5e8d73
Spelling fix
fullofcaffeine Aug 20, 2021
f7250b6
Remove debug code
fullofcaffeine Aug 20, 2021
6062648
Fix invalid props warning
fullofcaffeine Aug 21, 2021
a01225f
Empty dependency array to avoid listener from being re-added at each …
fullofcaffeine Aug 24, 2021
a4d2453
Remove unused style
fullofcaffeine Aug 24, 2021
18e0231
wip
fullofcaffeine Aug 25, 2021
2391e35
Address code review suggestions partially, and refactor to use the ex…
fullofcaffeine Aug 25, 2021
a878802
Improve component by allowing it to be used without the `confirm` hel…
fullofcaffeine Aug 25, 2021
b09651b
Update README
fullofcaffeine Aug 26, 2021
c221e9d
Update confirm call in post-visibility
fullofcaffeine Aug 26, 2021
cac2732
Remove role prop as it is not explicitely used at the moment
fullofcaffeine Aug 26, 2021
0942707
Add basic tests
fullofcaffeine Aug 26, 2021
7d629b8
Test that the Confirm closes when clicking the overlay
fullofcaffeine Aug 27, 2021
12f0b18
Test that the Confirm closes when pressing Escape
fullofcaffeine Aug 27, 2021
f99c357
Merge branch 'trunk' into replace/confirm-et-al
fullofcaffeine Sep 9, 2021
f746119
Fix types and forwardRef, and adapt to the new functional Modal compo…
fullofcaffeine Sep 9, 2021
5fd979f
Merge branch 'trunk' into replace/confirm-et-al
fullofcaffeine Sep 9, 2021
13bf86a
Fix tests
fullofcaffeine Sep 9, 2021
d68508f
Simple naive inline declarative usage without a Context
fullofcaffeine Sep 9, 2021
56ea610
Fix z-index to show it above all elements, and forwardRef to Modal's …
fullofcaffeine Sep 9, 2021
6245385
Improve callback handler name
fullofcaffeine Sep 10, 2021
814fc6f
Make `onCancel` optional
fullofcaffeine Sep 10, 2021
2b47a46
Add styles.ts
fullofcaffeine Sep 10, 2021
bf9c062
Add more tests
fullofcaffeine Sep 11, 2021
37befa2
Handle confirm on enter and add corresponding test
fullofcaffeine Sep 11, 2021
31b88e2
Rename to `ConfirmDialog`
fullofcaffeine Sep 13, 2021
2cd86e9
Redo stories, improve types and `selfClose` handling
fullofcaffeine Sep 14, 2021
21600b0
Fix tests after renaming to `ConfirmDialog`
fullofcaffeine Sep 14, 2021
b21c175
Update the post-visibility example after renaming to `ConfirmDialog`
fullofcaffeine Sep 14, 2021
922c103
Forward all other props to the underlying `Modal`
fullofcaffeine Sep 14, 2021
9c931c6
Refactor tests to test controlled and uncontrolled scenarios
fullofcaffeine Sep 15, 2021
5494585
Mark it as experimental, linter autofixes and snapshot updates
fullofcaffeine Sep 15, 2021
2c43770
Add a proper README
fullofcaffeine Sep 15, 2021
fdab69f
Fix typo
fullofcaffeine Sep 15, 2021
835c2e6
Fix grammar and add section about multiple instances to the README
fullofcaffeine Sep 15, 2021
61f7c1a
Fix stories
fullofcaffeine Sep 15, 2021
c1cf65a
Add message text knob to stories
fullofcaffeine Sep 16, 2021
20e2d23
Reset components/package.json
fullofcaffeine Sep 16, 2021
33e97e1
Merge branch 'trunk' into replace/confirm-et-al
fullofcaffeine Sep 16, 2021
566fe18
Misc changes after code review
fullofcaffeine Sep 24, 2021
d1be655
Abstract polymorphic event
fullofcaffeine Sep 24, 2021
81d9e76
Simplify tests to not use snapshots and be more explicit
fullofcaffeine Sep 24, 2021
8665ad6
Update README.md to reflect the new polymorphic event type
fullofcaffeine Sep 24, 2021
29c8c3a
Make the `Cancel` button a `tertiary` and DRY and improve types
fullofcaffeine Oct 7, 2021
f59553d
Make the title optional and adjust some styles
fullofcaffeine Oct 7, 2021
55ac46a
Fix typo
fullofcaffeine Oct 8, 2021
c96f575
Update packages/components/src/confirm-dialog/README.md
fullofcaffeine Oct 13, 2021
3abfc81
Update packages/components/src/confirm-dialog/README.md
fullofcaffeine Oct 13, 2021
605c2ae
Update packages/components/src/confirm-dialog/README.md
fullofcaffeine Oct 13, 2021
6a60f92
Remove unused imported `MouseEvent`
fullofcaffeine Oct 13, 2021
995f45f
Type `handleEvent`'s `callback` argument as optional
fullofcaffeine Oct 13, 2021
40cb594
Update packages/components/src/confirm-dialog/README.md
fullofcaffeine Oct 13, 2021
ceabb07
Update packages/components/src/confirm-dialog/README.md
fullofcaffeine Oct 13, 2021
aa7025d
Remove portion about the singleton wrapper component as it will not b…
fullofcaffeine Oct 13, 2021
d130ed1
Improve component types
fullofcaffeine Oct 13, 2021
054caae
Use `props.children` to pass the dialog message contents
fullofcaffeine Oct 13, 2021
85e9510
Add DOM structure tests
fullofcaffeine Oct 13, 2021
c20f85d
Convert all findBy* to getBy*
fullofcaffeine Oct 13, 2021
e9ee813
Update README to reflect the new API
fullofcaffeine Oct 13, 2021
316afc5
Improve title and xCloseButton tests
fullofcaffeine Oct 13, 2021
5b2b026
Add better and more concise description for the `title` prop in the R…
fullofcaffeine Oct 13, 2021
4e5abed
Try stacked margin
fullofcaffeine Oct 14, 2021
9bf6f88
Update packages/components/tsconfig.json
fullofcaffeine Oct 14, 2021
64c2816
Update packages/components/src/modal/index.js
fullofcaffeine Oct 14, 2021
414ab15
Destructure props in the function body in `Modal` to prevent TS error…
fullofcaffeine Oct 14, 2021
49eaa0e
Merge branch 'trunk' into replace/confirm-et-al
fullofcaffeine Oct 21, 2021
9f62ad3
Use VStack for a stacked margin approach and remove ability to set a …
fullofcaffeine Oct 21, 2021
af340b2
Update snapshots for the `preferences-modal` unit tests
fullofcaffeine Oct 22, 2021
710932d
Update snapshots for the `keyboard-shortcut-help-modal` unit tests
fullofcaffeine Oct 23, 2021
5e24ff0
Merge branch 'trunk' into replace/confirm-et-al
fullofcaffeine Oct 25, 2021
7d5860c
Merge branch 'trunk' into replace/confirm-et-al
fullofcaffeine Nov 20, 2021
febda4a
Revert post-visibility sample changes
fullofcaffeine Nov 20, 2021
8bed797
Keep modal's default behavior of showing the title div and remove an …
fullofcaffeine Nov 20, 2021
16ce455
Fix wrong message string in one of the stories
fullofcaffeine Nov 20, 2021
2ddbabc
Remove unnecessary explanation about unset props
ciampo Nov 24, 2021
bd6eb33
Rename selfClose to shouldSelfClose
ciampo Nov 24, 2021
94f8b47
Memoize callbacks
ciampo Nov 24, 2021
4d35f84
Extract `confirm` and `cancel` labels
ciampo Nov 24, 2021
74c5293
Improve stories: use `Heading`, better status sentence
ciampo Nov 24, 2021
f69a973
Temporarily enable knobs
ciampo Nov 24, 2021
541490c
Fix typo in test name
ciampo Nov 24, 2021
8eeb0c7
More descriptive storybook sentende, take 2
ciampo Nov 24, 2021
3c3f49a
Sort export alphabetically
ciampo Nov 24, 2021
173f2d4
Undo changes to `Modal` component (to be carried out in a separate PR)
ciampo Nov 24, 2021
4a81ba6
Undo changes to `Modal`-related snapshots (to be carried out in a sep…
ciampo Nov 24, 2021
e2ce3ef
Merge branch 'trunk' into replace/confirm-et-al
ciampo Nov 25, 2021
c2a9adb
Refactor `showTitle` prop on the Modal to `__experimentalHideHeader`
ciampo Nov 25, 2021
a320d85
CHANGELOG
ciampo Nov 25, 2021
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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,12 @@
"markdown_source": "../packages/components/src/combobox-control/README.md",
"parent": "components"
},
{
"title": "Confirm",
"slug": "confirm",
"markdown_source": "../packages/components/src/confirm/README.md",
"parent": "components"
},
{
"title": "CustomSelectControl",
"slug": "custom-select-control",
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"moment": "^2.22.1",
"re-resizable": "^6.4.0",
"react-colorful": "^5.3.0",
"react-confirm": "0.1.24",
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
"react-dates": "^17.1.1",
"react-resize-aware": "^3.1.0",
"react-use-gesture": "^9.0.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/confirm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `Confirm`
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved

TBD
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
80 changes: 80 additions & 0 deletions packages/components/src/confirm/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// @ts-nocheck (for now)
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved

/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import { Ref, useEffect } from 'react';
import { confirmable } from 'react-confirm';

/**
* Internal dependencies
*/
import Button from '../button';
import type { OwnProps } from './types';
import { Card, CardHeader, CardFooter } from '../card';
import { Heading } from '../heading';
import { contextConnect, PolymorphicComponentProps } from '../ui/context';
import { useConfirm } from './hook';
import type { KeyboardEvent } from 'react';

// @todo add type declarations for the react-confirm functions
function Confirm(
props: PolymorphicComponentProps< OwnProps, 'div', false >,
forwardedRef: Ref< any >
) {
const {
role,
wrapperClassName,
overlayClassName,
show,
proceed,
confirmation,
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
...otherProps
} = useConfirm( props );

function handleEscapePress( event: KeyboardEvent< HTMLDivElement > ) {
// `keyCode` is deprecated, so let's use `key`
if ( event.key === 'Escape' ) {
proceed( false );
}
}

useEffect( () => {
document.addEventListener( 'keydown', handleEscapePress );
return () =>
document.removeEventListener( 'keydown', handleEscapePress );
} );
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved

return (
<div
role={ role }
ref={ forwardedRef }
className={ wrapperClassName }
style={ { visibility: show ? 'visible' : 'hidden' } }
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
>
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
<Card onMouseDown={ ( event ) => event.preventDefault() }>
<CardHeader>
<Heading level="4">{ confirmation }</Heading>
</CardHeader>
<CardFooter justify="center">
<Button
variant="secondary"
onClick={ () => proceed( false ) }
>
Cancel
</Button>
<Button variant="primary" onClick={ () => proceed( true ) }>
OK
</Button>
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
</CardFooter>
</Card>
<div
className={ overlayClassName }
onClick={ () => proceed( false ) }
></div>
</div>
);
}

export default confirmable( contextConnect( Confirm, 'Confirm' ) );
30 changes: 30 additions & 0 deletions packages/components/src/confirm/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Internal dependencies
*/
import { useContextSystem, PolymorphicComponentProps } from '../ui/context';

/**
* Internal dependencies
*/
import * as styles from './styles';
import { useCx } from '../utils/hooks/use-cx';
import type { OwnProps } from './types';

export function useConfirm(
props: PolymorphicComponentProps< OwnProps, 'div' >
) {
const { className, ...otherProps } = useContextSystem( props, 'Confirm' );

const cx = useCx();

const classes = cx( className );
const wrapperClassName = cx( styles.wrapper );
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
const overlayClassName = cx( styles.overlay );

return {
className: classes,
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
wrapperClassName,
overlayClassName,
...otherProps,
};
}
15 changes: 15 additions & 0 deletions packages/components/src/confirm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ts-nocheck
ciampo marked this conversation as resolved.
Show resolved Hide resolved

/**
* External dependencies
*/
import { createConfirmation } from 'react-confirm';

/**
* Internal dependencies
*/
import Confirm from './component';

const confirm = createConfirmation( Confirm );

export { Confirm, confirm };
37 changes: 37 additions & 0 deletions packages/components/src/confirm/stories/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* External dependencies
*/
import React, { useState } from 'react';

/**
* Internal dependencies
*/
import Button from '../../button';
import { Confirm, confirm } from '..';

export default {
component: Confirm,
title: 'Components (Experimental)/Confirm',
argTypes: { proceed: { action: 'proceed' } },
};

export const _default = () => {
const [ confirmVal, setConfirmVal ] = useState();

async function triggerConfirm() {
if ( await confirm( { confirmation: 'Are you sure?' } ) ) {
setConfirmVal( 'You are sure!' );
} else {
setConfirmVal( 'Ok, take more time to decide!' );
}
}

return (
<>
<Button variant="primary" onClick={ triggerConfirm }>
Trigger Confirm
</Button>
{ confirmVal && <h1>{ confirmVal }</h1> }
</>
);
};
29 changes: 29 additions & 0 deletions packages/components/src/confirm/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* External dependencies
*/
import { css } from '@emotion/react';

export const wrapper = css`
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 9999999;
display: flex;
justify-content: center;
-ms-align-items: center;
align-items: center;
`;

export const overlay = css`
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: -1;
background: rgba( 0, 0, 0, 0.5 );
`;

export const dialogWrapper = css``;
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/components/src/confirm/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TBD
10 changes: 10 additions & 0 deletions packages/components/src/confirm/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Internal dependencies
*/

// TBD
export interface OwnProps {
show: boolean;
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
proceed: Function;
confirmation: string;
ciampo marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,4 @@ export {
} from './higher-order/with-focus-return';
export { default as withNotices } from './higher-order/with-notices';
export { default as withSpokenMessages } from './higher-order/with-spoken-messages';
export { confirm } from './confirm'; // @todo make it experimental
ciampo marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"src/base-field/**/*",
"src/button/**/*",
"src/card/**/*",
"src/confirm/**/*",
fullofcaffeine marked this conversation as resolved.
Show resolved Hide resolved
"src/dashicon/**/*",
"src/disabled/**/*",
"src/divider/**/*",
Expand Down
12 changes: 7 additions & 5 deletions packages/editor/src/components/post-visibility/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { VisuallyHidden } from '@wordpress/components';
import { VisuallyHidden, confirm } from '@wordpress/components';
import { withInstanceId, compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';

Expand Down Expand Up @@ -34,12 +34,14 @@ export class PostVisibility extends Component {
this.setState( { hasPassword: false } );
}

setPrivate() {
async setPrivate() {
if (
// eslint-disable-next-line no-alert
! window.confirm(
__( 'Would you like to privately publish this post now?' )
)
! ( await confirm( {
confirmation: __(
'Would you like to privately publish this post now?'
),
} ) )
) {
return;
}
Expand Down