build method
Describes the part of the user interface represented by this component.
The framework calls this method when this component is inserted into the tree in a given BuildContext and when the dependencies of this component change (e.g., an InheritedComponent referenced by this component changes). This method can potentially be called in every frame and should not have any side effects beyond building a component.
The framework replaces the subtree below this component with the component returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the component returned by this method can update the root of the existing subtree, as determined by calling Component.canUpdate.
Typically implementations return a newly created constellation of components that are configured with information from this component's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this component is being built. For example, the context provides the set of inherited components for this location in the tree. A given component might be built with multiple different BuildContext arguments over time if the component is moved around the tree or if the component is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the component, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedComponentOfExactType.
If a component's build method is to depend on anything else, use a StatefulComponent instead.
See also:
- StatelessComponent, which contains the discussion on performance considerations.
Implementation
@override
Component build(BuildContext context) {
if (!props.isOpen) {
return const dom.div(
styles: dom.Styles(raw: {'display': 'none'}),
[],
);
}
return dom.div(
classes: 'codex-command-overlay',
attributes: {
'data-command': 'true',
'data-command-closable': 'true', // JavaScript handles close
},
styles: const dom.Styles(raw: {
'position': 'fixed',
'inset': '0',
'z-index': '50',
'display': 'flex',
'align-items': 'flex-start',
'justify-content': 'center',
'padding-top': '20vh',
'background-color': 'rgba(0, 0, 0, 0.8)',
'animation': 'arcane-fade-in var(--arcane-transition-slow)',
}),
// Note: No Dart event handlers - JavaScript handles all interactions
[
dom.div(
classes: 'codex-command-dialog',
attributes: {
'role': 'dialog',
'aria-modal': 'true',
'aria-label': 'Command palette',
},
styles: const dom.Styles(raw: {
'width': '100%',
'max-width': '640px',
'background-color': 'var(--card)',
'color': 'var(--foreground)',
'border': '1px solid var(--border)',
'border-radius': 'var(--radius)',
'box-shadow': '0 14px 40px rgba(var(--primary-rgb), 0.15)',
'overflow': 'hidden',
'animation': 'arcane-scale-in var(--arcane-transition-slow)',
}),
[
// Search input
dom.div(
styles: const dom.Styles(raw: {
'display': 'flex',
'align-items': 'center',
'gap': '10px',
'padding': '16px 20px',
'border-bottom': '1px solid var(--border)',
}),
[
dom.span(
styles: const dom.Styles(raw: {
'color': 'var(--muted-foreground)',
'display': 'flex',
'align-items': 'center',
}),
[ArcaneIcon.search(size: IconSize.md)],
),
// Raw HTML input to prevent Jaspr client hydration crashes
dom.RawText(
'<input class="codex-command-input" type="text" '
'placeholder="${props.placeholder}" '
'autofocus autocomplete="off" spellcheck="false" '
'style="flex:1;background:transparent;border:none;'
'font-size:0.9375rem;color:var(--foreground);outline:none;">',
),
],
),
// Results
dom.div(
classes: 'codex-command-list',
attributes: {
'role': 'listbox',
},
styles: const dom.Styles(raw: {
'max-height': '400px',
'overflow-y': 'auto',
'padding': '10px',
}),
[
if (props.filteredItems.isEmpty)
dom.div(
styles: const dom.Styles(raw: {
'padding': '24px',
'text-align': 'center',
'color': 'var(--muted-foreground)',
'font-size': 'var(--font-size-sm)',
}),
[Component.text(props.emptyMessage)],
)
else if (props.searchQuery.isEmpty)
for (final group in props.groups) ...[
if (group.heading != null)
dom.div(
classes: 'codex-command-group-heading',
styles: const dom.Styles(raw: {
'padding': '10px 14px',
'font-size': 'var(--font-size-xs)',
'font-weight': 'var(--font-weight-semibold)',
'color': 'var(--muted-foreground)',
'text-transform': 'uppercase',
'letter-spacing': '0.05em',
}),
[Component.text(group.heading!)],
),
for (final item in group.items)
_buildItem(item, group.heading),
]
else
for (final item in props.filteredItems) _buildItem(item, null),
],
),
// Footer with keyboard hints
dom.div(
styles: const dom.Styles(raw: {
'display': 'flex',
'align-items': 'center',
'gap': '20px',
'padding': '10px 16px',
'border-top': '1px solid var(--border)',
'font-size': 'var(--font-size-xs)',
'color': 'var(--muted-foreground)',
}),
[
_buildKeyHint('\u{21B5}', 'Select'),
_buildKeyHint('\u{2191}\u{2193}', 'Navigate'),
_buildKeyHint('esc', 'Close'),
],
),
],
),
],
);
}