mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-18 15:44:36 +00:00
contextual loading bar
This commit is contained in:
1 parent
1f4a648597
commit
cae7a0a979
10 files changed
+285
-79
No files matched your search
@@ -0,0 +1,12 @@
|
||||
import { useSignals } from "@preact/signals-react/runtime";
|
||||
|
||||
import { LoadingBar } from "~/components/ui";
|
||||
import { loadingBar } from "~/signals/konvaContextuals";
|
||||
|
||||
export const ContextualLoadingBar = () => {
|
||||
useSignals();
|
||||
console.log("ContextualLoadingBar");
|
||||
const props = loadingBar.signal.value;
|
||||
|
||||
return <LoadingBar {...props} />;
|
||||
};
|
||||
@@ -3,11 +3,12 @@ import { Transition } from "@headlessui/react";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
import { ToolbarImage } from "../ToolbarImage";
|
||||
import { imageToolbar } from "~/signals/konva";
|
||||
import { imageToolbar } from "~/signals/konvaContextuals";
|
||||
import { transitionTimingStyles } from "~/components/styles";
|
||||
export const ContextualToolbarImage = () => {
|
||||
useSignals();
|
||||
console.log("ContextualToolbarImage");
|
||||
|
||||
const { position, isShowing } = imageToolbar.signal.value;
|
||||
|
||||
return (
|
||||
|
||||
@@ -2,24 +2,21 @@ import { Transition } from "@headlessui/react";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
import { faCheck, faArrowRotateLeft } from "@fortawesome/pro-thin-svg-icons";
|
||||
import {
|
||||
faCheck,
|
||||
faArrowRotateLeft,
|
||||
faStop,
|
||||
} from "@fortawesome/pro-thin-svg-icons";
|
||||
|
||||
import { Spinner } from "./Spinner";
|
||||
|
||||
export enum LoadingBarStatus {
|
||||
IDLE = "idle",
|
||||
LOADING = "loading",
|
||||
SUCCESS = "success",
|
||||
ERROR = "error",
|
||||
}
|
||||
|
||||
export const LoadingBar = ({
|
||||
progress = 0,
|
||||
position,
|
||||
status,
|
||||
onRetry,
|
||||
isShowing,
|
||||
message,
|
||||
}: {
|
||||
export type LoadingBarProps = {
|
||||
progress: number;
|
||||
status: LoadingBarStatus;
|
||||
isShowing: boolean;
|
||||
@@ -29,13 +26,22 @@ export const LoadingBar = ({
|
||||
};
|
||||
message?: string;
|
||||
onRetry?: () => void;
|
||||
}) => {
|
||||
};
|
||||
|
||||
export const LoadingBar = ({
|
||||
progress = 0,
|
||||
position,
|
||||
status,
|
||||
onRetry,
|
||||
isShowing,
|
||||
message,
|
||||
}: LoadingBarProps) => {
|
||||
return (
|
||||
<Transition show={isShowing}>
|
||||
<div
|
||||
className={twMerge(
|
||||
// default styles
|
||||
"flex flex-col items-center gap-2",
|
||||
"flex w-full gap-2",
|
||||
// position styles
|
||||
position && `fixed w-96`,
|
||||
// base transition properties
|
||||
@@ -52,13 +58,21 @@ export const LoadingBar = ({
|
||||
top: position?.y,
|
||||
}}
|
||||
>
|
||||
<div className="flex w-full items-center gap-2">
|
||||
<div className="bg-gray-200 h-2.5 flex-grow rounded-full">
|
||||
<div className="relative flex-grow">
|
||||
<div className="h-2.5 w-full rounded-full bg-gray-200">
|
||||
<div
|
||||
className="h-2.5 rounded-full bg-primary-500"
|
||||
style={{ width: `${progress}%` }}
|
||||
/>
|
||||
</div>
|
||||
{message && (
|
||||
<label className="mt-2 block w-full text-center">{message}</label>
|
||||
)}
|
||||
</div>
|
||||
<div className="-mt-1.5">
|
||||
{status === LoadingBarStatus.IDLE && (
|
||||
<FontAwesomeIcon icon={faStop} />
|
||||
)}
|
||||
{status === LoadingBarStatus.LOADING && (
|
||||
<Spinner className="size-5" />
|
||||
)}
|
||||
@@ -71,7 +85,6 @@ export const LoadingBar = ({
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{message && <label>{message}</label>}
|
||||
</div>
|
||||
</Transition>
|
||||
);
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useRef } from "react";
|
||||
export const useRenderCounter = (componentName: string) => {
|
||||
const renderCount = useRef(0);
|
||||
renderCount.current++;
|
||||
if (renderCount.current === 1) {
|
||||
if (renderCount.current === 1 && import.meta.env.DEV) {
|
||||
console.log(`${componentName} rerendered ${renderCount.current} times`);
|
||||
} else {
|
||||
console.warn(`${componentName} rerendered ${renderCount.current} times`);
|
||||
|
||||
+146
-52
@@ -1,66 +1,160 @@
|
||||
import { useRef } from "react";
|
||||
import { LoadingBar, LoadingBarStatus } from "~/components/ui";
|
||||
import { useSignals } from "@preact/signals-react/runtime";
|
||||
|
||||
import { ContextualToolbarImage } from "~/components/features/KonvaContainer/ContextualToolbarImage";
|
||||
import { ContextualLoadingBar } from "~/components/features/KonvaContainer/ContextualLoadingBar";
|
||||
|
||||
import { imageToolbar } from "~/signals/konva";
|
||||
import { LoadingBarStatus } from "~/components/ui/LoadingBar";
|
||||
|
||||
import {
|
||||
imageToolbar,
|
||||
loadingBar,
|
||||
ContextualLoadingBarProps,
|
||||
} from "~/signals/konvaContextuals";
|
||||
|
||||
export const Sandbox = () => {
|
||||
const { setPosition, hide } = imageToolbar;
|
||||
const xRef = useRef(0);
|
||||
const yRef = useRef(0);
|
||||
const xImageToolbarRef = useRef(0);
|
||||
const yImageToolbarRef = useRef(0);
|
||||
|
||||
const loadingBarPropsRef = useRef<ContextualLoadingBarProps>({
|
||||
position: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
},
|
||||
progress: 0,
|
||||
status: LoadingBarStatus.IDLE,
|
||||
isShowing: true,
|
||||
message: "Loading...",
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Sandbox</h1>
|
||||
<LoadingBar
|
||||
progress={50}
|
||||
status={LoadingBarStatus.LOADING}
|
||||
isShowing={true}
|
||||
message="Loading..."
|
||||
/>
|
||||
|
||||
<LoadingBar
|
||||
progress={50}
|
||||
position={{ x: 300, y: 200 }}
|
||||
status={LoadingBarStatus.LOADING}
|
||||
isShowing={true}
|
||||
message="Loading..."
|
||||
/>
|
||||
|
||||
<div className="p-2">
|
||||
<ContextualToolbarImage />
|
||||
<ContextualLoadingBar />
|
||||
<h1>Sandbox</h1>
|
||||
<div className="flex flex-col gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<label>Image Toolbar Position</label>
|
||||
<input
|
||||
className="w-20 rounded-md border border-black p-2"
|
||||
type="text"
|
||||
placeholder="X"
|
||||
onChange={(e) => {
|
||||
xImageToolbarRef.current = parseInt(e.target.value);
|
||||
}}
|
||||
/>
|
||||
<input
|
||||
className="w-20 rounded-md border border-black p-2"
|
||||
type="text"
|
||||
placeholder="Y"
|
||||
onChange={(e) => {
|
||||
yImageToolbarRef.current = parseInt(e.target.value);
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
className="rounded-md border border-black bg-gray-200 p-2 hover:bg-gray-300"
|
||||
onClick={() =>
|
||||
imageToolbar.setPosition({
|
||||
x: xImageToolbarRef.current,
|
||||
y: yImageToolbarRef.current,
|
||||
})
|
||||
}
|
||||
>
|
||||
Set Position
|
||||
</button>
|
||||
<button
|
||||
className="rounded-md border border-black bg-gray-200 p-2 hover:bg-gray-300"
|
||||
onClick={() => imageToolbar.hide()}
|
||||
>
|
||||
Hide
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<label>Image Toolbar Position</label>
|
||||
<input
|
||||
className="w-20 rounded-md border border-black p-2"
|
||||
type="text"
|
||||
placeholder="X"
|
||||
onChange={(e) => {
|
||||
xRef.current = parseInt(e.target.value);
|
||||
}}
|
||||
/>
|
||||
<input
|
||||
className="w-20 rounded-md border border-black p-2"
|
||||
type="text"
|
||||
placeholder="Y"
|
||||
onChange={(e) => {
|
||||
yRef.current = parseInt(e.target.value);
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
className="rounded-md border border-black bg-gray-200 p-2 hover:bg-gray-300"
|
||||
onClick={() => setPosition({ x: xRef.current, y: yRef.current })}
|
||||
>
|
||||
Set Position
|
||||
</button>
|
||||
<button
|
||||
className="rounded-md border border-black bg-gray-200 p-2 hover:bg-gray-300"
|
||||
onClick={() => hide()}
|
||||
>
|
||||
Hide
|
||||
</button>
|
||||
<div className="flex flex-col">
|
||||
<div className="flex items-center gap-2">
|
||||
<label>Loading Bar Props</label>
|
||||
<input
|
||||
className="w-20 rounded-md border border-black p-2"
|
||||
type="text"
|
||||
placeholder="X"
|
||||
onChange={(e) => {
|
||||
loadingBarPropsRef.current.position.x = parseInt(
|
||||
e.target.value,
|
||||
);
|
||||
}}
|
||||
/>
|
||||
<input
|
||||
className="w-20 rounded-md border border-black p-2"
|
||||
type="text"
|
||||
placeholder="Y"
|
||||
onChange={(e) => {
|
||||
loadingBarPropsRef.current.position.y = parseInt(
|
||||
e.target.value,
|
||||
);
|
||||
}}
|
||||
/>
|
||||
<input
|
||||
className="w-20 rounded-md border border-black p-2"
|
||||
type="number"
|
||||
min={0}
|
||||
max={100}
|
||||
placeholder="%"
|
||||
onChange={(e) => {
|
||||
loadingBarPropsRef.current.progress = parseInt(e.target.value);
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
className="rounded-md border border-black bg-gray-200 p-2 hover:bg-gray-300"
|
||||
onClick={() =>
|
||||
loadingBar.show({
|
||||
status: LoadingBarStatus.LOADING,
|
||||
progress: loadingBarPropsRef.current.progress,
|
||||
message: "Test Message...",
|
||||
position: {
|
||||
x: loadingBarPropsRef.current.position.x,
|
||||
y: loadingBarPropsRef.current.position.y,
|
||||
},
|
||||
})
|
||||
}
|
||||
>
|
||||
Show
|
||||
</button>
|
||||
<button
|
||||
className="rounded-md border border-black bg-gray-200 p-2 hover:bg-gray-300"
|
||||
onClick={() =>
|
||||
loadingBar.update({
|
||||
status: LoadingBarStatus.LOADING,
|
||||
progress: loadingBarPropsRef.current.progress,
|
||||
message: loadingBarPropsRef.current.message,
|
||||
position: {
|
||||
x: loadingBarPropsRef.current.position.x,
|
||||
y: loadingBarPropsRef.current.position.y,
|
||||
},
|
||||
})
|
||||
}
|
||||
>
|
||||
Update
|
||||
</button>
|
||||
<button
|
||||
className="rounded-md border border-black bg-gray-200 p-2 hover:bg-gray-300"
|
||||
onClick={() => loadingBar.hide()}
|
||||
>
|
||||
Hide
|
||||
</button>
|
||||
</div>
|
||||
<PrintLoadingBarProps />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const PrintLoadingBarProps = () => {
|
||||
useSignals();
|
||||
return (
|
||||
<>
|
||||
<p>Status: {loadingBar.signal.value.status}</p>
|
||||
<p>Message: {loadingBar.signal.value.message}</p>
|
||||
</>
|
||||
);
|
||||
};
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from "./authentication";
|
||||
export * from "./layout";
|
||||
export * from "./konvaContextuals";
|
||||
@@ -1,14 +1,7 @@
|
||||
import { signal } from "@preact/signals-core";
|
||||
import { ContextualUi } from "./type";
|
||||
|
||||
export type ContextualToolbar = {
|
||||
position: {
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
isShowing: boolean;
|
||||
};
|
||||
|
||||
const imageToolbarSignal = signal<ContextualToolbar>({
|
||||
const imageToolbarSignal = signal<ContextualUi>({
|
||||
position: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
@@ -18,7 +11,7 @@ const imageToolbarSignal = signal<ContextualToolbar>({
|
||||
|
||||
export const imageToolbar = {
|
||||
signal: imageToolbarSignal,
|
||||
setPosition(position: ContextualToolbar["position"]) {
|
||||
setPosition(position: ContextualUi["position"]) {
|
||||
imageToolbarSignal.value = {
|
||||
position,
|
||||
isShowing: true,
|
||||
@@ -0,0 +1,5 @@
|
||||
import { ContextualUi, ContextualLoadingBarProps } from "./type";
|
||||
|
||||
export type { ContextualUi, ContextualLoadingBarProps };
|
||||
export * from "./imageToolbar";
|
||||
export * from "./loadingBar";
|
||||
@@ -0,0 +1,70 @@
|
||||
import { signal } from "@preact/signals-core";
|
||||
import { ContextualLoadingBarProps } from "./type";
|
||||
import { LoadingBarStatus } from "~/components/ui/LoadingBar";
|
||||
|
||||
const loadingBarSignal = signal<ContextualLoadingBarProps>({
|
||||
position: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
},
|
||||
isShowing: false,
|
||||
progress: 0,
|
||||
status: LoadingBarStatus.IDLE,
|
||||
message: undefined,
|
||||
onRetry: undefined,
|
||||
});
|
||||
|
||||
export const loadingBar = {
|
||||
signal: loadingBarSignal,
|
||||
update(props: Omit<ContextualLoadingBarProps, "isShowing">) {
|
||||
loadingBarSignal.value = {
|
||||
...loadingBarSignal.value,
|
||||
...props,
|
||||
};
|
||||
},
|
||||
updateMessage(message: string) {
|
||||
loadingBarSignal.value = {
|
||||
...loadingBarSignal.value,
|
||||
message,
|
||||
};
|
||||
},
|
||||
updatePosition(position: { x: number; y: number }) {
|
||||
loadingBarSignal.value = {
|
||||
...loadingBarSignal.value,
|
||||
position,
|
||||
};
|
||||
},
|
||||
updateProgress(progress: number) {
|
||||
loadingBarSignal.value = {
|
||||
...loadingBarSignal.value,
|
||||
progress,
|
||||
};
|
||||
},
|
||||
updateStatus(status: LoadingBarStatus) {
|
||||
loadingBarSignal.value = {
|
||||
...loadingBarSignal.value,
|
||||
status,
|
||||
};
|
||||
},
|
||||
show(props: Omit<ContextualLoadingBarProps, "isShowing">) {
|
||||
if (loadingBarSignal.value.isShowing) {
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn(
|
||||
"Loading bar is already showing",
|
||||
"use the `update` methods instead",
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
loadingBarSignal.value = {
|
||||
...props,
|
||||
isShowing: true,
|
||||
};
|
||||
},
|
||||
hide() {
|
||||
loadingBarSignal.value = {
|
||||
...loadingBarSignal.value,
|
||||
isShowing: false,
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
import { LoadingBarStatus } from "~/components/ui";
|
||||
export { LoadingBarStatus };
|
||||
|
||||
export interface ContextualUi {
|
||||
position: {
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
isShowing: boolean;
|
||||
}
|
||||
|
||||
export interface ContextualLoadingBarProps extends ContextualUi {
|
||||
progress: number;
|
||||
status: LoadingBarStatus;
|
||||
message?: string;
|
||||
onRetry?: () => void;
|
||||
}
|
||||
Reference in new issue
Block a user