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

Refactor and expose swap in public API #2191

Merged
merged 7 commits into from
Jan 19, 2024
Merged

Refactor and expose swap in public API #2191

merged 7 commits into from
Jan 19, 2024

Conversation

Renerick
Copy link
Collaborator

@Renerick Renerick commented Jan 14, 2024

Description

This PR includes swap method in htmx public API. This feature has been requested on multiple occasions, was proposed for 2.0 and finally delivered. To make it possible, I did a bit of a refactoring by gathering every swap-related piece of code in fullSwap method (name is up for discussion), which handles

  • focus and selection preservation
  • title updates
  • head merging
  • scroll
  • OOB swapping
  • normal swapping
  • settling

This means that any extension and custom script can use full power of htmx's swapping features. I also anticipate that this change should significantly simplify maintenance of swapping in the future

This is a breaking change, as extensions no longer have access to selectAndSwap method. The new method fully covers the needs for swapping, while also removing responsibility of managing settling info.

The old method covered very specific part of swapping process and had a tricky signature which I struggle to keep untouched.

On the plus side, this is the entire code that's required to perform swapping from SSE extension

let target = api.getTarget(child);
let swapSpec = api.getSwapSpecification(child);
htmx.swap(target, event.data, {spec: swapSpec});

To be done/decided:

  • exposure of SwapSpecification type to the public world (are we ok with it? need to document it) - added to htmx.d.ts and documented in JS API section
  • documentation - documented
  • naming of the method - renamed to swap
  • upgrading official extensions (ws, sse) - TBD
  • handling view transitions from this method - TBD

Corresponding issue: 2.0

Testing

I ensured that this refactor did not affect existing tests at all. I also intend to write tests specifically for this method, if the team agrees that this approach works for us

@Renerick Renerick added the 2.0 label Jan 14, 2024
@alexpetros
Copy link
Collaborator

Fwiw, I feel like "extension-breaking" and "user breaking" are two separate concepts. I wouldn't want to break out extensions randomly, but the bar for breaking user functionality is higher than breaking the internal API.

@alexpetros
Copy link
Collaborator

Why fullSwap and not just swap?

@Renerick
Copy link
Collaborator Author

Renerick commented Jan 15, 2024

@alexpetros this was a placeholder name. I did it because we had swap method that implements specific swapping strategies and I wasn't sure how the refactor would turn out. I will rename it before merging

@Renerick
Copy link
Collaborator Author

Renerick commented Jan 17, 2024

As discussed, I've renamed the method, and also cleaned up signature a bit to get rid of duplicated parameters from swapSpec and swapOptions. I decided to separate swapSpec and swapOptions as separate arguments, since the former will be used much more often compared to the latter.

Also, some basic tests for the method.

@1cg 1cg merged commit 7fe27dd into v2.0v2.0 Jan 19, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants