Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suspend notification fading when mouse is over any notification #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/DefaultNotification.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
aria-live="polite"
in:fade
out:fade
on:mouseenter
on:mouseleave
>
<div class={getClass('content')}>
<slot>{text}</slot>
Expand Down
37 changes: 36 additions & 1 deletion src/components/Notification.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export let item;
export let notification = {};
export let withoutStyles = false;
export let fadeHalted;

const { removeNotification } = getNotificationsContext();
const {
Expand All @@ -17,18 +18,52 @@

let timeout = null;

let expirationTime = null;
let leftTime = null;
let running = false;

$ : {
if(fadeHalted)
haltFade()
else
resumeFade()
}

const haltFade = () => {
if(removeAfter && timeout && running) {
running = false;
leftTime = expirationTime - Date.now();
clearTimeout(timeout);
}
};

const resumeFade = () => {
if(removeAfter && timeout && !running) {
expirationTime = new Date(Date.now() + leftTime);
running = true;
timeout = setTimeout(removeNotificationHandler, leftTime);
}
};

if (removeAfter) {
expirationTime = new Date(Date.now() + removeAfter);
running = true;
timeout = setTimeout(removeNotificationHandler, removeAfter);
}

onDestroy(() => {
if (removeAfter && timeout) clearTimeout(timeout);
if (removeAfter && timeout) {
clearTimeout(timeout);
fadeHalted = false;
}
});
</script>

<svelte:component
this={item}
{notification}
{withoutStyles}
on:mouseenter={() => fadeHalted = true}
on:mouseleave={() => fadeHalted = false}
onRemove={removeNotificationHandler}
/>
3 changes: 3 additions & 0 deletions src/components/Notifications.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
export let withoutStyles = false;
export let zIndex = null;

let fadeHalted = false;

const getClass = (position = '') => {
const defaultPositionClass = ` default-position-style-${position}`;

Expand All @@ -76,6 +78,7 @@
<Notification
{notification}
{withoutStyles}
bind:fadeHalted
item={item || DefaultNotification}
/>
{/if}
Expand Down