element-book
Version:
An [`element-vir`](https://npmjs.com/package/element-vir) drop-in element for building, testing, and demonstrating a collection of elements (or, in other words, a design system).
93 lines (92 loc) • 3.06 kB
TypeScript
import { type EmptyObject } from 'type-fest';
/**
* Adds a control to an element-book page.
*
* @category Internal
*/
export type BookPageControl<ControlType extends BookPageControlType = BookPageControlType> = {
controlType: ControlType;
initValue: BookPageControlValueType[ControlType];
/** The name and label for the control. */
controlName: string;
} & (ControlType extends BookPageControlType.Dropdown ? {
options: string[];
} : EmptyObject);
/**
* Initialization options for an element-book page control.
*
* @category Internal
*/
export type BookPageControlInit<ControlType extends BookPageControlType> = Omit<BookPageControl<ControlType>, 'controlName'>;
/**
* Checks and type guards that the input page control init is of the given type.
*
* @internal
*/
export declare function isControlInitType<const SpecificControlType extends BookPageControlType>(controlInit: BookPageControlInit<any>, specificType: SpecificControlType): controlInit is BookPageControlInit<SpecificControlType>;
/**
* Define a page control. This doesn't do anything fancy (in fact it only returns the input) but it
* helps immensely with type inference.
*
* @category Main
*/
export declare function definePageControl<const ControlType extends BookPageControlType>(controlInit: BookPageControlInit<ControlType>): BookPageControlInit<ControlType>;
/**
* Maps an object of user-defined controls to their initial values.
*
* @category Internal
*/
export type ControlsToValues<ControlsInit extends BookPageControlsInitBase> = {
[ControlName in keyof ControlsInit]: ControlsInit[ControlName]['initValue'];
};
/**
* Base type for a arbitrary, user-defined object of page controls.
*
* @category Internal
*/
export type BookPageControlsInitBase = Record<string, BookPageControlInit<BookPageControlType>>;
/**
* Base type for a arbitrary, user-defined object of page control values.
*
* @category Internal
*/
export type BookPageControlsValues = ControlsToValues<BookPageControlsInitBase>;
/**
* All the supported page control types. One of these must be chosen when defining a page control.
*
* @category Main
*/
export declare enum BookPageControlType {
Checkbox = "checkbox",
Color = "color",
Dropdown = "dropdown",
/** Hidden controls allow any values but they aren't displayed to the user for editing. */
Hidden = "hidden",
Number = "number",
Text = "text"
}
/**
* Specifies the default value for each page control type, as well as its type.
*
* @category Internal
*/
export declare const controlValueTypes: {
checkbox: boolean;
color: string;
dropdown: string;
hidden: any;
number: number;
text: string;
};
/**
* Each page control type mapped to the type of their value.
*
* @category Internal
*/
export type BookPageControlValueType = typeof controlValueTypes;
/**
* Checks that the given control init object is valid.
*
* @internal
*/
export declare function checkControls(controlsInit: BookPageControlsInitBase | undefined, pageName: string): Error[];