@nexdom/uimed-vue 2.0.0-beta.31 → 2.0.0-beta.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md CHANGED
@@ -203,8 +203,9 @@ Use an existing composable (e.g. `use-toast`) as the reference, and deliver in t
203
203
  ```
204
204
 
205
205
  - Component layout: `src/components/<name>/<name>.vue`, `types.ts` for prop/option types, `__tests__/<name>.test.ts` for unit tests. Components that belong to a group live in the group's folder (e.g. the internal `src/components/dialogs/toast.vue`).
206
- - Composables live in a group folder: `src/composables/<group>/<name>.ts`, with `types.ts` and `__tests__/` in the group folder (e.g. `src/composables/dialogs/use-toast.ts`).
206
+ - Composables live in a group folder: `src/composables/<group>/<name>.ts`, with `types.ts` and `__tests__/` in the group folder (e.g. `src/composables/dialogs/use-toast.ts`). A composable (`use-*.ts`) must depend on `vue` or `vue-router`, directly or through other modules (enforced by dependency-cruiser's `composables-use-vue` rule).
207
207
  - Library-wide configuration constants that aren't components or composables live in `src/consts/<name>.ts` (e.g. `src/consts/locale.ts`, the language set by `createUimed()` and `vueTestUtilsPluginUimed()`).
208
+ - Pure helpers that don't use Vue live in `src/utils/<name>.ts`, with tests in `src/utils/__tests__/`. Generic helpers that any TypeScript project could use (e.g. `isDeepEqual`) belong in `@nexdom/shared` instead.
208
209
  - Public components/composables are re-exported from `src/components/index.ts` and `src/composables/index.ts`. Prop types stay internal (`types.ts` is not re-exported); consumers derive them with `ComponentProps` (see [Usage](#usage)).
209
210
  - Every publicly exported component must use the `U` prefix on its export identifier (e.g., `UButton`, `UMain`), while internal file names and component names remain unprefixed.
210
211
  - Use [JSDoc](https://jsdoc.app/about-getting-started) on every method/prop/type intended to be part of the public API — it's the primary documentation surface and supports markdown/code examples.
@@ -333,7 +333,11 @@ var app_bar_default = /* @__PURE__ */ defineComponent({
333
333
  class: "py-2 ml-5 mr-n3",
334
334
  cover: ""
335
335
  }, null, 8, ["src"])) : createCommentVNode("v-if", true),
336
- props.title ? (openBlock(), createBlock(unref(VAppBarTitle), { key: 2 }, {
336
+ props.title ? (openBlock(), createBlock(unref(VAppBarTitle), {
337
+ key: 2,
338
+ role: "heading",
339
+ "aria-level": "1"
340
+ }, {
337
341
  default: withCtx(() => [createTextVNode(toDisplayString(props.title), 1)]),
338
342
  _: 1
339
343
  })) : createCommentVNode("v-if", true)
@@ -1,4 +1,5 @@
1
1
  import { i as FeedbackColorVariant, n as ButtonVariant, r as ColorVariant } from "./types-DNT_odv7.js";
2
+ import { ComputedRef, MaybeRefOrGetter } from "vue";
2
3
  //#region src/composables/dialogs/types.d.ts
3
4
  interface ToastOptions {
4
5
  /**
@@ -190,4 +191,89 @@ declare function useConfirm(): {
190
191
  isRunning: import("vue").ComputedRef<boolean>;
191
192
  };
192
193
  //#endregion
193
- export { useConfirm, useDialog, useRunOrToast, useToast };
194
+ //#region src/composables/navigation/types.d.ts
195
+ /**
196
+ * Values compared by `useUnsavedChanges`.
197
+ */
198
+ interface UnsavedChangesValues<T> {
199
+ /**
200
+ * Original (last saved) value. To mark the form as saved, assign it a copy of the current value.
201
+ */
202
+ previous: MaybeRefOrGetter<T>;
203
+ /**
204
+ * Current value of the form.
205
+ */
206
+ current: MaybeRefOrGetter<T>;
207
+ }
208
+ /**
209
+ * Options of `useUnsavedChanges`.
210
+ */
211
+ interface UnsavedChangesOptions {
212
+ /**
213
+ * Plain text displayed as the confirmation dialog's title. HTML isn't interpreted.
214
+ * @default "Alterações não salvas"
215
+ */
216
+ title?: string;
217
+ /**
218
+ * Plain text displayed as the confirmation dialog's message. HTML isn't interpreted.
219
+ * @default "Existem alterações que ainda não foram salvas. Deseja sair sem salvar?"
220
+ */
221
+ message?: string;
222
+ /**
223
+ * Text displayed on the button which leaves the page.
224
+ * @default "Sair sem salvar"
225
+ */
226
+ confirmText?: string;
227
+ /**
228
+ * Text displayed on the button which cancels the navigation.
229
+ * @default "Continuar editando"
230
+ */
231
+ cancelText?: string;
232
+ /**
233
+ * Applies a color to the button which leaves the page.
234
+ * One of `primary`, `secondary`, `positive`, `informative`, `caution`, or `danger`.
235
+ * @default "danger"
236
+ */
237
+ color?: ColorVariant;
238
+ /**
239
+ * Also asks for the browser's native confirmation on reload (F5), tab close or leaving the app
240
+ * through an external link.
241
+ * @default true
242
+ */
243
+ beforeUnload?: boolean;
244
+ }
245
+ //#endregion
246
+ //#region src/composables/navigation/use-unsaved-changes.d.ts
247
+ /**
248
+ * Composable which compares the original and the current values of a form, and asks the user to
249
+ * confirm before leaving the page while they differ.
250
+ *
251
+ * Must be called in the `setup` of a component. Leaving through vue-router is only guarded in
252
+ * components rendered by a `<RouterView>`, and the confirmation dialog is displayed by `UMain`.
253
+ *
254
+ * @param values - The original (`previous`) and current (`current`) values, as refs, getters or
255
+ * reactive objects. They're compared in depth: key order doesn't matter, a key holding `undefined`
256
+ * equals a missing key, dates are compared by their time and arrays item by item, in order.
257
+ * @param options - Texts and color of the confirmation dialog, and whether to ask for the
258
+ * browser's native confirmation before unloading the page.
259
+ * @returns `hasChanges`, a computed property which is `true` while the values differ
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * const saved = ref({ name: "Maria" });
264
+ * const form = ref(structuredClone(toRaw(saved.value)));
265
+ *
266
+ * const { hasChanges } = useUnsavedChanges({ previous: saved, current: form });
267
+ *
268
+ * async function save() {
269
+ * await api.save(form.value);
270
+ * // Marks the form as saved
271
+ * saved.value = structuredClone(toRaw(form.value));
272
+ * }
273
+ * ```
274
+ */
275
+ declare function useUnsavedChanges<T>(values: UnsavedChangesValues<T>, options?: UnsavedChangesOptions): {
276
+ hasChanges: ComputedRef<boolean>;
277
+ };
278
+ //#endregion
279
+ export { useConfirm, useDialog, useRunOrToast, useToast, useUnsavedChanges };
@@ -1,5 +1,7 @@
1
1
  import { i as useDialog, o as useToast, t as openDialog } from "./use-dialog-Ck2_ZW7T.js";
