mirror of https://github.com/sveltejs/svelte
[feat] allow use:actions on <svelte:body> (#6608)
parent
74a1f29c26
commit
ce550adef6
@ -0,0 +1,18 @@
|
||||
export default {
|
||||
html: '<div></div>',
|
||||
|
||||
async test({ assert, target, window }) {
|
||||
const enter = new window.MouseEvent('mouseenter');
|
||||
const leave = new window.MouseEvent('mouseleave');
|
||||
|
||||
await window.document.body.dispatchEvent(enter);
|
||||
assert.htmlEqual(target.innerHTML, `
|
||||
<div>
|
||||
<div class="tooltip">Perform an Action</div>
|
||||
</div>
|
||||
`);
|
||||
|
||||
await window.document.body.dispatchEvent(leave);
|
||||
assert.htmlEqual(target.innerHTML, '<div></div>');
|
||||
}
|
||||
};
|
@ -0,0 +1,32 @@
|
||||
<script>
|
||||
let container;
|
||||
function tooltip(node, text) {
|
||||
let tooltip = null;
|
||||
|
||||
function onMouseEnter() {
|
||||
tooltip = document.createElement('div');
|
||||
tooltip.classList.add('tooltip');
|
||||
tooltip.textContent = text;
|
||||
container.appendChild(tooltip);
|
||||
}
|
||||
|
||||
function onMouseLeave() {
|
||||
if (!tooltip) return;
|
||||
tooltip.remove();
|
||||
tooltip = null;
|
||||
}
|
||||
|
||||
node.addEventListener('mouseenter', onMouseEnter);
|
||||
node.addEventListener('mouseleave', onMouseLeave);
|
||||
|
||||
return {
|
||||
destroy() {
|
||||
node.removeEventListener('mouseenter', onMouseEnter);
|
||||
node.removeEventListener('mouseleave', onMouseLeave);
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:body use:tooltip="{'Perform an Action'}" />
|
||||
<div bind:this={container} />
|
Loading…
Reference in new issue