commit 277525602b8831f036ab34fe96ea64f22c292b4c
parent 7d8483255847c5f492d902db45d82cb80e74ced8
Author: Sebastian <sebasjm@taler-systems.com>
Date: Mon, 1 Jun 2026 11:18:41 -0300
only one button type for all spas
Diffstat:
2 files changed, 136 insertions(+), 154 deletions(-)
diff --git a/packages/web-util/src/components/Attention.tsx b/packages/web-util/src/components/Attention.tsx
@@ -26,7 +26,7 @@ export function Attention({
const divHtml = useRef<HTMLDivElement>();
if (type == "error") type = "danger";
return (
- <div class={`group attention-${type} mt-2 shadow-lg`}>
+ <div class={`z-100 group attention-${type} mt-2 shadow-lg`}>
{/* {timeout.d_ms === "forever" ? undefined : <style>{`
.progress {
animation: notificationTimeoutBar ${Math.round(timeout.d_ms / 1000)}s ease-in-out;
@@ -52,7 +52,8 @@ export function Attention({
stroke="none"
viewBox="0 0 24 24"
fill="currentColor"
- class="w-8 h-8 group-[.attention-info]:text-blue-400 group-[.attention-warning]:text-yellow-400 group-[.attention-danger]:text-red-400 group-[.attention-success]:text-green-400"
+ style={{width:24, height: 24}}
+ // class="w-8 h-8 group-[.attention-info]:text-blue-400 group-[.attention-warning]:text-yellow-400 group-[.attention-danger]:text-red-400 group-[.attention-success]:text-green-400"
>
{(() => {
switch (type) {
diff --git a/packages/web-util/src/components/Button.tsx b/packages/web-util/src/components/Button.tsx
@@ -14,21 +14,31 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
+import { assertUnreachable } from "@gnu-taler/taler-util";
import { Fragment, VNode, h } from "preact";
-import { HTMLAttributes, useState } from "preact/compat";
import {
- SafeHandlerTemplate,
- useTranslationContext,
-} from "../index.browser.js";
+ CSSProperties,
+ HTMLAttributes,
+ useEffect,
+ useState,
+} from "preact/compat";
+import { SafeHandler, useNotificationContext } from "../index.browser.js";
import { doAutoFocus } from "./utils.js";
-export interface ButtonHandler {
- onClick: (() => Promise<void>) | undefined;
-}
+type Props = Omit<
+ Omit<HTMLAttributes<HTMLButtonElement>, "type">,
+ "onClick"
+> & {
+ submit?: boolean;
+ onClick: SafeHandler<any, any> | undefined;
+ focus?: boolean;
+};
-interface Props extends HTMLAttributes<HTMLButtonElement> {
- handler: ButtonHandler | undefined;
-}
+/**
+ * we should have a button-type and a submit-type
+ * submit tpye should not have focus sin the focus in the form
+ * submit should only be used on forms and there should be only one
+ */
/**
* This button accept an async function and report a notification
@@ -37,180 +47,151 @@ interface Props extends HTMLAttributes<HTMLButtonElement> {
* When the async function is running the inner text will change into
* a "loading" animation.
*
- * @deprecated use ButtonBetter
- *
* @param param0
* @returns
*/
export function Button({
- handler,
- children,
- disabled,
- onClick: clickEvent,
- ...rest
-}: Props): VNode {
- const { i18n } = useTranslationContext();
- const [running, setRunning] = useState(false);
- return (
- <button
- {...rest}
- disabled={disabled || running}
- onClick={(e) => {
- e.preventDefault();
- if (!handler || !handler.onClick) {
- return;
- }
- setRunning(true);
- handler.onClick().finally(() => {
- setRunning(false);
- });
- }}
- >
- {running ? <Wait /> : children}
- </button>
- );
-}
-
-type PropsBetter = Omit<
- Omit<HTMLAttributes<HTMLButtonElement>, "type">,
- "onClick"
-> & {
- submit?: boolean;
- onClick: SafeHandlerTemplate<any, any> | undefined;
- focus?: boolean;
-};
-/**
- * FIXME: removed deprecated and change for this one
- * @param param0
- * @returns
- */
-export function ButtonBetter({
children,
focus,
onClick,
disabled,
submit,
...rest
-}: PropsBetter): VNode {
- const [running, setRunning] = useState(false);
- return (
- <button
- {...rest}
- disabled={running || !onClick || !onClick.args || disabled}
- ref={focus ? doAutoFocus : undefined}
- type={submit ? "submit" : "button"}
- onClick={(e) => {
- e.preventDefault();
- if (!onClick || !onClick.args) {
- return;
- }
- setRunning(true);
- onClick.call().finally(() => {
- setRunning(false);
- });
- }}
- >
- {running ? <Wait /> : children}
- </button>
- );
-}
-/**
- * we should have a button-type and a submit-type
- * submit tpye should not have focus sin the focus in the form
- * submit should only be used on forms and there should be only one
- */
-// FIXME: we should stop using bulma css and remove all of this support
-export function ButtonBetterBulma({
- children,
- focus,
- disabled,
- onClick,
- ...rest
-}: PropsBetter & { "data-tooltip"?: string }): VNode {
+}: Props): VNode {
const [running, setRunning] = useState(false);
- if (onClick) {
- {
- const prev = onClick.onStart;
- onClick.onStart = () => {
- setRunning(true);
- prev();
- };
- }
- {
- const prev = onClick.onSuccess;
- onClick.onSuccess = (...args) => {
- setRunning(false);
- return prev(...args);
- };
+ const [failed, setFailed] = useState(false);
+ const { notification: ns } = useNotificationContext();
+
+ const notification = ns.length > 0 ? ns[0] : undefined;
+ // if the button is in failed state and the user
+ // change the state of the form that affect this action handler
+ // the remove the failed state for faster submit
+ useEffect(() => {
+ if (failed) {
+ notification?.acknowledge();
+ setFailed(false);
}
- {
- const prev = onClick.onFail;
- onClick.onFail = (...args) => {
- setRunning(false);
- return prev(...args);
- };
+ }, onClick?.args ?? []);
+
+ onClick?.addListener((e) => {
+ switch (e) {
+ case "start":
+ return setRunning(true);
+ case "fail":
+ return setFailed(true);
+ case "success":
+ return setFailed(false);
+ case "finish":
+ return setRunning(false);
+ default: {
+ assertUnreachable(e);
+ }
}
- }
+ });
+
return (
<button
- class="button is-success"
{...rest}
- ref={focus ? doAutoFocus : undefined}
disabled={running || !onClick || !onClick.args || disabled}
+ ref={focus ? doAutoFocus : undefined}
+ type={submit ? "submit" : "button"}
+ data-pepe="si"
+ data-failed={failed ? "true" : undefined}
onClick={(e) => {
e.preventDefault();
- if (!onClick || !onClick.args) {
- return;
+ if (failed) {
+ setFailed(false);
+ notification?.acknowledge();
+ } else {
+ onClick?.call();
}
- onClick.call();
}}
>
- {running ? <Spinner /> : children}
+ <div style={{ position: "relative" }}>
+ <span style={{ visibility: running || failed ? "hidden" : undefined }}>
+ {children}
+ </span>
+ {running ? <Wait /> : failed ? <Failed /> : undefined}
+ </div>
</button>
);
}
-function Spinner(): VNode {
+function Failed(): VNode {
return (
- <div
- class="columns is-centered is-vcentered"
- style={{
- width: 80,
- height: 40,
- }}
- >
- <div class="lds-ring small">
- <div />
- <div />
- <div />
- <div />
- </div>
+ <div role="status" style={{ ...centerInTheMiddle, ...tailwind_size24 }}>
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ fill="none"
+ viewBox="0 0 24 24"
+ stroke-width="1.5"
+ stroke="currentColor"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
+ />
+ </svg>
+ <span style={tailwind_srOnly}>Failed</span>
</div>
);
}
+//////
+// tailwind helper in domCssProps since this needs to work
+// with tailwind and bulma
+//////
+
+// const tailwind_textNeutralTertiary: CSSProperties = {};
+const tailwind_animateSpin: CSSProperties = {
+ animation: "spin 1s linear infinite",
+};
+// const tailwind_fillBrand: CSSProperties = {};
+const tailwind_size8: CSSProperties = {
+ width: 8,
+ height: 8,
+};
+const tailwind_size24: CSSProperties = {
+ width: 24,
+ height: 24,
+};
+const tailwind_srOnly: CSSProperties = {
+ position: "absolute",
+ width: 1,
+ height: 1,
+ padding: 0,
+ margin: -1,
+ overflow: "hidden",
+ clip: "rect(0, 0, 0, 0)",
+ whiteSpace: "nowrap",
+ border: 0,
+};
+const centerInTheMiddle: CSSProperties = {
+ position: "absolute",
+ top: "50%",
+ left: "50%",
+ transform: "translate(-50%,-50%)",
+};
+const singleLineHeight: CSSProperties = {
+ height: "1lh",
+};
+
function Wait(): VNode {
return (
- <Fragment>
- <div role="status">
- <svg
- aria-hidden="true"
- class="w-8 h-8 text-neutral-tertiary animate-spin fill-brand"
- viewBox="0 0 100 101"
- fill="none"
- xmlns="http://www.w3.org/2000/svg"
- >
- <path
- d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
- fill="currentColor"
- />
- <path
- d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
- fill="currentFill"
- />
- </svg>
- <span class="sr-only">Loading...</span>
- </div>
- </Fragment>
+ <div role="status" style={{ ...centerInTheMiddle, ...tailwind_size24 }}>
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ fill="none"
+ viewBox="0 0 24 24"
+ stroke-width="1.5"
+ stroke="currentColor"
+ style={{ ...singleLineHeight, ...tailwind_animateSpin }}
+ // class="text-neutral-tertiary animate-spin fill-brand "
+ >
+ <path d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99" />
+ </svg>
+ <span style={tailwind_srOnly}>Loading</span>
+ </div>
);
}