@jupyterlab/ui-components 4.7.0-alpha.2 → 4.7.0-beta.1

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.
@@ -327,6 +327,10 @@ export class Toolbar<T extends Widget = Widget> extends Widget {
327
327
  */
328
328
  handleEvent(event: Event): void {
329
329
  switch (event.type) {
330
+ case 'pointerdown':
331
+ case 'keydown':
332
+ this._focusBeforeClick = document.activeElement;
333
+ break;
330
334
  case 'click':
331
335
  this.handleClick(event);
332
336
  break;
@@ -342,6 +346,9 @@ export class Toolbar<T extends Widget = Widget> extends Widget {
342
346
  // Stop propagating the click outside the toolbar
343
347
  event.stopPropagation();
344
348
 
349
+ const focusBeforeClick = this._focusBeforeClick;
350
+ this._focusBeforeClick = null;
351
+
345
352
  // Clicking a label focuses the corresponding control
346
353
  // that is linked with `for` attribute, so let it be.
347
354
  if (event.target instanceof HTMLLabelElement) {
@@ -356,6 +363,16 @@ export class Toolbar<T extends Widget = Widget> extends Widget {
356
363
  return;
357
364
  }
358
365
 
366
+ // If this click moved the focus out of the toolbar, for example to a menu,
367
+ // let it be. The focus is on the body when the click did not focus anything.
368
+ if (
369
+ focusBeforeClick !== null &&
370
+ document.activeElement !== focusBeforeClick &&
371
+ document.activeElement !== document.body
372
+ ) {
373
+ return;
374
+ }
375
+
359
376
  // Otherwise, activate the parent widget, which may take focus if desired.
360
377
  if (this.parent) {
361
378
  this.parent.activate();
@@ -367,6 +384,8 @@ export class Toolbar<T extends Widget = Widget> extends Widget {
367
384
  */
368
385
  protected onAfterAttach(msg: Message): void {
369
386
  this.node.addEventListener('click', this);
387
+ this.node.addEventListener('pointerdown', this);
388
+ this.node.addEventListener('keydown', this);
370
389
  }
371
390
 
372
391
  /**
@@ -374,9 +393,12 @@ export class Toolbar<T extends Widget = Widget> extends Widget {
374
393
  */
375
394
  protected onBeforeDetach(msg: Message): void {
376
395
  this.node.removeEventListener('click', this);
396
+ this.node.removeEventListener('pointerdown', this);
397
+ this.node.removeEventListener('keydown', this);
377
398
  }
378
399
 
379
400
  noFocusOnClick: boolean;
401
+ private _focusBeforeClick: Element | null = null;
380
402
  }
381
403
 
382
404
  /**
@@ -390,8 +412,8 @@ export class ReactiveToolbar extends Toolbar<Widget> {
390
412
  super(options);
391
413
  this.insertItem(0, TOOLBAR_OPENER_NAME, this.popupOpener);
392
414
  this.popupOpener.hide();
393
- this._resizer = new Throttler(async (callTwice = false) => {
394
- await this._onResize(callTwice);
415
+ this._resizer = new Throttler(async () => {
416
+ await this._onResize();
395
417
  }, 500);
396
418
  }
397
419
 
@@ -406,11 +428,13 @@ export class ReactiveToolbar extends Toolbar<Widget> {
406
428
  if (this._resizer) {
407
429
  this._resizer.dispose();
408
430
  }
431
+ this._itemResizeObserver.disconnect();
409
432
 
410
433
  super.dispose();
411
434
  }
412
435
 
413
436
  protected onChildRemoved(msg: Widget.ChildMessage): void {
437
+ this._itemResizeObserver.unobserve(msg.child.node);
414
438
  // If the widget is being moved to the popup opener, keep tracking its
415
439
  // intended position so it can be restored to the right slot later.
416
440
  // NOTE: we cannot check msg.child.parent here because Lumino fires
@@ -504,6 +528,11 @@ export class ReactiveToolbar extends Toolbar<Widget> {
504
528
  Math.min(index, (this.layout as ToolbarLayout).widgets.length - 1)
505
529
  );
506
530
  status = super.insertItem(j, name, widget);
531
+ if (status && !widget.hasClass(TOOLBAR_SPACER_CLASS)) {
532
+ // Watch the border box: a padding change, for example from a theme,
533
+ // leaves the content box as it is but changes the width the layout uses.
534
+ this._itemResizeObserver.observe(widget.node, { box: 'border-box' });
535
+ }
507
536
 
508
537
  if (j !== index) {
509
538
  // This happens if the widget has been inserted at a wrong position:
@@ -539,11 +568,9 @@ export class ReactiveToolbar extends Toolbar<Widget> {
539
568
  this._widgetPositions.set(name, index);
540
569
 
541
570
  // Invokes resizing to ensure correct display of items after an addition, only
542
- // if the toolbar is rendered. Call the resizer twice (callTwice = true) so that
543
- // widgets that have not yet been painted (clientWidth === 0) get a chance to be
544
- // rendered before the second pass recalculates the layout.
571
+ // if the toolbar is rendered.
545
572
  if (this.isVisible) {
546
- void this._resizer.invoke(true);
573
+ void this._resizer.invoke();
547
574
  }
548
575
  }
549
576
  return status;
@@ -555,7 +582,7 @@ export class ReactiveToolbar extends Toolbar<Widget> {
555
582
  * Invokes resizing to ensure correct display of items.
556
583
  */
557
584
  onAfterShow(msg: Message): void {
558
- void this._resizer.stop().then(() => void this._resizer.invoke(true));
585
+ void this._resizer.stop().then(() => void this._resizer.invoke());
559
586
  }
560
587
 
561
588
  /**
@@ -570,14 +597,28 @@ export class ReactiveToolbar extends Toolbar<Widget> {
570
597
 
571
598
  protected onResize(msg: Widget.ResizeMessage): void {
572
599
  super.onResize(msg);
600
+ if (msg.width > 0 && this._resizer) {
601
+ void this._resizer.invoke();
602
+ }
603
+ }
573
604
 
574
- // Check if the resize event is due to a zoom change.
575
- const zoom = Math.round((window.outerWidth / window.innerWidth) * 100);
576
- if (zoom !== this._zoom) {
577
- this._zoomChanged = true;
578
- this._zoom = zoom;
605
+ /**
606
+ * Invoke the resizer when an item in the toolbar no longer has the width
607
+ * used to place the items, for example when the kernel name changes.
608
+ */
609
+ private _onItemResize(entries: ResizeObserverEntry[]): void {
610
+ if (!this.isVisible) {
611
+ return;
579
612
  }
580
- if (msg.width > 0 && this._resizer) {
613
+ const widgets = (this.layout as ToolbarLayout).widgets;
614
+ const resized = entries.some(entry => {
615
+ const widget = widgets.find(w => w.node === entry.target);
616
+ return (
617
+ widget !== undefined &&
618
+ widget.node.clientWidth !== this._getWidgetWidth(widget)
619
+ );
620
+ });
621
+ if (resized) {
581
622
  void this._resizer.invoke();
582
623
  }
583
624
  }
@@ -585,16 +626,19 @@ export class ReactiveToolbar extends Toolbar<Widget> {
585
626
  /**
586
627
  * Move the toolbar items between the reactive toolbar and the popup toolbar,
587
628
  * depending on the width of the toolbar and the width of each item.
588
- *
589
- * @param callTwice - whether to call the function twice.
590
- *
591
- * **NOTES**
592
- * The `callTwice` parameter is useful when the toolbar is displayed the first time,
593
- * because the size of the items is unknown before their first rendering. The first
594
- * call will usually add all the items in the main toolbar, and the second call will
595
- * reorganize the items between the main toolbar and the popup toolbar.
596
629
  */
597
- private async _onResize(callTwice = false) {
630
+ private async _onResize() {
631
+ // Wait for all the items to render before measuring any of them. Waiting
632
+ // between two measurements would let an item that is already measured
633
+ // change its width, and the resizer drops the call that the item observer
634
+ // makes while this method runs.
635
+ await Promise.all(
636
+ (this.layout as ToolbarLayout).widgets.map(widget =>
637
+ widget instanceof ReactWidget ? widget.renderPromise : undefined
638
+ )
639
+ ).catch(msg => {
640
+ console.error('Error while computing the ReactiveToolbar', msg);
641
+ });
598
642
  if (!(this.parent && this.parent.isAttached)) {
599
643
  return;
600
644
  }
@@ -673,9 +717,6 @@ export class ReactiveToolbar extends Toolbar<Widget> {
673
717
  } else {
674
718
  opener.hide();
675
719
  }
676
- if (callTwice) {
677
- await this._onResize();
678
- }
679
720
  })
680
721
  .catch(msg => {
681
722
  console.error('Error while computing the ReactiveToolbar', msg);
@@ -690,6 +731,16 @@ export class ReactiveToolbar extends Toolbar<Widget> {
690
731
  const opener = this.popupOpener;
691
732
  const widgets = [...(this.layout as ToolbarLayout).widgets];
692
733
  const toIndex = widgets.length - 1;
734
+ const widths = widgets
735
+ .slice(0, toIndex)
736
+ .map(widget =>
737
+ this._saveWidgetWidth(Private.nameProperty.get(widget), widget)
738
+ );
739
+ // Count the opener only if the items do not all fit without it. Otherwise
740
+ // the last items move to the popup although they all fit in the toolbar.
741
+ const fitWithoutOpener =
742
+ widths.reduce((total, itemWidth) => total + itemWidth, width) <=
743
+ toolbarWidth;
693
744
 
694
745
  const widgetsToRemove = [];
695
746
 
@@ -697,23 +748,11 @@ export class ReactiveToolbar extends Toolbar<Widget> {
697
748
  while (index < toIndex) {
698
749
  const widget = widgets[index];
699
750
  const name = Private.nameProperty.get(widget);
700
- // Compute the widget size only if
701
- // - the zoom has changed.
702
- // - the widget size has not been computed yet.
703
- let widgetWidth: number;
704
- if (this._zoomChanged) {
705
- widgetWidth = await this._saveWidgetWidth(name, widget);
706
- } else {
707
- // The widget widths can be 0px if it has been added to the toolbar but
708
- // not rendered, this is why we must use '||' instead of '??'.
709
- widgetWidth =
710
- this._getWidgetWidth(widget) ||
711
- (await this._saveWidgetWidth(name, widget));
712
- }
713
- width += widgetWidth;
751
+ width += widths[index];
714
752
  if (
715
753
  widgetsToRemove.length === 0 &&
716
754
  opener.isHidden &&
755
+ !fitWithoutOpener &&
717
756
  width + openerWidth > toolbarWidth
718
757
  ) {
719
758
  width += openerWidth;
@@ -730,20 +769,13 @@ export class ReactiveToolbar extends Toolbar<Widget> {
730
769
  }
731
770
  index++;
732
771
  }
733
- this._zoomChanged = false;
734
772
  return {
735
773
  width: width,
736
774
  widgetsToRemove: widgetsToRemove
737
775
  };
738
776
  }
739
777
 
740
- private async _saveWidgetWidth(
741
- name: string,
742
- widget: Widget
743
- ): Promise<number> {
744
- if (widget instanceof ReactWidget) {
745
- await widget.renderPromise;
746
- }
778
+ private _saveWidgetWidth(name: string, widget: Widget): number {
747
779
  const widgetWidth = widget.hasClass(TOOLBAR_SPACER_CLASS)
748
780
  ? 2
749
781
  : widget.node.clientWidth;
@@ -761,10 +793,9 @@ export class ReactiveToolbar extends Toolbar<Widget> {
761
793
  private readonly _widgetWidths = new Map<string, number>();
762
794
  private _widgetPositions = new Map<string, number>();
763
795
  private _movingToPopup = new Set<Widget>();
764
- // The zoom property is not the real browser zoom, but a value proportional to
765
- // the zoom, which is modified when the zoom changes.
766
- private _zoom: number;
767
- private _zoomChanged = true;
796
+ private _itemResizeObserver = new ResizeObserver(
797
+ this._onItemResize.bind(this)
798
+ );
768
799
  }
769
800
 
770
801
  /**
@@ -884,8 +915,12 @@ export function ToolbarButtonComponent(
884
915
 
885
916
  const handleKeyDown = (event: React.KeyboardEvent) => {
886
917
  const { key } = event;
887
- if (key === 'Enter' || key === ' ') {
888
- props.onClick?.(event);
918
+ if ((key === 'Enter' || key === ' ') && props.onClick) {
919
+ // Stop the browser from clicking the button, and then also
920
+ // passing the key to whatever the action focused
921
+ // (a cell editor, a dialog button).
922
+ event.preventDefault();
923
+ props.onClick(event);
889
924
  }
890
925
  };
891
926
 
@@ -962,7 +997,7 @@ export class ToolbarButton extends ReactWidget {
962
997
  addToolbarButtonClass(this);
963
998
  this._enabled = props.enabled ?? true;
964
999
  this._pressed = this._enabled! && (props.pressed ?? false);
965
- this._onClick = props.onClick!;
1000
+ this._onClick = props.onClick;
966
1001
  }
967
1002
 
968
1003
  /**
@@ -1024,7 +1059,7 @@ export class ToolbarButton extends ReactWidget {
1024
1059
  this.pressed = !this.pressed;
1025
1060
 
1026
1061
  // Call the original click handler, if defined
1027
- this._onClick(event);
1062
+ this._onClick?.(event);
1028
1063
  };
1029
1064
  }
1030
1065
 
@@ -1035,14 +1070,14 @@ export class ToolbarButton extends ReactWidget {
1035
1070
  noFocusOnClick={this.props.noFocusOnClick}
1036
1071
  pressed={this.pressed}
1037
1072
  enabled={this.enabled}
1038
- onClick={this.onClick}
1073
+ onClick={this._onClick ? this.onClick : undefined}
1039
1074
  />
1040
1075
  );
1041
1076
  }
1042
1077
 
1043
1078
  private _pressed: boolean;
1044
1079
  private _enabled: boolean;
1045
- private _onClick: (event?: React.SyntheticEvent) => void;
1080
+ private _onClick?: (event?: React.SyntheticEvent) => void;
1046
1081
  }
1047
1082
 
1048
1083
  /**
@@ -1135,7 +1170,7 @@ export class CommandToolbarButton extends ReactWidget {
1135
1170
  addCommandToolbarButtonClass(this);
1136
1171
  this.setCommandAttributes(commands, id, args);
1137
1172
  commands.commandChanged.connect((_, change) => {
1138
- if (change.id === props.id) {
1173
+ if (change.id === props.id || change.type === 'many-changed') {
1139
1174
  this.setCommandAttributes(commands, id, args);
1140
1175
  }
1141
1176
  }, this);
@@ -60,15 +60,40 @@ export abstract class ReactWidget extends Widget {
60
60
  MessageLoop.sendMessage(this, Widget.Msg.UpdateRequest);
61
61
  }
62
62
 
63
+ /**
64
+ * The parent of the widget.
65
+ */
66
+ get parent(): Widget | null {
67
+ return super.parent;
68
+ }
69
+ set parent(value: Widget | null) {
70
+ // A move to a detached parent, or to one without a layout to attach the
71
+ // widget, unmounts at once, while the node is still in the document.
72
+ this._moving = value !== null && value.isAttached && value.layout !== null;
73
+ try {
74
+ super.parent = value;
75
+ } finally {
76
+ this._moving = false;
77
+ }
78
+ }
79
+
63
80
  /**
64
81
  * Called before the widget is detached from the DOM.
65
82
  */
66
83
  protected onBeforeDetach(msg: Message): void {
67
- // Unmount the component so it can tear down.
68
- if (this._rootDOM !== null) {
69
- this._rootDOM.unmount();
70
- this._rootDOM = null;
84
+ if (this._moving) {
85
+ // A widget moved to an attached parent is attached again synchronously
86
+ // and keeps its component state; unmount if the layout did not attach it.
87
+ queueMicrotask(() => {
88
+ if (!this.isAttached) {
89
+ this._unmount();
90
+ }
91
+ });
92
+ return;
71
93
  }
94
+ // Unmount while the node is in the document, so that cleanups can still
95
+ // detach or measure what they rendered.
96
+ this._unmount();
72
97
  }
73
98
 
74
99
  /**
@@ -78,14 +103,27 @@ export abstract class ReactWidget extends Widget {
78
103
  if (this.isDisposed) {
79
104
  return;
80
105
  }
81
- // `onBeforeDetach` unmounts on a normal detach, but a widget disposed
82
- // while already detached would keep its React root mounted forever,
83
- // along with every subscription made by the rendered components.
84
- if (this._rootDOM !== null) {
85
- this._rootDOM.unmount();
106
+ // A widget disposed while already detached receives no `before-detach`
107
+ // message, and without this call its React root would stay mounted with
108
+ // every subscription of its components.
109
+ try {
110
+ this._unmount();
111
+ } finally {
112
+ super.dispose();
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Unmount the React tree, if one is mounted.
118
+ */
119
+ private _unmount(): void {
120
+ const root = this._rootDOM;
121
+ if (root !== null) {
122
+ // Forget the root first: a cleanup that throws would otherwise leave a
123
+ // root that cannot render again.
86
124
  this._rootDOM = null;
125
+ root.unmount();
87
126
  }
88
- super.dispose();
89
127
  }
90
128
 
91
129
  /**
@@ -113,8 +151,7 @@ export abstract class ReactWidget extends Widget {
113
151
  requestIdleCallback(() => resolve());
114
152
  } else {
115
153
  // If the virtual node is null, unmount the node content
116
- this._rootDOM.unmount();
117
- this._rootDOM = null;
154
+ this._unmount();
118
155
  requestIdleCallback(() => resolve());
119
156
  }
120
157
  });
@@ -123,6 +160,7 @@ export abstract class ReactWidget extends Widget {
123
160
  // Set whenever a new render is triggered and resolved when it is finished.
124
161
  renderPromise?: Promise<void>;
125
162
  private _rootDOM: Root | null = null;
163
+ private _moving = false;
126
164
  }
127
165
 
128
166
  /**
@@ -1302,6 +1302,24 @@ export class WindowedList<
1302
1302
  const { clientHeight, scrollHeight, scrollTop } =
1303
1303
  event.currentTarget as HTMLDivElement;
1304
1304
 
1305
+ // A scroll that the list did not request, such as one with the mouse
1306
+ // wheel, stops the list from scrolling back to the last requested
1307
+ // item. The type check excludes the synthetic onScroll call made
1308
+ // when windowing is toggled, which carries no event type.
1309
+ // When the list requests an offset past the end (such as the last
1310
+ // item plus a margin), the browser scrolls only to the end, so the
1311
+ // comparison limits the offset to the end.
1312
+ if (
1313
+ event.type === 'scroll' &&
1314
+ !this._scrollUpdateWasRequested &&
1315
+ Math.abs(
1316
+ Math.min(this.viewModel.scrollOffset, scrollHeight - clientHeight) -
1317
+ scrollTop
1318
+ ) > 1
1319
+ ) {
1320
+ this._scrollToItem = null;
1321
+ }
1322
+
1305
1323
  // TBC Firefox is emitting two events one with 1px diff then the _real_ scroll
1306
1324
  if (
1307
1325
  !this._scrollUpdateWasRequested &&
@@ -1,3 +1,3 @@
1
1
  <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
2
- <path fill="#616161" d="M5 2h10l5 5v13a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-6h1v2l4-3-4-3v2H3V4a2 2 0 0 1 2-2m7 16h4v-2h-4zm0-4h6v-2h-6zm0-4h6V8h-6zm-2 4a1 1 0 1 0 0-2 1 1 0 0 0 0 2m-7-2v2H1v-2z" class="jp-icon3"/>
2
+ <path fill="#616161" d="M5 2h10l5 5v13a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-6h1v2l4-3-4-3v2H3V4a2 2 0 0 1 2-2m7 16h4v-2h-4zm0-4h6v-2h-6zm0-4h6V8h-6zm-2 4a1 1 0 1 0 0-2 1 1 0 0 0 0 2m-7-2v2H1v-2z" class="jp-icon3 jp-icon-selectable"/>
3
3
  </svg>
@@ -112,3 +112,46 @@
112
112
  text-overflow: ellipsis;
113
113
  white-space: nowrap;
114
114
  }
115
+
116
+ .jp-AccordionPanel-dragHandle {
117
+ display: none;
118
+ touch-action: none;
119
+ align-items: center;
120
+ flex-shrink: 0;
121
+ padding: 0 4px;
122
+ opacity: 0.5;
123
+ cursor: grab;
124
+ }
125
+
126
+ .jp-AccordionPanel-dragHandle::before {
127
+ content: '⠿';
128
+ font-size: var(--jp-ui-font-size1);
129
+ line-height: 1;
130
+ color: var(--jp-ui-font-color1);
131
+ }
132
+
133
+ .jp-mod-dragging .jp-AccordionPanel-dragHandle {
134
+ cursor: grabbing;
135
+ }
136
+
137
+ .jp-AccordionPanel-dropIndicator {
138
+ position: absolute;
139
+ left: 0;
140
+ right: 0;
141
+ height: 2px;
142
+ background-color: var(--jp-brand-color1);
143
+ pointer-events: none;
144
+ z-index: 10;
145
+ }
146
+
147
+ .jp-mod-reorderable > .lm-AccordionPanel-title > .jp-AccordionPanel-dragHandle {
148
+ display: inline-flex;
149
+ }
150
+
151
+ .lm-AccordionPanel[data-orientation='horizontal']
152
+ > .jp-AccordionPanel-dropIndicator {
153
+ top: 0;
154
+ bottom: 0;
155
+ width: 2px;
156
+ height: auto;
157
+ }