svelte-toasty
Version:
Easily configurable toast notification component for Svelte
1,068 lines (1,001 loc) • 32.8 kB
JavaScript
function noop() { }
const identity = x => x;
function assign(tar, src) {
// @ts-ignore
for (const k in src)
tar[k] = src[k];
return tar;
}
function run(fn) {
return fn();
}
function blank_object() {
return Object.create(null);
}
function run_all(fns) {
fns.forEach(run);
}
function is_function(thing) {
return typeof thing === 'function';
}
function safe_not_equal(a, b) {
return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
function is_empty(obj) {
return Object.keys(obj).length === 0;
}
function create_slot(definition, ctx, $$scope, fn) {
if (definition) {
const slot_ctx = get_slot_context(definition, ctx, $$scope, fn);
return definition[0](slot_ctx);
}
}
function get_slot_context(definition, ctx, $$scope, fn) {
return definition[1] && fn
? assign($$scope.ctx.slice(), definition[1](fn(ctx)))
: $$scope.ctx;
}
function get_slot_changes(definition, $$scope, dirty, fn) {
if (definition[2] && fn) {
const lets = definition[2](fn(dirty));
if ($$scope.dirty === undefined) {
return lets;
}
if (typeof lets === 'object') {
const merged = [];
const len = Math.max($$scope.dirty.length, lets.length);
for (let i = 0; i < len; i += 1) {
merged[i] = $$scope.dirty[i] | lets[i];
}
return merged;
}
return $$scope.dirty | lets;
}
return $$scope.dirty;
}
function update_slot(slot, slot_definition, ctx, $$scope, dirty, get_slot_changes_fn, get_slot_context_fn) {
const slot_changes = get_slot_changes(slot_definition, $$scope, dirty, get_slot_changes_fn);
if (slot_changes) {
const slot_context = get_slot_context(slot_definition, ctx, $$scope, get_slot_context_fn);
slot.p(slot_context, slot_changes);
}
}
const is_client = typeof window !== 'undefined';
let now = is_client
? () => window.performance.now()
: () => Date.now();
let raf = is_client ? cb => requestAnimationFrame(cb) : noop;
const tasks = new Set();
function run_tasks(now) {
tasks.forEach(task => {
if (!task.c(now)) {
tasks.delete(task);
task.f();
}
});
if (tasks.size !== 0)
raf(run_tasks);
}
/**
* Creates a new task that runs on each raf frame
* until it returns a falsy value or is aborted
*/
function loop(callback) {
let task;
if (tasks.size === 0)
raf(run_tasks);
return {
promise: new Promise(fulfill => {
tasks.add(task = { c: callback, f: fulfill });
}),
abort() {
tasks.delete(task);
}
};
}
function append(target, node) {
target.appendChild(node);
}
function insert(target, node, anchor) {
target.insertBefore(node, anchor || null);
}
function detach(node) {
node.parentNode.removeChild(node);
}
function element(name) {
return document.createElement(name);
}
function text(data) {
return document.createTextNode(data);
}
function space() {
return text(' ');
}
function listen(node, event, handler, options) {
node.addEventListener(event, handler, options);
return () => node.removeEventListener(event, handler, options);
}
function attr(node, attribute, value) {
if (value == null)
node.removeAttribute(attribute);
else if (node.getAttribute(attribute) !== value)
node.setAttribute(attribute, value);
}
function children(element) {
return Array.from(element.childNodes);
}
function set_data(text, data) {
data = '' + data;
if (text.wholeText !== data)
text.data = data;
}
function set_style(node, key, value, important) {
node.style.setProperty(key, value, important ? 'important' : '');
}
function custom_event(type, detail) {
const e = document.createEvent('CustomEvent');
e.initCustomEvent(type, false, false, detail);
return e;
}
const active_docs = new Set();
let active = 0;
// https://github.com/darkskyapp/string-hash/blob/master/index.js
function hash(str) {
let hash = 5381;
let i = str.length;
while (i--)
hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
return hash >>> 0;
}
function create_rule(node, a, b, duration, delay, ease, fn, uid = 0) {
const step = 16.666 / duration;
let keyframes = '{\n';
for (let p = 0; p <= 1; p += step) {
const t = a + (b - a) * ease(p);
keyframes += p * 100 + `%{${fn(t, 1 - t)}}\n`;
}
const rule = keyframes + `100% {${fn(b, 1 - b)}}\n}`;
const name = `__svelte_${hash(rule)}_${uid}`;
const doc = node.ownerDocument;
active_docs.add(doc);
const stylesheet = doc.__svelte_stylesheet || (doc.__svelte_stylesheet = doc.head.appendChild(element('style')).sheet);
const current_rules = doc.__svelte_rules || (doc.__svelte_rules = {});
if (!current_rules[name]) {
current_rules[name] = true;
stylesheet.insertRule(`@keyframes ${name} ${rule}`, stylesheet.cssRules.length);
}
const animation = node.style.animation || '';
node.style.animation = `${animation ? `${animation}, ` : ''}${name} ${duration}ms linear ${delay}ms 1 both`;
active += 1;
return name;
}
function delete_rule(node, name) {
const previous = (node.style.animation || '').split(', ');
const next = previous.filter(name
? anim => anim.indexOf(name) < 0 // remove specific animation
: anim => anim.indexOf('__svelte') === -1 // remove all Svelte animations
);
const deleted = previous.length - next.length;
if (deleted) {
node.style.animation = next.join(', ');
active -= deleted;
if (!active)
clear_rules();
}
}
function clear_rules() {
raf(() => {
if (active)
return;
active_docs.forEach(doc => {
const stylesheet = doc.__svelte_stylesheet;
let i = stylesheet.cssRules.length;
while (i--)
stylesheet.deleteRule(i);
doc.__svelte_rules = {};
});
active_docs.clear();
});
}
let current_component;
function set_current_component(component) {
current_component = component;
}
function get_current_component() {
if (!current_component)
throw new Error('Function called outside component initialization');
return current_component;
}
function onDestroy(fn) {
get_current_component().$$.on_destroy.push(fn);
}
const dirty_components = [];
const binding_callbacks = [];
const render_callbacks = [];
const flush_callbacks = [];
const resolved_promise = Promise.resolve();
let update_scheduled = false;
function schedule_update() {
if (!update_scheduled) {
update_scheduled = true;
resolved_promise.then(flush);
}
}
function add_render_callback(fn) {
render_callbacks.push(fn);
}
let flushing = false;
const seen_callbacks = new Set();
function flush() {
if (flushing)
return;
flushing = true;
do {
// first, call beforeUpdate functions
// and update components
for (let i = 0; i < dirty_components.length; i += 1) {
const component = dirty_components[i];
set_current_component(component);
update(component.$$);
}
set_current_component(null);
dirty_components.length = 0;
while (binding_callbacks.length)
binding_callbacks.pop()();
// then, once components are updated, call
// afterUpdate functions. This may cause
// subsequent updates...
for (let i = 0; i < render_callbacks.length; i += 1) {
const callback = render_callbacks[i];
if (!seen_callbacks.has(callback)) {
// ...so guard against infinite loops
seen_callbacks.add(callback);
callback();
}
}
render_callbacks.length = 0;
} while (dirty_components.length);
while (flush_callbacks.length) {
flush_callbacks.pop()();
}
update_scheduled = false;
flushing = false;
seen_callbacks.clear();
}
function update($$) {
if ($$.fragment !== null) {
$$.update();
run_all($$.before_update);
const dirty = $$.dirty;
$$.dirty = [-1];
$$.fragment && $$.fragment.p($$.ctx, dirty);
$$.after_update.forEach(add_render_callback);
}
}
let promise;
function wait() {
if (!promise) {
promise = Promise.resolve();
promise.then(() => {
promise = null;
});
}
return promise;
}
function dispatch(node, direction, kind) {
node.dispatchEvent(custom_event(`${direction ? 'intro' : 'outro'}${kind}`));
}
const outroing = new Set();
let outros;
function group_outros() {
outros = {
r: 0,
c: [],
p: outros // parent group
};
}
function check_outros() {
if (!outros.r) {
run_all(outros.c);
}
outros = outros.p;
}
function transition_in(block, local) {
if (block && block.i) {
outroing.delete(block);
block.i(local);
}
}
function transition_out(block, local, detach, callback) {
if (block && block.o) {
if (outroing.has(block))
return;
outroing.add(block);
outros.c.push(() => {
outroing.delete(block);
if (callback) {
if (detach)
block.d(1);
callback();
}
});
block.o(local);
}
}
const null_transition = { duration: 0 };
function create_bidirectional_transition(node, fn, params, intro) {
let config = fn(node, params);
let t = intro ? 0 : 1;
let running_program = null;
let pending_program = null;
let animation_name = null;
function clear_animation() {
if (animation_name)
delete_rule(node, animation_name);
}
function init(program, duration) {
const d = program.b - t;
duration *= Math.abs(d);
return {
a: t,
b: program.b,
d,
duration,
start: program.start,
end: program.start + duration,
group: program.group
};
}
function go(b) {
const { delay = 0, duration = 300, easing = identity, tick = noop, css } = config || null_transition;
const program = {
start: now() + delay,
b
};
if (!b) {
// @ts-ignore todo: improve typings
program.group = outros;
outros.r += 1;
}
if (running_program || pending_program) {
pending_program = program;
}
else {
// if this is an intro, and there's a delay, we need to do
// an initial tick and/or apply CSS animation immediately
if (css) {
clear_animation();
animation_name = create_rule(node, t, b, duration, delay, easing, css);
}
if (b)
tick(0, 1);
running_program = init(program, duration);
add_render_callback(() => dispatch(node, b, 'start'));
loop(now => {
if (pending_program && now > pending_program.start) {
running_program = init(pending_program, duration);
pending_program = null;
dispatch(node, running_program.b, 'start');
if (css) {
clear_animation();
animation_name = create_rule(node, t, running_program.b, running_program.duration, 0, easing, config.css);
}
}
if (running_program) {
if (now >= running_program.end) {
tick(t = running_program.b, 1 - t);
dispatch(node, running_program.b, 'end');
if (!pending_program) {
// we're done
if (running_program.b) {
// intro — we can tidy up immediately
clear_animation();
}
else {
// outro — needs to be coordinated
if (!--running_program.group.r)
run_all(running_program.group.c);
}
}
running_program = null;
}
else if (now >= running_program.start) {
const p = now - running_program.start;
t = running_program.a + running_program.d * easing(p / running_program.duration);
tick(t, 1 - t);
}
}
return !!(running_program || pending_program);
});
}
}
return {
run(b) {
if (is_function(config)) {
wait().then(() => {
// @ts-ignore
config = config();
go(b);
});
}
else {
go(b);
}
},
end() {
clear_animation();
running_program = pending_program = null;
}
};
}
function outro_and_destroy_block(block, lookup) {
transition_out(block, 1, 1, () => {
lookup.delete(block.key);
});
}
function update_keyed_each(old_blocks, dirty, get_key, dynamic, ctx, list, lookup, node, destroy, create_each_block, next, get_context) {
let o = old_blocks.length;
let n = list.length;
let i = o;
const old_indexes = {};
while (i--)
old_indexes[old_blocks[i].key] = i;
const new_blocks = [];
const new_lookup = new Map();
const deltas = new Map();
i = n;
while (i--) {
const child_ctx = get_context(ctx, list, i);
const key = get_key(child_ctx);
let block = lookup.get(key);
if (!block) {
block = create_each_block(key, child_ctx);
block.c();
}
else if (dynamic) {
block.p(child_ctx, dirty);
}
new_lookup.set(key, new_blocks[i] = block);
if (key in old_indexes)
deltas.set(key, Math.abs(i - old_indexes[key]));
}
const will_move = new Set();
const did_move = new Set();
function insert(block) {
transition_in(block, 1);
block.m(node, next);
lookup.set(block.key, block);
next = block.first;
n--;
}
while (o && n) {
const new_block = new_blocks[n - 1];
const old_block = old_blocks[o - 1];
const new_key = new_block.key;
const old_key = old_block.key;
if (new_block === old_block) {
// do nothing
next = new_block.first;
o--;
n--;
}
else if (!new_lookup.has(old_key)) {
// remove old block
destroy(old_block, lookup);
o--;
}
else if (!lookup.has(new_key) || will_move.has(new_key)) {
insert(new_block);
}
else if (did_move.has(old_key)) {
o--;
}
else if (deltas.get(new_key) > deltas.get(old_key)) {
did_move.add(new_key);
insert(new_block);
}
else {
will_move.add(old_key);
o--;
}
}
while (o--) {
const old_block = old_blocks[o];
if (!new_lookup.has(old_block.key))
destroy(old_block, lookup);
}
while (n)
insert(new_blocks[n - 1]);
return new_blocks;
}
function mount_component(component, target, anchor) {
const { fragment, on_mount, on_destroy, after_update } = component.$$;
fragment && fragment.m(target, anchor);
// onMount happens before the initial afterUpdate
add_render_callback(() => {
const new_on_destroy = on_mount.map(run).filter(is_function);
if (on_destroy) {
on_destroy.push(...new_on_destroy);
}
else {
// Edge case - component was destroyed immediately,
// most likely as a result of a binding initialising
run_all(new_on_destroy);
}
component.$$.on_mount = [];
});
after_update.forEach(add_render_callback);
}
function destroy_component(component, detaching) {
const $$ = component.$$;
if ($$.fragment !== null) {
run_all($$.on_destroy);
$$.fragment && $$.fragment.d(detaching);
// TODO null out other refs, including component.$$ (but need to
// preserve final state?)
$$.on_destroy = $$.fragment = null;
$$.ctx = [];
}
}
function make_dirty(component, i) {
if (component.$$.dirty[0] === -1) {
dirty_components.push(component);
schedule_update();
component.$$.dirty.fill(0);
}
component.$$.dirty[(i / 31) | 0] |= (1 << (i % 31));
}
function init(component, options, instance, create_fragment, not_equal, props, dirty = [-1]) {
const parent_component = current_component;
set_current_component(component);
const $$ = component.$$ = {
fragment: null,
ctx: null,
// state
props,
update: noop,
not_equal,
bound: blank_object(),
// lifecycle
on_mount: [],
on_destroy: [],
before_update: [],
after_update: [],
context: new Map(parent_component ? parent_component.$$.context : []),
// everything else
callbacks: blank_object(),
dirty,
skip_bound: false
};
let ready = false;
$$.ctx = instance
? instance(component, options.props || {}, (i, ret, ...rest) => {
const value = rest.length ? rest[0] : ret;
if ($$.ctx && not_equal($$.ctx[i], $$.ctx[i] = value)) {
if (!$$.skip_bound && $$.bound[i])
$$.bound[i](value);
if (ready)
make_dirty(component, i);
}
return ret;
})
: [];
$$.update();
ready = true;
run_all($$.before_update);
// `false` as a special case of no DOM component
$$.fragment = create_fragment ? create_fragment($$.ctx) : false;
if (options.target) {
if (options.hydrate) {
const nodes = children(options.target);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment && $$.fragment.l(nodes);
nodes.forEach(detach);
}
else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment && $$.fragment.c();
}
if (options.intro)
transition_in(component.$$.fragment);
mount_component(component, options.target, options.anchor);
flush();
}
set_current_component(parent_component);
}
/**
* Base class for Svelte components. Used when dev=false.
*/
class SvelteComponent {
$destroy() {
destroy_component(this, 1);
this.$destroy = noop;
}
$on(type, callback) {
const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = []));
callbacks.push(callback);
return () => {
const index = callbacks.indexOf(callback);
if (index !== -1)
callbacks.splice(index, 1);
};
}
$set($$props) {
if (this.$$set && !is_empty($$props)) {
this.$$.skip_bound = true;
this.$$set($$props);
this.$$.skip_bound = false;
}
}
}
const subscriber_queue = [];
/**
* Create a `Writable` store that allows both updating and reading by subscription.
* @param {*=}value initial value
* @param {StartStopNotifier=}start start and stop notifications for subscriptions
*/
function writable(value, start = noop) {
let stop;
const subscribers = [];
function set(new_value) {
if (safe_not_equal(value, new_value)) {
value = new_value;
if (stop) { // store is ready
const run_queue = !subscriber_queue.length;
for (let i = 0; i < subscribers.length; i += 1) {
const s = subscribers[i];
s[1]();
subscriber_queue.push(s, value);
}
if (run_queue) {
for (let i = 0; i < subscriber_queue.length; i += 2) {
subscriber_queue[i][0](subscriber_queue[i + 1]);
}
subscriber_queue.length = 0;
}
}
}
}
function update(fn) {
set(fn(value));
}
function subscribe(run, invalidate = noop) {
const subscriber = [run, invalidate];
subscribers.push(subscriber);
if (subscribers.length === 1) {
stop = start(set) || noop;
}
run(value);
return () => {
const index = subscribers.indexOf(subscriber);
if (index !== -1) {
subscribers.splice(index, 1);
}
if (subscribers.length === 0) {
stop();
stop = null;
}
};
}
return { set, update, subscribe };
}
const store = writable();
function fade(node, { delay = 0, duration = 400, easing = identity } = {}) {
const o = +getComputedStyle(node).opacity;
return {
delay,
duration,
easing,
css: t => `opacity: ${t * o}`
};
}
/* src/svelte-toasty/toast-container.svelte generated by Svelte v3.32.1 */
function add_css() {
var style = element("style");
style.id = "svelte-11d8vkt-style";
style.textContent = ".svelte-toasty{list-style:none;position:fixed;right:0.5rem;top:0.5rem;padding:0;margin:0;z-index:9999;position:fixed;max-width:30rem;display:flex;flex-direction:column;justify-content:flex-end;align-items:flex-end}.svelte-toasty-toast{position:relative;margin:0.25rem 0;overflow:hidden;border-radius:.25rem;position:relative;width:auto;color:inherit;color:#fff;background-color:#2D3748;box-shadow:0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06)}.svelte-toasty-container{padding:1rem;display:block;font-weight:500;font-size:1rem;display:flex;align-items:center;justify-content:space-between}.svelte-toasty-content{display:flex;font-family:inherit;align-items:center}.svelte-toasty-progress{position:absolute;bottom:0;background-color:rgb(0, 0, 0, 0.3);height:0.4rem;width:100%;animation-name:svelte-11d8vkt-shrink;animation-timing-function:linear;animation-fill-mode:forwards}@keyframes svelte-11d8vkt-shrink{0%{width:100%}100%{width:0}}.svelte-toasty-toast.danger{background-color:#C53030}.svelte-toasty-toast.success{background-color:#38A169}.svelte-toasty-toast.warning{background-color:#ED8936}.svelte-toasty-toast.info{background-color:#3182CE}";
append(document.head, style);
}
function get_each_context(ctx, list, i) {
const child_ctx = ctx.slice();
child_ctx[11] = list[i];
child_ctx[14] = function func() {
return /*func*/ child_ctx[6](/*toast*/ child_ctx[11]);
};
return child_ctx;
}
const get_actions_slot_changes = dirty => ({
toast: dirty & /*toasts*/ 1,
remove: dirty & /*toasts*/ 1
});
const get_actions_slot_context = ctx => ({
toast: /*toast*/ ctx[11],
remove: /*func_func*/ ctx[14]
});
const get_message_slot_changes = dirty => ({ toast: dirty & /*toasts*/ 1 });
const get_message_slot_context = ctx => ({ toast: /*toast*/ ctx[11] });
const get_icon_slot_changes = dirty => ({ toast: dirty & /*toasts*/ 1 });
const get_icon_slot_context = ctx => ({ toast: /*toast*/ ctx[11] });
// (90:42) {toast.text}
function fallback_block(ctx) {
let t_value = /*toast*/ ctx[11].text + "";
let t;
return {
c() {
t = text(t_value);
},
m(target, anchor) {
insert(target, t, anchor);
},
p(ctx, dirty) {
if (dirty & /*toasts*/ 1 && t_value !== (t_value = /*toast*/ ctx[11].text + "")) set_data(t, t_value);
},
d(detaching) {
if (detaching) detach(t);
}
};
}
// (85:1) {#each toasts as toast (toast.id)}
function create_each_block(key_1, ctx) {
let li;
let div1;
let div0;
let t0;
let t1;
let t2;
let div2;
let div2_class_value;
let t3;
let li_class_value;
let li_transition;
let current;
let mounted;
let dispose;
const icon_slot_template = /*#slots*/ ctx[5].icon;
const icon_slot = create_slot(icon_slot_template, ctx, /*$$scope*/ ctx[4], get_icon_slot_context);
const message_slot_template = /*#slots*/ ctx[5].message;
const message_slot = create_slot(message_slot_template, ctx, /*$$scope*/ ctx[4], get_message_slot_context);
const message_slot_or_fallback = message_slot || fallback_block(ctx);
const actions_slot_template = /*#slots*/ ctx[5].actions;
const actions_slot = create_slot(actions_slot_template, ctx, /*$$scope*/ ctx[4], get_actions_slot_context);
function animationend_handler() {
return /*animationend_handler*/ ctx[7](/*toast*/ ctx[11]);
}
return {
key: key_1,
first: null,
c() {
li = element("li");
div1 = element("div");
div0 = element("div");
if (icon_slot) icon_slot.c();
t0 = space();
if (message_slot_or_fallback) message_slot_or_fallback.c();
t1 = space();
if (actions_slot) actions_slot.c();
t2 = space();
div2 = element("div");
t3 = space();
attr(div0, "class", "svelte-toasty-content");
attr(div1, "class", "svelte-toasty-container");
attr(div2, "aria-hidden", "true");
attr(div2, "class", div2_class_value = "svelte-toasty-progress " + (/*classes*/ ctx[1].progress ?? ""));
set_style(div2, "animation-duration", /*toast*/ ctx[11].timeout + "ms");
attr(li, "class", li_class_value = "svelte-toasty-toast " + /*toast*/ ctx[11].type + " " + (/*classes*/ ctx[1].toast ?? ""));
this.first = li;
},
m(target, anchor) {
insert(target, li, anchor);
append(li, div1);
append(div1, div0);
if (icon_slot) {
icon_slot.m(div0, null);
}
append(div0, t0);
if (message_slot_or_fallback) {
message_slot_or_fallback.m(div0, null);
}
append(div1, t1);
if (actions_slot) {
actions_slot.m(div1, null);
}
append(li, t2);
append(li, div2);
append(li, t3);
current = true;
if (!mounted) {
dispose = listen(div2, "animationend", animationend_handler);
mounted = true;
}
},
p(new_ctx, dirty) {
ctx = new_ctx;
if (icon_slot) {
if (icon_slot.p && dirty & /*$$scope, toasts*/ 17) {
update_slot(icon_slot, icon_slot_template, ctx, /*$$scope*/ ctx[4], dirty, get_icon_slot_changes, get_icon_slot_context);
}
}
if (message_slot) {
if (message_slot.p && dirty & /*$$scope, toasts*/ 17) {
update_slot(message_slot, message_slot_template, ctx, /*$$scope*/ ctx[4], dirty, get_message_slot_changes, get_message_slot_context);
}
} else {
if (message_slot_or_fallback && message_slot_or_fallback.p && dirty & /*toasts*/ 1) {
message_slot_or_fallback.p(ctx, dirty);
}
}
if (actions_slot) {
if (actions_slot.p && dirty & /*$$scope, toasts*/ 17) {
update_slot(actions_slot, actions_slot_template, ctx, /*$$scope*/ ctx[4], dirty, get_actions_slot_changes, get_actions_slot_context);
}
}
if (!current || dirty & /*classes*/ 2 && div2_class_value !== (div2_class_value = "svelte-toasty-progress " + (/*classes*/ ctx[1].progress ?? ""))) {
attr(div2, "class", div2_class_value);
}
if (!current || dirty & /*toasts*/ 1) {
set_style(div2, "animation-duration", /*toast*/ ctx[11].timeout + "ms");
}
if (!current || dirty & /*toasts, classes*/ 3 && li_class_value !== (li_class_value = "svelte-toasty-toast " + /*toast*/ ctx[11].type + " " + (/*classes*/ ctx[1].toast ?? ""))) {
attr(li, "class", li_class_value);
}
},
i(local) {
if (current) return;
transition_in(icon_slot, local);
transition_in(message_slot_or_fallback, local);
transition_in(actions_slot, local);
add_render_callback(() => {
if (!li_transition) li_transition = create_bidirectional_transition(li, fade, {}, true);
li_transition.run(1);
});
current = true;
},
o(local) {
transition_out(icon_slot, local);
transition_out(message_slot_or_fallback, local);
transition_out(actions_slot, local);
if (!li_transition) li_transition = create_bidirectional_transition(li, fade, {}, false);
li_transition.run(0);
current = false;
},
d(detaching) {
if (detaching) detach(li);
if (icon_slot) icon_slot.d(detaching);
if (message_slot_or_fallback) message_slot_or_fallback.d(detaching);
if (actions_slot) actions_slot.d(detaching);
if (detaching && li_transition) li_transition.end();
mounted = false;
dispose();
}
};
}
function create_fragment(ctx) {
let ul;
let each_blocks = [];
let each_1_lookup = new Map();
let ul_class_value;
let current;
let each_value = /*toasts*/ ctx[0];
const get_key = ctx => /*toast*/ ctx[11].id;
for (let i = 0; i < each_value.length; i += 1) {
let child_ctx = get_each_context(ctx, each_value, i);
let key = get_key(child_ctx);
each_1_lookup.set(key, each_blocks[i] = create_each_block(key, child_ctx));
}
return {
c() {
ul = element("ul");
for (let i = 0; i < each_blocks.length; i += 1) {
each_blocks[i].c();
}
attr(ul, "class", ul_class_value = "svelte-toasty " + (/*classes*/ ctx[1].list ?? ""));
attr(ul, "role", "alert");
},
m(target, anchor) {
insert(target, ul, anchor);
for (let i = 0; i < each_blocks.length; i += 1) {
each_blocks[i].m(ul, null);
}
current = true;
},
p(ctx, [dirty]) {
if (dirty & /*toasts, classes, removeToast, $$scope*/ 23) {
each_value = /*toasts*/ ctx[0];
group_outros();
each_blocks = update_keyed_each(each_blocks, dirty, get_key, 1, ctx, each_value, each_1_lookup, ul, outro_and_destroy_block, create_each_block, null, get_each_context);
check_outros();
}
if (!current || dirty & /*classes*/ 2 && ul_class_value !== (ul_class_value = "svelte-toasty " + (/*classes*/ ctx[1].list ?? ""))) {
attr(ul, "class", ul_class_value);
}
},
i(local) {
if (current) return;
for (let i = 0; i < each_value.length; i += 1) {
transition_in(each_blocks[i]);
}
current = true;
},
o(local) {
for (let i = 0; i < each_blocks.length; i += 1) {
transition_out(each_blocks[i]);
}
current = false;
},
d(detaching) {
if (detaching) detach(ul);
for (let i = 0; i < each_blocks.length; i += 1) {
each_blocks[i].d();
}
}
};
}
function instance($$self, $$props, $$invalidate) {
let { $$slots: slots = {}, $$scope } = $$props;
let { classes = { list: "", toast: "", progress: "" } } = $$props;
let { timeout = 3000 } = $$props;
let { toasts = [] } = $$props;
let count = 0;
let unsubscribe;
const createToast = ({ timeout: customTimeout, ...args }) => {
$$invalidate(0, toasts = [
{
id: count,
timeout: customTimeout || timeout, // TODO: Calculate timeout based on text
...args
},
...toasts
]);
count = count + 1;
};
unsubscribe = store.subscribe(value => {
if (!value) return;
createToast(value);
});
onDestroy(unsubscribe);
const removeToast = id => {
$$invalidate(0, toasts = toasts.filter(t => t.id != id));
};
const func = toast => removeToast(toast.id);
const animationend_handler = toast => removeToast(toast.id);
$$self.$$set = $$props => {
if ("classes" in $$props) $$invalidate(1, classes = $$props.classes);
if ("timeout" in $$props) $$invalidate(3, timeout = $$props.timeout);
if ("toasts" in $$props) $$invalidate(0, toasts = $$props.toasts);
if ("$$scope" in $$props) $$invalidate(4, $$scope = $$props.$$scope);
};
return [
toasts,
classes,
removeToast,
timeout,
$$scope,
slots,
func,
animationend_handler
];
}
class Toast_container extends SvelteComponent {
constructor(options) {
super();
if (!document.getElementById("svelte-11d8vkt-style")) add_css();
init(this, options, instance, create_fragment, safe_not_equal, { classes: 1, timeout: 3, toasts: 0 });
}
}
function toast({type = "default", timeout, ...args}) {
store.set({ type, timeout, ...args});
return new Promise((resolve) => setTimeout(resolve, timeout));
}
function danger(text, timeout) {
return toast({text, type: "danger", timeout});
}
function warning(text, timeout) {
return toast({text, type: "warning", timeout});
}
function info(text, timeout) {
return toast({text, type: "info", timeout});
}
function success(text, timeout) {
return toast({text, type: "success", timeout});
}
export { Toast_container as ToastContainer, danger, info, success, toast, warning };