2
- import { computed, ref } from "vue";
2
+ import { computed, onActivated, onDeactivated, onMounted, onUnmounted, ref, toValue, watch } from "vue";
3
+ import { isDeepEqual } from "@nexdom/shared/utils";
4
+ import { onBeforeRouteLeave } from "vue-router";
3
5
  //#region src/composables/dialogs/use-run-or-toast.ts
4
6
  const { toast } = useToast();
5
7
  /**
@@ -114,4 +116,87 @@ function useConfirm() {
114
116
  };
115
117
  }
116
118
  //#endregion
117
- export { useConfirm, useDialog, useRunOrToast, useToast };
119
+ //#region src/composables/navigation/use-unsaved-changes.ts
120
+ /**
121
+ * Calls whose page is on display with unsaved changes. The `beforeunload` listener is registered
122
+ * while at least one of them exists, so a call that stops guarding doesn't remove the guard of the
123
+ * others.
124
+ */
125
+ const unloadGuards = /* @__PURE__ */ new Set();
126
+ function preventUnload(event) {
127
+ event.preventDefault();
128
+ }
129
+ function updateUnloadListener() {
130
+ if (unloadGuards.size > 0) globalThis.addEventListener("beforeunload", preventUnload);
131
+ else globalThis.removeEventListener("beforeunload", preventUnload);
132
+ }
133
+ /**
134
+ * Composable which compares the original and the current values of a form, and asks the user to
135
+ * confirm before leaving the page while they differ.
136
+ *
137
+ * Must be called in the `setup` of a component. Leaving through vue-router is only guarded in
138
+ * components rendered by a `<RouterView>`, and the confirmation dialog is displayed by `UMain`.
139
+ *
140
+ * @param values - The original (`previous`) and current (`current`) values, as refs, getters or
141
+ * reactive objects. They're compared in depth: key order doesn't matter, a key holding `undefined`
142
+ * equals a missing key, dates are compared by their time and arrays item by item, in order.
143
+ * @param options - Texts and color of the confirmation dialog, and whether to ask for the
144
+ * browser's native confirmation before unloading the page.
145
+ * @returns `hasChanges`, a computed property which is `true` while the values differ
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const saved = ref({ name: "Maria" });
150
+ * const form = ref(structuredClone(toRaw(saved.value)));
151
+ *
152
+ * const { hasChanges } = useUnsavedChanges({ previous: saved, current: form });
153
+ *
154
+ * async function save() {
155
+ * await api.save(form.value);
156
+ * // Marks the form as saved
157
+ * saved.value = structuredClone(toRaw(form.value));
158
+ * }
159
+ * ```
160
+ */
161
+ function useUnsavedChanges(values, options = {}) {
162
+ const hasChanges = computed(() => !isDeepEqual(toValue(values.previous), toValue(values.current)));
163
+ const { confirm } = useConfirm();
164
+ /** Answer of the dialog on display, shared by every navigation made while it's open. */
165
+ let pending;
166
+ onBeforeRouteLeave(() => {
167
+ if (!hasChanges.value) return true;
168
+ pending ??= confirm({
169
+ title: options.title ?? "Alterações não salvas",
170
+ message: options.message ?? "Existem alterações que ainda não foram salvas. Deseja sair sem salvar?",
171
+ confirmText: options.confirmText ?? "Sair sem salvar",
172
+ cancelText: options.cancelText ?? "Continuar editando",
173
+ color: options.color ?? "danger"
174
+ }, async () => {}).then((answer) => answer !== false).finally(() => pending = void 0);
175
+ return pending;
176
+ });
177
+ /** Whether the component is on display, which is false while `<KeepAlive>` keeps it deactivated. */
178
+ let isActive = true;
179
+ if (options.beforeUnload ?? true) {
180
+ onMounted(() => watch(hasChanges, updateBeforeUnload, { immediate: true }));
181
+ onUnmounted(deactivate);
182
+ onDeactivated(deactivate);
183
+ onActivated(() => {
184
+ isActive = true;
185
+ updateBeforeUnload();
186
+ });
187
+ }
188
+ /** Identifies this call in `unloadGuards`. */
189
+ const unloadGuard = {};
190
+ function updateBeforeUnload() {
191
+ if (hasChanges.value && isActive) unloadGuards.add(unloadGuard);
192
+ else unloadGuards.delete(unloadGuard);
193
+ updateUnloadListener();
194
+ }
195
+ function deactivate() {
196
+ isActive = false;
197
+ updateBeforeUnload();
198
+ }
199
+ return { hasChanges };
200
+ }
201
+ //#endregion
202
+ export { useConfirm, useDialog, useRunOrToast, useToast, useUnsavedChanges };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexdom/uimed-vue",
3
- "version": "2.0.0-beta.31",
3
+ "version": "2.0.0-beta.33",
4
4
  "description": "A ui framework for NEXDOM applications.",
5
5
  "keywords": [
6
6
  "lib",
@@ -55,7 +55,7 @@
55
55
  "@commitlint/cli": "^21.2.1",
56
56
  "@commitlint/config-conventional": "^21.2.0",
57
57
  "@mdi/font": "^7.4.47",
58
- "@nexdom/shared": "^2.1.1",
58
+ "@nexdom/shared": "^2.3.0",
59
59
  "@playwright/test": "^1.62.1",
60
60
  "@sonar/scan": "^5.0.0",
61
61
  "@stryker-mutator/core": "^10.0.0",
@@ -87,7 +87,7 @@
87
87
  },
88
88
  "peerDependencies": {
89
89
  "@mdi/font": "^7.4.47",
90
- "@nexdom/shared": "^2.1.1",
90
+ "@nexdom/shared": "^2.3.0",
91
91
  "resize-observer-polyfill": "^1.5.1",
92
92
  "sass-embedded": "^1.103.1",
93
93
  "vite-plus": "^1.0.0",