/** * blocks * * Copyright (c) Ballerine * * This source code is licensed under the MIT license found in the * LICENSE.md file in the root directory of this source tree. * * @license MIT */ import * as React from 'react'; import { FunctionComponent, ReactNode, ComponentProps } from 'react'; import { BlocksProps as BlocksProps$1 } from '@/blocks'; /** * A cell may have any property as long as it has a `type` property to discriminate a union by. */ type Cell = { type: string; } & { [key: string]: unknown; }; type Block = Cell[]; type Blocks = Block[]; /** * Takes an array of arrays and flattens it once. [[1,2], [3,4]] => [1,2,3,4] */ type FlattenOnce = T extends [infer U, ...infer V] ? U extends any[] ? [...U, ...FlattenOnce] : [U, ...FlattenOnce] : []; /** * Allow the consumer of `@ballerine/blocks` to register their own cell types. * @example * declare module '@ballerine/blocks' { * interface BlocksClient { * cells: typeof blocks; // createBlocks(); * } * } */ interface BlocksClient { } /** * Infer the generic passed into an instance of `BlocksBuilder`. */ type InferBlocksBuilderGeneric> = TClass extends BlocksBuilder ? TCell : never; /** * Infer the generic passed into the instance of `BlocksBuilder` registered by the consumer of `@ballerine/blocks`. */ type Cells = InferBlocksBuilderGeneric; /** * Extract the type of cell from the registered cells. */ type CellType = Cells['type']; /** * Extract the props of a cell by type from the registered cells. * @example * const Cell: FunctionComponent> = ({ text }) =>

{text}

; */ type ExtractCellProps = Omit, 'type'>; /** * A type to help build objects of cell components. * @example * const cells: CellsMap = { * text: TextCell, * image: ImageCell, * } */ type CellsMap = { [TType in CellType]: FunctionComponent>; }; type InferAllButLastArrayElements = T extends [...infer U, any] ? U : []; type InferLastArrayElement = T extends [...any, infer U] ? U : never; type InferArrayElement = T extends Array ? U : never; interface BlocksProps { /** * @description A map of cell components * @example * const cells: CellsMap = { * text: TextCell, * image: ImageCell, * } */ cells: CellsMap; /** * @description Output of `createBlocks.build()` * @see {@link BlockBuilder.build} */ blocks: TCell[][]; /** * The `block` prop is only passed when the `Block` property component is passed. */ Block?: FunctionComponent<{ children: ReactNode | ReactNode[]; block: TCell[]; }>; /** * @description children as a function - provides access to the current block and cell * @param Cell * @param cell * @param block */ children: (Cell: CellsMap[keyof CellsMap], cell: ComponentProps, block: TCell[]) => ReactNode | ReactNode[]; } type InvalidCellMessage = "Please provide a union of available cell types discriminated by '{ type: string; }'"; interface BlocksOptions { debug?: boolean; verbose?: boolean; } declare class BlocksBuilder { #private; get blocksCount(): number; cellsCount(blockIndex: number): number; blockAt(index: TIndex): TBlocks[TIndex]; cellAt(blockIndex: TBlockIndex, cellIndex: TCellIndex): TBlocks[TBlockIndex][TCellIndex]; constructor(blocks: TBlocks, options: BlocksOptions); addBlock(): BlocksBuilder; addCell(cell: TCellType): BlocksBuilder, [...InferAllButLastArrayElements, InferLastArrayElement, TCellType]]>; build(): TBlocks; buildFlat(): FlattenOnce; } declare const createBlocks: (options?: BlocksOptions) => BlocksBuilder; declare const BlocksComponent: React.ForwardRefExoticComponent & React.RefAttributes | undefined>>; export { Block, Blocks, BlocksBuilder, BlocksClient, BlocksComponent, BlocksOptions, BlocksProps, Cell, CellType, Cells, CellsMap, ExtractCellProps, FlattenOnce, InferAllButLastArrayElements, InferArrayElement, InferLastArrayElement, InvalidCellMessage, createBlocks };