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 #3028

Closed
wants to merge 2 commits into from

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

@anentropic anentropic changed the base branch from master to dev November 19, 2024 13:06
@anentropic anentropic force-pushed the patch-1 branch 2 times, most recently from bfd8df0 to fa220a6 Compare November 19, 2024 13:22
@MichaelWest22
Copy link
Contributor

The dist/htmx.esm.d.ts is a compiled distribution output file and not an editable source file. This thing is built from the JSDoc comments in the src/htmx.js file like

/**
   * defineExtension initializes the extension and adds it to the htmx registry
   *
   * @see https://htmx.org/api/#defineExtension
   *
   * @param {string} name the extension name
   * @param {HtmxExtension} extension the extension definition
   */
  function defineExtension(name, extension) {

So this is where i think it would need updating. I would suggest making the change here on your local machine and then do a npm run dist to regenerate the esm.d.ts file and retest with this file to confirm it works as expected

@anentropic
Copy link
Contributor Author

The dist/htmx.esm.d.ts is a compiled distribution output file and not an editable source file. This thing is built from the JSDoc comments in the src/htmx.js file

Ah thanks, I have amended that PR to update the correct file

@MichaelWest22
Copy link
Contributor

Sorry if you use npm run dist to test the distributed output you need to exclude the dist folder changes from the PR as normally only src/htmx.js is changed in dev PR's to make it easier to review changes. Dist only updates during release process normally

@anentropic
Copy link
Contributor Author

anentropic commented Nov 20, 2024

Sorry if you use npm run dist to test the distributed output you need to exclude the dist folder changes from the PR as normally only src/htmx.js is changed in dev PR's to make it easier to review changes. Dist only updates during release process normally

Sorry yes, I realised that and have force pushed again to remove the latter commit

However that push appears to be stuck in GitHub 'processing' somehow:
Screenshot 2024-11-20 at 09 45 19

I've never seen that before 🤷‍♂️

Actual diff against my branch shows it gone: dev...anentropic:htmx:patch-1

Screenshot 2024-11-20 at 09 43 14

@anentropic
Copy link
Contributor Author

@anentropic
Copy link
Contributor Author

anentropic commented Nov 21, 2024

GitHub seems to have given up/failed

I'll close this and open a new PR

@anentropic anentropic closed this Nov 21, 2024
@anentropic
Copy link
Contributor Author

#3030

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