bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.61k stars 1.31k forks source link

typing for `defineExtension` should allow partials #3028

Closed anentropic closed 4 days ago

anentropic commented 6 days ago

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

MichaelWest22 commented 6 days ago

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 commented 5 days ago

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 commented 5 days ago

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 commented 5 days ago

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: https://github.com/bigskysoftware/htmx/compare/dev...anentropic:htmx:patch-1

Screenshot 2024-11-20 at 09 43 14
anentropic commented 5 days ago

https://stackoverflow.com/questions/77238329/github-shows-processing-updates-on-my-pull-request-after-every-push

I guess it will eventually resolve itself

anentropic commented 4 days ago

GitHub seems to have given up/failed

I'll close this and open a new PR

anentropic commented 4 days ago

https://github.com/bigskysoftware/htmx/pull/3030