This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Add a useSignalEffect
implementation that behaves like useEffect
#226
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f60cda7
Fix `useSignalEffect` so it behaves like `useEffect`
DAreRodz 910608f
Add failing test
luisherranz 04530ae
Fix mangled properties
luisherranz f63b4e0
Remove unnecessary options and RAF check
luisherranz 900cd81
Add e2e tests, init and change wp-show
luisherranz 751ad22
Add support for templates to the vdom algorigthm
luisherranz d9dbac2
Revert changes to wp-show
luisherranz d40ce00
Fix two minor issues
luisherranz 36c23d5
Add fake wp-show and avoid using window for testing
luisherranz 85a83a8
Remove unnecessary element
luisherranz 08d511e
Remove unneeded `return`
DAreRodz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,23 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Directives -- data-wp-effect</title> | ||
<meta itemprop="wp-client-side-navigation" content="active" /> | ||
</head> | ||
|
||
<body> | ||
<div data-wp-show="state.isOpen"> | ||
<input data-testid="input" data-wp-effect="effects.writeToWindow" /> | ||
</div> | ||
|
||
<div data-wp-effect="effects.changeFocus"></div> | ||
|
||
<button data-testid="toggle" data-wp-on.click="actions.toggle"> | ||
Update | ||
</button> | ||
|
||
<script defer src="../../build/e2e/directive-effect.js"></script> | ||
<script defer src="../../build/runtime.js"></script> | ||
<script defer src="../../build/vendors.js"></script> | ||
</body> | ||
</html> |
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,26 @@ | ||
import { store } from '../../src/runtime/store'; | ||
|
||
store({ | ||
state: { | ||
isOpen: true, | ||
}, | ||
actions: { | ||
toggle({ state }) { | ||
state.isOpen = !state.isOpen; | ||
}, | ||
}, | ||
effects: { | ||
writeToWindow: () => { | ||
window.effect = 'effect added'; | ||
|
||
return () => { | ||
window.effect = 'effect removed'; | ||
}; | ||
}, | ||
changeFocus: ({ state }) => { | ||
if (state.isOpen) { | ||
document.querySelector("[data-testid='input']").focus(); | ||
} | ||
}, | ||
}, | ||
}); |
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,26 @@ | ||
import { test, expect } from '../tests'; | ||
|
||
test.describe('data-wp-effect', () => { | ||
test.beforeEach(async ({ goToFile }) => { | ||
await goToFile('directives-effect.html'); | ||
}); | ||
|
||
test('check that effect runs when it is added', async ({ page }) => { | ||
const effect = await page.evaluate('window.effect'); | ||
expect(effect).toBe('effect added'); | ||
}); | ||
|
||
test('check that effect runs when it is removed', async ({ page }) => { | ||
await page.getByTestId('toggle').click(); | ||
const effect = await page.evaluate('window.effect'); | ||
expect(effect).toBe('effect removed'); | ||
}); | ||
|
||
test('change focus after DOM changes', async ({ page }) => { | ||
const el = page.getByTestId('input'); | ||
await expect(el).toBeFocused(); | ||
await page.getByTestId('toggle').click(); | ||
await page.getByTestId('toggle').click(); | ||
await expect(el).toBeFocused(); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This return is not needed; I'm pretty sure event listener callbacks always return
undefined
(see this). I'll change this, don't worry. 🙂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.
Oh, I see. So instead of returning
false
in theonclick
attribute, you'd useevent.stopPropagation()
in the event listener. And Preact is using the listener, not the attribute, right?