Close datagrid modal by esc key (#2507)
This commit is contained in:
parent
33cfd21a76
commit
5fdb51c0ee
3 changed files with 31 additions and 11 deletions
|
@ -50,12 +50,14 @@ const useAnimationStyles = (isOpen: boolean, duration: number) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
interface FullScreenContainerProps {
|
type FullScreenContainerProps = FC<
|
||||||
open?: boolean;
|
PropsWithChildren<{
|
||||||
className?: string;
|
open?: boolean;
|
||||||
}
|
className?: string;
|
||||||
|
}>
|
||||||
|
>;
|
||||||
|
|
||||||
const Portal = ({ className, children, open }) => {
|
const Portal: FullScreenContainerProps = ({ className, children, open }) => {
|
||||||
const { delayedState: delayedOpen, duration } = useDelayedState(open);
|
const { delayedState: delayedOpen, duration } = useDelayedState(open);
|
||||||
const styles = useAnimationStyles(open, duration);
|
const styles = useAnimationStyles(open, duration);
|
||||||
|
|
||||||
|
@ -67,13 +69,12 @@ const Portal = ({ className, children, open }) => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const FullScreenContainer: FC<PropsWithChildren<
|
export const FullScreenContainer: FullScreenContainerProps = ({
|
||||||
FullScreenContainerProps
|
children,
|
||||||
>> = ({ children, open, className }) => (
|
...rest
|
||||||
|
}) => (
|
||||||
<>
|
<>
|
||||||
<Portal className={className} open={open}>
|
<Portal {...rest}>{children}</Portal>
|
||||||
{children}
|
|
||||||
</Portal>
|
|
||||||
{children}
|
{children}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { usePreventHistoryBack } from "@saleor/hooks/usePreventHistoryBack";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import { useDelayedState } from "./useDelayedState";
|
import { useDelayedState } from "./useDelayedState";
|
||||||
|
import { usePressEscKey } from "./usePressEscKey";
|
||||||
|
|
||||||
export const useFullScreenMode = () => {
|
export const useFullScreenMode = () => {
|
||||||
const { enable, disable } = usePreventHistoryBack(document.body, {
|
const { enable, disable } = usePreventHistoryBack(document.body, {
|
||||||
|
@ -11,6 +12,10 @@ export const useFullScreenMode = () => {
|
||||||
const { delayedState: delayedOpen } = useDelayedState(!open);
|
const { delayedState: delayedOpen } = useDelayedState(!open);
|
||||||
const togglePreventHistory = open ? disable : enable;
|
const togglePreventHistory = open ? disable : enable;
|
||||||
|
|
||||||
|
usePressEscKey(() => {
|
||||||
|
setOpen(false);
|
||||||
|
});
|
||||||
|
|
||||||
const toggle = () => {
|
const toggle = () => {
|
||||||
setOpen(p => !p);
|
setOpen(p => !p);
|
||||||
togglePreventHistory();
|
togglePreventHistory();
|
||||||
|
|
14
src/components/Datagrid/usePressEscKey.tsx
Normal file
14
src/components/Datagrid/usePressEscKey.tsx
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
|
export const usePressEscKey = (callback?: () => void) => {
|
||||||
|
useEffect(() => {
|
||||||
|
const handler = (event: KeyboardEvent) => {
|
||||||
|
if (event.code === "Escape") {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener("keydown", handler);
|
||||||
|
return () => document.removeEventListener("keydown", handler);
|
||||||
|
}, [callback]);
|
||||||
|
};
|
Loading…
Reference in a new issue