@linzjs/windows 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LuiModalAsync/LuiModalAsync.scss +100 -0
- package/dist/LuiModalAsync/LuiModalAsync.tsx +75 -0
- package/dist/LuiModalAsync/LuiModalAsyncButtonGroup.tsx +55 -0
- package/dist/LuiModalAsync/LuiModalAsyncContent.tsx +9 -0
- package/dist/LuiModalAsync/LuiModalAsyncContext.tsx +37 -0
- package/dist/LuiModalAsync/LuiModalAsyncContextProvider.tsx +144 -0
- package/dist/LuiModalAsync/LuiModalAsyncHeader.tsx +62 -0
- package/dist/LuiModalAsync/LuiModalAsyncInstanceContext.ts +23 -0
- package/dist/LuiModalAsync/LuiModalAsyncMain.tsx +8 -0
- package/dist/LuiModalAsync/index.ts +10 -0
- package/dist/LuiModalAsync/useLuiModalPrefab.tsx +202 -0
- package/dist/LuiModalAsync/useShowAsyncModal.ts +24 -0
- package/package.json +1 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
@use "node_modules/@linzjs/lui/dist/scss/Core" as lui;
|
|
2
|
+
|
|
3
|
+
dialog.LuiModalAsync::backdrop {
|
|
4
|
+
background: rgb(0 0 0 / 10%);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
dialog.LuiModalAsync, div.LuiModalAsync {
|
|
8
|
+
@include lui.drop-shadow(sm);
|
|
9
|
+
border: none;
|
|
10
|
+
display: flex;
|
|
11
|
+
flex-direction: column;
|
|
12
|
+
border-radius: lui.$unit-xs;
|
|
13
|
+
padding: lui.$unit-md;
|
|
14
|
+
min-width: 400px;
|
|
15
|
+
max-width: 80vw;
|
|
16
|
+
max-height: 80vh;
|
|
17
|
+
gap: lui.$unit-md;
|
|
18
|
+
|
|
19
|
+
&.LuiModalPrefab-success {
|
|
20
|
+
border-left: lui.$unit-xs solid lui.$success;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
&.LuiModalPrefab-info {
|
|
24
|
+
border-left: lui.$unit-xs solid lui.$info;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
&.LuiModalPrefab-warning {
|
|
28
|
+
border-left: lui.$unit-xs solid lui.$warning;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
&.LuiModalPrefab-error {
|
|
32
|
+
border-left: lui.$unit-xs solid lui.$error;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
h4 {
|
|
36
|
+
margin-top: 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.LuiModalAsync-ButtonGroup {
|
|
40
|
+
display: flex;
|
|
41
|
+
|
|
42
|
+
button {
|
|
43
|
+
flex: 1;
|
|
44
|
+
border-color: transparent;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.LuiModalAsync-header {
|
|
49
|
+
display: flex;
|
|
50
|
+
gap: lui.$unit-xs;
|
|
51
|
+
|
|
52
|
+
flex-direction: row;
|
|
53
|
+
align-items: center;
|
|
54
|
+
|
|
55
|
+
h4 {
|
|
56
|
+
flex: 1;
|
|
57
|
+
color: lui.$charcoal;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.LuiModalAsync-main {
|
|
62
|
+
display: flex;
|
|
63
|
+
flex-direction: column;
|
|
64
|
+
flex: 1;
|
|
65
|
+
gap: lui.$unit-rg;
|
|
66
|
+
overflow: hidden;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.LuiModalAsync-content {
|
|
70
|
+
flex: 1;
|
|
71
|
+
overflow: auto;
|
|
72
|
+
font-weight: 400;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.lui-button:focus {
|
|
76
|
+
// Button focus not showing unless window content has focus
|
|
77
|
+
outline: lui.$input-border-width solid lui.$input-focus;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.LuiModalAsync-header-icon {
|
|
81
|
+
fill: lui.$info;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.LuiModalAsync-header-icon.LuiModalAsync-header-icon-ic_check_circle_outline {
|
|
85
|
+
fill: lui.$success;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.LuiModalAsync-header-icon.LuiModalAsync-header-icon-ic_warning_outline {
|
|
89
|
+
fill: lui.$warning;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.LuiModalAsync-header-icon.LuiModalAsync-header-icon-ic_info_outline {
|
|
93
|
+
fill: lui.$info;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.LuiModalAsync-header-icon.LuiModalAsync-header-icon-ic_error_outline {
|
|
97
|
+
fill: lui.$error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import "./LuiModalAsync.scss";
|
|
2
|
+
import "@linzjs/lui/dist/scss/base.scss";
|
|
3
|
+
|
|
4
|
+
import { LuiModalAsyncInstanceContext } from "./LuiModalAsyncInstanceContext";
|
|
5
|
+
import clsx from "clsx";
|
|
6
|
+
import { delay } from "lodash";
|
|
7
|
+
import React, { CSSProperties, PropsWithChildren, ReactElement, useContext, useEffect, useRef } from "react";
|
|
8
|
+
|
|
9
|
+
import "@linzjs/lui/dist/fonts";
|
|
10
|
+
|
|
11
|
+
export interface LuiModalAsyncProps {
|
|
12
|
+
className?: string;
|
|
13
|
+
style?: CSSProperties;
|
|
14
|
+
/**
|
|
15
|
+
* Close on clicking overlay
|
|
16
|
+
*/
|
|
17
|
+
closeOnOverlayClick?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* This creates the dialog element.
|
|
22
|
+
*/
|
|
23
|
+
export const LuiModalAsync = ({
|
|
24
|
+
style,
|
|
25
|
+
className,
|
|
26
|
+
closeOnOverlayClick,
|
|
27
|
+
children,
|
|
28
|
+
}: PropsWithChildren<LuiModalAsyncProps>): ReactElement => {
|
|
29
|
+
const dialogRef = useRef<HTMLDialogElement>(null);
|
|
30
|
+
|
|
31
|
+
const { close } = useContext(LuiModalAsyncInstanceContext);
|
|
32
|
+
|
|
33
|
+
// The only way to create a modal dialog is to call showModal, open attribute will not work
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
// Check if it's open already to support .vite hot deploys
|
|
36
|
+
!dialogRef.current?.open && dialogRef.current?.showModal();
|
|
37
|
+
}, []);
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
// Dialogs auto select the first focusable element, this is in case you don't want that
|
|
41
|
+
// For example we don't want to select the help button by default, so we add data-noautofocus
|
|
42
|
+
delay(() => {
|
|
43
|
+
const d = dialogRef.current;
|
|
44
|
+
if (!d) return;
|
|
45
|
+
let input = d.querySelectorAll("[data-autofocus]")[0] as any;
|
|
46
|
+
if (!input) {
|
|
47
|
+
input = d.querySelectorAll("button:not([data-noautofocus]),input:not([data-noautofocus])")[0] as any;
|
|
48
|
+
}
|
|
49
|
+
if (input) {
|
|
50
|
+
input.focus?.();
|
|
51
|
+
input.select?.();
|
|
52
|
+
}
|
|
53
|
+
}, 100);
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
// You cannot override dialogs position from absolute.
|
|
57
|
+
return className?.includes("storybook") ? (
|
|
58
|
+
<div className={clsx("LuiModalAsync", className)} style={style}>
|
|
59
|
+
{children}
|
|
60
|
+
</div>
|
|
61
|
+
) : (
|
|
62
|
+
<dialog
|
|
63
|
+
className={clsx("LuiModalAsync", className)}
|
|
64
|
+
style={style}
|
|
65
|
+
ref={dialogRef}
|
|
66
|
+
onCancel={(e) => {
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
close();
|
|
69
|
+
}}
|
|
70
|
+
onClick={(e) => closeOnOverlayClick && e.target === e.currentTarget && close()}
|
|
71
|
+
>
|
|
72
|
+
{children}
|
|
73
|
+
</dialog>
|
|
74
|
+
);
|
|
75
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { LuiModalAsyncInstanceContext } from "./LuiModalAsyncInstanceContext";
|
|
2
|
+
import React, { PropsWithChildren, useContext } from "react";
|
|
3
|
+
|
|
4
|
+
import { LuiButton } from "@linzjs/lui";
|
|
5
|
+
import { LuiButtonProps } from "@linzjs/lui/dist/components/LuiButton/LuiButton";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wrapper for buttons at the bottom of model.
|
|
9
|
+
*/
|
|
10
|
+
export const LuiModalAsyncButtonGroup = ({ children }: PropsWithChildren<unknown>) => (
|
|
11
|
+
<div className={"LuiModalAsync-ButtonGroup"}>{children}</div>
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Calls model close on click.
|
|
16
|
+
*/
|
|
17
|
+
export const LuiModalAsyncButtonDismiss = React.forwardRef<HTMLButtonElement, LuiButtonProps & { autofocus?: boolean }>(
|
|
18
|
+
function LuiModalAsyncButtonDismiss(props, ref) {
|
|
19
|
+
return (
|
|
20
|
+
<LuiButton
|
|
21
|
+
{...props}
|
|
22
|
+
{...(props.level ? {} : { level: "tertiary" })}
|
|
23
|
+
{...(props.autofocus ? { buttonProps: { "data-autofocus": true } } : {})}
|
|
24
|
+
ref={ref}
|
|
25
|
+
onClick={useContext(LuiModalAsyncInstanceContext).close}
|
|
26
|
+
>
|
|
27
|
+
{props.children}
|
|
28
|
+
</LuiButton>
|
|
29
|
+
);
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resolves to value on click, or if onClick is specified calls onClick.
|
|
35
|
+
*/
|
|
36
|
+
export const LuiModalAsyncButtonContinue = React.forwardRef<
|
|
37
|
+
HTMLButtonElement,
|
|
38
|
+
LuiButtonProps & { value?: any; autofocus?: boolean }
|
|
39
|
+
>(function LuiModalAsyncButtonContinue(props, ref) {
|
|
40
|
+
const { resolve } = useContext(LuiModalAsyncInstanceContext);
|
|
41
|
+
return (
|
|
42
|
+
<LuiButton
|
|
43
|
+
{...props}
|
|
44
|
+
{...(props.level ? {} : { level: "primary" })}
|
|
45
|
+
{...(props.autofocus ? { buttonProps: { "data-autofocus": true } } : {})}
|
|
46
|
+
ref={ref}
|
|
47
|
+
onClick={async (e) => {
|
|
48
|
+
if (props.onClick) props.onClick(e);
|
|
49
|
+
else resolve(props.value ?? "continue");
|
|
50
|
+
}}
|
|
51
|
+
>
|
|
52
|
+
{props.children}
|
|
53
|
+
</LuiButton>
|
|
54
|
+
);
|
|
55
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wrapper for modal content.
|
|
5
|
+
* Content is the scrollable area.
|
|
6
|
+
*/
|
|
7
|
+
export const LuiModalAsyncContent = ({ children }: PropsWithChildren<unknown>) => (
|
|
8
|
+
<div className={"LuiModalAsync-content"}>{children}</div>
|
|
9
|
+
);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ComponentProps, MutableRefObject, ReactElement, createContext } from "react";
|
|
2
|
+
|
|
3
|
+
export type ComponentType = (props: any) => ReactElement<any, any>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Promise extended to add the ability to externally resolve dialog.
|
|
7
|
+
*/
|
|
8
|
+
export interface PromiseWithResolve<RT> extends Promise<RT> {
|
|
9
|
+
resolve: (value: RT) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Properties passed to the modal component to allow resolving.
|
|
14
|
+
*/
|
|
15
|
+
export interface LuiModalAsyncCallback<R> {
|
|
16
|
+
resolve: (result: R | undefined) => void;
|
|
17
|
+
close: () => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface LuiModalAsyncContextType {
|
|
21
|
+
showModal: <
|
|
22
|
+
OR extends HTMLElement | null,
|
|
23
|
+
PROPS extends LuiModalAsyncCallback<any>,
|
|
24
|
+
CT extends (props: PROPS) => ReactElement<any, any>,
|
|
25
|
+
RT = Parameters<ComponentProps<CT>["resolve"]>[0],
|
|
26
|
+
>(
|
|
27
|
+
ownerRef: MutableRefObject<OR>,
|
|
28
|
+
component: CT,
|
|
29
|
+
args: Omit<ComponentProps<CT>, "resolve" | "close">,
|
|
30
|
+
) => PromiseWithResolve<RT>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const LuiModalAsyncContext = createContext<LuiModalAsyncContextType>({
|
|
34
|
+
showModal: (async () => {
|
|
35
|
+
console.error("Missing LuiModalAsyncContext Provider");
|
|
36
|
+
}) as unknown as LuiModalAsyncContextType["showModal"],
|
|
37
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { ComponentType, LuiModalAsyncContext, PromiseWithResolve } from "./LuiModalAsyncContext";
|
|
2
|
+
import { LuiModalAsyncInstanceContext } from "./LuiModalAsyncInstanceContext";
|
|
3
|
+
import React, { MutableRefObject, PropsWithChildren, ReactElement, useCallback, useState } from "react";
|
|
4
|
+
import { createPortal } from "react-dom";
|
|
5
|
+
import { useInterval } from "usehooks-ts";
|
|
6
|
+
import { v4 as makeUuid } from "uuid";
|
|
7
|
+
|
|
8
|
+
export interface LuiModalAsyncInstance {
|
|
9
|
+
uuid: string;
|
|
10
|
+
ownerRef: MutableRefObject<HTMLElement | null>;
|
|
11
|
+
componentInstance: ReactElement;
|
|
12
|
+
resolve: (result: any) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Provides the ability to show modals using react components without needing useState boilerplate and inline dialogs.
|
|
17
|
+
*
|
|
18
|
+
* To use:
|
|
19
|
+
* <ol>
|
|
20
|
+
* <li>Add this Provider somewhere in your standard providers
|
|
21
|
+
* <pre>
|
|
22
|
+
* <LuiModalAsyncContextProvider>
|
|
23
|
+
* ...children
|
|
24
|
+
* </LuiModalAsyncContextProvider>
|
|
25
|
+
* </pre>
|
|
26
|
+
* </li>
|
|
27
|
+
* <li>Add the modal return type to the element as below, and use the props resolve/close from ModalProps for results.
|
|
28
|
+
* <pre>
|
|
29
|
+
* export interface SomeModalProps extends ModalProps<number> {
|
|
30
|
+
* someProp?: number; // user props
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* export const SomeModal = ({initialState, resolve, close}: SomeModalProps): ReactElement => {
|
|
34
|
+
* return Modal(
|
|
35
|
+
* <div>
|
|
36
|
+
* Itsa me, I'm a modal
|
|
37
|
+
* <button onClick={close}>Cancel</button>
|
|
38
|
+
* <button onClick={()=>resolve(someProp)}>Save</button>
|
|
39
|
+
* </div>
|
|
40
|
+
* )}
|
|
41
|
+
* }
|
|
42
|
+
* </pre>
|
|
43
|
+
* </li>
|
|
44
|
+
* <li> To show the dialog and get the result...
|
|
45
|
+
* <pre>
|
|
46
|
+
* // Note: modalOwnerRef is only required if you need to support popout windows
|
|
47
|
+
* const { showModal, modalOwnerRef } = useContext(ModalContext);
|
|
48
|
+
* ...
|
|
49
|
+
* const showModal = () => {
|
|
50
|
+
* const result = await showModal(SomeModal, { someProp: 1 });
|
|
51
|
+
* if (!result) return; // modal cancelled
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* return <button onClick={showModal} ref={modalOwnerRef}>Show Modal!</button>
|
|
55
|
+
* </pre>
|
|
56
|
+
* </li>
|
|
57
|
+
* </ol>
|
|
58
|
+
*/
|
|
59
|
+
export const LuiModalAsyncContextProvider = ({ children }: PropsWithChildren<unknown>): ReactElement => {
|
|
60
|
+
const [modals, setModals] = useState<LuiModalAsyncInstance[]>([]);
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Inserts the modal into the page, and removes once modal has a result.
|
|
64
|
+
* Note: full generic types are provided by the interface. Function def has been simplified here.
|
|
65
|
+
*
|
|
66
|
+
* @param ownerRef Reference to element that opened this dialog such that it works for popout windows.
|
|
67
|
+
* @param Component React component.
|
|
68
|
+
* @param args Arguments for react component.
|
|
69
|
+
*/
|
|
70
|
+
const showModal = useCallback(
|
|
71
|
+
(ownerRef: MutableRefObject<HTMLElement | null>, Component: ComponentType, args: any): PromiseWithResolve<any> => {
|
|
72
|
+
const uuid = makeUuid();
|
|
73
|
+
let extResolve: PromiseWithResolve<any>["resolve"];
|
|
74
|
+
const promise = new Promise((resolve) => {
|
|
75
|
+
extResolve = resolve;
|
|
76
|
+
try {
|
|
77
|
+
// If there are any exceptions the modal won't show
|
|
78
|
+
setModals((modals) => [
|
|
79
|
+
...modals,
|
|
80
|
+
{
|
|
81
|
+
uuid,
|
|
82
|
+
ownerRef,
|
|
83
|
+
componentInstance: <Component {...args} resolve={resolve} close={() => resolve(undefined)} />,
|
|
84
|
+
resolve,
|
|
85
|
+
},
|
|
86
|
+
]);
|
|
87
|
+
} catch (e) {
|
|
88
|
+
console.error(e);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}).then((result) => {
|
|
92
|
+
// Close modal
|
|
93
|
+
setModals((modals) => modals.filter((e) => e.uuid !== uuid));
|
|
94
|
+
return result;
|
|
95
|
+
}) as PromiseWithResolve<any>;
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
97
|
+
promise.resolve = extResolve!;
|
|
98
|
+
return promise;
|
|
99
|
+
},
|
|
100
|
+
[],
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Check modal is still attached to a window.
|
|
105
|
+
*/
|
|
106
|
+
const modalHasView = useCallback(
|
|
107
|
+
(modalInstance: LuiModalAsyncInstance): boolean => !!modalInstance.ownerRef.current?.ownerDocument?.defaultView,
|
|
108
|
+
[],
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Tidy up modals that have closed because of an external window closing
|
|
113
|
+
*/
|
|
114
|
+
useInterval(() => {
|
|
115
|
+
const newModals = modals.filter(modalHasView);
|
|
116
|
+
newModals.length !== modals.length && setModals(newModals);
|
|
117
|
+
}, 500);
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<LuiModalAsyncContext.Provider
|
|
121
|
+
value={{
|
|
122
|
+
showModal,
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
<>
|
|
126
|
+
{modals.filter(modalHasView).map((modalInstance) =>
|
|
127
|
+
createPortal(
|
|
128
|
+
<LuiModalAsyncInstanceContext.Provider
|
|
129
|
+
value={{
|
|
130
|
+
ownerRef: modalInstance.ownerRef,
|
|
131
|
+
close: () => modalInstance.resolve(undefined),
|
|
132
|
+
resolve: modalInstance.resolve,
|
|
133
|
+
}}
|
|
134
|
+
>
|
|
135
|
+
{modalInstance.componentInstance}
|
|
136
|
+
</LuiModalAsyncInstanceContext.Provider>,
|
|
137
|
+
(modalInstance.ownerRef.current?.ownerDocument ?? document).body,
|
|
138
|
+
),
|
|
139
|
+
)}
|
|
140
|
+
</>
|
|
141
|
+
<>{children}</>
|
|
142
|
+
</LuiModalAsyncContext.Provider>
|
|
143
|
+
);
|
|
144
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import "./LuiModalAsync.scss";
|
|
2
|
+
|
|
3
|
+
import { LuiModalAsyncInstanceContext } from "./LuiModalAsyncInstanceContext";
|
|
4
|
+
import React, { PropsWithChildren, ReactElement, useContext } from "react";
|
|
5
|
+
|
|
6
|
+
import { LuiButton, LuiIcon } from "@linzjs/lui";
|
|
7
|
+
import { LuiIconName } from "@linzjs/lui/dist/assets/svg-content";
|
|
8
|
+
|
|
9
|
+
export interface LuiModalAsyncHeaderProps {
|
|
10
|
+
icon?: LuiIconName | ReactElement;
|
|
11
|
+
title: string;
|
|
12
|
+
helpLink?: string;
|
|
13
|
+
onHelpClick?: () => void;
|
|
14
|
+
helpButtonLevel?: "text" | "plain-text" | "primary" | "secondary" | "tertiary" | "success" | "error";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Generic modal header.
|
|
19
|
+
*/
|
|
20
|
+
export const LuiModalAsyncHeader = ({
|
|
21
|
+
icon,
|
|
22
|
+
title,
|
|
23
|
+
helpLink,
|
|
24
|
+
onHelpClick,
|
|
25
|
+
helpButtonLevel,
|
|
26
|
+
}: PropsWithChildren<LuiModalAsyncHeaderProps>) => {
|
|
27
|
+
const { ownerRef } = useContext(LuiModalAsyncInstanceContext);
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div className={"LuiModalAsync-header"}>
|
|
31
|
+
{icon &&
|
|
32
|
+
(typeof icon === "string" ? (
|
|
33
|
+
<LuiIcon
|
|
34
|
+
name={icon}
|
|
35
|
+
alt={"icon"}
|
|
36
|
+
size={"md"}
|
|
37
|
+
className={`LuiModalAsync-header-icon LuiModalAsync-header-icon-${icon}`}
|
|
38
|
+
/>
|
|
39
|
+
) : (
|
|
40
|
+
icon
|
|
41
|
+
))}
|
|
42
|
+
<h4>{title}</h4>
|
|
43
|
+
{(helpLink || onHelpClick) && (
|
|
44
|
+
<LuiButton
|
|
45
|
+
type={"button"}
|
|
46
|
+
level={helpButtonLevel || "plain-text"}
|
|
47
|
+
aria-label={"Help"}
|
|
48
|
+
title={"Help"}
|
|
49
|
+
className={"lui-button-icon-only"}
|
|
50
|
+
buttonProps={{ "data-noautofocus": true }}
|
|
51
|
+
onClick={
|
|
52
|
+
helpLink
|
|
53
|
+
? () => (ownerRef.current?.ownerDocument?.defaultView ?? window).open(helpLink, "_blank")
|
|
54
|
+
: onHelpClick
|
|
55
|
+
}
|
|
56
|
+
>
|
|
57
|
+
<LuiIcon name="ic_help_outline" alt="Help" size="md" />
|
|
58
|
+
</LuiButton>
|
|
59
|
+
)}
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MutableRefObject, createContext } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Actions that can be taken from within the modal via instance context
|
|
5
|
+
*/
|
|
6
|
+
export interface LuiModalAsyncInstanceContextType {
|
|
7
|
+
ownerRef: MutableRefObject<HTMLElement | null>;
|
|
8
|
+
close: () => void;
|
|
9
|
+
resolve: (value: any) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const NoContextError = () => {
|
|
13
|
+
console.error("Missing LuiModalAsyncInstanceContext Provider");
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Provides access to resolving/closing to modal elements.
|
|
18
|
+
*/
|
|
19
|
+
export const LuiModalAsyncInstanceContext = createContext<LuiModalAsyncInstanceContextType>({
|
|
20
|
+
ownerRef: { current: null },
|
|
21
|
+
close: NoContextError,
|
|
22
|
+
resolve: NoContextError,
|
|
23
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wraps the content and header. This is just to match the way figma is arranged.
|
|
5
|
+
*/
|
|
6
|
+
export const LuiModalAsyncMain = ({ children }: PropsWithChildren<unknown>) => (
|
|
7
|
+
<div className={"LuiModalAsync-main"}>{children}</div>
|
|
8
|
+
);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./LuiModalAsync";
|
|
2
|
+
export * from "./LuiModalAsyncButtonGroup";
|
|
3
|
+
export * from "./LuiModalAsyncContent";
|
|
4
|
+
export * from "./LuiModalAsyncHeader";
|
|
5
|
+
export * from "./LuiModalAsyncMain";
|
|
6
|
+
export * from "./LuiModalAsyncContext";
|
|
7
|
+
export * from "./LuiModalAsyncContextProvider";
|
|
8
|
+
export * from "./LuiModalAsyncInstanceContext";
|
|
9
|
+
export * from "./useShowAsyncModal";
|
|
10
|
+
export * from "./useLuiModalPrefab";
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { LuiModalAsync, LuiModalAsyncProps } from "./LuiModalAsync";
|
|
2
|
+
import { LuiModalAsyncButtonGroup } from "./LuiModalAsyncButtonGroup";
|
|
3
|
+
import { LuiModalAsyncContent } from "./LuiModalAsyncContent";
|
|
4
|
+
import { LuiModalAsyncCallback, PromiseWithResolve } from "./LuiModalAsyncContext";
|
|
5
|
+
import { LuiModalAsyncHeader } from "./LuiModalAsyncHeader";
|
|
6
|
+
import { LuiModalAsyncMain } from "./LuiModalAsyncMain";
|
|
7
|
+
import { useShowAsyncModal } from "./useShowAsyncModal";
|
|
8
|
+
import clsx from "clsx";
|
|
9
|
+
import { flatMap } from "lodash";
|
|
10
|
+
import React, { PropsWithChildren, ReactElement, useCallback, useMemo, useState } from "react";
|
|
11
|
+
|
|
12
|
+
import { LuiButton, LuiCheckboxInput, LuiMiniSpinner } from "@linzjs/lui";
|
|
13
|
+
import { LuiButtonProps } from "@linzjs/lui/dist/components/LuiButton/LuiButton";
|
|
14
|
+
import { IconName } from "@linzjs/lui/dist/components/LuiIcon/LuiIcon";
|
|
15
|
+
|
|
16
|
+
export type PrefabType = "success" | "info" | "warning" | "error" | "progress";
|
|
17
|
+
|
|
18
|
+
export interface LuiModalAsyncPrefabButton<RT> {
|
|
19
|
+
default?: boolean;
|
|
20
|
+
level?: LuiButtonProps["level"];
|
|
21
|
+
title: string;
|
|
22
|
+
value?: RT;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface useLuiModalPrefabProps<RT extends any = string> extends LuiModalAsyncProps {
|
|
26
|
+
level: PrefabType;
|
|
27
|
+
title: string;
|
|
28
|
+
helpLink?: string;
|
|
29
|
+
onHelpClick?: () => void;
|
|
30
|
+
buttons?: LuiModalAsyncPrefabButton<RT>[];
|
|
31
|
+
dontShowAgainSessionKey?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface LuiModalAsyncPrefabProps<RT> extends useLuiModalPrefabProps<RT>, LuiModalAsyncCallback<RT> {
|
|
35
|
+
//
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const getIconForLevel = (level: PrefabType): IconName | "custom_progress" => {
|
|
39
|
+
switch (level) {
|
|
40
|
+
case "success":
|
|
41
|
+
return "ic_check_circle_outline";
|
|
42
|
+
case "info":
|
|
43
|
+
return "ic_info_outline";
|
|
44
|
+
case "warning":
|
|
45
|
+
return "ic_warning_outline";
|
|
46
|
+
case "error":
|
|
47
|
+
return "ic_error_outline";
|
|
48
|
+
case "progress":
|
|
49
|
+
return "custom_progress";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const LuiModalDontShowSessionStorageKey = (userKey: string): string => `__LuiModalContext_dontshowagain_${userKey}`;
|
|
54
|
+
|
|
55
|
+
export const LuiModalDontShowSessionSet = (userKey: string): void =>
|
|
56
|
+
window.sessionStorage.setItem(LuiModalDontShowSessionStorageKey(userKey), "true");
|
|
57
|
+
|
|
58
|
+
export const LuiModalDontShowSessionRemove = (userKey: string): void =>
|
|
59
|
+
window.sessionStorage.removeItem(LuiModalDontShowSessionStorageKey(userKey));
|
|
60
|
+
|
|
61
|
+
export const LuiModalDontShowSessionCheck = (userKey: string): boolean =>
|
|
62
|
+
!!window.sessionStorage.getItem(LuiModalDontShowSessionStorageKey(userKey));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A generic template for common types of modals.
|
|
66
|
+
*/
|
|
67
|
+
export const LuiModalPrefab = (props: PropsWithChildren<LuiModalAsyncPrefabProps<any>>): ReactElement => {
|
|
68
|
+
const {
|
|
69
|
+
level = "warning",
|
|
70
|
+
className,
|
|
71
|
+
style,
|
|
72
|
+
title,
|
|
73
|
+
helpLink,
|
|
74
|
+
onHelpClick,
|
|
75
|
+
children,
|
|
76
|
+
resolve,
|
|
77
|
+
closeOnOverlayClick,
|
|
78
|
+
dontShowAgainSessionKey,
|
|
79
|
+
} = props;
|
|
80
|
+
const [dontShowAgain, setDontShowAgain] = useState(false);
|
|
81
|
+
|
|
82
|
+
const icon = getIconForLevel(level);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Default buttons
|
|
86
|
+
* Warning, Info, Error, Success: "Dismiss"
|
|
87
|
+
* Progress: "Cancel"
|
|
88
|
+
*/
|
|
89
|
+
const buttons: LuiModalAsyncPrefabButton<any>[] = props.buttons ?? [
|
|
90
|
+
{
|
|
91
|
+
title: level === "progress" ? "Cancel" : "Dismiss",
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
// If there are two options, option 1 is secondary, option 2 is primary
|
|
96
|
+
// Otherwise all options are secondary
|
|
97
|
+
buttons.length === 2 &&
|
|
98
|
+
buttons.forEach((b, i) => {
|
|
99
|
+
if (!b.level) {
|
|
100
|
+
// Last button is primary all others are secondary
|
|
101
|
+
b.level = i === buttons.length - 1 ? "primary" : "secondary";
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* When dialog closes save the "don't show" property
|
|
107
|
+
*/
|
|
108
|
+
const setDontShow = () => {
|
|
109
|
+
if (dontShowAgainSessionKey && dontShowAgain) {
|
|
110
|
+
LuiModalDontShowSessionSet(dontShowAgainSessionKey);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
<LuiModalAsync
|
|
116
|
+
className={clsx("LuiModalPrefab", `LuiModalPrefab-${level}`, className)}
|
|
117
|
+
style={style}
|
|
118
|
+
closeOnOverlayClick={closeOnOverlayClick}
|
|
119
|
+
>
|
|
120
|
+
<LuiModalAsyncMain>
|
|
121
|
+
<LuiModalAsyncHeader
|
|
122
|
+
title={title}
|
|
123
|
+
icon={icon === "custom_progress" ? <LuiMiniSpinner size={24} /> : icon}
|
|
124
|
+
helpLink={helpLink}
|
|
125
|
+
onHelpClick={onHelpClick}
|
|
126
|
+
/>
|
|
127
|
+
<LuiModalAsyncContent>{children}</LuiModalAsyncContent>
|
|
128
|
+
{dontShowAgainSessionKey && (
|
|
129
|
+
<LuiCheckboxInput
|
|
130
|
+
className={"LuiCheckboxInput--nomargin"}
|
|
131
|
+
label={"Don't show me this again"}
|
|
132
|
+
value={"dontShowAgain"}
|
|
133
|
+
isChecked={dontShowAgain}
|
|
134
|
+
onChange={(e) => setDontShowAgain(e.target.checked)}
|
|
135
|
+
/>
|
|
136
|
+
)}
|
|
137
|
+
</LuiModalAsyncMain>
|
|
138
|
+
<LuiModalAsyncButtonGroup>
|
|
139
|
+
{buttons.map((pf, i) => (
|
|
140
|
+
<LuiButton
|
|
141
|
+
key={i}
|
|
142
|
+
level={pf.level ?? "secondary"}
|
|
143
|
+
onClick={() => {
|
|
144
|
+
setDontShow();
|
|
145
|
+
resolve(pf.value);
|
|
146
|
+
}}
|
|
147
|
+
buttonProps={pf.default ? { "data-autofocus": true } : undefined}
|
|
148
|
+
>
|
|
149
|
+
{pf.title}
|
|
150
|
+
</LuiButton>
|
|
151
|
+
))}
|
|
152
|
+
</LuiModalAsyncButtonGroup>
|
|
153
|
+
</LuiModalAsync>
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Instantiate a generic template for common types of modals.
|
|
159
|
+
*
|
|
160
|
+
* Usage:
|
|
161
|
+
* const { modalOwnerRef, showPrefabModal } = useLuiModalPrefab();
|
|
162
|
+
*
|
|
163
|
+
* return (
|
|
164
|
+
* <button
|
|
165
|
+
* ref={modalOwnerRef}
|
|
166
|
+
* onClick={() =>
|
|
167
|
+
* showPrefabModal({
|
|
168
|
+
* level: 'info',
|
|
169
|
+
* title: 'You are a fantastic person',
|
|
170
|
+
* children: 'Keep it up!',
|
|
171
|
+
* })
|
|
172
|
+
* }>
|
|
173
|
+
* Open Modal
|
|
174
|
+
* </button>
|
|
175
|
+
*/
|
|
176
|
+
export const useLuiModalPrefab = () => {
|
|
177
|
+
const { showModal, modalOwnerRef } = useShowAsyncModal();
|
|
178
|
+
|
|
179
|
+
const showPrefabModal = useCallback(
|
|
180
|
+
<RT extends any = string>(props: PropsWithChildren<useLuiModalPrefabProps<RT>>) => {
|
|
181
|
+
if (typeof props.children === "string") {
|
|
182
|
+
// Convert \n to <br/>
|
|
183
|
+
const split = props.children.split("\n");
|
|
184
|
+
props.children = flatMap(split.map((part, i) => (i !== split.length - 1 ? [part, <br key={i} />] : part)));
|
|
185
|
+
}
|
|
186
|
+
if (props.dontShowAgainSessionKey && LuiModalDontShowSessionCheck(props.dontShowAgainSessionKey)) {
|
|
187
|
+
return Promise.resolve(undefined);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return showModal(LuiModalPrefab, props) as PromiseWithResolve<RT>;
|
|
191
|
+
},
|
|
192
|
+
[showModal],
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return useMemo(
|
|
196
|
+
() => ({
|
|
197
|
+
modalOwnerRef,
|
|
198
|
+
showPrefabModal,
|
|
199
|
+
}),
|
|
200
|
+
[modalOwnerRef, showPrefabModal],
|
|
201
|
+
);
|
|
202
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ComponentType, LuiModalAsyncContext, PromiseWithResolve } from "./LuiModalAsyncContext";
|
|
2
|
+
import { ComponentProps, useCallback, useContext, useMemo, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
export const useShowAsyncModal = () => {
|
|
5
|
+
const { showModal } = useContext(LuiModalAsyncContext);
|
|
6
|
+
|
|
7
|
+
const modalOwnerRef = useRef<any>(null);
|
|
8
|
+
|
|
9
|
+
const showModalAsync = useCallback(
|
|
10
|
+
<CT extends ComponentType>(
|
|
11
|
+
component: CT,
|
|
12
|
+
args: Omit<ComponentProps<CT>, "resolve" | "close">,
|
|
13
|
+
): PromiseWithResolve<Parameters<ComponentProps<CT>["resolve"]>[0]> => showModal(modalOwnerRef, component, args),
|
|
14
|
+
[showModal],
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
return useMemo(
|
|
18
|
+
() => ({
|
|
19
|
+
showModal: showModalAsync,
|
|
20
|
+
modalOwnerRef,
|
|
21
|
+
}),
|
|
22
|
+
[showModalAsync],
|
|
23
|
+
);
|
|
24
|
+
};
|