|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +import React, { useRef, useEffect } from "react"; |
| 4 | + |
| 5 | +import { PulseProps } from "./Pulse.types"; |
| 6 | +import "./Pulse.scss"; |
| 7 | +import useFontsizeMapper from "../../hooks/useFontsizeMapper"; |
| 8 | +import Text from "../../utils/Text"; |
| 9 | + |
| 10 | +const Pulse = (props: PulseProps) => { |
| 11 | + const svgPathRef = useRef<SVGPathElement>(null); |
| 12 | + |
| 13 | + // Styles |
| 14 | + let styles: React.CSSProperties = Object(props?.style); |
| 15 | + |
| 16 | + /* Size SETTINGS */ |
| 17 | + let fontSize: string | number = useFontsizeMapper(props?.size); |
| 18 | + |
| 19 | + // Setting size by specifying font-size in style attr |
| 20 | + // and modifying styles to exclude fontSize |
| 21 | + if (props?.style?.fontSize) { |
| 22 | + const { fontSize: cssFontSize, ...css } = props?.style; |
| 23 | + |
| 24 | + styles = css; |
| 25 | + fontSize = cssFontSize; |
| 26 | + } |
| 27 | + |
| 28 | + /* Color SETTINGS */ |
| 29 | + // If Color property is a string, that is the color of all rings |
| 30 | + // If color property is an array, that is color for each rings |
| 31 | + const pulseColor: string | string[] = props?.color ?? ""; |
| 32 | + const pulseColorStyles: React.CSSProperties = |
| 33 | + pulseColor instanceof Array |
| 34 | + ? { ...genStyleFromColorArr(pulseColor) } |
| 35 | + : { ...genStyleFromColorStr(pulseColor) }; |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + if (svgPathRef.current) { |
| 39 | + const pathEl: SVGPathElement = svgPathRef.current; |
| 40 | + const parentSvgEl: HTMLElement | null = pathEl.parentElement; |
| 41 | + const pathLength: number = pathEl.getTotalLength(); |
| 42 | + console.log("SVG PATH LENGTH: ", pathLength); |
| 43 | + |
| 44 | + const pathLengthRepeats = pathLength / 2; |
| 45 | + const dashArray = pathLengthRepeats * 0.94; |
| 46 | + const dashArrayGap = pathLengthRepeats * 0.06; |
| 47 | + |
| 48 | + pathEl.style.strokeDasharray = `${dashArray} ${dashArrayGap}`; |
| 49 | + pathEl.style.strokeDashoffset = `${pathLength}`; |
| 50 | + parentSvgEl && (parentSvgEl.style.visibility = "visible"); |
| 51 | + |
| 52 | + // Trigger a layout so styles are calculated & the browser |
| 53 | + // picks up the starting position before animating |
| 54 | + pathEl.getBoundingClientRect(); |
| 55 | + |
| 56 | + const pulseKeyframes = new KeyframeEffect( |
| 57 | + pathEl, // element to animate |
| 58 | + [{ strokeDashoffset: `${pathLength}` }, { strokeDashoffset: `0` }], |
| 59 | + { duration: 2000, iterations: Infinity, easing: "linear" } |
| 60 | + ); |
| 61 | + |
| 62 | + const pulseAnimation = new Animation(pulseKeyframes, document.timeline); |
| 63 | + |
| 64 | + // Play pulse animation |
| 65 | + pulseAnimation.play(); |
| 66 | + } |
| 67 | + }, []); |
| 68 | + |
| 69 | + return ( |
| 70 | + <span |
| 71 | + className="rli-d-i-b pulse-bounding-box" |
| 72 | + style={{ ...(fontSize && { fontSize }) }} |
| 73 | + > |
| 74 | + <span |
| 75 | + className="rli-d-i-b pulse-loader" |
| 76 | + style={{ ...pulseColorStyles, ...styles }} |
| 77 | + > |
| 78 | + {/* Original size SVG */} |
| 79 | + {/* <svg |
| 80 | + width="16em" // ratio -> 2.947368421 |
| 81 | + height="5.428571429em" |
| 82 | + xmlns="http://www.w3.org/2000/svg" |
| 83 | + version="1.2" |
| 84 | + viewBox="0 0 550 190" |
| 85 | + preserveAspectRatio="xMinYMin meet" |
| 86 | + > |
| 87 | + <path |
| 88 | + ref={svgPathRef} |
| 89 | + stroke="currentColor" |
| 90 | + fill="none" |
| 91 | + strokeWidth="2" |
| 92 | + strokeLinejoin="round" |
| 93 | + d="M0 70h250q7-30 12-3t5 8 3-7 3 4 6 35 7-60 4 60 7-20s2-11 10-10 1 1 8-10l4 8c6 4 8-6 10-17s2 10 9 11h210" |
| 94 | + /> |
| 95 | + </svg> */} |
| 96 | + |
| 97 | + {/* Zoomed in SVG */} |
| 98 | + <svg |
| 99 | + width="14em" // ratio -> 2.051282051 |
| 100 | + height="6.825000001em" |
| 101 | + xmlns="http://www.w3.org/2000/svg" |
| 102 | + version="1.2" |
| 103 | + viewBox="0 0 400 50" |
| 104 | + preserveAspectRatio="xMinYMin meet" |
| 105 | + className="rli-pulse-svg" |
| 106 | + > |
| 107 | + <path |
| 108 | + ref={svgPathRef} |
| 109 | + stroke="currentColor" |
| 110 | + fill="none" |
| 111 | + strokeWidth="2" |
| 112 | + strokeLinejoin="round" |
| 113 | + d="M-80 70h250q7-30 12-3t5 8 3-7 3 4 6 35 7-60 4 60 7-20s2-11 10-10 1 1 8-10l4 8c6 4 8-6 10-17s2 10 9 11h210" |
| 114 | + ></path> |
| 115 | + </svg> |
| 116 | + |
| 117 | + <Text |
| 118 | + className="pulse-text" |
| 119 | + text={props?.text} |
| 120 | + textColor={props?.textColor} |
| 121 | + /> |
| 122 | + </span> |
| 123 | + </span> |
| 124 | + ); |
| 125 | +}; |
| 126 | + |
| 127 | +export { Pulse }; |
| 128 | + |
| 129 | +function genStyleFromColorStr( |
| 130 | + colorStr: string | undefined |
| 131 | +): React.CSSProperties { |
| 132 | + colorStr = colorStr ?? ""; |
| 133 | + |
| 134 | + const stylesObject: any = {}; |
| 135 | + |
| 136 | + stylesObject["color"] = colorStr; |
| 137 | + |
| 138 | + return stylesObject; |
| 139 | +} |
| 140 | + |
| 141 | +function genStyleFromColorArr(colorArr: string[]): React.CSSProperties { |
| 142 | + const stylesObject: any = {}; |
| 143 | + |
| 144 | + // NOT supporting Individual coloring |
| 145 | + const [color] = colorArr; |
| 146 | + |
| 147 | + stylesObject["color"] = color; |
| 148 | + |
| 149 | + return stylesObject; |
| 150 | +} |
0 commit comments