ads-nuxt 0.1.4 → 0.2.0

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.
Files changed (90) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +573 -577
  3. package/app/app.config.ts +25 -25
  4. package/app/assets/ads/checkbox-check.svg +5 -5
  5. package/app/assets/ads/checkbox-minus.svg +5 -5
  6. package/app/assets/brand/farmarcas.svg +6 -6
  7. package/app/assets/brand/radar-symbol.svg +9 -0
  8. package/app/assets/brand/radar.svg +9 -9
  9. package/app/assets/css/ads-colors.css +257 -246
  10. package/app/assets/css/component-tokens.css +367 -403
  11. package/app/assets/css/main.css +624 -617
  12. package/app/components/AdsBadge.vue +47 -48
  13. package/app/components/AdsBroadcast.vue +24 -45
  14. package/app/components/AdsButton.vue +539 -569
  15. package/app/components/AdsButtonGroup.vue +39 -39
  16. package/app/components/AdsButtonMenu.vue +48 -48
  17. package/app/components/AdsCard.vue +188 -117
  18. package/app/components/AdsCardHint.vue +22 -0
  19. package/app/components/AdsCardPeriod.vue +43 -0
  20. package/app/components/AdsCardVariation.vue +31 -0
  21. package/app/components/AdsCharttip.vue +78 -78
  22. package/app/components/AdsCheckbox.vue +77 -77
  23. package/app/components/AdsDatePicker.vue +492 -479
  24. package/app/components/AdsDropdownMenu.vue +30 -19
  25. package/app/components/AdsEmptyState.vue +58 -58
  26. package/app/components/AdsFeatureCard.vue +210 -219
  27. package/app/components/AdsFileCard.vue +98 -101
  28. package/app/components/AdsFooter.vue +25 -25
  29. package/app/components/AdsHeavyAccountCell.vue +59 -0
  30. package/app/components/AdsHeavyHeaderCell.vue +70 -0
  31. package/app/components/AdsHeavyTable.vue +135 -0
  32. package/app/components/AdsInput.vue +147 -143
  33. package/app/components/AdsLoading.vue +217 -109
  34. package/app/components/AdsMenuBar.vue +103 -103
  35. package/app/components/AdsMetricCard.vue +216 -119
  36. package/app/components/AdsModal.vue +157 -157
  37. package/app/components/AdsMonthPicker.vue +80 -80
  38. package/app/components/AdsNavbarApp.vue +67 -67
  39. package/app/components/AdsNavbarDesk.vue +62 -62
  40. package/app/components/AdsNotabilityPill.vue +24 -27
  41. package/app/components/AdsNumberInput.vue +173 -166
  42. package/app/components/AdsPagination.vue +91 -91
  43. package/app/components/AdsPanelCard.vue +99 -0
  44. package/app/components/AdsPill.vue +144 -92
  45. package/app/components/AdsProductTag.vue +61 -61
  46. package/app/components/AdsRadio.vue +70 -70
  47. package/app/components/AdsSearchInput.vue +122 -113
  48. package/app/components/AdsSegmentedTabs.vue +62 -28
  49. package/app/components/{AdsFilterSelect.vue → AdsSelect.vue} +266 -203
  50. package/app/components/AdsSelectItem.vue +128 -0
  51. package/app/components/AdsSelectPanel.vue +150 -0
  52. package/app/components/AdsSideMenu.vue +101 -101
  53. package/app/components/AdsSmalltip.vue +44 -44
  54. package/app/components/AdsStepper.vue +222 -0
  55. package/app/components/AdsSteptip.vue +101 -101
  56. package/app/components/AdsSwitch.vue +59 -59
  57. package/app/components/AdsTable.vue +161 -100
  58. package/app/components/AdsTableHeaderCell.vue +80 -0
  59. package/app/components/AdsTableSkeleton.vue +113 -0
  60. package/app/components/AdsTableToolbar.vue +44 -44
  61. package/app/components/AdsTabs.vue +51 -51
  62. package/app/components/AdsTextarea.vue +116 -109
  63. package/app/components/AdsToast.vue +183 -181
  64. package/app/components/AdsTooltip.vue +73 -73
  65. package/app/components/OverlayLoading.vue +173 -173
  66. package/app/components/ProgressBarLoading.vue +235 -256
  67. package/app/composables/useOverlayLoading.ts +66 -66
  68. package/app/constants/ads-color-swatches.ts +240 -239
  69. package/app/locale/pt-br.ts +6 -6
  70. package/app/plugins/ads-toast.client.ts +24 -24
  71. package/app/theme/ads-theme.ts +374 -370
  72. package/app/theme/ads-tokens.ts +391 -379
  73. package/app/theme/component-tokens.json +366 -402
  74. package/app/theme/component-tokens.ts +807 -815
  75. package/app/types/overlay-loading.types.ts +26 -26
  76. package/app/utils/ads-filter-select.ts +191 -172
  77. package/app/utils/ads-table.ts +395 -204
  78. package/app/utils/calendar-date.ts +163 -163
  79. package/app/utils/category-role.ts +45 -45
  80. package/app/utils/money.ts +77 -77
  81. package/app/utils/notability.ts +8 -8
  82. package/app/utils/parser.ts +134 -134
  83. package/app/utils/percentage.ts +54 -54
  84. package/nuxt.config.ts +31 -31
  85. package/package.json +1 -1
  86. package/app/components/AdsFarmarcasPill.vue +0 -74
  87. package/app/components/AdsFilterDropdown.vue +0 -174
  88. package/app/components/AdsFilterOption.vue +0 -74
  89. package/app/components/AdsListDropdown.vue +0 -44
  90. package/app/components/AdsMiniPill.vue +0 -40
