@lerx/promise-modal 0.8.2 → 0.8.4
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/components/Anchor/style.d.ts +1 -0
- package/dist/index.cjs +43 -20
- package/dist/index.mjs +43 -20
- package/dist/providers/ConfigurationContext/ConfigurationContext.d.ts +4 -2
- package/dist/providers/ConfigurationContext/useConfigurationContext.d.ts +4 -2
- package/dist/types/option.d.ts +2 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -8,10 +8,10 @@ const hash = require('@winglet/common-utils/hash');
|
|
|
8
8
|
const lib = require('@winglet/common-utils/lib');
|
|
9
9
|
const styleManager = require('@winglet/style-utils/style-manager');
|
|
10
10
|
const util = require('@winglet/style-utils/util');
|
|
11
|
+
const filter = require('@winglet/common-utils/filter');
|
|
11
12
|
const reactDom = require('react-dom');
|
|
12
13
|
const array = require('@winglet/common-utils/array');
|
|
13
14
|
const hoc = require('@winglet/react-utils/hoc');
|
|
14
|
-
const filter = require('@winglet/common-utils/filter');
|
|
15
15
|
const render = require('@winglet/react-utils/render');
|
|
16
16
|
|
|
17
17
|
class ModalManager {
|
|
@@ -415,7 +415,7 @@ const ConfigurationContext = react.createContext({});
|
|
|
415
415
|
const DEFAULT_OPTIONS = {
|
|
416
416
|
zIndex: DEFAULT_Z_INDEX,
|
|
417
417
|
duration: DEFAULT_ANIMATION_DURATION,
|
|
418
|
-
backdrop: DEFAULT_BACKDROP_COLOR,
|
|
418
|
+
backdrop: { backgroundColor: DEFAULT_BACKDROP_COLOR },
|
|
419
419
|
closeOnBackdropClick: true,
|
|
420
420
|
manualDestroy: false,
|
|
421
421
|
};
|
|
@@ -429,20 +429,28 @@ const ConfigurationContextProvider = react.memo(({ ForegroundComponent, Backgrou
|
|
|
429
429
|
FooterComponent: react.memo(FooterComponent || FallbackFooter),
|
|
430
430
|
});
|
|
431
431
|
const options = hook.useSnapshot(inputOptions);
|
|
432
|
-
const value = react.useMemo(() =>
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
432
|
+
const value = react.useMemo(() => {
|
|
433
|
+
const { backdrop: defaultBackdrop, ...defaultOptions } = DEFAULT_OPTIONS;
|
|
434
|
+
const backdrop = getBackdropStyle(options?.backdrop, defaultBackdrop);
|
|
435
|
+
return {
|
|
436
|
+
ForegroundComponent: constant.ForegroundComponent,
|
|
437
|
+
BackgroundComponent: constant.BackgroundComponent,
|
|
438
|
+
TitleComponent: constant.TitleComponent,
|
|
439
|
+
SubtitleComponent: constant.SubtitleComponent,
|
|
440
|
+
ContentComponent: constant.ContentComponent,
|
|
441
|
+
FooterComponent: constant.FooterComponent,
|
|
442
|
+
options: { ...defaultOptions, ...options, backdrop },
|
|
443
|
+
};
|
|
444
|
+
}, [constant, options]);
|
|
444
445
|
return (jsxRuntime.jsx(ConfigurationContext.Provider, { value: value, children: children }));
|
|
445
446
|
});
|
|
447
|
+
const getBackdropStyle = (backdrop, defaultBackdrop) => {
|
|
448
|
+
if (filter.isPlainObject(backdrop))
|
|
449
|
+
return backdrop;
|
|
450
|
+
if (filter.isString(backdrop))
|
|
451
|
+
return { backgroundColor: backdrop };
|
|
452
|
+
return defaultBackdrop;
|
|
453
|
+
};
|
|
446
454
|
|
|
447
455
|
const useConfigurationContext = () => react.useContext(ConfigurationContext);
|
|
448
456
|
const useConfigurationOptions = () => {
|
|
@@ -842,9 +850,20 @@ const style = `
|
|
|
842
850
|
inset: 0;
|
|
843
851
|
pointer-events: none;
|
|
844
852
|
z-index: var(--z-index);
|
|
845
|
-
|
|
853
|
+
}`;
|
|
854
|
+
const backdrop = ModalManager.getHashedClassNames('backdrop');
|
|
855
|
+
const backdropStyle = `
|
|
856
|
+
.${backdrop} {
|
|
857
|
+
position: fixed;
|
|
858
|
+
inset: 0;
|
|
859
|
+
opacity: 0;
|
|
860
|
+
transition-property: opacity;
|
|
861
|
+
transition-duration: var(--transition-duration);
|
|
862
|
+
transition-timing-function: ease-in-out;
|
|
863
|
+
pointer-events: none;
|
|
846
864
|
}`;
|
|
847
865
|
ModalManager.defineStyleSheet('anchor', style);
|
|
866
|
+
ModalManager.defineStyleSheet('backdrop', backdropStyle);
|
|
848
867
|
|
|
849
868
|
const { getValue, increment, reset } = lib.counterFactory(0);
|
|
850
869
|
const AnchorInner = () => {
|
|
@@ -857,11 +876,15 @@ const AnchorInner = () => {
|
|
|
857
876
|
const dimmed = useActiveModalCount(validateDimmable, version);
|
|
858
877
|
if (!dimmed)
|
|
859
878
|
reset();
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
879
|
+
const anchorStyle = react.useMemo(() => ({
|
|
880
|
+
'--z-index': options.zIndex,
|
|
881
|
+
'--transition-duration': options.duration,
|
|
882
|
+
}), [options]);
|
|
883
|
+
const backdropStyle = react.useMemo(() => ({
|
|
884
|
+
...options.backdrop,
|
|
885
|
+
opacity: dimmed ? 1 : 0,
|
|
886
|
+
}), [dimmed, options]);
|
|
887
|
+
return (jsxRuntime.jsxs("div", { className: anchor, style: anchorStyle, children: [jsxRuntime.jsx("div", { className: backdrop, style: backdropStyle }), array.map(modalIds, (id) => (jsxRuntime.jsx(Presenter, { modalId: id, getValue: getValue, increment: increment, reset: reset }, id)))] }));
|
|
865
888
|
};
|
|
866
889
|
const validateDimmable = (modal) => modal?.visible && modal.dimmed;
|
|
867
890
|
const Anchor = react.memo(hoc.withErrorBoundary(AnchorInner));
|
package/dist/index.mjs
CHANGED
|
@@ -6,10 +6,10 @@ import { polynomialHash } from '@winglet/common-utils/hash';
|
|
|
6
6
|
import { getRandomString, counterFactory } from '@winglet/common-utils/lib';
|
|
7
7
|
import { styleManagerFactory, destroyScope } from '@winglet/style-utils/style-manager';
|
|
8
8
|
import { compressCss, cxLite } from '@winglet/style-utils/util';
|
|
9
|
+
import { isPlainObject, isString, isFunction } from '@winglet/common-utils/filter';
|
|
9
10
|
import { createPortal } from 'react-dom';
|
|
10
11
|
import { map } from '@winglet/common-utils/array';
|
|
11
12
|
import { withErrorBoundary } from '@winglet/react-utils/hoc';
|
|
12
|
-
import { isString, isFunction } from '@winglet/common-utils/filter';
|
|
13
13
|
import { renderComponent } from '@winglet/react-utils/render';
|
|
14
14
|
|
|
15
15
|
class ModalManager {
|
|
@@ -413,7 +413,7 @@ const ConfigurationContext = createContext({});
|
|
|
413
413
|
const DEFAULT_OPTIONS = {
|
|
414
414
|
zIndex: DEFAULT_Z_INDEX,
|
|
415
415
|
duration: DEFAULT_ANIMATION_DURATION,
|
|
416
|
-
backdrop: DEFAULT_BACKDROP_COLOR,
|
|
416
|
+
backdrop: { backgroundColor: DEFAULT_BACKDROP_COLOR },
|
|
417
417
|
closeOnBackdropClick: true,
|
|
418
418
|
manualDestroy: false,
|
|
419
419
|
};
|
|
@@ -427,20 +427,28 @@ const ConfigurationContextProvider = memo(({ ForegroundComponent, BackgroundComp
|
|
|
427
427
|
FooterComponent: memo(FooterComponent || FallbackFooter),
|
|
428
428
|
});
|
|
429
429
|
const options = useSnapshot(inputOptions);
|
|
430
|
-
const value = useMemo(() =>
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
430
|
+
const value = useMemo(() => {
|
|
431
|
+
const { backdrop: defaultBackdrop, ...defaultOptions } = DEFAULT_OPTIONS;
|
|
432
|
+
const backdrop = getBackdropStyle(options?.backdrop, defaultBackdrop);
|
|
433
|
+
return {
|
|
434
|
+
ForegroundComponent: constant.ForegroundComponent,
|
|
435
|
+
BackgroundComponent: constant.BackgroundComponent,
|
|
436
|
+
TitleComponent: constant.TitleComponent,
|
|
437
|
+
SubtitleComponent: constant.SubtitleComponent,
|
|
438
|
+
ContentComponent: constant.ContentComponent,
|
|
439
|
+
FooterComponent: constant.FooterComponent,
|
|
440
|
+
options: { ...defaultOptions, ...options, backdrop },
|
|
441
|
+
};
|
|
442
|
+
}, [constant, options]);
|
|
442
443
|
return (jsx(ConfigurationContext.Provider, { value: value, children: children }));
|
|
443
444
|
});
|
|
445
|
+
const getBackdropStyle = (backdrop, defaultBackdrop) => {
|
|
446
|
+
if (isPlainObject(backdrop))
|
|
447
|
+
return backdrop;
|
|
448
|
+
if (isString(backdrop))
|
|
449
|
+
return { backgroundColor: backdrop };
|
|
450
|
+
return defaultBackdrop;
|
|
451
|
+
};
|
|
444
452
|
|
|
445
453
|
const useConfigurationContext = () => useContext(ConfigurationContext);
|
|
446
454
|
const useConfigurationOptions = () => {
|
|
@@ -840,9 +848,20 @@ const style = `
|
|
|
840
848
|
inset: 0;
|
|
841
849
|
pointer-events: none;
|
|
842
850
|
z-index: var(--z-index);
|
|
843
|
-
|
|
851
|
+
}`;
|
|
852
|
+
const backdrop = ModalManager.getHashedClassNames('backdrop');
|
|
853
|
+
const backdropStyle = `
|
|
854
|
+
.${backdrop} {
|
|
855
|
+
position: fixed;
|
|
856
|
+
inset: 0;
|
|
857
|
+
opacity: 0;
|
|
858
|
+
transition-property: opacity;
|
|
859
|
+
transition-duration: var(--transition-duration);
|
|
860
|
+
transition-timing-function: ease-in-out;
|
|
861
|
+
pointer-events: none;
|
|
844
862
|
}`;
|
|
845
863
|
ModalManager.defineStyleSheet('anchor', style);
|
|
864
|
+
ModalManager.defineStyleSheet('backdrop', backdropStyle);
|
|
846
865
|
|
|
847
866
|
const { getValue, increment, reset } = counterFactory(0);
|
|
848
867
|
const AnchorInner = () => {
|
|
@@ -855,11 +874,15 @@ const AnchorInner = () => {
|
|
|
855
874
|
const dimmed = useActiveModalCount(validateDimmable, version);
|
|
856
875
|
if (!dimmed)
|
|
857
876
|
reset();
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
877
|
+
const anchorStyle = useMemo(() => ({
|
|
878
|
+
'--z-index': options.zIndex,
|
|
879
|
+
'--transition-duration': options.duration,
|
|
880
|
+
}), [options]);
|
|
881
|
+
const backdropStyle = useMemo(() => ({
|
|
882
|
+
...options.backdrop,
|
|
883
|
+
opacity: dimmed ? 1 : 0,
|
|
884
|
+
}), [dimmed, options]);
|
|
885
|
+
return (jsxs("div", { className: anchor, style: anchorStyle, children: [jsx("div", { className: backdrop, style: backdropStyle }), map(modalIds, (id) => (jsx(Presenter, { modalId: id, getValue: getValue, increment: increment, reset: reset }, id)))] }));
|
|
863
886
|
};
|
|
864
887
|
const validateDimmable = (modal) => modal?.visible && modal.dimmed;
|
|
865
888
|
const Anchor = memo(withErrorBoundary(AnchorInner));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ComponentType } from 'react';
|
|
1
|
+
import { type CSSProperties, type ComponentType } from 'react';
|
|
2
2
|
import type { BackgroundComponent, FooterComponentProps, ForegroundComponent, ModalOptions, WrapperComponentProps } from '../../types';
|
|
3
3
|
export interface ConfigurationContextProps {
|
|
4
4
|
ForegroundComponent: ForegroundComponent;
|
|
@@ -7,6 +7,8 @@ export interface ConfigurationContextProps {
|
|
|
7
7
|
SubtitleComponent: ComponentType<WrapperComponentProps>;
|
|
8
8
|
ContentComponent: ComponentType<WrapperComponentProps>;
|
|
9
9
|
FooterComponent: ComponentType<FooterComponentProps>;
|
|
10
|
-
options: Required<ModalOptions
|
|
10
|
+
options: Required<Omit<ModalOptions, 'backdrop'> & {
|
|
11
|
+
backdrop: CSSProperties;
|
|
12
|
+
}>;
|
|
11
13
|
}
|
|
12
14
|
export declare const ConfigurationContext: import("react").Context<ConfigurationContextProps>;
|
|
@@ -92,7 +92,9 @@ export declare const useConfigurationContext: () => import("./ConfigurationConte
|
|
|
92
92
|
* }
|
|
93
93
|
* ```
|
|
94
94
|
*/
|
|
95
|
-
export declare const useConfigurationOptions: () => Required<import("../..").ModalOptions
|
|
95
|
+
export declare const useConfigurationOptions: () => Required<Omit<import("../..").ModalOptions, "backdrop"> & {
|
|
96
|
+
backdrop: import("react").CSSProperties;
|
|
97
|
+
}>;
|
|
96
98
|
/**
|
|
97
99
|
* Hook that provides modal animation duration in multiple formats.
|
|
98
100
|
*
|
|
@@ -277,4 +279,4 @@ export declare const useConfigurationDuration: () => {
|
|
|
277
279
|
* - Default backdrop is typically semi-transparent black
|
|
278
280
|
* - Can be overridden at provider level or per-modal
|
|
279
281
|
*/
|
|
280
|
-
export declare const useConfigurationBackdrop: () => import("
|
|
282
|
+
export declare const useConfigurationBackdrop: () => import("react").CSSProperties;
|
package/dist/types/option.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
1
2
|
import type { Color, Duration } from '../@aileron/declare';
|
|
2
3
|
export interface ModalOptions {
|
|
3
4
|
/** Modal z-index */
|
|
@@ -5,7 +6,7 @@ export interface ModalOptions {
|
|
|
5
6
|
/** Modal transition time(ms, s) */
|
|
6
7
|
duration?: Duration;
|
|
7
8
|
/** Modal backdrop color */
|
|
8
|
-
backdrop?: Color;
|
|
9
|
+
backdrop?: Color | CSSProperties;
|
|
9
10
|
/** Whether to destroy the modal manually */
|
|
10
11
|
manualDestroy?: boolean;
|
|
11
12
|
/** Whether to close the modal when the backdrop is clicked */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lerx/promise-modal",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4",
|
|
4
4
|
"description": "Universal React modal utility that can be used outside React components with promise-based results for alert, confirm, and prompt modals",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"size-limit": "size-limit",
|
|
59
59
|
"start": "yarn build && yarn storybook",
|
|
60
60
|
"storybook": "storybook dev -p 6006",
|
|
61
|
-
"test": "
|
|
61
|
+
"test": "vitest",
|
|
62
62
|
"version:major": "yarn version major",
|
|
63
63
|
"version:minor": "yarn version minor",
|
|
64
64
|
"version:patch": "yarn version patch"
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"dependencies": {
|
|
67
67
|
"@winglet/common-utils": "^0.8.1",
|
|
68
68
|
"@winglet/react-utils": "^0.8.0",
|
|
69
|
-
"@winglet/style-utils": "^0.8.
|
|
69
|
+
"@winglet/style-utils": "^0.8.2"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@types/react": "^19.0.0",
|