svelte-hamburger
Version:
Animated hamburger icon
479 lines (452 loc) • 16.5 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Imgix = factory());
})(this, (function () { 'use strict';
function noop() { }
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 exclude_internal_props(props) {
const result = {};
for (const k in props)
if (k[0] !== '$')
result[k] = props[k];
return result;
}
function append(target, node) {
target.appendChild(node);
}
function append_styles(target, style_sheet_id, styles) {
const append_styles_to = get_root_for_style(target);
if (!append_styles_to.getElementById(style_sheet_id)) {
const style = element('style');
style.id = style_sheet_id;
style.textContent = styles;
append_stylesheet(append_styles_to, style);
}
}
function get_root_for_style(node) {
if (!node)
return document;
const root = node.getRootNode ? node.getRootNode() : node.ownerDocument;
if (root && root.host) {
return root;
}
return node.ownerDocument;
}
function append_stylesheet(node, style) {
append(node.head || node, style);
}
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 toggle_class(element, name, toggle) {
element.classList[toggle ? 'add' : 'remove'](name);
}
let current_component;
function set_current_component(component) {
current_component = component;
}
// TODO figure out if we still want to support
// shorthand events, or if we want to implement
// a real bubbling mechanism
function bubble(component, event) {
const callbacks = component.$$.callbacks[event.type];
if (callbacks) {
// @ts-ignore
callbacks.slice().forEach(fn => fn.call(this, event));
}
}
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);
}
}
const outroing = new Set();
function transition_in(block, local) {
if (block && block.i) {
outroing.delete(block);
block.i(local);
}
}
function mount_component(component, target, anchor, customElement) {
const { fragment, on_mount, on_destroy, after_update } = component.$$;
fragment && fragment.m(target, anchor);
if (!customElement) {
// 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, append_styles, 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: [],
on_disconnect: [],
before_update: [],
after_update: [],
context: new Map(options.context || (parent_component ? parent_component.$$.context : [])),
// everything else
callbacks: blank_object(),
dirty,
skip_bound: false,
root: options.target || parent_component.$$.root
};
append_styles && append_styles($$.root);
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, options.customElement);
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;
}
}
}
/* src/Hamburger.svelte generated by Svelte v3.44.2 */
function add_css(target) {
append_styles(target, "svelte-auzaro", ".hamburger.svelte-auzaro.svelte-auzaro{position:relative;cursor:pointer;height:1rem;width:1.5rem;outline:none !important}.line.svelte-auzaro.svelte-auzaro{position:absolute;background:currentColor;width:100%;height:var(--line-width, 2px);border-radius:var(--line-width, 2px)}.line--first.svelte-auzaro.svelte-auzaro{top:0}.line--middle.svelte-auzaro.svelte-auzaro{top:50%}.line--bottom.svelte-auzaro.svelte-auzaro{top:100%}.open.svelte-auzaro .line.svelte-auzaro{top:50% !important}");
}
// (61:2) {#if !duoLine}
function create_if_block(ctx) {
let span;
let span_style_value;
return {
c() {
span = element("span");
attr(span, "class", "line line--middle svelte-auzaro");
attr(span, "style", span_style_value = "transition: " + /*lineTransition*/ ctx[2] + "; " + (/*open*/ ctx[0] ? 'transform: rotate(45deg)' : ''));
},
m(target, anchor) {
insert(target, span, anchor);
},
p(ctx, dirty) {
if (dirty & /*lineTransition, open*/ 5 && span_style_value !== (span_style_value = "transition: " + /*lineTransition*/ ctx[2] + "; " + (/*open*/ ctx[0] ? 'transform: rotate(45deg)' : ''))) {
attr(span, "style", span_style_value);
}
},
d(detaching) {
if (detaching) detach(span);
}
};
}
function create_fragment(ctx) {
let div;
let span0;
let span0_style_value;
let t0;
let t1;
let span1;
let span1_style_value;
let div_class_value;
let mounted;
let dispose;
let if_block = !/*duoLine*/ ctx[1] && create_if_block(ctx);
return {
c() {
div = element("div");
span0 = element("span");
t0 = space();
if (if_block) if_block.c();
t1 = space();
span1 = element("span");
attr(span0, "class", "line line--first svelte-auzaro");
attr(span0, "style", span0_style_value = "transition: " + /*lineTransition*/ ctx[2] + "; " + (/*open*/ ctx[0] ? 'transform: rotate(45deg)' : ''));
attr(span1, "class", "line line--bottom svelte-auzaro");
attr(span1, "style", span1_style_value = "transition: " + /*lineTransition*/ ctx[2] + "; " + (/*open*/ ctx[0] ? 'transform: rotate(-45deg)' : ''));
attr(div, "class", div_class_value = "hamburger " + (/*$$props*/ ctx[3].class || '') + " svelte-auzaro");
toggle_class(div, "open", /*open*/ ctx[0]);
},
m(target, anchor) {
insert(target, div, anchor);
append(div, span0);
append(div, t0);
if (if_block) if_block.m(div, null);
append(div, t1);
append(div, span1);
if (!mounted) {
dispose = [
listen(div, "click", /*click_handler*/ ctx[4]),
listen(div, "keydown", /*keydown_handler*/ ctx[5]),
listen(div, "keypress", /*keypress_handler*/ ctx[6])
];
mounted = true;
}
},
p(ctx, [dirty]) {
if (dirty & /*lineTransition, open*/ 5 && span0_style_value !== (span0_style_value = "transition: " + /*lineTransition*/ ctx[2] + "; " + (/*open*/ ctx[0] ? 'transform: rotate(45deg)' : ''))) {
attr(span0, "style", span0_style_value);
}
if (!/*duoLine*/ ctx[1]) {
if (if_block) {
if_block.p(ctx, dirty);
} else {
if_block = create_if_block(ctx);
if_block.c();
if_block.m(div, t1);
}
} else if (if_block) {
if_block.d(1);
if_block = null;
}
if (dirty & /*lineTransition, open*/ 5 && span1_style_value !== (span1_style_value = "transition: " + /*lineTransition*/ ctx[2] + "; " + (/*open*/ ctx[0] ? 'transform: rotate(-45deg)' : ''))) {
attr(span1, "style", span1_style_value);
}
if (dirty & /*$$props*/ 8 && div_class_value !== (div_class_value = "hamburger " + (/*$$props*/ ctx[3].class || '') + " svelte-auzaro")) {
attr(div, "class", div_class_value);
}
if (dirty & /*$$props, open*/ 9) {
toggle_class(div, "open", /*open*/ ctx[0]);
}
},
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(div);
if (if_block) if_block.d();
mounted = false;
run_all(dispose);
}
};
}
function instance($$self, $$props, $$invalidate) {
let lineTransition;
let { open } = $$props;
let { duoLine } = $$props;
const EASING = `cubic-bezier(0.4, 0, 0.2, 1)`;
function click_handler(event) {
bubble.call(this, $$self, event);
}
function keydown_handler(event) {
bubble.call(this, $$self, event);
}
function keypress_handler(event) {
bubble.call(this, $$self, event);
}
$$self.$$set = $$new_props => {
$$invalidate(3, $$props = assign(assign({}, $$props), exclude_internal_props($$new_props)));
if ('open' in $$new_props) $$invalidate(0, open = $$new_props.open);
if ('duoLine' in $$new_props) $$invalidate(1, duoLine = $$new_props.duoLine);
};
$$self.$$.update = () => {
if ($$self.$$.dirty & /*open*/ 1) {
$$invalidate(2, lineTransition = open
? `top 300ms 50ms ${EASING}, transform 300ms 350ms ${EASING}`
: `top 300ms 350ms ${EASING}, transform 300ms 50ms ${EASING}`);
}
};
$$props = exclude_internal_props($$props);
return [
open,
duoLine,
lineTransition,
$$props,
click_handler,
keydown_handler,
keypress_handler
];
}
class Hamburger extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, { open: 0, duoLine: 1 }, add_css);
}
}
return Hamburger;
}));