From 335053aa6c8248a46af3564eaf979dfc3c17501c Mon Sep 17 00:00:00 2001 From: Marcelo Serpa <81248+fullofcaffeine@users.noreply.github.com> Date: Fri, 20 Aug 2021 17:59:08 -0500 Subject: [PATCH] Improve UX to more closely resembles a native confirm --- packages/components/src/confirm/component.tsx | 26 ++++++++++++++++--- packages/components/src/confirm/hook.ts | 6 ++--- packages/components/src/confirm/styles.ts | 19 +++++++++----- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/packages/components/src/confirm/component.tsx b/packages/components/src/confirm/component.tsx index 5f58b88ea5f02..8d1bf5eac15e9 100644 --- a/packages/components/src/confirm/component.tsx +++ b/packages/components/src/confirm/component.tsx @@ -4,7 +4,7 @@ * External dependencies */ // eslint-disable-next-line no-restricted-imports -import type { Ref } from 'react'; +import { Ref, useEffect } from 'react'; import { confirmable } from 'react-confirm'; /** @@ -16,6 +16,8 @@ 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'; +import { ESCAPE } from '@wordpress/keycodes'; // @todo deal with overlay click event, close dialog // @todo add type declarations for the react-confirm functions @@ -26,21 +28,35 @@ function Confirm( const { role, wrapperClassName, + overlayClassName, show, proceed, confirmation, ...otherProps } = useConfirm( props ); + function handleEscapePress( event: KeyboardEvent< HTMLDivElement > ) { + // `keyCode` is depreacted, so let's use `key` + if ( event.key === 'Escape' ) { + proceed( false ); + } + } + + useEffect( () => { + document.addEventListener( 'keydown', handleEscapePress ); + return () => + document.removeEventListener( 'keydown', handleEscapePress ); + } ); + return (