Repository navigation
Visual review fixes: touch affordances, cart overlay, and merchant-set social links #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3d5a7ac
Let an editorial collection card be opened by touch
hk1682 f34f588
Give the product grids a tablet step, and the search page its filter row
hk1682 ff4b103
Stop clipping product titles, and give loading images the theme's grey
hk1682 74cd712
Let a touch screen reach the parts of a product page built for a mouse
hk1682 97ec21b
Stop the cart counting a pending line twice
hk1682 cba43ac
Raise form fields to 16px only where that prevents a zoom
hk1682 15cd645
Let a merchant choose the social icons on the bar and in the footer
hk1682 17ab112
Run the cart overlay rules in CI
hk1682 8e446f4
Keep the social links a shop already saved
hk1682 14a724d
Re-take the cart baseline whenever a fetcher submits
hk1682 ae520d9
Open the volume pricing table from the keyboard
hk1682 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { CartForm, useOptimisticCart } from "@shopify/hydrogen"; | ||
| import { useRef } from "react"; | ||
| import { useFetchers } from "react-router"; | ||
| import type { CartApiQueryFragment } from "storefront-api.generated"; | ||
|
|
||
| /** | ||
| * Hydrogen's optimistic cart, dropped once the server has already applied it. | ||
| * | ||
| * `useOptimisticCart` lays every cart fetcher that still carries `formData` | ||
| * over the cart it is given, and React Router keeps `formData` through both | ||
| * `submitting` and `loading` — so the overlay outlives the action that | ||
| * produced it. The cart itself reaches the drawer as a deferred promise, and a | ||
| * revalidated one can resolve while the fetcher is still in `loading`: the | ||
| * line is then counted twice, once by the server and once by the overlay. A | ||
| * quantity of 1 reads as 2 until the fetcher finally goes idle. | ||
| * | ||
| * The overlay is worth keeping only while the cart in hand predates the | ||
| * mutation. That is what this checks, and it drops the overlay wholesale when | ||
| * it cannot: the figures Hydrogen computed are inside a `structuredClone` it | ||
| * owns, so there is no way to take one fetcher's contribution back out without | ||
| * reimplementing its matching rules. | ||
| * | ||
| * The cost is a brief undercount when two cart fetchers overlap — one's | ||
| * landing invalidates the other's overlay early. It corrects itself the moment | ||
| * that one lands too, and showing one too few for an instant is a smaller lie | ||
| * than showing one too many. | ||
| */ | ||
| export function useSettledOptimisticCart( | ||
| originalCart: CartApiQueryFragment | null, | ||
| ) { | ||
| // Unconditionally: hooks cannot be skipped, so the overlay is always built | ||
| // and only the decision to use it varies. | ||
| const optimisticCart = useOptimisticCart<CartApiQueryFragment>(originalCart); | ||
| const fetchers = useFetchers(); | ||
| const baselines = useRef(new Map<string, string>()); | ||
|
|
||
| const stale = settlementStale(baselines.current, fetchers, originalCart); | ||
|
|
||
| return stale ? (originalCart as typeof optimisticCart) : optimisticCart; | ||
| } | ||
|
|
||
| /** | ||
| * One transition of the per-fetcher baselines against the cart now on screen. | ||
| * | ||
| * Takes the baselines as an argument — and writes to them — so scripted event | ||
| * sequences can drive it in tests the way the hook's ref does across renders. | ||
| */ | ||
| export function settlementStale( | ||
| baselines: Map<string, string>, | ||
| fetchers: Array<{ | ||
| key: string; | ||
| state: "submitting" | "loading" | "idle"; | ||
| formData?: FormData | null; | ||
| }>, | ||
| cart: CartApiQueryFragment | null, | ||
| ): boolean { | ||
| const fingerprint = cartFingerprint(cart); | ||
| const live = new Set<string>(); | ||
| let stale = false; | ||
|
|
||
| for (const fetcher of fetchers) { | ||
| if (!(fetcher.formData && isCartMutation(fetcher.formData))) { | ||
| continue; | ||
| } | ||
| live.add(fetcher.key); | ||
|
|
||
| if (fetcher.state === "submitting") { | ||
| // React Router revalidates loaders only once the action has returned, | ||
| // so a cart read during `submitting` cannot contain this submission. | ||
| // That makes the baseline sound by invariant rather than by whatever | ||
| // moment this happened to render on. | ||
| // | ||
| // Re-taken every time, not only on a key first seen: a keyed fetcher | ||
| // submitted again before it goes idle — a double-tapped Add to cart, a | ||
| // stepper keyed by line id — runs `loading` straight into `submitting`. | ||
| // Keeping the first baseline would compare the second submission | ||
| // against a cart the first one has already changed, and its overlay | ||
| // would be dropped for the whole of its flight. | ||
| baselines.set(fetcher.key, fingerprint); | ||
| continue; | ||
| } | ||
|
|
||
| if (!baselines.has(fetcher.key)) { | ||
| // First seen mid-flight — the drawer mounts when it opens, which may be | ||
| // after the action already returned. Nothing here establishes that the | ||
| // cart predates the mutation, so the overlay cannot be trusted. | ||
| stale = true; | ||
| continue; | ||
| } | ||
|
|
||
| if (baselines.get(fetcher.key) !== fingerprint) { | ||
| stale = true; | ||
| } | ||
| } | ||
|
|
||
| for (const key of [...baselines.keys()]) { | ||
| if (!live.has(key)) { | ||
| baselines.delete(key); | ||
| } | ||
| } | ||
|
|
||
| return stale; | ||
| } | ||
|
|
||
| const CART_MUTATIONS = new Set<string>([ | ||
| CartForm.ACTIONS.LinesAdd, | ||
| CartForm.ACTIONS.LinesRemove, | ||
| CartForm.ACTIONS.LinesUpdate, | ||
| ]); | ||
|
|
||
| function isCartMutation(formData: FormData) { | ||
| try { | ||
| const { action } = CartForm.getFormInput(formData); | ||
| return typeof action === "string" && CART_MUTATIONS.has(action); | ||
| } catch { | ||
| // A fetcher submitting something that is not a cart form at all. | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * What the server's cart holds, as one comparable string. | ||
| * | ||
| * `updatedAt` looks like the obvious marker and is not: Shopify's `DateTime` | ||
| * is second-resolution, so a removal and the add that follows it land on the | ||
| * same stamp — exactly the sequence that shows this bug. Line quantities move | ||
| * whenever the cart does, whatever the clock says. | ||
| */ | ||
| function cartFingerprint(cart: CartApiQueryFragment | null) { | ||
| const lines = cart?.lines?.nodes ?? []; | ||
| return lines | ||
| .map((line) => `${line.merchandise?.id ?? line.id}:${line.quantity}`) | ||
| .sort() | ||
| .join("|"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The baseline is only taken the first time a key is seen. A keyed fetcher re-submitted while it is still
loading(double-tapping Add to cart, or the line stepper keyed by line id) goesloading→submittingwith no idle render in between. The old baseline therefore survives. If the first mutation's cart has already landed, the second submission compares against the stale fingerprint and its overlay is dropped for its whole flight, so the badge and totals don't move until the server answers. The PR's own invariant covers the fix: a cart read duringsubmittingcan't contain this submission, so re-take the baseline on everysubmittingrender:A scripted case in
optimistic-cart.test.mjswould be worth adding: submitting → loading (server landed) → submitting again on the same key.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied as written,
14a724de. The invariant does carry it: a cart read duringsubmittingcan't contain that submission, first on its key or not.Test added in the same commit — submitting → landed → submitting again on the same key → in flight → landed, all through one
playso each step measures against the baselines the previous ones left. 7/7.