@jay-js/ui
Version:
A library of UI components for Jay JS with Tailwind CSS and daisyUI.
164 lines (163 loc) • 5.75 kB
JavaScript
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _extends() {
_extends = Object.assign || function(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i];
for(var key in source){
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _object_destructuring_empty(o) {
if (o === null || o === void 0) throw new TypeError("Cannot destructure " + o);
return o;
}
function _object_spread(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_define_property(target, key, source[key]);
});
}
return target;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function(sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _object_spread_props(target, source) {
source = source != null ? source : {};
if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function(key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function _object_without_properties(source, excluded) {
if (source == null) return {};
var target = _object_without_properties_loose(source, excluded);
var key, i;
if (Object.getOwnPropertySymbols) {
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
for(i = 0; i < sourceSymbolKeys.length; i++){
key = sourceSymbolKeys[i];
if (excluded.indexOf(key) >= 0) continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
target[key] = source[key];
}
}
return target;
}
function _object_without_properties_loose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for(i = 0; i < sourceKeys.length; i++){
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
import { Toast } from "../components/index.js";
/**
* A hook to create and manage toast notifications
*
* @param props - Configuration options for the toast
* @returns A function that creates and displays a toast notification
* @throws Error if no toast container element is found
*/ export function useToast(_param = {}) {
var props = _extends({}, _object_destructuring_empty(_param));
const toastContainerId = props.toastId || props.for;
const selector = toastContainerId ? `#${toastContainerId}` : ".toast-container";
const toastContainer = document.querySelector(selector);
if (!toastContainer) {
throw new Error(`useToast: No element found for selector: ${selector}`);
}
return (_param)=>{
var { duration, vertical, horizontal, children } = _param, props = _object_without_properties(_param, [
"duration",
"vertical",
"horizontal",
"children"
]);
const toastSettings = toastContainer.dataset;
if (!vertical) {
vertical = toastSettings.vertical || "toast-top";
}
if (!horizontal) {
horizontal = toastSettings.horizontal || "toast-end";
}
if (!duration) {
duration = Number(toastSettings.duration) || 5000;
}
const toastList = toastContainer.querySelectorAll(".toast");
let existingToast = null;
if (toastList.length > 0) {
const toastListArray = Array.from(toastList);
existingToast = toastListArray.find((toastItem)=>{
return toastItem.classList.contains(vertical) && toastItem.classList.contains(horizontal);
}) || null;
}
if (existingToast) {
const currentToast = existingToast;
if (children) {
const currentNode = children;
currentToast.prepend(currentNode);
setTimeout(()=>{
currentNode.remove();
}, duration);
return;
}
}
const newToast = Toast(_object_spread_props(_object_spread({}, props), {
duration,
vertical,
horizontal,
children
}));
if (children) {
const currentNode = children;
setTimeout(()=>{
currentNode.remove();
}, duration);
}
toastContainer.appendChild(newToast);
};
}