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) {
final bool isDisabled = props.disabled || props.loading;
// Codex base styles - more breathing room than ShadCN
final Map<String, String> baseStyles = {
'display': 'inline-flex',
'align-items': 'center',
'justify-content': 'center',
'gap': '0.625rem', // 10px (vs ShadCN 8px)
'white-space': 'nowrap',
'border-radius': 'var(--radius)', // 14px
'font-size': 'var(--font-size-sm)',
'font-weight': 'var(--font-weight-medium)',
'line-height': '1.25rem',
'transition': 'all var(--transition)',
'outline': 'none',
'cursor': isDisabled ? 'not-allowed' : 'pointer',
'pointer-events': isDisabled ? 'none' : 'auto',
'opacity': isDisabled ? '0.5' : '1',
'user-select': 'none',
'-webkit-user-select': 'none',
};
// Codex variant styles - accent-driven with subtle glows
final Map<String, String> variantStyles = switch (props.variant) {
ButtonVariant.primary => {
'background-color': 'var(--primary)',
'color': '#ffffff',
'border': 'none',
'box-shadow': '0 0 20px rgba(var(--primary-rgb), 0.3)',
},
ButtonVariant.destructive => {
'background-color': 'var(--destructive)',
'color': 'var(--destructive-foreground)',
'border': 'none',
'box-shadow': '0 0 20px rgba(var(--destructive-rgb), 0.3)',
},
ButtonVariant.outline => {
'background-color': 'transparent',
'color': 'var(--primary)',
'border': '1px solid var(--primary)',
},
ButtonVariant.secondary => {
'background-color': 'var(--secondary)',
'color': 'var(--secondary-foreground)',
'border': '1px solid var(--border)',
},
ButtonVariant.ghost => {
'background-color': 'transparent',
'color': 'var(--foreground)',
'border': 'none',
},
ButtonVariant.link => {
'background-color': 'transparent',
'color': 'var(--primary)',
'border': 'none',
'text-decoration': 'underline',
'text-underline-offset': '4px',
'padding': '0',
'height': 'auto',
},
ButtonVariant.success => {
'background-color': 'var(--success)',
'color': 'var(--success-foreground)',
'border': 'none',
'box-shadow': '0 0 20px rgba(var(--success-rgb), 0.3)',
},
ButtonVariant.warning => {
'background-color': 'var(--warning)',
'color': 'var(--warning-foreground)',
'border': 'none',
'box-shadow': '0 0 20px rgba(var(--warning-rgb), 0.25)',
},
ButtonVariant.info => {
'background-color': 'var(--info)',
'color': 'var(--info-foreground)',
'border': 'none',
'box-shadow': '0 0 20px rgba(var(--info-rgb), 0.3)',
},
ButtonVariant.accent => {
'background': 'linear-gradient(135deg, var(--primary), color-mix(in srgb, var(--primary) 80%, white))',
'color': '#ffffff',
'border': 'none',
'box-shadow': '0 0 20px rgba(var(--primary-rgb), 0.3)',
},
};
// Codex size styles - 1.25x ShadCN for more breathing room
final Map<String, String> sizeStyles = switch (props.size) {
ButtonSize.small => {
'height': '2.5rem', // 40px (vs ShadCN 36px)
'padding': '0 1rem', // 16px (vs ShadCN 12px)
},
ButtonSize.medium => {
'height': '3rem', // 48px (vs ShadCN 40px)
'padding': '0.625rem 1.25rem', // 10px 20px (vs ShadCN 8px 16px)
},
ButtonSize.large => {
'height': '3.5rem', // 56px (vs ShadCN 44px)
'padding': '0 2.5rem', // 40px (vs ShadCN 32px)
},
ButtonSize.icon => {
'height': '3rem', // 48px (vs ShadCN 40px)
'width': '3rem',
'padding': '0',
},
ButtonSize.iconSmall => {
'height': '2.5rem', // 40px (vs ShadCN 36px)
'width': '2.5rem',
'padding': '0',
},
ButtonSize.iconLarge => {
'height': '3.5rem', // 56px (vs ShadCN 44px)
'width': '3.5rem',
'padding': '0',
},
};
// Combine all styles
final Map<String, String> allStyles = {
...baseStyles,
...variantStyles,
...sizeStyles,
if (props.fullWidth) 'width': '100%',
};
// Build button content
final List<Component> children = [];
if (props.loading) {
children.add(_buildSpinner());
} else if (props.icon != null) {
children.add(props.icon!);
}
if (props.label != null) {
children.add(Component.text(props.label!));
}
if (props.child != null) {
children.add(props.child!);
}
if (props.trailing != null && !props.loading) {
children.add(props.trailing!);
}
// Add arrow indicator if showArrow is true
if (props.showArrow && !props.loading) {
children.add(const dom.span(
styles: dom.Styles(raw: {
'margin-left': '0.25rem',
'transition': 'transform var(--arcane-transition)',
}),
[Component.text('\u2192')],
));
}
// Render as anchor if href is provided, otherwise as button
if (props.href != null) {
return dom.a(
id: props.id,
classes: 'codex-button',
href: props.href!,
attributes: {
if (isDisabled) 'aria-disabled': 'true',
...?props.attributes,
},
styles: dom.Styles(raw: {
...allStyles,
'text-decoration': 'none',
}),
events: {
if (props.onPressed != null)
'click': (event) {
if (!isDisabled) {
props.onPressed!();
}
},
},
children,
);
}
return dom.button(
id: props.id,
classes: 'codex-button',
attributes: {
if (isDisabled) 'disabled': 'true',
'type': 'button',
...?props.attributes,
},
styles: dom.Styles(raw: allStyles),
events: {
'click': (event) {
if (!isDisabled && props.onPressed != null) {
props.onPressed!();
}
},
},
children,
);
}