All files flex.ts

74.47% Statements 35/47
55.56% Branches 15/27
28.57% Functions 2/7
73.33% Lines 33/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1011x                               1x                                                         1x                 14x 14x 14x 14x 14x   14x 13x 13x 8x 8x 1x 1x 7x 1x 6x 1x 1x 1x 1x 5x 2x 2x 2x 3x 3x 3x 3x           5x 5x       14x                
import {
  MediaQueryAttribute,
  MediaQueryEvent,
  Modifier,
} from '@rhtml/custom-attributes';
 
interface Styles {
  flex: string;
  boxSizing: string;
  minWidth: string;
  maxWidth?: string;
}
 
@Modifier({
  selector: 'fxFlex',
})
export class Flex extends MediaQueryAttribute<Styles> {
  private prevValue: string;
 
  OnInit() {
    this.modify();
    super.OnInit();
  }
 
  OnDestroy() {
    this.clean();
    super.OnDestroy();
  }
 
  OnUpdate() {
    this.modify();
  }
 
  OnEnterMediaQuery([, attribute]: MediaQueryEvent) {
    this.prevValue = this.value;
    this.value = attribute.value ?? this.value;
    this.modify();
  }
 
  OnExitMediaQuery() {
    this.value = this.prevValue ?? this.value;
    this.modify();
  }
 
  clean() {
    this.setStyles({
      boxSizing: null,
      flex: null,
      minWidth: null,
      maxWidth: null,
    })(this.element);
  }
 
  modify() {
    let grow = '1';
    let shrink = '1';
    let basis = '0.000000001px';
    let flex = `${grow} ${shrink} ${basis}`;
    let maxWidth: string = null;
 
    if (this.value) {
      const parts = this.value.split(' ');
      if (parts.length === 1) {
        const val = parts[0];
        if (val === 'none') {
          flex = 'none';
          maxWidth = 'none';
        } else if (val === 'initial') {
          flex = 'initial';
        } else if (val === 'auto') {
          grow = '1';
          shrink = '1';
          basis = 'auto';
          flex = `${grow} ${shrink} ${basis}`;
        } else if (val.endsWith('%') || val.endsWith('px')) {
          basis = val;
          flex = `${grow} ${shrink} ${basis}`;
          maxWidth = '100%';
        } else Eif (!isNaN(Number(val))) {
          basis = `${val}%`;
          flex = `${grow} ${shrink} ${basis}`;
          maxWidth = '100%';
        } else {
          basis = val;
          flex = `${grow} ${shrink} ${basis}`;
        }
      } else {
        [grow = '1', shrink = '1', basis = '0.000000001px'] = parts;
        flex = `${grow} ${shrink} ${basis}`;
      }
    }
 
    this.setStyles({
      boxSizing: 'border-box',
      flex,
      minWidth: '0',
      maxWidth,
    })(this.element);
  }
}