cx 26.4.2 → 26.4.3

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 (38) hide show
  1. package/build/jsx-dev-runtime.d.ts +1 -0
  2. package/build/jsx-dev-runtime.d.ts.map +1 -1
  3. package/build/jsx-dev-runtime.js +1 -0
  4. package/dist/manifest.js +889 -889
  5. package/package.json +1 -1
  6. package/src/charts/BarGraph.scss +31 -31
  7. package/src/charts/Legend.scss +57 -57
  8. package/src/charts/LegendEntry.scss +35 -35
  9. package/src/charts/LineGraph.scss +28 -28
  10. package/src/charts/helpers/SnapPointFinder.ts +136 -136
  11. package/src/charts/helpers/ValueAtFinder.ts +72 -72
  12. package/src/data/createAccessorModelProxy.ts +66 -66
  13. package/src/jsx-dev-runtime.ts +1 -0
  14. package/src/ui/DataProxy.ts +55 -55
  15. package/src/ui/Repeater.spec.tsx +181 -181
  16. package/src/ui/Rescope.ts +50 -50
  17. package/src/ui/adapter/ArrayAdapter.ts +229 -229
  18. package/src/ui/exprHelpers.ts +96 -96
  19. package/src/util/scss/include.scss +69 -69
  20. package/src/widgets/Button.maps.scss +103 -103
  21. package/src/widgets/Sandbox.ts +104 -104
  22. package/src/widgets/form/Calendar.tsx +772 -772
  23. package/src/widgets/form/ColorField.scss +112 -112
  24. package/src/widgets/form/DateTimeField.scss +111 -111
  25. package/src/widgets/form/LookupField.maps.scss +26 -26
  26. package/src/widgets/form/LookupField.scss +227 -227
  27. package/src/widgets/form/MonthField.scss +113 -113
  28. package/src/widgets/form/NumberField.scss +72 -72
  29. package/src/widgets/form/Select.scss +104 -104
  30. package/src/widgets/form/TextField.scss +66 -66
  31. package/src/widgets/grid/Grid.scss +657 -657
  32. package/src/widgets/grid/variables.scss +47 -47
  33. package/src/widgets/index.ts +63 -63
  34. package/src/widgets/nav/MenuItem.scss +150 -150
  35. package/src/widgets/nav/MenuItem.tsx +525 -525
  36. package/src/widgets/nav/Tab.ts +122 -122
  37. package/src/widgets/overlay/Overlay.tsx +1029 -1029
  38. package/src/widgets/variables.scss +61 -61
