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) {
  // Codex sizes - 1.25x larger
  final (String height, String fontSize, String padding) = switch (props.size) {
    SelectSize.sm => ('36px', '0.8125rem', '0.5rem 0.875rem'),
    SelectSize.md => ('44px', '0.875rem', '0.625rem 1rem'), // vs 40px
    SelectSize.lg => ('52px', '1rem', '0.75rem 1.25rem'), // vs 48px
  };

  // Get display text
  final String displayText = _getDisplayText();

  return dom.div(
    classes: 'codex-select ${props.disabled ? 'disabled' : ''} ${props.error != null ? 'error' : ''}',
    styles: const dom.Styles(raw: {
      'position': 'relative',
      'width': '100%',
    }),
    [
      // Label
      if (props.label != null)
        dom.label(
          classes: 'codex-select-label',
          styles: const dom.Styles(raw: {
            'display': 'block',
            'font-size': 'var(--font-size-sm)',
            'font-weight': 'var(--font-weight-medium)',
            'color': 'var(--foreground)',
            'margin-bottom': '0.5rem',
          }),
          [
            Component.text(props.label!),
            if (props.required)
              const dom.span(
                styles: dom.Styles(raw: {
                  'color': 'var(--destructive)',
                  'margin-left': '0.25rem',
                }),
                [Component.text('*')],
              ),
          ],
        ),

      // Select trigger
      dom.button(
        classes: 'codex-select-trigger ${props.isOpen ? 'open' : ''}',
        attributes: {
          'type': 'button',
          if (props.disabled) 'disabled': 'true',
        },
        styles: dom.Styles(raw: {
          'display': 'flex',
          'align-items': 'center',
          'justify-content': 'space-between',
          'width': '100%',
          'height': height,
          'padding': padding,
          'background-color': 'var(--input)',
          'border': props.error != null
              ? '1px solid var(--destructive)'
              : props.isOpen
                  ? '1px solid var(--primary)'
                  : '1px solid var(--border)',
          'border-radius': 'var(--radius)',
          'font-size': fontSize,
          'color': props.value != null || (props.values != null && props.values!.isNotEmpty)
              ? 'var(--foreground)'
              : 'var(--muted-foreground)',
          'cursor': props.disabled ? 'not-allowed' : 'pointer',
          'outline': 'none',
          'transition': 'all var(--transition)',
          // Codex: glow when open
          if (props.isOpen) 'box-shadow': '0 0 0 2px rgba(var(--primary-rgb), 0.2)',
          if (props.disabled) 'opacity': '0.5',
        }),
        events: props.disabled || props.onToggle == null
            ? null
            : {'click': (_) => props.onToggle!()},
        [
          // Prefix
          if (props.prefix != null)
            dom.span(
              styles: const dom.Styles(raw: {
                'margin-right': '0.5rem',
                'color': 'var(--muted-foreground)',
              }),
              [props.prefix!],
            ),

          // Display text
          dom.span(
            styles: const dom.Styles(raw: {
              'flex': '1',
              'text-align': 'left',
              'overflow': 'hidden',
              'text-overflow': 'ellipsis',
              'white-space': 'nowrap',
            }),
            [Component.text(displayText)],
          ),

          // Clear button (if clearable and has value)
          if (props.clearable &&
              (props.value != null || (props.values != null && props.values!.isNotEmpty)))
            dom.span(
              classes: 'codex-select-clear',
              styles: const dom.Styles(raw: {
                'display': 'flex',
                'align-items': 'center',
                'justify-content': 'center',
                'width': '18px',
                'height': '18px',
                'margin-right': '0.25rem',
                'color': 'var(--muted-foreground)',
                'cursor': 'pointer',
              }),
              events: props.onClear == null
                  ? null
                  : {
                      'click': (e) {
                        e.stopPropagation();
                        props.onClear!();
                      },
                    },
              [const Component.text('\u2715')],
            ),

          // Chevron
          dom.span(
            styles: dom.Styles(raw: {
              'display': 'flex',
              'align-items': 'center',
              'color': 'var(--muted-foreground)',
              'font-size': 'var(--font-size-xs)',
              'transition': 'transform var(--transition)',
              'transform': props.isOpen ? 'rotate(180deg)' : 'rotate(0deg)',
            }),
            [const Component.text('\u25BC')],
          ),
        ],
      ),

      // Dropdown
      if (props.isOpen)
        dom.div(
          classes: 'codex-select-dropdown',
          styles: dom.Styles(raw: {
            'position': 'absolute',
            'z-index': '50',
            'width': '100%',
            'margin-top': '0.5rem',
            if (props.dropdownDirection == SelectDropdownDirection.up) ...{
              'bottom': '100%',
              'margin-bottom': '0.5rem',
              'margin-top': '0',
            },
            'max-height': props.maxDropdownHeight ?? '300px',
            'overflow-y': 'auto',
            // Codex: glass effect
            'background-color': 'rgba(10, 10, 10, 0.95)',
            'backdrop-filter': 'blur(12px)',
            '-webkit-backdrop-filter': 'blur(12px)',
            'border': '1px solid var(--border)',
            'border-radius': 'var(--radius)',
            'box-shadow': '0 10px 40px rgba(0, 0, 0, 0.3)',
          }),
          [
            // Search input
            if (props.searchable)
              dom.div(
                classes: 'codex-select-search',
                styles: const dom.Styles(raw: {
                  'padding': '0.5rem',
                  'border-bottom': '1px solid var(--border)',
                }),
                [
                  dom.input(
                    type: dom.InputType.text,
                    attributes: {
                      'placeholder': props.searchPlaceholder,
                      'value': props.searchQuery,
                    },
                    styles: const dom.Styles(raw: {
                      'width': '100%',
                      'padding': '0.5rem 0.75rem',
                      'background-color': 'var(--secondary)',
                      'border': '1px solid var(--border)',
                      'border-radius': 'var(--radius-md)',
                      'font-size': 'var(--font-size-sm)',
                      'color': 'var(--foreground)',
                      'outline': 'none',
                    }),
                    events: props.onSearchChange == null
                        ? null
                        : {
                            'input': (e) {
                              final dynamic target = e.target;
                              final String? value = target?.value;
                              if (value != null) props.onSearchChange!(value);
                            },
                          },
                  ),
                ],
              ),

            // Loading state
            if (props.loading)
              dom.div(
                classes: 'codex-select-loading',
                styles: const dom.Styles(raw: {
                  'padding': '1rem',
                  'text-align': 'center',
                  'color': 'var(--muted-foreground)',
                }),
                [Component.text(props.loadingText)],
              )
            // Empty state
            else if (props.filteredOptions.isEmpty)
              dom.div(
                classes: 'codex-select-empty',
                styles: const dom.Styles(raw: {
                  'padding': '1rem',
                  'text-align': 'center',
                  'color': 'var(--muted-foreground)',
                }),
                [Component.text(props.emptyMessage)],
              )
            // Options
            else
              dom.div(
                classes: 'codex-select-options',
                styles: const dom.Styles(raw: {
                  'padding': '0.375rem',
                }),
                [
                  for (final option in props.filteredOptions)
                    _buildOption(option),
                ],
              ),
          ],
        ),

      // Helper text or error
      if (props.error != null)
        dom.div(
          classes: 'codex-select-error',
          styles: const dom.Styles(raw: {
            'font-size': 'var(--font-size-xs)',
            'color': 'var(--destructive)',
            'margin-top': '0.5rem',
          }),
          [Component.text(props.error!)],
        )
      else if (props.helperText != null)
        dom.div(
          classes: 'codex-select-helper',
          styles: const dom.Styles(raw: {
            'font-size': 'var(--font-size-xs)',
            'color': 'var(--muted-foreground)',
            'margin-top': '0.5rem',
          }),
          [Component.text(props.helperText!)],
        ),
    ],
  );
}