Create custom bubbles
Stay organized with collections
Save and categorize content based on your preferences.
A bubble is a pop up UI that looks like a speech bubble you would see in a
comic. They have a "tail" that points at a block, and a "head" that contains
arbitrary svg elements.

If the built-in bubbles don't work for your use case, you
can create a custom bubble by subclassing the Bubble
class.
Create the view
A bubble's view is all of the svg elements that live inside the "head" of the
bubble. These elements are created inside of the bubble's constructor, and they
should be children of the this.contentContainer
element so that they get
automatically cleaned up when the icon is destroyed.
The Blockly.utils.dom
module provides a clean interface
for instantiating svgs.
class MyBubble extends Blockly.bubbles.Bubble {
// See the Blockly.bubbles.Bubble class for information about what these
// parameters are.
constructor(workspace, anchor, ownerRect) {
super(workspace, anchor, ownerRect);
this.text = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.TEXT,
{'class': 'my-bubble-class'},
this.contentContainer);
const node = Blockly.utils.dom.createTextNode('some text');
this.text.appendChild(node);
}
}
Set the size
The bubble's size needs to be set using setSize
so that the outer
border can properly surround the contents of the bubble. It should be set
during construction, and whenever the size of the UI elements change.
constructor(workspace, anchor, ownerRect) {
// Create the view elements... (see above)
const bbox = this.text.getBBox();
this.setSize(
new Blockly.utils.Size(
bbox.width + Blockly.bubbles.Bubble.BORDER_WIDTH * 2,
bbox.height + Blockly.bubbles.Bubble.BORDER_WIDTH * 2,
),
true
);
}
Dispose of the bubble
Bubbles should clean up any dom elements or external references when they are
disposed. By default, anything that is appended to this.contentContainer
gets
destroyed, but other references need to be manually cleaned up. This should be
done within the dispose
method.
dispose() {
super.dispose();
// Dispose of other references.
this.myArbitraryReference.dispose();
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-04-14 UTC."],[[["\u003cp\u003eBubbles are pop-up UI elements resembling speech bubbles with a tail pointing to a block and a head containing SVG elements.\u003c/p\u003e\n"],["\u003cp\u003eCustom bubbles can be created by extending the \u003ccode\u003eBlockly.bubbles.Bubble\u003c/code\u003e class and defining the SVG elements within the constructor.\u003c/p\u003e\n"],["\u003cp\u003eThe size of the bubble needs to be set using \u003ccode\u003esetSize\u003c/code\u003e to accommodate the content and border width.\u003c/p\u003e\n"],["\u003cp\u003eWhen disposing of a bubble, ensure all DOM elements and external references are cleaned up within the \u003ccode\u003edispose\u003c/code\u003e method.\u003c/p\u003e\n"]]],["Bubbles are pop-up UIs with a tail pointing to a block and a head containing SVG elements. Custom bubbles can be created by subclassing the `Bubble` class. Inside the constructor, SVG elements are created using the `Blockly.utils.dom` module and added to `this.contentContainer`. The `setSize` method, called during construction, adjusts the bubble's border based on the content's size. The `dispose` method must clean up all references beyond those in `this.contentContainer`.\n"],null,["# Create custom bubbles\n\nA bubble is a pop up UI that looks like a speech bubble you would see in a\ncomic. They have a \"tail\" that points at a block, and a \"head\" that contains\narbitrary svg elements.\n\nIf the [built-in bubbles](/blockly/reference/js/blockly.bubbles_namespace) don't work for your use case, you\ncan create a custom bubble by subclassing the [`Bubble`](/blockly/reference/js/blockly.bubbles_namespace.bubble_class) class.\n\nCreate the view\n---------------\n\nA bubble's view is all of the svg elements that live inside the \"head\" of the\nbubble. These elements are created inside of the bubble's constructor, and they\nshould be children of the `this.contentContainer` element so that they get\nautomatically cleaned up when the icon is destroyed.\n\nThe [`Blockly.utils.dom`](/blockly/reference/js/blockly.utils_namespace.dom_namespace) module provides a clean interface\nfor instantiating svgs. \n\n class MyBubble extends Blockly.bubbles.Bubble {\n // See the Blockly.bubbles.Bubble class for information about what these\n // parameters are.\n constructor(workspace, anchor, ownerRect) {\n super(workspace, anchor, ownerRect);\n\n this.text = Blockly.utils.dom.createSvgElement(\n Blockly.utils.Svg.TEXT,\n {'class': 'my-bubble-class'},\n this.contentContainer);\n const node = Blockly.utils.dom.createTextNode('some text');\n this.text.appendChild(node);\n }\n }\n\nSet the size\n------------\n\nThe bubble's size needs to be set using [`setSize`](/blockly/reference/js/blockly.bubbles_namespace.bubble_class.setsize_1_method) so that the outer\nborder can properly surround the contents of the bubble. It should be set\nduring construction, and whenever the size of the UI elements change. \n\n constructor(workspace, anchor, ownerRect) {\n // Create the view elements... (see above)\n\n const bbox = this.text.getBBox();\n this.setSize(\n new Blockly.utils.Size(\n bbox.width + Blockly.bubbles.Bubble.BORDER_WIDTH * 2,\n bbox.height + Blockly.bubbles.Bubble.BORDER_WIDTH * 2,\n ),\n true\n );\n }\n\nDispose of the bubble\n---------------------\n\nBubbles should clean up any dom elements or external references when they are\ndisposed. By default, anything that is appended to `this.contentContainer` gets\ndestroyed, but other references need to be manually cleaned up. This should be\ndone within the [`dispose`](/blockly/reference/js/blockly.bubbles_namespace.bubble_class.dispose_1_method) method. \n\n dispose() {\n super.dispose();\n\n // Dispose of other references.\n this.myArbitraryReference.dispose();\n }"]]