Use pop-up bubbles

  • Blockly's built-in icons use a pop-up bubble UI for extra information, and custom icons can be designed to maintain this consistent behavior.

  • Custom icons with bubbles need to implement the IHasBubble interface including methods to show/hide the bubble and update its position.

  • The setBubbleVisible method controls the bubble's visibility, while onLocationChange ensures the bubble moves with the icon when the block is dragged.

  • Developers need to provide methods like getAnchorLocation and getOwnerRect to correctly position the bubble relative to the icon and block.

  • The isBubbleVisible method is required to report the current visibility status of the bubble.

The built-in icons use a pop-up-bubble-style UI to display extra information or editors. Custom icons can also replicate this behavior, to maintain a consistent look-and-feel.

If your icon should show a bubble, you need to implement the IHasBubble interface.

Show or hide the bubble

Icons that use bubbles should implement the setBubbleVisible method to show or hide the bubble.

// Implement the setBubbleVisible method of the IHasBubble interface.
async setBubbleVisible(visible) {
  // State is already correct.
  if (!!this.myBubble === visible) return;

  // Wait for queued renders to finish so that the icon will be correctly
  // positioned before displaying the bubble.
  await Blockly.renderManagement.finishQueuedRenders();

  if (visible) {
    this.myBubble = new MyBubble(this.getAnchorLocation(), this.getOwnerRect());
  } else {
    this.myBubble?.dispose();
  }
}

// Implement helper methods for getting the anchor location and bounds.

// Returns the location of the middle of this icon in workspace coordinates.
getAnchorLocation() {
  const size = this.getSize();
  const midIcon = new Blockly.utils.Coordinate(size.width / 2, size.height / 2);
  return Blockly.utils.Coordinate.sum(this.workspaceLocation, midIcon);
}

// Returns the rect the bubble should avoid overlapping, i.e. the block this
// icon is appended to.
getOwnerRect() {
  const bbox = this.sourceBlock.getSvgRoot().getBBox();
  return new Blockly.utils.Rect(
      bbox.y, bbox.y + bbox.height, bbox.x, bbox.x + bbox.width);
}

Deal with dragging the block

When the icon changes location, the bubble does not automatically move with it. You either need to update the location of the bubble, or hide it. This can be done inside the onLocationChange method of the IIcon interface.

onLocationChange(blockOrigin) {
  super.onLocationChange(blockOrigin);
  this.myBubble?.setAnchorLocation(this.getAnchorLocation());
}

Return the visibility of the bubble

The IHasBubble interface also requires that you implement an bubbleIsVisible method that returns whether the bubble is visible or not.

isBubbleVisible() {
  return !!this.myBubble;
}