Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add <Line> component to render horizontal and vertical lines #556

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/components/Line.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
/* eslint-disable react/no-unused-prop-types */
import React, {forwardRef} from 'react';
import {type Styles} from '../styles.js';
import {type DOMElement} from '../dom.js';

export type Props = Pick<
Styles,
| 'position'
| 'marginTop'
| 'marginBottom'
| 'marginLeft'
| 'marginRight'
| 'borderStyle'
| 'borderColor'
| 'height'
| 'width'
> & {
orientation?: 'horizontal' | 'vertical';

/**
* Margin on all sides. Equivalent to setting `marginTop`, `marginBottom`, `marginLeft` and `marginRight`.
*
* @default 0
*/
readonly margin?: number;

/**
* Horizontal margin. Equivalent to setting `marginLeft` and `marginRight`.
*
* @default 0
*/
readonly marginX?: number;

/**
* Vertical margin. Equivalent to setting `marginTop` and `marginBottom`.
*
* @default 0
*/
readonly marginY?: number;
};

/**
* Line renders a horizontal or vertical line with the given border style and color.
*/
const Line = forwardRef<DOMElement, Props>(({orientation, ...style}, ref) => {
const transformedStyle = {
...style,
marginLeft: style.marginLeft || style.marginX || style.margin || 0,
marginRight: style.marginRight || style.marginX || style.margin || 0,
marginTop: style.marginTop || style.marginY || style.margin || 0,
marginBottom: style.marginBottom || style.marginY || style.margin || 0,
width: orientation === 'horizontal' ? style.width : 1,
height: orientation === 'vertical' ? style.height : 1
};

return (
<ink-line ref={ref} orientation={orientation} style={transformedStyle} />
);
});

Line.displayName = 'Line';

Line.defaultProps = {
orientation: 'horizontal'
};

export default Line;
4 changes: 3 additions & 1 deletion src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type TextName = '#text';
export type ElementNames =
| 'ink-root'
| 'ink-box'
| 'ink-line'
| 'ink-text'
| 'ink-virtual-text';

Expand Down Expand Up @@ -159,7 +160,8 @@ export const setStyle = (node: DOMNode, style: Styles): void => {
node.style = style;

if (node.yogaNode) {
applyStyles(node.yogaNode, style);
// @ts-expect-error we did check that node.yogaNode exists
applyStyles(node, style);
}
};

Expand Down
19 changes: 19 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare global {
interface IntrinsicElements {
'ink-box': Ink.Box;
'ink-text': Ink.Text;
'ink-line': Ink.Line;
}
}
}
Expand All @@ -22,6 +23,24 @@ declare namespace Ink {
style?: Except<Styles, 'textWrap'>;
};

type Line = {
key?: Key;
ref?: LegacyRef<DOMElement>;
orientation?: 'horizontal' | 'vertical';
style?: Pick<
Styles,
| 'position'
| 'marginTop'
| 'marginBottom'
| 'marginLeft'
| 'marginRight'
| 'borderStyle'
| 'borderColor'
| 'width'
| 'height'
>;
};

type Text = {
children?: ReactNode;
key?: Key;
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type {RenderOptions, Instance} from './render.js';
export {default as render} from './render.js';
export type {Props as BoxProps} from './components/Box.js';
export {default as Box} from './components/Box.js';
export type {Props as LineProps} from './components/Line.js';
export {default as Line} from './components/Line.js';
export type {Props as TextProps} from './components/Text.js';
export {default as Text} from './components/Text.js';
export type {Props as AppProps} from './components/AppContext.js';
Expand Down
4 changes: 4 additions & 0 deletions src/reconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export default createReconciler<
throw new Error(`<Box> can’t be nested inside <Text> component`);
}

if (hostContext.isInsideText && originalType === 'ink-line') {
throw new Error(`<Line> can’t be nested inside <Text> component`);
}

const type =
originalType === 'ink-text' && hostContext.isInsideText
? 'ink-virtual-text'
Expand Down
29 changes: 29 additions & 0 deletions src/render-line.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import cliBoxes from 'cli-boxes';
import colorize from './colorize.js';
import {type DOMNode} from './dom.js';
import type Output from './output.js';

const renderLine = (
x: number,
y: number,
node: DOMNode,
output: Output
): void => {
if (typeof node.style.borderStyle === 'string') {
const width = Math.max(1, node.yogaNode!.getComputedWidth());
const height = Math.max(1, node.yogaNode!.getComputedHeight());
const color = node.style.borderColor;
const box = cliBoxes[node.style.borderStyle];

const border =
(node as any).attributes.orientation === 'vertical'
? // Vertical line
(colorize(box.left, color, 'foreground') + '\n').repeat(height)
: // Horizontal line
colorize(box.top.repeat(width), color, 'foreground');

output.write(x, y, border, {transformers: []});
}
};

export default renderLine;
6 changes: 6 additions & 0 deletions src/render-node-to-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import squashTextNodes from './squash-text-nodes.js';
import renderBorder from './render-border.js';
import {type DOMElement} from './dom.js';
import type Output from './output.js';
import renderLine from './render-line.js';

