websnacks/src/component.ts
M. George Hansen da241efb59
feat: provide Fragment component
Adds a new Fragment component that provides the same functionality as
React.Fragment. See issue #9 for details.

closes issue #9
2020-06-04 11:25:34 -07:00

52 lines
1.2 KiB
TypeScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* An in-memory representation of a renderable HTML element.
*/
export interface HTMLElement {
/**
* Name of the tag that gets output upon rendering.
*/
tag: string;
/**
* Record of attribute names and values that should be output in the opening
* tag.
*/
attributes: Record<string, string | number | boolean>;
/**
* Child elements to render nested within this HTML element.
*/
children: Element[];
}
/**
* All valid types of elements that can be rendered to HTML.
*/
export type Element =
| Element[]
| HTMLElement
| string
| number
| boolean
| undefined
| null;
/**
* Custom HTMLElement factory that can be parameterized by props.
*/
export interface Component<P extends object = {}> {
(
props: P & {
children?: Element[];
}
): HTMLElement;
}
export const Fragment: Component<{}> = ({ children }) => ({
tag: "#fragment",
attributes: {},
children: children || [],
});