UNPKG

react-square-text

Version:

A JavaScript utility to display text in a square shape using HTML and CSS. Supports monospaced fonts for better alignment.

98 lines (97 loc) 2.68 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // index.tsx import React from "react"; var SquareTextWriter = /* @__PURE__ */ __name(({ text, size, id, options = {} }) => { const monospacedFonts = [ "Menlo", "Courier New", "Consolas", "Lucida Console", "Monaco", "monospace" ]; let fontFamily = "Menlo, monospace"; if (options.fontFamily === "customFont" && options.customFont) { fontFamily = options.customFont; } else if (monospacedFonts.includes(options.fontFamily || "")) { fontFamily = options.fontFamily || "Menlo"; } const fontSize = options.fontSize || 16; const halfFontSize = fontSize / 2; const letters = text.split(""); const fontSizex2 = fontSize * 1; const newsize = size - fontSizex2; const perimeter = newsize * 4; const spacing = perimeter / letters.length; return /* @__PURE__ */ React.createElement( "div", { id, "aria-label": text, style: { height: `${size}px`, width: `${size}px`, position: "relative", display: "flex", justifyContent: "center", alignItems: "center", margin: "40px" } }, letters.map((letter, index) => { const distance = index * spacing; const newDistanceToRest = distance / 100 * 0.25; const newDistance = distance - newDistanceToRest; let x = 0, y = 0, rotation = 0; if (distance < newsize) { x = distance; y = halfFontSize; rotation = 0; } else if (distance < newsize * 2) { x = newsize; y = newDistance - newsize + fontSize; rotation = 90; } else if (distance < newsize * 3) { x = newsize - (newDistance - newsize * 2); y = newsize + halfFontSize; rotation = 180; } else { x = 0; y = newsize - (newDistance - newsize * 3); rotation = 270; } return /* @__PURE__ */ React.createElement( "p", { key: index, style: { position: "absolute", left: `${x}px`, top: `${y}px`, transform: `rotate(${rotation}deg)`, color: options.color || "#000000", fontFamily, fontWeight: options.fontWeight || 400, fontSize, margin: 0, lineHeight: 0, padding: 0, whiteSpace: "nowrap" } }, letter ); }) ); }, "SquareTextWriter"); var index_default = SquareTextWriter; export { index_default as default }; //# sourceMappingURL=index.mjs.map