UI/Button: move state out of the button

This commit is contained in:
Johannes Kirschbauer
2025-08-13 14:15:29 +02:00
parent 62ccba9fb5
commit 176b54e29d
2 changed files with 18 additions and 25 deletions

View File

@@ -22,7 +22,7 @@ export interface ButtonProps
startIcon?: IconVariant; startIcon?: IconVariant;
endIcon?: IconVariant; endIcon?: IconVariant;
class?: string; class?: string;
onAction?: Action; loading?: boolean;
} }
const iconSizes: Record<Size, string> = { const iconSizes: Record<Size, string> = {
@@ -40,31 +40,12 @@ export const Button = (props: ButtonProps) => {
"startIcon", "startIcon",
"endIcon", "endIcon",
"class", "class",
"onAction", "loading",
]); ]);
const size = local.size || "default"; const size = local.size || "default";
const hierarchy = local.hierarchy || "primary"; const hierarchy = local.hierarchy || "primary";
const [loading, setLoading] = createSignal(false);
const onClick = async () => {
if (!local.onAction) {
console.error("this should not be possible");
return;
}
setLoading(true);
try {
await local.onAction();
} catch (error) {
console.error("Error while executing action", error);
}
setLoading(false);
};
const iconSize = iconSizes[local.size || "default"]; const iconSize = iconSizes[local.size || "default"];
const loadingClass = const loadingClass =
@@ -81,16 +62,19 @@ export const Button = (props: ButtonProps) => {
hierarchy, hierarchy,
{ {
icon: local.icon, icon: local.icon,
loading: loading(), loading: props.loading,
ghost: local.ghost, ghost: local.ghost,
}, },
)} )}
onClick={local.onAction ? onClick : undefined} onClick={props.onClick}
{...other} {...other}
> >
<Loader <Loader
hierarchy={hierarchy} hierarchy={hierarchy}
class={cx({ [idleClass]: !loading(), [loadingClass]: loading() })} class={cx({
[idleClass]: !props.loading,
[loadingClass]: props.loading,
})}
/> />
{local.startIcon && ( {local.startIcon && (

View File

@@ -99,8 +99,12 @@ const welcome = (props: {
}) => { }) => {
const navigate = useNavigate(); const navigate = useNavigate();
const [loading, setLoading] = createSignal(false);
const selectFolder = async () => { const selectFolder = async () => {
setLoading(true);
const uri = await selectClanFolder(); const uri = await selectClanFolder();
setLoading(false);
navigateToClan(navigate, uri); navigateToClan(navigate, uri);
}; };
@@ -148,7 +152,12 @@ const welcome = (props: {
</Typography> </Typography>
<Divider orientation="horizontal" /> <Divider orientation="horizontal" />
</div> </div>
<Button hierarchy="primary" ghost={true} onAction={selectFolder}> <Button
hierarchy="primary"
ghost={true}
loading={loading()}
onClick={selectFolder}
>
Select folder Select folder
</Button> </Button>
</div> </div>