feat: add Embla initialization hooks#145
Conversation
2d40494 to
1a0dd9c
Compare
|
Additional local manual test after adding
|
|
Additional local test using WordPress block-style classes:
This keeps the PR API compatible with normal WordPress block style workflows. |
a55530e to
1a0dd9c
Compare
| ); | ||
| ``` | ||
|
|
||
| Both filters receive the carousel context object as the third argument. `rtcamp.carouselKit.emblaPlugins` also receives the filtered options on `options`. |
There was a problem hiding this comment.
Contradicting statement: In @wordpress/hooks, the first argument passed to a filter callback is the filtered value (i.e. options or plugins).
applyFilters( 'hookName', content, arg1, arg2, moreArgs, finalArg )
The registered callback will receive filterContext as its second argument (not the third).
https://developer.wordpress.org/block-editor/reference-guides/packages/packages-hooks/
So the second argument received by the callback is not the "carousel context object", but rather a wrapper filterContext object containing the context property along with root, viewport, etc.
| Both filters receive the carousel context object as the third argument. `rtcamp.carouselKit.emblaPlugins` also receives the filtered options on `options`. | |
| Both filters receive the filter context object as the second argument of the callback. `rtcamp.carouselKit.emblaPlugins` also receives the filtered options on the `options` property of this object. |
| return value; | ||
| } | ||
|
|
||
| return applyFilters( hookName, value, filterContext ) as T; |
There was a problem hiding this comment.
The return value is cast without runtime validation.
| return applyFilters( hookName, value, filterContext ) as T; | |
| const result = applyFilters( hookName, value, filterContext ); | |
| return ( result !== null && result !== undefined ) ? result as T : value; |
| { ...filterContext, options: filteredOptions }, | ||
| ); | ||
|
|
||
| const embla = EmblaCarousel( viewport, filteredOptions, filteredPlugins ); |
There was a problem hiding this comment.
No guard on filteredPlugins being an array
| const embla = EmblaCarousel( viewport, filteredOptions, filteredPlugins ); | |
| const safePlugins = Array.isArray( filteredPlugins ) ? filteredPlugins : plugins; | |
| const embla = EmblaCarousel( viewport, filteredOptions, safePlugins ); |
There was a problem hiding this comment.
Thanks for the review. I’ve pushed an update addressing these points:
- Corrected the API docs to describe the
@wordpress/hooksfilter callback arguments accurately. - Updated the filter wrapper so
null/undefinedresults fall back to the original value. - Added a guard so
rtcamp.carouselKit.emblaPluginsonly passes an array to Embla, falling back to the original plugins otherwise. - Added regression tests for the defensive fallback cases.
Verified with:
npm run test:js -- src/blocks/carousel/__tests__/view.test.ts --runInBandnpm run lint:js:typesnpm run lint:jsnpm run test:js -- --runInBandnpm run build
milindmore22
left a comment
There was a problem hiding this comment.
Thank you for the PR; this looks good to me. A great addition 👍🏼
theMasudRana
left a comment
There was a problem hiding this comment.
Hi @sunmorgn,
Could we rename the new hook namespace from rtcamp.carouselKit.* to follow the current plugin convention, since the plugin has been renamed to rt-carousel?
For example, something like:
rtcamp.rtCarousel.emblaOptionsrtcamp.rtCarousel.emblaPluginsrtcamp.rtCarousel.emblaInit
This would keep the extension API aligned with the current plugin branding and avoid carrying forward the old carouselKit name.
|
Thanks for catching that. I’ve pushed an update renaming the new Embla hooks to follow the current plugin namespace:
I updated the docs, source, and tests. I left the existing Verified with the JS tests, type check, lint, and build. |
Summary
Adds stable JavaScript hooks around rtCarousel's Embla initialization so projects can adjust Embla options, append Embla plugins, and access the initialized Embla API for methods and events without forking the plugin.
Type of change
Related issue(s)
Closes #144
What changed
rtcamp.carouselKit.emblaOptionsto filter the options passed toEmblaCarousel.rtcamp.carouselKit.emblaPluginsto filter the Embla plugin array after the built-in Autoplay plugin is configured.rtcamp.carouselKit.emblaInitso integrations can call Embla methods or subscribe to Embla events after initialization.Breaking changes
Does this introduce a breaking change? If yes, describe the impact and migration path below.
Testing
Describe how this was tested.
Test details:
npm run lint:jsnpm run lint:js:typesnpm run test:js -- --runTestsByPath src/blocks/carousel/__tests__/view.test.tsnpm run buildScreenshots / recordings
Not applicable; this change exposes developer extension hooks and does not alter saved markup or default carousel behavior.
Checklist