@nicholasniculas/monofy
v0.1.4
Published
Monospace any text while keeping its original font.
Maintainers
Readme
monofy
Monofy any text by giving every character the width of the widest one.
Install
npm install @nicholasniculas/monofyUsage
import Monofy from "@nicholasniculas/monofy";
const html = Monofy("12:59");Plain HTML
<!doctype html>
<html lang="es">
<body>
<h1 id="timer"></h1>
<script type="module">
import Monofy from "./dist/index.js";
const el = document.getElementById("timer");
el.innerHTML = Monofy("12:59", { width: "auto", font: '700 72px Georgia' });
</script>
</body>
</html>React
import { Monofy } from "@nicholasniculas/monofy/react";
export function Clock() {
const timer = "12:59";
return <h1>{Monofy(timer, { width: "auto", font: '900 360px "Versus"' })}</h1>;
}If you use the React subpath, install React in your app:
npm install reactOptions
Monofy("12:59", {
width: "auto",
font: '900 360px "Versus"',
characterType: "numeric",
measurement: "safe",
widthOffset: -2,
ignore: [":"],
className: "monofy-char",
align: "center",
});React (auto font from element)
import { MonofyText } from "@nicholasniculas/monofy/react-text";
export function Clock({ value }: { value: string }) {
return (
<MonofyText
as="h1"
text={value}
characterType="numeric"
ignore={[":"]}
/>
);
}The component reads the computed font from its own element, so it adapts when CSS, useAutoFit, or media queries change the size.
You can also pass an external ref:
const ref = useRef<HTMLHeadingElement>(null);
<MonofyText
as="h1"
text={value}
sourceRef={ref}
characterType="numeric"
ignore={[":"]}
/>Notes
widthdefaults to"auto".characterTypedefines which set of characters is measured."full"(default): uses the current text."numeric": measures0-9."alphabetic": measures letters."alphanumeric": measures letters and digits.
measurementis"safe"(default) or"visual"."safe"uses typographic advance width."visual"uses actual ink bounds.
widthOffsetadds or subtracts pixels from the measured width.ignoreleaves specific characters at their natural width.autoneeds a browser environment whenwidthis not numeric.- For
autoto be accurate,fontmust match the realfont-weight,font-size, andfont-familyused by the text. monofyreturns HTML string.monofy/reactreturns React nodes.- The text inherits styling from its parent element.
