From 8df1ebbceaab54df2ff7ab7554f0d2cbd7746356 Mon Sep 17 00:00:00 2001 From: Viktor Renkema Date: Thu, 12 Dec 2024 17:46:50 +0100 Subject: [PATCH] Apply vertical alignment to Tables as configured in-app --- .../DocumentView/Table/RecordColumnValue.tsx | 17 ++++++++-- .../DocumentView/Table/RecordRow.tsx | 18 +++++++++-- .../DocumentView/Table/table.module.css | 2 +- .../components/DocumentView/Table/utils.ts | 32 ++++++++++++++++++- 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx b/packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx index af5a00a485..345538738b 100644 --- a/packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx +++ b/packages/gitbook/src/components/DocumentView/Table/RecordColumnValue.tsx @@ -11,7 +11,7 @@ import { tcls } from '@/lib/tailwind'; import { filterOutNullable } from '@/lib/typescript'; import { TableRecordKV } from './Table'; -import { getColumnAlignment } from './utils'; +import { getColumnAlignment, VerticalAlignment } from './utils'; import { BlockProps } from '../Block'; import { Blocks } from '../Blocks'; import { FileIcon } from '../FileIcon'; @@ -25,9 +25,19 @@ export async function RecordColumnValue( ariaLabelledBy?: string; record: TableRecordKV; column: string; + verticalAlignment: VerticalAlignment; }, ) { - const { tag: Tag = 'div', ariaLabelledBy, block, document, record, column, context } = props; + const { + tag: Tag = 'div', + ariaLabelledBy, + block, + document, + record, + column, + context, + verticalAlignment, + } = props; const definition = block.data.definition[column]; const value = record[1].values[column]; @@ -99,7 +109,7 @@ export async function RecordColumnValue( // @ts-ignore const fragment = getNodeFragmentByName(block, value); if (!fragment) { - return {''}; + return {''}; } const alignment = getColumnAlignment(definition); @@ -115,6 +125,7 @@ export async function RecordColumnValue( 'space-y-2', 'lg:space-y-3', 'leading-normal', + verticalAlignment, alignment === 'right' ? 'text-right' : null, alignment === 'center' ? 'text-center' : null, ]} diff --git a/packages/gitbook/src/components/DocumentView/Table/RecordRow.tsx b/packages/gitbook/src/components/DocumentView/Table/RecordRow.tsx index 3658de32b0..358f8c7e13 100644 --- a/packages/gitbook/src/components/DocumentView/Table/RecordRow.tsx +++ b/packages/gitbook/src/components/DocumentView/Table/RecordRow.tsx @@ -1,9 +1,12 @@ import { DocumentTableViewGrid } from '@gitbook/api'; import React from 'react'; +import { tcls } from '@/lib/tailwind'; + import { RecordColumnValue } from './RecordColumnValue'; import { TableRecordKV, TableViewProps } from './Table'; import styles from './table.module.css'; +import { getColumnVerticalAlignment } from './utils'; import { getColumnWidth } from './ViewGrid'; export function RecordRow( @@ -13,7 +16,7 @@ export function RecordRow( fixedColumns: string[]; }, ) { - const { view, autoSizedColumns, fixedColumns } = props; + const { view, autoSizedColumns, fixedColumns, block } = props; return (
@@ -24,17 +27,26 @@ export function RecordRow( autoSizedColumns, fixedColumns, }); + const verticalAlignment = getColumnVerticalAlignment( + block.data.definition[column], + view, + ); + return (
- +
); })} diff --git a/packages/gitbook/src/components/DocumentView/Table/table.module.css b/packages/gitbook/src/components/DocumentView/Table/table.module.css index 302b975f97..000ffa77ee 100644 --- a/packages/gitbook/src/components/DocumentView/Table/table.module.css +++ b/packages/gitbook/src/components/DocumentView/Table/table.module.css @@ -35,7 +35,7 @@ } .cell { - @apply flex-1 align-middle border-dark/2 py-2 px-3 text-sm lg:text-base dark:border-light/2; + @apply flex-1 flex align-middle border-dark/2 py-2 px-3 text-sm h-full lg:text-base dark:border-light/2; } .columnHeader { diff --git a/packages/gitbook/src/components/DocumentView/Table/utils.ts b/packages/gitbook/src/components/DocumentView/Table/utils.ts index 6a584adfa8..c86429c253 100644 --- a/packages/gitbook/src/components/DocumentView/Table/utils.ts +++ b/packages/gitbook/src/components/DocumentView/Table/utils.ts @@ -1,4 +1,9 @@ -import { ContentRef, DocumentTableRecord, DocumentTableDefinition } from '@gitbook/api'; +import { + ContentRef, + DocumentTableRecord, + DocumentTableDefinition, + DocumentTableViewGrid, +} from '@gitbook/api'; /** * Get the value for a column in a record. @@ -20,3 +25,28 @@ export function getColumnAlignment(column: DocumentTableDefinition): 'left' | 'r } return 'left'; } + +/** + * Get the vertical alignment for a column. + * + * Vertical alignment is configurable on both table and column-level. + * Column-level takes priority over table-level. + */ + +export type VerticalAlignment = 'self-center' | 'self-end' | 'self-start'; +export function getColumnVerticalAlignment( + column: DocumentTableDefinition, + view: DocumentTableViewGrid, +): VerticalAlignment { + const verticalAlignment = column.verticalAlignment ?? view.verticalAlignment ?? 'center'; + + if (verticalAlignment === 'top') { + return 'self-start'; + } + + if (verticalAlignment === 'bottom') { + return 'self-end'; + } + + return 'self-center'; +}