UNPKG

@base-framework/ui

Version:

This is a UI package that adds components and atoms that use Tailwind CSS and a theme based on Shadcn.

834 lines (833 loc) 25.1 kB
import { Div as h, Img as _, Span as H, Button as L, OnState as J, On as z } from "@base-framework/atoms"; import { Component as O, Data as B, Atom as j, DateTime as T } from "@base-framework/base"; import { B as f } from "./buttons-Cm9etaEG.js"; import { Icons as P } from "./icons.es.js"; const U = (n, t) => { const e = n ? n.getBoundingClientRect() : { top: 0, bottom: 0, left: 0 }, a = t.getBoundingClientRect(), s = 10, r = globalThis.scrollX, c = globalThis.scrollY; let i = e.left + r, d = e.bottom + c; const g = globalThis.innerHeight - e.bottom, m = e.top; return i + a.width > globalThis.innerWidth && (i = globalThis.innerWidth - a.width - s), g < a.height && m > g ? d = e.top + c - a.height - s : g < a.height && (d = e.bottom + c - (a.height - g) - s), { x: i, y: d }; }; class Pt extends O { /** * This will set up the data. * * @returns {object} */ setData() { const t = this.parent.data || new B(); return t.set({ position: { x: 0, y: 0 } }), t; } /** * This will get the class size. * * @returns {string} */ getSize() { switch (this.size || "lg") { // @ts-ignore case "sm": return "w-48"; // @ts-ignore case "md": return "w-64"; case "lg": return "w-[250px]"; // @ts-ignore case "xl": return "w-96"; // @ts-ignore case "2xl": return "w-[400px]"; // @ts-ignore case "fit": return "w-fit"; // @ts-ignore case "full": return "w-full"; } } /** * This will render the modal component. * * @returns {object} */ render() { const t = this.getSize(); return h({ class: `absolute inset-auto fadeIn mt-2 rounded-md p-0 shadow-lg bg-popover min-h-12 backdrop:bg-transparent text-inherit r z-30 ${t}`, popover: "manual", toggle: (e, { state: a }) => e.newState === "closed" ? a.open = !1 : null, style: "top: [[position.y]]px; left: [[position.x]]px" }, this.children); } /** * This will setup the states. * * @returns {object} */ setupStates() { return { open: { id: this.parent.getId(), callBack: (a) => { this.state.open === !1 && this.destroy(); } } }; } /** * Updates the dropdown position. * * @returns {void} */ updatePosition() { const t = this.button ?? null, e = this.panel, a = U(t, e); this.data.position = a; } /** * This will run after the setup. * * @returns {void} */ afterSetup() { this.panel.showPopover(), this.updatePosition(); } /** * This will check if the element clicked was in the * component of the button. * * @param {object} element * @returns {boolean} */ isOutsideClick(t) { return !this.panel.contains(t) && this.button && !this.button.contains(t); } /** * This will set up the events. * * @returns {array} */ setupEvents() { return [ ["click", document, (t) => { this.isOutsideClick(t.target) && (this.state.open = !1); }], ["resize", globalThis, (t) => this.updatePosition()], ["scroll", document, (t) => this.updatePosition()] ]; } /** * This will override the set up to use the app shell. * * @param {object} container */ setContainer(t) { this.container = app.root; } /** * This will hide the popover before destroying. * * @returns {void} */ beforeDestroy() { this.panel.hidePopover(); } } const q = j(({ src: n, alt: t }) => n ? _({ class: "absolute w-full h-full rounded-full object-cover fadeIn", src: n, alt: t, /** * If there's an error loading the image, hide it. */ error: (e) => e.target.style.display = "none" }) : null), K = (n) => n && (Array.isArray(n) && (n = n.join(" ")), typeof n != "string" && (n = String(n)), n.split(" ").map((t) => t.charAt(0)).join("").toUpperCase()), G = (n) => !n || n.length < 2 ? n : K(n), Q = (n) => H([n, (t, e) => { e.textContent = G(t); }]), A = { xs: "h-6 w-6", sm: "h-8 w-8", md: "h-12 w-12", lg: "h-16 w-16", xl: "h-24 w-24", "2xl": "h-32 w-32", "3xl": "h-48 w-48", "4xl": "h-64 w-64", default: "h-12 w-12" }, I = { xs: "text-[7px]", sm: "text-xs", md: "text-base", lg: "text-xl", xl: "text-2xl", "2xl": "text-3xl", "3xl": "text-4xl", "4xl": "text-5xl", default: "text-base" }, V = (n) => A[n] || A.default, Z = (n) => I[n] || I.default, tt = (n, t = null, e = "md") => { const a = G(n), s = Z(e); return h( { class: ` flex items-center justify-center w-full h-full rounded-full bg-muted text-muted-foreground font-medium ${s} ` }, [ t ? Q(t) : H({ class: "uppercase" }, a) ] ); }, Tt = j(({ src: n, alt: t, fallbackText: e, watcherFallback: a, size: s }) => { const r = V(s); return h( { class: `relative flex items-center justify-center ${r}` }, [ q({ src: n, alt: t }), tt(e, a, s) ] ); }), et = ({ currentMonth: n, currentYear: t, onSelect: e }) => h( { class: "grid grid-cols-3 gap-2" }, T.monthNames.map( (a, s) => f( { click: (r) => { r.preventDefault(), r.stopPropagation(), e(s); }, variant: n === s ? "primary" : "ghost", "aria-label": `Select ${a} ${t}` }, a.substring(0, 3) ) ) ), D = (n) => (n *= 1, n < 10 ? `0${n}` : String(n)), nt = (n) => (n.indexOf("T") === -1 && n.indexOf(" ") === -1 && (n += "T00:00:01"), n.replace(" ", "T"), n), w = (n, t, e) => `${n}-${D(t + 1)}-${D(e)}`, at = (n) => n ? "bg-accent text-primary" : "", st = (n) => n ? "text-muted-foreground opacity-50" : "", rt = (n, t) => n === t, ot = (n, t) => rt(n, t) ? "bg-primary text-primary-foreground" : "", it = (n, t, e, a) => { const s = ot(t, a); return s || (n ? at(n) : e ? st(e) : "text-foreground"); }, N = (n) => { const { day: t, currentDate: e, date: a, isToday: s, isOutsideMonth: r, select: c, disabled: i } = n; return L( { class: ` flex items-center justify-center h-9 w-auto p-0 font-normal text-sm rounded-md ${it(s, e, r, a)} hover:bg-muted/50 hover:text-muted-foreground focus:z-10 disabled:pointer-events-none disabled:opacity-50 `, disabled: i || t === null, "aria-label": t ? `Day ${t}` : null, // Only call select if it's not disabled. click: () => !i && c(a) }, t.toString() ); }, Y = (n, t, e, a) => n === a.date && t === a.month && e === a.year, ct = (n, t, e, a = !1) => { const { year: s, month: r } = n, c = w(s, r, n.date), i = new Date(s, r, 1).getDay(), d = new Date(s, r + 1, 0).getDate(), g = new Date(s, r, 0).getDate(), m = [], C = r === 0 ? 11 : r - 1, v = r === 0 ? s - 1 : s; for (let o = i - 1; o >= 0; o--) { const u = g - o, p = Y(u, C, v, t), y = new Date(v, C, u) < new Date(t.year, t.month, t.date), x = a && y; m.push( N({ day: u, currentDate: c, date: w(v, C, u), isToday: p, isOutsideMonth: !0, select: e, disabled: x }) ); } for (let o = 1; o <= d; o++) { const u = Y(o, r, s, t), p = new Date(s, r, o) < new Date(t.year, t.month, t.date), y = a && p; m.push( N({ day: o, currentDate: c, date: w(s, r, o), isToday: u, isOutsideMonth: !1, select: e, disabled: y }) ); } const b = r === 11 ? 0 : r + 1, $ = r === 11 ? s + 1 : s, l = (7 - m.length % 7) % 7; for (let o = 1; o <= l; o++) { const u = Y(o, b, $, t), p = new Date($, b, o) < new Date(t.year, t.month, t.date), y = a && p; m.push( N({ day: o, currentDate: c, date: w($, b, o), isToday: u, isOutsideMonth: !0, select: e, disabled: y }) ); } return m; }, R = ({ label: n, click: t }) => f( { class: ` inline-flex items-center justify-center h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100 text-muted-foreground absolute ${n === "Previous" ? "left-1" : "right-1"} focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 `, click: t, "aria-label": `${n} month`, variant: "icon", icon: n === "Previous" ? P.chevron.single.left : P.chevron.single.right } ), lt = ({ onMonthClick: n, onYearClick: t, next: e, previous: a }) => h({ class: "flex items-center justify-center space-x-2 relative min-h-12 text-sm font-medium" }, [ f({ click: n, variant: "ghost", "aria-label": "Select month" }, "[[monthName]]"), f({ click: t, variant: "ghost", "aria-label": "Select year" }, "[[current.year]]"), R({ label: "Previous", click: a }), R({ label: "Next", click: e }) ]), ht = (n) => h( { class: "flex items-center justify-center h-9 w-auto text-[0.8rem] font-normal text-muted-foreground" }, n ), ut = () => ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"].map(ht), dt = (n) => h({ class: "rdp w-full space-y-1" }, [ lt({ onMonthClick: n.onMonthClick, onYearClick: n.onYearClick, next: n.next, previous: n.previous }), h({ class: "flex flex-auto flex-col w-full", onSet: [ "currentDate", () => [ h({ class: "grid grid-cols-7" }, ut()), h( { class: "grid grid-cols-7" }, ct( n.current, n.today, n.select, n.blockPriorDates ) ) ] ] }) ]), gt = ({ currentMonth: n, currentYear: t, onSelect: e }) => { const a = t - 50, s = Array.from({ length: 101 }, (r, c) => a + c); return h( { class: "grid grid-cols-4 gap-2 overflow-y-auto max-h-72" }, s.map( (r) => f( { click: (c) => { c.preventDefault(), c.stopPropagation(), e(r); }, variant: r === t ? "primary" : "ghost", "aria-label": `Select ${r}` }, r.toString() ) ) ); }; class Nt extends O { /** * This will declare the props for the compiler. * * @returns {void} */ declareProps() { this.selectedDate = null, this.selectedCallBack = null, this.blockPriorDates = !1; } /** * This will get the selected data. * * @param {object} today * @returns {Date} */ getSelectedDate(t) { const e = this.selectedDate ? new Date(nt(this.selectedDate)) : t; return new Date(e.getFullYear(), e.getMonth(), e.getDate()); } /** * This will set up the data for the calendar. * * @returns {Data} */ setData() { const t = /* @__PURE__ */ new Date(), e = this.getSelectedDate(t), a = e.getMonth(); return new B({ monthName: this.getMonthName(a), currentDate: `${e.getFullYear()}-${a + 1}-${e.getDate()}`, current: { date: e.getDate(), year: e.getFullYear(), month: a }, today: { date: t.getDate(), month: t.getMonth(), year: t.getFullYear() } }); } /** * This will set up the states for the calendar. * * @return {object} */ setupStates() { return { view: "calendar" // 'calendar' | 'months' | 'years' }; } /** * This will get the name of the month. * * @param {number} month * @returns {string} */ getMonthName(t) { return T.monthNames[t]; } /** * This will go to the previous month. * * @returns {void} */ goToPreviousMonth() { const t = this.data; let e = t.current.month, a = t.current.year; e === 0 ? (e = 11, a--) : e--, this.setCurrentDate(e, a); } /** * This will go to the next month. * * @returns {void} */ goToNextMonth() { const t = this.data; let e = t.current.month, a = t.current.year; e === 11 ? (e = 0, a++) : e++, this.setCurrentDate(e, a); } /** * This will set the current month and year. * * @param {number} month * @param {number} year * @param {number} [date=null] * @returns {void} */ setCurrentDate(t, e, a = null) { const s = this.data; s.current.month = t, s.current.year = e, typeof a == "number" && (s.current.date = D(a)), s.currentDate = `${e}-${D(t + 1)}-${s.current.date}`, s.monthName = this.getMonthName(t); } /** * This will select a date. * * @param {string} date * @returns {void} */ selectDate(t) { const e = /* @__PURE__ */ new Date(t + "T00:00:00"); this.setCurrentDate(e.getMonth(), e.getFullYear(), e.getDate()), typeof this.selectedCallBack == "function" && this.selectedCallBack(this.data.currentDate); } /** * This will render the calendar. * * @returns {object} */ render() { return h({ class: "calendar-container p-3 rounded-md border min-w-80" }, [ J("view", (t) => { switch (t) { case "months": return et( { currentMonth: this.data.current.month, currentYear: this.data.current.year, onSelect: (e) => { this.setCurrentDate(e, this.data.current.year), this.state.view = "calendar"; } } ); case "years": return gt( { currentMonth: this.data.current.month, currentYear: this.data.current.year, onSelect: (e) => { this.setCurrentDate(this.data.current.month, e), this.state.view = "calendar"; } } ); default: return dt({ current: this.data.current, today: this.data.today, select: (e) => this.selectDate(e), next: () => this.goToNextMonth(), previous: () => this.goToPreviousMonth(), blockPriorDates: this.blockPriorDates || !1, onMonthClick: (e) => { e.preventDefault(), e.stopPropagation(), this.state.view = "months"; }, onYearClick: (e) => { e.preventDefault(), e.stopPropagation(), this.state.view = "years"; } }); } }) ]); } } const W = ({ label: n, click: t }) => f( { class: ` inline-flex items-center justify-center h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100 text-muted-foreground absolute ${n === "Previous" ? "left-1" : "right-1"} focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 `, click: t, "aria-label": `${n} month`, variant: "icon", icon: n === "Previous" ? P.chevron.single.left : P.chevron.single.right } ), mt = ({ onMonthClick: n, onYearClick: t, next: e, previous: a }) => h({ class: "flex items-center justify-center space-x-2 relative min-h-12 text-sm font-medium" }, [ f({ click: n, variant: "ghost", "aria-label": "Select month" }, "[[monthName]]"), f({ click: t, variant: "ghost", "aria-label": "Select year" }, "[[current.year]]"), W({ label: "Previous", click: a }), W({ label: "Next", click: e }) ]), ft = (n) => h({ class: "h-9 flex items-center justify-center font-medium" }, n), pt = () => h( { class: "grid grid-cols-7 gap-1 text-center text-xs font-medium text-muted-foreground mb-2" }, ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"].map(ft) ), yt = ({ day: n, iso: t, disabled: e, isStart: a, isEnd: s, isBetween: r, isOtherMonth: c, click: i }) => { let d = "ghost", g = ""; return a || s ? (d = "default", g = "bg-primary text-primary-foreground hover:bg-primary/90") : r && (g = "bg-accent text-accent-foreground hover:bg-accent/80"), e && (g += " opacity-50 cursor-not-allowed"), c && (g += " text-muted-foreground opacity-50"), f( { class: `flex items-center justify-center p-0 text-sm font-medium rounded-md transition-colors ${g}`, variant: d, disabled: e, click: (m) => { m.preventDefault(), m.stopPropagation(), e || i(); } }, n.toString() ); }, Dt = ({ today: n, current: t, blockPriorDates: e, onDateClick: a }) => { const { start: s, end: r } = t, c = new Date(t.year, t.month, 1).getDay(), i = new Date(t.year, t.month + 1, 0).getDate(), d = [], g = t.month === 0 ? 11 : t.month - 1, m = t.month === 0 ? t.year - 1 : t.year, C = new Date(m, g + 1, 0).getDate(), v = t.month === 11 ? 0 : t.month + 1, b = t.month === 11 ? t.year + 1 : t.year; for (let l = c - 1; l >= 0; l--) { const o = C - l, u = w(m, g, o), p = new Date(m, g, o), y = new Date(n.year, n.month, n.date), x = p < y, M = e && x, S = s === u, k = r === u, X = s && r && u > s && u < r; d.push({ day: o, iso: u, disabled: M, isStart: S, isEnd: k, isBetween: X, isOtherMonth: !0, click: () => a(u) }); } for (let l = 1; l <= i; l++) { const o = w(t.year, t.month, l), u = new Date(t.year, t.month, l), p = new Date(n.year, n.month, n.date), y = u < p, x = e && y, M = s === o, S = r === o, k = s && r && o > s && o < r; d.push({ day: l, iso: o, disabled: x, isStart: M, isEnd: S, isBetween: k, isOtherMonth: !1, click: () => { a(o); } }); } const F = (7 - d.length % 7) % 7; for (let l = 1; l <= F; l++) { const o = w(b, v, l), u = new Date(b, v, l), p = new Date(n.year, n.month, n.date), y = u < p, x = e && y, M = s === o, S = r === o, k = s && r && o > s && o < r; d.push({ day: l, iso: o, disabled: x, isStart: M, isEnd: S, isBetween: k, isOtherMonth: !0, click: () => a(o) }); } return h( { class: "grid grid-cols-7 gap-1" }, d.map( (l, o) => yt({ day: l.day, iso: l.iso, disabled: l.disabled, isStart: l.isStart, isEnd: l.isEnd, isBetween: l.isBetween, isOtherMonth: l.isOtherMonth, click: l.click }) ) ); }, xt = j((n, t) => h({ class: "flex flex-auto flex-col" }, [ mt({ onMonthClick: n.onMonthClick, onYearClick: n.onYearClick, next: n.next, previous: n.previous }), h({ class: "flex flex-auto flex-col w-full", onSet: [ "currentDate", () => [ pt(), Dt({ today: n.today, current: n.current, blockPriorDates: n.blockPriorDates, onDateClick: n.onDateClick }) ] ] }) ])), wt = ({ currentMonth: n, currentYear: t, onSelect: e }) => h( { class: "grid grid-cols-3 gap-2" }, T.monthNames.map( (a, s) => f( { click: (r) => { r.preventDefault(), r.stopPropagation(), e(s); }, variant: n === s ? "primary" : "ghost", "aria-label": `Select ${a} ${t}` }, a.substring(0, 3) ) ) ), vt = ({ start: n, end: t, selecting: e, onSelectStart: a, onSelectEnd: s }) => h({ class: "flex space-x-2 mb-4" }, [ z("selecting", (r) => f({ click: a, variant: r === "start" ? "primary" : "outline", class: "flex-1" }, [ h({ class: "font-medium" }, E("current.start", "Start Date")) ])), z("selecting", (r) => f({ click: s, variant: r === "end" ? "primary" : "outline", class: "flex-1" }, [ h({ class: "font-medium" }, E("current.end", "End Date")) ])) ]), E = (n, t) => [`[[${n}]]`, (e) => e == null ? t : bt(e)], bt = (n) => { const t = /* @__PURE__ */ new Date(n + "T00:00:00"), e = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], a = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; return `${e[t.getDay()]}, ${t.getDate()} ${a[t.getMonth()]}`; }, Ct = ({ currentYear: n, onSelect: t }) => { const e = n - 50, a = Array.from({ length: 101 }, (s, r) => e + r); return h( { class: "grid grid-cols-4 gap-2 overflow-y-auto max-h-72" }, a.map( (s) => f( { click: (r) => { r.preventDefault(), r.stopPropagation(), t(s); }, variant: s === n ? "primary" : "ghost", "aria-label": `Select ${s}` }, s.toString() ) ) ); }; class Yt extends O { /** * Declare compiler props. * * @returns {void} */ declareProps() { this.startDate = null, this.endDate = null, this.onRangeSelect = null, this.blockPriorDates = !1; } /** * Initialize reactive data for today and current view. * * @returns {Data} */ setData() { const t = /* @__PURE__ */ new Date(), e = this.startDate ? /* @__PURE__ */ new Date(this.startDate + "T00:00:00") : t, a = e.getMonth(), s = e.getFullYear(); return new B({ today: { date: t.getDate(), month: t.getMonth(), year: t.getFullYear() }, monthName: this.getMonthName(a), currentData: `${e.getFullYear()}-${D(e.getMonth() + 1)}-${D(e.getDate())}`, current: { date: t.getDate(), month: a, year: s, start: this.startDate, end: this.endDate }, selecting: "start" }); } /** * This will get the name of the month. * * @param {number} month * @returns {string} */ getMonthName(t) { return T.monthNames[t]; } /** * Initialize component state. * * @returns {{view:string}} */ setupStates() { return { view: "calendar" // 'calendar', 'months', or 'years' }; } /** * Handle a date cell click. * * @param {string} isoDate * @returns {void} */ handleClick(t) { if (this.data.selecting === "start") { this.data.current.start = t, this.data.current.end = null, this.data.selecting = "end"; const e = /* @__PURE__ */ new Date(t + "T00:00:00"); this.setCurrent({ month: e.getMonth(), year: e.getFullYear(), date: e.getDate() }); } else { this.data.current.start && t < this.data.current.start ? (this.data.current.end = this.data.current.start, this.data.current.start = t) : this.data.current.end = t, this.data.selecting = "start"; const e = /* @__PURE__ */ new Date(t + "T00:00:00"); this.setCurrent({ month: e.getMonth(), year: e.getFullYear(), date: e.getDate() }), typeof this.onRangeSelect == "function" && this.onRangeSelect(this.data.current.start, this.data.current.end); } } /** * Update current month/year in data. * * @param {object} obj * @returns {void} */ setCurrent({ month: t, year: e, date: a = null }) { const s = this.data.current; s.month = (t + 12) % 12, s.year = e + (t < 0 ? -1 : t > 11 ? 1 : 0), a !== null && (s.date = a), this.data.monthName = this.getMonthName(s.month), this.data.currentDate = `${e}-${D(t + 1)}-${D(s.date)}`; } /** * Render the range calendar. * * @returns {object} */ render() { const { today: t, current: e, selecting: a } = this.data, { start: s, end: r } = e; return h({ class: "range-calendar bg-background border border-border rounded-lg shadow-md p-4 w-full max-w-sm min-w-80" }, [ vt({ start: s, end: r, selecting: a, onSelectStart: (c) => { c.preventDefault(), c.stopPropagation(), this.data.selecting = "start"; }, onSelectEnd: (c) => { c.preventDefault(), c.stopPropagation(), this.data.selecting = "end"; } }), J("view", (c) => { switch (c) { case "months": return wt( { currentMonth: e.month, currentYear: e.year, onSelect: (i) => { this.setCurrent({ month: i, year: e.year }), this.state.view = "calendar"; } } ); case "years": return Ct( { currentYear: e.year, onSelect: (i) => { this.setCurrent({ month: e.month, year: i }), this.state.view = "calendar"; } } ); default: return xt({ monthName: this.data.monthName, year: e.year, today: t, current: e, blockPriorDates: this.blockPriorDates, onDateClick: (i) => this.handleClick(i), onMonthClick: (i) => { i.preventDefault(), i.stopPropagation(), this.state.view = "months"; }, onYearClick: (i) => { i.preventDefault(), i.stopPropagation(), this.state.view = "years"; }, next: () => { const i = this.data.current, d = i.month === 11 ? { month: 0, year: i.year + 1 } : { month: i.month + 1, year: i.year }; this.setCurrent(d); }, previous: () => { const i = this.data.current, d = i.month === 0 ? { month: 11, year: i.year - 1 } : { month: i.month - 1, year: i.year }; this.setCurrent(d); } }); } }) ]); } } export { Tt as A, ct as C, N as D, w as F, dt as M, Pt as P, Yt as R, ht as a, Nt as b, nt as c, U as g, D as p };