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: 'arcane-command-overlay',
attributes: {
'data-command': 'true',
'data-command-closable': 'true', // JavaScript handles close
'data-state': 'open',
},
styles: const dom.Styles(
raw: {
'position': 'fixed',
'inset': '0',
// ShadCN: z-50
'z-index': '50',
'display': 'flex',
'align-items': 'flex-start',
'justify-content': 'center',
'padding-top': '20vh',
// ShadCN: bg-black/80
'background-color': 'rgba(0, 0, 0, 0.5)',
'animation': 'arcane-fade-in var(--transition-slow)',
},
),
// Note: No Dart event handlers - JavaScript handles all interactions
[
dom.div(
classes: 'arcane-command-dialog',
attributes: {
'role': 'dialog',
'aria-modal': 'true',
'aria-label': 'Command palette',
},
styles: const dom.Styles(
raw: {
'width': '100%',
// ShadCN: max-w-lg
'max-width': '640px',
// ShadCN: bg-popover
'background-color': 'var(--popover)',
'color': 'var(--popover-foreground)',
// ShadCN: border
'border': '1px solid var(--border)',
// ShadCN: rounded-lg
'border-radius': 'var(--radius-md)',
// ShadCN: shadow-lg
'box-shadow':
'0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1)',
'overflow': 'hidden',
'animation': 'arcane-scale-in var(--transition-slow)',
},
),
[
// Search input - ShadCN: flex items-center border-b px-3
dom.div(
styles: const dom.Styles(
raw: {
'display': 'flex',
'align-items': 'center',
'gap': 'var(--space-2)',
'padding': '12px 16px',
'border-bottom': '1px solid var(--border)',
},
),
[
// ShadCN: Search icon
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="arcane-command-input" type="text" '
'placeholder="${props.placeholder}" '
'autofocus autocomplete="off" spellcheck="false" '
'style="flex:1;background:transparent;border:none;'
'font-size:var(--font-size-sm);color:var(--foreground);outline:none;">',
),
],
),
// Results - ShadCN: max-h-[300px] overflow-y-auto overflow-x-hidden
dom.div(
classes: 'arcane-command-list',
attributes: {'role': 'listbox'},
styles: const dom.Styles(
raw: {
'max-height': '400px',
'overflow-y': 'auto',
'padding': '8px',
},
),
[
if (props.filteredItems.isEmpty)
// ShadCN: py-6 text-center text-sm
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)
// Show groups when not searching
for (final group in props.groups) ...[
if (group.heading != null)
// ShadCN: px-2 py-1.5 text-xs font-medium text-muted-foreground
dom.div(
classes: 'arcane-command-group-heading',
styles: const dom.Styles(
raw: {
'padding': '8px 12px',
'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
// Show flat list when searching
for (final item in props.filteredItems)
_buildItem(item, null),
],
),
// Footer with keyboard hints - ShadCN: border-t
dom.div(
styles: const dom.Styles(
raw: {
'display': 'flex',
'align-items': 'center',
'gap': '16px',
'padding': '8px 12px',
'border-top': '1px solid var(--border)',
'font-size': 'var(--font-size-xs)',
'color': 'var(--muted-foreground)',
},
),
[
_buildKeyHint('Enter', 'Select'),
_buildKeyHint('Up/Down', 'Navigate'),
_buildKeyHint('esc', 'Close'),
],
),
],
),
],
);
}