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 (
- + event.preventDefault() }> { confirmation } @@ -56,6 +72,10 @@ function Confirm( +
proceed( false ) } + >
); } diff --git a/packages/components/src/confirm/hook.ts b/packages/components/src/confirm/hook.ts index 5ed593f402091..69a2eb9ddfb44 100644 --- a/packages/components/src/confirm/hook.ts +++ b/packages/components/src/confirm/hook.ts @@ -18,15 +18,15 @@ export function useConfirm( const cx = useCx(); const classes = cx( className ); - const wrapperClassName = cx( styles.overlayWrapper ); - const dialogWrapperClassName = cx( styles.dialogWrapper ); + const wrapperClassName = cx( styles.wrapper ); + const overlayClassName = cx( styles.overlay ); console.log( otherProps ); return { className: classes, wrapperClassName, - dialogWrapperClassName, + overlayClassName, ...otherProps, }; } diff --git a/packages/components/src/confirm/styles.ts b/packages/components/src/confirm/styles.ts index ba5fbfe8a515b..b7a389abefa65 100644 --- a/packages/components/src/confirm/styles.ts +++ b/packages/components/src/confirm/styles.ts @@ -3,22 +3,27 @@ */ import { css } from '@emotion/react'; -export const overlayWrapper = css` +export const wrapper = css` position: fixed; top: 0; left: 0; right: 0; bottom: 0; - z-index: 99; - background: rgba( 0, 0, 0, 0.5 ); - display: -webkit-flex; - display: -moz-flex; - display: -ms-flex; - display: -o-flex; + 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``;