@rivtn/noseur 1.0.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.
Files changed (138) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/dist/esm/Template.d.ts +7 -0
  4. package/dist/esm/Template.js +13 -0
  5. package/dist/esm/compose/Composed.css +437 -0
  6. package/dist/esm/compose/exotic/ColorPicker.d.ts +41 -0
  7. package/dist/esm/compose/exotic/ColorPicker.js +85 -0
  8. package/dist/esm/compose/exotic/ColorSlider.d.ts +30 -0
  9. package/dist/esm/compose/exotic/ColorSlider.js +99 -0
  10. package/dist/esm/compose/form/ComposedPassword.d.ts +25 -0
  11. package/dist/esm/compose/form/ComposedPassword.js +103 -0
  12. package/dist/esm/compose/form/DateTimeInput.d.ts +28 -0
  13. package/dist/esm/compose/form/DateTimeInput.js +154 -0
  14. package/dist/esm/compose/form/DateTimePicker.d.ts +341 -0
  15. package/dist/esm/compose/form/DateTimePicker.js +1265 -0
  16. package/dist/esm/compose/form/FormControl.d.ts +33 -0
  17. package/dist/esm/compose/form/FormControl.js +163 -0
  18. package/dist/esm/compose/form/FormGroup.d.ts +14 -0
  19. package/dist/esm/compose/form/FormGroup.js +58 -0
  20. package/dist/esm/compose/overlay/Alert.d.ts +56 -0
  21. package/dist/esm/compose/overlay/Alert.js +267 -0
  22. package/dist/esm/constants/Alignment.d.ts +17 -0
  23. package/dist/esm/constants/Alignment.js +18 -0
  24. package/dist/esm/constants/Direction.d.ts +26 -0
  25. package/dist/esm/constants/Direction.js +22 -0
  26. package/dist/esm/constants/Orientation.d.ts +8 -0
  27. package/dist/esm/constants/Orientation.js +9 -0
  28. package/dist/esm/constants/Scheme.d.ts +39 -0
  29. package/dist/esm/constants/Scheme.js +43 -0
  30. package/dist/esm/constants/Transition.d.ts +81 -0
  31. package/dist/esm/constants/Transition.js +85 -0
  32. package/dist/esm/constants/Types.d.ts +74 -0
  33. package/dist/esm/constants/Types.js +14 -0
  34. package/dist/esm/core/ComponentBaseProps.d.ts +53 -0
  35. package/dist/esm/core/ComponentBaseProps.js +33 -0
  36. package/dist/esm/core/noseur.css +2390 -0
  37. package/dist/esm/data/Column.d.ts +235 -0
  38. package/dist/esm/data/Column.js +98 -0
  39. package/dist/esm/data/Data.css +213 -0
  40. package/dist/esm/data/Data.d.ts +98 -0
  41. package/dist/esm/data/Data.js +142 -0
  42. package/dist/esm/data/List.d.ts +14 -0
  43. package/dist/esm/data/List.js +117 -0
  44. package/dist/esm/data/Table.d.ts +22 -0
  45. package/dist/esm/data/Table.js +236 -0
  46. package/dist/esm/form/Button.d.ts +35 -0
  47. package/dist/esm/form/Button.js +131 -0
  48. package/dist/esm/form/Calendar.d.ts +20 -0
  49. package/dist/esm/form/Calendar.js +46 -0
  50. package/dist/esm/form/Checkbox.d.ts +20 -0
  51. package/dist/esm/form/Checkbox.js +155 -0
  52. package/dist/esm/form/ColorMap.d.ts +22 -0
  53. package/dist/esm/form/ColorMap.js +43 -0
  54. package/dist/esm/form/ColorPalette.d.ts +28 -0
  55. package/dist/esm/form/ColorPalette.js +87 -0
  56. package/dist/esm/form/Dropdown.d.ts +78 -0
  57. package/dist/esm/form/Dropdown.js +360 -0
  58. package/dist/esm/form/FileInput.d.ts +134 -0
  59. package/dist/esm/form/FileInput.js +523 -0
  60. package/dist/esm/form/Form.css +467 -0
  61. package/dist/esm/form/Input.d.ts +45 -0
  62. package/dist/esm/form/Input.js +186 -0
  63. package/dist/esm/form/RadioButton.d.ts +21 -0
  64. package/dist/esm/form/RadioButton.js +178 -0
  65. package/dist/esm/form/Slider.d.ts +21 -0
  66. package/dist/esm/form/Slider.js +163 -0
  67. package/dist/esm/hooks/useSearchParams.d.ts +15 -0
  68. package/dist/esm/hooks/useSearchParams.js +50 -0
  69. package/dist/esm/hooks/useSubscription.d.ts +9 -0
  70. package/dist/esm/hooks/useSubscription.js +26 -0
  71. package/dist/esm/index.d.ts +60 -0
  72. package/dist/esm/index.js +73 -0
  73. package/dist/esm/layout/GridView.d.ts +23 -0
  74. package/dist/esm/layout/GridView.js +34 -0
  75. package/dist/esm/layout/Layout.css +5 -0
  76. package/dist/esm/misc/Misc.css +164 -0
  77. package/dist/esm/misc/ProgressBar.d.ts +22 -0
  78. package/dist/esm/misc/ProgressBar.js +86 -0
  79. package/dist/esm/overlay/Dialog.d.ts +46 -0
  80. package/dist/esm/overlay/Dialog.js +290 -0
  81. package/dist/esm/overlay/Message.d.ts +35 -0
  82. package/dist/esm/overlay/Message.js +187 -0
  83. package/dist/esm/overlay/Overlay.css +382 -0
  84. package/dist/esm/overlay/Popover.d.ts +30 -0
  85. package/dist/esm/overlay/Popover.js +245 -0
  86. package/dist/esm/overlay/Portal.d.ts +11 -0
  87. package/dist/esm/overlay/Portal.js +29 -0
  88. package/dist/esm/overlay/Toast.d.ts +49 -0
  89. package/dist/esm/overlay/Toast.js +341 -0
  90. package/dist/esm/panel/Accordion.d.ts +26 -0
  91. package/dist/esm/panel/Accordion.js +127 -0
  92. package/dist/esm/panel/Panel.css +317 -0
  93. package/dist/esm/panel/Panel.d.ts +43 -0
  94. package/dist/esm/panel/Panel.js +125 -0
  95. package/dist/esm/panel/ScrollPanel.d.ts +27 -0
  96. package/dist/esm/panel/ScrollPanel.js +43 -0
  97. package/dist/esm/panel/TabPane.d.ts +82 -0
  98. package/dist/esm/panel/TabPane.js +237 -0
  99. package/dist/esm/presentation/Chart.d.ts +49 -0
  100. package/dist/esm/presentation/Chart.js +62 -0
  101. package/dist/esm/presentation/Paginator.d.ts +90 -0
  102. package/dist/esm/presentation/Paginator.js +331 -0
  103. package/dist/esm/presentation/Presentation.css +23 -0
  104. package/dist/esm/sensor/DragSensor.d.ts +50 -0
  105. package/dist/esm/sensor/DragSensor.js +156 -0
  106. package/dist/esm/sensor/ViewportSensor.d.ts +18 -0
  107. package/dist/esm/sensor/ViewportSensor.js +85 -0
  108. package/dist/esm/utils/BoolHelper.d.ts +13 -0
  109. package/dist/esm/utils/BoolHelper.js +105 -0
  110. package/dist/esm/utils/Classname.d.ts +9 -0
  111. package/dist/esm/utils/Classname.js +20 -0
  112. package/dist/esm/utils/ColorHelper.d.ts +127 -0
  113. package/dist/esm/utils/ColorHelper.js +198 -0
  114. package/dist/esm/utils/DOMUtils.d.ts +142 -0
  115. package/dist/esm/utils/DOMUtils.js +759 -0
  116. package/dist/esm/utils/DateHelper.d.ts +115 -0
  117. package/dist/esm/utils/DateHelper.js +338 -0
  118. package/dist/esm/utils/Debugger.d.ts +12 -0
  119. package/dist/esm/utils/Debugger.js +30 -0
  120. package/dist/esm/utils/FileHelper.d.ts +8 -0
  121. package/dist/esm/utils/FileHelper.js +65 -0
  122. package/dist/esm/utils/FunctionStackManager.d.ts +13 -0
  123. package/dist/esm/utils/FunctionStackManager.js +59 -0
  124. package/dist/esm/utils/InputHelper.d.ts +37 -0
  125. package/dist/esm/utils/InputHelper.js +70 -0
  126. package/dist/esm/utils/MicroBuilder.d.ts +24 -0
  127. package/dist/esm/utils/MicroBuilder.js +61 -0
  128. package/dist/esm/utils/ObjectHelper.d.ts +51 -0
  129. package/dist/esm/utils/ObjectHelper.js +334 -0
  130. package/dist/esm/utils/StringHelper.d.ts +10 -0
  131. package/dist/esm/utils/StringHelper.js +54 -0
  132. package/dist/esm/utils/Subscriber.d.ts +14 -0
  133. package/dist/esm/utils/Subscriber.js +27 -0
  134. package/dist/esm/utils/Timer.d.ts +32 -0
  135. package/dist/esm/utils/Timer.js +68 -0
  136. package/dist/esm/utils/TypeChecker.d.ts +12 -0
  137. package/dist/esm/utils/TypeChecker.js +38 -0
  138. package/package.json +63 -0
