Files

79 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2026-10-12 19:14:14 -04:00
import React, { createContext, useContext, useMemo } from 'react';
2026-10-12 09:04:03 -04:00
import { PanelLink, type PanelDestination } from '@/sdk/navigation';
2026-10-12 19:14:14 -04:00
import { replaceable, useSkin, type PrimitiveState, type UiTone } from '@/themes/skins';
2026-10-12 09:04:03 -04:00
export interface ListRowProps extends React.HTMLAttributes<HTMLElement> {
as?: 'div' | 'li' | 'article';
/** Renders the row as a link to the destination. */
destination?: PanelDestination;
exact?: boolean;
/** Hover and press feedback; on for links and clickable rows unless set. */
interactive?: boolean;
2026-10-12 19:14:14 -04:00
/** Draws the `status` part in this tone, such as a server's power state. */
status?: UiTone;
2026-10-12 09:04:03 -04:00
}
2026-10-12 19:14:14 -04:00
const RowState = createContext<PrimitiveState<'ListRow'>>({ interactive: false });
2026-10-12 09:04:03 -04:00
/** A row of a list, such as a server on the dashboard. */
2026-10-12 19:14:14 -04:00
function CoreListRow({
as: Element = 'div',
destination,
exact,
interactive,
status,
className,
children,
...props
}: ListRowProps) {
const clickable = interactive ?? (destination !== undefined || props.onClick !== undefined);
const state = useMemo(() => ({ interactive: clickable, status }), [clickable, status]);
const ui = useSkin('ListRow', state);
const content = (
<RowState.Provider value={state}>
{children}
{status && <div {...ui('status')} />}
</RowState.Provider>
);
2026-10-12 09:04:03 -04:00
if (destination) {
2026-10-12 19:14:14 -04:00
return (
<PanelLink destination={destination} exact={exact} {...ui('root', className)} {...props}>
{content}
</PanelLink>
);
2026-10-12 09:04:03 -04:00
}
2026-10-12 19:14:14 -04:00
return (
<Element {...ui('root', className)} {...props}>
{content}
</Element>
);
}
2026-10-12 20:14:18 -04:00
interface ListRowPartProps {
className?: string;
children?: React.ReactNode;
}
/** A part of the row around it, for the row's content to place. */
function rowPart(part: 'icon' | 'title' | 'description', Element: 'div' | 'p') {
function ListRowPart({ className, children }: ListRowPartProps) {
const ui = useSkin('ListRow', useContext(RowState));
return <Element {...ui(part, className)}>{children}</Element>;
}
ListRowPart.displayName = `ListRow.${part.charAt(0).toUpperCase()}${part.slice(1)}`;
2026-10-12 19:14:14 -04:00
2026-10-12 20:14:18 -04:00
return ListRowPart;
2026-10-12 09:04:03 -04:00
}
2026-10-12 20:14:18 -04:00
const ListRow = Object.assign(replaceable('ListRow', CoreListRow), {
Icon: rowPart('icon', 'div'),
Title: rowPart('title', 'p'),
Description: rowPart('description', 'p'),
});
2026-10-12 09:04:03 -04:00
export default ListRow;