Skip to content
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

Search modal view transitions #1831

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

barvian
Copy link

@barvian barvian commented Jun 3, 2024

I have no idea if the team is interested in something like this, but I figured I'd propose it 🙂

Live demo: tailwind-search.barvian.me (the Vercel previews are choppy for me; I think it's the dev toolbar)

search.mp4

I've always thought the search bars on the site would look great if they smoothly transitioned to the modal. The View Transitions API makes this trivial to implement while gracefully falling back to existing behavior if the browser doesn't support it or if the user prefers reduced motion. Plus, there's no lag because the modal is fully active while it's transitioning, so you can start typing immediately without waiting for the animation to end. I used a linear() spring transition with a perceptual duration of 250ms to keep things feeling snappy. I made sure to support the small-screen version too:

sm-search.mp4

The only noticeable difference from prod for browsers that support View Transitions is shown above 👆🏻: the "Quick search..." button on the homepage disappears whenever the search icon in the header is visible (on prod both are visible between sm and md breakpoints). This dramatically simplifies the view transition logic. Like everything else, though, it only applies if the user's browser supports view transitions and is "motion-safe".

It even works with route transitions:

route-transitions.mp4

I'm curious to hear your thoughts, if any!

Copy link

vercel bot commented Jun 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tailwindcss-com ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 10, 2024 0:28am

Copy link
Contributor

@brandonmcconnell brandonmcconnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incredible. What an insanely nice yet simple interaction. Well done. 💯

Comment on lines 5 to 16
const motionSafe =
typeof window !== 'undefined'
? window.matchMedia('(prefers-reduced-motion: no-preference)')
: undefined

export function startViewTransition(cb) {
if (motionSafe?.matches && typeof document?.startViewTransition === 'function') {
document.startViewTransition(() => {
flushSync(cb)
})
} else cb()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this prevent the transition from animating when prefers-reduced-motion is active, which will trigger the current non-animated behavior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup!

This reverts part of commit b7f6caf.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants