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 double percentage =
      ((props.value - props.min) / (props.max - props.min) * 100)
          .clamp(0.0, 100.0);

  // Codex Slider sizes - 1.25x larger than ShadCN
  final (String trackHeight, String thumbSize, String hitAreaHeight) =
      switch (props.size) {
    SliderSize.sm => ('8px', '20px', '32px'), // vs ShadCN 6px/16px
    SliderSize.md => ('10px', '24px', '40px'), // vs ShadCN 8px/20px
    SliderSize.lg => ('12px', '28px', '48px'), // vs ShadCN 10px/24px
  };

  final int thumbSizeNum = int.parse(thumbSize.replaceAll('px', ''));

  // Codex variant colors with accent as primary
  final (String fillColor, String glowColor) = switch (props.variant) {
    SliderVariant.primary => (
        'var(--primary)',
        '0 0 15px rgba(var(--primary-rgb), 0.2)',
      ),
    SliderVariant.success => (
        'var(--success)',
        '0 0 10px rgba(var(--success-rgb), 0.25)',
      ),
    SliderVariant.warning => (
        'var(--warning)',
        '0 0 10px rgba(var(--warning-rgb), 0.25)',
      ),
    SliderVariant.error => (
        'var(--destructive)',
        '0 0 10px rgba(var(--destructive-rgb), 0.25)',
      ),
  };

  return dom.div(
    classes: 'codex-slider ${props.disabled ? 'disabled' : ''}',
    styles: dom.Styles(raw: {
      'width': '100%',
      'opacity': props.disabled ? '0.5' : '1',
      'pointer-events': props.disabled ? 'none' : 'auto',
    }),
    [
      // Label row
      if (props.label != null || props.showValue)
        dom.div(
          styles: const dom.Styles(raw: {
            'display': 'flex',
            'justify-content': 'space-between',
            'align-items': 'center',
            'margin-bottom': '0.75rem', // Codex: more spacing
          }),
          [
            if (props.label != null)
              dom.span(
                styles: const dom.Styles(raw: {
                  'font-size': 'var(--font-size-sm)',
                  'font-weight': 'var(--font-weight-medium)',
                  'color': 'var(--foreground)',
                }),
                [Component.text(props.label!)],
              ),
            if (props.showValue)
              dom.span(
                classes: 'codex-slider-value',
                styles: const dom.Styles(raw: {
                  'font-size': 'var(--font-size-sm)',
                  'font-weight': 'var(--font-weight-medium)',
                  'font-variant-numeric': 'tabular-nums',
                  'color': 'var(--muted-foreground)',
                  'min-width': '40px',
                  'text-align': 'right',
                }),
                [
                  Component.text(
                    '${props.valuePrefix ?? ''}${props.value.toStringAsFixed(props.valueDecimals)}${props.valueSuffix ?? ''}',
                  ),
                ],
              ),
          ],
        ),

      // Track container with hit area
      dom.div(
        classes: 'codex-slider-track-container',
        styles: dom.Styles(raw: {
          'position': 'relative',
          'width': '100%',
          'height': hitAreaHeight,
          'display': 'flex',
          'align-items': 'center',
          'cursor': 'pointer',
          'touch-action': 'none',
          'user-select': 'none',
        }),
        [
          // Track background - Codex uses muted color
          dom.div(
            classes: 'codex-slider-track',
            styles: dom.Styles(raw: {
              'position': 'absolute',
              'left': '0',
              'right': '0',
              'height': trackHeight,
              'background-color': 'var(--muted)',
              'border-radius': 'var(--arcane-radius-full)',
              'overflow': 'hidden',
            }),
            [
              // Range/filled portion with accent color
              dom.div(
                classes: 'codex-slider-track-fill',
                styles: dom.Styles(raw: {
                  'position': 'absolute',
                  'left': '0',
                  'top': '0',
                  'width': '$percentage%',
                  'height': '100%',
                  'background-color': fillColor,
                  'transition': 'width 0.1s ease-out',
                }),
                [],
              ),
            ],
          ),

          // Step markers (if enabled)
          if (props.showSteps && props.step != null)
            _buildStepMarkers(trackHeight),

          // Thumb - Codex styling with glow
          dom.div(
            classes: 'codex-slider-thumb',
            styles: dom.Styles(raw: {
              'position': 'absolute',
              'left': 'calc($percentage% - ${thumbSizeNum / 2}px)',
              'top': '50%',
              'transform': 'translateY(-50%)',
              'width': thumbSize,
              'height': thumbSize,
              'background-color': 'var(--foreground)',
              'border': '2px solid $fillColor',
              'border-radius': 'var(--arcane-radius-full)',
              // Codex: subtle glow on thumb
              'box-shadow': glowColor,
              'transition': 'all var(--transition)',
              'pointer-events': 'none',
              'z-index': '2',
            }),
            [],
          ),

          // Hidden native range input for interaction
          dom.input(
            type: dom.InputType.range,
            classes: 'codex-slider-input',
            attributes: {
              'min': props.min.toString(),
              'max': props.max.toString(),
              'value': props.value.toString(),
              'step': props.step?.toString() ?? 'any',
              if (props.disabled) 'disabled': 'true',
            },
            styles: const dom.Styles(raw: {
              'position': 'absolute',
              'width': '100%',
              'height': '100%',
              'opacity': '0',
              'cursor': 'pointer',
              'margin': '0',
              'z-index': '3',
            }),
            events: props.onChanged == null
                ? null
                : {
                    'input': (e) {
                      final dynamic target = e.target;
                      final String? valueStr = target?.value;
                      final double? newValue = double.tryParse(valueStr ?? '');
                      if (newValue != null) {
                        props.onChanged!(newValue);
                      }
                    },
                  },
          ),
        ],
      ),

      // Min/Max labels (optional, shown when no label is provided)
      if (props.label == null && !props.showValue)
        dom.div(
          styles: const dom.Styles(raw: {
            'display': 'flex',
            'justify-content': 'space-between',
            'margin-top': '0.375rem', // Codex: slightly more
          }),
          [
            dom.span(
              styles: const dom.Styles(raw: {
                'font-size': 'var(--font-size-xs)',
                'color': 'var(--muted-foreground)',
              }),
              [Component.text(props.min.toStringAsFixed(0))],
            ),
            dom.span(
              styles: const dom.Styles(raw: {
                'font-size': 'var(--font-size-xs)',
                'color': 'var(--muted-foreground)',
              }),
              [Component.text(props.max.toStringAsFixed(0))],
            ),
          ],
        ),
    ],
  );
}