Comments (8)
In the meantime, ReturnType<Swup['fetchPage']>
should work as the return type and resolve to Promise<PageData>
.
from swup.
Yes, let's export it as well. Technically, it's probably PageData
, not CacheData
, as we return the result of the fetch before any users or plugins can augment the CacheData
object.
from swup.
Interesting! Would you prefer exporting PageData
over using ReturnType<Swup['fetchPage']>
? I actually quite like this, as it will make sure the method will throw as soon as the implementation changes in swup. There are others, some of which can be typed using ReturnType
, for example the destroy
methods returned from delegate-it
:
mouseEnterDelegate?: ReturnType<Swup["delegateEvent"]>;
I wasn't able to correctly type the delegate handlers and events, though:
onMouseEnter(event) {}
should be typed like this:
onMouseEnter: DelegateEventHandler = (event: DelegateEvent<MouseEvent, Element>) => {}
DelegateEvent
is also needed for typing the event
in the link:hover
hook:
export interface HookDefinitions {
'link:hover': { el: HTMLAnchorElement, event: DelegateEvent }, // Cannot find name 'DelegateEvent'
'page:preload': { page: ReturnType<Swup['fetchPage']> }
}
I'm a bit confused now ๐ I'll open a PR against swup for exporting the types and you tell me which should be exported and which shouldn't.
from swup.
I'd go with exporting as many types as possible. Won't hurt ๐ค The ReturnType<>
thing works but feels a bit hacky to me and is less readable. PageType
, DelegateEvent
and DelegateEventUnsubscribe
will come in handy in lots of cases. Let's brainstorm which other ones we might need. HookArguments
?
from swup.
There's two things wrong with ReturnType<Swup['fetchPage']>
in my opinion:
fetchPage
returns aPromise<PageData>
whereas thepage:preload
returns an actualPageData
object- It looks rather cryptic in autocompletion. As a user, I guess I'd prefer
{ page: PageData }
for clarity
from swup.
Great! Let's export the types then ๐
from swup.
I'm trying to augment the hooks "link:hover" and "page:preload" from Preload Plugin now:
declare module 'swup' {
export interface HookDefinitions {
'link:hover': { el: HTMLAnchorElement; event: DelegateEvent };
'page:preload': { page: PageData };
}
}
When I try to use the event in my test project, I need to use this:
const onHoverLink: Handler<"link:hover"> = (visit, { el, event }) => {
console.log('link:hover:', el)
}
swup.hooks.on("link:hover", onHoverLink);
In this case, the visit
object doesn't make much sense. It would be great if we could simplify the callback by modifying the Handler
type so that it doesn't always need visit
and instead spreads the second args
argument for selected hook types, so that we can do:
const onHoverLink: Handler<"link:hover"> = (el, event) => {
console.log('link:hover:', el);
}
from swup.
I think itโll get confusing if 10 out of 12 hooks have the args as a second argument object, and two others have their arguments spread into the handlers. We might need to spell this out very explicitly in the docs โ the visit object for these two hooks is most likely useless ๐ฅด Or we actually find a way of resetting the visit object to some sort of interim visit object, that is somehow marked as stale or inactive?
from swup.
Related Issues (20)
- Rethink tests setup for official plugins HOT 5
- pageA goto pageB; but๏ผwith diffrent link style; when back to PageA,the links style is losted
- [Bug]: Undefined link href on Safari 13.1 and lower HOT 10
- [Feature Request]: Add way to easily listen to ALL hook events HOT 3
- [Feature Request]: Expose AbortController to be able abort request programmatically HOT 20
- Feature: Allow aborting running visits HOT 1
- Ignore errors in user hooks HOT 1
- Importing helpers from CDN module HOT 2
- History navigation ignored when using custom link selector HOT 5
- [Bug]: URL in `visit:start` inconsistent between normal and history visits HOT 4
- Feature: Store parsed html document in visit object HOT 11
- [Solved]: New values in <head> are not updated HOT 2
- [Bug]: [data-swup-ignore-script] issue with Contact Form 7 in Wordpress HOT 4
- Implement tests for official plugins HOT 1
- [Feature Request]: Add drop-in attribute to swap/replace content of elements outside the swup container HOT 4
- Slim down core
- [Idea]: New plugin for all route-related stuff HOT 3
- Idea: Move core and plugins into monorepo HOT 4
- Anime.js and Swup.js conflict
- [Feature Request]: Allow to control `options.linkToSelf` via callback function HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swup.