Skip to content

Commit

Permalink
Interactivity API: Allow "default" suffix values (#65815)
Browse files Browse the repository at this point in the history
Fix an issue where "default" could not be used as "suffix" values in
Interactivity API directives, for example in a directive like:

    data-wp-class--default="…"

---

Co-authored-by: sirreal <[email protected]>
Co-authored-by: michalczaplinski <[email protected]>
Co-authored-by: gziolo <[email protected]>
  • Loading branch information
4 people authored and gutenbergplugin committed Oct 8, 2024
1 parent 43468fe commit 3ee7c74
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ class="foo bar baz"
data-testid="can toggle class when class attribute is missing"
></div>

<div data-wp-context='{ "falseValue": false }'>
<div data-wp-context='{ "value": false }'>
<div
class="foo"
data-wp-class--foo="context.falseValue"
data-wp-class--foo="context.value"
data-testid="can use context values"
></div>
<button
data-wp-on--click="actions.toggleContextFalseValue"
data-wp-on--click="actions.toggleContextValue"
data-testid="toggle context false value"
>
Toggle context falseValue
Toggle context value
</button>
</div>

Expand All @@ -83,4 +83,16 @@ class="foo"
data-testid="can use classes with several dashes"
></div>

<div data-wp-context='{ "value": false }'>
<div
data-wp-class--default="context.value"
data-testid="class name default"
></div>
<button
data-wp-on--click="actions.toggleContextValue"
data-testid="toggle class name default"
>
Toggle context val
</button>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const { state } = store( 'directive-class', {
toggleFalseValue: () => {
state.falseValue = ! state.falseValue;
},
toggleContextFalseValue: () => {
toggleContextValue: () => {
const context = getContext();
context.falseValue = ! context.falseValue;
context.value = ! context.value;
},
},
} );
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { directive } = privateApis(
directive(
'show-mock',
( { directives: { 'show-mock': showMock }, element, evaluate } ) => {
const entry = showMock.find( ( { suffix } ) => suffix === 'default' );
const entry = showMock.find( ( { suffix } ) => suffix === null );
if ( ! evaluate( entry ) ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { directive } = privateApis(
directive(
'show-mock',
( { directives: { 'show-mock': showMock }, element, evaluate } ) => {
const entry = showMock.find( ( { suffix } ) => suffix === 'default' );
const entry = showMock.find( ( { suffix } ) => suffix === null );
if ( ! evaluate( entry ) ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { directive } = privateApis(
directive(
'show-mock',
( { directives: { 'show-mock': showMock }, element, evaluate } ) => {
const entry = showMock.find( ( { suffix } ) => suffix === 'default' );
const entry = showMock.find( ( { suffix } ) => suffix === null );
if ( ! evaluate( entry ) ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ directive(
element,
evaluate,
} ) => {
const entry = showChildren.find(
( { suffix } ) => suffix === 'default'
);
const entry = showChildren.find( ( { suffix } ) => suffix === null );
return evaluate( entry )
? element
: cloneElement( element, { children: null } );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { directive } = privateApis(
directive(
'show-mock',
( { directives: { 'show-mock': showMock }, element, evaluate } ) => {
const entry = showMock.find( ( { suffix } ) => suffix === 'default' );
const entry = showMock.find( ( { suffix } ) => suffix === null );
if ( ! evaluate( entry ) ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { directive, h } = privateApis(
directive(
'show-mock',
( { directives: { 'show-mock': showMock }, element, evaluate } ) => {
const entry = showMock.find( ( { suffix } ) => suffix === 'default' );
const entry = showMock.find( ( { suffix } ) => suffix === null );

if ( ! evaluate( entry ) ) {
element.props.children = h(
Expand Down
1 change: 1 addition & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bug Fixes

- Fix an issue where "default" could not be used as a directive suffix ([#65815](https://github.com/WordPress/gutenberg/pull/65815)).
- Correctly handle lazily added, deeply nested properties with `deepMerge()` ([#65465](https://github.com/WordPress/gutenberg/pull/65465)).

## 6.8.0 (2024-09-19)
Expand Down
Loading

0 comments on commit 3ee7c74

Please sign in to comment.