GithubHelp home page GithubHelp logo

Export types for plugins about swup HOT 8 CLOSED

hirasso avatar hirasso commented on September 27, 2024
Export types for plugins

from swup.

Comments (8)

daun avatar daun commented on September 27, 2024 1

In the meantime, ReturnType<Swup['fetchPage']> should work as the return type and resolve to Promise<PageData>.

from swup.

daun avatar daun commented on September 27, 2024

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.

hirasso avatar hirasso commented on September 27, 2024

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.

daun avatar daun commented on September 27, 2024

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.

daun avatar daun commented on September 27, 2024

There's two things wrong with ReturnType<Swup['fetchPage']> in my opinion:

  • fetchPage returns a Promise<PageData> whereas the page:preload returns an actual PageData object
  • It looks rather cryptic in autocompletion. As a user, I guess I'd prefer { page: PageData } for clarity

from swup.

hirasso avatar hirasso commented on September 27, 2024

Great! Let's export the types then ๐Ÿ‘

from swup.

hirasso avatar hirasso commented on September 27, 2024

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.

daun avatar daun commented on September 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.