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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
feat(exercises): a × clears the Library and exercise-picker search
Boris 'B' Kurktchiev0dd43aaeaa3f
diff --git a/frontend/src/index.css b/frontend/src/index.cssindex a167187e5..6539a3eb9 100644--- a/frontend/src/index.css+++ b/frontend/src/index.css@@ -604,6 +604,17 @@ body.chips-dragging *{cursor:inherit}background:var(--surface-3);padding:2px 8px;border-radius:99px;font-variant-numeric:tabular-nums;pointer-events:none;user-select:none;}+/* The × that empties the search (Library and the exercise picker), the same round button as+ SearchField's, at the end of the field; the count moves in beside it while both show. */+.search .clear{+ position:absolute;inset-inline-end:8px;top:50%;transform:translateY(-50%);z-index:1;+ width:22px;height:22px;border-radius:50%;+ background:var(--label-3);color:var(--bg);display:flex;align-items:center;+ justify-content:center;font-size:11px;--icon-stroke:3;+}+.search.has-clear .input{padding-inline-end:36px}+.search.has-count.has-clear .input{padding-inline-end:90px}+.search.has-clear .search-count{inset-inline-end:36px}/* --- switch --- */.sw{diff --git a/frontend/src/search-clear.test.jsx b/frontend/src/search-clear.test.jsxnew file mode 100644index 000000000..e09ca3876--- /dev/null+++ b/frontend/src/search-clear.test.jsx@@ -0,0 +1,72 @@+// @vitest-environment happy-dom+// The × in the exercise search (Library and the exercise picker), beside the live result+// count (search-count.test.jsx). It shows while there is text, one tap empties the search, and it+// clears only the text: a body-part filter that also narrows the list keeps its count.+import React, { act } from 'react'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { createRoot } from 'react-dom/client'+import { MemoryRouter } from 'react-router-dom'+import { DEF, useStore } from './store/useStore.js'+import { useUI } from './store/useUI.js'+import { exercisePicker } from './sheets.jsx'+import Library from './views/Library.jsx'+import { EXDB } from './lib/exercises.js'++globalThis.IS_REACT_ACT_ENVIRONMENT = true+const mounted = []+function mount(node) {+ const host = document.createElement('div')+ document.body.appendChild(host)+ const root = createRoot(host)+ mounted.push(root)+ act(() => root.render(node))+ return host+}+const renderTopSheet = () => {+ const sheet = useUI.getState().sheets.at(-1)+ return mount(sheet.render(() => useUI.getState().closeSheet(sheet.id)))+}+function type(el, value) {+ Object.getOwnPropertyDescriptor(el.constructor.prototype, 'value').set.call(el, value)+ el.dispatchEvent(new Event('input', { bubbles: true }))+}+const chip = (host, text) => [...host.querySelectorAll('.chips .chip')].find(b => b.textContent.trim() === text)+const clear = host => host.querySelector('.search .clear')+const count = host => host.querySelector('.search .search-count')++beforeEach(() => {+ useUI.setState({ sheets: [], toastMsg: '' })+ useStore.setState({ S: structuredClone(DEF), user: null })+ document.body.innerHTML = ''+})+afterEach(() => { act(() => { mounted.splice(0).forEach(root => root.unmount()) }) })++describe('exercise search clear button', () => {+ for (const [where, open] of [+ ['Library', () => mount(<MemoryRouter><Library /></MemoryRouter>)],+ ['exercise picker', () => { exercisePicker(vi.fn()); return renderTopSheet() }],+ ]) {+ it(`${where}: shows with text, and one tap empties the search`, () => {+ const host = open()+ const input = host.querySelector('.search input')+ expect(clear(host)).toBeNull()+ act(() => type(input, 'bench press'))+ expect(clear(host).getAttribute('aria-label')).toBe('Clear')+ expect(host.querySelector('.search').classList.contains('has-clear')).toBe(true)+ act(() => clear(host).click())+ expect(input.value).toBe('')+ expect(clear(host)).toBeNull()+ expect(count(host)).toBeNull()+ expect(host.querySelector('.search').classList.contains('has-clear')).toBe(false)+ })++ it(`${where}: clears only the text; a body-part filter keeps its count`, () => {+ const host = open()+ act(() => chip(host, 'neck').click())+ act(() => type(host.querySelector('.search input'), 'stretch'))+ act(() => clear(host).click())+ expect(host.querySelector('.search input').value).toBe('')+ expect(Number(count(host).textContent)).toBe(EXDB.filter(e => e.bp === 'neck').length)+ })+ }+})diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsxindex 71020da59..2e5cb2418 100644--- a/frontend/src/sheets.jsx+++ b/frontend/src/sheets.jsx@@ -1123,9 +1123,10 @@ function ExercisePicker({ onPick, title, close }) {</div>{/* .picker-search is what index.css keys the keyboard-aware sheet layout on: the sheetlifts above the keys and the search stays put while the list scrolls under it. */}- <div className="picker-search"><div className={'search' + (narrowed ? ' has-count' : '')}><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg>+ <div className="picker-search"><div className={'search' + (narrowed ? ' has-count' : '') + (q ? ' has-clear' : '')}><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg><input ref={searchRef} className="input" placeholder={t('Search {0} exercises…', all.length)} value={q} onFocus={onSearchFocus} onChange={e => { setQ(e.target.value); setShown(50) }} />- {narrowed && <span className="search-count" role="status" aria-label={exCount(f.length)}>{fmtNum(f.length)}</span>}</div></div>+ {narrowed && <span className="search-count" role="status" aria-label={exCount(f.length)}>{fmtNum(f.length)}</span>}+ {q && <button className="clear" onClick={() => { setQ(''); setShown(50) }} aria-label={t('Clear')}><Icon name="xmark" /></button>}</div></div>{profile && <div className="small dim row" style={{ margin: '8px 0 2px', gap: 6, alignItems: 'center' }}><Icon name="dumbbell" style={{ fontSize: 13 }} />{showAll ? t('Showing all equipment') : t('Showing what you have in "{0}"', profile.name)}diff --git a/frontend/src/views/Library.jsx b/frontend/src/views/Library.jsxindex d6608895a..379768123 100644--- a/frontend/src/views/Library.jsx+++ b/frontend/src/views/Library.jsx@@ -41,9 +41,10 @@ export default function Library() {<div className="hdr"><div><h1>{t('Exercises')}</h1><div className="sub">{t('{0} exercises with animations', EXDB.length)}</div></div><Button size="sm" variant="tinted" icon="target" onClick={() => nav('/muscles')}>{t('By muscle')}</Button></div>- <div className={'search' + (narrowed ? ' has-count' : '')} style={{ marginBottom: 10 }}><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg>+ <div className={'search' + (narrowed ? ' has-count' : '') + (q ? ' has-clear' : '')} style={{ marginBottom: 10 }}><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg><input className="input" placeholder={t('Search…')} value={q} onChange={e => { setQ(e.target.value); setShown(40) }} />- {narrowed && <span className="search-count" role="status" aria-label={exCount(f.length)}>{fmtNum(f.length)}</span>}</div>+ {narrowed && <span className="search-count" role="status" aria-label={exCount(f.length)}>{fmtNum(f.length)}</span>}+ {q && <button className="clear" onClick={() => { setQ(''); setShown(40) }} aria-label={t('Clear')}><Icon name="xmark" /></button>}</div>{profile && <div className="small dim row" style={{ margin: '-4px 2px 10px', gap: 6, alignItems: 'center' }}><Icon name="dumbbell" style={{ fontSize: 13 }} />{showAll ? t('Showing all equipment') : t('Showing what you have in "{0}"', profile.name)}