Files
vue-tui/packages/runtime/src/components/Text.ts
T
Yunfei He 1503c18a66 feat(runtime): add wrap='hard' mode to Text component
Hard wrap breaks text at exact width boundaries without respecting
word boundaries. Uses wrap-ansi with { hard: true, trim: false }.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 20:45:21 +08:00

43 lines
1.1 KiB
TypeScript

import { defineComponent, getCurrentInstance, h, type PropType } from "vue";
type Color = string | [number, number, number];
type WrapMode =
| "wrap"
| "hard"
| "truncate"
| "truncate-end"
| "truncate-middle"
| "truncate-start";
export const Text = defineComponent({
name: "Text",
props: {
color: [String, Array] as PropType<Color>,
backgroundColor: [String, Array] as PropType<Color>,
dimColor: Boolean,
bold: Boolean,
italic: Boolean,
underline: Boolean,
strikethrough: Boolean,
inverse: Boolean,
wrap: { type: String as PropType<WrapMode>, default: "wrap" },
},
setup(props, { slots }) {
return () => {
const insideText = isInsideText();
const elementType = insideText ? "virtual-text" : "text";
return h(elementType, props as never, slots.default?.());
};
},
});
function isInsideText(): boolean {
let parent = getCurrentInstance()?.parent;
while (parent) {
const name = parent.type && (parent.type as { name?: string }).name;
if (name === "Text") return true;
parent = parent.parent;
}
return false;
}