asciitorium
Version:
an ASCII ui framework for web + cli
46 lines (45 loc) • 1.9 kB
JavaScript
import { Component } from '../core/Component';
export class Box extends Component {
constructor(options) {
// Calculate auto-dimensions from children if not provided
const autoWidth = options.width ?? Box.calculateAutoWidth(options.children, options.layout);
const autoHeight = options.height ?? Box.calculateAutoHeight(options.children, options.layout);
// Account for border if present (add 2 for left+right or top+bottom)
const borderAdjustment = options.border ? 2 : 0;
const finalWidth = options.width ?? (autoWidth + borderAdjustment);
const finalHeight = options.height ?? (autoHeight + borderAdjustment);
super({
...options,
width: finalWidth,
height: finalHeight,
layout: options.layout ?? 'vertical', // Default to vertical layout
});
}
static calculateAutoWidth(children, layout) {
if (!children || children.length === 0)
return 1;
if (layout === 'horizontal') {
// Sum widths + gaps for horizontal layout
return children.reduce((sum, child) => sum + child.width + child.gap, 0);
}
else {
// Max width for vertical layout
return Math.max(...children.map(child => child.width));
}
}
static calculateAutoHeight(children, layout) {
if (!children || children.length === 0)
return 1;
if (layout === 'vertical') {
// Sum heights + gaps for vertical layout
return children.reduce((sum, child) => sum + child.height + child.gap, 0);
}
else {
// Max height for horizontal layout
return Math.max(...children.map(child => child.height));
}
}
draw() {
return super.draw(); // Uses Component's built-in rendering with children support
}
}