import type { JSX, ValidComponent } from "@solidjs/web"; import { dynamic } from "@solidjs/web"; import { omit } from "solid-js"; import { mergeRootProps } from "../../internals/merge-root-props"; import { resolveClassStyle } from "../../internals/resolve-class-style"; import type { BaseUIComponentProps } from "../../internals/types"; import { type ProgressRootState, useProgressRootContext, } from "../progress-root-context.ts"; export interface ProgressValueState extends ProgressRootState {} export type ProgressValueChildren = | JSX.Element | ((formattedValue: string | null, value: number | null) => JSX.Element); export interface ProgressValueProps extends Omit, "children"> { /** * Tag/component to render. Defaults to `"span"`. Replaces the * pre-spike `render` prop (axis G). */ as?: ValidComponent; children?: ProgressValueChildren | undefined; } /** * A text label displaying the current value. Renders a `` (or `as` * override). */ export function ProgressValue(props: ProgressValueProps): JSX.Element { const context = useProgressRootContext(); const content = (): JSX.Element => { const value = context.value(); const formattedValue = context.formattedValue(); const child = props.children; if (typeof child === "function") { const arg = value == null ? "indeterminate" : formattedValue; return child(arg, value); } if (child !== undefined) return child; return value == null ? null : formattedValue; }; // Resolve `class`/`style` so function values (stateful styling) are invoked // with the progress state instead of being spread to the DOM raw. const userClassStyle = resolveClassStyle(props, () => ({ status: context.status(), })); const userProps = omit(props, "as", "children", "class", "style", "attrs"); const internalSlot = { "aria-hidden": true, get children(): JSX.Element { return content(); }, get "data-indeterminate"(): "" | undefined { return context.status() === "indeterminate" ? "" : undefined; }, get "data-progressing"(): "" | undefined { return context.status() === "progressing" ? "" : undefined; }, get "data-complete"(): "" | undefined { return context.status() === "complete" ? "" : undefined; }, }; let merged: Record = mergeRootProps( internalSlot, userClassStyle, ); merged = mergeRootProps(merged, userProps); const Tag = dynamic(() => props.as ?? "span"); return ; }