log-tags
Version:
Color tags for devtools console.log
26 lines (25 loc) • 872 B
JavaScript
import counterColor from "counter-color";
import { combineLogTags, objectToStyleCssString, randomHexColor, strColorHash } from "./utils";
export function logTag(text, styleOrBg) {
let style = typeof styleOrBg === "string"
? { background: styleOrBg }
: styleOrBg || {};
const bg = style?.background === "random"
? randomHexColor()
: style?.background || strColorHash(text);
const fg = counterColor(bg, { threshold: 0.3 });
const css = objectToStyleCssString({
color: fg,
"font-size": "1em",
padding: ".1em .4em",
"margin-left": ".4em",
"border-radius": ".3em",
"text-shadow": `0 0 4px #22222233`,
...style,
background: bg,
});
return [`%c${text}`, css];
}
export function logTags(...tags) {
return combineLogTags(tags.map(tag => logTag(...tag)));
}