|
import "dequal"; |
|
import { d as derived, w as writable, r as readable } from "./index2.js"; |
|
import { o as onDestroy, r as get_store_value } from "./ssr.js"; |
|
import { o as onMount } from "./ssr2.js"; |
|
function styleToString(style) { |
|
return Object.keys(style).reduce((str, key) => { |
|
if (style[key] === void 0) |
|
return str; |
|
return str + `${key}:${style[key]};`; |
|
}, ""); |
|
} |
|
function disabledAttr(disabled) { |
|
return disabled ? true : void 0; |
|
} |
|
({ |
|
type: "hidden", |
|
"aria-hidden": true, |
|
hidden: true, |
|
tabIndex: -1, |
|
style: styleToString({ |
|
position: "absolute", |
|
opacity: 0, |
|
"pointer-events": "none", |
|
margin: 0, |
|
transform: "translateX(-100%)" |
|
}) |
|
}); |
|
function portalAttr(portal) { |
|
if (portal !== null) { |
|
return ""; |
|
} |
|
return void 0; |
|
} |
|
function lightable(value) { |
|
function subscribe(run) { |
|
run(value); |
|
return () => { |
|
}; |
|
} |
|
return { subscribe }; |
|
} |
|
const hiddenAction = (obj) => { |
|
return new Proxy(obj, { |
|
get(target, prop, receiver) { |
|
return Reflect.get(target, prop, receiver); |
|
}, |
|
ownKeys(target) { |
|
return Reflect.ownKeys(target).filter((key) => key !== "action"); |
|
} |
|
}); |
|
}; |
|
const isFunctionWithParams = (fn) => { |
|
return typeof fn === "function"; |
|
}; |
|
makeElement("empty"); |
|
function makeElement(name, args) { |
|
const { stores, action, returned } = args ?? {}; |
|
const derivedStore = (() => { |
|
if (stores && returned) { |
|
return derived(stores, (values) => { |
|
const result = returned(values); |
|
if (isFunctionWithParams(result)) { |
|
const fn = (...args2) => { |
|
return hiddenAction({ |
|
...result(...args2), |
|
[`data-melt-${name}`]: "", |
|
action: action ?? noop |
|
}); |
|
}; |
|
fn.action = action ?? noop; |
|
return fn; |
|
} |
|
return hiddenAction({ |
|
...result, |
|
[`data-melt-${name}`]: "", |
|
action: action ?? noop |
|
}); |
|
}); |
|
} else { |
|
const returnedFn = returned; |
|
const result = returnedFn?.(); |
|
if (isFunctionWithParams(result)) { |
|
const resultFn = (...args2) => { |
|
return hiddenAction({ |
|
...result(...args2), |
|
[`data-melt-${name}`]: "", |
|
action: action ?? noop |
|
}); |
|
}; |
|
resultFn.action = action ?? noop; |
|
return lightable(resultFn); |
|
} |
|
return lightable(hiddenAction({ |
|
...result, |
|
[`data-melt-${name}`]: "", |
|
action: action ?? noop |
|
})); |
|
} |
|
})(); |
|
const actionFn = action ?? (() => { |
|
}); |
|
actionFn.subscribe = derivedStore.subscribe; |
|
return actionFn; |
|
} |
|
function createElHelpers(prefix) { |
|
const name = (part) => part ? `${prefix}-${part}` : prefix; |
|
const attribute = (part) => `data-melt-${prefix}${part ? `-${part}` : ""}`; |
|
const selector = (part) => `[data-melt-${prefix}${part ? `-${part}` : ""}]`; |
|
const getEl = (part) => document.querySelector(selector(part)); |
|
return { |
|
name, |
|
attribute, |
|
selector, |
|
getEl |
|
}; |
|
} |
|
const isBrowser = typeof document !== "undefined"; |
|
const isFunction = (v) => typeof v === "function"; |
|
function isElement(element) { |
|
return element instanceof Element; |
|
} |
|
function isHTMLElement(element) { |
|
return element instanceof HTMLElement; |
|
} |
|
function isElementDisabled(element) { |
|
const ariaDisabled = element.getAttribute("aria-disabled"); |
|
const disabled = element.getAttribute("disabled"); |
|
const dataDisabled = element.hasAttribute("data-disabled"); |
|
if (ariaDisabled === "true" || disabled !== null || dataDisabled) { |
|
return true; |
|
} |
|
return false; |
|
} |
|
function isObject(value) { |
|
return value !== null && typeof value === "object"; |
|
} |
|
function isReadable(value) { |
|
return isObject(value) && "subscribe" in value; |
|
} |
|
function executeCallbacks(...callbacks) { |
|
return (...args) => { |
|
for (const callback of callbacks) { |
|
if (typeof callback === "function") { |
|
callback(...args); |
|
} |
|
} |
|
}; |
|
} |
|
function noop() { |
|
} |
|
function addEventListener(target, event, handler, options) { |
|
const events = Array.isArray(event) ? event : [event]; |
|
events.forEach((_event) => target.addEventListener(_event, handler, options)); |
|
return () => { |
|
events.forEach((_event) => target.removeEventListener(_event, handler, options)); |
|
}; |
|
} |
|
function addMeltEventListener(target, event, handler, options) { |
|
const events = Array.isArray(event) ? event : [event]; |
|
if (typeof handler === "function") { |
|
const handlerWithMelt = withMelt((_event) => handler(_event)); |
|
events.forEach((_event) => target.addEventListener(_event, handlerWithMelt, options)); |
|
return () => { |
|
events.forEach((_event) => target.removeEventListener(_event, handlerWithMelt, options)); |
|
}; |
|
} |
|
return () => noop(); |
|
} |
|
function dispatchMeltEvent(originalEvent) { |
|
const node = originalEvent.currentTarget; |
|
if (!isHTMLElement(node)) |
|
return null; |
|
const customMeltEvent = new CustomEvent(`m-${originalEvent.type}`, { |
|
detail: { |
|
originalEvent |
|
}, |
|
cancelable: true |
|
}); |
|
node.dispatchEvent(customMeltEvent); |
|
return customMeltEvent; |
|
} |
|
function withMelt(handler) { |
|
return (event) => { |
|
const customEvent = dispatchMeltEvent(event); |
|
if (customEvent?.defaultPrevented) |
|
return; |
|
return handler(event); |
|
}; |
|
} |
|
const safeOnMount = (fn) => { |
|
try { |
|
onMount(fn); |
|
} catch { |
|
return fn; |
|
} |
|
}; |
|
const safeOnDestroy = (fn) => { |
|
try { |
|
onDestroy(fn); |
|
} catch { |
|
return fn; |
|
} |
|
}; |
|
function omit(obj, ...keys) { |
|
const result = {}; |
|
for (const key of Object.keys(obj)) { |
|
if (!keys.includes(key)) { |
|
result[key] = obj[key]; |
|
} |
|
} |
|
return result; |
|
} |
|
function withGet(store) { |
|
return { |
|
...store, |
|
get: () => get_store_value(store) |
|
}; |
|
} |
|
withGet.writable = function(initial) { |
|
const internal = writable(initial); |
|
let value = initial; |
|
return { |
|
subscribe: internal.subscribe, |
|
set(newValue) { |
|
internal.set(newValue); |
|
value = newValue; |
|
}, |
|
update(updater) { |
|
const newValue = updater(value); |
|
internal.set(newValue); |
|
value = newValue; |
|
}, |
|
get() { |
|
return value; |
|
} |
|
}; |
|
}; |
|
withGet.derived = function(stores, fn) { |
|
const subscribers = new Map(); |
|
const get = () => { |
|
const values = Array.isArray(stores) ? stores.map((store) => store.get()) : stores.get(); |
|
return fn(values); |
|
}; |
|
const subscribe = (subscriber) => { |
|
const unsubscribers = []; |
|
const storesArr = Array.isArray(stores) ? stores : [stores]; |
|
storesArr.forEach((store) => { |
|
unsubscribers.push(store.subscribe(() => { |
|
subscriber(get()); |
|
})); |
|
}); |
|
subscriber(get()); |
|
subscribers.set(subscriber, unsubscribers); |
|
return () => { |
|
const unsubscribers2 = subscribers.get(subscriber); |
|
if (unsubscribers2) { |
|
for (const unsubscribe of unsubscribers2) { |
|
unsubscribe(); |
|
} |
|
} |
|
subscribers.delete(subscriber); |
|
}; |
|
}; |
|
return { |
|
get, |
|
subscribe |
|
}; |
|
}; |
|
const kbd = { |
|
ALT: "Alt", |
|
ARROW_DOWN: "ArrowDown", |
|
ARROW_LEFT: "ArrowLeft", |
|
ARROW_RIGHT: "ArrowRight", |
|
ARROW_UP: "ArrowUp", |
|
BACKSPACE: "Backspace", |
|
CAPS_LOCK: "CapsLock", |
|
CONTROL: "Control", |
|
DELETE: "Delete", |
|
END: "End", |
|
ENTER: "Enter", |
|
ESCAPE: "Escape", |
|
F1: "F1", |
|
F10: "F10", |
|
F11: "F11", |
|
F12: "F12", |
|
F2: "F2", |
|
F3: "F3", |
|
F4: "F4", |
|
F5: "F5", |
|
F6: "F6", |
|
F7: "F7", |
|
F8: "F8", |
|
F9: "F9", |
|
HOME: "Home", |
|
META: "Meta", |
|
PAGE_DOWN: "PageDown", |
|
PAGE_UP: "PageUp", |
|
SHIFT: "Shift", |
|
SPACE: " ", |
|
TAB: "Tab", |
|
CTRL: "Control", |
|
ASTERISK: "*", |
|
A: "a", |
|
P: "p" |
|
}; |
|
const FIRST_KEYS = [kbd.ARROW_DOWN, kbd.PAGE_UP, kbd.HOME]; |
|
const LAST_KEYS = [kbd.ARROW_UP, kbd.PAGE_DOWN, kbd.END]; |
|
const FIRST_LAST_KEYS = [...FIRST_KEYS, ...LAST_KEYS]; |
|
const SELECTION_KEYS = [kbd.ENTER, kbd.SPACE]; |
|
function effect(stores, fn) { |
|
let cb = void 0; |
|
const destroy = derived(stores, (stores2) => { |
|
cb?.(); |
|
cb = fn(stores2); |
|
}).subscribe(noop); |
|
const unsub = () => { |
|
destroy(); |
|
cb?.(); |
|
}; |
|
safeOnDestroy(unsub); |
|
return unsub; |
|
} |
|
readable(void 0, (set) => { |
|
function clicked(event) { |
|
set(event); |
|
set(void 0); |
|
} |
|
const unsubscribe = addEventListener(document, "pointerup", clicked, { |
|
passive: false, |
|
capture: true |
|
}); |
|
return unsubscribe; |
|
}); |
|
const documentEscapeKeyStore = readable(void 0, (set) => { |
|
function keydown(event) { |
|
if (event && event.key === kbd.ESCAPE) { |
|
set(event); |
|
} |
|
set(void 0); |
|
} |
|
const unsubscribe = addEventListener(document, "keydown", keydown, { |
|
passive: false |
|
}); |
|
return unsubscribe; |
|
}); |
|
const useEscapeKeydown = (node, config = {}) => { |
|
let unsub = noop; |
|
function update(config2 = {}) { |
|
unsub(); |
|
const options = { enabled: true, ...config2 }; |
|
const enabled = isReadable(options.enabled) ? options.enabled : readable(options.enabled); |
|
unsub = executeCallbacks( |
|
|
|
documentEscapeKeyStore.subscribe((e) => { |
|
if (!e || !get_store_value(enabled)) |
|
return; |
|
const target = e.target; |
|
if (!isHTMLElement(target) || target.closest("[data-escapee]") !== node) { |
|
return; |
|
} |
|
e.preventDefault(); |
|
if (options.ignore) { |
|
if (isFunction(options.ignore)) { |
|
if (options.ignore(e)) |
|
return; |
|
} else if (Array.isArray(options.ignore)) { |
|
if (options.ignore.length > 0 && options.ignore.some((ignoreEl) => { |
|
return ignoreEl && target === ignoreEl; |
|
})) |
|
return; |
|
} |
|
} |
|
options.handler?.(e); |
|
}), |
|
effect(enabled, ($enabled) => { |
|
if ($enabled) { |
|
node.dataset.escapee = ""; |
|
} else { |
|
delete node.dataset.escapee; |
|
} |
|
}) |
|
); |
|
} |
|
update(config); |
|
return { |
|
update, |
|
destroy() { |
|
node.removeAttribute("data-escapee"); |
|
unsub(); |
|
} |
|
}; |
|
}; |
|
({ |
|
prefix: "", |
|
disabled: readable(false), |
|
required: readable(false), |
|
name: readable(void 0) |
|
}); |
|
const defaults = { |
|
isDateDisabled: void 0, |
|
isDateUnavailable: void 0, |
|
value: void 0, |
|
preventDeselect: false, |
|
numberOfMonths: 1, |
|
pagedNavigation: false, |
|
weekStartsOn: 0, |
|
fixedWeeks: false, |
|
calendarLabel: "Event Date", |
|
locale: "en", |
|
minValue: void 0, |
|
maxValue: void 0, |
|
disabled: false, |
|
readonly: false, |
|
weekdayFormat: "narrow" |
|
}; |
|
({ |
|
isDateDisabled: void 0, |
|
isDateUnavailable: void 0, |
|
value: void 0, |
|
positioning: { |
|
placement: "bottom" |
|
}, |
|
closeOnEscape: true, |
|
closeOnOutsideClick: true, |
|
onOutsideClick: void 0, |
|
preventScroll: false, |
|
forceVisible: false, |
|
locale: "en", |
|
granularity: void 0, |
|
disabled: false, |
|
readonly: false, |
|
minValue: void 0, |
|
maxValue: void 0, |
|
weekdayFormat: "narrow", |
|
...omit(defaults, "isDateDisabled", "isDateUnavailable", "value", "locale", "disabled", "readonly", "minValue", "maxValue", "weekdayFormat") |
|
}); |
|
export { |
|
FIRST_LAST_KEYS as F, |
|
SELECTION_KEYS as S, |
|
addMeltEventListener as a, |
|
isHTMLElement as b, |
|
createElHelpers as c, |
|
disabledAttr as d, |
|
executeCallbacks as e, |
|
isFunction as f, |
|
isElement as g, |
|
addEventListener as h, |
|
isBrowser as i, |
|
effect as j, |
|
kbd as k, |
|
safeOnMount as l, |
|
makeElement as m, |
|
noop as n, |
|
omit as o, |
|
portalAttr as p, |
|
isElementDisabled as q, |
|
styleToString as s, |
|
useEscapeKeydown as u, |
|
withGet as w |
|
}; |
|
|
|
|