@@ -1,479 +1,492 @@
1
- <script setup lang="ts">
2
- import { isEqualMonth, toCalendarDate, type CalendarDate, type DateValue } from "@internationalized/date";
3
- import { computed, nextTick, ref, shallowRef, watch } from "vue";
4
- import AdsButton from "./AdsButton.vue";
5
- import { UI_LOCALE_CODE } from "../locale/pt-br";
6
- import { formatDateOnlyToPtBr, getTodayDateString } from "../utils/parser";
7
- import {
8
- calendarDateToDateString,
9
- calendarMonthTitle,
10
- calendarRangeToDateStrings,
11
- calendarRangesEqual,
12
- dateStringToCalendarDate,
13
- dateStringsToCalendarRange,
14
- inclusiveCalendarDays,
15
- isCalendarDateRange,
16
- isOutsideMaximumWindow,
17
- shiftVisibleMonth,
18
- visibleMonthAfterPlaceholder,
19
- toCalendarModel,
20
- type CalendarDateRangeValue,
21
- type CalendarEventValue,
22
- } from "../utils/calendar-date";
23
-
24
- const DATE_PLACEHOLDER = "dd/mm/aaaa";
25
- const RANGE_SEPARATOR = " – ";
26
-
27
- const props = withDefaults(
28
- defineProps<{
29
- range?: boolean;
30
- min?: string;
31
- max?: string;
32
- disabled?: boolean;
33
- readonly?: boolean;
34
- size?: "sm" | "md" | "lg";
35
- maximumDays?: number;
36
- class?: string;
37
- }>(),
38
- {
39
- range: false,
40
- size: "md",
41
- },
42
- );
43
-
44
- const modelValue = defineModel<string>({ default: "" });
45
- const start = defineModel<string>("start", { default: "" });
46
- const end = defineModel<string>("end", { default: "" });
47
-
48
- const inputDateRef = useTemplateRef("inputDateRef");
49
- const syncingFromCalendar = ref(false);
50
- const rangeOpen = ref(false);
51
- const rangeCalendarKey = ref(0);
52
- const ignoreDraftUpdates = ref(false);
53
- const rangeSessionStart = ref("");
54
- const rangeSessionEnd = ref("");
55
-
56
- type CalendarModel = CalendarDate | CalendarDateRangeValue | undefined;
57
-
58
- const calendarModel = shallowRef<CalendarModel>();
59
- const draftRange = shallowRef<CalendarDateRangeValue | undefined>();
60
- const leftPlaceholder = shallowRef<CalendarDate>(
61
- dateStringToCalendarDate(getTodayDateString())!.set({ day: 1 }),
62
- );
63
-
64
- const minCalendarDate = computed(() =>
65
- props.min ? dateStringToCalendarDate(props.min) : undefined,
66
- );
67
-
68
- const maxCalendarDate = computed(() =>
69
- props.max ? dateStringToCalendarDate(props.max) : undefined,
70
- );
71
-
72
- const leftMonth = computed(() => leftPlaceholder.value.set({ day: 1 }));
73
- const rightMonth = computed(() => leftMonth.value.add({ months: 1 }));
74
-
75
- const navigationMax = computed(() => {
76
- const hardMax = maxCalendarDate.value;
77
- const draft = draftRange.value;
78
- if (!props.maximumDays || !draft?.start || draft.end) return hardMax;
79
-
80
- const windowEnd = draft.start.add({ days: props.maximumDays - 1 });
81
- if (!hardMax || windowEnd.compare(hardMax) < 0) return windowEnd;
82
- return hardMax;
83
- });
84
-
85
- const canShiftToPreviousMonth = computed(
86
- () =>
87
- shiftVisibleMonth(
88
- leftPlaceholder.value,
89
- -1,
90
- minCalendarDate.value,
91
- navigationMax.value,
92
- ).compare(leftMonth.value) !== 0,
93
- );
94
-
95
- const canShiftToNextMonth = computed(
96
- () =>
97
- shiftVisibleMonth(
98
- leftPlaceholder.value,
99
- 1,
100
- minCalendarDate.value,
101
- navigationMax.value,
102
- ).compare(leftMonth.value) !== 0,
103
- );
104
-
105
- const rangeStepLabel = computed(() => {
106
- const draft = draftRange.value;
107
- if (!draft?.start) return "Escolha o início";
108
-
109
- const startLabel = formatDateOnlyToPtBr(calendarDateToDateString(draft.start));
110
- if (!draft.end) {
111
- return props.maximumDays
112
- ? `Escolha o fim. Máximo de ${props.maximumDays} dias.`
113
- : "Escolha o fim";
114
- }
115
-
116
- const endLabel = formatDateOnlyToPtBr(calendarDateToDateString(draft.end));
117
- const days = inclusiveCalendarDays(draft.start, draft.end);
118
- const dayLabel = days === 1 ? "dia" : "dias";
119
- return `${startLabel} – ${endLabel} · ${days} ${dayLabel}`;
120
- });
121
-
122
- const canApplyRange = computed(
123
- () => Boolean(draftRange.value?.start && draftRange.value?.end),
124
- );
125
-
126
- const rangeDisplayText = computed(() => {
127
- const startLabel = start.value
128
- ? formatDateOnlyToPtBr(start.value)
129
- : DATE_PLACEHOLDER;
130
- const endLabel = end.value
131
- ? formatDateOnlyToPtBr(end.value)
132
- : DATE_PLACEHOLDER;
133
-
134
- if (!start.value && !end.value) {
135
- return `${DATE_PLACEHOLDER}${RANGE_SEPARATOR}${DATE_PLACEHOLDER}`;
136
- }
137
-
138
- return `${startLabel}${RANGE_SEPARATOR}${endLabel}`;
139
- });
140
-
141
- const popoverReference = computed(() => {
142
- const input = inputDateRef.value;
143
- const segments = input?.inputsRef;
144
- if (!segments?.length) return undefined;
145
- return segments[segments.length - 1]?.$el;
146
- });
147
-
148
- const rangeFieldClass = computed(() => [
149
- "group relative inline-flex w-full cursor-pointer items-center text-left transition-colors",
150
- "h-[var(--ads-date-height)] rounded-[var(--ads-date-radius)] px-[var(--ads-date-padding-x)] text-[length:var(--ads-date-font-size)] [font-weight:var(--ads-date-weight)] leading-[var(--ads-date-line-height)] text-[var(--ads-date-text)] bg-[var(--ads-date-background)] ring ring-inset ring-[var(--ads-date-border)]",
151
- "enabled:hover:ring-[var(--ads-date-hover-border)] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--ads-date-focus-border)]",
152
- "disabled:cursor-not-allowed disabled:opacity-75",
153
- props.class,
154
- ]);
155
-
156
- const popoverUi = { content: "w-auto max-w-none p-0" };
157
-
158
- /**
159
- * Grade do range no Figma (5712:6691): mês com 258px, 7 colunas de 35,14px e 2px entre elas,
160
- * linhas de 36px (semana + 5 semanas = 216px) e dia de 32px de altura.
161
- */
162
- const rangeCalendarUi = {
163
- body: "flex-row gap-14 space-x-0 space-y-0 pt-0 sm:space-x-0",
164
- grid: "w-[258px] space-y-0",
165
- gridWeekDaysRow: "mb-0 h-9 grid-cols-7 items-center gap-x-0.5",
166
- gridBody: "grid",
167
- gridRow: "h-9 grid-cols-7 items-center gap-x-0.5",
168
- cell: "flex h-9 w-full items-center justify-self-stretch",
169
- cellTrigger: "m-0 h-8 w-full",
170
- };
171
-
172
- /** Intl em pt-BR devolve o mês em minúscula ("outubro de 2026"). Só a primeira letra muda; o ano fica igual. */
173
- function capitalizeFirst(value: string) {
174
- return value ? value.charAt(0).toLocaleUpperCase(UI_LOCALE_CODE) + value.slice(1) : value;
175
- }
176
-
177
- /** Cabeçalho do mês no Figma (_Date Picker Header, 5709:19265): Inter Bold 14, linha 130%, text/primary. */
178
- const singleCalendarUi = {
179
- heading: "text-sm font-bold leading-[1.3] text-[var(--ads-text-primary)]",
180
- };
181
-
182
- function monthTitle(date: CalendarDate) {
183
- return calendarMonthTitle(date, UI_LOCALE_CODE);
184
- }
185
-
186
- function onRangePlaceholderUpdate(placeholder: DateValue) {
187
- const value = toCalendarDate(placeholder);
188
- const next = visibleMonthAfterPlaceholder(leftPlaceholder.value, value);
189
- if (next.compare(leftPlaceholder.value) !== 0) {
190
- leftPlaceholder.value = next;
191
- return;
192
- }
193
- if (isEqualMonth(value, leftPlaceholder.value) && value.day === 1) return;
194
- leftPlaceholder.value = leftPlaceholder.value.copy();
195
- }
196
-
197
- function moveVisibleMonth(delta: -1 | 1) {
198
- const next = shiftVisibleMonth(
199
- leftPlaceholder.value,
200
- delta,
201
- minCalendarDate.value,
202
- navigationMax.value,
203
- );
204
- if (next.compare(leftPlaceholder.value) === 0) return;
205
- leftPlaceholder.value = next;
206
- }
207
-
208
- function weekdayLetter(day: string) {
209
- return day.replace(".", "").charAt(0).toUpperCase();
210
- }
211
-
212
- function applyExternalModels() {
213
- if (syncingFromCalendar.value || props.range) return;
214
-
215
- const next = dateStringToCalendarDate(modelValue.value);
216
- if (!calendarRangesEqual(calendarModel.value, next)) {
217
- calendarModel.value = next;
218
- }
219
- }
220
-
221
- function onCalendarModelUpdate(event: CalendarEventValue) {
222
- const value = toCalendarModel(event);
223
- if (calendarRangesEqual(calendarModel.value, value)) return;
224
-
225
- syncingFromCalendar.value = true;
226
- calendarModel.value = value;
227
-
228
- if (!props.range && value && !isCalendarDateRange(value)) {
229
- modelValue.value = calendarDateToDateString(value);
230
- }
231
-
232
- nextTick(() => {
233
- syncingFromCalendar.value = false;
234
- });
235
- }
236
-
237
- function resetDraftFromCommitted() {
238
- draftRange.value = dateStringsToCalendarRange(start.value, end.value);
239
- const anchor =
240
- draftRange.value?.start ?? dateStringToCalendarDate(getTodayDateString())!;
241
- leftPlaceholder.value = anchor.set({ day: 1 });
242
- }
243
-
244
- function onDraftUpdate(value: CalendarEventValue) {
245
- if (ignoreDraftUpdates.value) return;
246
- const range = toCalendarModel(value);
247
- draftRange.value = range && isCalendarDateRange(range) ? range : undefined;
248
- }
249
-
250
- function clearDraft() {
251
- // Os dois calendários guardam o intervalo por conta própria. Remontar
252
- // descarta o destaque; o flag ignora o eco desse estado ao desmontar.
253
- ignoreDraftUpdates.value = true;
254
- draftRange.value = undefined;
255
- start.value = "";
256
- end.value = "";
257
- rangeCalendarKey.value += 1;
258
- nextTick(() => {
259
- draftRange.value = undefined;
260
- nextTick(() => {
261
- ignoreDraftUpdates.value = false;
262
- });
263
- });
264
- }
265
-
266
- function applyDraft(close: () => void) {
267
- if (!draftRange.value?.start || !draftRange.value.end) return;
268
- const next = calendarRangeToDateStrings({
269
- start: draftRange.value.start,
270
- end: draftRange.value.end,
271
- });
272
- start.value = next.start;
273
- end.value = next.end;
274
- close();
275
- }
276
-
277
- function cancelDraft(close: () => void) {
278
- start.value = rangeSessionStart.value;
279
- end.value = rangeSessionEnd.value;
280
- resetDraftFromCommitted();
281
- close();
282
- }
283
-
284
- watch(rangeOpen, (isOpen) => {
285
- if (!isOpen) return;
286
- rangeSessionStart.value = start.value;
287
- rangeSessionEnd.value = end.value;
288
- resetDraftFromCommitted();
289
- });
290
-
291
- watch([modelValue, start, end, () => props.range], applyExternalModels, {
292
- immediate: true,
293
- });
294
-
295
- function isDateUnavailable(value: DateValue) {
296
- const date = toCalendarDate(value);
297
- if (minCalendarDate.value && date.compare(minCalendarDate.value) < 0) {
298
- return true;
299
- }
300
- if (maxCalendarDate.value && date.compare(maxCalendarDate.value) > 0) {
301
- return true;
302
- }
303
- if (!props.range || !props.maximumDays) return false;
304
- const draft = draftRange.value;
305
- if (!draft?.start || draft.end) return false;
306
- return isOutsideMaximumWindow(date, draft.start, props.maximumDays);
307
- }
308
- </script>
309
-
310
- <template>
311
- <UPopover
312
- v-if="range"
313
- v-model:open="rangeOpen"
314
- :content="{ align: 'start', side: 'bottom' }"
315
- :ui="popoverUi"
316
- >
317
- <button
318
- type="button"
319
- :disabled="disabled || readonly"
320
- :aria-label="`Selecionar período. ${rangeDisplayText}`"
321
- :class="rangeFieldClass"
322
- >
323
- <span
324
- class="min-w-0 flex-1 truncate tabular-nums"
325
- :class="start || end ? 'text-[var(--ads-date-text)]' : 'text-[var(--ads-date-placeholder)]'"
326
- >
327
- {{ rangeDisplayText }}
328
- </span>
329
- <UIcon name="i-lucide-calendar" class="size-5 shrink-0 text-[var(--ads-date-placeholder)]" />
330
- </button>
331
-
332
- <template #content="{ close }">
333
- <div class="ads-date-range-panel max-w-[calc(100vw-2rem)] overflow-x-auto bg-white">
334
- <p class="px-4 pt-4 text-sm font-medium text-neutral-900" aria-live="polite">
335
- {{ rangeStepLabel }}
336
- </p>
337
- <!-- Figma Range Date Picker (5712:6691): dois painéis de 290×284, 24px entre eles. -->
338
- <div class="relative w-[604px]">
339
- <div class="pointer-events-none absolute inset-0 grid grid-cols-[290px_290px] gap-6" aria-hidden="true">
340
- <div class="rounded-s-[var(--ads-radius-xsm)] bg-[var(--ads-surface-card)]" />
341
- <div class="rounded-e-[var(--ads-radius-xsm)] bg-[var(--ads-surface-card)]" />
342
- </div>
343
- <div class="relative flex flex-col gap-3 p-4">
344
- <div class="grid grid-cols-[258px_258px] gap-x-14">
345
- <div class="flex h-6 items-center justify-between">
346
- <button
347
- type="button"
348
- class="ads-date-range-nav"
349
- aria-label="Mês anterior"
350
- :disabled="!canShiftToPreviousMonth"
351
- @click="moveVisibleMonth(-1)"
352
- >
353
- <UIcon name="i-lucide-chevron-left" class="size-6" />
354
- </button>
355
- <span class="ads-date-range-title">{{ monthTitle(leftMonth) }}</span>
356
- <span class="size-6 shrink-0" aria-hidden="true" />
357
- </div>
358
- <div class="flex h-6 items-center justify-between">
359
- <span class="size-6 shrink-0" aria-hidden="true" />
360
- <span class="ads-date-range-title">{{ monthTitle(rightMonth) }}</span>
361
- <button
362
- type="button"
363
- class="ads-date-range-nav"
364
- aria-label="Próximo mês"
365
- :disabled="!canShiftToNextMonth"
366
- @click="moveVisibleMonth(1)"
367
- >
368
- <UIcon name="i-lucide-chevron-right" class="size-6" />
369
- </button>
370
- </div>
371
- </div>
372
- <UCalendar
373
- :key="`range-${rangeCalendarKey}`"
374
- :model-value="draftRange"
375
- :placeholder="leftMonth"
376
- :locale="UI_LOCALE_CODE"
377
- range
378
- :number-of-months="2"
379
- :month-controls="false"
380
- :year-controls="false"
381
- weekday-format="narrow"
382
- :week-starts-on="0"
383
- :min-value="minCalendarDate"
384
- :max-value="maxCalendarDate"
385
- disable-days-outside-current-view
386
- :is-date-unavailable="isDateUnavailable"
387
- color="neutral"
388
- variant="outline"
389
- class="ads-date-picker"
390
- :ui="rangeCalendarUi"
391
- @update:model-value="onDraftUpdate"
392
- @update:placeholder="onRangePlaceholderUpdate"
393
- >
394
- <template #week-day="{ day }">
395
- {{ weekdayLetter(day) }}
396
- </template>
397
- </UCalendar>
398
- </div>
399
- </div>
400
-
401
- <div class="flex items-center justify-between gap-3 px-4 pb-4">
402
- <AdsButton
403
- type="unfilled"
404
- size="sm"
405
- leading
406
- icon="i-lucide-x"
407
- label="Limpar seleção"
408
- @click="clearDraft"
409
- />
410
- <div class="flex items-center gap-2">
411
- <AdsButton
412
- type="unfilled"
413
- label="Cancelar"
414
- @click="cancelDraft(close)"
415
- />
416
- <AdsButton
417
- label="Aplicar"
418
- :disabled="!canApplyRange"
419
- @click="applyDraft(close)"
420
- />
421
- </div>
422
- </div>
423
- </div>
424
- </template>
425
- </UPopover>
426
-
427
- <UInputDate
428
- v-else
429
- ref="inputDateRef"
430
- :model-value="calendarModel"
431
- :locale="UI_LOCALE_CODE"
432
- :size="size"
433
- :disabled="disabled"
434
- :readonly="readonly"
435
- :min-value="minCalendarDate"
436
- :max-value="maxCalendarDate"
437
- :is-date-unavailable="isDateUnavailable"
438
- trailing
439
- :class="props.class"
440
- @update:model-value="onCalendarModelUpdate"
441
- >
442
- <template #trailing>
443
- <UPopover
444
- :reference="popoverReference"
445
- :content="{ align: 'end', side: 'bottom' }"
446
- :ui="popoverUi"
447
- >
448
- <AdsButton
449
- type="unfilled"
450
- size="sm"
451
- leading
452
- icon="i-lucide-calendar"
453
- aria-label="Selecionar data"
454
- class="px-0"
455
- :disabled="disabled || readonly"
456
- />
457
-
458
- <template #content>
459
- <UCalendar
460
- :model-value="calendarModel"
461
- :locale="UI_LOCALE_CODE"
462
- :min-value="minCalendarDate"
463
- :max-value="maxCalendarDate"
464
- :is-date-unavailable="isDateUnavailable"
465
- color="primary"
466
- variant="solid"
467
- class="ads-date-picker p-2"
468
- :ui="singleCalendarUi"
469
- @update:model-value="onCalendarModelUpdate"
470
- >
471
- <template #heading="{ value }">
472
- {{ capitalizeFirst(value) }}
473
- </template>
474
- </UCalendar>
475
- </template>
476
- </UPopover>
477
- </template>
478
- </UInputDate>
479
- </template>
1
+ <script setup lang="ts">
2
+ import { isEqualMonth, toCalendarDate, type CalendarDate, type DateValue } from "@internationalized/date";
3
+ import { computed, nextTick, ref, shallowRef, watch } from "vue";
4
+ import AdsButton from "./AdsButton.vue";
5
+ import { UI_LOCALE_CODE } from "../locale/pt-br";
6
+ import { formatDateOnlyToPtBr, getTodayDateString } from "../utils/parser";
7
+ import {
8
+ calendarDateToDateString,
9
+ calendarMonthTitle,
10
+ calendarRangeToDateStrings,
11
+ calendarRangesEqual,
12
+ dateStringToCalendarDate,
13
+ dateStringsToCalendarRange,
14
+ inclusiveCalendarDays,
15
+ isCalendarDateRange,
16
+ isOutsideMaximumWindow,
17
+ shiftVisibleMonth,
18
+ visibleMonthAfterPlaceholder,
19
+ toCalendarModel,
20
+ type CalendarDateRangeValue,
21
+ type CalendarEventValue,
22
+ } from "../utils/calendar-date";
23
+
24
+ const DATE_PLACEHOLDER = "dd/mm/aaaa";
25
+ const RANGE_SEPARATOR = " – ";
26
+
27
+ const props = withDefaults(
28
+ defineProps<{
29
+ range?: boolean;
30
+ min?: string;
31
+ max?: string;
32
+ disabled?: boolean;
33
+ readonly?: boolean;
34
+ size?: "sm" | "md" | "lg";
35
+ maximumDays?: number;
36
+ class?: string;
37
+ /** O valor veio de uma sugestão de IA. Quem usa limpa a marca quando a pessoa edita. */
38
+ aiSuggestion?: boolean;
39
+ }>(),
40
+ {
41
+ range: false,
42
+ size: "md",
43
+ aiSuggestion: false,
44
+ },
45
+ );
46
+
47
+ const modelValue = defineModel<string>({ default: "" });
48
+ const start = defineModel<string>("start", { default: "" });
49
+ const end = defineModel<string>("end", { default: "" });
50
+
51
+ const inputDateRef = useTemplateRef("inputDateRef");
52
+ const syncingFromCalendar = ref(false);
53
+ const rangeOpen = ref(false);
54
+ const rangeCalendarKey = ref(0);
55
+ const ignoreDraftUpdates = ref(false);
56
+ const rangeSessionStart = ref("");
57
+ const rangeSessionEnd = ref("");
58
+
59
+ type CalendarModel = CalendarDate | CalendarDateRangeValue | undefined;
60
+
61
+ const calendarModel = shallowRef<CalendarModel>();
62
+ const draftRange = shallowRef<CalendarDateRangeValue | undefined>();
63
+ const leftPlaceholder = shallowRef<CalendarDate>(
64
+ dateStringToCalendarDate(getTodayDateString())!.set({ day: 1 }),
65
+ );
66
+
67
+ const minCalendarDate = computed(() =>
68
+ props.min ? dateStringToCalendarDate(props.min) : undefined,
69
+ );
70
+
71
+ const maxCalendarDate = computed(() =>
72
+ props.max ? dateStringToCalendarDate(props.max) : undefined,
73
+ );
74
+
75
+ const leftMonth = computed(() => leftPlaceholder.value.set({ day: 1 }));
76
+ const rightMonth = computed(() => leftMonth.value.add({ months: 1 }));
77
+
78
+ const navigationMax = computed(() => {
79
+ const hardMax = maxCalendarDate.value;
80
+ const draft = draftRange.value;
81
+ if (!props.maximumDays || !draft?.start || draft.end) return hardMax;
82
+
83
+ const windowEnd = draft.start.add({ days: props.maximumDays - 1 });
84
+ if (!hardMax || windowEnd.compare(hardMax) < 0) return windowEnd;
85
+ return hardMax;
86
+ });
87
+
88
+ const canShiftToPreviousMonth = computed(
89
+ () =>
90
+ shiftVisibleMonth(
91
+ leftPlaceholder.value,
92
+ -1,
93
+ minCalendarDate.value,
94
+ navigationMax.value,
95
+ ).compare(leftMonth.value) !== 0,
96
+ );
97
+
98
+ const canShiftToNextMonth = computed(
99
+ () =>
100
+ shiftVisibleMonth(
101
+ leftPlaceholder.value,
102
+ 1,
103
+ minCalendarDate.value,
104
+ navigationMax.value,
105
+ ).compare(leftMonth.value) !== 0,
106
+ );
107
+
108
+ const rangeStepLabel = computed(() => {
109
+ const draft = draftRange.value;
110
+ if (!draft?.start) return "Escolha o início";
111
+
112
+ const startLabel = formatDateOnlyToPtBr(calendarDateToDateString(draft.start));
113
+ if (!draft.end) {
114
+ return props.maximumDays
115
+ ? `Escolha o fim. Máximo de ${props.maximumDays} dias.`
116
+ : "Escolha o fim";
117
+ }
118
+
119
+ const endLabel = formatDateOnlyToPtBr(calendarDateToDateString(draft.end));
120
+ const days = inclusiveCalendarDays(draft.start, draft.end);
121
+ const dayLabel = days === 1 ? "dia" : "dias";
122
+ return `${startLabel} – ${endLabel} · ${days} ${dayLabel}`;
123
+ });
124
+
125
+ const canApplyRange = computed(
126
+ () => Boolean(draftRange.value?.start && draftRange.value?.end),
127
+ );
128
+
129
+ const rangeDisplayText = computed(() => {
130
+ const startLabel = start.value
131
+ ? formatDateOnlyToPtBr(start.value)
132
+ : DATE_PLACEHOLDER;
133
+ const endLabel = end.value
134
+ ? formatDateOnlyToPtBr(end.value)
135
+ : DATE_PLACEHOLDER;
136
+
137
+ if (!start.value && !end.value) {
138
+ return `${DATE_PLACEHOLDER}${RANGE_SEPARATOR}${DATE_PLACEHOLDER}`;
139
+ }
140
+
141
+ return `${startLabel}${RANGE_SEPARATOR}${endLabel}`;
142
+ });
143
+
144
+ const popoverReference = computed(() => {
145
+ const input = inputDateRef.value;
146
+ const segments = input?.inputsRef;
147
+ if (!segments?.length) return undefined;
148
+ return segments[segments.length - 1]?.$el;
149
+ });
150
+
151
+ const rangeFieldClass = computed(() => [
152
+ "group relative inline-flex w-full cursor-pointer items-center text-left transition-colors",
153
+ "h-[var(--ads-date-height)] rounded-[var(--ads-date-radius)] px-[var(--ads-date-padding-x)] text-[length:var(--ads-date-font-size)] [font-weight:var(--ads-date-weight)] leading-[var(--ads-date-line-height)] text-[var(--ads-date-text)] bg-[var(--ads-date-background)] ring ring-inset ring-[var(--ads-date-border)]",
154
+ "enabled:hover:ring-[var(--ads-date-hover-border)] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--ads-date-focus-border)]",
155
+ "disabled:cursor-not-allowed disabled:opacity-75",
156
+ props.aiSuggestion && "bg-[var(--ads-button-ia-background)] ring-[var(--ads-button-ia-border)]",
157
+ props.class,
158
+ ]);
159
+
160
+ /** O tema do UInputDate usa `!`, então a sugestão de IA também precisa de `!` para vencer. */
161
+ const inputDateUi = computed(() =>
162
+ props.aiSuggestion
163
+ ? { base: "!bg-[var(--ads-button-ia-background)] !ring-[var(--ads-button-ia-border)]" }
164
+ : undefined,
165
+ );
166
+
167
+ const popoverUi = { content: "w-auto max-w-none p-0" };
168
+
169
+ /**
170
+ * Grade do range no Figma (5712:6691): mês com 258px, 7 colunas de 35,14px e 2px entre elas,
171
+ * linhas de 36px (semana + 5 semanas = 216px) e dia de 32px de altura.
172
+ */
173
+ const rangeCalendarUi = {
174
+ body: "flex-row gap-14 space-x-0 space-y-0 pt-0 sm:space-x-0",
175
+ grid: "w-[258px] space-y-0",
176
+ gridWeekDaysRow: "mb-0 h-9 grid-cols-7 items-center gap-x-0.5",
177
+ gridBody: "grid",
178
+ gridRow: "h-9 grid-cols-7 items-center gap-x-0.5",
179
+ cell: "flex h-9 w-full items-center justify-self-stretch",
180
+ cellTrigger: "m-0 h-8 w-full",
181
+ };
182
+
183
+ /** Intl em pt-BR devolve o mês em minúscula ("outubro de 2026"). Só a primeira letra muda; o ano fica igual. */
184
+ function capitalizeFirst(value: string) {
185
+ return value ? value.charAt(0).toLocaleUpperCase(UI_LOCALE_CODE) + value.slice(1) : value;
186
+ }
187
+
188
+ /** Cabeçalho do mês no Figma (_Date Picker Header, 5709:19265): Inter Bold 14, linha 130%, text/primary. */
189
+ const singleCalendarUi = {
190
+ heading: "text-sm font-bold leading-[1.3] text-[var(--ads-text-primary)]",
191
+ };
192
+
193
+ function monthTitle(date: CalendarDate) {
194
+ return calendarMonthTitle(date, UI_LOCALE_CODE);
195
+ }
196
+
197
+ function onRangePlaceholderUpdate(placeholder: DateValue) {
198
+ const value = toCalendarDate(placeholder);
199
+ const next = visibleMonthAfterPlaceholder(leftPlaceholder.value, value);
200
+ if (next.compare(leftPlaceholder.value) !== 0) {
201
+ leftPlaceholder.value = next;
202
+ return;
203
+ }
204
+ if (isEqualMonth(value, leftPlaceholder.value) && value.day === 1) return;
205
+ leftPlaceholder.value = leftPlaceholder.value.copy();
206
+ }
207
+
208
+ async function moveVisibleMonth(delta: -1 | 1) {
209
+ const next = shiftVisibleMonth(
210
+ leftPlaceholder.value,
211
+ delta,
212
+ minCalendarDate.value,
213
+ navigationMax.value,
214
+ );
215
+ if (next.compare(leftPlaceholder.value) === 0) return;
216
+ leftPlaceholder.value = next.add({ years: 1 });
217
+ await nextTick();
218
+ leftPlaceholder.value = next;
219
+ }
220
+
221
+ function weekdayLetter(day: string) {
222
+ return day.replace(".", "").charAt(0).toUpperCase();
223
+ }
224
+
225
+ function applyExternalModels() {
226
+ if (syncingFromCalendar.value || props.range) return;
227
+
228
+ const next = dateStringToCalendarDate(modelValue.value);
229
+ if (!calendarRangesEqual(calendarModel.value, next)) {
230
+ calendarModel.value = next;
231
+ }
232
+ }
233
+
234
+ function onCalendarModelUpdate(event: CalendarEventValue) {
235
+ const value = toCalendarModel(event);
236
+ if (calendarRangesEqual(calendarModel.value, value)) return;
237
+
238
+ syncingFromCalendar.value = true;
239
+ calendarModel.value = value;
240
+
241
+ if (!props.range && value && !isCalendarDateRange(value)) {
242
+ modelValue.value = calendarDateToDateString(value);
243
+ }
244
+
245
+ nextTick(() => {
246
+ syncingFromCalendar.value = false;
247
+ });
248
+ }
249
+
250
+ function resetDraftFromCommitted() {
251
+ draftRange.value = dateStringsToCalendarRange(start.value, end.value);
252
+ const anchor =
253
+ draftRange.value?.start ?? dateStringToCalendarDate(getTodayDateString())!;
254
+ leftPlaceholder.value = anchor.set({ day: 1 });
255
+ }
256
+
257
+ function onDraftUpdate(value: CalendarEventValue) {
258
+ if (ignoreDraftUpdates.value) return;
259
+ const range = toCalendarModel(value);
260
+ draftRange.value = range && isCalendarDateRange(range) ? range : undefined;
261
+ }
262
+
263
+ function clearDraft() {
264
+ // Os dois calendários guardam o intervalo por conta própria. Remontar
265
+ // descarta o destaque; o flag ignora o eco desse estado ao desmontar. ignoreDraftUpdates.value = true;
266
+ draftRange.value = undefined;
267
+ start.value = "";
268
+ end.value = "";
269
+ rangeCalendarKey.value += 1;
270
+ nextTick(() => {
271
+ draftRange.value = undefined;
272
+ nextTick(() => {
273
+ ignoreDraftUpdates.value = false;
274
+ });
275
+ });
276
+ }
277
+
278
+ function applyDraft(close: () => void) {
279
+ if (!draftRange.value?.start || !draftRange.value.end) return;
280
+ const next = calendarRangeToDateStrings({
281
+ start: draftRange.value.start,
282
+ end: draftRange.value.end,
283
+ });
284
+ start.value = next.start;
285
+ end.value = next.end;
286
+ close();
287
+ }
288
+
289
+ function cancelDraft(close: () => void) {
290
+ start.value = rangeSessionStart.value;
291
+ end.value = rangeSessionEnd.value;
292
+ resetDraftFromCommitted();
293
+ close();
294
+ }
295
+
296
+ watch(rangeOpen, (isOpen) => {
297
+ if (!isOpen) return;
298
+ rangeSessionStart.value = start.value;
299
+ rangeSessionEnd.value = end.value;
300
+ resetDraftFromCommitted();
301
+ });
302
+
303
+ watch([modelValue, start, end, () => props.range], applyExternalModels, {
304
+ immediate: true,
305
+ });
306
+
307
+ function isDateUnavailable(value: DateValue) {
308
+ const date = toCalendarDate(value);
309
+ if (minCalendarDate.value && date.compare(minCalendarDate.value) < 0) {
310
+ return true;
311
+ }
312
+ if (maxCalendarDate.value && date.compare(maxCalendarDate.value) > 0) {
313
+ return true;
314
+ }
315
+ if (!props.range || !props.maximumDays) return false;
316
+ const draft = draftRange.value;
317
+ if (!draft?.start || draft.end) return false;
318
+ return isOutsideMaximumWindow(date, draft.start, props.maximumDays);
319
+ }
320
+ </script>
321
+
322
+ <template>
323
+ <UPopover
324
+ v-if="range"
325
+ v-model:open="rangeOpen"
326
+ :content="{ align: 'start', side: 'bottom' }"
327
+ :ui="popoverUi"
328
+ >
329
+ <button
330
+ type="button"
331
+ :disabled="disabled || readonly"
332
+ :aria-label="`Selecionar período. ${rangeDisplayText}`"
333
+ :class="rangeFieldClass"
334
+ >
335
+ <span
336
+ class="min-w-0 flex-1 truncate tabular-nums"
337
+ :class="start || end ? 'text-[var(--ads-date-text)]' : 'text-[var(--ads-date-placeholder)]'"
338
+ >
339
+ {{ rangeDisplayText }}
340
+ </span>
341
+ <UIcon name="i-lucide-calendar" class="size-5 shrink-0 text-[var(--ads-date-placeholder)]" />
342
+ </button>
343
+
344
+ <template #content="{ close }">
345
+ <div class="ads-date-range-panel max-w-[calc(100vw-2rem)] overflow-x-auto bg-white">
346
+ <p class="px-4 pt-4 text-sm font-medium text-neutral-900" aria-live="polite">
347
+ {{ rangeStepLabel }}
348
+ </p>
349
+ <!-- Figma Range Date Picker (5712:6691): dois painéis de 290×284, 24px entre eles. -->
350
+ <div class="relative w-[604px]">
351
+ <div class="pointer-events-none absolute inset-0 grid grid-cols-[290px_290px] gap-6" aria-hidden="true">
352
+ <div class="rounded-s-[var(--ads-radius-xsm)] bg-[var(--ads-surface-card)]" />
353
+ <div class="rounded-e-[var(--ads-radius-xsm)] bg-[var(--ads-surface-card)]" />
354
+ </div>
355
+ <div class="relative flex flex-col gap-3 p-4">
356
+ <div class="grid grid-cols-[258px_258px] gap-x-14">
357
+ <div class="flex h-6 items-center justify-between">
358
+ <button
359
+ type="button"
360
+ class="ads-date-range-nav"
361
+ aria-label="Mês anterior"
362
+ :disabled="!canShiftToPreviousMonth"
363
+ @click="moveVisibleMonth(-1)"
364
+ >
365
+ <UIcon name="i-lucide-chevron-left" class="size-6" />
366
+ </button>
367
+ <span class="ads-date-range-title">{{ monthTitle(leftMonth) }}</span>
368
+ <span class="size-6 shrink-0" aria-hidden="true" />
369
+ </div>
370
+ <div class="flex h-6 items-center justify-between">
371
+ <span class="size-6 shrink-0" aria-hidden="true" />
372
+ <span class="ads-date-range-title">{{ monthTitle(rightMonth) }}</span>
373
+ <button
374
+ type="button"
375
+ class="ads-date-range-nav"
376
+ aria-label="Próximo mês"
377
+ :disabled="!canShiftToNextMonth"
378
+ @click="moveVisibleMonth(1)"
379
+ >
380
+ <UIcon name="i-lucide-chevron-right" class="size-6" />
381
+ </button>
382
+ </div>
383
+ </div>
384
+ <UCalendar
385
+ :key="`range-${rangeCalendarKey}`"
386
+ :model-value="draftRange"
387
+ :placeholder="leftMonth"
388
+ :locale="UI_LOCALE_CODE"
389
+ range
390
+ :number-of-months="2"
391
+ :month-controls="false"
392
+ :year-controls="false"
393
+ weekday-format="narrow"
394
+ :week-starts-on="0"
395
+ :min-value="minCalendarDate"
396
+ :max-value="maxCalendarDate"
397
+ disable-days-outside-current-view
398
+ :is-date-unavailable="isDateUnavailable"
399
+ color="neutral"
400
+ variant="outline"
401
+ class="ads-date-picker"
402
+ :ui="rangeCalendarUi"
403
+ @update:model-value="onDraftUpdate"
404
+ @update:placeholder="onRangePlaceholderUpdate"
405
+ >
406
+ <template #week-day="{ day }">
407
+ {{ weekdayLetter(day) }}
408
+ </template>
409
+ </UCalendar>
410
+ </div>
411
+ </div>
412
+
413
+ <div class="flex items-center justify-between gap-3 px-4 pb-4">
414
+ <AdsButton
415
+ type="unfilled"
416
+ size="sm"
417
+ leading
418
+ icon="i-lucide-x"
419
+ label="Limpar seleção"
420
+ @click="clearDraft"
421
+ />
422
+ <div class="flex items-center gap-2">
423
+ <AdsButton
424
+ type="unfilled"
425
+ label="Cancelar"
426
+ @click="cancelDraft(close)"
427
+ />
428
+ <AdsButton
429
+ label="Aplicar"
430
+ :disabled="!canApplyRange"
431
+ @click="applyDraft(close)"
432
+ />
433
+ </div>
434
+ </div>
435
+ </div>
436
+ </template>
437
+ </UPopover>
438
+
439
+ <UInputDate
440
+ v-else
441
+ ref="inputDateRef"
442
+ :model-value="calendarModel"
443
+ :locale="UI_LOCALE_CODE"
444
+ :size="size"
445
+ :ui="inputDateUi"
446
+ :disabled="disabled"
447
+ :readonly="readonly"
448
+ :min-value="minCalendarDate"
449
+ :max-value="maxCalendarDate"
450
+ :is-date-unavailable="isDateUnavailable"
451
+ trailing
452
+ :class="props.class"
453
+ @update:model-value="onCalendarModelUpdate"
454
+ >
455
+ <template #trailing>
456
+ <UPopover
457
+ :reference="popoverReference"
458
+ :content="{ align: 'end', side: 'bottom' }"
459
+ :ui="popoverUi"
460
+ >
461
+ <AdsButton
462
+ type="unfilled"
463
+ size="sm"
464
+ leading
465
+ icon="i-lucide-calendar"
466
+ aria-label="Selecionar data"
467
+ class="px-0"
468
+ :disabled="disabled || readonly"
469
+ />
470
+
471
+ <template #content>
472
+ <UCalendar
473
+ :model-value="calendarModel"
474
+ :locale="UI_LOCALE_CODE"
475
+ :min-value="minCalendarDate"
476
+ :max-value="maxCalendarDate"
477
+ :is-date-unavailable="isDateUnavailable"
478
+ color="primary"
479
+ variant="solid"
480
+ class="ads-date-picker p-2"
481
+ :ui="singleCalendarUi"
482
+ @update:model-value="onCalendarModelUpdate"
483
+ >
484
+ <template #heading="{ value }">
485
+ {{ capitalizeFirst(value) }}
486
+ </template>
487
+ </UCalendar>
488
+ </template>
489
+ </UPopover>
490
+ </template>
491
+ </UInputDate>
492
+ </template>