// If parent container is `<Box>`, text nodes will be treated as separate nodes in
// the tree and will have their own coordinates in the layout.
Expand Down Expand Up @@ -89,6 +90,11 @@ const renderNodeToOutput = (
return;
}

if (node.nodeName === 'ink-line') {
renderLine(x, y, node, output);
return;
}

let clipped = false;

if (node.nodeName === 'ink-box') {
Expand Down
62 changes: 53 additions & 9 deletions src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Yoga, {type YogaNode} from 'yoga-layout-prebuilt';
import {type Boxes} from 'cli-boxes';
import {type LiteralUnion} from 'type-fest';
import {type ForegroundColorName} from 'chalk';
import {DOMNode} from './dom.js';

export type Styles = {
readonly textWrap?:
Expand Down Expand Up @@ -153,7 +154,12 @@ export type Styles = {
readonly overflowY?: 'visible' | 'hidden';
};

const applyPositionStyles = (node: Yoga.YogaNode, style: Styles): void => {
const applyPositionStyles = (
domNode: DOMNode & {yogaNode: YogaNode},
style: Styles
): void => {
const node = domNode.yogaNode;

if ('position' in style) {
node.setPositionType(
style.position === 'absolute'
Expand All @@ -163,7 +169,12 @@ const applyPositionStyles = (node: Yoga.YogaNode, style: Styles): void => {
}
};

const applyMarginStyles = (node: Yoga.YogaNode, style: Styles): void => {
const applyMarginStyles = (
domNode: DOMNode & {yogaNode: YogaNode},
style: Styles
): void => {
const node = domNode.yogaNode;

if ('marginLeft' in style) {
node.setMargin(Yoga.EDGE_START, style.marginLeft || 0);
}
Expand All @@ -181,7 +192,12 @@ const applyMarginStyles = (node: Yoga.YogaNode, style: Styles): void => {
}
};

const applyPaddingStyles = (node: Yoga.YogaNode, style: Styles): void => {
const applyPaddingStyles = (
domNode: DOMNode & {yogaNode: YogaNode},
style: Styles
): void => {
const node = domNode.yogaNode;

if ('paddingLeft' in style) {
node.setPadding(Yoga.EDGE_LEFT, style.paddingLeft || 0);
}
Expand All @@ -199,7 +215,12 @@ const applyPaddingStyles = (node: Yoga.YogaNode, style: Styles): void => {
}
};

const applyFlexStyles = (node: YogaNode, style: Styles): void => {
const applyFlexStyles = (
domNode: DOMNode & {yogaNode: YogaNode},
style: Styles
): void => {
const node = domNode.yogaNode;

if ('flexGrow' in style) {
node.setFlexGrow(style.flexGrow ?? 0);
}
Expand Down Expand Up @@ -298,7 +319,12 @@ const applyFlexStyles = (node: YogaNode, style: Styles): void => {
}
};

const applyDimensionStyles = (node: YogaNode, style: Styles): void => {
const applyDimensionStyles = (
domNode: DOMNode & {yogaNode: YogaNode},
style: Styles
): void => {
const node = domNode.yogaNode;

if ('width' in style) {
if (typeof style.width === 'number') {
node.setWidth(style.width);
Expand Down Expand Up @@ -336,17 +362,32 @@ const applyDimensionStyles = (node: YogaNode, style: Styles): void => {
}
};

const applyDisplayStyles = (node: YogaNode, style: Styles): void => {
const applyDisplayStyles = (
domNode: DOMNode & {yogaNode: YogaNode},
style: Styles
): void => {
const node = domNode.yogaNode;

if ('display' in style) {
node.setDisplay(
style.display === 'flex' ? Yoga.DISPLAY_FLEX : Yoga.DISPLAY_NONE
);
}
};

const applyBorderStyles = (node: YogaNode, style: Styles): void => {
const applyBorderStyles = (
domNode: DOMNode & {yogaNode: YogaNode},
style: Styles
): void => {
const node = domNode.yogaNode;

if ('borderStyle' in style) {
const borderWidth = typeof style.borderStyle === 'string' ? 1 : 0;
const borderWidth =
domNode.nodeName === 'ink-line'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed access to nodeName here, so I figured I'd update all the applyXYZ method to take the DOM node instead.

? 0
: typeof style.borderStyle === 'string'
? 1
: 0;

node.setBorder(Yoga.EDGE_TOP, borderWidth);
node.setBorder(Yoga.EDGE_BOTTOM, borderWidth);
Expand All @@ -355,7 +396,10 @@ const applyBorderStyles = (node: YogaNode, style: Styles): void => {
}
};

const styles = (node: YogaNode, style: Styles = {}): void => {
const styles = (
node: DOMNode & {yogaNode: YogaNode},
style: Styles = {}
): void => {
applyPositionStyles(node, style);
applyMarginStyles(node, style);
applyPaddingStyles(node, style);
Expand Down
Loading