@@ -0,0 +1,245 @@
1
+ import "./Overlay.css";
2
+ import React from 'react';
3
+ import { Portal } from "./Portal";
4
+ import { Classname } from "../utils/Classname";
5
+ import { TypeChecker } from "../utils/TypeChecker";
6
+ import { Transition } from "../constants/Transition";
7
+ import { ObjectHelper } from "../utils/ObjectHelper";
8
+ import { CSSTransition } from 'react-transition-group';
9
+ import { BaseZIndex, DOMHelper, ScrollHandler, ZIndexHandler } from "../utils/DOMUtils";
10
+ class PopoverComponent extends React.Component {
11
+ static defaultProps = {
12
+ transition: Transition.NONE,
13
+ outsideClickLogic: "elemental",
14
+ transitionTimeout: { enter: 130, exit: 110 },
15
+ pointingArrowClassName: "noseur-popover-arrow",
16
+ };
17
+ state = {
18
+ sticky: false,
19
+ visible: false,
20
+ };
21
+ internalElement;
22
+ target;
23
+ hostRectBeforeRerendering = {};
24
+ documentScrollHandler;
25
+ transitionNodeRef;
26
+ windowResizeListener;
27
+ documentClickListener;
28
+ constructor(props) {
29
+ super(props);
30
+ this.toggle = this.toggle.bind(this);
31
+ this.onExit = this.onExit.bind(this);
32
+ this.onEnter = this.onEnter.bind(this);
33
+ this.onExited = this.onExited.bind(this);
34
+ this.onEntered = this.onEntered.bind(this);
35
+ this.rePosition = this.rePosition.bind(this);
36
+ this.showPopover = this.showPopover.bind(this);
37
+ this.hidePopover = this.hidePopover.bind(this);
38
+ this.transitionNodeRef = React.createRef();
39
+ this.resolvePopoverStyle = this.resolvePopoverStyle.bind(this);
40
+ }
41
+ componentDidMount() {
42
+ ObjectHelper.resolveManageRef(this, {
43
+ toggle: this.toggle,
44
+ rePosition: this.rePosition,
45
+ visible: () => this.state.visible,
46
+ sticky: (sticky) => this.setState({ sticky }),
47
+ hide: () => {
48
+ if (this.state.visible)
49
+ this.hidePopover();
50
+ },
51
+ show: (event, target) => {
52
+ if (this.state.visible)
53
+ this.rePosition(event);
54
+ else
55
+ this.showPopover(event, target);
56
+ },
57
+ });
58
+ }
59
+ componentWillUnmount() {
60
+ ObjectHelper.resolveManageRef(this, null);
61
+ }
62
+ toggle(event, target) {
63
+ (!this.state.visible) ? this.showPopover(event, target) : this.hidePopover();
64
+ }
65
+ onEnter() {
66
+ ZIndexHandler.setElementZIndex(this.internalElement, BaseZIndex.OVERLAY);
67
+ this.resolvePopoverStyle();
68
+ }
69
+ onEntered() {
70
+ const elementToReceiveOpenFocus = this.props.onOpenFocusRef?.current;
71
+ if (elementToReceiveOpenFocus && TypeChecker.isFunction(elementToReceiveOpenFocus?.focus)) {
72
+ elementToReceiveOpenFocus.focus();
73
+ }
74
+ this.bindDocumentClickListener();
75
+ this.bindScrollListener();
76
+ this.bindResizeListener();
77
+ if (this.props.onShow)
78
+ this.props.onShow();
79
+ }
80
+ onExit() {
81
+ this.unbindDocumentClickListener();
82
+ this.unbindScrollListener();
83
+ this.unbindResizeListener();
84
+ }
85
+ onExited() {
86
+ ZIndexHandler.removeElementZIndex(this.internalElement);
87
+ const elementToReceiveCloseFocus = this.props.onCloseFocusRef?.current;
88
+ if (elementToReceiveCloseFocus && TypeChecker.isFunction(elementToReceiveCloseFocus?.focus)) {
89
+ elementToReceiveCloseFocus.focus();
90
+ }
91
+ if (this.props.onHide)
92
+ this.props.onHide();
93
+ }
94
+ showPopover(event, target) {
95
+ this.target = target ?? event?.target ?? event?.currentTarget;
96
+ if (this.state.visible) {
97
+ this.resolvePopoverStyle();
98
+ return;
99
+ }
100
+ this.setState({ visible: true });
101
+ }
102
+ hidePopover() {
103
+ this.setState({ visible: false });
104
+ }
105
+ isNotToggleElement(event) {
106
+ const target = this.target;
107
+ const eventTarget = (event.target);
108
+ return target && target != eventTarget && target.isSameNode && !(target.isSameNode(eventTarget) || target.contains(eventTarget));
109
+ }
110
+ isOutsideClicked(event) {
111
+ if (this.props.outsideClickLogic === "elemental") {
112
+ const target = event.target;
113
+ return this.props.dismissOnClick || (this.internalElement && !(this.internalElement.isSameNode(target) || this.internalElement.contains(target)));
114
+ }
115
+ const { x, y } = event;
116
+ const hostRect = this.internalElement.getBoundingClientRect();
117
+ const hostX = Math.min(hostRect.x, this.hostRectBeforeRerendering.x ?? 0);
118
+ const hostY = Math.min(hostRect.y, this.hostRectBeforeRerendering.y ?? 0);
119
+ const hostWidth = Math.max(hostRect.width, this.hostRectBeforeRerendering.width ?? 0);
120
+ const hostHeight = Math.max(hostRect.height, this.hostRectBeforeRerendering.height ?? 0);
121
+ const hostAbsoluteWidth = hostX + hostWidth;
122
+ const hostAbsoluteHeight = hostY + hostHeight;
123
+ const isOutSide = !((x >= hostX && x <= hostAbsoluteWidth) && (y >= hostY && y <= hostAbsoluteHeight));
124
+ this.hostRectBeforeRerendering = this.internalElement.getBoundingClientRect();
125
+ return isOutSide;
126
+ }
127
+ bindDocumentClickListener() {
128
+ if (this.documentClickListener || this.props.notDismissable)
129
+ return;
130
+ const elementToReceiveOpenFocus = this.props.onOpenFocusRef?.current;
131
+ this.documentClickListener = (event) => {
132
+ if (!this.state.sticky && this.isNotToggleElement(event) && this.isOutsideClicked(event)) {
133
+ if (this.props.trapFocus && TypeChecker.isFunction(elementToReceiveOpenFocus?.focus)) {
134
+ elementToReceiveOpenFocus.focus();
135
+ return;
136
+ }
137
+ if (this.props.sticky)
138
+ return;
139
+ this.hidePopover();
140
+ }
141
+ };
142
+ document.addEventListener('click', this.documentClickListener);
143
+ }
144
+ unbindDocumentClickListener() {
145
+ if (!this.documentClickListener)
146
+ return;
147
+ document.removeEventListener('click', this.documentClickListener);
148
+ this.documentClickListener = undefined;
149
+ }
150
+ bindScrollListener() {
151
+ if (this.documentScrollHandler || !this.target)
152
+ return;
153
+ this.documentScrollHandler = ScrollHandler.handle(this.target, this.rePosition);
154
+ this.documentScrollHandler.attach();
155
+ }
156
+ unbindScrollListener() {
157
+ if (!this.documentScrollHandler)
158
+ return;
159
+ this.documentScrollHandler.detach();
160
+ this.documentScrollHandler = undefined;
161
+ }
162
+ bindResizeListener() {
163
+ if (this.windowResizeListener)
164
+ return;
165
+ this.windowResizeListener = (_) => {
166
+ if (this.state.visible)
167
+ this.hidePopover(); // is android or ios
168
+ };
169
+ window.addEventListener('resize', this.windowResizeListener);
170
+ }
171
+ unbindResizeListener() {
172
+ if (!this.windowResizeListener)
173
+ return;
174
+ window.removeEventListener('resize', this.windowResizeListener);
175
+ this.windowResizeListener = undefined;
176
+ }
177
+ rePosition(event, cb) {
178
+ if (!this.state.visible)
179
+ return;
180
+ this.target = event?.target ?? event?.currentTarget ?? this.target;
181
+ if (!this.target || !this.target.getBoundingClientRect)
182
+ return;
183
+ DOMHelper.absolutePositionRelatively(this.internalElement, this.target, this.props.positional);
184
+ const targetOffset = DOMHelper.getElementOffset(this.target);
185
+ const popoverOffset = DOMHelper.getElementOffset(this.internalElement);
186
+ if (targetOffset.top > popoverOffset.top) {
187
+ DOMHelper.addClass(this.internalElement, 'noseur-popover-arrow-flipped');
188
+ }
189
+ else {
190
+ if (DOMHelper.hasClass(this.internalElement, "noseur-popover-arrow-flipped")) {
191
+ DOMHelper.removeClass(this.internalElement, "noseur-popover-arrow-flipped");
192
+ }
193
+ }
194
+ cb && cb();
195
+ }
196
+ matchTargetSizeCb(_, styleValue, __, targetComputedStyle) {
197
+ const sidePaddingSum = DOMHelper.sanitizeStyleValue(targetComputedStyle.paddingLeft) + DOMHelper.sanitizeStyleValue(targetComputedStyle.paddingRight);
198
+ return (DOMHelper.sanitizeStyleValue(styleValue) - sidePaddingSum) + "px";
199
+ }
200
+ resolvePopoverStyle() {
201
+ if (!this.target || !this.target?.getBoundingClientRect)
202
+ return;
203
+ if (this.props.matchTargetSize) {
204
+ DOMHelper.matchStyles(this.target, [this.internalElement], ["width"], this.matchTargetSizeCb);
205
+ }
206
+ DOMHelper.absolutePositionRelatively(this.internalElement, this.target, this.props.positional);
207
+ if (!this.props.pointingArrowClassName?.startsWith("noseur-popover-arrow"))
208
+ return;
209
+ const targetOffset = DOMHelper.getElementOffset(this.target);
210
+ const popoverOffset = DOMHelper.getElementOffset(this.internalElement);
211
+ if (TypeChecker.isString(targetOffset.left) || TypeChecker.isString(popoverOffset.left))
212
+ return;
213
+ if (targetOffset.left > popoverOffset.left) {
214
+ const arrowLeftOffset = targetOffset.left - popoverOffset.left - 10;
215
+ this.internalElement.style.setProperty('--popoverArrowLeftOffset', `${arrowLeftOffset}px`);
216
+ }
217
+ if (targetOffset.top > popoverOffset.top) {
218
+ DOMHelper.addClass(this.internalElement, 'noseur-popover-arrow-flipped');
219
+ }
220
+ }
221
+ renderChildren() {
222
+ const transition = this.props.transition;
223
+ let className = Classname.build('noseur-popover', this.props.pointingArrowClassName, this.props.className);
224
+ const props = {
225
+ className,
226
+ id: this.props.id,
227
+ key: this.props.key,
228
+ style: this.props.style,
229
+ };
230
+ const ref = (el) => {
231
+ this.internalElement = el;
232
+ ObjectHelper.resolveRef(this.props.forwardRef, el);
233
+ ObjectHelper.resolveRef(this.transitionNodeRef, el);
234
+ if (el && !this.hostRectBeforeRerendering.width)
235
+ this.hostRectBeforeRerendering = this.internalElement.getBoundingClientRect();
236
+ };
237
+ return (React.createElement(CSSTransition, { classNames: transition, timeout: this.props.transitionTimeout, in: this.state.visible, options: this.props.transitionOptions, unmountOnExit: true, onEnter: this.onEnter, onEntered: this.onEntered, onExit: this.onExit, onExited: this.onExited, nodeRef: this.transitionNodeRef },
238
+ React.createElement("div", { ref: ref, ...props }, this.props.children)));
239
+ }
240
+ render() {
241
+ const children = this.renderChildren();
242
+ return React.createElement(Portal, { children: children, container: this.props.container, visible: true });
243
+ }
244
+ }
245
+ export const Popover = ({ ref, ...props }) => (React.createElement(PopoverComponent, { ...props, forwardRef: ref }));
@@ -0,0 +1,11 @@
1
+ import "./Overlay.css";
2
+ import React from 'react';
3
+ import { ComponentBaseProps } from "../core/ComponentBaseProps";
4
+ import { NoseurElement, NoseurRawElement } from '../constants/Types';
5
+ export type PortalContainerEvent = () => NoseurRawElement;
6
+ export interface PortalProps extends ComponentBaseProps<HTMLElement> {
7
+ visible: boolean;
8
+ children: NoseurElement;
9
+ container: PortalContainerEvent | NoseurRawElement;
10
+ }
11
+ export declare const Portal: ({ ref, ...props }: Partial<PortalProps>) => React.JSX.Element;
@@ -0,0 +1,29 @@
1
+ import "./Overlay.css";
2
+ import React from 'react';
3
+ import ReactDOM from 'react-dom';
4
+ class PortalComponent extends React.Component {
5
+ static defaultProps = {
6
+ visible: false
7
+ };
8
+ state = {
9
+ renderChild: false
10
+ };
11
+ constructor(props) {
12
+ super(props);
13
+ }
14
+ isDOMReady() {
15
+ return (typeof window !== 'undefined' && window.document && window.document.createElement);
16
+ }
17
+ componentDidMount() {
18
+ if (this.isDOMReady() && this.props.visible && !this.state.renderChild) {
19
+ this.setState({ renderChild: true });
20
+ }
21
+ }
22
+ render() {
23
+ if (!(this.props.children && this.state.renderChild))
24
+ return null;
25
+ const container = this.props.container ? ((this.props.container instanceof Function) ? this.props.container() : this.props.container) : document.body;
26
+ return ReactDOM.createPortal(this.props.children, container);
27
+ }
28
+ }
29
+ export const Portal = ({ ref, ...props }) => (React.createElement(PortalComponent, { ...props, forwardRef: ref }));
@@ -0,0 +1,49 @@
1
+ import "./Overlay.css";
2
+ import React from 'react';
3
+ import { Orientation } from "../constants/Orientation";
4
+ import { Alignment } from "../constants/Alignment";
5
+ import { ComponentBaseProps } from "../core/ComponentBaseProps";
6
+ import { NoseurRawElement } from "../constants/Types";
7
+ import { MessageProps } from "./Message";
8
+ declare enum ToastType {
9
+ TOAST = "toast",
10
+ TOASTER = "toaster",
11
+ MESSAGES = "messages"
12
+ }
13
+ export type ToastMessageEventHandler = (message?: Partial<MessageProps>, key?: string) => void;
14
+ export type ToastAttributesRelays = {};
15
+ export interface ToastManageRef {
16
+ clear: () => void;
17
+ reverse: () => void;
18
+ changeLimit: (limit: number) => void;
19
+ remove: (key: string | string[]) => void;
20
+ changePosition: (position: Alignment) => void;
21
+ changeOrientation: (orientation: Orientation) => void;
22
+ show: (message: Partial<MessageProps> | Partial<MessageProps>[], key?: string | string[]) => string[];
23
+ update: (key: string | string[], message: Partial<MessageProps> | Partial<MessageProps>[], borrowLifetime?: boolean) => void;
24
+ }
25
+ export interface ToastProps extends ComponentBaseProps<HTMLDivElement, ToastManageRef, ToastAttributesRelays> {
26
+ limit: number;
27
+ reverse: boolean;
28
+ position: Alignment;
29
+ orientation: Orientation;
30
+ container: NoseurRawElement;
31
+ positionProps: {
32
+ relative?: boolean;
33
+ } & Partial<ComponentBaseProps<HTMLDivElement>>;
34
+ onAction: ToastMessageEventHandler;
35
+ onRemove: ToastMessageEventHandler;
36
+ __type__: ToastType;
37
+ }
38
+ interface ToasterInterfaceRelay {
39
+ destroy: () => void;
40
+ init: (props: Partial<ToastProps>, ref?: any) => string[] | void;
41
+ toast: (message: Partial<MessageProps> | Partial<MessageProps>[], key?: string | string[]) => string[];
42
+ }
43
+ export type ToasterInterface = ToasterInterfaceRelay & ToastManageRef;
44
+ export type MessagesProps = ToastProps;
45
+ export type MessagesManageRef = ToastManageRef;
46
+ export declare const Toast: ({ ref, ...props }: Partial<ToastProps>) => React.JSX.Element;
47
+ export declare const Messages: ({ ref, ...props }: Partial<ToastProps>) => React.JSX.Element;
48
+ export declare const Toaster: ToasterInterface;
49
+ export {};
@@ -0,0 +1,341 @@
1
+ import "./Overlay.css";
2
+ import React from 'react';
3
+ import { Portal } from "./Portal";
4
+ import ReactDOM from "react-dom/client";
5
+ import { DOMHelper } from "../utils/DOMUtils";
6
+ import { Classname } from "../utils/Classname";
7
+ import { TypeChecker } from "../utils/TypeChecker";
8
+ import { ObjectHelper } from "../utils/ObjectHelper";
9
+ import { Orientation } from "../constants/Orientation";
10
+ import { Alignment, Position } from "../constants/Alignment";
11
+ import { Message } from "./Message";
12
+ var ToastType;
13
+ (function (ToastType) {
14
+ ToastType["TOAST"] = "toast";
15
+ ToastType["TOASTER"] = "toaster";
16
+ ToastType["MESSAGES"] = "messages";
17
+ })(ToastType || (ToastType = {}));
18
+ class ToastComponent extends React.Component {
19
+ static defaultProps = {
20
+ reverse: false,
21
+ position: Position.TOP_RIGHT,
22
+ };
23
+ state = {
24
+ breads: {},
25
+ reverse: this.props.reverse,
26
+ position: this.props.position,
27
+ breadsLimit: this.props.limit,
28
+ orientation: this.props.orientation,
29
+ };
30
+ synchronizationIndex = 0;
31
+ positionElementsMap = {};
32
+ synchronizer = {}; // to ensure messages are shown regardless of react setState asyncronous property
33
+ constructor(props) {
34
+ super(props);
35
+ this.showMessage = this.showMessage.bind(this);
36
+ this.actOnMessage = this.actOnMessage.bind(this);
37
+ this.removeMessage = this.removeMessage.bind(this);
38
+ this.clearMessages = this.clearMessages.bind(this);
39
+ this.updateMessage = this.updateMessage.bind(this);
40
+ this.renderMessages = this.renderMessages.bind(this);
41
+ this.operateOnMessageByKeys = this.operateOnMessageByKeys.bind(this);
42
+ }
43
+ componentDidMount() {
44
+ ObjectHelper.resolveManageRef(this, {
45
+ show: this.showMessage,
46
+ clear: this.clearMessages,
47
+ update: this.updateMessage,
48
+ remove: this.removeMessage,
49
+ reverse: () => this.setState({ reverse: !this.state.reverse }),
50
+ changePosition: (position) => this.setState({ position }),
51
+ changeLimit: (limit) => this.setState({ breadsLimit: limit }),
52
+ changeOrientation: (orientation) => this.setState({ orientation }),
53
+ });
54
+ }
55
+ showMessage(message, key, synchronizationKey_) {
56
+ // start synchronization
57
+ const synchronizerKeys = Object.keys(this.synchronizer);
58
+ const synchronizationKey = `${synchronizationKey_ ?? this.synchronizationIndex++}`;
59
+ if (!synchronizerKeys.includes(synchronizationKey) && synchronizerKeys.length) {
60
+ this.synchronizer[synchronizationKey] = () => this.showMessage(message, key, synchronizationKey);
61
+ return [];
62
+ }
63
+ // end synchronization
64
+ const stateBreads = ObjectHelper.clone(this.state.breads);
65
+ let keys = (key && key instanceof Array) ? key : (key ? [key] : []);
66
+ const breads = message instanceof Array ? message : (message ? [message] : []);
67
+ if (keys.length && keys.length != breads.length) {
68
+ throw new Error("The toast keys count must match the count of the messages");
69
+ }
70
+ const position = this.state.position;
71
+ const newBreads = { ...stateBreads };
72
+ if (!keys.length)
73
+ keys = breads.map((message) => (message.key ?? DOMHelper.uniqueElementId("msg_")));
74
+ const positionBreads = breads.reduce((acc, bread, index) => {
75
+ bread.key = keys[index];
76
+ acc[bread.key] = bread;
77
+ return acc;
78
+ }, {});
79
+ newBreads[position] = {
80
+ ...stateBreads[position],
81
+ ...positionBreads,
82
+ };
83
+ this.synchronizer[synchronizationKey] = () => delete this.synchronizer[synchronizationKey];
84
+ this.setState({ breads: newBreads }, () => {
85
+ const desyncFun = (synchronizationKey && synchronizationKey in this.synchronizer) ? this.synchronizer[synchronizationKey] : undefined;
86
+ desyncFun && desyncFun();
87
+ const synchronizerKeys = Object.keys(this.synchronizer);
88
+ if (synchronizerKeys.length) {
89
+ const nextFire = this.synchronizer[synchronizerKeys[0]];
90
+ nextFire();
91
+ }
92
+ else {
93
+ this.synchronizationIndex = 0;
94
+ }
95
+ this.renderMessages(position, this.state.breads[position]);
96
+ });
97
+ return keys;
98
+ }
99
+ operateOnMessageByKeys(keys, operation) {
100
+ if (!keys.length)
101
+ return;
102
+ const stateBreads = this.state.breads;
103
+ const positions = Object.keys(this.state.breads);
104
+ positions.forEach((position) => {
105
+ const positionalBreads = stateBreads[position];
106
+ const positionals = Object.keys(positionalBreads);
107
+ positionals.forEach((positional) => {
108
+ const message = positionalBreads[positional];
109
+ if (!message || !keys.includes(message.key))
110
+ return;
111
+ operation(message.key, message);
112
+ });
113
+ });
114
+ }
115
+ actOnMessage(key, message, action) {
116
+ if (!message.__manage_ref__) {
117
+ console.warn("noseur - toast/messages: gabbage collection failed, likely a memory leak, ", key);
118
+ return;
119
+ }
120
+ action(message.__manage_ref__);
121
+ }
122
+ removeMessage(key) {
123
+ let keys = (key && key instanceof Array) ? key : (key ? [key] : []);
124
+ this.operateOnMessageByKeys(keys, (key, message) => {
125
+ this.actOnMessage(key, message, (manageRef) => manageRef.close());
126
+ });
127
+ }
128
+ updateMessage(key, message, borrowLifetime = true) {
129
+ let keys = (key && key instanceof Array) ? key : (key ? [key] : []);
130
+ const breads = message instanceof Array ? message : (message ? [message] : []);
131
+ if (keys.length && keys.length != breads.length) {
132
+ throw new Error("The toast keys count must match the count of the messages");
133
+ }
134
+ this.operateOnMessageByKeys(keys, (key, message) => {
135
+ const index = keys.indexOf(key);
136
+ this.actOnMessage(key, message, (manageRef) => manageRef.update(breads[index], borrowLifetime));
137
+ });
138
+ }
139
+ clearMessages() {
140
+ Object.keys(this.state.breads).forEach(key => {
141
+ const positionBreads = this.state.breads[key];
142
+ Object.keys(positionBreads).forEach(skey => {
143
+ this.actOnMessage(skey, positionBreads[skey], (manageRef) => manageRef.close());
144
+ });
145
+ });
146
+ }
147
+ renderMessage(position, positionBreads, key) {
148
+ const message = positionBreads[key];
149
+ const cachedOnAction = message?.onAction;
150
+ const cachedOnExitDOM = message?.onExitDOM;
151
+ const cachedManageRef = message?.manageRef;
152
+ return React.createElement(Message, ({
153
+ ...message,
154
+ manageRef: (r) => {
155
+ if (r && cachedManageRef)
156
+ ObjectHelper.resolveRef(cachedManageRef, r);
157
+ message.__manage_ref__ = r;
158
+ },
159
+ onAction: () => {
160
+ if (cachedOnAction)
161
+ cachedOnAction();
162
+ this.props.onAction && this.props.onAction(message, key);
163
+ },
164
+ onExitDOM: () => {
165
+ delete positionBreads[key];
166
+ const breads = this.state.breads;
167
+ breads[position] = positionBreads;
168
+ if (cachedOnExitDOM)
169
+ cachedOnExitDOM();
170
+ this.props.onRemove && this.props.onRemove(message, key);
171
+ this.setState({ breads }, () => {
172
+ if (!(position in this.state.breads))
173
+ return;
174
+ const positionBreads = this.state.breads[position] || {};
175
+ if (Object.keys(positionBreads).length) {
176
+ this.renderMessages(position, positionBreads);
177
+ return;
178
+ }
179
+ delete breads[position];
180
+ this.setState({ breads }, () => {
181
+ if (position in this.positionElementsMap) {
182
+ delete this.positionElementsMap[position];
183
+ }
184
+ });
185
+ });
186
+ }
187
+ }));
188
+ }
189
+ renderMessages(position, positionBreads) {
190
+ const root = this.positionElementsMap[position];
191
+ if (!root)
192
+ return;
193
+ let keys = Object.keys(positionBreads);
194
+ if (TypeChecker.isNumber(this.state.breadsLimit) && this.state.breadsLimit)
195
+ keys = keys.slice(0, this.state.breadsLimit);
196
+ const messages = keys.map((key) => this.renderMessage(position, positionBreads, key));
197
+ root.render(this.state.reverse ? messages.reverse() : messages);
198
+ }
199
+ buildPositionRoot(type, position) {
200
+ const style = {};
201
+ if (this.state.orientation === Orientation.HORIZONTAL) {
202
+ style["flexDirection"] = "row";
203
+ if (position === Alignment.TOP || position === Alignment.BOTTOM || position.includes("center")) {
204
+ style["--messageMarginLeft"] = "0.25em";
205
+ style["--messageMarginRight"] = "0.25em";
206
+ }
207
+ else if (position.includes("left")) {
208
+ style["--messageMarginRight"] = "0.5em";
209
+ }
210
+ else if (position.includes("right")) {
211
+ style["--messageMarginLeft"] = "0.5em";
212
+ }
213
+ }
214
+ else {
215
+ if (position.includes("top")) {
216
+ style["--messageMarginBottom"] = "0.5em";
217
+ }
218
+ else if (position.includes("bottom")) {
219
+ style["--messageMarginTop"] = "0.5em";
220
+ }
221
+ else if (position === Alignment.LEFT || position === Alignment.RIGHT || position.includes("center")) {
222
+ style["--messageMarginTop"] = "0.25em";
223
+ style["--messageMarginBottom"] = "0.25em";
224
+ }
225
+ }
226
+ if (this.props.positionProps?.relative) {
227
+ style.position = "relative";
228
+ }
229
+ return (React.createElement("div", { key: position, className: Classname.build(`noseur-${type}-${position}`, this.props.positionProps?.className), id: this.props.positionProps?.id, style: ObjectHelper.merge(style, this.props.positionProps?.style ?? {}), ref: (r) => {
230
+ if (!(position in this.positionElementsMap)) {
231
+ this.positionElementsMap[position] = ReactDOM.createRoot(r);
232
+ this.renderMessages(position, this.state.breads[position]);
233
+ }
234
+ } }));
235
+ }
236
+ render() {
237
+ const breadsKeys = Object.keys(this.state.breads);
238
+ if (!breadsKeys.length && this.props.__type__ === ToastType.TOAST)
239
+ return null;
240
+ const type = (this.props.__type__ === ToastType.TOASTER) ? ToastType.TOAST : this.props.__type__;
241
+ const className = Classname.build(`noseur-${type}`, this.props.className);
242
+ const positionElements = breadsKeys.map((position) => this.buildPositionRoot(type, position));
243
+ const content = (React.createElement("div", { ref: this.props.forwardRef, className: className, id: this.props.id, style: this.props.style }, positionElements));
244
+ return (!this.props.container ? content : React.createElement(Portal, { children: content, container: this.props.container, visible: true }));
245
+ }
246
+ }
247
+ ;
248
+ export const Toast = ({ ref, ...props }) => (React.createElement(ToastComponent, { ...props, forwardRef: ref, __type__: ToastType.TOAST, container: document.body }));
249
+ export const Messages = ({ ref, ...props }) => (React.createElement(ToastComponent, { ...props, forwardRef: ref, __type__: ToastType.MESSAGES }));
250
+ let noseurInternalGlobalToasterRoot;
251
+ let noseurInternalGlobalToasterManageRef;
252
+ let noseurInternalGlobalToasterCallStack = [];
253
+ export const Toaster = {
254
+ init: (props, onMount, ref) => {
255
+ if (noseurInternalGlobalToasterRoot) {
256
+ if (!noseurInternalGlobalToasterManageRef && onMount) {
257
+ noseurInternalGlobalToasterCallStack.push(onMount);
258
+ }
259
+ return;
260
+ }
261
+ let result = null;
262
+ const ___noseur_toaster___ = React.createElement(ToastComponent, {
263
+ ...props,
264
+ container: document.body,
265
+ __type__: ToastType.TOASTER,
266
+ forwardRef: ref ?? props.ref,
267
+ manageRef: (r) => {
268
+ noseurInternalGlobalToasterManageRef = r;
269
+ onMount && onMount();
270
+ for (const fun of (noseurInternalGlobalToasterCallStack ?? [])) {
271
+ let sresult = fun();
272
+ if (sresult)
273
+ result = sresult;
274
+ }
275
+ noseurInternalGlobalToasterCallStack = null;
276
+ },
277
+ });
278
+ const wrapper = document.createDocumentFragment();
279
+ DOMHelper.appendChild(wrapper, document.body);
280
+ noseurInternalGlobalToasterRoot = ReactDOM.createRoot(wrapper);
281
+ noseurInternalGlobalToasterRoot.render(___noseur_toaster___);
282
+ if (result)
283
+ return result;
284
+ },
285
+ destroy: () => {
286
+ if (!noseurInternalGlobalToasterRoot)
287
+ return;
288
+ noseurInternalGlobalToasterRoot.unmount();
289
+ noseurInternalGlobalToasterRoot = null;
290
+ },
291
+ toast: (message, key) => {
292
+ return Toaster.show(message, key);
293
+ },
294
+ clear: () => {
295
+ if (!noseurInternalGlobalToasterRoot || !noseurInternalGlobalToasterManageRef)
296
+ Toaster.init({});
297
+ noseurInternalGlobalToasterManageRef?.clear();
298
+ },
299
+ reverse: () => {
300
+ if (!noseurInternalGlobalToasterRoot || !noseurInternalGlobalToasterManageRef) {
301
+ Toaster.init({}, Toaster.reverse());
302
+ }
303
+ noseurInternalGlobalToasterManageRef?.reverse();
304
+ },
305
+ changeLimit: (limit) => {
306
+ if (!noseurInternalGlobalToasterRoot || !noseurInternalGlobalToasterManageRef) {
307
+ Toaster.init({}, Toaster.changeLimit(limit));
308
+ return;
309
+ }
310
+ noseurInternalGlobalToasterManageRef?.changeLimit(limit);
311
+ },
312
+ remove: (key) => {
313
+ if (!noseurInternalGlobalToasterRoot || !noseurInternalGlobalToasterManageRef)
314
+ Toaster.init({});
315
+ noseurInternalGlobalToasterManageRef?.remove(key);
316
+ },
317
+ changePosition: (position) => {
318
+ if (!noseurInternalGlobalToasterRoot || !noseurInternalGlobalToasterManageRef) {
319
+ return Toaster.init({}, Toaster.changePosition(position));
320
+ }
321
+ noseurInternalGlobalToasterManageRef?.changePosition(position);
322
+ },
323
+ changeOrientation: (orientation) => {
324
+ if (!noseurInternalGlobalToasterRoot || !noseurInternalGlobalToasterManageRef) {
325
+ return Toaster.init({}, Toaster.changeOrientation(orientation));
326
+ }
327
+ noseurInternalGlobalToasterManageRef?.changeOrientation(orientation);
328
+ },
329
+ show: (message, key) => {
330
+ if (!noseurInternalGlobalToasterRoot || !noseurInternalGlobalToasterManageRef) {
331
+ return Toaster.init({}, () => Toaster.show(message, key));
332
+ }
333
+ return noseurInternalGlobalToasterManageRef?.show(message, key);
334
+ },
335
+ update: (key, message, borrowLifetime) => {
336
+ if (!noseurInternalGlobalToasterRoot || !noseurInternalGlobalToasterManageRef) {
337
+ return Toaster.init({}, () => Toaster.update(key, message, borrowLifetime));
338
+ }
339
+ noseurInternalGlobalToasterManageRef?.update(key, message, borrowLifetime);
340
+ },
341
+ };