-
Notifications
You must be signed in to change notification settings - Fork 28
TRY: Make namespace optional and combine with hookName using dot notation #108
Conversation
Codecov Report
@@ Coverage Diff @@
## master #108 +/- ##
==========================================
+ Coverage 64.54% 64.71% +0.16%
==========================================
Files 41 43 +2
Lines 598 615 +17
Branches 117 122 +5
==========================================
+ Hits 386 398 +12
- Misses 170 174 +4
- Partials 42 43 +1
Continue to review full report at Codecov.
|
@adamsilverstein, thanks for opening this PR. It seems like the proposed approach is the best possible moving forward. I have only one concern, which is a blocker in my opinion. This won't work with Gutenberg because of the way we name our hooks. See: https://wordpress.org/gutenberg/handbook/extensibility/extending-blocks/. Some examples:
It won't work properly with what you proposed and I'm afraid it's too late to change those names inside Gutenberg. Can we seek for a better naming convention that would work as you proposed, but would also ensure that Gutenberg hooks will work as before? Some alternatives are:
I think we have 2 different styles because there were 2 different validation method for both params. We can update 2nd part to match whatever we agree upon in here. |
@@ -18,21 +18,52 @@ A lightweight & efficient filter and action manager. | |||
|
|||
### API Usage | |||
|
|||
When adding a hooked action or filter, best practice is to add a namespace in the form `hookName.vendor/plugin/functionName`. This enables checking for and removing the hooked callback. | |||
|
|||
// Create a hooks instance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not wrapped in code so we should either do it or remove preceding //
:)
if ( ! /^[a-zA-Z][a-zA-Z0-9_.-]*$/.test( hookName ) ) { | ||
console.error( 'The hook name can only contain numbers, letters, dashes, periods and underscores.' ); | ||
if ( ! /^[a-zA-Z][a-zA-Z0-9_-]*$/.test( hookName ) ) { | ||
console.error( 'The hook name can only contain numbers, letters, dashes and underscores.' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gutenberg and its hooks depend on periods :(
I quite like this proposal. Feels very natural and fitting for Javascript. It could be worth exploring if adding console warnings when namespaces are omitted is something we want to encourage good standards, perhaps as an option that can be passed to Regarding the above. While periods would be preferred in my opinion due to the consistency with jQuery we could of course replace them with say a |
I took a slightly different approach in #113 by making the namespace parameter optional, but not moving it into the hook parameter. This will give us better back compat for existing use cases, and work correctly with Gutenberg. For plugins, the idea would be to have registerPlugin pass the plugin namespace to created hooks.
|
Issue moved to WordPress/gutenberg #7820 via ZenHub |
This PR seeks to remove the namespace parameter and instead enable support for an optional namespace added to the hookName using dot notation, in the form
hookName.vendor/plugin/function
. Fixes #107.Includes updated docs: https://github.com/WordPress/packages/blob/fda1b56a606afccf40b86867f80710f6895559bb/packages/hooks/README.md