A TypeScript library for advanced DOM element height calculations, with support for complex layouts including CSS Grid, Flexbox, and nested containers.
- π― Accurate height calculation for complex CSS layouts (Grid, Flexbox, Block)
- π Recursive depth control to balance performance and precision
- β‘ Multiple calculation modes (fast, standard, precise, grid-optimized)
- ποΈ Configurable options for margins, padding, and scroll height
- π± Universal compatibility works with all modern browsers
- π Full TypeScript support with comprehensive type definitions
npm install @yuntijs/dom-helpers
# or
pnpm install @yuntijs/dom-helpers
# or
yarn add @yuntijs/dom-helpersimport { getContentHeight, getContentHeightFast } from '@yuntijs/dom-helpers';
const container = document.getElementById('my-container');
// Basic usage
const height = getContentHeight(container);
// Fast calculation (performance-optimized)
const fastHeight = getContentHeightFast(container);
// Custom configuration
const preciseHeight = getContentHeight(container, {
maxDepth: 5,
includeMargins: true,
includePadding: true,
layoutTypes: ['grid', 'flex', 'block']
});The primary function for calculating element content height.
Parameters:
container: HTMLElement- The DOM element to calculate height foroptions?: HeightCalculationOptions- Configuration options
Returns: number - The calculated height in pixels
// Fast mode: 1 layer depth, uses scrollHeight
getContentHeightFast(container: HTMLElement): number
// Standard mode: 3 layers, optimized for grid/flex
getContentHeightPrecise(container: HTMLElement): number
// Grid optimized: specialized for CSS Grid layouts
getContentHeightForGrid(container: HTMLElement): numberinterface HeightCalculationOptions {
maxDepth?: number; // Maximum recursion depth (default: 3)
includeMargins?: boolean; // Include element margins (default: true)
includePadding?: boolean; // Include container padding (default: true)
useScrollHeight?: boolean; // Use scrollHeight as fallback (default: false)
layoutTypes?: string[]; // Layout types for deep calculation (default: ['grid', 'flex', 'block'])
}| Preset | Max Depth | Layout Types | Use Case |
|---|---|---|---|
fast |
1 | - | Performance-critical scenarios |
standard |
3 | grid, flex | General purpose, balanced |
precise |
5 | grid, flex, block | High accuracy needed |
gridOptimized |
2 | grid | CSS Grid specific layouts |
import { getContentHeight } from '@yuntijs/dom-helpers';
const container = document.querySelector('.content-wrapper');
const height = getContentHeight(container);
console.log(`Content height: ${height}px`);import { getContentHeightForGrid } from '@yuntijs/dom-helpers';
const gridContainer = document.querySelector('.grid-container');
const gridHeight = getContentHeightForGrid(gridContainer);
// Apply calculated height
gridContainer.style.height = `${gridHeight}px`;import { getContentHeight } from '@yuntijs/dom-helpers';
const complexLayout = document.querySelector('.complex-layout');
const height = getContentHeight(complexLayout, {
maxDepth: 4,
includeMargins: false,
includePadding: true,
useScrollHeight: true,
layoutTypes: ['grid', 'flex']
});import {
getContentHeight,
getContentHeightFast,
getContentHeightPrecise
} from '@yuntijs/dom-helpers';
const element = document.getElementById('dynamic-content');
// Fast but less accurate
const fastHeight = getContentHeightFast(element);
// Balanced approach
const standardHeight = getContentHeight(element);
// Slow but most accurate
const preciseHeight = getContentHeightPrecise(element);
console.log({ fastHeight, standardHeight, preciseHeight });- β Real-time calculations during scrolling/resizing
- β Simple layouts without complex nesting
- β Performance is critical
- β Most common use cases
- β Grid and Flexbox layouts
- β Good balance of speed and accuracy
- β Complex nested layouts
- β Accuracy is more important than speed
- β Final calculations for layout positioning
- β CSS Grid specific issues
- β Grid items with dynamic content
- β Grid containers with auto-sizing
- Chrome 60+
- Firefox 55+
- Safari 12+
- Edge 79+
# Install dependencies
pnpm install
# Development mode
npm run dev
# Build library
npm run build
# Run tests
npm test
# Type checking
npm run type-checkMIT