react-square-text
Version:
A JavaScript utility to display text in a square shape using HTML and CSS. Supports monospaced fonts for better alignment.
127 lines (126 loc) • 4.17 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.tsx
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
var import_react = __toESM(require("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__ */ import_react.default.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__ */ import_react.default.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;
//# sourceMappingURL=index.js.map