react-modal-port 0.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/cjs/context.js +140 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/modal-port.js +26 -0
- package/dist/esm/context.js +133 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/modal-port.js +24 -0
- package/package.json +59 -0
- package/readme.md +289 -0
- package/types/index.d.ts +51 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
50
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
51
|
+
if (ar || !(i in from)) {
|
|
52
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
53
|
+
ar[i] = from[i];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
57
|
+
};
|
|
58
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
59
|
+
exports.useModalState = exports.useModal = exports.useModalContext = exports.ModalContextProvider = exports.ModalContext = void 0;
|
|
60
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
61
|
+
var react_1 = require("react");
|
|
62
|
+
exports.ModalContext = (0, react_1.createContext)({
|
|
63
|
+
stack: [],
|
|
64
|
+
launchModal: function () { },
|
|
65
|
+
updateStack: function () { },
|
|
66
|
+
updateState: function () { },
|
|
67
|
+
});
|
|
68
|
+
var ModalContextProvider = function (_a) {
|
|
69
|
+
var children = _a.children;
|
|
70
|
+
var _b = (0, react_1.useState)([]), stack = _b[0], updateStack = _b[1];
|
|
71
|
+
var launchModal = function (render, resolvers, onBackdropClickUse) {
|
|
72
|
+
// Wrap resolvers to update the stack
|
|
73
|
+
var wrappedResovers = Object.keys(resolvers).reduce(function (acc, key) {
|
|
74
|
+
var resolver = resolvers[key];
|
|
75
|
+
acc[key] = function (ev) { return __awaiter(void 0, void 0, void 0, function () {
|
|
76
|
+
var result;
|
|
77
|
+
return __generator(this, function (_a) {
|
|
78
|
+
switch (_a.label) {
|
|
79
|
+
case 0: return [4 /*yield*/, resolver(ev)];
|
|
80
|
+
case 1:
|
|
81
|
+
result = _a.sent();
|
|
82
|
+
updateStack(stack.slice(0, -1));
|
|
83
|
+
return [2 /*return*/, result];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}); };
|
|
87
|
+
return acc;
|
|
88
|
+
}, {});
|
|
89
|
+
updateStack(__spreadArray(__spreadArray([], stack, true), [
|
|
90
|
+
{
|
|
91
|
+
render: render,
|
|
92
|
+
resolvers: wrappedResovers,
|
|
93
|
+
onBackdropClickUse: onBackdropClickUse,
|
|
94
|
+
state: {},
|
|
95
|
+
},
|
|
96
|
+
], false));
|
|
97
|
+
};
|
|
98
|
+
var updateState = function (newState) {
|
|
99
|
+
var updatedStack = __spreadArray([], stack, true);
|
|
100
|
+
updatedStack[stack.length - 1].state = __assign({}, newState);
|
|
101
|
+
updateStack(updatedStack);
|
|
102
|
+
};
|
|
103
|
+
var contextProperties = {
|
|
104
|
+
stack: stack,
|
|
105
|
+
launchModal: launchModal,
|
|
106
|
+
updateStack: updateStack,
|
|
107
|
+
updateState: updateState,
|
|
108
|
+
};
|
|
109
|
+
return (0, jsx_runtime_1.jsx)(exports.ModalContext.Provider, { value: contextProperties, children: children });
|
|
110
|
+
};
|
|
111
|
+
exports.ModalContextProvider = ModalContextProvider;
|
|
112
|
+
/**
|
|
113
|
+
* Get properties from the modal context
|
|
114
|
+
* @returns {ModalContextProperties} Object with the modal context properties
|
|
115
|
+
*/
|
|
116
|
+
function useModalContext() {
|
|
117
|
+
return (0, react_1.useContext)(exports.ModalContext);
|
|
118
|
+
}
|
|
119
|
+
exports.useModalContext = useModalContext;
|
|
120
|
+
/**
|
|
121
|
+
* Short-hand for `useModalContext().launchModal`
|
|
122
|
+
* @returns {LaunchModal} The launchModal function
|
|
123
|
+
*/
|
|
124
|
+
function useModal() {
|
|
125
|
+
var launchModal = useModalContext().launchModal;
|
|
126
|
+
return launchModal;
|
|
127
|
+
}
|
|
128
|
+
exports.useModal = useModal;
|
|
129
|
+
/**
|
|
130
|
+
* Get access to the current modal state and a function to update it
|
|
131
|
+
* @returns {[ ModalState, UpdateModalState ]} A tuple with the current modal state and a function to update it
|
|
132
|
+
*/
|
|
133
|
+
function useModalState() {
|
|
134
|
+
var _a = useModalContext(), stack = _a.stack, updateState = _a.updateState;
|
|
135
|
+
if (stack.length === 0)
|
|
136
|
+
throw new Error('Trying to access modal state while no modal is open');
|
|
137
|
+
var state = stack[stack.length - 1].state;
|
|
138
|
+
return [state, updateState];
|
|
139
|
+
}
|
|
140
|
+
exports.useModalState = useModalState;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ModalPort = exports.useModal = exports.useModalContext = exports.ModalContextProvider = void 0;
|
|
7
|
+
var context_1 = require("./context");
|
|
8
|
+
Object.defineProperty(exports, "ModalContextProvider", { enumerable: true, get: function () { return context_1.ModalContextProvider; } });
|
|
9
|
+
Object.defineProperty(exports, "useModalContext", { enumerable: true, get: function () { return context_1.useModalContext; } });
|
|
10
|
+
Object.defineProperty(exports, "useModal", { enumerable: true, get: function () { return context_1.useModal; } });
|
|
11
|
+
var modal_port_1 = require("./modal-port");
|
|
12
|
+
Object.defineProperty(exports, "ModalPort", { enumerable: true, get: function () { return __importDefault(modal_port_1).default; } });
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
4
|
+
var context_1 = require("./context");
|
|
5
|
+
var react_1 = require("react");
|
|
6
|
+
var ModalPort = function (_a) {
|
|
7
|
+
var onModalLaunch = _a.onModalLaunch, onModalClose = _a.onModalClose, Backdrop = _a.render;
|
|
8
|
+
var stack = (0, context_1.useModalContext)().stack;
|
|
9
|
+
var currentModal = (0, react_1.useMemo)(function () { return stack.length > 0 ? stack[stack.length - 1] : null; }, [stack]);
|
|
10
|
+
var onBackdropClick = (0, react_1.useCallback)(function (ev) {
|
|
11
|
+
if (ev.currentTarget !== ev.target)
|
|
12
|
+
return;
|
|
13
|
+
(currentModal === null || currentModal === void 0 ? void 0 : currentModal.onBackdropClickUse) &&
|
|
14
|
+
currentModal.resolvers[currentModal.onBackdropClickUse](ev);
|
|
15
|
+
}, [currentModal]);
|
|
16
|
+
(0, react_1.useEffect)(function () {
|
|
17
|
+
if (currentModal) {
|
|
18
|
+
onModalLaunch && onModalLaunch();
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
onModalClose && onModalClose();
|
|
22
|
+
}
|
|
23
|
+
}, [currentModal, onModalLaunch, onModalClose]);
|
|
24
|
+
return currentModal && ((0, jsx_runtime_1.jsx)(Backdrop, { onBackdropClick: onBackdropClick, children: currentModal.render(currentModal.resolvers) })) || null;
|
|
25
|
+
};
|
|
26
|
+
exports.default = ModalPort;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
49
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
50
|
+
if (ar || !(i in from)) {
|
|
51
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
52
|
+
ar[i] = from[i];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
56
|
+
};
|
|
57
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
58
|
+
import { createContext, useContext, useState } from 'react';
|
|
59
|
+
export var ModalContext = createContext({
|
|
60
|
+
stack: [],
|
|
61
|
+
launchModal: function () { },
|
|
62
|
+
updateStack: function () { },
|
|
63
|
+
updateState: function () { },
|
|
64
|
+
});
|
|
65
|
+
export var ModalContextProvider = function (_a) {
|
|
66
|
+
var children = _a.children;
|
|
67
|
+
var _b = useState([]), stack = _b[0], updateStack = _b[1];
|
|
68
|
+
var launchModal = function (render, resolvers, onBackdropClickUse) {
|
|
69
|
+
// Wrap resolvers to update the stack
|
|
70
|
+
var wrappedResovers = Object.keys(resolvers).reduce(function (acc, key) {
|
|
71
|
+
var resolver = resolvers[key];
|
|
72
|
+
acc[key] = function (ev) { return __awaiter(void 0, void 0, void 0, function () {
|
|
73
|
+
var result;
|
|
74
|
+
return __generator(this, function (_a) {
|
|
75
|
+
switch (_a.label) {
|
|
76
|
+
case 0: return [4 /*yield*/, resolver(ev)];
|
|
77
|
+
case 1:
|
|
78
|
+
result = _a.sent();
|
|
79
|
+
updateStack(stack.slice(0, -1));
|
|
80
|
+
return [2 /*return*/, result];
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}); };
|
|
84
|
+
return acc;
|
|
85
|
+
}, {});
|
|
86
|
+
updateStack(__spreadArray(__spreadArray([], stack, true), [
|
|
87
|
+
{
|
|
88
|
+
render: render,
|
|
89
|
+
resolvers: wrappedResovers,
|
|
90
|
+
onBackdropClickUse: onBackdropClickUse,
|
|
91
|
+
state: {},
|
|
92
|
+
},
|
|
93
|
+
], false));
|
|
94
|
+
};
|
|
95
|
+
var updateState = function (newState) {
|
|
96
|
+
var updatedStack = __spreadArray([], stack, true);
|
|
97
|
+
updatedStack[stack.length - 1].state = __assign({}, newState);
|
|
98
|
+
updateStack(updatedStack);
|
|
99
|
+
};
|
|
100
|
+
var contextProperties = {
|
|
101
|
+
stack: stack,
|
|
102
|
+
launchModal: launchModal,
|
|
103
|
+
updateStack: updateStack,
|
|
104
|
+
updateState: updateState,
|
|
105
|
+
};
|
|
106
|
+
return _jsx(ModalContext.Provider, { value: contextProperties, children: children });
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Get properties from the modal context
|
|
110
|
+
* @returns {ModalContextProperties} Object with the modal context properties
|
|
111
|
+
*/
|
|
112
|
+
export function useModalContext() {
|
|
113
|
+
return useContext(ModalContext);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Short-hand for `useModalContext().launchModal`
|
|
117
|
+
* @returns {LaunchModal} The launchModal function
|
|
118
|
+
*/
|
|
119
|
+
export function useModal() {
|
|
120
|
+
var launchModal = useModalContext().launchModal;
|
|
121
|
+
return launchModal;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get access to the current modal state and a function to update it
|
|
125
|
+
* @returns {[ ModalState, UpdateModalState ]} A tuple with the current modal state and a function to update it
|
|
126
|
+
*/
|
|
127
|
+
export function useModalState() {
|
|
128
|
+
var _a = useModalContext(), stack = _a.stack, updateState = _a.updateState;
|
|
129
|
+
if (stack.length === 0)
|
|
130
|
+
throw new Error('Trying to access modal state while no modal is open');
|
|
131
|
+
var state = stack[stack.length - 1].state;
|
|
132
|
+
return [state, updateState];
|
|
133
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useModalContext } from "./context";
|
|
3
|
+
import { useCallback, useEffect, useMemo } from "react";
|
|
4
|
+
var ModalPort = function (_a) {
|
|
5
|
+
var onModalLaunch = _a.onModalLaunch, onModalClose = _a.onModalClose, Backdrop = _a.render;
|
|
6
|
+
var stack = useModalContext().stack;
|
|
7
|
+
var currentModal = useMemo(function () { return stack.length > 0 ? stack[stack.length - 1] : null; }, [stack]);
|
|
8
|
+
var onBackdropClick = useCallback(function (ev) {
|
|
9
|
+
if (ev.currentTarget !== ev.target)
|
|
10
|
+
return;
|
|
11
|
+
(currentModal === null || currentModal === void 0 ? void 0 : currentModal.onBackdropClickUse) &&
|
|
12
|
+
currentModal.resolvers[currentModal.onBackdropClickUse](ev);
|
|
13
|
+
}, [currentModal]);
|
|
14
|
+
useEffect(function () {
|
|
15
|
+
if (currentModal) {
|
|
16
|
+
onModalLaunch && onModalLaunch();
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
onModalClose && onModalClose();
|
|
20
|
+
}
|
|
21
|
+
}, [currentModal, onModalLaunch, onModalClose]);
|
|
22
|
+
return currentModal && (_jsx(Backdrop, { onBackdropClick: onBackdropClick, children: currentModal.render(currentModal.resolvers) })) || null;
|
|
23
|
+
};
|
|
24
|
+
export default ModalPort;
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-modal-port",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Launch modals from any React component.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"types": "./types/index.d.ts",
|
|
7
|
+
"main": "./dist/cjs/index.js",
|
|
8
|
+
"module": "./dist/esm/index.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./dist/esm/index.js",
|
|
11
|
+
"require": "./dist/cjs/index.js",
|
|
12
|
+
"types": "./types/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"types"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "npm run build:esm && npm run build:cjs",
|
|
20
|
+
"build:esm": "tsc --outDir dist/esm --module esnext",
|
|
21
|
+
"build:cjs": "tsc --outDir dist/cjs --module commonjs",
|
|
22
|
+
"prepare": "npm run build",
|
|
23
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/oliverwehn/react-modal-port.git"
|
|
28
|
+
},
|
|
29
|
+
"tags": [
|
|
30
|
+
"react",
|
|
31
|
+
"modal",
|
|
32
|
+
"dialog"
|
|
33
|
+
],
|
|
34
|
+
"keywords": [
|
|
35
|
+
"react",
|
|
36
|
+
"react-component",
|
|
37
|
+
"modal",
|
|
38
|
+
"dialog"
|
|
39
|
+
],
|
|
40
|
+
"author": "Oliver Greuter-Wehn",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/oliverwehn/react-modal-port/issues"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/oliverwehn/react-modal-port#readme",
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/react": "^18",
|
|
48
|
+
"@types/react-dom": "^18.2.19",
|
|
49
|
+
"react": "^18.2.0",
|
|
50
|
+
"react-dom": "^18",
|
|
51
|
+
"tslib": "^2.6.2",
|
|
52
|
+
"typescript": "^5.3.3"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@types/react": "^18",
|
|
56
|
+
"react": "^18",
|
|
57
|
+
"react-dom": "^18"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# react-modal-port
|
|
2
|
+
|
|
3
|
+
This small but neat package allows you to:
|
|
4
|
+
* Define and style modals the way *you* want or need.
|
|
5
|
+
* Launch modals flexibly from anywhere in your React app.
|
|
6
|
+
* Have them render at one specific place in your DOM.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
$ npm install react-modal-port
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 🧩 Components
|
|
15
|
+
|
|
16
|
+
### ModalContextProvider
|
|
17
|
+
|
|
18
|
+
The `ModalContextProvider` wraps your app to allow your app’s components to launch or render modals.
|
|
19
|
+
|
|
20
|
+
### ModalPort
|
|
21
|
+
|
|
22
|
+
The `ModalPort` handles the rendering of your modals. It leaves the *how* you want to render your modals fully up to you. It simply requires a `React.FunctionComponent` to be passed to it via its `render` prop. This component allows you to wrap any rendered modal in, for example, a backdrop component.
|
|
23
|
+
|
|
24
|
+
Besides `children` a prop `onBackdropClick` is passed to `render`. It holds, if provided to the modal on launch, the click handler function that should be called if the backdrop is clicked, e.g. to dismiss the modal.
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import React from 'react';
|
|
29
|
+
import { ModalContextProvider } from 'react-modal-port';
|
|
30
|
+
|
|
31
|
+
// Backdrop component to be passed to the `ModalPort`’s `render` prop
|
|
32
|
+
const Backdrop: React.FC<ModalPortRenderProps> = ({ children, onBackdropClick }) => (
|
|
33
|
+
<div
|
|
34
|
+
style="
|
|
35
|
+
position: fixed;
|
|
36
|
+
z-index: 1000,
|
|
37
|
+
display: flex;
|
|
38
|
+
justify-content: center;
|
|
39
|
+
align-items-center;
|
|
40
|
+
background-color: rgba(0,0,0,.65);
|
|
41
|
+
top: 0;
|
|
42
|
+
right: 0;
|
|
43
|
+
bottom: 0;
|
|
44
|
+
left: 0;
|
|
45
|
+
"
|
|
46
|
+
onClick={onBackdropClick}
|
|
47
|
+
>
|
|
48
|
+
{children}
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// Your app’s root layout component
|
|
53
|
+
const RootLayout:React.FC<PropsWithChildren> = ({ children }) => (
|
|
54
|
+
<ModalContextProvider>
|
|
55
|
+
<div style="width: 100%; min-height: 100vh;">
|
|
56
|
+
{children}
|
|
57
|
+
<ModalPort render={Backdrop} />
|
|
58
|
+
</div>
|
|
59
|
+
</ModalContextProvider>
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
export default RootLayout;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## 🚀 Launching Modals
|
|
66
|
+
|
|
67
|
+
Launching modals makes use of the `useModal` hook that provides you with the `launchModal` function.
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import React from 'react';
|
|
71
|
+
import { useModal } from 'react-modal-port';
|
|
72
|
+
import DecisionModal from './modals/decision';
|
|
73
|
+
|
|
74
|
+
const Page: React.FC = () => {
|
|
75
|
+
|
|
76
|
+
const launchModal = useModal();
|
|
77
|
+
const [ decision, setDecision ] = useState<boolean|null>(null);
|
|
78
|
+
|
|
79
|
+
const onClick = () => {
|
|
80
|
+
launchModal(
|
|
81
|
+
// Modal component
|
|
82
|
+
DecisionModal,
|
|
83
|
+
// Modal resolvers
|
|
84
|
+
{
|
|
85
|
+
decideYay: () => setDecision(true),
|
|
86
|
+
decideNay: () => setDecision(false),
|
|
87
|
+
leaveMeAlone: () => {},
|
|
88
|
+
},
|
|
89
|
+
// Optional name of the `onBackdropClick` resolver
|
|
90
|
+
"leaveMeAlone"
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<div style="padding: 4rem;">
|
|
96
|
+
<h1>Welcome</h1>
|
|
97
|
+
{decision === null && (
|
|
98
|
+
<p>Make your decision!</p>
|
|
99
|
+
) || (
|
|
100
|
+
<p>Your decision is: {decision && "Yay" || "Nay"}</p>
|
|
101
|
+
)}
|
|
102
|
+
|
|
103
|
+
<div style="display: flex; flex-direction: row; gap: 0.5rem;">
|
|
104
|
+
<button type="button" onClick={onClick}>Let me decide now</button>
|
|
105
|
+
</div>
|
|
106
|
+
</div>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
The `launchModal` function expects the following arguments:
|
|
112
|
+
| Argument | Type | Description |
|
|
113
|
+
|----------|------|-------------|
|
|
114
|
+
| render | `React.FC` | A function that renders the modal and returns a `ReactNode`. |
|
|
115
|
+
| resolvers | `{ [key: string]: (ev?: SyntheticEvent) => Promise<any>\|void }` | An object of resolver functions that will be passed as props to `render`. |
|
|
116
|
+
| onBackdropClickUse | `string` | Optional: The key of the resolver function in the `resolvers` object that should be passed to the backdrop component’s `onBackdropClick` prop. |
|
|
117
|
+
|
|
118
|
+
Modals can be created and designed to fully fit your needs. The only requirement is to define the functions to resolve them, for example, when clicking a button. The modal component for the example above could look like this:
|
|
119
|
+
|
|
120
|
+
```tsx
|
|
121
|
+
import React from 'react';
|
|
122
|
+
|
|
123
|
+
type DecisionModalProps = {
|
|
124
|
+
decideYay: () => void;
|
|
125
|
+
decideNay: () => void;
|
|
126
|
+
leaveMeAlone?: () => void;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const DecisionModal: React.FC<DecisionModalProps> = ({
|
|
130
|
+
decideYay,
|
|
131
|
+
decideNay,
|
|
132
|
+
}) => (
|
|
133
|
+
<div
|
|
134
|
+
style="background-color: white; border-radius: 0.5rem; padding: 1rem; display: flex; flex-direction: column; align-items: center; gap: 0.5rem;"
|
|
135
|
+
role="dialog"
|
|
136
|
+
aria-modal="true"
|
|
137
|
+
aria-labeledby="modal-title"
|
|
138
|
+
aria-describedby="modal-description"
|
|
139
|
+
>
|
|
140
|
+
<h4 id="modal-title">How do you decide?</h4>
|
|
141
|
+
<div id ="modal-description">
|
|
142
|
+
<p>Make your decision and click one of the buttons below.</p>
|
|
143
|
+
</div>
|
|
144
|
+
<div style="display: flex; flex-direction: row; gap: 0.25rem; justify-content: center;">
|
|
145
|
+
<button type="button" onClick={decideYay}>Yay</button>
|
|
146
|
+
<button type="button" onClick={decideNay}>Nay</button>
|
|
147
|
+
</div>
|
|
148
|
+
</div>
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
export default DecisionModal;
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## 🔂 Asynchronous Resolution
|
|
155
|
+
|
|
156
|
+
Modals can be simple like the one above or highly complex like a full wizard experience or questionnaire. It is also possible to trigger asynchronous operations within a resolver function. As long as the resolver function returns a promise, the modal won’t close until the promise is resolved.
|
|
157
|
+
|
|
158
|
+
So you could adapt the `onClick` handler in our earlier example as follows:
|
|
159
|
+
```tsx
|
|
160
|
+
// ...
|
|
161
|
+
|
|
162
|
+
const onClick = () => {
|
|
163
|
+
launchModal(
|
|
164
|
+
// Modal component
|
|
165
|
+
DecisionModal,
|
|
166
|
+
// Modal resolvers
|
|
167
|
+
{
|
|
168
|
+
decideYay: () => setDecision(true),
|
|
169
|
+
decideNay: () => setDecision(false),
|
|
170
|
+
letMeThinkAboutIt: (p: Promise) => p,
|
|
171
|
+
leaveMeAlone: () => {},
|
|
172
|
+
},
|
|
173
|
+
// Optional name of the `onBackdropClick` resolver
|
|
174
|
+
"leaveMeAlone"
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ...
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
It then could cater the following modal functionality:
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
import React from 'react';
|
|
186
|
+
|
|
187
|
+
type DecisionModalProps = {
|
|
188
|
+
decideYay: () => void;
|
|
189
|
+
decideNay: () => void;
|
|
190
|
+
letMeThinkAboutIt: (p: Promise) => Promise;
|
|
191
|
+
leaveMeAlone?: () => void;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const DecisionModal: React.FC<DecisionModalProps> = ({
|
|
195
|
+
decideYay,
|
|
196
|
+
decideNay,
|
|
197
|
+
letMeThinkAboutIt,
|
|
198
|
+
}) => {
|
|
199
|
+
|
|
200
|
+
const imUndecided = () => {
|
|
201
|
+
const p = new Promise((resolve) => {
|
|
202
|
+
const options = [ decideYay, decideNay ];
|
|
203
|
+
setTimeout(
|
|
204
|
+
() => resolve(options[Math.round(Math.random())]),
|
|
205
|
+
Math.random() * 60000
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
letMeThinkAboutIt(p);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<div
|
|
213
|
+
style="background-color: white; border-radius: 0.5rem; padding: 1rem; display: flex; flex-direction: column; align-items: center; gap: 0.5rem;"
|
|
214
|
+
role="dialog"
|
|
215
|
+
aria-modal="true"
|
|
216
|
+
aria-labeledby="modal-title"
|
|
217
|
+
aria-describedby="modal-description"
|
|
218
|
+
>
|
|
219
|
+
<h4 id="modal-title">How do you decide?</h4>
|
|
220
|
+
<div id ="modal-description">
|
|
221
|
+
<p>Make your decision and click one of the buttons below.</p>
|
|
222
|
+
</div>
|
|
223
|
+
<div style="display: flex; flex-direction: row; gap: 0.25rem; justify-content: center;">
|
|
224
|
+
<button type="button" onClick={decideYay}>Yay</button>
|
|
225
|
+
<button type="button" onClick={decideNay}>Nay</button>
|
|
226
|
+
<button type="button" onClick={imUndecided}>Let me think</button>
|
|
227
|
+
</div>
|
|
228
|
+
</div>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export default DecisionModal;
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## 🥞 Stacking Modals And Handling Modal State
|
|
236
|
+
|
|
237
|
+
If you need to launch a modal from a modal, the modals become (logically) stacked. That means that the `ModalPort` will always render the modal at the top of the modal stack and will return to the previous one as soon as the current one is resolved. Via the resolver functions, data can be passed from the resolving modal to the modal it was launched from.
|
|
238
|
+
|
|
239
|
+
In case you need to handle state within multiple stacked modal, you can create, access, and update the modal state using the `useModalState` hook. It returns a tuple with the current modal state and a function to update the state. The modal state is always an object holding key-value pairs. Each modal’s modal state will persist until the modal itself is resolved.
|
|
240
|
+
|
|
241
|
+
In the example below, we’ll ask the user to enter their name and prompt them in a second modal to confirm. We’ll fall back to the first modal with the name previously entered if the user wishes to edit it once more.
|
|
242
|
+
|
|
243
|
+
```tsx
|
|
244
|
+
import React from "react";
|
|
245
|
+
import ConfirmModal from "./ConfirmModal";
|
|
246
|
+
|
|
247
|
+
type AskForNameModalProps = {
|
|
248
|
+
provideName: ModalResolver,
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
export const AskForNameModal: React.FC<AskForNameModalProps> = ({
|
|
252
|
+
provideName,
|
|
253
|
+
}) => {
|
|
254
|
+
|
|
255
|
+
const launchModal = useModal();
|
|
256
|
+
const [ modalState, updateModalState ] = useModalState();
|
|
257
|
+
const onNameChange = (ev) => {
|
|
258
|
+
updateModalState({
|
|
259
|
+
...modalState,
|
|
260
|
+
name: ev.target.value,
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const onButtonClick = () => {
|
|
265
|
+
launchModal(ConfirmModal, {
|
|
266
|
+
confirm: () => provideName(modalState.name),
|
|
267
|
+
});
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<div
|
|
272
|
+
style="background-color: white; border-radius: 0.5rem; padding: 1rem; display: flex; flex-direction: column; align-items: center; gap: 0.5rem;"
|
|
273
|
+
role="dialog"
|
|
274
|
+
aria-modal="true"
|
|
275
|
+
aria-labeledby="modal-title"
|
|
276
|
+
aria-describedby="modal-description"
|
|
277
|
+
>
|
|
278
|
+
<h4 id="modal-title">How should we call you?</h4>
|
|
279
|
+
<div id="modal-description">
|
|
280
|
+
<p>Please enter your firstname or nickname of choice.</p>
|
|
281
|
+
</div>
|
|
282
|
+
<input type="text" value={modalState.name} onChange={onNameChange} style="width: 100%">
|
|
283
|
+
<button type="button" onClick={onButtonClick}>Set name</button>
|
|
284
|
+
</div>
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
```
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
|
|
3
|
+
export type ModalResolver = (...args: any[]) => Promise<void>|void;
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export type LaunchModalResolvers = {
|
|
7
|
+
[key: string]: ModalResolver;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type ModalStackItem = {
|
|
11
|
+
render: React.FunctionComponent<LaunchModalResolvers>;
|
|
12
|
+
resolvers: LaunchModalResolvers;
|
|
13
|
+
onBackdropClickUse?: string;
|
|
14
|
+
state: ModalState;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type LaunchModal = (
|
|
18
|
+
render: React.FunctionComponent<LaunchModalResolvers>,
|
|
19
|
+
resolvers: LaunchModalResolvers,
|
|
20
|
+
onBackdropClickUse?: string,
|
|
21
|
+
) => void;
|
|
22
|
+
|
|
23
|
+
export type ModalState = { [key: string]: any };
|
|
24
|
+
export type UpdateModalState = (newState: ModalState) => void;
|
|
25
|
+
|
|
26
|
+
export type ModalContextProperties = {
|
|
27
|
+
stack: ModalStackItem[];
|
|
28
|
+
launchModal: LaunchModal;
|
|
29
|
+
updateStack: (newStack: ModalStackItem[]) => void;
|
|
30
|
+
updateState: (newState: ModalState) => void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type ModalPortRenderProps = React.PropsWithChildren & {
|
|
34
|
+
onBackdropClick?: (ev: React.SyntheticEvent) => void;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type ModalPortProps = {
|
|
38
|
+
onModalLaunch?: () => void | null;
|
|
39
|
+
onModalClose?: () => void | null;
|
|
40
|
+
render: React.FC<ModalPortRenderProps>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
declare module 'react-modal-port' {
|
|
44
|
+
|
|
45
|
+
const ModalContextProvider: React.FC<PropsWithChildren>;
|
|
46
|
+
const useModalContext: () => ModalContextProperties;
|
|
47
|
+
const useModal: () => ModalContextProperties['launchModal'];
|
|
48
|
+
const ModalPort: React.FC<ModalPortProps>;
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|