import { Component, ComponentChildren, ContextType, createContext, } from "preact"; type CtxProps = { min: number; max: number; striped: boolean; animated: boolean; }; const PCtx = createContext(null); type BarProps = { value: number; striped?: boolean; animated?: boolean; class?: string; }; class ProgressBar extends Component { static contextType = PCtx; declare context: ContextType; render() { let cls = "progress-bar"; const striped = this.props.striped === undefined ? this.context?.striped : this.props.striped; const animated = this.props.animated === undefined ? this.context?.animated : this.props.animated; if (striped || animated) cls += " progress-bar-striped"; if (animated) cls += " progress-bar-animated"; if (this.props.class) cls += " " + this.props.class; const max = this.context?.max || 100; const min = this.context?.min || 0; const v = this.props.value; const style = `width: ${(v - min) / (max - min) * 100}%;`; return (
); } } type Props = { /**@default {0} */ min?: number; /**@default {100} */ max?: number; /**@default {false} */ striped?: boolean; /**@default {false} */ animated?: boolean; children: ComponentChildren; }; export default class Progress extends Component { static readonly Bar = ProgressBar; render() { return (
{this.props.children}
); } }