Handbook
TypeScript
A guide to using TypeScript with Solid Base.
Namespaces#
Types are organized in namespaces, merged with each part. Every part has two core types:
Props(such asTooltip.Root.Props)State(such asTooltip.Root.State)
They are available on the namespace you import (Tooltip.Root.Props), and as flat named exports
from the same module (TooltipRootProps, TooltipRootState).
Props#
When creating wrapping components, you can use the Props type to accept all of the underlying
props for the component.
import { Tooltip } from '@egoist/solid-base/tooltip';
function MyTooltip(props: Tooltip.Root.Props) {
return <Tooltip.Root {...props} />;
}State#
The State type is the internal state of the component. It is the argument of the function form
of class, style and render. For example, Positioner parts (such as <Popover.Positioner>)
have state that describes the position of the element in relation to their anchor.
import type { HTMLProps } from '@egoist/solid-base';
import { Popover } from '@egoist/solid-base/popover';
function renderPositioner(props: HTMLProps, state: Popover.Positioner.State) {
return (
<div {...props}>
<ul>
<li>The popover is {state.open ? 'open' : 'closed'}</li>
<li>I am on the {state.side} side of the anchor</li>
<li>I am aligned at the {state.align} of the side</li>
<li>The anchor is {state.anchorHidden ? 'hidden' : 'visible'}</li>
</ul>
{props.children}
</div>
);
}
<Popover.Positioner render={renderPositioner} />;state is a live object: each expression in the JSX above updates when the property it reads
changes.
Events#
Types relating to change events are also exported on the parts' namespaces.
ChangeEventDetails(such asCombobox.Root.ChangeEventDetails) is the object passed to change handlers likeonValueChange.OpenChangeEventDetails(such asCombobox.Root.OpenChangeEventDetails) is the object passed toonOpenChangeon components whoseChangeEventDetailsis shared with other handlers. It extendsChangeEventDetailswithpreventUnmountOnClose().ChangeEventReason(such asCombobox.Root.ChangeEventReason) is the union of possible reason strings for a change event.
import { Combobox } from '@egoist/solid-base/combobox';
function onValueChange(value: string, eventDetails: Combobox.Root.ChangeEventDetails) {
console.log(value, eventDetails);
}
function onOpenChange(open: boolean, eventDetails: Combobox.Root.OpenChangeEventDetails) {
console.log(open, eventDetails);
}The generic BaseUIChangeEventDetails type, and BaseUIEvent<E> — a native event extended with
preventBaseUIHandler() — are exported from the root entry:
import type { BaseUIChangeEventDetails, BaseUIEvent } from '@egoist/solid-base';
function handleClick(event: BaseUIEvent<MouseEvent>) {
event.preventBaseUIHandler();
}Other accessible types#
Depending on the component API, other types are also exported on component parts or utility functions. The following list is non-exhaustive, and each of these are documented where necessary.
- Popups have an
actionsRefprop to access imperative methods. For example,Menu.Root.Actionsgives access to the shape of theactionsRefobject prop on<Menu.Root>. - The
toastobject on<Toast.Root>is a complex object with many properties.Toast.Root.ToastObjectgives access to this interface. - Components that have a
renderprop have Solid's intrinsic element props, enhanced with arenderprop. TheuseRender.ComponentPropstype gives access to this interface for your own components, andHTMLPropstypes the props arenderfunction receives. - Data attribute and CSS variable names are exported as constants, such as
MenuItemDataAttributes.highlighted('data-highlighted') andMenuPositionerCssVariables.availableHeight('--available-height').
import { useRender } from '@egoist/solid-base/use-render';
interface CounterState {
odd: boolean;
}
interface CounterProps extends useRender.ComponentProps<'button', CounterState> {}