Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next: Progress component #470

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 25 additions & 33 deletions packages/bits-ui/src/lib/bits/progress/components/progress.svelte
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
<script lang="ts">
import { melt } from "@melt-ui/svelte";
import { setCtx } from "../ctx.js";
import type { Props } from "../index.js";
import type { RootProps } from "../index.js";
import { setProgressRootState } from "../progress.svelte.js";
import { readonlyBox } from "$lib/internal/box.svelte.js";
import { styleToString } from "$lib/internal/style.js";

type $$Props = Props;
let {
asChild,
child,
children,
value: valueProp = 0,
max: maxProp = 100,
el = $bindable(),
style = {},
...restProps
}: RootProps = $props();

export let max: $$Props["max"] = undefined;
export let value: $$Props["value"] = undefined;
export let onValueChange: $$Props["onValueChange"] = undefined;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const value = readonlyBox(() => valueProp);
const max = readonlyBox(() => maxProp);

const {
elements: { root },
states: { value: localValue },
updateOption,
getAttrs,
} = setCtx({
max,
defaultValue: value,
onValueChange: ({ next }) => {
onValueChange?.(next);
value = next;
return next;
},
});
const progress = setProgressRootState({ value, max });

const attrs = getAttrs("root");

$: value !== undefined && localValue.set(value);
$: updateOption("max", max);

$: builder = $root;
$: Object.assign(builder, attrs);
const mergedProps = {
...restProps,
...progress.props,
style: styleToString(style),
};
</script>

{#if asChild}
<slot {builder} />
{@render child?.({ props: mergedProps })}
{:else}
<div bind:this={el} use:melt={builder} {...$$restProps}>
<slot {builder} />
<div bind:this={el} {...mergedProps}>
{@render children?.()}
</div>
{/if}
23 changes: 0 additions & 23 deletions packages/bits-ui/src/lib/bits/progress/ctx.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/bits-ui/src/lib/bits/progress/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as Root } from "./components/progress.svelte";

export type { ProgressProps as Props } from "./types.js";
export type { ProgressRootProps as RootProps } from "./types.js";
53 changes: 53 additions & 0 deletions packages/bits-ui/src/lib/bits/progress/progress.svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { BoxedValues, ReadonlyBoxedValues } from "$lib/internal/box.svelte.js";

type ProgressRootStateProps = ReadonlyBoxedValues<{
value: number | null;
max: number;
}>;

class ProgressRootState {
#value = undefined as unknown as ProgressRootStateProps["value"];
#max = undefined as unknown as ProgressRootStateProps["max"];

#attrs = $derived({
role: "meter",
value: this.#value.value,
max: this.#max.value,
"aria-valuemin": 0,
"aria-valuemax": this.#max.value,
"aria-valuenow": this.#value.value,
"data-value": this.#value.value,
"data-state": getProgressDataState(this.#value.value, this.#max.value),
"data-max": this.#max.value,
"data-bits-progress-root": "",
} as const);

constructor(props: ProgressRootStateProps) {
this.#value = props.value;
this.#max = props.max;
}

get props() {
return this.#attrs;
}
}

//
// HELPERS
//

function getProgressDataState(
value: number | null,
max: number
): "indeterminate" | "loaded" | "loading" {
if (value === null) return "indeterminate";
return value === max ? "loaded" : "loading";
}

//
// STATE PROVIDERS
//

export function setProgressRootState(props: ProgressRootStateProps) {
return new ProgressRootState(props);
}
37 changes: 14 additions & 23 deletions packages/bits-ui/src/lib/bits/progress/types.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import type { CreateProgressProps as MeltProgressProps } from "@melt-ui/svelte";
import type {
DOMElement,
Expand,
HTMLDivAttributes,
OmitValue,
OnChangeFn,
} from "$lib/internal/index.js";
import type { PrimitiveDivAttributes, WithAsChild } from "$lib/internal/index.js";

export type ProgressPropsWithoutHTML = Expand<
OmitValue<MeltProgressProps> & {
/**
* The value of the progress bar.
* You can bind this to a number value to programmatically control the value.
*/
value?: MeltProgressProps["defaultValue"];
export type ProgressRootPropsWithoutHTML = WithAsChild<{
/**
* The current value of the progress bar.
* If `null`, the progress bar will be in an indeterminate state.
*/
value?: number | null;

/**
* A callback function called when the value changes.
*/
onValueChange?: OnChangeFn<number | null>;
} & DOMElement
>;
//
/**
* The maximum value of the progress bar. Used to calculate the percentage
* of the progress bar along with the `value` prop.
*/
max?: number;
}>;

export type ProgressProps = ProgressPropsWithoutHTML & HTMLDivAttributes;
export type ProgressRootProps = ProgressRootPropsWithoutHTML & PrimitiveDivAttributes;
Loading