From e9aa6677c4cbe739f4c2efa7d0dbd44e5ad60f46 Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Mon, 22 Jan 2024 11:51:09 +0100 Subject: [PATCH] feat(text): stylable text wrapper --- src/components/Text.tsx | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/components/Text.tsx diff --git a/src/components/Text.tsx b/src/components/Text.tsx new file mode 100644 index 0000000..b2a9874 --- /dev/null +++ b/src/components/Text.tsx @@ -0,0 +1,55 @@ +import config from "~/../tailwind.config"; + +const THEME = config.theme.extend.colors; +type TerminalColor = keyof { + [K in keyof typeof THEME]: K extends `color${number}` ? K : never; +}; + +export const Text = (props: { + children: string | Array; + bold?: boolean; + fg?: TerminalColor; + bg?: TerminalColor; +}) => ( + + {props.children} + +); + +export const Text2 = (props: { children: string | Array }) => { + const text = Array.isArray(props.children) + ? props.children.join("") + : props.children; + const parts = text.split("$"); + + const nodes = parts.map((x, i) => { + let colorId = 7; + let bold = false; + if (x.startsWith("#") && x[1] && "0123456789abcdef".includes(x[1])) { + colorId = parseInt(x[1], 16); + x = x.substring(2); + + if (x[0] && x.startsWith("*")) { + bold = true; + x = x.substring(1); + } + } + + const colors = config.theme.extend.colors as Record; + const color = + colors[`color${colorId}`] ?? config.theme.extend.colors.color7; + return ( + + {x} + + ); + }); + + return nodes; +};