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

Support touchstart, focus, blur events in link prefetch #2134

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

Conversation

m5r
Copy link

@m5r m5r commented Dec 13, 2024

Add support for touchstart, focus, and blur events in link prefetch hover behavior.

This is hugely inspired from React Router's link prefetching code. Their event handlers composition is pretty neat too but I don't think it's necessary here.

@m5r m5r marked this pull request as ready for review December 13, 2024 20:04
@@ -165,7 +165,7 @@ const Link = forwardRef<unknown, InertiaLinkProps>(
}, prefetchModes)

const regularEvents = {
onClick: (event) => {
onClick: (event: Parameters<InertiaLinkProps['onClick']>[0]) => {
Copy link
Author

Choose a reason for hiding this comment

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

This looks a bit ugly but it's basically reusing the type of the first parameter of the onClick property defined in InertiaLinkProps above

click: regularEvents.click,
}
} satisfies ActionEventHandlers
Copy link
Author

Choose a reason for hiding this comment

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

I replaced the type assignment with the satisfies operator to make autocomplete smarter. Now typescript knows which properties from ActionEventHandlers are defined and which aren't while still validating the types are correct.

@@ -51,11 +51,11 @@ test('can prefetch using link props', async ({ page }) => {

await page.getByRole('link', { name: 'On Mount' }).click()
await isPrefetchPage(page, 2)
await expect(requests.requests.length).toBe(0)
expect(requests.requests.length).toBe(0)
Copy link
Author

Choose a reason for hiding this comment

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

awaits here were not necessary, expect().toBe() doesn't return a promise, it's all synchronous

@m5r m5r force-pushed the expand-prefetch-behavior branch from 4d726f2 to c9e0aa2 Compare December 13, 2024 20:14
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.

1 participant