· 3 min read
Cards that become the page
Clicking a card (blog, Devlog, projects, Awesome, the home page) turns it into its page: the title, picture, date line, description and tags fly to their…
What we did
- Clicking a card (blog, Devlog, projects, Awesome, the home page) turns it into its page: the title, picture, date line, description and tags fly to their places in the page's header, while the other cards drop away and the page body rises in after them. Back, or the breadcrumbs, play it in reverse. Choices from an interview: all five parts move, a tile without a cover fades out, the other cards drop away.
src/lib/morph.ts:cardLinkingTo(url)(the card whose title links there),pageHeader(),mark(),unmarkAll(),picturesReady(). The root layout'sonNavigatemarks one end before the transition and the other inupdate(), and adds the typemorph.- Markup:
data-morph-cardonTeaserCard,data-morph-pageonArticleHeader,ProjectHeaderand an Awesome list's header,data-morph="title|picture|meta|description|tags"on the parts (the article header's date line got a wrapper for it). global.css: names only inside[data-morphing]during amorphtransition,view-transition-class: morphfor their shared timing (450ms),fit-contenttext, the cover cropped to the moving box,route-dropfor the old page.- Fixed for every route change: the old page's picture now stays where it was on screen (
--route-shift).
What I learned
- Two elements with the same
view-transition-name, one before and one after, become one moving box. Names must be unique, so they're set on the clicked card only, at the moment of the click. - The old and new pictures of a group keep their own aspect ratio by default;
object-fit: covercrops them to the box, but only withoverflow: clipon the pictures and their image pair. ::view-transition-old(.morph):only-childstyles a part that has no partner on the other page.- The browser takes the "after" picture when
update()ends, so an image that's still loading flies in empty:img.decode(), raced against a timeout.
Problems & fixes
- Going Back, the old article showed from its middle: its picture is placed where the new content starts, and SvelteKit restores the list's scroll. The layout measures the content's top before and after and moves the old picture by the difference.
- A click during a transition skips it, and its clean-up removed the next one's marks: only the latest transition cleans up now, and every navigation clears old marks before setting new ones.
Links
- MDN: view-transition-name
- MDN: view-transition-class
- Chrome for Developers: same-document view transitions
Next
- Alfonso tries it on a phone and says if the timing needs tuning.