All files index.tsx

100% Statements 15/15
100% Branches 6/6
100% Functions 3/3
100% Lines 15/15

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 2011x 1x                                                                                                                                                                                                                                                                                                                                           1x   5x   5x 5x 5x 3x   5x 1x   5x 5x       5x             5x               1x  
import React, { memo } from "react";
import Typed from "typed.js";
export interface ReactTypedProps {
  /**
   * strings to be typed
   * @default [
    'These are the default values...',
    'You know what you should do?',
    'Use your own!',
    'Have a great day!',
  ]
   * */
  strings?: string[];
  /**
   * ID or instance of HTML element of element containing string children
   * @default null
   */
  stringsElement?: string | Element;
  /**
   * type speed in milliseconds
   * @default 0
   */
  typeSpeed?: number;
  /**
   * time before typing starts in milliseconds
   * @default 0
   */
  startDelay?: number;
  /**
   * backspacing speed in milliseconds
   * @default 0
   */
  backSpeed?: number;
  /**
   * only backspace what doesn't match the previous string
   * @default true
   */
  smartBackspace?: boolean;
  /**
   * shuffle the strings
   * @default true
   */
  shuffle?: boolean;
  /**
   * time before backspacing in milliseconds
   * @default 700
   */
  backDelay?: number;
  /**
   * Fade out instead of backspace
   * @default false
   */
  fadeOut?: boolean;
  /**
   * css class for fade animation
   * @default typed-fade-out
   */
  fadeOutClass?: string;
  /**
   * Fade out delay in milliseconds
   * @default 500
   */
  fadeOutDelay?: number;
  /**
   * loop strings
   * @default false
   */
  loop?: boolean;
  /**
   * amount of loops
   * @default Infinity
   */
  loopCount?: number;
  /**
   * show cursor
   * @default true
   */
  showCursor?: boolean;
  /**
   * character for cursor
   * @default |
   */
  cursorChar?: string;
  /**
   * insert CSS for cursor and fadeOut into HTML
   * @default true
   */
  autoInsertCss?: boolean;
  /**
   * attribute for typing Ex: input placeholder, value, or just HTML text
   * @default null
   */
  attr?: string;
  /**
   * bind to focus and blur if el is text input
   * @default false
   */
  bindInputFocusEvents?: boolean;
  /**
   * 'html' or 'null' for plaintext
   * @default html
   */
  contentType?: string;
  /**
   * Before it begins typing the first string
   */
  onBegin?(self: Typed): void;
  /**
   * All typing is complete
   */
  onComplete?(self: Typed): void;
  /**
   * Before each string is typed
   */
  preStringTyped?(arrayPos: number, self: Typed): void;
  /**
   * After each string is typed
   */
  onStringTyped?(arrayPos: number, self: Typed): void;
  /**
   * During looping, after last string is typed
   */
  onLastStringBackspaced?(self: Typed): void;
  /**
   * Typing has been stopped
   */
  onTypingPaused?(arrayPos: number, self: Typed): void;
  /**
   * Typing has been started after being stopped
   */
  onTypingResumed?(arrayPos: number, self: Typed): void;
  /**
   * After reset
   */
  onReset?(self: Typed): void;
  /**
   * After stop
   */
  onStop?(arrayPos: number, self: Typed): void;
  /**
   * After start
   */
  onStart?(arrayPos: number, self: Typed): void;
  /**
   * After destroy
   */
  onDestroy?(self: Typed): void;
  /**
   * Styles for the outer element
   * */
  style?: React.CSSProperties;
  /**
   * class name for the outer element
   * */
  className?: string;
  /**
   * callback that returns the typed instance
   * */
  typedRef?: (typed: Typed) => void;
  /**
   * if true will be initialized in stopped state
   * @default false
   * */
  stopped?: boolean;
  children?: React.ReactElement;
}
 
export const ReactTyped: React.FC<ReactTypedProps> = memo(
  ({ style, className, typedRef, stopped, children, ...typedOptions }) => {
    const rootElement = React.useRef(null);
 
    React.useEffect(() => {
      const typed = new Typed(rootElement.current, { ...typedOptions });
      if (typedRef && typed) {
        typedRef(typed);
      }
      if (stopped) {
        typed?.stop();
      }
      return () => {
        typed.destroy();
      };
    }, [typedOptions]);
 
    const child = !children ? (
      <span style={style} ref={rootElement} />
    ) : (
      React.cloneElement(children, {
        ref: rootElement,
      })
    );
    return (
      <span style={style} className={className} data-testid="react-typed">
        {child}
      </span>
    );
  }
);
 
export { Typed };