ads-nuxt 0.4.0 → 0.4.2

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