-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interactivity API: add Slot and Fill directives (#53958)
* Add undefined and null to the removeAttribute This should be done in a separate PR. * Adding first test cases (WIP) * Add comment * Remove unnecessary comment * Update test cases * Fix bind useEffect logic * Update changelog * Add type and comments to matrix entries * Fix phpcs errors * Copy current implementation * Refactor data-wp-slot * Add slot and fill tests * Add changelog * Fix changelog --------- Co-authored-by: Luis Herranz <[email protected]>
- Loading branch information
1 parent
db18bf3
commit 470730f
Showing
7 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/e2e-tests/plugins/interactive-blocks/directive-slots/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "test/directive-slots", | ||
"title": "E2E Interactivity tests - directive slots", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScript": "directive-slots-view", | ||
"render": "file:./render.php" | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/e2e-tests/plugins/interactive-blocks/directive-slots/render.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* HTML for testing the directive `data-wp-bind`. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
|
||
?> | ||
<div | ||
data-wp-interactive | ||
data-wp-slot-provider | ||
data-wp-context='{ "text": "fill" }' | ||
> | ||
<div data-testid="slots" data-wp-context='{ "text": "fill inside slots" }'> | ||
<div | ||
data-testid="slot-1" | ||
data-wp-key="slot-1" | ||
data-wp-slot="slot-1" | ||
data-wp-context='{ "text": "fill inside slot 1" }' | ||
>[1]</div> | ||
<div | ||
data-testid="slot-2" | ||
data-wp-key="slot-2" | ||
data-wp-slot='{ "name": "slot-2", "position": "before" }' | ||
data-wp-context='{ "text": "[2]" }' | ||
data-wp-text='context.text' | ||
data-wp-on--click="actions.updateSlotText" | ||
>[2]</div> | ||
<div | ||
data-testid="slot-3" | ||
data-wp-key="slot-3" | ||
data-wp-slot='{ "name": "slot-3", "position": "after" }' | ||
data-wp-context='{ "text": "[3]" }' | ||
data-wp-text='context.text' | ||
data-wp-on--click="actions.updateSlotText" | ||
>[3]</div> | ||
<div | ||
data-testid="slot-4" | ||
data-wp-key="slot-4" | ||
data-wp-slot='{ "name": "slot-4", "position": "children" }' | ||
data-wp-context='{ "text": "fill inside slot 4" }' | ||
>[4]</div> | ||
<div | ||
data-testid="slot-5" | ||
data-wp-key="slot-5" | ||
data-wp-slot='{ "name": "slot-5", "position": "replace" }' | ||
data-wp-context='{ "text": "fill inside slot 5" }' | ||
>[5]</div> | ||
</div> | ||
|
||
<div data-testid="fill-container"> | ||
<span | ||
data-testid="fill" | ||
data-wp-fill="state.slot" | ||
data-wp-text="context.text" | ||
>initial</span> | ||
</div> | ||
|
||
<div data-wp-on--click="actions.changeSlot"> | ||
<button data-testid="slot-1-button" data-slot="slot-1">slot-1</button> | ||
<button data-testid="slot-2-button" data-slot="slot-2">slot-2</button> | ||
<button data-testid="slot-3-button" data-slot="slot-3">slot-3</button> | ||
<button data-testid="slot-4-button" data-slot="slot-4">slot-4</button> | ||
<button data-testid="slot-5-button" data-slot="slot-5">slot-5</button> | ||
<button data-testid="reset" data-slot="">reset</button> | ||
</div> | ||
</div> |
18 changes: 18 additions & 0 deletions
18
packages/e2e-tests/plugins/interactive-blocks/directive-slots/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
( ( { wp } ) => { | ||
const { store } = wp.interactivity; | ||
|
||
store( { | ||
state: { | ||
slot: '' | ||
}, | ||
actions: { | ||
changeSlot: ( { state, event } ) => { | ||
state.slot = event.target.dataset.slot; | ||
}, | ||
updateSlotText: ( { context } ) => { | ||
const n = context.text[1]; | ||
context.text = `[${n} updated]`; | ||
}, | ||
}, | ||
} ); | ||
} )( window ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { createContext } from 'preact'; | ||
import { useContext, useEffect } from 'preact/hooks'; | ||
import { signal } from '@preact/signals'; | ||
|
||
const slotsContext = createContext(); | ||
|
||
export const Fill = ( { slot, children } ) => { | ||
const slots = useContext( slotsContext ); | ||
|
||
useEffect( () => { | ||
if ( slot ) { | ||
slots.value = { ...slots.value, [ slot ]: children }; | ||
return () => { | ||
slots.value = { ...slots.value, [ slot ]: null }; | ||
}; | ||
} | ||
}, [ slots, slot, children ] ); | ||
|
||
return !! slot ? null : children; | ||
}; | ||
|
||
export const SlotProvider = ( { children } ) => { | ||
return ( | ||
// TODO: We can change this to use deepsignal once this PR is merged. | ||
// https://github.com/luisherranz/deepsignal/pull/38 | ||
<slotsContext.Provider value={ signal( {} ) }> | ||
{ children } | ||
</slotsContext.Provider> | ||
); | ||
}; | ||
|
||
export const Slot = ( { name, children } ) => { | ||
const slots = useContext( slotsContext ); | ||
return slots.value[ name ] || children; | ||
}; |
Oops, something went wrong.