diff --git a/.changeset/mean-hornets-sip.md b/.changeset/mean-hornets-sip.md new file mode 100644 index 0000000000..60732ad4be --- /dev/null +++ b/.changeset/mean-hornets-sip.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Update to column width sizing diff --git a/packages/gitbook/src/components/DocumentView/Columns/Columns.tsx b/packages/gitbook/src/components/DocumentView/Columns/Columns.tsx index fc98d7548d..5e7ef3cdc8 100644 --- a/packages/gitbook/src/components/DocumentView/Columns/Columns.tsx +++ b/packages/gitbook/src/components/DocumentView/Columns/Columns.tsx @@ -1,17 +1,57 @@ import { tcls } from '@/lib/tailwind'; import { type DocumentBlockColumns, type Length, VerticalAlignment } from '@gitbook/api'; +import React from 'react'; import type { BlockProps } from '../Block'; import { Blocks } from '../Blocks'; export function Columns(props: BlockProps) { const { block, style, ancestorBlocks, document, context } = props; + + const columnWidths = React.useMemo(() => { + const widths = block.nodes.map((block) => { + const width = block.data.width; + return width ? getFractionalWidth(width) : 0; + }); + + const totalWidth = widths.reduce((acc, width) => acc + width, 0); + // If not all columns widths are set, distribute the remaining widths as equally as we can + if (totalWidth < 1.0 && widths.some((width) => width === 0)) { + const unsetWidths = widths.filter((width) => width === 0); + let remainingWidth = 1.0 - totalWidth; + let unsetWidthsLength = unsetWidths.length; + widths.forEach((width, index) => { + if (width === 0) { + const calculatedWidth = + Math.round((remainingWidth / unsetWidthsLength) * COLUMN_DIVISIONS) / + COLUMN_DIVISIONS; + widths[index] = calculatedWidth; // Assign width to empty columns + unsetWidthsLength--; + remainingWidth -= calculatedWidth; + } + }); + } + return widths; + }, [block.nodes]); + return ( -
- {block.nodes.map((columnBlock) => { +
+ {block.nodes.map((columnBlock, index) => { + const columnWidth = columnWidths[index]; return ( ) { export function Column(props: { children?: React.ReactNode; - width?: Length; + width: Length; verticalAlignment?: VerticalAlignment; }) { const { width, verticalAlignment } = props; - const { className, style } = transformLengthToCSS(width); + const { className, style } = transformLengthToCSS(width) ?? {}; return (