|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +import React from "react"; |
| 4 | + |
| 5 | +import { SlabProps } from "./Slab.types"; |
| 6 | +import "./Slab.scss"; |
| 7 | +import useFontsizeMapper from "../../hooks/useFontsizeMapper"; |
| 8 | + |
| 9 | +const Slab = (props: SlabProps) => { |
| 10 | + // Styles |
| 11 | + let styles: React.CSSProperties = Object(props?.style); |
| 12 | + |
| 13 | + /* Size SETTINGS */ |
| 14 | + let fontSize: string | number = useFontsizeMapper(props?.size); |
| 15 | + |
| 16 | + // Setting size by specifying font-size in style attr |
| 17 | + // and modifying styles to exclude fontSize |
| 18 | + if (props?.style?.fontSize) { |
| 19 | + const { fontSize: cssFontSize, ...css } = props?.style; |
| 20 | + |
| 21 | + styles = css; |
| 22 | + fontSize = cssFontSize; |
| 23 | + } |
| 24 | + |
| 25 | + /* Color SETTINGS */ |
| 26 | + // If Color property is a string, that is the color of all rings |
| 27 | + // If color property is an array, that is color for each rings |
| 28 | + const slabColor: string | string[] = props?.color ?? ""; |
| 29 | + const slabColorStyles: React.CSSProperties = |
| 30 | + slabColor instanceof Array |
| 31 | + ? { ...genStyleFromColorArr(slabColor) } |
| 32 | + : { ...genStyleFromColorStr(slabColor) }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <span |
| 36 | + className="rli-d-i-b slab-bounding-box" |
| 37 | + style={{ ...(fontSize && { fontSize }), ...slabColorStyles, ...styles }} |
| 38 | + > |
| 39 | + <span className="rli-d-i-b slab-loader slabs"> |
| 40 | + <span className="slab"></span> |
| 41 | + <span className="slab"></span> |
| 42 | + <span className="slab"></span> |
| 43 | + <span className="slab"></span> |
| 44 | + </span> |
| 45 | + <span |
| 46 | + className="rli-d-i-b rli-text-format slab-text" |
| 47 | + style={{ |
| 48 | + ...(props?.textColor && { |
| 49 | + color: props?.textColor, |
| 50 | + mixBlendMode: "unset" |
| 51 | + }) |
| 52 | + }} |
| 53 | + > |
| 54 | + {props?.text |
| 55 | + ? typeof props?.text === "string" && props?.text.length |
| 56 | + ? props?.text |
| 57 | + : "loading" |
| 58 | + : null} |
| 59 | + </span> |
| 60 | + </span> |
| 61 | + ); |
| 62 | +}; |
| 63 | + |
| 64 | +export { Slab }; |
| 65 | + |
| 66 | +function genStyleFromColorStr( |
| 67 | + colorStr: string | undefined |
| 68 | +): React.CSSProperties { |
| 69 | + colorStr = colorStr ?? ""; |
| 70 | + |
| 71 | + const stylesObject: any = {}; |
| 72 | + |
| 73 | + stylesObject["color"] = colorStr; |
| 74 | + |
| 75 | + return stylesObject; |
| 76 | +} |
| 77 | + |
| 78 | +function genStyleFromColorArr(colorArr: string[]): React.CSSProperties { |
| 79 | + const stylesObject: any = {}; |
| 80 | + |
| 81 | + // NOT supporting Individual bubble coloring |
| 82 | + const [color] = colorArr; |
| 83 | + |
| 84 | + stylesObject["color"] = color; |
| 85 | + |
| 86 | + return stylesObject; |
| 87 | +} |
0 commit comments