UNPKG

7.37 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12var __assign = (this && this.__assign) || Object.assign || function(t) {
13 for (var s, i = 1, n = arguments.length; i < n; i++) {
14 s = arguments[i];
15 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
16 t[p] = s[p];
17 }
18 return t;
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21var react_malibu_1 = require("@heroku/react-malibu");
22var classnames = require("classnames");
23var React = require("react");
24var react_transition_group_1 = require("react-transition-group");
25var simple_react_modal_1 = require("simple-react-modal");
26var HKButton_1 = require("./HKButton");
27var Type;
28(function (Type) {
29 Type["Actionable"] = "actionable";
30 Type["Destructive"] = "destructive";
31 Type["Presentation"] = "presentation";
32})(Type = exports.Type || (exports.Type = {}));
33/*
34Dramatis personae of the event flow in this component
35* Show: props.show. Used by the controlling component to indicate a desire to show/hide.
36* iS: state.isShowing. Initially false.
37* iC: state.isClosing. Initially false.
38* in: What should be passed as a prop to <Transition in={ aBoolean } ... />
39
40 Show iS iC in
411. F F F - never displayed
422. T F F - controlling component wants us to display! gDSFP will derive the state on the following line.
433. T T F T Shows the modal. In becomes true so transition in happens.
444. *** props.onDismiss() is called, resulting in the controlling component setting show to F ***
455. F T F T controlling component wants us to hide! gDSFP will derive the following state
466. F T T F Transition to closed. In becomes false, triggers transition state "exiting"
477. *** Transition out continues until handleExited() fires, setting isShowing to false
488. F F T F Modal is hidden. Transition state is 'exited'
499. T F T F Controlling component wants us to display! gDSFP will take us to step 3.
50*/
51var HKModal = /** @class */ (function (_super) {
52 __extends(HKModal, _super);
53 function HKModal() {
54 var _this = _super !== null && _super.apply(this, arguments) || this;
55 _this.state = {
56 isClosing: false,
57 isShowing: false,
58 };
59 _this.handleClose = function (e) {
60 _this.props.onDismiss('cancel');
61 };
62 _this.handleButtonClick = function (e) {
63 _this.props.onDismiss(e.currentTarget.value);
64 };
65 _this.handleExited = function (node) {
66 node.addEventListener('transitionend', function () {
67 _this.setState({ isShowing: false });
68 }, false);
69 };
70 return _this;
71 }
72 HKModal.getDerivedStateFromProps = function (props, state) {
73 if (props.show) {
74 // reset state, we're showing the thing and not closing at all
75 return { isShowing: true, isClosing: false };
76 }
77 else {
78 // We're somewhere in the process of closing the modal
79 // So set isClosing to true.
80 // isShowing will be set to false by handleExited(),
81 // which is the handler fired at the end of the transition out.
82 return { isClosing: true };
83 }
84 };
85 HKModal.prototype.render = function () {
86 var _this = this;
87 var duration = 250;
88 var _a = this.props, children = _a.children, onDismiss = _a.onDismiss, header = _a.header, buttons = _a.buttons, isFlyout = _a.isFlyout, type = _a.type;
89 var _b = this.state, isShowing = _b.isShowing, isClosing = _b.isClosing;
90 var fadeTransition = {
91 transition: "background " + duration + "ms ease-in-out, opacity " + duration + "ms ease-in-out",
92 };
93 var fadeStyles = {
94 entered: { background: 'rgba(0, 0, 0, .2)', opacity: 1 },
95 entering: { background: 'rgba(0, 0, 0, 0)', opacity: 0 },
96 exited: { background: 'rgba(0, 0, 0, 0)', opacity: 0 },
97 exiting: { background: 'rgba(0, 0, 0, .2)', opacity: 1 },
98 };
99 var innerTransition = isFlyout ? {
100 transform: 'translateX(100%)',
101 transition: "transform " + duration + "ms cubic-bezier(0,1,0.5,1)",
102 } : {};
103 var innerStyles = isFlyout ? {
104 entered: { transform: 'translateX(0)', width: '350px' },
105 entering: { transform: 'translateX(100%)', width: '350px' },
106 exited: { transform: 'translateX(100%)', width: '350px' },
107 exiting: { transform: 'translateX(0)', width: '350px' },
108 } : {};
109 var dismissElem = onDismiss && (React.createElement("div", { className: classnames('right-1 absolute pointer', { 'top-1': !header }), onClick: this.handleClose },
110 React.createElement(react_malibu_1.MalibuIcon, { name: 'delete-16', fillClass: 'dark-gray', size: 16, extraClasses: 'icon malibu-icon h1 w1 fill-dark-gray o-50 hover-o-100 h-100 v-mid' })));
111 var modalParentClass = {
112 'items-center justify-center': !isFlyout,
113 };
114 var modalClass = {
115 'fixed right-0 w7 h-100 flex flex-column': isFlyout,
116 'w-100 mw7 center br1': !isFlyout,
117 };
118 var modalChildrenClass = {
119 'flex-auto': isFlyout,
120 };
121 var footer = (buttons || []).map(function (b) { return (React.createElement(HKButton_1.default, { key: b.value, value: b.value, type: b.type, disabled: b.disabled, onClick: _this.handleButtonClick, className: classnames('ml1', b.classNames) }, b.text)); });
122 // in={!isClosing} is derived from the state table at the top of this component
123 return (React.createElement(react_transition_group_1.Transition, { in: !isClosing, timeout: 0, onExited: this.handleExited }, function (state) { return (React.createElement(simple_react_modal_1.default, { containerStyle: __assign({}, innerTransition, innerStyles[state]), containerClassName: classnames('bg-white shadow-outer-1 relative', modalClass), style: __assign({ bottom: 0, left: 0, position: 'fixed', right: 0, top: 0, zIndex: 9999 }, fadeTransition, fadeStyles[state]), className: classnames('flex flex-column', modalParentClass), closeOnOuterClick: true, show: isShowing, onClose: _this.handleClose },
124 React.createElement("div", { className: classnames('hk-modal-header f4 flex items-center justify-center br--top br2', { 'bg-near-white bb b--light-silver pa4': header, 'red': type === 'destructive' }) },
125 header,
126 dismissElem),
127 React.createElement("div", { className: classnames(modalChildrenClass) }, children),
128 buttons && React.createElement("div", { className: 'bt b--light-silver w-100 pa3 tr' }, footer))); }));
129 };
130 HKModal.defaultProps = {
131 isFlyout: false,
132 };
133 return HKModal;
134}(React.Component));
135exports.default = HKModal;
136//# sourceMappingURL=HKModal.js.map
\No newline at end of file