HtmlTreeMenu
A TreeMenu control that builds its entries from nested HTML lists.
import { HtmlTreeMenu } from "@schukai/monster/source/components/tree-menu/html-tree-menu.mjs";- Root 1
- Root 1 - A
- Root 1 - B
- Root 2
Introduction
The monster-html-tree-menu renders a tree menu from nested HTML lists and matches the look and behavior of monster-tree-menu. It accepts <ul> / <li> structures and converts them into interactive entries.
Basic Usage
<monster-html-tree-menu>
<ul>
<li data-monster-value="root-1">Root 1
<ul class="hidden">
<li data-monster-value="root-1-a">Root 1 - A</li>
<li data-monster-value="root-1-b">Root 1 - B</li>
</ul>
</li>
<li data-monster-value="root-2">Root 2</li>
</ul>
</monster-html-tree-menu>
- Use
data-monster-valuefor stable IDs; otherwise a random ID is generated. - Initial collapse is controlled via
hiddenorhiddenclass on nested lists or items. - The original list is hidden after import.
Navigation vs. Selection
If a list item contains an <a href="...">, the browser navigates. Without a link, the component treats the item as a selectable entry and fires selection events.
<li data-monster-value="docs"><a href="/docs">Docs</a></li>
<li data-monster-value="local">Local entry without page</li>
Lazy Loading
To load children on first expand, set a data-monster-endpoint on the list item. The endpoint must return HTML containing <ul>/<li> or plain <li> items.
<li data-monster-value="root-2" data-monster-endpoint="/issue-350/nodes">Root 2 (lazy)</li>
Basic HTML Tree Menu
A basic HTML tree menu rendered from nested lists. The first subtree is collapsed by marking the nested list as hidden.
- Root 1
- Root 1 - A
- Root 1 - B
- Root 2
Javascript
import '@schukai/monster/source/components/tree-menu/html-tree-menu.mjs';HTML
<monster-html-tree-menu id="html-tree-menu-simple">
<ul>
<li data-monster-value="root-1">Root 1
<ul class="hidden">
<li data-monster-value="root-1-a">Root 1 - A</li>
<li data-monster-value="root-1-b">Root 1 - B</li>
</ul>
</li>
<li data-monster-value="root-2">Root 2</li>
</ul>
</monster-html-tree-menu>Stylesheet
/** no additional stylesheet is defined **/Lazy Loading
This example loads the children of Root 2 on first expand using a lazy endpoint.
- Root 1
- Root 2 (lazy)
Javascript
import '@schukai/monster/source/components/tree-menu/html-tree-menu.mjs';
const menu = document.querySelector('#html-tree-menu-lazy');
menu.setOption('lazy.enabled', true);
menu.setOption('lazy.attribute', 'data-monster-endpoint');
menu.setOption('lazy.fetchOptions', { method: 'GET' });HTML
<monster-html-tree-menu id="html-tree-menu-lazy">
<ul>
<li data-monster-value="root-1">Root 1</li>
<li data-monster-value="root-2" data-monster-endpoint="/issue-350/nodes">Root 2 (lazy)</li>
</ul>
</monster-html-tree-menu>Stylesheet
/** no additional stylesheet is defined **/Component Design
The HTML tree menu shares the same stylesheet as monster-tree-menu. Selected entries use the same rule:
[data-monster-role=entry].selected:not([data-monster-state=open])
Exported Parts
control: Wrapper around the rendered entries.entries: Container for the generated entry list.entry-label: Label part inside each entry.
monster-html-tree-menu::part(entry-label) {
font-weight: 600;
}
HTML Structure
<monster-html-tree-menu></monster-html-tree-menu>JavaScript Initialization
const element = document.createElement('monster-html-tree-menu');
document.body.appendChild(element);Exported
HtmlTreeMenuDerived from
CustomElementOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the CustomElement.
- since
- deprecated
Properties and Attributes
The Properties and Attributes listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits Properties and Attributes from these parent classes. If you cannot find a specific Properties and Attributes in this list, we recommend consulting the documentation of the CustomElement.
data-monster-options: Sets the configuration options for the collapse component when used as an HTML attribute.data-monster-option-[name]: Sets the value of the configuration option[name]for the collapse component when used as an HTML attribute.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the CustomElement.
Behavioral methods
closeEntry(value)value{string}: value
- {void}
hideEntry(value)value{string}: value
- {void}
openEntry(value)value{string}: value
- {void}
removeEntry(value)value{string}: value
- {void}
showEntry(value)value{string}: value
- {void}
State query methods
findEntry(value)value{string}: value
- {Object|null}
Static methods
[instanceSymbol]()- {symbol}
instanceof operator.getCSSStyleSheet()- {CSSStyleSheet[]}
getTag()- {string}
Lifecycle methods
Lifecycle methods are called by the environment and are usually not intended to be called directly.
[assembleMethodSymbol]()- {void}
Other methods
collapseEntry(value)value{string}: value
- {void}
expandEntry(value)value{string}: value
- {void}
insertEntry(entry,)entry{object}: entryundefined{string|null}: parentValue
- {void}
insertEntryAfter(entry,referenceValue)entry{object}: entryreferenceValue{string}: referenceValue
- {void}
insertEntryBefore(entry,referenceValue)entry{object}: entryreferenceValue{string}: referenceValue
- {void}
selectEntry(value)value{string}: value
- {void}
Events
The component emits the following events:
entries-imported
For more information on how to handle events, see the mdn documentation.