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,235 +1,235 @@
1
- <template>
2
- <div :class="rootClass">
3
- <div v-if="hasHeader" class="flex items-start gap-2">
4
- <UIcon
5
- v-if="showIcon"
6
- :name="resolvedIcon"
7
- class="mt-px size-[18px] shrink-0"
8
- :class="[
9
- isSuccess ? 'text-[var(--ads-progress-success)]' : 'text-[var(--ads-progress-fill)]',
10
- { 'animate-spin motion-reduce:animate-none': spinIcon && !isSuccess },
11
- ]"
12
- aria-hidden="true"
13
- />
14
- <div class="flex min-w-0 flex-1 flex-col gap-0.5 text-sm leading-[1.3]">
15
- <p v-if="title" class="font-bold text-[var(--ads-text-primary)]">{{ title }}</p>
16
- <p v-if="description" class="text-[var(--ads-text-secondary)]">{{ description }}</p>
17
- </div>
18
- <span
19
- v-if="reservePercentSlot"
20
- class="shrink-0 text-sm leading-[1.3] font-medium tabular-nums text-[var(--ads-text-primary)]"
21
- :class="{ invisible: !showPercent }"
22
- :aria-hidden="!showPercent"
23
- >
24
- {{ indeterminate ? "—" : `${percentClamped}%` }}
25
- </span>
26
- </div>
27
-
28
- <div
29
- class="relative h-[var(--ads-progress-height)] w-full overflow-hidden rounded-[var(--ads-progress-radius)] bg-[var(--ads-progress-track)]"
30
- role="progressbar"
31
- :aria-valuenow="indeterminate ? undefined : percentClamped"
32
- aria-valuemin="0"
33
- aria-valuemax="100"
34
- :aria-busy="indeterminate ? true : undefined"
35
- :aria-label="effectiveAriaLabel"
36
- >
37
- <div
38
- v-if="indeterminate"
39
- class="progress-bar-loading__indeterminate absolute inset-y-0 rounded-[var(--ads-progress-radius)]"
40
- :class="fillClass"
41
- />
42
- <div
43
- v-else
44
- class="h-full rounded-[var(--ads-progress-radius)] transition-[width] duration-300 ease-out"
45
- :class="fillClass"
46
- :style="{ width: `${percentClamped}%` }"
47
- />
48
- </div>
49
-
50
- <p
51
- v-if="reserveElapsedTimeSlot"
52
- class="text-xs leading-[1.2] tabular-nums text-[var(--ads-text-secondary)]"
53
- :class="{ invisible: !showElapsedTime }"
54
- role="timer"
55
- aria-live="off"
56
- :aria-hidden="!showElapsedTime"
57
- >
58
- Tempo decorrido: {{ elapsedDisplay }}
59
- </p>
60
- </div>
61
- </template>
62
-
63
- <script setup lang="ts">
64
- import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
65
-
66
- type ProgressBarLoadingVariant = "primary" | "success";
67
-
68
- interface Props {
69
- title?: string;
70
- description?: string;
71
- /** Ícone do andamento. No sucesso vira o check verde. */
72
- icon?: string;
73
- showIcon?: boolean;
74
- spinIcon?: boolean;
75
- /** Sem percentual conhecido: mostra "—" e um trecho da barra que atravessa a trilha. */
76
- indeterminate?: boolean;
77
- percent?: number;
78
- showPercent?: boolean;
79
- /** "Tempo decorrido: 00:42", abaixo da barra. */
80
- showElapsedTime?: boolean;
81
- ariaLabel?: string;
82
- variant?: ProgressBarLoadingVariant;
83
- rootClass?: string;
84
- }
85
-
86
- const props = withDefaults(defineProps<Props>(), {
87
- title: "",
88
- description: "",
89
- icon: "i-lucide-loader-circle",
90
- showIcon: true,
91
- spinIcon: true,
92
- indeterminate: false,
93
- percent: 0,
94
- showPercent: true,
95
- showElapsedTime: true,
96
- ariaLabel: "",
97
- variant: "primary",
98
- rootClass: "flex w-full flex-col gap-3",
99
- });
100
-
101
- const percentClamped = computed(() =>
102
- Math.max(0, Math.min(100, Math.round(props.percent ?? 0))),
103
- );
104
-
105
- const isSuccess = computed(() => props.variant === "success");
106
-
107
- /** Em andamento: ícone de carregamento azul. Sucesso: check verde, como no Figma. */
108
- const resolvedIcon = computed(() => (isSuccess.value ? "i-lucide-circle-check" : props.icon));
109
-
110
- const fillClass = computed(() =>
111
- isSuccess.value ? "bg-[var(--ads-progress-success)]" : "bg-[var(--ads-progress-fill)]",
112
- );
113
-
114
- const effectiveAriaLabel = computed(
115
- () => props.ariaLabel || props.title || "Carregando",
116
- );
117
-
118
- /**
119
- * Slots reservados de forma "sticky-true": uma vez necessário (cabeçalho, tempo ou
120
- * percentual), o nó fica renderizado. Assim a troca de estado não move nada no eixo Y;
121
- * a alternância visual usa só `invisible`.
122
- */
123
- const computedHasHeader = () =>
124
- Boolean(props.showIcon) ||
125
- Boolean(props.title) ||
126
- Boolean(props.description) ||
127
- Boolean(props.showPercent);
128
-
129
- const hasHeader = ref(computedHasHeader());
130
- const reserveElapsedTimeSlot = ref(Boolean(props.showElapsedTime));
131
- const reservePercentSlot = ref(Boolean(props.showPercent));
132
-
133
- watch(computedHasHeader, (val) => {
134
- if (val) hasHeader.value = true;
135
- });
136
-
137
- watch(
138
- () => Boolean(props.showElapsedTime),
139
- (val) => {
140
- if (val) reserveElapsedTimeSlot.value = true;
141
- },
142
- );
143
-
144
- watch(
145
- () => Boolean(props.showPercent),
146
- (val) => {
147
- if (val) reservePercentSlot.value = true;
148
- },
149
- );
150
-
151
- const startedAt = ref<number>(0);
152
- const now = ref<number>(0);
153
- let elapsedTimerId: ReturnType<typeof setInterval> | null = null;
154
-
155
- const stopElapsedTimer = () => {
156
- if (elapsedTimerId != null) {
157
- clearInterval(elapsedTimerId);
158
- elapsedTimerId = null;
159
- }
160
- };
161
-
162
- const startElapsedTimer = () => {
163
- stopElapsedTimer();
164
- startedAt.value = Date.now();
165
- now.value = startedAt.value;
166
- if (typeof window === "undefined") {
167
- return;
168
- }
169
- elapsedTimerId = setInterval(() => {
170
- now.value = Date.now();
171
- }, 1000);
172
- };
173
-
174
- const elapsedSeconds = computed(() =>
175
- Math.max(0, Math.floor((now.value - startedAt.value) / 1000)),
176
- );
177
-
178
- const elapsedDisplay = computed(() => {
179
- const total = elapsedSeconds.value;
180
- const hours = Math.floor(total / 3600);
181
- const minutes = Math.floor((total % 3600) / 60);
182
- const seconds = total % 60;
183
- const pad = (n: number) => String(n).padStart(2, "0");
184
- if (hours > 0) {
185
- return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
186
- }
187
- return `${pad(minutes)}:${pad(seconds)}`;
188
- });
189
-
190
- watch(
191
- () => props.showElapsedTime,
192
- (enabled) => {
193
- if (enabled) {
194
- startElapsedTimer();
195
- } else {
196
- stopElapsedTimer();
197
- }
198
- },
199
- );
200
-
201
- onMounted(() => {
202
- if (props.showElapsedTime) {
203
- startElapsedTimer();
204
- }
205
- });
206
-
207
- onBeforeUnmount(() => {
208
- stopElapsedTimer();
209
- });
210
- </script>
211
-
212
- <style scoped>
213
- /* Indeterminado: trecho de 30% da trilha (no Figma, parado no meio) que atravessa a barra. */
214
- .progress-bar-loading__indeterminate {
215
- width: 30%;
216
- animation: progress-bar-loading-indeterminate 1.4s ease-in-out infinite;
217
- }
218
-
219
- @keyframes progress-bar-loading-indeterminate {
220
- 0% {
221
- left: -30%;
222
- }
223
-
224
- 100% {
225
- left: 100%;
226
- }
227
- }
228
-
229
- @media (prefers-reduced-motion: reduce) {
230
- .progress-bar-loading__indeterminate {
231
- left: 35%;
232
- animation: none;
233
- }
234
- }
235
- </style>
1
+ <template>
2
+ <div :class="rootClass">
3
+ <div v-if="hasHeader" class="flex items-start gap-2">
4
+ <UIcon
5
+ v-if="showIcon"
6
+ :name="resolvedIcon"
7
+ class="mt-px size-[18px] shrink-0"
8
+ :class="[
9
+ isSuccess ? 'text-[var(--ads-progress-success)]' : 'text-[var(--ads-progress-fill)]',
10
+ { 'animate-spin motion-reduce:animate-none': spinIcon && !isSuccess },
11
+ ]"
12
+ aria-hidden="true"
13
+ />
14
+ <div class="flex min-w-0 flex-1 flex-col gap-0.5 text-sm leading-[1.3]">
15
+ <p v-if="title" class="font-bold text-[var(--ads-text-primary)]">{{ title }}</p>
16
+ <p v-if="description" class="text-[var(--ads-text-secondary)]">{{ description }}</p>
17
+ </div>
18
+ <span
19
+ v-if="reservePercentSlot"
20
+ class="shrink-0 text-sm leading-[1.3] font-medium tabular-nums text-[var(--ads-text-primary)]"
21
+ :class="{ invisible: !showPercent }"
22
+ :aria-hidden="!showPercent"
23
+ >
24
+ {{ indeterminate ? "—" : `${percentClamped}%` }}
25
+ </span>
26
+ </div>
27
+
28
+ <div
29
+ class="relative h-[var(--ads-progress-height)] w-full overflow-hidden rounded-[var(--ads-progress-radius)] bg-[var(--ads-progress-track)]"
30
+ role="progressbar"
31
+ :aria-valuenow="indeterminate ? undefined : percentClamped"
32
+ aria-valuemin="0"
33
+ aria-valuemax="100"
34
+ :aria-busy="indeterminate ? true : undefined"
35
+ :aria-label="effectiveAriaLabel"
36
+ >
37
+ <div
38
+ v-if="indeterminate"
39
+ class="progress-bar-loading__indeterminate absolute inset-y-0 rounded-[var(--ads-progress-radius)]"
40
+ :class="fillClass"
41
+ />
42
+ <div
43
+ v-else
44
+ class="h-full rounded-[var(--ads-progress-radius)] transition-[width] duration-300 ease-out"
45
+ :class="fillClass"
46
+ :style="{ width: `${percentClamped}%` }"
47
+ />
48
+ </div>
49
+
50
+ <p
51
+ v-if="reserveElapsedTimeSlot"
52
+ class="text-xs leading-[1.2] tabular-nums text-[var(--ads-text-secondary)]"
53
+ :class="{ invisible: !showElapsedTime }"
54
+ role="timer"
55
+ aria-live="off"
56
+ :aria-hidden="!showElapsedTime"
57
+ >
58
+ Tempo decorrido: {{ elapsedDisplay }}
59
+ </p>
60
+ </div>
61
+ </template>
62
+
63
+ <script setup lang="ts">
64
+ import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
65
+
66
+ type ProgressBarLoadingVariant = "primary" | "success";
67
+
68
+ interface Props {
69
+ title?: string;
70
+ description?: string;
71
+ /** Ícone do andamento. No sucesso vira o check verde. */
72
+ icon?: string;
73
+ showIcon?: boolean;
74
+ spinIcon?: boolean;
75
+ /** Sem percentual conhecido: mostra "—" e um trecho da barra que atravessa a trilha. */
76
+ indeterminate?: boolean;
77
+ percent?: number;
78
+ showPercent?: boolean;
79
+ /** "Tempo decorrido: 00:42", abaixo da barra. */
80
+ showElapsedTime?: boolean;
81
+ ariaLabel?: string;
82
+ variant?: ProgressBarLoadingVariant;
83
+ rootClass?: string;
84
+ }
85
+
86
+ const props = withDefaults(defineProps<Props>(), {
87
+ title: "",
88
+ description: "",
89
+ icon: "i-lucide-loader-circle",
90
+ showIcon: true,
91
+ spinIcon: true,
92
+ indeterminate: false,
93
+ percent: 0,
94
+ showPercent: true,
95
+ showElapsedTime: true,
96
+ ariaLabel: "",
97
+ variant: "primary",
98
+ rootClass: "flex w-full flex-col gap-3",
99
+ });
100
+
101
+ const percentClamped = computed(() =>
102
+ Math.max(0, Math.min(100, Math.round(props.percent ?? 0))),
103
+ );
104
+
105
+ const isSuccess = computed(() => props.variant === "success");
106
+
107
+ /** Em andamento: ícone de carregamento azul. Sucesso: check verde, como no Figma. */
108
+ const resolvedIcon = computed(() => (isSuccess.value ? "i-lucide-circle-check" : props.icon));
109
+
110
+ const fillClass = computed(() =>
111
+ isSuccess.value ? "bg-[var(--ads-progress-success)]" : "bg-[var(--ads-progress-fill)]",
112
+ );
113
+
114
+ const effectiveAriaLabel = computed(
115
+ () => props.ariaLabel || props.title || "Carregando",
116
+ );
117
+
118
+ /**
119
+ * Slots reservados de forma "sticky-true": uma vez necessário (cabeçalho, tempo ou
120
+ * percentual), o nó fica renderizado. Assim a troca de estado não move nada no eixo Y;
121
+ * a alternância visual usa só `invisible`.
122
+ */
123
+ const computedHasHeader = () =>
124
+ Boolean(props.showIcon) ||
125
+ Boolean(props.title) ||
126
+ Boolean(props.description) ||
127
+ Boolean(props.showPercent);
128
+
129
+ const hasHeader = ref(computedHasHeader());
130
+ const reserveElapsedTimeSlot = ref(Boolean(props.showElapsedTime));
131
+ const reservePercentSlot = ref(Boolean(props.showPercent));
132
+
133
+ watch(computedHasHeader, (val) => {
134
+ if (val) hasHeader.value = true;
135
+ });
136
+
137
+ watch(
138
+ () => Boolean(props.showElapsedTime),
139
+ (val) => {
140
+ if (val) reserveElapsedTimeSlot.value = true;
141
+ },
142
+ );
143
+
144
+ watch(
145
+ () => Boolean(props.showPercent),
146
+ (val) => {
147
+ if (val) reservePercentSlot.value = true;
148
+ },
149
+ );
150
+
151
+ const startedAt = ref<number>(0);
152
+ const now = ref<number>(0);
153
+ let elapsedTimerId: ReturnType<typeof setInterval> | null = null;
154
+
155
+ const stopElapsedTimer = () => {
156
+ if (elapsedTimerId != null) {
157
+ clearInterval(elapsedTimerId);
158
+ elapsedTimerId = null;
159
+ }
160
+ };
161
+
162
+ const startElapsedTimer = () => {
163
+ stopElapsedTimer();
164
+ startedAt.value = Date.now();
165
+ now.value = startedAt.value;
166
+ if (typeof window === "undefined") {
167
+ return;
168
+ }
169
+ elapsedTimerId = setInterval(() => {
170
+ now.value = Date.now();
171
+ }, 1000);
172
+ };
173
+
174
+ const elapsedSeconds = computed(() =>
175
+ Math.max(0, Math.floor((now.value - startedAt.value) / 1000)),
176
+ );
177
+
178
+ const elapsedDisplay = computed(() => {
179
+ const total = elapsedSeconds.value;
180
+ const hours = Math.floor(total / 3600);
181
+ const minutes = Math.floor((total % 3600) / 60);
182
+ const seconds = total % 60;
183
+ const pad = (n: number) => String(n).padStart(2, "0");
184
+ if (hours > 0) {
185
+ return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
186
+ }
187
+ return `${pad(minutes)}:${pad(seconds)}`;
188
+ });
189
+
190
+ watch(
191
+ () => props.showElapsedTime,
192
+ (enabled) => {
193
+ if (enabled) {
194
+ startElapsedTimer();
195
+ } else {
196
+ stopElapsedTimer();
197
+ }
198
+ },
199
+ );
200
+
201
+ onMounted(() => {
202
+ if (props.showElapsedTime) {
203
+ startElapsedTimer();
204
+ }
205
+ });
206
+
207
+ onBeforeUnmount(() => {
208
+ stopElapsedTimer();
209
+ });
210
+ </script>
211
+
212
+ <style scoped>
213
+ /* Indeterminado: trecho de 30% da trilha (no Figma, parado no meio) que atravessa a barra. */
214
+ .progress-bar-loading__indeterminate {
215
+ width: 30%;
216
+ animation: progress-bar-loading-indeterminate 1.4s ease-in-out infinite;
217
+ }
218
+
219
+ @keyframes progress-bar-loading-indeterminate {
220
+ 0% {
221
+ left: -30%;
222
+ }
223
+
224
+ 100% {
225
+ left: 100%;
226
+ }
227
+ }
228
+
229
+ @media (prefers-reduced-motion: reduce) {
230
+ .progress-bar-loading__indeterminate {
231
+ left: 35%;
232
+ animation: none;
233
+ }
234
+ }
235
+ </style>
@@ -1,88 +1,88 @@
1
- import type { OverlayLoadingOptions } from "../types/overlay-loading.types";
2
-
3
- export const useOverlayLoading = () => {
4
- const overlay = useOverlay();
5
- const OverlayLoadingComponent = defineAsyncComponent(
6
- () => import("../components/OverlayLoading.vue")
7
- );
8
-
9
- let currentOverlay: {
10
- id: symbol;
11
- open: () => void;
12
- close: () => void;
13
- patch: (props: Record<string, unknown>) => void;
14
- } | null = null;
15
- /**
16
- * O overlay é destruído ao fechar (destroyOnClose). Fechar pelo X ou pela ação emite `close`
17
- * direto no useOverlay, sem passar por aqui; chamar close/patch depois disso lança
18
- * "Overlay not found". Por isso a referência é limpa quando o overlay fecha, por qualquer caminho.
19
- */
20
- const safely = (action: () => void) => {
21
- try {
22
- action();
23
- } catch {
24
- currentOverlay = null;
25
- }
26
- };
27
-
28
- const show = (options: OverlayLoadingOptions) => {
29
- if (currentOverlay) {
30
- const previous = currentOverlay;
31
- safely(() => previous.close());
32
- }
33
- const created = overlay.create(OverlayLoadingComponent, {
34
- destroyOnClose: true,
35
- props: {
36
- isVisible: true,
37
- ...options,
38
- },
39
- });
40
- currentOverlay = created;
41
-
42
- const opened = created.open();
43
- void Promise.resolve(opened).then(() => {
44
- if (currentOverlay?.id === created.id) currentOverlay = null;
45
- });
46
-
47
- return created.id;
48
- };
49
-
50
- const close = () => {
51
- if (currentOverlay) {
52
- const target = currentOverlay;
53
- currentOverlay = null;
54
- safely(() => target.close());
55
- }
56
- };
57
-
58
- /**
59
- * Mescla props no componente do overlay. Props devem ser planas (title, description, …):
60
- * o `patch` do useOverlay faz `{ ...overlay.props, ...patch }`, não aceita `{ props: { … } }`.
61
- */
62
- const update = (options: Partial<OverlayLoadingOptions>) => {
63
- if (currentOverlay) {
64
- const target = currentOverlay;
65
- safely(() =>
66
- target.patch({
67
- isVisible: true,
68
- ...options,
69
- }),
70
- );
71
- }
72
- };
73
-
74
- const isOpen = () => {
75
- return currentOverlay !== null;
76
- };
77
-
78
- const getCurrentId = () => currentOverlay?.id ?? null;
79
-
80
- return {
81
- show,
82
- close,
83
- update,
84
- isOpen,
85
- getCurrentId,
86
- };
87
- };
88
-
1
+ import type { OverlayLoadingOptions } from "../types/overlay-loading.types";
2
+
3
+ export const useOverlayLoading = () => {
4
+ const overlay = useOverlay();
5
+ const OverlayLoadingComponent = defineAsyncComponent(
6
+ () => import("../components/OverlayLoading.vue")
7
+ );
8
+
9
+ let currentOverlay: {
10
+ id: symbol;
11
+ open: () => void;
12
+ close: () => void;
13
+ patch: (props: Record<string, unknown>) => void;
14
+ } | null = null;
15
+ /**
16
+ * O overlay é destruído ao fechar (destroyOnClose). Fechar pela ação emite `close`
17
+ * direto no useOverlay, sem passar por aqui; chamar close/patch depois disso lança
18
+ * "Overlay not found". Por isso a referência é limpa quando o overlay fecha, por qualquer caminho.
19
+ */
20
+ const safely = (action: () => void) => {
21
+ try {
22
+ action();
23
+ } catch {
24
+ currentOverlay = null;
25
+ }
26
+ };
27
+
28
+ const show = (options: OverlayLoadingOptions) => {
29
+ if (currentOverlay) {
30
+ const previous = currentOverlay;
31
+ safely(() => previous.close());
32
+ }
33
+ const created = overlay.create(OverlayLoadingComponent, {
34
+ destroyOnClose: true,
35
+ props: {
36
+ isVisible: true,
37
+ ...options,
38
+ },
39
+ });
40
+ currentOverlay = created;
41
+
42
+ const opened = created.open();
43
+ void Promise.resolve(opened).then(() => {
44
+ if (currentOverlay?.id === created.id) currentOverlay = null;
45
+ });
46
+
47
+ return created.id;
48
+ };
49
+
50
+ const close = () => {
51
+ if (currentOverlay) {
52
+ const target = currentOverlay;
53
+ currentOverlay = null;
54
+ safely(() => target.close());
55
+ }
56
+ };
57
+
58
+ /**
59
+ * Mescla props no componente do overlay. Props devem ser planas (title, description, …):
60
+ * o `patch` do useOverlay faz `{ ...overlay.props, ...patch }`, não aceita `{ props: { … } }`.
61
+ */
62
+ const update = (options: Partial<OverlayLoadingOptions>) => {
63
+ if (currentOverlay) {
64
+ const target = currentOverlay;
65
+ safely(() =>
66
+ target.patch({
67
+ isVisible: true,
68
+ ...options,
69
+ }),
70
+ );
71
+ }
72
+ };
73
+
74
+ const isOpen = () => {
75
+ return currentOverlay !== null;
76
+ };
77
+
78
+ const getCurrentId = () => currentOverlay?.id ?? null;
79
+
80
+ return {
81
+ show,
82
+ close,
83
+ update,
84
+ isOpen,
85
+ getCurrentId,
86
+ };
87
+ };
88
+