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

typing for defineExtension should allow partials #3030

Merged
merged 1 commit into from
Nov 22, 2024

Conversation

anentropic
Copy link
Contributor

Description

Docs for custom extensions show:

https://htmx.org/extensions/building/

To define an extension you call the htmx.defineExtension() function:

<script>
  htmx.defineExtension('my-ext', {
    onEvent : function(name, evt) {
        console.log("Fired event: " + name, evt);
    }
  })
</script>

Typically, this is done in a stand-alone javascript file, rather than in an inline script tag.
...
Extensions can override the following default extension points to add or change functionality:

However the current typing doesn't allow this - it requires a fully populated HtmxExtension object defining all the methods:

{
    init: function(api) {return null;},
    getSelectors: function() {return null;},
    onEvent : function(name, evt) {return true;},
    transformResponse : function(text, xhr, elt) {return text;},
    isInlineSwap : function(swapStyle) {return false;},
    handleSwap : function(swapStyle, target, fragment, settleInfo) {return false;},
    encodeParameters : function(xhr, parameters, elt) {return null;}
}

Testing

I made a working extension in my project and ignored the typing errors. (This is what prompted making this PR)

Changing the type to Partial<HtmxExtension> in my installed copy of htmx.esm.d.ts makes the typing errors go away and seems to reflect intended usage.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

@Telroshan Telroshan added bug Something isn't working documentation Improvements or additions to documentation ready for review Issues that are ready to be considered for merging labels Nov 22, 2024
Copy link
Collaborator

@Telroshan Telroshan left a comment

Choose a reason for hiding this comment

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

Cool, didn't know about Partial, I had struggled to find a way to make those optional yet not have them as "potentially null" when called by htmx itself...
Nice to know there was such a simple way to achieve this, thanks!

@Telroshan Telroshan removed the ready for review Issues that are ready to be considered for merging label Nov 22, 2024
@Telroshan Telroshan merged commit bc7ea4a into bigskysoftware:dev Nov 22, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants