Skip to main content

Dynamic categories

Dynamic categories are categories that are dynamically repopulated based on a function each time they are opened.

Blockly supports this by allowing you to associate a category with a function via a registered string key. The function should return a definition of a category's contents (including blocks, buttons, labels, etc). The contents can be specified as JSON or XML, although JSON is recommended.

Also note that the function is provided the target workspace as a parameter, so the blocks in your dynamic category can be based on the state of the workspace.

// Returns an array of objects.
var coloursFlyoutCallback = function(workspace) {
// Returns an array of hex colours, e.g. ['#4286f4', '#ef0447']
var colourList = getPalette();
var blockList = [];
for (var i = 0; i < colourList.length; i++) {
blockList.push({
'kind': 'block',
'type': 'colour_picker',
'fields': {
'COLOUR': colourList[i]
}
});
}
return blockList;
};

// Associates the function with the string 'COLOUR_PALETTE'
myWorkspace.registerToolboxCategoryCallback(
'COLOUR_PALETTE', coloursFlyoutCallback);

After the dynamic category functions are associated with a string key (aka registered) you can assign this string key to the custom property of your category definition to make the category dynamic.

{
"kind": "category",
"name": "Colours",
"custom": "COLOUR_PALETTE"
}

Built-in dynamic categories

Blockly provides three built-in dynamic categories.

  • 'VARIABLE' creates a category for untyped variables.
  • 'VARIABLE_DYNAMIC' creates a category for typed variables. It has buttons to create strings, numbers, and colours.
  • 'PROCEDURE' creates a category for function blocks.
{
"kind": "category",
"name": "Variables",
"custom": "VARIABLE"
},
{
"kind": "category",
"name": "Variables",
"custom": "VARIABLE_DYNAMIC"
},
{
"kind": "category",
"name": "Functions",
"custom": "PROCEDURE"
}

Note: The word 'procedure' is used throughout the Blockly codebase, but the word 'function' has found to be more understandable by students. Sorry for the mismatch.