Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/mean-hornets-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook": patch
---

Update to column width sizing
92 changes: 58 additions & 34 deletions packages/gitbook/src/components/DocumentView/Columns/Columns.tsx
Original file line number Diff line number Diff line change
@@ -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<DocumentBlockColumns>) {
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<number>((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 (
<div className={tcls('flex flex-col gap-x-8 md:flex-row', style)}>
{block.nodes.map((columnBlock) => {
<div
className={tcls(
'grid w-full grid-cols-1 gap-x-8 gap-y-4 md:grid-flow-col md:grid-cols-[repeat(var(--grid-slices),minmax(0,1fr))] md:grid-rows-1',
style
)}
style={{ '--grid-slices': COLUMN_DIVISIONS } as React.CSSProperties}
>
{block.nodes.map((columnBlock, index) => {
const columnWidth = columnWidths[index];
return (
<Column
key={columnBlock.key}
width={columnBlock.data.width}
width={
columnBlock.data.width ??
(columnWidth
? { value: columnWidth * 100, unit: '%' }
: { value: 0, unit: '%' })
}
verticalAlignment={columnBlock.data.verticalAlignment}
>
<Blocks
Expand All @@ -31,11 +71,11 @@ export function Columns(props: BlockProps<DocumentBlockColumns>) {

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 (
<div
className={tcls(
Expand All @@ -53,41 +93,25 @@ export function Column(props: {
);
}

function transformLengthToCSS(length: Length | undefined) {
if (!length) {
return { className: ['md:w-full'] }; // default to full width if no length is specified
}
const COLUMN_DIVISIONS = 12;

export function transformLengthToCSS(length: Length) {
if (typeof length === 'number') {
return { style: undefined }; // not implemented yet with non-percentage lengths
return; // not implemented yet with non-percentage lengths
}

if (length.unit === '%') {
return {
className: [
'md:shrink-0',
COLUMN_WIDTHS[Math.round(length.value * 0.01 * (COLUMN_WIDTHS.length - 1))],
],
className: 'md:flex-shrink-0 md:[grid-column:var(--grid-col)]',
style: {
'--grid-col': `auto / span ${Math.round(length.value * 0.01 * COLUMN_DIVISIONS)}`,
} as React.CSSProperties,
};
}

return { style: undefined }; // not implemented yet with non-percentage lengths
}

// Tailwind CSS classes for column widths.
// The index of the array corresponds to the percentage width of the column.
const COLUMN_WIDTHS = [
'md:w-0',
'md:w-1/12',
'md:w-2/12',
'md:w-3/12',
'md:w-4/12',
'md:w-5/12',
'md:w-6/12',
'md:w-7/12',
'md:w-8/12',
'md:w-9/12',
'md:w-10/12',
'md:w-11/12',
'md:w-full',
];
function getFractionalWidth(length: Length) {
if (typeof length === 'number') {
return 0;
}
return length.value / 100;
}