build method

  1. @override
Component build(
  1. BuildContext context
)
override

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:

If a component's build method is to depend on anything else, use a StatefulComponent instead.

See also:

Implementation

@override
Component build(BuildContext context) {
  final (primary, bgColor, borderColor, shadow) = _colors;

  // Codex accent Alert styles vary by style variant
  final containerStyles = switch (props.style) {
    AlertStyle.solid => <String, String>{
        'background': 'linear-gradient(180deg, color-mix(in srgb, $primary 82%, #0d1110), $primary)',
        'border': '1px solid transparent',
        'border-radius': 'var(--arcane-radius-md)',
        'color': '#ffffff',
        'box-shadow': shadow,
      },
    AlertStyle.subtle => <String, String>{
        'background-color': bgColor,
        'border': '1px solid $borderColor',
        'border-radius': 'var(--arcane-radius-md)',
        'box-shadow': shadow,
      },
    AlertStyle.outline => <String, String>{
        'background-color': 'var(--card)',
        'border': '1px solid $borderColor',
        'border-radius': 'var(--arcane-radius-md)',
        'box-shadow': shadow,
      },
    AlertStyle.accent => <String, String>{
        'background-color': bgColor,
        'border': '1px solid $borderColor',
        'border-left': '4px solid $primary',
        'border-radius': 'var(--arcane-radius-md)',
        'box-shadow': shadow,
      },
  };

  final isSolid = props.style == AlertStyle.solid;

  return dom.div(
    classes: 'codex-alert ',
    attributes: {
      'role': 'alert',
      'data-variant': props.color.name,
      'data-style': props.style.name,
    },
    styles: dom.Styles(raw: {
      'position': 'relative',
      'width': '100%',
      'display': 'flex',
      'align-items': 'flex-start',
      'gap': '12px',
      'padding': '16px',
      ...containerStyles,
    }),
    [
      // Icon
      if (props.showIcon)
        dom.div(
          classes: 'codex-alert-icon',
          styles: dom.Styles(raw: {
            'flex-shrink': '0',
            'width': '16px',
            'height': '16px',
            'display': 'flex',
            'align-items': 'center',
            'justify-content': 'center',
            'color': isSolid ? '#ffffff' : primary,
            'font-size': 'var(--font-size-base)',
            'margin-top': '1px',
          }),
          [props.icon ?? Component.text(_defaultIcon)],
        ),

      // Content
      dom.div(
        classes: 'codex-alert-content',
        styles: const dom.Styles(raw: {
          'flex': '1',
          'min-width': '0',
        }),
        [
          // Title
          if (props.title != null)
            dom.div(
              classes: 'codex-alert-title',
              styles: dom.Styles(raw: {
                'font-weight': 'var(--font-weight-medium)',
                'line-height': '1',
                'letter-spacing': '-0.025em',
                'color': isSolid ? '#ffffff' : 'var(--foreground)',
                if (props.message != null || props.child != null) 'margin-bottom': '4px',
              }),
              [Component.text(props.title!)],
            ),
          // Description
          if (props.message != null)
            dom.div(
              classes: 'codex-alert-description',
              styles: dom.Styles(raw: {
                'font-size': 'var(--font-size-sm)',
                'line-height': '1.625',
                'color': isSolid ? 'rgba(255, 255, 255, 0.9)' : 'var(--muted-foreground)',
              }),
              [Component.text(props.message!)],
            ),
          if (props.child != null) props.child!,
          if (props.action != null)
            dom.div(
              styles: const dom.Styles(raw: {
                'margin-top': '12px',
              }),
              [props.action!],
            ),
        ],
      ),

      // Dismiss button
      if (props.dismissible)
        dom.button(
          type: dom.ButtonType.button,
          classes: 'codex-alert-dismiss',
          attributes: const {'aria-label': 'Dismiss'},
          styles: dom.Styles(raw: {
            'position': 'absolute',
            'right': '8px',
            'top': '8px',
            'display': 'inline-flex',
            'align-items': 'center',
            'justify-content': 'center',
            'width': '24px',
            'height': '24px',
            'padding': '0',
            'border': 'none',
            'background': 'transparent',
            'color': isSolid ? 'rgba(255, 255, 255, 0.8)' : 'var(--muted-foreground)',
            'cursor': 'pointer',
            'border-radius': 'var(--arcane-radius-xs)',
            'opacity': '0.7',
            'transition': 'opacity var(--arcane-transition)',
            'font-size': 'var(--font-size-base)',
          }),
          events: {'click': (_) => props.onDismiss?.call()},
          [const Component.text('\u00D7')], // x
        ),
    ],
  );
}