@stacksjs/components 0.2.347 → 0.2.348
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Calendar-f3nzpc2y.stx +320 -0
- package/dist/DateRangePicker-7vc2rvc2.stx +218 -0
- package/dist/EmptyState-s790dbym.stx +57 -0
- package/dist/ThemeToggle-4wm9zare.stx +155 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +15 -9
- package/dist/ui/date-range-picker/index.d.ts +22 -0
- package/dist/ui/empty-state/index.d.ts +10 -0
- package/dist/ui/theme-toggle/index.d.ts +10 -0
- package/dist/utils/highlighter.js +7 -1
- package/package.json +3 -3
- package/src/index.ts +3 -0
- package/src/ui/calendar/Calendar.stx +146 -15
- package/src/ui/date-range-picker/DateRangePicker.stx +218 -0
- package/src/ui/date-range-picker/index.ts +26 -0
- package/src/ui/empty-state/EmptyState.stx +57 -0
- package/src/ui/empty-state/index.ts +13 -0
- package/src/ui/theme-toggle/ThemeToggle.stx +155 -0
- package/src/ui/theme-toggle/index.ts +13 -0
- package/src/utils/highlighter.ts +155 -11
- package/dist/Calendar-ppqdw19s.stx +0 -189
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
export const value = $props.value || null
|
|
3
|
+
/*
|
|
4
|
+
* `range` is the mode four apps were missing (stacksjs/stx#1981).
|
|
5
|
+
*
|
|
6
|
+
* `<Calendar>` was single-date only, so every app that needed "last 7 days /
|
|
7
|
+
* custom range" built its own picker - four near-identical copies, hand-ported
|
|
8
|
+
* between repos, four copies of the same bugs. Range selection is the half an
|
|
9
|
+
* app cannot reasonably compose from what exists; the preset row is a Button
|
|
10
|
+
* loop, which is why `<DateRangePicker>` sits on top of this rather than
|
|
11
|
+
* replacing it.
|
|
12
|
+
*/
|
|
13
|
+
export const mode = $props.mode === 'range' ? 'range' : 'single'
|
|
14
|
+
export const start = $props.start || null
|
|
15
|
+
export const end = $props.end || null
|
|
16
|
+
export const minDate = $props.minDate || null
|
|
17
|
+
export const maxDate = $props.maxDate || null
|
|
18
|
+
export const disabledDates = $props.disabledDates || []
|
|
19
|
+
export const locale = $props.locale || 'en-US'
|
|
20
|
+
export const firstDayOfWeek = $props.firstDayOfWeek || 0
|
|
21
|
+
export const showToday = $props.showToday !== false
|
|
22
|
+
export const className = $props.className || ''
|
|
23
|
+
export const highlightToday = $props.highlightToday !== false
|
|
24
|
+
|
|
25
|
+
const baseClasses = 'bg-panel rounded-panel shadow-lg p-4 w-full max-w-sm'
|
|
26
|
+
export const calendarClasses = `${baseClasses} ${className}`.trim()
|
|
27
|
+
export const headerClasses = 'flex items-center justify-between mb-4'
|
|
28
|
+
export const monthYearClasses = 'text-lg font-semibold text-fg'
|
|
29
|
+
export const buttonClasses = 'p-2 hover:bg-surface-hover rounded-control transition-colors'
|
|
30
|
+
export const weekdaysClasses = 'grid grid-cols-7 gap-1 mb-2'
|
|
31
|
+
export const weekdayClasses = 'text-center text-xs font-medium text-fg-muted py-2'
|
|
32
|
+
export const daysGridClasses = 'grid grid-cols-7 gap-1'
|
|
33
|
+
|
|
34
|
+
const valueIso = value ? new Date(value).toISOString() : null
|
|
35
|
+
export const initialValueIso = valueIso
|
|
36
|
+
export const initialStartIso = start ? new Date(start).toISOString() : null
|
|
37
|
+
export const initialEndIso = end ? new Date(end).toISOString() : null
|
|
38
|
+
const minIso = minDate ? new Date(minDate).toISOString() : null
|
|
39
|
+
const maxIso = maxDate ? new Date(maxDate).toISOString() : null
|
|
40
|
+
export const minIsoValue = minIso
|
|
41
|
+
export const maxIsoValue = maxIso
|
|
42
|
+
export const disabledIsoList = disabledDates.map(d => new Date(d).toISOString())
|
|
43
|
+
</script>
|
|
44
|
+
|
|
45
|
+
<script client>
|
|
46
|
+
const emit = defineEmits()
|
|
47
|
+
const locale = {{ locale }}
|
|
48
|
+
const firstDayOfWeek = {{ firstDayOfWeek }}
|
|
49
|
+
const highlightToday = {{ highlightToday }}
|
|
50
|
+
/*
|
|
51
|
+
* The ISO strings are interpolated and guarded in JS, not inlined twice through
|
|
52
|
+
* a `=== null ? 'null' : …` ternary (stacksjs/stx#1981).
|
|
53
|
+
*
|
|
54
|
+
* That ternary produced the STRING "null", because `{{ }}` in a client script
|
|
55
|
+
* emits a JSON literal - so the guard `"null" ? …` was always true and every
|
|
56
|
+
* one of these became `new Date("null")`, an Invalid Date. With no props at all
|
|
57
|
+
* `<Calendar />` rendered a header reading "Invalid Date NaN" and 42 cells of
|
|
58
|
+
* Invalid Date: it has never worked without an explicit `value`, which is some
|
|
59
|
+
* of why four apps hand-rolled a picker rather than composing this one.
|
|
60
|
+
*
|
|
61
|
+
* A server-side `null` interpolates as the `null` literal, so the guard means
|
|
62
|
+
* what it says.
|
|
63
|
+
*/
|
|
64
|
+
const minIso = {{ minIsoValue }}
|
|
65
|
+
const maxIso = {{ maxIsoValue }}
|
|
66
|
+
const minDate = minIso ? new Date(minIso) : null
|
|
67
|
+
const maxDate = maxIso ? new Date(maxIso) : null
|
|
68
|
+
const disabledDates = ({{ disabledIsoList }}).map(s => new Date(s))
|
|
69
|
+
const initialValueIso = {{ initialValueIso }}
|
|
70
|
+
const initialValue = initialValueIso ? new Date(initialValueIso) : null
|
|
71
|
+
|
|
72
|
+
const mode = {{ mode }}
|
|
73
|
+
const startIso = {{ initialStartIso }}
|
|
74
|
+
const endIso = {{ initialEndIso }}
|
|
75
|
+
const initialStart = startIso ? new Date(startIso) : null
|
|
76
|
+
const initialEnd = endIso ? new Date(endIso) : null
|
|
77
|
+
|
|
78
|
+
const today = new Date()
|
|
79
|
+
const currentMonth = state(new Date(initialStart || initialValue || today))
|
|
80
|
+
const selectedDate = state(initialValue)
|
|
81
|
+
const rangeStart = state(initialStart)
|
|
82
|
+
const rangeEnd = state(initialEnd)
|
|
83
|
+
/*
|
|
84
|
+
* The day under the cursor while a range is half-made, so the span previews
|
|
85
|
+
* before the second click. Without it a range picker gives no feedback between
|
|
86
|
+
* the two clicks, which is the moment a user is deciding.
|
|
87
|
+
*/
|
|
88
|
+
const hoverDate = state(null)
|
|
89
|
+
|
|
90
|
+
function isSameDay(d1, d2) {
|
|
91
|
+
if (!d1 || !d2) return false
|
|
92
|
+
return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isToday(d) { return isSameDay(d, today) }
|
|
96
|
+
|
|
97
|
+
function isDisabled(d) {
|
|
98
|
+
if (minDate && d < minDate) return true
|
|
99
|
+
if (maxDate && d > maxDate) return true
|
|
100
|
+
return disabledDates.some(x => isSameDay(x, d))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function getDaysInMonth(d) { return new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate() }
|
|
104
|
+
function getFirstDayOfMonth(d) { return new Date(d.getFullYear(), d.getMonth(), 1).getDay() }
|
|
105
|
+
|
|
106
|
+
const monthYearLabel = derived(() => {
|
|
107
|
+
const m = currentMonth()
|
|
108
|
+
return `${m.toLocaleDateString(locale, { month: 'long' })} ${m.getFullYear()}`
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const dayNames = (() => {
|
|
112
|
+
const days = []
|
|
113
|
+
const baseDate = new Date(2024, 0, firstDayOfWeek)
|
|
114
|
+
for (let i = 0; i < 7; i++) {
|
|
115
|
+
const day = new Date(baseDate)
|
|
116
|
+
day.setDate(baseDate.getDate() + i)
|
|
117
|
+
days.push(day.toLocaleDateString(locale, { weekday: 'short' }))
|
|
118
|
+
}
|
|
119
|
+
return days
|
|
120
|
+
})()
|
|
121
|
+
|
|
122
|
+
const calendarDays = derived(() => {
|
|
123
|
+
const month = currentMonth()
|
|
124
|
+
const daysInMonth = getDaysInMonth(month)
|
|
125
|
+
const firstDay = (getFirstDayOfMonth(month) - firstDayOfWeek + 7) % 7
|
|
126
|
+
const days = []
|
|
127
|
+
const prev = new Date(month.getFullYear(), month.getMonth() - 1, 1)
|
|
128
|
+
const daysInPrev = getDaysInMonth(prev)
|
|
129
|
+
for (let i = firstDay - 1; i >= 0; i--) {
|
|
130
|
+
days.push({ date: new Date(prev.getFullYear(), prev.getMonth(), daysInPrev - i), isCurrentMonth: false })
|
|
131
|
+
}
|
|
132
|
+
for (let i = 1; i <= daysInMonth; i++) {
|
|
133
|
+
days.push({ date: new Date(month.getFullYear(), month.getMonth(), i), isCurrentMonth: true })
|
|
134
|
+
}
|
|
135
|
+
const remaining = 42 - days.length
|
|
136
|
+
const next = new Date(month.getFullYear(), month.getMonth() + 1, 1)
|
|
137
|
+
for (let i = 1; i <= remaining; i++) {
|
|
138
|
+
days.push({ date: new Date(next.getFullYear(), next.getMonth(), i), isCurrentMonth: false })
|
|
139
|
+
}
|
|
140
|
+
return days
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
function previousMonth() {
|
|
144
|
+
const m = currentMonth()
|
|
145
|
+
currentMonth.set(new Date(m.getFullYear(), m.getMonth() - 1, 1))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function nextMonthFn() {
|
|
149
|
+
const m = currentMonth()
|
|
150
|
+
currentMonth.set(new Date(m.getFullYear(), m.getMonth() + 1, 1))
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Midnight, so a comparison is about the DAY rather than the time of day. */
|
|
154
|
+
function atMidnight(d) {
|
|
155
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate())
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function selectDate(date) {
|
|
159
|
+
if (isDisabled(date)) return
|
|
160
|
+
|
|
161
|
+
if (mode !== 'range') {
|
|
162
|
+
selectedDate.set(date)
|
|
163
|
+
emit('change', date)
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const day = atMidnight(date)
|
|
168
|
+
const from = rangeStart()
|
|
169
|
+
const to = rangeEnd()
|
|
170
|
+
|
|
171
|
+
// A complete range, or none at all, starts a new one. Otherwise this click
|
|
172
|
+
// closes the open one.
|
|
173
|
+
if (!from || to) {
|
|
174
|
+
rangeStart.set(day)
|
|
175
|
+
rangeEnd.set(null)
|
|
176
|
+
hoverDate.set(null)
|
|
177
|
+
emit('change', { start: day, end: null })
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Clicking before the start reads as "I meant to begin here", which is what
|
|
182
|
+
// every range picker does rather than refusing the click.
|
|
183
|
+
const ordered = day < from ? { start: day, end: from } : { start: from, end: day }
|
|
184
|
+
rangeStart.set(ordered.start)
|
|
185
|
+
rangeEnd.set(ordered.end)
|
|
186
|
+
hoverDate.set(null)
|
|
187
|
+
emit('change', ordered)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** The span to paint: the chosen range, or the one the cursor is proposing. */
|
|
191
|
+
function spanNow() {
|
|
192
|
+
const from = rangeStart()
|
|
193
|
+
if (!from) return null
|
|
194
|
+
const to = rangeEnd() || hoverDate()
|
|
195
|
+
if (!to) return { start: from, end: from }
|
|
196
|
+
return to < from ? { start: to, end: from } : { start: from, end: to }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function onDayHover(date) {
|
|
200
|
+
if (mode !== 'range' || !rangeStart() || rangeEnd()) return
|
|
201
|
+
hoverDate.set(atMidnight(date))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isRangeEnd(date) {
|
|
205
|
+
const day = atMidnight(date)
|
|
206
|
+
const from = rangeStart()
|
|
207
|
+
const to = rangeEnd()
|
|
208
|
+
return (!!from && day.getTime() === from.getTime()) || (!!to && day.getTime() === to.getTime())
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function isInSpan(date) {
|
|
212
|
+
const span = spanNow()
|
|
213
|
+
if (!span) return false
|
|
214
|
+
const t = atMidnight(date).getTime()
|
|
215
|
+
return t >= span.start.getTime() && t <= span.end.getTime()
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function goToToday() {
|
|
219
|
+
currentMonth.set(new Date(today))
|
|
220
|
+
selectDate(today)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/*
|
|
224
|
+
* Joined on a space rather than concatenated.
|
|
225
|
+
*
|
|
226
|
+
* Every branch here appended straight onto the previous string, so the class
|
|
227
|
+
* either side of each join was destroyed: a day cell rendered
|
|
228
|
+
* `transition-colorstext-fgcursor-pointer` and
|
|
229
|
+
* `hover:bg-surface-hoverbg-accent-solid`. Five classes were lost that way,
|
|
230
|
+
* including `bg-accent-solid` - so the SELECTED day had no background at all,
|
|
231
|
+
* in every app shipping this component. Building a list and joining it is the
|
|
232
|
+
* shape that cannot have the bug (stacksjs/stx#1981).
|
|
233
|
+
*/
|
|
234
|
+
function dayClasses(day) {
|
|
235
|
+
const inRange = mode === 'range' && isInSpan(day.date)
|
|
236
|
+
const isEndpoint = mode === 'range' && isRangeEnd(day.date)
|
|
237
|
+
const isSelected = mode === 'range' ? isEndpoint : isSameDay(day.date, selectedDate())
|
|
238
|
+
const isTodayDate = isToday(day.date)
|
|
239
|
+
const disabled = isDisabled(day.date)
|
|
240
|
+
|
|
241
|
+
const parts = ['flex items-center justify-center h-10 w-10 text-sm transition-colors']
|
|
242
|
+
|
|
243
|
+
// Square middles, rounded ends: a run of rounded cells reads as separate
|
|
244
|
+
// chips rather than one span.
|
|
245
|
+
parts.push(inRange && !isEndpoint ? 'rounded-none' : 'rounded-control')
|
|
246
|
+
parts.push(day.isCurrentMonth ? 'text-fg' : 'text-fg-subtle')
|
|
247
|
+
parts.push(disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer hover:bg-surface-hover')
|
|
248
|
+
|
|
249
|
+
if (isSelected)
|
|
250
|
+
parts.push('bg-accent-solid text-accent-ink hover:bg-accent-solid-hover')
|
|
251
|
+
else if (inRange)
|
|
252
|
+
parts.push('bg-accent-soft text-accent-soft-ink')
|
|
253
|
+
|
|
254
|
+
if (isTodayDate && highlightToday && !isSelected)
|
|
255
|
+
parts.push('border-2 border-accent')
|
|
256
|
+
|
|
257
|
+
return parts.join(' ')
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
defineExpose({ selectedDate, currentMonth, selectDate, rangeStart, rangeEnd })
|
|
261
|
+
</script>
|
|
262
|
+
|
|
263
|
+
<div class="{{ calendarClasses }}">
|
|
264
|
+
<div class="{{ headerClasses }}">
|
|
265
|
+
<button
|
|
266
|
+
type="button"
|
|
267
|
+
class="{{ buttonClasses }}"
|
|
268
|
+
@click="previousMonth()"
|
|
269
|
+
aria-label="Previous month"
|
|
270
|
+
>
|
|
271
|
+
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
272
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
|
|
273
|
+
</svg>
|
|
274
|
+
</button>
|
|
275
|
+
|
|
276
|
+
<div class="{{ monthYearClasses }}" x-text="monthYearLabel()"></div>
|
|
277
|
+
|
|
278
|
+
<button
|
|
279
|
+
type="button"
|
|
280
|
+
class="{{ buttonClasses }}"
|
|
281
|
+
@click="nextMonthFn()"
|
|
282
|
+
aria-label="Next month"
|
|
283
|
+
>
|
|
284
|
+
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
285
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
|
|
286
|
+
</svg>
|
|
287
|
+
</button>
|
|
288
|
+
</div>
|
|
289
|
+
|
|
290
|
+
<div class="{{ weekdaysClasses }}">
|
|
291
|
+
<div :for="d in dayNames" :key="d" class="{{ weekdayClasses }}" x-text="d"></div>
|
|
292
|
+
</div>
|
|
293
|
+
|
|
294
|
+
<div class="{{ daysGridClasses }}">
|
|
295
|
+
<button
|
|
296
|
+
:for="day in calendarDays()"
|
|
297
|
+
:key="day.date.toISOString()"
|
|
298
|
+
type="button"
|
|
299
|
+
:class="dayClasses(day)"
|
|
300
|
+
@click="selectDate(day.date)"
|
|
301
|
+
@mouseenter="onDayHover(day.date)"
|
|
302
|
+
:aria-label="day.date.toLocaleDateString(locale)"
|
|
303
|
+
:aria-selected="isRangeEnd(day.date)"
|
|
304
|
+
:disabled="isDisabled(day.date)"
|
|
305
|
+
x-text="day.date.getDate()"
|
|
306
|
+
></button>
|
|
307
|
+
</div>
|
|
308
|
+
|
|
309
|
+
@if(showToday)
|
|
310
|
+
<div class="mt-4 flex justify-center">
|
|
311
|
+
<button
|
|
312
|
+
type="button"
|
|
313
|
+
class="px-4 py-2 text-sm font-medium text-accent hover:bg-surface-hover rounded-control transition-colors"
|
|
314
|
+
@click="goToToday()"
|
|
315
|
+
>
|
|
316
|
+
Today
|
|
317
|
+
</button>
|
|
318
|
+
</div>
|
|
319
|
+
@endif
|
|
320
|
+
</div>
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* The picker four apps were each maintaining their own copy of
|
|
4
|
+
* (stacksjs/stx#1981).
|
|
5
|
+
*
|
|
6
|
+
* `<Calendar>` was single-date only, so "last 7 days / last 30 days / custom
|
|
7
|
+
* range" had to be built per app: four near-identical files, hand-ported
|
|
8
|
+
* between repos, four copies of every bug. Range selection now lives on
|
|
9
|
+
* `<Calendar mode="range">` - that is the half an app cannot reasonably
|
|
10
|
+
* compose - and this is the trigger, the panel and the preset row around it.
|
|
11
|
+
*/
|
|
12
|
+
export const start = $props.start || null
|
|
13
|
+
export const end = $props.end || null
|
|
14
|
+
/** Preset spans in days. `[7, 30, 90]` gives "Last 7 days" and so on. */
|
|
15
|
+
export const presets = Array.isArray($props.presets) ? $props.presets : [7, 30, 90]
|
|
16
|
+
export const placeholder = $props.placeholder || 'Select dates'
|
|
17
|
+
export const locale = $props.locale || 'en-US'
|
|
18
|
+
export const minDate = $props.minDate || null
|
|
19
|
+
export const maxDate = $props.maxDate || null
|
|
20
|
+
export const align = $props.align === 'right' ? 'right' : 'left'
|
|
21
|
+
export const className = $props.className || ''
|
|
22
|
+
export const disabled = $bool($props.disabled)
|
|
23
|
+
|
|
24
|
+
export const startIso = start ? new Date(start).toISOString() : null
|
|
25
|
+
export const endIso = end ? new Date(end).toISOString() : null
|
|
26
|
+
export const minIso = minDate ? new Date(minDate).toISOString() : null
|
|
27
|
+
export const maxIso = maxDate ? new Date(maxDate).toISOString() : null
|
|
28
|
+
|
|
29
|
+
export const rootClasses = `relative inline-block ${className}`.trim()
|
|
30
|
+
export const triggerClasses = 'inline-flex items-center gap-2 px-4 py-2 text-sm font-medium text-fg bg-field rounded-control ring-1 ring-inset ring-line-strong hover:bg-field-hover focus:outline-none focus:ring-2 focus:ring-accent disabled:opacity-50 disabled:cursor-not-allowed transition-colors'
|
|
31
|
+
export const panelClasses = `absolute z-20 mt-2 flex gap-4 p-4 bg-panel rounded-panel shadow-lg ring-1 ring-line ${align === 'right' ? 'right-0' : 'left-0'}`
|
|
32
|
+
export const presetListClasses = 'flex flex-col gap-1 min-w-36'
|
|
33
|
+
export const presetClasses = 'px-3 py-1.5 text-left text-sm text-fg-strong rounded-control hover:bg-surface-hover focus:outline-none focus:ring-2 focus:ring-accent transition-colors'
|
|
34
|
+
export const presetActiveClasses = 'px-3 py-1.5 text-left text-sm font-medium text-accent-soft-ink bg-accent-soft rounded-control focus:outline-none focus:ring-2 focus:ring-accent transition-colors'
|
|
35
|
+
export const instanceId = $uid
|
|
36
|
+
</script>
|
|
37
|
+
|
|
38
|
+
<script client>
|
|
39
|
+
const emit = defineEmits()
|
|
40
|
+
const instanceId = {{ instanceId }}
|
|
41
|
+
const locale = {{ locale }}
|
|
42
|
+
const placeholder = {{ placeholder }}
|
|
43
|
+
const presets = {{ presets }}
|
|
44
|
+
|
|
45
|
+
/*
|
|
46
|
+
* Guarded in JS rather than inlined through a `=== null ? 'null' : …` ternary.
|
|
47
|
+
* That pattern emits the STRING "null", which is truthy, and it is what left
|
|
48
|
+
* every optional date in `<Calendar>` an Invalid Date (#1981).
|
|
49
|
+
*/
|
|
50
|
+
const startIso = {{ startIso }}
|
|
51
|
+
const endIso = {{ endIso }}
|
|
52
|
+
|
|
53
|
+
const isOpen = state(false)
|
|
54
|
+
const rangeStart = state(startIso ? new Date(startIso) : null)
|
|
55
|
+
const rangeEnd = state(endIso ? new Date(endIso) : null)
|
|
56
|
+
const activePreset = state(null)
|
|
57
|
+
|
|
58
|
+
let rootEl = null
|
|
59
|
+
let outsideHandler = null
|
|
60
|
+
let keyHandler = null
|
|
61
|
+
|
|
62
|
+
function midnight(d) {
|
|
63
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate())
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function formatOne(d) {
|
|
67
|
+
return d.toLocaleDateString(locale, { month: 'short', day: 'numeric', year: 'numeric' })
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** What the trigger says: a range, a half-made range, or the placeholder. */
|
|
71
|
+
const label = derived(() => {
|
|
72
|
+
const from = rangeStart()
|
|
73
|
+
const to = rangeEnd()
|
|
74
|
+
if (!from) return placeholder
|
|
75
|
+
if (!to) return formatOne(from)
|
|
76
|
+
return formatOne(from) + ' - ' + formatOne(to)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
function open() {
|
|
80
|
+
if (isOpen()) return
|
|
81
|
+
isOpen.set(true)
|
|
82
|
+
emit('open')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function close() {
|
|
86
|
+
if (!isOpen()) return
|
|
87
|
+
isOpen.set(false)
|
|
88
|
+
emit('close')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function toggle(event) {
|
|
92
|
+
if (event) event.stopPropagation()
|
|
93
|
+
isOpen() ? close() : open()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/*
|
|
97
|
+
* A preset is "the last N days, ending today", counted inclusively - so 7 days
|
|
98
|
+
* is today and the six before it, which is what every dashboard means by it.
|
|
99
|
+
*/
|
|
100
|
+
function applyPreset(days) {
|
|
101
|
+
const to = midnight(new Date())
|
|
102
|
+
const from = midnight(new Date())
|
|
103
|
+
from.setDate(from.getDate() - (days - 1))
|
|
104
|
+
rangeStart.set(from)
|
|
105
|
+
rangeEnd.set(to)
|
|
106
|
+
activePreset.set(days)
|
|
107
|
+
emit('change', { start: from, end: to, preset: days })
|
|
108
|
+
close()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function presetLabel(days) {
|
|
112
|
+
return 'Last ' + days + ' days'
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function presetButtonClasses(days) {
|
|
116
|
+
return activePreset() === days ? {{ presetActiveClasses }} : {{ presetClasses }}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/*
|
|
120
|
+
* The calendar's own `change` event, caught as it bubbles.
|
|
121
|
+
*
|
|
122
|
+
* Listened for on the WRAPPER rather than with `@change` on the component tag:
|
|
123
|
+
* forwarding an event onto a nested component's root clobbers that component's
|
|
124
|
+
* own scope, so Calendar's bindings stop resolving. `defineEmits` dispatches
|
|
125
|
+
* with `bubbles: true`, so a listener one element out gets the same event and
|
|
126
|
+
* the child is left alone.
|
|
127
|
+
*/
|
|
128
|
+
function onCalendarChange(event) {
|
|
129
|
+
const detail = event && event.detail ? event.detail : event
|
|
130
|
+
if (!detail || !detail.start) return
|
|
131
|
+
rangeStart.set(detail.start)
|
|
132
|
+
rangeEnd.set(detail.end || null)
|
|
133
|
+
// A hand-picked range is not one of the presets any more.
|
|
134
|
+
activePreset.set(null)
|
|
135
|
+
// Emitted only once the range is closed: a half-made one is not an answer,
|
|
136
|
+
// and a consumer refetching on every click would fire twice per selection.
|
|
137
|
+
if (detail.end) {
|
|
138
|
+
emit('change', { start: detail.start, end: detail.end, preset: null })
|
|
139
|
+
close()
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function clear() {
|
|
144
|
+
rangeStart.set(null)
|
|
145
|
+
rangeEnd.set(null)
|
|
146
|
+
activePreset.set(null)
|
|
147
|
+
emit('change', { start: null, end: null, preset: null })
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
onMount(() => {
|
|
151
|
+
rootEl = document.querySelector('[data-stx-range-picker="' + instanceId + '"]')
|
|
152
|
+
outsideHandler = (event) => {
|
|
153
|
+
if (!isOpen() || !rootEl) return
|
|
154
|
+
if (!rootEl.contains(event.target)) close()
|
|
155
|
+
}
|
|
156
|
+
keyHandler = (event) => {
|
|
157
|
+
if (event.key === 'Escape' && isOpen()) {
|
|
158
|
+
event.preventDefault()
|
|
159
|
+
close()
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
document.addEventListener('click', outsideHandler)
|
|
163
|
+
document.addEventListener('keydown', keyHandler)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
onDestroy(() => {
|
|
167
|
+
if (outsideHandler) document.removeEventListener('click', outsideHandler)
|
|
168
|
+
if (keyHandler) document.removeEventListener('keydown', keyHandler)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
defineExpose({ isOpen, rangeStart, rangeEnd, activePreset, applyPreset, onCalendarChange, clear, label })
|
|
172
|
+
</script>
|
|
173
|
+
|
|
174
|
+
<div class="{{ rootClasses }}" data-stx-range-picker="{{ instanceId }}">
|
|
175
|
+
<button
|
|
176
|
+
type="button"
|
|
177
|
+
class="{{ triggerClasses }}"
|
|
178
|
+
@if(disabled)disabled@endif
|
|
179
|
+
aria-haspopup="dialog"
|
|
180
|
+
:aria-expanded="isOpen() ? 'true' : 'false'"
|
|
181
|
+
@click="toggle($event)"
|
|
182
|
+
>
|
|
183
|
+
<svg class="h-4 w-4 text-fg-soft" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
184
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
|
|
185
|
+
</svg>
|
|
186
|
+
<span :text="label()">{{ placeholder }}</span>
|
|
187
|
+
</button>
|
|
188
|
+
|
|
189
|
+
<div :show="isOpen()" x-cloak class="{{ panelClasses }}" role="dialog" aria-label="Choose a date range">
|
|
190
|
+
<div class="{{ presetListClasses }}">
|
|
191
|
+
<button
|
|
192
|
+
:for="days in presets"
|
|
193
|
+
:key="days"
|
|
194
|
+
type="button"
|
|
195
|
+
:class="presetButtonClasses(days)"
|
|
196
|
+
@click="applyPreset(days)"
|
|
197
|
+
:text="presetLabel(days)"
|
|
198
|
+
></button>
|
|
199
|
+
<button
|
|
200
|
+
type="button"
|
|
201
|
+
class="{{ presetClasses }} text-fg-muted"
|
|
202
|
+
@click="clear()"
|
|
203
|
+
>Clear</button>
|
|
204
|
+
</div>
|
|
205
|
+
|
|
206
|
+
{{-- The listener sits here, not on the <Calendar> tag: see onCalendarChange. --}}
|
|
207
|
+
<div @change="onCalendarChange($event)">
|
|
208
|
+
<Calendar
|
|
209
|
+
mode="range"
|
|
210
|
+
:start="rangeStart()"
|
|
211
|
+
:end="rangeEnd()"
|
|
212
|
+
min-date="{{ minIso }}"
|
|
213
|
+
max-date="{{ maxIso }}"
|
|
214
|
+
locale="{{ locale }}"
|
|
215
|
+
/>
|
|
216
|
+
</div>
|
|
217
|
+
</div>
|
|
218
|
+
</div>
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* 29 hand-built empty states across six apps (stacksjs/stx#1981): icon,
|
|
4
|
+
* heading, one line of explanation, optional action. `Card` and `Button` both
|
|
5
|
+
* exist; the composition did not.
|
|
6
|
+
*
|
|
7
|
+
* Low complexity, high repetition - which is exactly why it ends up subtly
|
|
8
|
+
* inconsistent across a suite: everyone writes their own, and the padding, the
|
|
9
|
+
* heading level and the gap all drift.
|
|
10
|
+
*/
|
|
11
|
+
export const title = $props.title || ''
|
|
12
|
+
export const description = $props.description || ''
|
|
13
|
+
/*
|
|
14
|
+
* Raw SVG markup, not an icon name. Naming would mean this component owning a
|
|
15
|
+
* registry, and the library's icons are 200k Iconify entries across generated
|
|
16
|
+
* packages - so `<EmptyState>` takes the markup or the `icon` slot and stays
|
|
17
|
+
* out of that decision.
|
|
18
|
+
*/
|
|
19
|
+
export const icon = $props.icon || ''
|
|
20
|
+
export const as = $props.as || 'div'
|
|
21
|
+
/** `panel` draws the bordered card; `bare` is for something already in one. */
|
|
22
|
+
export const variant = $props.variant === 'bare' ? 'bare' : 'panel'
|
|
23
|
+
export const align = $props.align === 'left' ? 'left' : 'center'
|
|
24
|
+
export const className = $props.className || ''
|
|
25
|
+
|
|
26
|
+
const panel = variant === 'panel' ? 'bg-panel rounded-panel ring-1 ring-line' : ''
|
|
27
|
+
const alignment = align === 'left' ? 'items-start text-left' : 'items-center text-center'
|
|
28
|
+
export const rootClasses = `flex flex-col ${alignment} gap-3 px-6 py-12 ${panel} ${className}`.trim()
|
|
29
|
+
export const iconWrapClasses = 'flex items-center justify-center h-12 w-12 rounded-full bg-surface-sunken text-fg-soft'
|
|
30
|
+
export const titleClasses = 'text-base font-semibold text-fg'
|
|
31
|
+
export const descriptionClasses = 'max-w-sm text-sm text-fg-muted'
|
|
32
|
+
export const actionClasses = 'mt-2 flex flex-wrap items-center gap-2'
|
|
33
|
+
export const hasIcon = icon !== ''
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<component :is="{{ as }}" class="{{ rootClasses }}">
|
|
37
|
+
{{-- Only when there is one: an empty state without an icon would otherwise
|
|
38
|
+
carry a floating grey disc. A named slot is not offered because stx
|
|
39
|
+
cannot tell an empty slot from an absent one at render time, so the
|
|
40
|
+
circle would render either way - the prop is the honest shape, and a
|
|
41
|
+
caller wanting a whole <Icon /> composes it with variant="bare". --}}
|
|
42
|
+
@if(hasIcon)
|
|
43
|
+
<div class="{{ iconWrapClasses }}">{!! icon !!}</div>
|
|
44
|
+
@endif
|
|
45
|
+
|
|
46
|
+
@if(title)
|
|
47
|
+
<h3 class="{{ titleClasses }}">{{ title }}</h3>
|
|
48
|
+
@endif
|
|
49
|
+
|
|
50
|
+
@if(description)
|
|
51
|
+
<p class="{{ descriptionClasses }}">{{ description }}</p>
|
|
52
|
+
@endif
|
|
53
|
+
|
|
54
|
+
<div class="{{ actionClasses }}">
|
|
55
|
+
<slot />
|
|
56
|
+
</div>
|
|
57
|
+
</component>
|