@jupyterlab/ui-components 4.1.0-alpha.3 → 4.1.0-alpha.4

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.
@@ -1,6 +1,13 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
3
 
4
+ import { Button } from '@jupyter/react-components';
5
+ import {
6
+ addJupyterLabThemeChangeListener,
7
+ jpButton,
8
+ jpToolbar,
9
+ provideJupyterDesignSystem
10
+ } from '@jupyter/web-components';
4
11
  import { ITranslator, nullTranslator } from '@jupyterlab/translation';
5
12
  import { find, map, some } from '@lumino/algorithm';
6
13
  import { CommandRegistry } from '@lumino/commands';
@@ -10,11 +17,13 @@ import { AttachedProperty } from '@lumino/properties';
10
17
  import { Layout, PanelLayout, Widget } from '@lumino/widgets';
11
18
  import { Throttler } from '@lumino/polling';
12
19
  import * as React from 'react';
13
- import { Button } from './button';
14
20
  import { ellipsesIcon, LabIcon } from '../icon';
15
21
  import { classes } from '../utils';
16
22
  import { ReactWidget, UseSignal } from './vdom';
17
23
 
24
+ provideJupyterDesignSystem().register([jpButton(), jpToolbar()]);
25
+ addJupyterLabThemeChangeListener();
26
+
18
27
  /**
19
28
  * The class name added to toolbars.
20
29
  */