@@ -1,1029 +1,1029 @@
1
- /** @jsxImportSource react */
2
- import type { Root } from "cx-react";
3
- import { isBinding, isBindingObject } from "../../data/Binding";
4
- import { Store } from "../../data/Store";
5
- import { View } from "../../data/View";
6
- import { startAppLoop } from "../../ui/app/startAppLoop";
7
- import { ContainerBase, StyledContainerConfig } from "../../ui/Container";
8
- import { FocusManager, offFocusOut, oneFocusOut } from "../../ui/FocusManager";
9
- import { Instance } from "../../ui/Instance";
10
- import { BooleanProp, NumberProp } from "../../ui/Prop";
11
- import { RenderingContext } from "../../ui/RenderingContext";
12
- import { VDOM, Widget } from "../../ui/Widget";
13
- import { ZIndexManager } from "../../ui/ZIndexManager";
14
- import { addEventListenerWithOptions } from "../../util/addEventListenerWithOptions";
15
- import { closest, isSelfOrDescendant } from "../../util/DOM";
16
- import { getActiveElement } from "../../util/getActiveElement";
17
- import { getTopLevelBoundingClientRect } from "../../util/getTopLevelBoundingClientRect";
18
- import { isDataRecord } from "../../util/isDataRecord";
19
- import { isNumber } from "../../util/isNumber";
20
- import { KeyCode } from "../../util/KeyCode";
21
- import { parseStyle } from "../../util/parseStyle";
22
- import { SubscriberList } from "../../util/SubscriberList";
23
- import { ddDetect, ddMouseDown, ddMouseUp } from "../drag-drop/ops";
24
- import { captureMouseOrTouch, getCursorPos } from "./captureMouse";
25
-
26
- /*
27
- Features:
28
- - renders itself on top of other elements
29
- - provide resizing capabilities
30
- - adds positioning hook and ability to position itself in the center of the page
31
- - provides header, body, and footer elements and updates body's height on resize (move this to Window)
32
- - stop mouse events from bubbling to parents, but allow keystrokes
33
- */
34
-
35
- export interface OverlayConfig extends StyledContainerConfig {
36
- /** Set to `true` to enable resizing. */
37
- resizable?: BooleanProp;
38
-
39
- /** Set to `true` to enable dragging the overlay. */
40
- draggable?: BooleanProp;
41
-
42
- /** Base CSS class to be applied to the field. Defaults to `overlay`. */
43
- baseClass?: string;
44
-
45
- /** Width of resize handle area. */
46
- resizeWidth?: number;
47
-
48
- /** Set to `true` to initially place the overlay in the center of the page. */
49
- center?: boolean;
50
-
51
- /** Set to `true` to initially place the overlay in the center of the page horizontally. */
52
- centerX?: boolean;
53
-
54
- /** Set to `true` to initially place the overlay in the center of the page vertically. */
55
- centerY?: boolean;
56
-
57
- /** Set to `true` to add a modal backdrop which masks mouse events for the rest of the page. */
58
- modal?: boolean;
59
-
60
- /** Set to `true` to add a modal backdrop which will dismiss the window when clicked. */
61
- backdrop?: boolean;
62
-
63
- /** Set to `true` to force the element to be rendered inline, instead of being appended to the body element.
64
- * Inline overlays have z-index set to a very high value, to ensure they are displayed on top of the other content. */
65
- inline?: boolean;
66
-
67
- /** Set to `true` to automatically focus the top level overlay element. */
68
- autoFocus?: boolean;
69
-
70
- /** Set to `true` to automatically focus the first focusable child in the overlay. */
71
- autoFocusFirstChild?: boolean;
72
-
73
- /** Set to `true` to append the set animate state after the initial render. Appended CSS class may be used to add show/hide animations. */
74
- animate?: boolean;
75
-
76
- /** Number of milliseconds to wait, before removing the element from the DOM. Used in combination with the animate property. */
77
- destroyDelay?: number;
78
-
79
- /** Automatically dismiss overlay if it loses focus. */
80
- dismissOnFocusOut?: boolean;
81
-
82
- /** Set to true to make the top level overlay element focusable. */
83
- focusable?: boolean;
84
-
85
- /** Set to `true` to dismiss the window if the user presses the back button in the browser. */
86
- dismissOnPopState?: boolean;
87
-
88
- /** A callback function which fires while the overlay is being moved around. */
89
- onMove?: string | ((e: Event, instance: Instance, component: any) => void);
90
-
91
- /** A callback function which fires while the overlay is being resized. */
92
- onResize?: string | ((e: Event, instance: Instance, component: any) => void);
93
-
94
- /** zIndex */
95
- zIndex?: NumberProp;
96
-
97
- /** Set to `true` to make the window automatically close if Esc is pressed on the keyboard. Default value is false.*/
98
- closeOnEscape?: boolean;
99
-
100
- /** Custom CSS styling for the container element. */
101
- containerStyle?: string;
102
-
103
- /** Callback for focus out event. */
104
- onFocusOut?: string;
105
-
106
- /** Callback for mouse enter event. */
107
- onMouseEnter?: string;
108
-
109
- /** Callback for mouse leave event. */
110
- onMouseLeave?: string;
111
-
112
- /** Callback for backdrop click. */
113
- onBackdropClick?: string;
114
-
115
- /** Callback for mouse down event. */
116
- onMouseDown?: string;
117
-
118
- /** Callback for key down event. */
119
- onKeyDown?: string;
120
-
121
- /** Callback fired before dismiss. */
122
- onBeforeDismiss?: string | (() => boolean);
123
-
124
- /** Callback fired when overlay will dismiss. */
125
- overlayWillDismiss?: (instance: Instance, component: any) => boolean;
126
-
127
- /** Callback for click event. */
128
- onClick?: string;
129
- }
130
-
131
- export interface OverlayOpenOptions {
132
- initiatingEvent?: React.SyntheticEvent;
133
- name?: string;
134
- dismiss?: () => void;
135
- parentEl?: Element;
136
- subscribeToBeforeDismiss?: (callback: () => boolean) => void;
137
- destroyDelay?: number;
138
- removeParentDOMElement?: boolean;
139
- }
140
-
141
- export interface ConfigureOverlayContainerContext {
142
- relatedElement?: HTMLElement | null;
143
- initiatingEvent?: React.SyntheticEvent;
144
- }
145
-
146
- export class OverlayInstance<WidgetType extends OverlayBase<any, any> = Overlay> extends Instance<WidgetType> {
147
- declare positionChangeSubscribers: SubscriberList;
148
- declare dismiss?: () => void;
149
- onBeforeDismiss?: () => boolean;
150
- declare beaconEl?: HTMLElement | null;
151
- }
152
-
153
- export class OverlayBase<
154
- Config extends OverlayConfig = OverlayConfig,
155
- InstanceType extends OverlayInstance<any> = OverlayInstance<any>,
156
- > extends ContainerBase<Config, InstanceType> {
157
- static configureOverlayContainer?: (containerEl: HTMLElement, context: ConfigureOverlayContainerContext) => void;
158
-
159
- // Properties declared here to support prototype assignments
160
- declare styled: true;
161
- declare baseClass: string;
162
- declare resizable?: BooleanProp;
163
- declare resizeWidth: number;
164
- declare center?: boolean;
165
- declare centerX?: boolean;
166
- declare centerY?: boolean;
167
- declare modal?: boolean;
168
- declare backdrop?: boolean;
169
- declare inline?: boolean;
170
- declare autoFocus?: boolean;
171
- declare autoFocusFirstChild?: boolean;
172
- declare animate?: boolean;
173
- declare draggable?: BooleanProp;
174
- declare destroyDelay?: number;
175
- declare dismissOnFocusOut?: boolean;
176
- declare focusable?: boolean;
177
- declare containerStyle?: string;
178
- declare dismissOnPopState?: boolean;
179
- declare closeOnEscape?: boolean;
180
- declare onFocusOut?: string;
181
- declare onMouseLeave?: string;
182
- declare onMouseEnter?: string;
183
- declare onKeyDown?: string;
184
- declare onMove?: string | ((e: Event, instance: Instance, component: any) => void);
185
- declare onResize?: string | ((e: Event, instance: Instance, component: any) => void);
186
- declare onClick?: string;
187
- declare onMouseDown?: string;
188
- declare onBackdropClick?: string;
189
- declare overlayWillDismiss?: (instance: Instance, component: any) => boolean;
190
- declare style?: any;
191
- declare pad?: boolean;
192
- declare needsBeacon: boolean;
193
-
194
- init() {
195
- if (this.center) this.centerX = this.centerY = this.center;
196
-
197
- super.init();
198
- }
199
-
200
- declareData(...args: any[]) {
201
- super.declareData(...args, {
202
- shadowStyle: {
203
- structured: true,
204
- },
205
- resizable: undefined,
206
- draggable: undefined,
207
- zIndex: undefined,
208
- });
209
- }
210
-
211
- prepareData(context: RenderingContext, instance: InstanceType): void {
212
- let { data } = instance;
213
- data.stateMods = {
214
- ...data.stateMods,
215
- inline: this.inline,
216
- modal: this.modal,
217
- pad: this.pad,
218
- resizable: data.resizable,
219
- draggable: data.draggable,
220
- animate: this.animate,
221
- shadow: this.modal || this.backdrop,
222
- };
223
-
224
- super.prepareData(context, instance);
225
- }
226
-
227
- initInstance(context: RenderingContext, instance: InstanceType): void {
228
- instance.positionChangeSubscribers = new SubscriberList();
229
- super.initInstance(context, instance);
230
- }
231
-
232
- explore(context: RenderingContext, instance: InstanceType): void {
233
- if (isBinding(this.visible)) {
234
- if (!instance.dismiss) {
235
- instance.dismiss = () => {
236
- if (instance.onBeforeDismiss && instance.onBeforeDismiss() === false) return;
237
- instance.set("visible", false);
238
- };
239
- }
240
- } else if (context.options.dismiss) instance.dismiss = context.options.dismiss;
241
-
242
- if (instance.dismiss) {
243
- context.push("parentOptions", {
244
- ...context.parentOptions,
245
- dismiss: instance.dismiss,
246
- });
247
- }
248
-
249
- if (instance.cache("dismiss", instance.dismiss)) instance.markShouldUpdate(context);
250
-
251
- context.push("parentPositionChangeEvent", instance.positionChangeSubscribers);
252
-
253
- super.explore(context, instance);
254
- }
255
-
256
- exploreCleanup(context: RenderingContext, instance: InstanceType): void {
257
- if (instance.dismiss) context.pop("parentOptions");
258
- context.pop("parentPositionChangeEvent");
259
- }
260
-
261
- render(context: RenderingContext, instance: InstanceType, key: string): any {
262
- if (this.needsBeacon)
263
- return (
264
- <OverlayBeacon
265
- key={key}
266
- childrenFactory={(beaconEl) => (
267
- <OverlayComponent
268
- beaconEl={beaconEl}
269
- instance={instance}
270
- subscribeToBeforeDismiss={context.options.subscribeToBeforeDismiss}
271
- parentEl={context.options.parentEl}
272
- >
273
- {this.renderContents(context, instance)}
274
- </OverlayComponent>
275
- )}
276
- />
277
- );
278
-
279
- return (
280
- <OverlayComponent
281
- key={key}
282
- beaconEl={null}
283
- instance={instance}
284
- subscribeToBeforeDismiss={context.options.subscribeToBeforeDismiss}
285
- parentEl={context.options.parentEl}
286
- >
287
- {this.renderContents(context, instance)}
288
- </OverlayComponent>
289
- );
290
- }
291
-
292
- renderContents(context: RenderingContext, instance: InstanceType): any {
293
- return this.renderChildren(context, instance);
294
- }
295
-
296
- getConfigureOverlayContainerContext(
297
- instance?: InstanceType,
298
- initiatingEvent?: React.SyntheticEvent,
299
- ): ConfigureOverlayContainerContext {
300
- return {
301
- initiatingEvent,
302
- relatedElement: instance?.beaconEl,
303
- };
304
- }
305
-
306
- overlayDidMount(instance: InstanceType, component: any): void {
307
- let { el } = component;
308
- if (this.centerX) if (!el.style.left) el.style.left = `${(window.innerWidth - el.offsetWidth) / 2}px`;
309
- if (this.centerY)
310
- if (!el.style.top) el.style.top = `${Math.max(0, (window.innerHeight - el.offsetHeight) / 2)}px`;
311
- }
312
-
313
- overlayDidUpdate(instance: InstanceType, component: any): void {}
314
-
315
- overlayWillUnmount(instance: InstanceType, component: any): void {}
316
-
317
- handleFocusOut(instance: InstanceType, component: any): void {
318
- if (this.onFocusOut) instance.invoke("onFocusOut", instance, component);
319
-
320
- if (this.dismissOnFocusOut && instance.dismiss) instance.dismiss();
321
- }
322
-
323
- handleKeyDown(e: any, instance: InstanceType, component?: any): void | false {
324
- if (this.onKeyDown && instance.invoke("onKeyDown", e, instance, component) === false) return false;
325
-
326
- if (this.closeOnEscape && e.keyCode == KeyCode.esc && instance.dismiss) {
327
- instance.dismiss();
328
- e.stopPropagation();
329
- }
330
- }
331
-
332
- handleMouseLeave(instance: InstanceType, component: any): void {
333
- if (this.onMouseLeave) instance.invoke("onMouseLeave", instance, component);
334
- }
335
-
336
- handleMouseEnter(instance: InstanceType, component: any): void {
337
- if (this.onMouseEnter) instance.invoke("onMouseEnter", instance, component);
338
- }
339
-
340
- getOverlayContainer(): HTMLElement {
341
- return document.body;
342
- }
343
-
344
- containerFactory(instance?: InstanceType, initiatingEvent?: React.SyntheticEvent): HTMLElement {
345
- let el = document.createElement("div");
346
- let container = this.getOverlayContainer();
347
- container.appendChild(el);
348
- el.style.position = "absolute";
349
- if (this.containerStyle) Object.assign(el.style, parseStyle(this.containerStyle));
350
-
351
- if (OverlayBase.configureOverlayContainer)
352
- OverlayBase.configureOverlayContainer(el, this.getConfigureOverlayContainerContext(instance, initiatingEvent));
353
-
354
- return el;
355
- }
356
-
357
- open(storeOrInstance?: View | Instance, options?: OverlayOpenOptions): () => void {
358
- if (!this.initialized) this.init();
359
-
360
- let el = this.containerFactory(undefined, options?.initiatingEvent);
361
- el.style.display = "hidden";
362
-
363
- let beforeDismiss: (() => boolean) | null = null;
364
- let stop: any;
365
-
366
- options = {
367
- destroyDelay: this.destroyDelay,
368
- removeParentDOMElement: true,
369
- ...options,
370
- parentEl: el,
371
- dismiss: () => {
372
- if (beforeDismiss && beforeDismiss() === false) return;
373
- stop();
374
- beforeDismiss = null;
375
- },
376
- subscribeToBeforeDismiss: (cb: () => boolean) => {
377
- beforeDismiss = cb;
378
- },
379
- };
380
- options.name = options.name || "overlay";
381
- stop = startAppLoop(el, storeOrInstance, this, options);
382
- return options.dismiss!;
383
- }
384
-
385
- handleMove(e: any, instance: InstanceType, component: any): void {
386
- let { widget } = instance;
387
- if (!widget.onMove || instance.invoke("onMove", e, instance, component) !== false) {
388
- instance.store.silently(() => {
389
- if (isDataRecord(this.style) && isBindingObject(this.style.top)) {
390
- instance.store.set(this.style.top.bind, component.el.style.top);
391
- }
392
-
393
- if (isDataRecord(this.style) && isBindingObject(this.style.left)) {
394
- instance.store.set(this.style.left.bind, component.el.style.left);
395
- }
396
- });
397
- }
398
- instance.positionChangeSubscribers.notify();
399
- }
400
-
401
- handleResize(e: any, instance: InstanceType, component: any): void {
402
- let { widget } = instance;
403
- if (!widget.onResize || instance.invoke("onResize", e, instance, component) !== false) {
404
- instance.store.silently(() => {
405
- if (isDataRecord(this.style) && isBindingObject(this.style.width)) {
406
- instance.store.set(this.style.width.bind, component.el.style.width);
407
- }
408
-
409
- if (isDataRecord(this.style) && isBindingObject(this.style.height)) {
410
- instance.store.set(this.style.height.bind, component.el.style.height);
411
- }
412
- });
413
- }
414
- instance.positionChangeSubscribers.notify();
415
- }
416
- }
417
-
418
- OverlayBase.prototype.styled = true;
419
- OverlayBase.prototype.baseClass = "overlay";
420
- OverlayBase.prototype.resizable = false;
421
- OverlayBase.prototype.resizeWidth = 7;
422
- OverlayBase.prototype.center = false;
423
- OverlayBase.prototype.centerX = false;
424
- OverlayBase.prototype.centerY = false;
425
- OverlayBase.prototype.modal = false;
426
- OverlayBase.prototype.backdrop = false;
427
- OverlayBase.prototype.inline = false;
428
- OverlayBase.prototype.autoFocus = false;
429
- OverlayBase.prototype.autoFocusFirstChild = false;
430
- OverlayBase.prototype.animate = false;
431
- OverlayBase.prototype.draggable = false;
432
- OverlayBase.prototype.destroyDelay = 0;
433
- OverlayBase.prototype.dismissOnFocusOut = false;
434
- OverlayBase.prototype.focusable = false;
435
- OverlayBase.prototype.containerStyle = undefined;
436
- OverlayBase.prototype.dismissOnPopState = false;
437
- OverlayBase.prototype.closeOnEscape = false;
438
- OverlayBase.prototype.needsBeacon = false;
439
-
440
- export class Overlay extends OverlayBase<OverlayConfig, OverlayInstance> {}
441
-
442
- Widget.alias("overlay", Overlay);
443
-
444
- interface OverlayContentProps {
445
- onRef: (el: HTMLDivElement | null) => void;
446
- className: string;
447
- style: any;
448
- tabIndex: number | undefined;
449
- onFocus: () => void;
450
- onBlur: () => void;
451
- onKeyDown: (e: any) => void;
452
- onMouseMove: (e: any, captureData?: any) => void;
453
- onMouseUp: (e: any) => void;
454
- onMouseDown: (e: any) => void;
455
- onTouchStart: (e: any) => void;
456
- onTouchEnd: (e: any) => void;
457
- onTouchMove: (e: any, captureData?: any) => void;
458
- onMouseEnter: (e: any) => void;
459
- onMouseLeave: (e: any) => void;
460
- onClick: (e: any) => void;
461
- onDidUpdate: () => void;
462
- focusableOverlayContainer?: boolean;
463
- children: any;
464
- }
465
-
466
- //TODO: all el related logic should be moved here
467
- class OverlayContent extends VDOM.Component<OverlayContentProps, {}> {
468
- render() {
469
- return (
470
- <div
471
- ref={this.props.onRef}
472
- className={this.props.className}
473
- style={this.props.style}
474
- tabIndex={this.props.tabIndex}
475
- onFocus={this.props.onFocus}
476
- onBlur={this.props.onBlur}
477
- onKeyDown={this.props.onKeyDown}
478
- onMouseMove={this.props.onMouseMove}
479
- onMouseUp={this.props.onMouseUp}
480
- onMouseDown={this.props.onMouseDown}
481
- onTouchStart={this.props.onTouchStart}
482
- onTouchEnd={this.props.onTouchEnd}
483
- onTouchMove={this.props.onTouchMove}
484
- onMouseEnter={this.props.onMouseEnter}
485
- onMouseLeave={this.props.onMouseLeave}
486
- onClick={this.props.onClick}
487
- data-focusable-overlay-container={this.props.focusableOverlayContainer}
488
- >
489
- {this.props.children}
490
- </div>
491
- );
492
- }
493
-
494
- componentDidUpdate() {
495
- this.props.onDidUpdate();
496
- }
497
- }
498
-
499
- export interface OverlayComponentProps {
500
- instance: OverlayInstance;
501
- parentEl?: HTMLElement;
502
- subscribeToBeforeDismiss?: (cb: () => boolean) => void;
503
- children: any;
504
- beaconEl: HTMLElement | null;
505
- }
506
-
507
- export interface OverlayComponentState {
508
- animated?: boolean;
509
- mods?: Record<string, boolean>;
510
- }
511
-
512
- //TODO: This should be called OverlayPortal
513
- export class OverlayComponent<
514
- Props extends OverlayComponentProps = OverlayComponentProps,
515
- State extends OverlayComponentState = OverlayComponentState,
516
- > extends VDOM.Component<Props, State> {
517
- declare el?: HTMLElement | null;
518
- declare containerEl?: HTMLElement | null;
519
- declare ownedEl?: HTMLElement | null;
520
- onOverlayRef?: (el: HTMLElement | null) => void;
521
- declare shadowEl?: HTMLElement | null;
522
- declare dismissed?: boolean;
523
- declare unmounting?: boolean;
524
- onPopState?: () => void;
525
- unsubscribeWheelBlock?: () => void;
526
- declare customStyle: any;
527
- declare root: Root;
528
-
529
- constructor(props: Props) {
530
- super(props);
531
- this.state = {} as State;
532
- this.customStyle = {};
533
- }
534
-
535
- render() {
536
- let { instance, parentEl } = this.props;
537
- let { widget } = instance;
538
-
539
- if (widget.inline || parentEl) return this.renderOverlay();
540
-
541
- if (!this.containerEl) {
542
- instance.beaconEl = this.props.beaconEl;
543
- this.ownedEl = widget.containerFactory(instance);
544
- this.ownedEl.style.display = "hidden";
545
- this.containerEl = this.ownedEl;
546
- }
547
-
548
- // content is rendered in componentDidUpdate if portals are not supported
549
- return VDOM.DOM.createPortal ? VDOM.DOM.createPortal!(this.renderOverlay(), this.containerEl) : null;
550
- }
551
-
552
- renderOverlay() {
553
- let { widget, data } = this.props.instance;
554
- let { CSS, baseClass } = widget;
555
-
556
- if (!this.onOverlayRef)
557
- this.onOverlayRef = (el) => {
558
- this.el = el;
559
- };
560
-
561
- let content = (
562
- <OverlayContent
563
- onRef={this.onOverlayRef}
564
- className={data.classNames}
565
- style={data.style}
566
- tabIndex={widget.focusable ? 0 : undefined}
567
- onFocus={this.onFocus.bind(this)}
568
- onBlur={this.onBlur.bind(this)}
569
- onKeyDown={this.onKeyDown.bind(this)}
570
- onMouseDown={this.onMouseDown.bind(this)}
571
- onMouseUp={this.onMouseUp.bind(this)}
572
- onMouseMove={this.onMouseMove.bind(this)}
573
- onTouchStart={this.onMouseDown.bind(this)}
574
- onTouchEnd={this.onMouseUp.bind(this)}
575
- onTouchMove={this.onMouseMove.bind(this)}
576
- onMouseLeave={this.onMouseLeave.bind(this)}
577
- onMouseEnter={this.onMouseEnter.bind(this)}
578
- onClick={this.onClick.bind(this)}
579
- onDidUpdate={this.overlayDidUpdate.bind(this)}
580
- focusableOverlayContainer={widget.dismissOnFocusOut}
581
- >
582
- {this.renderOverlayBody()}
583
- </OverlayContent>
584
- );
585
-
586
- let result = content;
587
-
588
- if (widget.modal || widget.backdrop) {
589
- result = (
590
- <div
591
- key="shadow"
592
- ref={(el) => {
593
- this.shadowEl = el;
594
- }}
595
- className={CSS.element(baseClass, "shadow", {
596
- animated: this.state.animated,
597
- "animate-enter": this.state.animated && !this.dismissed,
598
- animate: widget.animate,
599
- })}
600
- style={parseStyle(data.shadowStyle)}
601
- >
602
- <div
603
- key="backdrop"
604
- className={CSS.element("overlay", "modal-backdrop")}
605
- onClick={this.onBackdropClick.bind(this)}
606
- />
607
- {content}
608
- </div>
609
- );
610
- }
611
-
612
- return result;
613
- }
614
-
615
- renderOverlayBody() {
616
- return this.props.children;
617
- }
618
-
619
- onFocus() {
620
- FocusManager.nudge();
621
- this.onFocusIn();
622
- if (this.el) oneFocusOut(this, this.el, this.onFocusOut.bind(this));
623
- }
624
-
625
- onBlur() {
626
- FocusManager.nudge();
627
- }
628
-
629
- onFocusIn() {}
630
-
631
- onFocusOut() {
632
- let { widget } = this.props.instance;
633
- widget.handleFocusOut(this.props.instance, this);
634
- }
635
-
636
- onMouseEnter(e: React.MouseEvent) {
637
- let { widget } = this.props.instance;
638
- widget.handleMouseEnter(this.props.instance, this);
639
- }
640
-
641
- onMouseLeave(e: React.MouseEvent) {
642
- let { widget } = this.props.instance;
643
- widget.handleMouseLeave(this.props.instance, this);
644
- }
645
-
646
- onClick(e: React.MouseEvent) {
647
- let { instance } = this.props;
648
- let { widget } = instance;
649
- if (widget.onClick) instance.invoke("onClick", e, instance, this);
650
- }
651
-
652
- onKeyDown(e: React.KeyboardEvent) {
653
- let { widget } = this.props.instance;
654
- widget.handleKeyDown(e, this.props.instance, this);
655
- }
656
-
657
- getResizePrefix(e: MouseEvent | React.MouseEvent | React.TouchEvent) {
658
- let { widget, data } = this.props.instance;
659
- if (!data.resizable) return "";
660
- let cursor = getCursorPos(e);
661
- let bounds = getTopLevelBoundingClientRect(this.el!);
662
- let leftMargin = cursor.clientX - bounds.left;
663
- let rightMargin = bounds.right - cursor.clientX;
664
- let topMargin = cursor.clientY - bounds.top;
665
- let bottomMargin = bounds.bottom - cursor.clientY;
666
- let prefix = "";
667
-
668
- if (topMargin >= 0 && topMargin < widget.resizeWidth) prefix += "n";
669
- else if (bottomMargin >= 0 && bottomMargin < widget.resizeWidth) prefix += "s";
670
-
671
- if (leftMargin >= 0 && leftMargin < widget.resizeWidth) prefix += "w";
672
- else if (rightMargin >= 0 && rightMargin < widget.resizeWidth) prefix += "e";
673
- return prefix;
674
- }
675
-
676
- onMouseDown(e: React.MouseEvent | React.TouchEvent) {
677
- let { instance } = this.props;
678
- let { widget, data } = instance;
679
-
680
- if (widget.onMouseDown && instance.invoke("onMouseDown", e, instance) === false) return;
681
-
682
- let prefix = this.getResizePrefix(e);
683
- if (prefix) {
684
- //e.preventDefault();
685
- let rect = getTopLevelBoundingClientRect(this.el!);
686
- let cursor = getCursorPos(e);
687
- let captureData = {
688
- prefix: prefix,
689
- dl: cursor.clientX - rect.left,
690
- dt: cursor.clientY - rect.top,
691
- dr: cursor.clientX - rect.right,
692
- db: cursor.clientY - rect.bottom,
693
- rect: rect,
694
- };
695
- captureMouseOrTouch(e, this.onMouseMove.bind(this), undefined, captureData, prefix + "-resize");
696
- } else if (data.draggable) {
697
- ddMouseDown(e);
698
- }
699
- //e.stopPropagation();
700
- }
701
-
702
- onBackdropClick(e: React.MouseEvent) {
703
- e.stopPropagation();
704
- let { instance } = this.props;
705
- let { widget } = instance;
706
-
707
- if (widget.onBackdropClick) instance.invoke("onBackdropClick", e, instance);
708
-
709
- if (widget.backdrop) {
710
- if (instance.dismiss) instance.dismiss();
711
- } else if (widget.modal) {
712
- FocusManager.focus(this.el!);
713
- }
714
- }
715
-
716
- onMouseUp(e: React.MouseEvent | React.TouchEvent) {
717
- ddMouseUp();
718
- e.stopPropagation();
719
- }
720
-
721
- onMouseMove(e: MouseEvent, captureData: any) {
722
- // handle dragging
723
- let { instance } = this.props;
724
- let { data, widget } = instance;
725
- let detect = ddDetect(e);
726
- if (data.draggable && detect) {
727
- this.startMoveOperation(e);
728
- return;
729
- }
730
-
731
- if (captureData && captureData.prefix) {
732
- let { prefix, rect, dl, dt, dr, db } = captureData;
733
- let cursor = getCursorPos(e);
734
-
735
- if (prefix.indexOf("w") != -1)
736
- this.setCustomStyle({
737
- left: cursor.clientX - dl + "px",
738
- width: rect.right - cursor.clientX + dl + "px",
739
- right: "auto",
740
- });
741
-
742
- if (prefix.indexOf("n") != -1)
743
- this.setCustomStyle({
744
- top: cursor.clientY - dt + "px",
745
- height: rect.bottom - cursor.clientY + dt + "px",
746
- bottom: "auto",
747
- });
748
-
749
- if (prefix.indexOf("e") != -1)
750
- this.setCustomStyle({
751
- width: cursor.clientX - dr - rect.left + "px",
752
- left: `${rect.left}px`,
753
- right: "auto",
754
- });
755
-
756
- if (prefix.indexOf("s") != -1)
757
- this.setCustomStyle({
758
- height: cursor.clientY - db - rect.top + "px",
759
- top: `${rect.top}px`,
760
- bottom: "auto",
761
- });
762
-
763
- if (prefix.indexOf("w") >= 0 || prefix.indexOf("n") >= 0) widget.handleMove(e, instance, this);
764
-
765
- widget.handleResize(e, instance, this);
766
- } else {
767
- let prefix = this.getResizePrefix(e);
768
- this.setCustomStyle({
769
- cursor: prefix ? prefix + "-resize" : undefined,
770
- });
771
- }
772
- }
773
-
774
- startMoveOperation(e: MouseEvent | React.MouseEvent | React.TouchEvent) {
775
- if (this.el && !this.getResizePrefix(e)) {
776
- e.stopPropagation();
777
- let rect = getTopLevelBoundingClientRect(this.el);
778
- let cursor = getCursorPos(e);
779
- let data = {
780
- dx: cursor.clientX - rect.left,
781
- dy: cursor.clientY - rect.top,
782
- };
783
-
784
- captureMouseOrTouch(
785
- e,
786
- this.onMove.bind(this),
787
- undefined,
788
- data,
789
- getComputedStyle(e.target as HTMLElement).cursor,
790
- );
791
- }
792
- }
793
-
794
- onMove(e: MouseEvent, data: any) {
795
- if (data) {
796
- let cursor = getCursorPos(e);
797
- e.preventDefault();
798
- this.setCustomStyle({
799
- left: cursor.clientX - data.dx + "px",
800
- top: cursor.clientY - data.dy + "px",
801
- right: "auto",
802
- bottom: "auto",
803
- });
804
-
805
- let { instance } = this.props;
806
- let { widget } = instance;
807
- widget.handleMove(e, instance, this);
808
- }
809
- }
810
-
811
- onBeforeDismiss() {
812
- let { instance } = this.props;
813
- let { widget } = instance;
814
-
815
- if (widget.overlayWillDismiss && widget.overlayWillDismiss(instance, this) === false) return false;
816
-
817
- this.dismissed = true;
818
-
819
- //this.el might be null if visible is set to false
820
- if (this.el) {
821
- this.el.className = this.getOverlayCssClass();
822
-
823
- // if (widget.animate)
824
- // this.setState({
825
- // animated: false
826
- // });
827
- }
828
- return true;
829
- }
830
-
831
- componentDidMount() {
832
- let { instance, subscribeToBeforeDismiss, parentEl } = this.props;
833
- let { widget, data } = instance;
834
-
835
- this.setZIndex(isNumber(data.zIndex) ? data.zIndex : ZIndexManager.next());
836
-
837
- this.componentDidUpdate();
838
- widget.overlayDidMount(instance, this);
839
-
840
- if (this.containerEl) this.containerEl.style.removeProperty("display");
841
- else if (parentEl) parentEl.style.removeProperty("display");
842
-
843
- let childHasFocus = isSelfOrDescendant(this.el!, getActiveElement());
844
-
845
- if (childHasFocus) oneFocusOut(this, this.el, this.onFocusOut.bind(this));
846
- else {
847
- if (!widget.autoFocusFirstChild || !FocusManager.focusFirstChild(this.el!))
848
- if (widget.focusable && widget.autoFocus) FocusManager.focus(this.el!);
849
- }
850
-
851
- instance.onBeforeDismiss = this.onBeforeDismiss.bind(this);
852
-
853
- if (subscribeToBeforeDismiss) {
854
- subscribeToBeforeDismiss(instance.onBeforeDismiss);
855
- }
856
-
857
- if (widget.animate) {
858
- setTimeout(() => {
859
- if (!this.unmounting)
860
- this.setState({
861
- animated: true,
862
- });
863
- }, 0);
864
- }
865
-
866
- if (widget.dismissOnPopState) {
867
- this.onPopState = () => {
868
- this.props.instance.dismiss?.();
869
- };
870
- window.addEventListener("popstate", this.onPopState);
871
- }
872
-
873
- if (this.shadowEl)
874
- this.unsubscribeWheelBlock = addEventListenerWithOptions(
875
- this.shadowEl,
876
- "wheel",
877
- (e) => {
878
- if (e.shiftKey || e.ctrlKey) return;
879
- //check if there is a scrollable element within the shadow or overlay contents
880
- //such that its scrollbar is not at the very end
881
- let scrollAllowed = false;
882
- closest(e.target as Element, (el) => {
883
- if (
884
- (e.deltaY > 0 && el.scrollTop < el.scrollHeight - el.clientHeight) ||
885
- (e.deltaY < 0 && el.scrollTop > 0)
886
- ) {
887
- scrollAllowed = true;
888
- return true;
889
- }
890
- return el == e.currentTarget;
891
- });
892
- if (!scrollAllowed) e.preventDefault();
893
- },
894
- { passive: false },
895
- );
896
- }
897
-
898
- componentWillUnmount() {
899
- if (this.onPopState) window.removeEventListener("popstate", this.onPopState);
900
-
901
- if (this.unsubscribeWheelBlock) this.unsubscribeWheelBlock();
902
-
903
- offFocusOut(this);
904
- this.unmounting = true;
905
-
906
- let { widget } = this.props.instance;
907
- let { baseClass, CSS } = widget;
908
-
909
- // //we didn't have a chance to call onBeforeDismiss
910
- if (this.state.animated && this.el) {
911
- this.el.className = this.getOverlayCssClass();
912
- if (this.shadowEl)
913
- this.shadowEl.className = CSS.element(baseClass, "shadow", {
914
- animate: widget.animate,
915
- "animate-leave": true,
916
- });
917
- }
918
-
919
- widget.overlayWillUnmount(this.props.instance, this);
920
-
921
- if (this.ownedEl) {
922
- setTimeout(() => {
923
- this.root?.unmount();
924
- if (this.ownedEl?.parentNode) this.ownedEl.parentNode.removeChild(this.ownedEl);
925
- this.ownedEl = null;
926
- }, widget.destroyDelay);
927
- }
928
-
929
- delete this.containerEl;
930
- }
931
-
932
- setZIndex(zIndex: number) {
933
- if (this.shadowEl) this.shadowEl.style.zIndex = zIndex.toString();
934
- this.setCustomStyle({
935
- zIndex: zIndex.toString(),
936
- });
937
- }
938
-
939
- setCustomStyle(style: Partial<CSSStyleDeclaration>) {
940
- Object.assign(this.customStyle, style);
941
- if (this.el) Object.assign(this.el.style, this.customStyle);
942
- }
943
-
944
- getOverlayStyle() {
945
- let { data } = this.props.instance;
946
- return {
947
- ...data.style,
948
- ...this.customStyle,
949
- };
950
- }
951
-
952
- setCSSState(mods: Record<string, boolean>) {
953
- let m: Record<string, boolean> = { ...this.state.mods };
954
- let changed = false;
955
- for (let k in mods)
956
- if (m[k] !== mods[k]) {
957
- m[k] = mods[k];
958
- changed = true;
959
- }
960
-
961
- if (changed)
962
- this.setState({
963
- mods: mods,
964
- });
965
- }
966
-
967
- getOverlayCssClass() {
968
- let { data, widget } = this.props.instance;
969
- let { CSS } = widget;
970
-
971
- return (
972
- CSS.expand(
973
- data.classNames,
974
- CSS.state({
975
- ...this.state.mods,
976
- animated: this.state.animated && !this.unmounting && !this.dismissed,
977
- "animate-enter": this.state.animated && !this.dismissed,
978
- "animate-leave": widget.animate && this.dismissed,
979
- }),
980
- ) ?? ""
981
- );
982
- }
983
-
984
- overlayDidUpdate() {
985
- if (this.el && !this.dismissed) {
986
- let { widget } = this.props.instance;
987
- widget.overlayDidUpdate(this.props.instance, this);
988
- this.el.className = this.getOverlayCssClass();
989
- Object.assign(this.el.style, this.getOverlayStyle());
990
- }
991
- }
992
-
993
- componentDidUpdate() {
994
- if (this.containerEl && !VDOM.DOM.createPortal) {
995
- this.root = VDOM.DOM.createRoot(this.containerEl);
996
- this.root.render(this.renderOverlay());
997
- }
998
- this.overlayDidUpdate();
999
- }
1000
- }
1001
-
1002
- interface OverlayBeaconProps {
1003
- childrenFactory: (beacon: HTMLElement) => React.ReactNode;
1004
- }
1005
-
1006
- export interface OverlayBeaconState {
1007
- beaconRendered?: boolean;
1008
- }
1009
-
1010
- export class OverlayBeacon extends VDOM.Component<OverlayBeaconProps, OverlayBeaconState> {
1011
- el: HTMLElement | null;
1012
- render() {
1013
- return (
1014
- <>
1015
- <div
1016
- ref={(el) => {
1017
- this.el = el;
1018
- }}
1019
- style={{ position: "absolute", display: "none" }}
1020
- />
1021
- {this.el && this.props.childrenFactory(this.el)}
1022
- </>
1023
- );
1024
- }
1025
-
1026
- componentDidMount(): void {
1027
- this.setState({ beaconRendered: true });
1028
- }
1029
- }
1
+ /** @jsxImportSource react */
2
+ import type { Root } from "cx-react";
3
+ import { isBinding, isBindingObject } from "../../data/Binding";
4
+ import { Store } from "../../data/Store";
5
+ import { View } from "../../data/View";
6
+ import { startAppLoop } from "../../ui/app/startAppLoop";
7
+ import { ContainerBase, StyledContainerConfig } from "../../ui/Container";
8
+ import { FocusManager, offFocusOut, oneFocusOut } from "../../ui/FocusManager";
9
+ import { Instance } from "../../ui/Instance";
10
+ import { BooleanProp, NumberProp } from "../../ui/Prop";
11
+ import { RenderingContext } from "../../ui/RenderingContext";
12
+ import { VDOM, Widget } from "../../ui/Widget";
13
+ import { ZIndexManager } from "../../ui/ZIndexManager";
14
+ import { addEventListenerWithOptions } from "../../util/addEventListenerWithOptions";
15
+ import { closest, isSelfOrDescendant } from "../../util/DOM";
16
+ import { getActiveElement } from "../../util/getActiveElement";
17
+ import { getTopLevelBoundingClientRect } from "../../util/getTopLevelBoundingClientRect";
18
+ import { isDataRecord } from "../../util/isDataRecord";
19
+ import { isNumber } from "../../util/isNumber";
20
+ import { KeyCode } from "../../util/KeyCode";
21
+ import { parseStyle } from "../../util/parseStyle";
22
+ import { SubscriberList } from "../../util/SubscriberList";
23
+ import { ddDetect, ddMouseDown, ddMouseUp } from "../drag-drop/ops";
24
+ import { captureMouseOrTouch, getCursorPos } from "./captureMouse";
25
+
26
+ /*
27
+ Features:
28
+ - renders itself on top of other elements
29
+ - provide resizing capabilities
30
+ - adds positioning hook and ability to position itself in the center of the page
31
+ - provides header, body, and footer elements and updates body's height on resize (move this to Window)
32
+ - stop mouse events from bubbling to parents, but allow keystrokes
33
+ */
34
+
35
+ export interface OverlayConfig extends StyledContainerConfig {
36
+ /** Set to `true` to enable resizing. */
37
+ resizable?: BooleanProp;
38
+
39
+ /** Set to `true` to enable dragging the overlay. */
40
+ draggable?: BooleanProp;
41
+
42
+ /** Base CSS class to be applied to the field. Defaults to `overlay`. */
43
+ baseClass?: string;
44
+
45
+ /** Width of resize handle area. */
46
+ resizeWidth?: number;
47
+
48
+ /** Set to `true` to initially place the overlay in the center of the page. */
49
+ center?: boolean;
50
+
51
+ /** Set to `true` to initially place the overlay in the center of the page horizontally. */
52
+ centerX?: boolean;
53
+
54
+ /** Set to `true` to initially place the overlay in the center of the page vertically. */
55
+ centerY?: boolean;
56
+
57
+ /** Set to `true` to add a modal backdrop which masks mouse events for the rest of the page. */
58
+ modal?: boolean;
59
+
60
+ /** Set to `true` to add a modal backdrop which will dismiss the window when clicked. */
61
+ backdrop?: boolean;
62
+
63
+ /** Set to `true` to force the element to be rendered inline, instead of being appended to the body element.
64
+ * Inline overlays have z-index set to a very high value, to ensure they are displayed on top of the other content. */
65
+ inline?: boolean;
66
+
67
+ /** Set to `true` to automatically focus the top level overlay element. */
68
+ autoFocus?: boolean;
69
+
70
+ /** Set to `true` to automatically focus the first focusable child in the overlay. */
71
+ autoFocusFirstChild?: boolean;
72
+
73
+ /** Set to `true` to append the set animate state after the initial render. Appended CSS class may be used to add show/hide animations. */
74
+ animate?: boolean;
75
+
76
+ /** Number of milliseconds to wait, before removing the element from the DOM. Used in combination with the animate property. */
77
+ destroyDelay?: number;
78
+
79
+ /** Automatically dismiss overlay if it loses focus. */
80
+ dismissOnFocusOut?: boolean;
81
+
82
+ /** Set to true to make the top level overlay element focusable. */
83
+ focusable?: boolean;
84
+
85
+ /** Set to `true` to dismiss the window if the user presses the back button in the browser. */
86
+ dismissOnPopState?: boolean;
87
+
88
+ /** A callback function which fires while the overlay is being moved around. */
89
+ onMove?: string | ((e: Event, instance: Instance, component: any) => void);
90
+
91
+ /** A callback function which fires while the overlay is being resized. */
92
+ onResize?: string | ((e: Event, instance: Instance, component: any) => void);
93
+
94
+ /** zIndex */
95
+ zIndex?: NumberProp;
96
+
97
+ /** Set to `true` to make the window automatically close if Esc is pressed on the keyboard. Default value is false.*/
98
+ closeOnEscape?: boolean;
99
+
100
+ /** Custom CSS styling for the container element. */
101
+ containerStyle?: string;
102
+
103
+ /** Callback for focus out event. */
104
+ onFocusOut?: string;
105
+
106
+ /** Callback for mouse enter event. */
107
+ onMouseEnter?: string;
108
+
109
+ /** Callback for mouse leave event. */
110
+ onMouseLeave?: string;
111
+
112
+ /** Callback for backdrop click. */
113
+ onBackdropClick?: string;
114
+
115
+ /** Callback for mouse down event. */
116
+ onMouseDown?: string;
117
+
118
+ /** Callback for key down event. */
119
+ onKeyDown?: string;
120
+
121
+ /** Callback fired before dismiss. */
122
+ onBeforeDismiss?: string | (() => boolean);
123
+
124
+ /** Callback fired when overlay will dismiss. */
125
+ overlayWillDismiss?: (instance: Instance, component: any) => boolean;
126
+
127
+ /** Callback for click event. */
128
+ onClick?: string;
129
+ }
130
+
131
+ export interface OverlayOpenOptions {
132
+ initiatingEvent?: React.SyntheticEvent;
133
+ name?: string;
134
+ dismiss?: () => void;
135
+ parentEl?: Element;
136
+ subscribeToBeforeDismiss?: (callback: () => boolean) => void;
137
+ destroyDelay?: number;
138
+ removeParentDOMElement?: boolean;
139
+ }
140
+
141
+ export interface ConfigureOverlayContainerContext {
142
+ relatedElement?: HTMLElement | null;
143
+ initiatingEvent?: React.SyntheticEvent;
144
+ }
145
+
146
+ export class OverlayInstance<WidgetType extends OverlayBase<any, any> = Overlay> extends Instance<WidgetType> {
147
+ declare positionChangeSubscribers: SubscriberList;
148
+ declare dismiss?: () => void;
149
+ onBeforeDismiss?: () => boolean;
150
+ declare beaconEl?: HTMLElement | null;
151
+ }
152
+
153
+ export class OverlayBase<
154
+ Config extends OverlayConfig = OverlayConfig,
155
+ InstanceType extends OverlayInstance<any> = OverlayInstance<any>,
156
+ > extends ContainerBase<Config, InstanceType> {
157
+ static configureOverlayContainer?: (containerEl: HTMLElement, context: ConfigureOverlayContainerContext) => void;
158
+
159
+ // Properties declared here to support prototype assignments
160
+ declare styled: true;
161
+ declare baseClass: string;
162
+ declare resizable?: BooleanProp;
163
+ declare resizeWidth: number;
164
+ declare center?: boolean;
165
+ declare centerX?: boolean;
166
+ declare centerY?: boolean;
167
+ declare modal?: boolean;
168
+ declare backdrop?: boolean;
169
+ declare inline?: boolean;
170
+ declare autoFocus?: boolean;
171
+ declare autoFocusFirstChild?: boolean;
172
+ declare animate?: boolean;
173
+ declare draggable?: BooleanProp;
174
+ declare destroyDelay?: number;
175
+ declare dismissOnFocusOut?: boolean;
176
+ declare focusable?: boolean;
177
+ declare containerStyle?: string;
178
+ declare dismissOnPopState?: boolean;
179
+ declare closeOnEscape?: boolean;
180
+ declare onFocusOut?: string;
181
+ declare onMouseLeave?: string;
182
+ declare onMouseEnter?: string;
183
+ declare onKeyDown?: string;
184
+ declare onMove?: string | ((e: Event, instance: Instance, component: any) => void);
185
+ declare onResize?: string | ((e: Event, instance: Instance, component: any) => void);
186
+ declare onClick?: string;
187
+ declare onMouseDown?: string;
188
+ declare onBackdropClick?: string;
189
+ declare overlayWillDismiss?: (instance: Instance, component: any) => boolean;
190
+ declare style?: any;
191
+ declare pad?: boolean;
192
+ declare needsBeacon: boolean;
193
+
194
+ init() {
195
+ if (this.center) this.centerX = this.centerY = this.center;
196
+
197
+ super.init();
198
+ }
199
+
200
+ declareData(...args: any[]) {
201
+ super.declareData(...args, {
202
+ shadowStyle: {
203
+ structured: true,
204
+ },
205
+ resizable: undefined,
206
+ draggable: undefined,
207
+ zIndex: undefined,
208
+ });
209
+ }
210
+
211
+ prepareData(context: RenderingContext, instance: InstanceType): void {
212
+ let { data } = instance;
213
+ data.stateMods = {
214
+ ...data.stateMods,
215
+ inline: this.inline,
216
+ modal: this.modal,
217
+ pad: this.pad,
218
+ resizable: data.resizable,
219
+ draggable: data.draggable,
220
+ animate: this.animate,
221
+ shadow: this.modal || this.backdrop,
222
+ };
223
+
224
+ super.prepareData(context, instance);
225
+ }
226
+
227
+ initInstance(context: RenderingContext, instance: InstanceType): void {
228
+ instance.positionChangeSubscribers = new SubscriberList();
229
+ super.initInstance(context, instance);
230
+ }
231
+
232
+ explore(context: RenderingContext, instance: InstanceType): void {
233
+ if (isBinding(this.visible)) {
234
+ if (!instance.dismiss) {
235
+ instance.dismiss = () => {
236
+ if (instance.onBeforeDismiss && instance.onBeforeDismiss() === false) return;
237
+ instance.set("visible", false);
238
+ };
239
+ }
240
+ } else if (context.options.dismiss) instance.dismiss = context.options.dismiss;
241
+
242
+ if (instance.dismiss) {
243
+ context.push("parentOptions", {
244
+ ...context.parentOptions,
245
+ dismiss: instance.dismiss,
246
+ });
247
+ }
248
+
249
+ if (instance.cache("dismiss", instance.dismiss)) instance.markShouldUpdate(context);
250
+
251
+ context.push("parentPositionChangeEvent", instance.positionChangeSubscribers);
252
+
253
+ super.explore(context, instance);
254
+ }
255
+
256
+ exploreCleanup(context: RenderingContext, instance: InstanceType): void {
257
+ if (instance.dismiss) context.pop("parentOptions");
258
+ context.pop("parentPositionChangeEvent");
259
+ }
260
+
261
+ render(context: RenderingContext, instance: InstanceType, key: string): any {
262
+ if (this.needsBeacon)
263
+ return (
264
+ <OverlayBeacon
265
+ key={key}
266
+ childrenFactory={(beaconEl) => (
267
+ <OverlayComponent
268
+ beaconEl={beaconEl}
269
+ instance={instance}
270
+ subscribeToBeforeDismiss={context.options.subscribeToBeforeDismiss}
271
+ parentEl={context.options.parentEl}
272
+ >
273
+ {this.renderContents(context, instance)}
274
+ </OverlayComponent>
275
+ )}
276
+ />
277
+ );
278
+
279
+ return (
280
+ <OverlayComponent
281
+ key={key}
282
+ beaconEl={null}
283
+ instance={instance}
284
+ subscribeToBeforeDismiss={context.options.subscribeToBeforeDismiss}
285
+ parentEl={context.options.parentEl}
286
+ >
287
+ {this.renderContents(context, instance)}
288
+ </OverlayComponent>
289
+ );
290
+ }
291
+
292
+ renderContents(context: RenderingContext, instance: InstanceType): any {
293
+ return this.renderChildren(context, instance);
294
+ }
295
+
296
+ getConfigureOverlayContainerContext(
297
+ instance?: InstanceType,
298
+ initiatingEvent?: React.SyntheticEvent,
299
+ ): ConfigureOverlayContainerContext {
300
+ return {
301
+ initiatingEvent,
302
+ relatedElement: instance?.beaconEl,
303
+ };
304
+ }
305
+
306
+ overlayDidMount(instance: InstanceType, component: any): void {
307
+ let { el } = component;
308
+ if (this.centerX) if (!el.style.left) el.style.left = `${(window.innerWidth - el.offsetWidth) / 2}px`;
309
+ if (this.centerY)
310
+ if (!el.style.top) el.style.top = `${Math.max(0, (window.innerHeight - el.offsetHeight) / 2)}px`;
311
+ }
312
+
313
+ overlayDidUpdate(instance: InstanceType, component: any): void {}
314
+
315
+ overlayWillUnmount(instance: InstanceType, component: any): void {}
316
+
317
+ handleFocusOut(instance: InstanceType, component: any): void {
318
+ if (this.onFocusOut) instance.invoke("onFocusOut", instance, component);
319
+
320
+ if (this.dismissOnFocusOut && instance.dismiss) instance.dismiss();
321
+ }
322
+
323
+ handleKeyDown(e: any, instance: InstanceType, component?: any): void | false {
324
+ if (this.onKeyDown && instance.invoke("onKeyDown", e, instance, component) === false) return false;
325
+
326
+ if (this.closeOnEscape && e.keyCode == KeyCode.esc && instance.dismiss) {
327
+ instance.dismiss();
328
+ e.stopPropagation();
329
+ }
330
+ }
331
+
332
+ handleMouseLeave(instance: InstanceType, component: any): void {
333
+ if (this.onMouseLeave) instance.invoke("onMouseLeave", instance, component);
334
+ }
335
+
336
+ handleMouseEnter(instance: InstanceType, component: any): void {
337
+ if (this.onMouseEnter) instance.invoke("onMouseEnter", instance, component);
338
+ }
339
+
340
+ getOverlayContainer(): HTMLElement {
341
+ return document.body;
342
+ }
343
+
344
+ containerFactory(instance?: InstanceType, initiatingEvent?: React.SyntheticEvent): HTMLElement {
345
+ let el = document.createElement("div");
346
+ let container = this.getOverlayContainer();
347
+ container.appendChild(el);
348
+ el.style.position = "absolute";
349
+ if (this.containerStyle) Object.assign(el.style, parseStyle(this.containerStyle));
350
+
351
+ if (OverlayBase.configureOverlayContainer)
352
+ OverlayBase.configureOverlayContainer(el, this.getConfigureOverlayContainerContext(instance, initiatingEvent));
353
+
354
+ return el;
355
+ }
356
+
357
+ open(storeOrInstance?: View | Instance, options?: OverlayOpenOptions): () => void {
358
+ if (!this.initialized) this.init();
359
+
360
+ let el = this.containerFactory(undefined, options?.initiatingEvent);
361
+ el.style.display = "hidden";
362
+
363
+ let beforeDismiss: (() => boolean) | null = null;
364
+ let stop: any;
365
+
366
+ options = {
367
+ destroyDelay: this.destroyDelay,
368
+ removeParentDOMElement: true,
369
+ ...options,
370
+ parentEl: el,
371
+ dismiss: () => {
372
+ if (beforeDismiss && beforeDismiss() === false) return;
373
+ stop();
374
+ beforeDismiss = null;
375
+ },
376
+ subscribeToBeforeDismiss: (cb: () => boolean) => {
377
+ beforeDismiss = cb;
378
+ },
379
+ };
380
+ options.name = options.name || "overlay";
381
+ stop = startAppLoop(el, storeOrInstance, this, options);
382
+ return options.dismiss!;
383
+ }
384
+
385
+ handleMove(e: any, instance: InstanceType, component: any): void {
386
+ let { widget } = instance;
387
+ if (!widget.onMove || instance.invoke("onMove", e, instance, component) !== false) {
388
+ instance.store.silently(() => {
389
+ if (isDataRecord(this.style) && isBindingObject(this.style.top)) {
390
+ instance.store.set(this.style.top.bind, component.el.style.top);
391
+ }
392
+
393
+ if (isDataRecord(this.style) && isBindingObject(this.style.left)) {
394
+ instance.store.set(this.style.left.bind, component.el.style.left);
395
+ }
396
+ });
397
+ }
398
+ instance.positionChangeSubscribers.notify();
399
+ }
400
+
401
+ handleResize(e: any, instance: InstanceType, component: any): void {
402
+ let { widget } = instance;
403
+ if (!widget.onResize || instance.invoke("onResize", e, instance, component) !== false) {
404
+ instance.store.silently(() => {
405
+ if (isDataRecord(this.style) && isBindingObject(this.style.width)) {
406
+ instance.store.set(this.style.width.bind, component.el.style.width);
407
+ }
408
+
409
+ if (isDataRecord(this.style) && isBindingObject(this.style.height)) {
410
+ instance.store.set(this.style.height.bind, component.el.style.height);
411
+ }
412
+ });
413
+ }
414
+ instance.positionChangeSubscribers.notify();
415
+ }
416
+ }
417
+
418
+ OverlayBase.prototype.styled = true;
419
+ OverlayBase.prototype.baseClass = "overlay";
420
+ OverlayBase.prototype.resizable = false;
421
+ OverlayBase.prototype.resizeWidth = 7;
422
+ OverlayBase.prototype.center = false;
423
+ OverlayBase.prototype.centerX = false;
424
+ OverlayBase.prototype.centerY = false;
425
+ OverlayBase.prototype.modal = false;
426
+ OverlayBase.prototype.backdrop = false;
427
+ OverlayBase.prototype.inline = false;
428
+ OverlayBase.prototype.autoFocus = false;
429
+ OverlayBase.prototype.autoFocusFirstChild = false;
430
+ OverlayBase.prototype.animate = false;
431
+ OverlayBase.prototype.draggable = false;
432
+ OverlayBase.prototype.destroyDelay = 0;
433
+ OverlayBase.prototype.dismissOnFocusOut = false;
434
+ OverlayBase.prototype.focusable = false;
435
+ OverlayBase.prototype.containerStyle = undefined;
436
+ OverlayBase.prototype.dismissOnPopState = false;
437
+ OverlayBase.prototype.closeOnEscape = false;
438
+ OverlayBase.prototype.needsBeacon = false;
439
+
440
+ export class Overlay extends OverlayBase<OverlayConfig, OverlayInstance> {}
441
+
442
+ Widget.alias("overlay", Overlay);
443
+
444
+ interface OverlayContentProps {
445
+ onRef: (el: HTMLDivElement | null) => void;
446
+ className: string;
447
+ style: any;
448
+ tabIndex: number | undefined;
449
+ onFocus: () => void;
450
+ onBlur: () => void;
451
+ onKeyDown: (e: any) => void;
452
+ onMouseMove: (e: any, captureData?: any) => void;
453
+ onMouseUp: (e: any) => void;
454
+ onMouseDown: (e: any) => void;
455
+ onTouchStart: (e: any) => void;
456
+ onTouchEnd: (e: any) => void;
457
+ onTouchMove: (e: any, captureData?: any) => void;
458
+ onMouseEnter: (e: any) => void;
459
+ onMouseLeave: (e: any) => void;
460
+ onClick: (e: any) => void;
461
+ onDidUpdate: () => void;
462
+ focusableOverlayContainer?: boolean;
463
+ children: any;
464
+ }
465
+
466
+ //TODO: all el related logic should be moved here
467
+ class OverlayContent extends VDOM.Component<OverlayContentProps, {}> {
468
+ render() {
469
+ return (
470
+ <div
471
+ ref={this.props.onRef}
472
+ className={this.props.className}
473
+ style={this.props.style}
474
+ tabIndex={this.props.tabIndex}
475
+ onFocus={this.props.onFocus}
476
+ onBlur={this.props.onBlur}
477
+ onKeyDown={this.props.onKeyDown}
478
+ onMouseMove={this.props.onMouseMove}
479
+ onMouseUp={this.props.onMouseUp}
480
+ onMouseDown={this.props.onMouseDown}
481
+ onTouchStart={this.props.onTouchStart}
482
+ onTouchEnd={this.props.onTouchEnd}
483
+ onTouchMove={this.props.onTouchMove}
484
+ onMouseEnter={this.props.onMouseEnter}
485
+ onMouseLeave={this.props.onMouseLeave}
486
+ onClick={this.props.onClick}
487
+ data-focusable-overlay-container={this.props.focusableOverlayContainer}
488
+ >
489
+ {this.props.children}
490
+ </div>
491
+ );
492
+ }
493
+
494
+ componentDidUpdate() {
495
+ this.props.onDidUpdate();
496
+ }
497
+ }
498
+
499
+ export interface OverlayComponentProps {
500
+ instance: OverlayInstance;
501
+ parentEl?: HTMLElement;
502
+ subscribeToBeforeDismiss?: (cb: () => boolean) => void;
503
+ children: any;
504
+ beaconEl: HTMLElement | null;
505
+ }
506
+
507
+ export interface OverlayComponentState {
508
+ animated?: boolean;
509
+ mods?: Record<string, boolean>;
510
+ }
511
+
512
+ //TODO: This should be called OverlayPortal
513
+ export class OverlayComponent<
514
+ Props extends OverlayComponentProps = OverlayComponentProps,
515
+ State extends OverlayComponentState = OverlayComponentState,
516
+ > extends VDOM.Component<Props, State> {
517
+ declare el?: HTMLElement | null;
518
+ declare containerEl?: HTMLElement | null;
519
+ declare ownedEl?: HTMLElement | null;
520
+ onOverlayRef?: (el: HTMLElement | null) => void;
521
+ declare shadowEl?: HTMLElement | null;
522
+ declare dismissed?: boolean;
523
+ declare unmounting?: boolean;
524
+ onPopState?: () => void;
525
+ unsubscribeWheelBlock?: () => void;
526
+ declare customStyle: any;
527
+ declare root: Root;
528
+
529
+ constructor(props: Props) {
530
+ super(props);
531
+ this.state = {} as State;
532
+ this.customStyle = {};
533
+ }
534
+
535
+ render() {
536
+ let { instance, parentEl } = this.props;
537
+ let { widget } = instance;
538
+
539
+ if (widget.inline || parentEl) return this.renderOverlay();
540
+
541
+ if (!this.containerEl) {
542
+ instance.beaconEl = this.props.beaconEl;
543
+ this.ownedEl = widget.containerFactory(instance);
544
+ this.ownedEl.style.display = "hidden";
545
+ this.containerEl = this.ownedEl;
546
+ }
547
+
548
+ // content is rendered in componentDidUpdate if portals are not supported
549
+ return VDOM.DOM.createPortal ? VDOM.DOM.createPortal!(this.renderOverlay(), this.containerEl) : null;
550
+ }
551
+
552
+ renderOverlay() {
553
+ let { widget, data } = this.props.instance;
554
+ let { CSS, baseClass } = widget;
555
+
556
+ if (!this.onOverlayRef)
557
+ this.onOverlayRef = (el) => {
558
+ this.el = el;
559
+ };
560
+
561
+ let content = (
562
+ <OverlayContent
563
+ onRef={this.onOverlayRef}
564
+ className={data.classNames}
565
+ style={data.style}
566
+ tabIndex={widget.focusable ? 0 : undefined}
567
+ onFocus={this.onFocus.bind(this)}
568
+ onBlur={this.onBlur.bind(this)}
569
+ onKeyDown={this.onKeyDown.bind(this)}
570
+ onMouseDown={this.onMouseDown.bind(this)}
571
+ onMouseUp={this.onMouseUp.bind(this)}
572
+ onMouseMove={this.onMouseMove.bind(this)}
573
+ onTouchStart={this.onMouseDown.bind(this)}
574
+ onTouchEnd={this.onMouseUp.bind(this)}
575
+ onTouchMove={this.onMouseMove.bind(this)}
576
+ onMouseLeave={this.onMouseLeave.bind(this)}
577
+ onMouseEnter={this.onMouseEnter.bind(this)}
578
+ onClick={this.onClick.bind(this)}
579
+ onDidUpdate={this.overlayDidUpdate.bind(this)}
580
+ focusableOverlayContainer={widget.dismissOnFocusOut}
581
+ >
582
+ {this.renderOverlayBody()}
583
+ </OverlayContent>
584
+ );
585
+
586
+ let result = content;
587
+
588
+ if (widget.modal || widget.backdrop) {
589
+ result = (
590
+ <div
591
+ key="shadow"
592
+ ref={(el) => {
593
+ this.shadowEl = el;
594
+ }}
595
+ className={CSS.element(baseClass, "shadow", {
596
+ animated: this.state.animated,
597
+ "animate-enter": this.state.animated && !this.dismissed,
598
+ animate: widget.animate,
599
+ })}
600
+ style={parseStyle(data.shadowStyle)}
601
+ >
602
+ <div
603
+ key="backdrop"
604
+ className={CSS.element("overlay", "modal-backdrop")}
605
+ onClick={this.onBackdropClick.bind(this)}
606
+ />
607
+ {content}
608
+ </div>
609
+ );
610
+ }
611
+
612
+ return result;
613
+ }
614
+
615
+ renderOverlayBody() {
616
+ return this.props.children;
617
+ }
618
+
619
+ onFocus() {
620
+ FocusManager.nudge();
621
+ this.onFocusIn();
622
+ if (this.el) oneFocusOut(this, this.el, this.onFocusOut.bind(this));
623
+ }
624
+
625
+ onBlur() {
626
+ FocusManager.nudge();
627
+ }
628
+
629
+ onFocusIn() {}
630
+
631
+ onFocusOut() {
632
+ let { widget } = this.props.instance;
633
+ widget.handleFocusOut(this.props.instance, this);
634
+ }
635
+
636
+ onMouseEnter(e: React.MouseEvent) {
637
+ let { widget } = this.props.instance;
638
+ widget.handleMouseEnter(this.props.instance, this);
639
+ }
640
+
641
+ onMouseLeave(e: React.MouseEvent) {
642
+ let { widget } = this.props.instance;
643
+ widget.handleMouseLeave(this.props.instance, this);
644
+ }
645
+
646
+ onClick(e: React.MouseEvent) {
647
+ let { instance } = this.props;
648
+ let { widget } = instance;
649
+ if (widget.onClick) instance.invoke("onClick", e, instance, this);
650
+ }
651
+
652
+ onKeyDown(e: React.KeyboardEvent) {
653
+ let { widget } = this.props.instance;
654
+ widget.handleKeyDown(e, this.props.instance, this);
655
+ }
656
+
657
+ getResizePrefix(e: MouseEvent | React.MouseEvent | React.TouchEvent) {
658
+ let { widget, data } = this.props.instance;
659
+ if (!data.resizable) return "";
660
+ let cursor = getCursorPos(e);
661
+ let bounds = getTopLevelBoundingClientRect(this.el!);
662
+ let leftMargin = cursor.clientX - bounds.left;
663
+ let rightMargin = bounds.right - cursor.clientX;
664
+ let topMargin = cursor.clientY - bounds.top;
665
+ let bottomMargin = bounds.bottom - cursor.clientY;
666
+ let prefix = "";
667
+
668
+ if (topMargin >= 0 && topMargin < widget.resizeWidth) prefix += "n";
669
+ else if (bottomMargin >= 0 && bottomMargin < widget.resizeWidth) prefix += "s";
670
+
671
+ if (leftMargin >= 0 && leftMargin < widget.resizeWidth) prefix += "w";
672
+ else if (rightMargin >= 0 && rightMargin < widget.resizeWidth) prefix += "e";
673
+ return prefix;
674
+ }
675
+
676
+ onMouseDown(e: React.MouseEvent | React.TouchEvent) {
677
+ let { instance } = this.props;
678
+ let { widget, data } = instance;
679
+
680
+ if (widget.onMouseDown && instance.invoke("onMouseDown", e, instance) === false) return;
681
+
682
+ let prefix = this.getResizePrefix(e);
683
+ if (prefix) {
684
+ //e.preventDefault();
685
+ let rect = getTopLevelBoundingClientRect(this.el!);
686
+ let cursor = getCursorPos(e);
687
+ let captureData = {
688
+ prefix: prefix,
689
+ dl: cursor.clientX - rect.left,
690
+ dt: cursor.clientY - rect.top,
691
+ dr: cursor.clientX - rect.right,
692
+ db: cursor.clientY - rect.bottom,
693
+ rect: rect,
694
+ };
695
+ captureMouseOrTouch(e, this.onMouseMove.bind(this), undefined, captureData, prefix + "-resize");
696
+ } else if (data.draggable) {
697
+ ddMouseDown(e);
698
+ }
699
+ //e.stopPropagation();
700
+ }
701
+
702
+ onBackdropClick(e: React.MouseEvent) {
703
+ e.stopPropagation();
704
+ let { instance } = this.props;
705
+ let { widget } = instance;
706
+
707
+ if (widget.onBackdropClick) instance.invoke("onBackdropClick", e, instance);
708
+
709
+ if (widget.backdrop) {
710
+ if (instance.dismiss) instance.dismiss();
711
+ } else if (widget.modal) {
712
+ FocusManager.focus(this.el!);
713
+ }
714
+ }
715
+
716
+ onMouseUp(e: React.MouseEvent | React.TouchEvent) {
717
+ ddMouseUp();
718
+ e.stopPropagation();
719
+ }
720
+
721
+ onMouseMove(e: MouseEvent, captureData: any) {
722
+ // handle dragging
723
+ let { instance } = this.props;
724
+ let { data, widget } = instance;
725
+ let detect = ddDetect(e);
726
+ if (data.draggable && detect) {
727
+ this.startMoveOperation(e);
728
+ return;
729
+ }
730
+
731
+ if (captureData && captureData.prefix) {
732
+ let { prefix, rect, dl, dt, dr, db } = captureData;
733
+ let cursor = getCursorPos(e);
734
+
735
+ if (prefix.indexOf("w") != -1)
736
+ this.setCustomStyle({
737
+ left: cursor.clientX - dl + "px",
738
+ width: rect.right - cursor.clientX + dl + "px",
739
+ right: "auto",
740
+ });
741
+
742
+ if (prefix.indexOf("n") != -1)
743
+ this.setCustomStyle({
744
+ top: cursor.clientY - dt + "px",
745
+ height: rect.bottom - cursor.clientY + dt + "px",
746
+ bottom: "auto",
747
+ });
748
+
749
+ if (prefix.indexOf("e") != -1)
750
+ this.setCustomStyle({
751
+ width: cursor.clientX - dr - rect.left + "px",
752
+ left: `${rect.left}px`,
753
+ right: "auto",
754
+ });
755
+
756
+ if (prefix.indexOf("s") != -1)
757
+ this.setCustomStyle({
758
+ height: cursor.clientY - db - rect.top + "px",
759
+ top: `${rect.top}px`,
760
+ bottom: "auto",
761
+ });
762
+
763
+ if (prefix.indexOf("w") >= 0 || prefix.indexOf("n") >= 0) widget.handleMove(e, instance, this);
764
+
765
+ widget.handleResize(e, instance, this);
766
+ } else {
767
+ let prefix = this.getResizePrefix(e);
768
+ this.setCustomStyle({
769
+ cursor: prefix ? prefix + "-resize" : undefined,
770
+ });
771
+ }
772
+ }
773
+
774
+ startMoveOperation(e: MouseEvent | React.MouseEvent | React.TouchEvent) {
775
+ if (this.el && !this.getResizePrefix(e)) {
776
+ e.stopPropagation();
777
+ let rect = getTopLevelBoundingClientRect(this.el);
778
+ let cursor = getCursorPos(e);
779
+ let data = {
780
+ dx: cursor.clientX - rect.left,
781
+ dy: cursor.clientY - rect.top,
782
+ };
783
+
784
+ captureMouseOrTouch(
785
+ e,
786
+ this.onMove.bind(this),
787
+ undefined,
788
+ data,
789
+ getComputedStyle(e.target as HTMLElement).cursor,
790
+ );
791
+ }
792
+ }
793
+
794
+ onMove(e: MouseEvent, data: any) {
795
+ if (data) {
796
+ let cursor = getCursorPos(e);
797
+ e.preventDefault();
798
+ this.setCustomStyle({
799
+ left: cursor.clientX - data.dx + "px",
800
+ top: cursor.clientY - data.dy + "px",
801
+ right: "auto",
802
+ bottom: "auto",
803
+ });
804
+
805
+ let { instance } = this.props;
806
+ let { widget } = instance;
807
+ widget.handleMove(e, instance, this);
808
+ }
809
+ }
810
+
811
+ onBeforeDismiss() {
812
+ let { instance } = this.props;
813
+ let { widget } = instance;
814
+
815
+ if (widget.overlayWillDismiss && widget.overlayWillDismiss(instance, this) === false) return false;
816
+
817
+ this.dismissed = true;
818
+
819
+ //this.el might be null if visible is set to false
820
+ if (this.el) {
821
+ this.el.className = this.getOverlayCssClass();
822
+
823
+ // if (widget.animate)
824
+ // this.setState({
825
+ // animated: false
826
+ // });
827
+ }
828
+ return true;
829
+ }
830
+
831
+ componentDidMount() {
832
+ let { instance, subscribeToBeforeDismiss, parentEl } = this.props;
833
+ let { widget, data } = instance;
834
+
835
+ this.setZIndex(isNumber(data.zIndex) ? data.zIndex : ZIndexManager.next());
836
+
837
+ this.componentDidUpdate();
838
+ widget.overlayDidMount(instance, this);
839
+
840
+ if (this.containerEl) this.containerEl.style.removeProperty("display");
841
+ else if (parentEl) parentEl.style.removeProperty("display");
842
+
843
+ let childHasFocus = isSelfOrDescendant(this.el!, getActiveElement());
844
+
845
+ if (childHasFocus) oneFocusOut(this, this.el, this.onFocusOut.bind(this));
846
+ else {
847
+ if (!widget.autoFocusFirstChild || !FocusManager.focusFirstChild(this.el!))
848
+ if (widget.focusable && widget.autoFocus) FocusManager.focus(this.el!);
849
+ }
850
+
851
+ instance.onBeforeDismiss = this.onBeforeDismiss.bind(this);
852
+
853
+ if (subscribeToBeforeDismiss) {
854
+ subscribeToBeforeDismiss(instance.onBeforeDismiss);
855
+ }
856
+
857
+ if (widget.animate) {
858
+ setTimeout(() => {
859
+ if (!this.unmounting)
860
+ this.setState({
861
+ animated: true,
862
+ });
863
+ }, 0);
864
+ }
865
+
866
+ if (widget.dismissOnPopState) {
867
+ this.onPopState = () => {
868
+ this.props.instance.dismiss?.();
869
+ };
870
+ window.addEventListener("popstate", this.onPopState);
871
+ }
872
+
873
+ if (this.shadowEl)
874
+ this.unsubscribeWheelBlock = addEventListenerWithOptions(
875
+ this.shadowEl,
876
+ "wheel",
877
+ (e) => {
878
+ if (e.shiftKey || e.ctrlKey) return;
879
+ //check if there is a scrollable element within the shadow or overlay contents
880
+ //such that its scrollbar is not at the very end
881
+ let scrollAllowed = false;
882
+ closest(e.target as Element, (el) => {
883
+ if (
884
+ (e.deltaY > 0 && el.scrollTop < el.scrollHeight - el.clientHeight) ||
885
+ (e.deltaY < 0 && el.scrollTop > 0)
886
+ ) {
887
+ scrollAllowed = true;
888
+ return true;
889
+ }
890
+ return el == e.currentTarget;
891
+ });
892
+ if (!scrollAllowed) e.preventDefault();
893
+ },
894
+ { passive: false },
895
+ );
896
+ }
897
+
898
+ componentWillUnmount() {
899
+ if (this.onPopState) window.removeEventListener("popstate", this.onPopState);
900
+
901
+ if (this.unsubscribeWheelBlock) this.unsubscribeWheelBlock();
902
+
903
+ offFocusOut(this);
904
+ this.unmounting = true;
905
+
906
+ let { widget } = this.props.instance;
907
+ let { baseClass, CSS } = widget;
908
+
909
+ // //we didn't have a chance to call onBeforeDismiss
910
+ if (this.state.animated && this.el) {
911
+ this.el.className = this.getOverlayCssClass();
912
+ if (this.shadowEl)
913
+ this.shadowEl.className = CSS.element(baseClass, "shadow", {
914
+ animate: widget.animate,
915
+ "animate-leave": true,
916
+ });
917
+ }
918
+
919
+ widget.overlayWillUnmount(this.props.instance, this);
920
+
921
+ if (this.ownedEl) {
922
+ setTimeout(() => {
923
+ this.root?.unmount();
924
+ if (this.ownedEl?.parentNode) this.ownedEl.parentNode.removeChild(this.ownedEl);
925
+ this.ownedEl = null;
926
+ }, widget.destroyDelay);
927
+ }
928
+
929
+ delete this.containerEl;
930
+ }
931
+
932
+ setZIndex(zIndex: number) {
933
+ if (this.shadowEl) this.shadowEl.style.zIndex = zIndex.toString();
934
+ this.setCustomStyle({
935
+ zIndex: zIndex.toString(),
936
+ });
937
+ }
938
+
939
+ setCustomStyle(style: Partial<CSSStyleDeclaration>) {
940
+ Object.assign(this.customStyle, style);
941
+ if (this.el) Object.assign(this.el.style, this.customStyle);
942
+ }
943
+
944
+ getOverlayStyle() {
945
+ let { data } = this.props.instance;
946
+ return {
947
+ ...data.style,
948
+ ...this.customStyle,
949
+ };
950
+ }
951
+
952
+ setCSSState(mods: Record<string, boolean>) {
953
+ let m: Record<string, boolean> = { ...this.state.mods };
954
+ let changed = false;
955
+ for (let k in mods)
956
+ if (m[k] !== mods[k]) {
957
+ m[k] = mods[k];
958
+ changed = true;
959
+ }
960
+
961
+ if (changed)
962
+ this.setState({
963
+ mods: mods,
964
+ });
965
+ }
966
+
967
+ getOverlayCssClass() {
968
+ let { data, widget } = this.props.instance;
969
+ let { CSS } = widget;
970
+
971
+ return (
972
+ CSS.expand(
973
+ data.classNames,
974
+ CSS.state({
975
+ ...this.state.mods,
976
+ animated: this.state.animated && !this.unmounting && !this.dismissed,
977
+ "animate-enter": this.state.animated && !this.dismissed,
978
+ "animate-leave": widget.animate && this.dismissed,
979
+ }),
980
+ ) ?? ""
981
+ );
982
+ }
983
+
984
+ overlayDidUpdate() {
985
+ if (this.el && !this.dismissed) {
986
+ let { widget } = this.props.instance;
987
+ widget.overlayDidUpdate(this.props.instance, this);
988
+ this.el.className = this.getOverlayCssClass();
989
+ Object.assign(this.el.style, this.getOverlayStyle());
990
+ }
991
+ }
992
+
993
+ componentDidUpdate() {
994
+ if (this.containerEl && !VDOM.DOM.createPortal) {
995
+ this.root = VDOM.DOM.createRoot(this.containerEl);
996
+ this.root.render(this.renderOverlay());
997
+ }
998
+ this.overlayDidUpdate();
999
+ }
1000
+ }
1001
+
1002
+ interface OverlayBeaconProps {
1003
+ childrenFactory: (beacon: HTMLElement) => React.ReactNode;
1004
+ }
1005
+
1006
+ export interface OverlayBeaconState {
1007
+ beaconRendered?: boolean;
1008
+ }
1009
+
1010
+ export class OverlayBeacon extends VDOM.Component<OverlayBeaconProps, OverlayBeaconState> {
1011
+ el: HTMLElement | null;
1012
+ render() {
1013
+ return (
1014
+ <>
1015
+ <div
1016
+ ref={(el) => {
1017
+ this.el = el;
1018
+ }}
1019
+ style={{ position: "absolute", display: "none" }}
1020
+ />
1021
+ {this.el && this.props.childrenFactory(this.el)}
1022
+ </>
1023
+ );
1024
+ }
1025
+
1026
+ componentDidMount(): void {
1027
+ this.setState({ beaconRendered: true });
1028
+ }
1029
+ }