Repository navigation
Bug-fix release. No public API removals.
Fixed
beforeunloadno longer prompts when the form is clean. Previously every page with a guard showed the browser's "Leave site?" dialog on refresh or close.- Clean forms no longer intercept link clicks. When the form is dirty and the user confirms, the click is replayed instead of forcing
window.location.href, so SPA routers (Next.js<Link>, React Router, Inertia) keep navigating client-side. The original click is stopped so routers can't navigate while the prompt is open. - Ctrl/Cmd/Shift/Alt-click, middle-click,
<a download>,targetother than_self,#links and in-page anchors on the current URL are no longer intercepted. - No second native dialog after the user already confirmed leaving in the modal. The guard stays quiet until the next edit.
- Back/forward (
popstate) does nothing while the form is clean. When dirty it restores the URL where editing happened, and after confirmation goes back one step (wasgo(-2), which skipped an entry). - Several guards on one page share one set of global listeners and show one prompt per navigation. Every dirty guard emits
leave. - Concurrent
confirmLeave()calls share one prompt; the modal never stacks, and body scroll is restored correctly. reset()emitscleanonly when the form was actually dirty.addForm()keeps unsaved changes in forms that were already guarded (it used to re-snapshot everything).removeForm()recomputes dirty state and disconnects the removed form's MutationObserver and submit listener.- Dynamically removed fields leave the snapshot, so deleting the only changed field makes the form clean again.
- A native form reset (
<button type="reset">,form.reset()) now returns the guard to clean. ignoreHiddenFieldsnow also skips fields inside[hidden],display:noneorvisibility:hiddenancestors.- Under a strict CSP, the injected modal
<style>reuses the nonce of an existing<script nonce>/<style nonce>on the page. - An invalid submit is no longer treated as a save. With
novalidate(or JS-handled forms),submitfires even when a required field is empty or a value is invalid. The guard now resets only if every field passes HTML constraint validation, so unsaved data stays protected. Validity is read without firinginvalidevents. detectContentEditablenow matches barecontenteditableandcontenteditable="plaintext-only", not only="true".contenteditable="false"is still skipped.- The modal now actually receives keyboard focus.
visibilitywas part of the fade-in, so the dialog was still hidden when it tried to focus "Stay"; browsers refuse to focus hidden elements. Focus stayed behind the modal and the Tab trap didn't work. Found by the Playwright tests. - Clicking an in-page
#anchorwhile dirty no longer triggers the prompt (anchors firepopstate, which was treated like the back button). - The modal answers as soon as the user clicks; the fade-out no longer delays navigation, and a prompt opened during the fade gets a fresh modal instead of reusing the closing one (a quick second Back press used to slip through).
- "Leave" button contrast now meets WCAG AA. White on
#ef4444was 3.76:1; the default is now#dc2626(4.83:1), hover#b91c1c. Found by the axe-core audit. - TypeScript types for CommonJS consumers.
require()users got ESM-flavoured types ("masquerading as ESM"); there are now.d.ctstwins and per-conditiontypesinexports. Subpath types (/react,/vue) also resolve under legacymoduleResolution: nodeviatypesVersions. sideEffectsno longer lets bundlers drop CSS imports. It's now["*.css"]instead offalse.src/styles.cssnow matches the modal. It used different class and variable names (.fg-modal-wrapper,--fg-duration…), so importing it styled nothing. It's now generated from the injected CSS, and a test keeps them in sync.enable()afterdisable()emitscleanif the form was dirty, keeping the VueisDirtyref in sync.
Changed
- Stylesheet ships as
dist/styles.css, importable as@oblique-code/form-guard/styles.css. The 1.0.0 path@oblique-code/form-guard/src/styles.csskeeps working (it now points at the same file). - The npm package no longer includes raw
src/*.js(source maps still embed the sources).package.jsonis exported. - A disabled guard never prompts:
confirmLeave()resolvestruewhile disabled.
Added
- Real-browser end-to-end tests with Playwright in Chromium, Firefox and WebKit (
npm run test:e2e): nativebeforeunload, modal accessibility and keyboard, links, SPA routing, back/forward, every field type, validation, dark mode, multiple guards and CSP. They run in CI as a job per browser. - Automated accessibility audit with axe-core (WCAG 2.0–2.2 A/AA) of the modal in light and dark mode, including hover/focus states, in all three browsers.
npm run check:package: publint, arethetypeswrong and an install smoke test of the packed tarball (ESM, CJS,/react,/vue,/styles.css, types). It runs in CI and beforenpm publish.npm run serve: a dependency-free static server to try the examples athttp://localhost:4173/examples/vanilla.html.- Test suite (Vitest + jsdom): core, snapshot, events, router, modal, React and Vue adapters, plus the shipped examples (vanilla against the built IIFE bundle).
npm test. npm run test:coverage,npm run size(with a 6 KB gzip budget) andnpm run release:zip.- Versioned release ZIP + SHA-256 checksum (
releases/form-guard-vX.Y.Z.zip). - GitHub Actions: CI on every push/PR, and a tag-triggered release that attaches the ZIP.
- Docs: TESTING, RELEASING, ROADMAP.
Docs
- README: new "React & Vue Adapters: What to Know" section (behaviour, current limitations, exactly what is and isn't tested), stylesheet/CDN usage, TypeScript module-format support, tested browsers.
- README: measured bundle sizes; corrected React, Vue Router and Next.js Pages Router examples; notes on native reset and CSP.
- Examples: all three add a required field that starts empty (Company) and validate on save. The vanilla demo adds every other field type (number, date, radio group, range, color, file, multi-select).
- Examples: React uses
onDirty/onCleanstate (the oldisDirty()in render never re-rendered); Vue usesonBeforeRouteLeaveinstead of a leaking globalrouter.beforeEach; vanilla uses a native "Discard changes" reset and shows an in-page anchor.
Removed
.npmignore(redundant —package.json#filesalready controls what is published).