@@ -165,7 +174,7 @@ export class Toolbar<T extends Widget = Widget> extends Widget {
165
174
  * Construct a new toolbar widget.
166
175
  */
167
176
  constructor(options: Toolbar.IOptions = {}) {
168
- super();
177
+ super({ node: document.createElement('jp-toolbar') });
169
178
  this.addClass(TOOLBAR_CLASS);
170
179
  this.layout = options.layout ?? new ToolbarLayout();
171
180
  }
@@ -362,7 +371,9 @@ export class ReactiveToolbar extends Toolbar<Widget> {
362
371
  super();
363
372
  this.insertItem(0, TOOLBAR_OPENER_NAME, this.popupOpener);
364
373
  this.popupOpener.hide();
365
- this._resizer = new Throttler(this._onResize.bind(this), 500);
374
+ this._resizer = new Throttler(async () => {
375
+ await this._onResize();
376
+ }, 300);
366
377
  }
367
378
 
368
379
  /**
@@ -421,15 +432,22 @@ export class ReactiveToolbar extends Toolbar<Widget> {
421
432
  * The item can be removed from the toolbar by setting its parent to `null`.
422
433
  */
423
434
  insertItem(index: number, name: string, widget: Widget): boolean {
435
+ let status: boolean;
424
436
  if (widget instanceof ToolbarPopupOpener) {
425
- return super.insertItem(index, name, widget);
437
+ status = super.insertItem(index, name, widget);
426
438
  } else {
427
439
  const j = Math.max(
428
440
  0,
429
441
  Math.min(index, (this.layout as ToolbarLayout).widgets.length - 1)
430
442
  );
431
- return super.insertItem(j, name, widget);
443
+ status = super.insertItem(j, name, widget);
444
+ }
445
+
446
+ // Save the widgets position when a new widget is inserted.
447
+ if (this._widgetPositions.get(name) === undefined) {
448
+ this._saveWidgetPosition();
432
449
  }
450
+ return status;
433
451
  }
434
452
 
435
453
  /**
@@ -444,92 +462,142 @@ export class ReactiveToolbar extends Toolbar<Widget> {
444
462
 
445
463
  protected onResize(msg: Widget.ResizeMessage): void {
446
464
  super.onResize(msg);
465
+
466
+ // Check if the resize event is due to a zoom change.
467
+ const zoom = Math.round((window.outerWidth / window.innerWidth) * 100);
468
+ if (zoom !== this._zoom) {
469
+ this._zoomChanged = true;
470
+ this._zoom = zoom;
471
+ }
447
472
  if (msg.width > 0 && this._resizer) {
448
473
  void this._resizer.invoke();
449
474
  }
450
475
  }
451
476
 
452
477
  private _onResize() {
453
- if (this.parent && this.parent.isAttached) {
454
- const toolbarWidth = this.node.clientWidth;
455
- const opener = this.popupOpener;
456
- const openerWidth = 30;
457
- const toolbarPadding = 2;
458
- const layout = this.layout as ToolbarLayout;
459
-
460
- let width = opener.isHidden
461
- ? toolbarPadding
462
- : toolbarPadding + openerWidth;
463
- let index = 0;
464
- const widgetsToRemove = [];
465
- const toIndex = layout.widgets.length - 1;
466
-
467
- while (index < toIndex) {
468
- const widget = layout.widgets[index];
469
- this._saveWidgetWidth(widget);
470
- width += this._getWidgetWidth(widget);
471
- if (
472
- widgetsToRemove.length === 0 &&
473
- opener.isHidden &&
474
- width + openerWidth > toolbarWidth
475
- ) {
476
- width += openerWidth;
477
- }
478
- if (width > toolbarWidth) {
479
- widgetsToRemove.push(widget);
478
+ if (!(this.parent && this.parent.isAttached)) {
479
+ return;
480
+ }
481
+ const toolbarWidth = this.node.clientWidth;
482
+ const opener = this.popupOpener;
483
+ const openerWidth = 30;
484
+ // left and right padding.
485
+ const toolbarPadding = 2 + 5;
486
+ let width = opener.isHidden ? toolbarPadding : toolbarPadding + openerWidth;
487
+
488
+ this._getWidgetsToRemove(width, toolbarWidth, openerWidth)
489
+ .then(values => {
490
+ let { width, widgetsToRemove } = values;
491
+ while (widgetsToRemove.length > 0) {
492
+ // Insert the widget in the right position in the opener popup.
493
+ const widget = widgetsToRemove.pop() as Widget;
494
+ const name = Private.nameProperty.get(widget);
495
+ width -= this._widgetWidths![name];
496
+ const position = this._widgetPositions.get(name) ?? 0;
497
+ const index =
498
+ opener.widgetCount() + position - this._widgetPositions.size;
499
+ opener.insertWidget(index, widget);
480
500
  }
481
- index++;
482
- }
501
+ if (opener.widgetCount() > 0) {
502
+ const widgetsToAdd = [];
503
+ let index = 0;
504
+ let widget = opener.widgetAt(index);
505
+ const widgetCount = opener.widgetCount();
483
506
 
484
- while (widgetsToRemove.length > 0) {
485
- const widget = widgetsToRemove.pop() as Widget;
486
- width -= this._getWidgetWidth(widget);
487
- opener.addWidget(widget);
488
- }
489
-
490
- if (opener.widgetCount() > 0) {
491
- const widgetsToAdd = [];
492
- let index = 0;
493
- let widget = opener.widgetAt(index);
494
- const widgetCount = opener.widgetCount();
507
+ width += this._getWidgetWidth(widget);
495
508
 
496
- width += this._getWidgetWidth(widget);
509
+ if (widgetCount === 1 && width - openerWidth <= toolbarWidth) {
510
+ width -= openerWidth;
511
+ }
497
512
 
498
- if (widgetCount === 1 && width - openerWidth <= toolbarWidth) {
499
- width -= openerWidth;
500
- }
513
+ while (width < toolbarWidth && index < widgetCount) {
514
+ widgetsToAdd.push(widget);
515
+ index++;
516
+ widget = opener.widgetAt(index);
517
+ if (widget) {
518
+ width += this._getWidgetWidth(widget);
519
+ } else {
520
+ break;
521
+ }
522
+ }
501
523
 
502
- while (width < toolbarWidth && index < widgetCount) {
503
- widgetsToAdd.push(widget);
504
- index++;
505
- widget = opener.widgetAt(index);
506
- if (widget) {
507
- width += this._getWidgetWidth(widget);
508
- } else {
509
- break;
524
+ while (widgetsToAdd.length > 0) {
525
+ // Insert the widget in the right position in the toolbar.
526
+ const widget = widgetsToAdd.shift()!;
527
+ const name = Private.nameProperty.get(widget);
528
+ const index = this._widgetPositions.get(name) ?? 0;
529
+ this.insertItem(index, name, widget);
510
530
  }
511
531
  }
512
532
 
513
- while (widgetsToAdd.length > 0) {
514
- const widget = widgetsToAdd.shift()!;
515
- this.addItem(Private.nameProperty.get(widget), widget);
533
+ if (opener.widgetCount() > 0) {
534
+ opener.updatePopup();
535
+ opener.show();
536
+ } else {
537
+ opener.hide();
516
538
  }
517
- }
539
+ })
540
+ .catch(msg => {
541
+ console.error('Error while computing the ReactiveToolbar', msg);
542
+ });
543
+ }
518
544
 
519
- if (opener.widgetCount() > 0) {
520
- opener.updatePopup();
521
- opener.show();
545
+ private async _getWidgetsToRemove(
546
+ width: number,
547
+ toolbarWidth: number,
548
+ openerWidth: number
549
+ ) {
550
+ const opener = this.popupOpener;
551
+ const layout = this.layout as ToolbarLayout;
552
+ const toIndex = layout.widgets.length - 1;
553
+
554
+ const widgetsToRemove = [];
555
+
556
+ let index = 0;
557
+ while (index < toIndex) {
558
+ const widget = layout.widgets[index];
559
+ // Compute the widget size only if
560
+ // - the zoom has changed.
561
+ // - the widget size has not been computed yet.
562
+ let widgetWidth: number;
563
+ if (this._zoomChanged) {
564
+ widgetWidth = await this._saveWidgetWidth(widget);
522
565
  } else {
523
- opener.hide();
566
+ widgetWidth =
567
+ this._getWidgetWidth(widget) ?? (await this._saveWidgetWidth(widget));
568
+ }
569
+ width += widgetWidth;
570
+ if (
571
+ widgetsToRemove.length === 0 &&
572
+ opener.isHidden &&
573
+ width + openerWidth > toolbarWidth
574
+ ) {
575
+ width += openerWidth;
524
576
  }
577
+ if (width > toolbarWidth) {
578
+ widgetsToRemove.push(widget);
579
+ }
580
+ index++;
525
581
  }
582
+
583
+ this._zoomChanged = false;
584
+ return {
585
+ width: width,
586
+ widgetsToRemove: widgetsToRemove
587
+ };
526
588
  }
527
589
 
528
- private _saveWidgetWidth(widget: Widget) {
590
+ private async _saveWidgetWidth(widget: Widget): Promise<number> {
591
+ if (widget instanceof ReactWidget) {
592
+ await widget.renderPromise;
593
+ }
529
594
  const widgetName = Private.nameProperty.get(widget);
530
- this._widgetWidths![widgetName] = widget.hasClass(TOOLBAR_SPACER_CLASS)
595
+
596
+ const widgetWidth = widget.hasClass(TOOLBAR_SPACER_CLASS)
531
597
  ? 2
532
598
  : widget.node.clientWidth;
599
+ this._widgetWidths![widgetName] = widgetWidth;
600
+ return widgetWidth;
533
601
  }
534
602
 
535
603
  private _getWidgetWidth(widget: Widget): number {
@@ -537,9 +605,21 @@ export class ReactiveToolbar extends Toolbar<Widget> {
537
605
  return this._widgetWidths![widgetName];
538
606
  }
539
607
 
608
+ private _saveWidgetPosition(): void {
609
+ this._widgetPositions.clear();
610
+ (this.layout as ToolbarLayout).widgets.forEach((widget, index) => {
611
+ this._widgetPositions.set(Private.nameProperty.get(widget), index);
612
+ });
613
+ }
614
+
540
615
  protected readonly popupOpener: ToolbarPopupOpener = new ToolbarPopupOpener();
541
616
  private readonly _widgetWidths: { [key: string]: number } = {};
542
617
  private readonly _resizer: Throttler;
618
+ private _widgetPositions = new Map<string, number>();
619
+ // The zoom property is not the real browser zoom, but a value proportional to
620
+ // the zoom, which is modified when the zoom changes.
621
+ private _zoom: number;
622
+ private _zoomChanged = true;
543
623
  }
544
624
 
545
625
  /**
@@ -626,17 +706,32 @@ export namespace ToolbarButtonComponent {
626
706
  export function ToolbarButtonComponent(
627
707
  props: ToolbarButtonComponent.IProps
628
708
  ): JSX.Element {
709
+ const actualOnClick = props.actualOnClick ?? false;
710
+
629
711
  // In some browsers, a button click event moves the focus from the main
630
712
  // content to the button (see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus).
631
713
  // We avoid a click event by calling preventDefault in mousedown, and
632
714
  // we bind the button action to `mousedown`.
633
- const handleMouseDown = (event: React.MouseEvent) => {
634
- // Fire action only when left button is pressed.
635
- if (event.button === 0) {
636
- event.preventDefault();
637
- props.onClick?.();
638
- }
639
- };
715
+ const handleMouseDown = actualOnClick
716
+ ? undefined
717
+ : (event: React.MouseEvent) => {
718
+ // Fire action only when left button is pressed.
719
+ if (event.button === 0) {
720
+ event.preventDefault();
721
+ props.onClick?.();
722
+ }
723
+ };
724
+
725
+ const handleClick = actualOnClick
726
+ ? (event: React.MouseEvent) => {
727
+ if (event.button === 0) {
728
+ props.onClick?.();
729
+ }
730
+ }
731
+ : (event: React.MouseEvent) => {
732
+ // Prevent the toolbar widget to steal focus
733
+ event.stopPropagation();
734
+ };
640
735
 
641
736
  const handleKeyDown = (event: React.KeyboardEvent) => {
642
737
  const { key } = event;
@@ -645,12 +740,6 @@ export function ToolbarButtonComponent(
645
740
  }
646
741
  };
647
742
 
648
- const handleClick = (event: React.MouseEvent) => {
649
- if (event.button === 0) {
650
- props.onClick?.();
651
- }
652
- };
653
-
654
743
  const getTooltip = () => {
655
744
  if (props.enabled === false && props.disabledTooltip) {
656
745
  return props.disabledTooltip;
@@ -663,6 +752,7 @@ export function ToolbarButtonComponent(
663
752
 
664
753
  return (
665
754
  <Button
755
+ appearance="stealth"
666
756
  className={
667
757
  props.className
668
758
  ? props.className + ' jp-ToolbarButtonComponent'
@@ -672,24 +762,20 @@ export function ToolbarButtonComponent(
672
762
  aria-disabled={props.enabled === false}
673
763
  {...props.dataset}
674
764
  disabled={props.enabled === false}
675
- onClick={props.actualOnClick ?? false ? handleClick : undefined}
676
- onMouseDown={
677
- !(props.actualOnClick ?? false) ? handleMouseDown : undefined
678
- }
765
+ onClick={handleClick}
766
+ onMouseDown={handleMouseDown}
679
767
  onKeyDown={handleKeyDown}
680
768
  title={getTooltip()}
681
769
  minimal
682
770
  >
683
771
  {(props.icon || props.iconClass) && (
684
772
  <LabIcon.resolveReact
685
- icon={props.pressed ? props.pressedIcon : props.icon}
773
+ icon={props.pressed ? props.pressedIcon ?? props.icon : props.icon}
686
774
  iconClass={
687
775
  // add some extra classes for proper support of icons-as-css-background
688
776
  classes(props.iconClass, 'jp-Icon')
689
777
  }
690
- className="jp-ToolbarButtonComponent-icon"
691
- tag="span"
692
- stylesheet="toolbarButton"
778
+ tag={null}
693
779
  />
694
780
  )}
695
781
  {props.label && (
@@ -849,7 +935,11 @@ export function CommandToolbarButtonComponent(
849
935
  args.type === 'many-changed'
850
936
  }
851
937
  >
852
- {() => <ToolbarButtonComponent {...Private.propsFromCommand(props)} />}
938
+ {() =>
939
+ props.commands.listCommands().includes(props.id) ? (
940
+ <ToolbarButtonComponent {...Private.propsFromCommand(props)} />
941
+ ) : null
942
+ }
853
943
  </UseSignal>
854
944
  );
855
945
  }
@@ -892,7 +982,8 @@ class ToolbarPopup extends Widget {
892
982
  * Construct a new ToolbarPopup
893
983
  */
894
984
  constructor() {
895
- super();
985
+ super({ node: document.createElement('jp-toolbar') });
986
+ this.addClass('jp-Toolbar');
896
987
  this.addClass('jp-Toolbar-responsive-popup');
897
988
  this.layout = new PanelLayout();
898
989
  Widget.attach(this, document.body);
@@ -937,7 +1028,7 @@ class ToolbarPopup extends Widget {
937
1028
  * @param widget widget to add
938
1029
  */
939
1030
  insertWidget(index: number, widget: Widget) {
940
- (this.layout as PanelLayout).insertWidget(0, widget);
1031
+ (this.layout as PanelLayout).insertWidget(index, widget);
941
1032
  }
942
1033
 
943
1034
  /**
@@ -987,6 +1078,14 @@ class ToolbarPopupOpener extends ToolbarButton {
987
1078
  this.popup.insertWidget(0, widget);
988
1079
  }
989
1080
 
1081
+ /**
1082
+ * Insert widget to the popup.
1083
+ * @param widget the widget to add
1084
+ */
1085
+ insertWidget(index: number, widget: Widget) {
1086
+ this.popup.insertWidget(index, widget);
1087
+ }
1088
+
990
1089
  /**
991
1090
  * Dispose of the widget and its descendant widgets.
992
1091
  *
@@ -1067,9 +1166,13 @@ namespace Private {
1067
1166
 
1068
1167
  const label = commands.label(id, args);
1069
1168
  let className = commands.className(id, args);
1070
- // Add the boolean state classes.
1071
- if (commands.isToggled(id, args)) {
1072
- className += ' lm-mod-toggled';
1169
+ // Add the boolean state classes and aria attributes.
1170
+ let pressed;
1171
+ if (commands.isToggleable(id, args)) {
1172
+ pressed = commands.isToggled(id, args);
1173
+ if (pressed) {
1174
+ className += ' lm-mod-toggled';
1175
+ }
1073
1176
  }
1074
1177
  if (!commands.isVisible(id, args)) {
1075
1178
  className += ' lm-mod-hidden';
@@ -1096,7 +1199,8 @@ namespace Private {
1096
1199
  tooltip: options.caption ?? tooltip,
1097
1200
  onClick,
1098
1201
  enabled,
1099
- label: options.label ?? label
1202
+ label: options.label ?? label,
1203
+ pressed
1100
1204
  };
1101
1205
  }
1102
1206
 
@@ -332,28 +332,39 @@ export class LabIcon implements LabIcon.ILabIcon, VirtualElement.IRenderer {
332
332
  return document.createElement('div');
333
333
  }
334
334
 
335
- let returnSvgElement = true;
336
335
  if (container) {
337
336
  // take ownership by removing any existing children
338
337
  while (container.firstChild) {
339
338
  container.firstChild.remove();
340
339
  }
341
- } else {
340
+ } else if (tag) {
342
341
  // create a container if needed
343
342
  container = document.createElement(tag);
343
+ }
344
344
 
345
- returnSvgElement = false;
345
+ const svgElement = this.svgElement.cloneNode(true) as HTMLElement;
346
+ if (!container) {
347
+ if (label) {
348
+ console.warn();
349
+ }
350
+ return svgElement;
346
351
  }
352
+
347
353
  if (label != null) {
348
354
  container.textContent = label;
349
355
  }
350
- Private.initContainer({ container, className, styleProps, title });
356
+
357
+ Private.initContainer({
358
+ container: container!,
359
+ className,
360
+ styleProps,
361
+ title
362
+ });
351
363
 
352
364
  // add the svg node to the container
353
- const svgElement = this.svgElement.cloneNode(true) as HTMLElement;
354
365
  container.appendChild(svgElement);
355
366
 
356
- return returnSvgElement ? svgElement : container;
367
+ return container;
357
368
  }
358
369
 
359
370
  render(container: HTMLElement, options?: LabIcon.IRendererOptions): void {
@@ -468,7 +479,7 @@ export class LabIcon implements LabIcon.ILabIcon, VirtualElement.IRenderer {
468
479
  });
469
480
 
470
481
  // make it so that tag can be used as a jsx component
471
- const Tag = tag;
482
+ const Tag = tag ?? React.Fragment;
472
483
 
473
484
  // ensure that svg html is valid
474
485
  if (!(this.svgInnerHTML && this.svgReactAttrs)) {
@@ -494,15 +505,18 @@ export class LabIcon implements LabIcon.ILabIcon, VirtualElement.IRenderer {
494
505
  </React.Fragment>
495
506
  );
496
507
  } else {
497
- return (
498
- <Tag
499
- className={
508
+ let attributes = {};
509
+ if (Tag !== React.Fragment) {
510
+ attributes = {
511
+ className:
500
512
  className || styleProps
501
513
  ? classes(className, LabIconStyle.styleClass(styleProps))
502
- : undefined
503
- }
504
- title={title}
505
- >
514
+ : undefined,
515
+ title: title
516
+ };
517
+ }
518
+ return (
519
+ <Tag {...attributes}>
506
520
  {svgComponent}
507
521
  {label}
508
522
  </Tag>
@@ -670,8 +684,12 @@ export namespace LabIcon {
670
684
  /**
671
685
  * HTML element tag used to create the icon's outermost container node,
672
686
  * if no container is passed in
687
+ *
688
+ * #### Notes
689
+ * If `null` is provided and no container is defined, the icon SVG will return directly
690
+ * ignoring all other attributes (label, title,...)
673
691
  */
674
- tag?: 'div' | 'span';
692
+ tag?: 'div' | 'span' | null;
675
693
 
676
694
  /**
677
695
  * Optional title that will be set on the icon's outermost container node
@@ -741,7 +759,7 @@ namespace Private {
741
759
  }
742
760
  } else {
743
761
  // create a container if needed
744
- container = document.createElement(tag);
762
+ container = document.createElement(tag ?? 'div');
745
763
  }
746
764
  if (label != null) {
747
765
  container.textContent = label;
@@ -764,7 +782,7 @@ namespace Private {
764
782
  ref: LabIcon.IReactRef
765
783
  ) => {
766
784
  // make it so that tag can be used as a jsx component
767
- const Tag = tag;
785
+ const Tag = tag ?? 'div';
768
786
 
769
787
  if (container) {
770
788
  initContainer({ container, className, styleProps, title });