Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Undo toast: a × to send it away at once

Duarte Santosa680a6b9db24
diff --git a/frontend/src/components/Toast.jsx b/frontend/src/components/Toast.jsx
index d20bc8dd4..c788acb7b 100644
--- a/frontend/src/components/Toast.jsx
+++ b/frontend/src/components/Toast.jsx
@@ -1,12 +1,14 @@
import { useUI } from '../store/useUI.js'
import Icon from './Icon.jsx'
+import { t } from '../lib/i18n.js'
// The one toast. Mostly text; with an action (the Undo after a set is removed) it carries a button
-// at its end, stays longer and holds still while a finger or the mouse rests on it.
+// at its end, stays longer and holds still while a finger or the mouse rests on it. Its × sends it
+// away at once: five seconds of Undo over the set list is long when you did mean to delete.
export default function Toast() {
const msg = useUI(s => s.toastMsg)
const action = useUI(s => s.toastAction)
- const { pauseToast, resumeToast, runToastAction } = useUI.getState()
+ const { pauseToast, resumeToast, runToastAction, dismissToast } = useUI.getState()
const live = !!(msg && action)
return <div id="toast" role="status" className={(msg ? 'show' : '') + (live ? ' has-action' : '')}
onPointerEnter={live ? pauseToast : undefined} onPointerDown={live ? pauseToast : undefined}
@@ -16,5 +18,8 @@ export default function Toast() {
{live && <button type="button" className="toast-act" key={action.id} onClick={runToastAction}>
<Icon name="undo" />{action.label}
</button>}
+ {live && <button type="button" className="toast-x" aria-label={t('Close')} onClick={dismissToast}>
+ <Icon name="xmark" />
+ </button>}
</div>
}
diff --git a/frontend/src/index.css b/frontend/src/index.css
index 93d4d905e..d7856f4f6 100644
--- a/frontend/src/index.css
+++ b/frontend/src/index.css
@@ -1733,6 +1733,9 @@ body.no-tabbar #timer.working{box-shadow:inset 0 var(--hair) 0 var(--acc),0 -10p
#toast .toast-act{flex:none;display:inline-flex;align-items:center;gap:5px;color:var(--acc);font-weight:700;padding:8px 12px;border-radius:14px;min-height:36px}
#toast .toast-act:active{background:color-mix(in srgb,var(--label) 10%,transparent)}
#toast .toast-act .icn{font-size:16px}
+#toast .toast-x{flex:none;display:grid;place-items:center;width:36px;height:36px;margin-left:-4px;border-radius:50%;color:var(--label-2)}
+#toast .toast-x:active{background:color-mix(in srgb,var(--label) 10%,transparent)}
+#toast .toast-x .icn{font-size:15px}
body.no-tabbar #toast{bottom:calc(24px + var(--sab))}
body.no-tabbar.resting #toast{bottom:calc(100px + var(--sab))}
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index 8b5cbe3b2..c16e82b6e 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -260,6 +260,12 @@ export const useUI = create((set, get) => ({
set({ toastMsg: '', toastAction: null })
act.run()
},
+ // The toast's ×: gone now, its action with it (what it would have undone stays done).
+ dismissToast() {
+ clearTimeout(toastTm)
+ toastPaused = false
+ set({ toastMsg: '', toastAction: null })
+ },
pauseToast() {
if (!get().toastAction || toastPaused) return
toastPaused = true
diff --git a/frontend/src/store/useUI.toast.test.js b/frontend/src/store/useUI.toast.test.js
index 92924f1be..6d96aeeaf 100644
--- a/frontend/src/store/useUI.toast.test.js
+++ b/frontend/src/store/useUI.toast.test.js
@@ -28,6 +28,21 @@ describe('toast with an action', () => {
expect(useUI.getState().toastMsg).toBe('')
})
+ it('the × sends it away at once, without running the action', () => {
+ const onAction = vi.fn()
+ useUI.getState().toast('Set 2 gone.', { action: 'Undo', onAction })
+ useUI.getState().pauseToast()
+ useUI.getState().dismissToast()
+ expect(useUI.getState().toastMsg).toBe('')
+ expect(useUI.getState().toastAction).toBeNull()
+ useUI.getState().resumeToast()
+ useUI.getState().runToastAction()
+ expect(onAction).not.toHaveBeenCalled()
+ useUI.getState().toast('Saved')
+ vi.advanceTimersByTime(2201)
+ expect(useUI.getState().toastMsg).toBe('')
+ })
+
it('times out after 5 s without running the action', () => {
const onAction = vi.fn()
useUI.getState().toast('Set 2 gone.', { action: 'Undo', onAction })