@aerogel/core 0.0.0-next.980a397d575dcb5ff8c5a0bff769d09f938ea03c → 0.0.0-next.a68f133e2c9a1ae9ba84b4e2e42df909289e5fba

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 (58) hide show
  1. package/dist/aerogel-core.cjs.js +1 -1
  2. package/dist/aerogel-core.cjs.js.map +1 -1
  3. package/dist/aerogel-core.d.ts +493 -107
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/package.json +2 -2
  7. package/src/bootstrap/bootstrap.test.ts +3 -3
  8. package/src/bootstrap/index.ts +13 -3
  9. package/src/bootstrap/options.ts +3 -0
  10. package/src/components/AGAppLayout.vue +3 -2
  11. package/src/components/AGAppOverlays.vue +5 -1
  12. package/src/components/forms/AGCheckbox.vue +7 -1
  13. package/src/components/forms/AGInput.vue +8 -6
  14. package/src/components/forms/AGSelect.story.vue +21 -3
  15. package/src/components/forms/AGSelect.vue +10 -3
  16. package/src/components/headless/forms/AGHeadlessInput.ts +5 -10
  17. package/src/components/headless/forms/AGHeadlessSelect.ts +20 -22
  18. package/src/components/headless/forms/AGHeadlessSelect.vue +23 -22
  19. package/src/components/headless/forms/AGHeadlessSelectOption.vue +6 -6
  20. package/src/components/headless/modals/AGHeadlessModal.ts +19 -1
  21. package/src/components/headless/modals/AGHeadlessModal.vue +3 -5
  22. package/src/components/headless/snackbars/index.ts +23 -8
  23. package/src/components/lib/AGMeasured.vue +15 -0
  24. package/src/components/lib/index.ts +1 -0
  25. package/src/components/modals/AGAlertModal.ts +15 -0
  26. package/src/components/modals/AGAlertModal.vue +3 -14
  27. package/src/components/modals/AGConfirmModal.ts +17 -0
  28. package/src/components/modals/AGConfirmModal.vue +6 -10
  29. package/src/components/modals/AGErrorReportModal.ts +27 -1
  30. package/src/components/modals/AGErrorReportModal.vue +7 -15
  31. package/src/components/modals/AGErrorReportModalButtons.vue +4 -2
  32. package/src/components/modals/AGLoadingModal.ts +14 -0
  33. package/src/components/modals/AGLoadingModal.vue +3 -7
  34. package/src/components/modals/AGModal.vue +14 -12
  35. package/src/components/modals/AGPromptModal.ts +30 -0
  36. package/src/components/modals/AGPromptModal.vue +34 -0
  37. package/src/components/modals/index.ts +11 -19
  38. package/src/components/snackbars/AGSnackbar.vue +2 -8
  39. package/src/components/utils.ts +10 -0
  40. package/src/directives/index.ts +3 -1
  41. package/src/directives/measure.ts +12 -0
  42. package/src/errors/Errors.ts +17 -8
  43. package/src/errors/index.ts +1 -11
  44. package/src/forms/Form.ts +1 -0
  45. package/src/lang/Lang.ts +1 -1
  46. package/src/services/App.state.ts +1 -2
  47. package/src/services/App.ts +21 -3
  48. package/src/services/Events.ts +1 -1
  49. package/src/services/Service.ts +36 -10
  50. package/src/services/index.ts +2 -1
  51. package/src/services/store.ts +8 -5
  52. package/src/ui/UI.ts +93 -12
  53. package/src/ui/index.ts +8 -3
  54. package/src/utils/composition/events.ts +1 -0
  55. package/src/utils/index.ts +1 -0
  56. package/src/utils/tailwindcss.test.ts +26 -0
  57. package/src/utils/tailwindcss.ts +7 -0
  58. package/src/utils/vue.ts +10 -1
@@ -1,5 +1,6 @@
1
1
  import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
2
2
  import type { Constructor } from '@noeldemartin/utils';
3
+ import type { MaybeRef } from 'vue';
3
4
  import type { Store } from 'pinia';
4
5
 
5
6
  import ServiceBootError from '@/errors/ServiceBootError';
@@ -8,9 +9,12 @@ import { defineServiceStore } from '@/services/store';
8
9
  export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
9
10
  export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
10
11
  export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
12
+ export type UnrefServiceState<State extends ServiceState> = {
13
+ [K in keyof State]: State[K] extends MaybeRef<infer T> ? T : State[K];
14
+ };
11
15
 
12
16
  export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
13
- [K in keyof TComputedState]: (state: TState) => TComputedState[K];
17
+ [K in keyof TComputedState]: (state: UnrefServiceState<TState>) => TComputedState[K];
14
18
  } & ThisType<{
15
19
  readonly [K in keyof TComputedState]: TComputedState[K];
16
20
  }>;
@@ -20,12 +24,14 @@ export function defineServiceState<
20
24
  ComputedState extends ServiceState = {}
21
25
  >(options: {
22
26
  name: string;
23
- initialState: State;
27
+ initialState: State | (() => State);
24
28
  persist?: (keyof State)[];
25
29
  computed?: ComputedStateDefinition<State, ComputedState>;
26
30
  serialize?: (state: Partial<State>) => Partial<State>;
27
- }): Constructor<State> & Constructor<ComputedState> & Constructor<Service<State, ComputedState, Partial<State>>> {
28
- return class extends Service<State, ComputedState> {
31
+ }): Constructor<UnrefServiceState<State>> &
32
+ Constructor<ComputedState> &
33
+ Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>> {
34
+ return class extends Service<UnrefServiceState<State>, ComputedState> {
29
35
 
30
36
  public static persist = (options.persist as string[]) ?? [];
31
37
 
@@ -37,21 +43,41 @@ export function defineServiceState<
37
43
  return options.name ?? null;
38
44
  }
39
45
 
40
- protected getInitialState(): State {
41
- return options.initialState;
46
+ protected getInitialState(): UnrefServiceState<State> {
47
+ if (typeof options.initialState === 'function') {
48
+ return options.initialState();
49
+ }
50
+
51
+ return Object.entries(options.initialState).reduce((state, [key, value]) => {
52
+ try {
53
+ value = structuredClone(value);
54
+ } catch (error) {
55
+ // eslint-disable-next-line no-console
56
+ console.warn(
57
+ `Could not clone '${key}' state from ${this.getName()} service, ` +
58
+ 'this may cause problems if you\'re using multiple instances of the service ' +
59
+ '(for example, in unit tests).\n' +
60
+ 'To fix this problem, declare your initialState as a function instead.',
61
+ );
62
+ }
63
+
64
+ state[key as keyof State] = value;
65
+
66
+ return state;
67
+ }, {} as UnrefServiceState<State>);
42
68
  }
43
69
 
44
- protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
45
- return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
70
+ protected getComputedStateDefinition(): ComputedStateDefinition<UnrefServiceState<State>, ComputedState> {
71
+ return (options.computed ?? {}) as ComputedStateDefinition<UnrefServiceState<State>, ComputedState>;
46
72
  }
47
73
 
48
74
  protected serializePersistedState(state: Partial<State>): Partial<State> {
49
75
  return options.serialize?.(state) ?? state;
50
76
  }
51
77
 
52
- } as unknown as Constructor<State> &
78
+ } as unknown as Constructor<UnrefServiceState<State>> &
53
79
  Constructor<ComputedState> &
54
- Constructor<Service<State, ComputedState, Partial<State>>>;
80
+ Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>>;
55
81
  }
56
82
 
57
83
  export default class Service<
@@ -10,6 +10,7 @@ import { getPiniaStore } from './store';
10
10
  export * from './App';
11
11
  export * from './Events';
12
12
  export * from './Service';
13
+ export * from './store';
13
14
 
14
15
  export { App, Events, Service };
15
16
 
@@ -50,7 +51,7 @@ export default definePlugin({
50
51
  });
51
52
 
52
53
  declare module '@/bootstrap/options' {
53
- interface AerogelOptions {
54
+ export interface AerogelOptions {
54
55
  services?: Record<string, Service>;
55
56
  }
56
57
  }
@@ -1,16 +1,19 @@
1
+ import { tap } from '@noeldemartin/utils';
1
2
  import { createPinia, defineStore, setActivePinia } from 'pinia';
2
3
  import type { DefineStoreOptions, Pinia, StateTree, Store, _GettersTree } from 'pinia';
3
4
 
4
5
  let _store: Pinia | null = null;
5
6
 
6
7
  function initializePiniaStore(): Pinia {
7
- if (!_store) {
8
- _store = createPinia();
8
+ return _store ?? resetPiniaStore();
9
+ }
9
10
 
10
- setActivePinia(_store);
11
- }
11
+ export function resetPiniaStore(): Pinia {
12
+ return tap(createPinia(), (store) => {
13
+ _store = store;
12
14
 
13
- return _store;
15
+ setActivePinia(store);
16
+ });
14
17
  }
15
18
 
16
19
  export function getPiniaStore(): Pinia {
package/src/ui/UI.ts CHANGED
@@ -5,6 +5,7 @@ import type { ObjectValues } from '@noeldemartin/utils';
5
5
 
6
6
  import Events from '@/services/Events';
7
7
  import type { SnackbarAction, SnackbarColor } from '@/components/headless/snackbars';
8
+ import type { AGAlertModalProps, AGConfirmModalProps, AGLoadingModalProps, AGPromptModalProps } from '@/components';
8
9
 
9
10
  import Service from './UI.state';
10
11
  import type { Modal, ModalComponent, Snackbar } from './UI.state';
@@ -24,11 +25,26 @@ export const UIComponents = {
24
25
  ConfirmModal: 'confirm-modal',
25
26
  ErrorReportModal: 'error-report-modal',
26
27
  LoadingModal: 'loading-modal',
28
+ PromptModal: 'prompt-modal',
27
29
  Snackbar: 'snackbar',
30
+ StartupCrash: 'startup-crash',
28
31
  } as const;
29
32
 
30
33
  export type UIComponent = ObjectValues<typeof UIComponents>;
31
34
 
35
+ export interface ConfirmOptions {
36
+ acceptText?: string;
37
+ cancelText?: string;
38
+ }
39
+
40
+ export interface PromptOptions {
41
+ label?: string;
42
+ defaultValue?: string;
43
+ placeholder?: string;
44
+ acceptText?: string;
45
+ cancelText?: string;
46
+ }
47
+
32
48
  export interface ShowSnackbarOptions {
33
49
  component?: Component;
34
50
  color?: SnackbarColor;
@@ -47,33 +63,98 @@ export class UIService extends Service {
47
63
  public alert(message: string): void;
48
64
  public alert(title: string, message: string): void;
49
65
  public alert(messageOrTitle: string, message?: string): void {
50
- const options = typeof message === 'string' ? { title: messageOrTitle, message } : { message: messageOrTitle };
66
+ const getProperties = (): AGAlertModalProps => {
67
+ if (typeof message !== 'string') {
68
+ return { message: messageOrTitle };
69
+ }
70
+
71
+ return {
72
+ title: messageOrTitle,
73
+ message,
74
+ };
75
+ };
51
76
 
52
- this.openModal(this.requireComponent(UIComponents.AlertModal), options);
77
+ this.openModal(this.requireComponent(UIComponents.AlertModal), getProperties());
53
78
  }
54
79
 
55
- public async confirm(message: string): Promise<boolean>;
56
- public async confirm(title: string, message: string): Promise<boolean>;
57
- public async confirm(messageOrTitle: string, message?: string): Promise<boolean> {
58
- const options = typeof message === 'string' ? { title: messageOrTitle, message } : { message: messageOrTitle };
59
- const modal = await this.openModal<ModalComponent<{ message: string }, boolean>>(
80
+ public async confirm(message: string, options?: ConfirmOptions): Promise<boolean>;
81
+ public async confirm(title: string, message: string, options?: ConfirmOptions): Promise<boolean>;
82
+ public async confirm(
83
+ messageOrTitle: string,
84
+ messageOrOptions?: string | ConfirmOptions,
85
+ options?: ConfirmOptions,
86
+ ): Promise<boolean> {
87
+ const getProperties = (): AGConfirmModalProps => {
88
+ if (typeof messageOrOptions !== 'string') {
89
+ return {
90
+ message: messageOrTitle,
91
+ ...(messageOrOptions ?? {}),
92
+ };
93
+ }
94
+
95
+ return {
96
+ title: messageOrTitle,
97
+ message: messageOrOptions,
98
+ ...(options ?? {}),
99
+ };
100
+ };
101
+
102
+ const modal = await this.openModal<ModalComponent<AGConfirmModalProps, boolean>>(
60
103
  this.requireComponent(UIComponents.ConfirmModal),
61
- options,
104
+ getProperties(),
62
105
  );
63
106
  const result = await modal.beforeClose;
64
107
 
65
108
  return result ?? false;
66
109
  }
67
110
 
111
+ public async prompt(message: string, options?: PromptOptions): Promise<string | null>;
112
+ public async prompt(title: string, message: string, options?: PromptOptions): Promise<string | null>;
113
+ public async prompt(
114
+ messageOrTitle: string,
115
+ messageOrOptions?: string | PromptOptions,
116
+ options?: PromptOptions,
117
+ ): Promise<string | null> {
118
+ const getProperties = (): AGPromptModalProps => {
119
+ if (typeof messageOrOptions !== 'string') {
120
+ return {
121
+ message: messageOrTitle,
122
+ ...(messageOrOptions ?? {}),
123
+ };
124
+ }
125
+
126
+ return {
127
+ title: messageOrTitle,
128
+ message: messageOrOptions,
129
+ ...(options ?? {}),
130
+ };
131
+ };
132
+
133
+ const modal = await this.openModal<ModalComponent<AGPromptModalProps, string | null>>(
134
+ this.requireComponent(UIComponents.PromptModal),
135
+ getProperties(),
136
+ );
137
+ const result = await modal.beforeClose;
138
+
139
+ return result ?? null;
140
+ }
141
+
68
142
  public async loading<T>(operation: Promise<T>): Promise<T>;
69
143
  public async loading<T>(message: string, operation: Promise<T>): Promise<T>;
70
144
  public async loading<T>(messageOrOperation: string | Promise<T>, operation?: Promise<T>): Promise<T> {
71
- operation = typeof messageOrOperation === 'string' ? (operation as Promise<T>) : messageOrOperation;
145
+ const getProperties = (): AGLoadingModalProps => {
146
+ if (typeof messageOrOperation !== 'string') {
147
+ return {};
148
+ }
72
149
 
73
- const message = typeof messageOrOperation === 'string' ? messageOrOperation : undefined;
74
- const modal = await this.openModal(this.requireComponent(UIComponents.LoadingModal), { message });
150
+ return { message: messageOrOperation };
151
+ };
152
+
153
+ const modal = await this.openModal(this.requireComponent(UIComponents.LoadingModal), getProperties());
75
154
 
76
155
  try {
156
+ operation = typeof messageOrOperation === 'string' ? (operation as Promise<T>) : messageOrOperation;
157
+
77
158
  const [result] = await Promise.all([operation, after({ seconds: 1 })]);
78
159
 
79
160
  return result;
@@ -189,7 +270,7 @@ export class UIService extends Service {
189
270
 
190
271
  }
191
272
 
192
- export default facade(new UIService());
273
+ export default facade(UIService);
193
274
 
194
275
  declare module '@/services/Events' {
195
276
  export interface EventsPayload {
package/src/ui/index.ts CHANGED
@@ -8,13 +8,16 @@ import AGAlertModal from '../components/modals/AGAlertModal.vue';
8
8
  import AGConfirmModal from '../components/modals/AGConfirmModal.vue';
9
9
  import AGErrorReportModal from '../components/modals/AGErrorReportModal.vue';
10
10
  import AGLoadingModal from '../components/modals/AGLoadingModal.vue';
11
+ import AGPromptModal from '../components/modals/AGPromptModal.vue';
11
12
  import AGSnackbar from '../components/snackbars/AGSnackbar.vue';
13
+ import AGStartupCrash from '../components/lib/AGStartupCrash.vue';
12
14
  import type { UIComponent } from './UI';
13
15
 
14
- export { UI, UIComponents, UIComponent };
15
-
16
16
  const services = { $ui: UI };
17
17
 
18
+ export * from './UI';
19
+ export { default as UI } from './UI';
20
+
18
21
  export type UIServices = typeof services;
19
22
 
20
23
  export default definePlugin({
@@ -24,7 +27,9 @@ export default definePlugin({
24
27
  [UIComponents.ConfirmModal]: AGConfirmModal,
25
28
  [UIComponents.ErrorReportModal]: AGErrorReportModal,
26
29
  [UIComponents.LoadingModal]: AGLoadingModal,
30
+ [UIComponents.PromptModal]: AGPromptModal,
27
31
  [UIComponents.Snackbar]: AGSnackbar,
32
+ [UIComponents.StartupCrash]: AGStartupCrash,
28
33
  };
29
34
 
30
35
  Object.entries({
@@ -37,7 +42,7 @@ export default definePlugin({
37
42
  });
38
43
 
39
44
  declare module '@/bootstrap/options' {
40
- interface AerogelOptions {
45
+ export interface AerogelOptions {
41
46
  components?: Partial<Record<UIComponent, Component>>;
42
47
  }
43
48
  }
@@ -14,6 +14,7 @@ export function useEvent<Event extends EventWithPayload>(
14
14
  event: Event,
15
15
  listener: EventListener<EventsPayload[Event]>
16
16
  ): void;
17
+ export function useEvent<Payload>(event: string, listener: (payload: Payload) => unknown): void;
17
18
  export function useEvent<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): void;
18
19
 
19
20
  export function useEvent(event: string, listener: EventListener): void {
@@ -1,4 +1,5 @@
1
1
  export * from './composition/events';
2
2
  export * from './composition/forms';
3
3
  export * from './composition/hooks';
4
+ export * from './tailwindcss';
4
5
  export * from './vue';
@@ -0,0 +1,26 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { removeInteractiveClasses } from './tailwindcss';
4
+
5
+ describe('TailwindCSS utils', () => {
6
+
7
+ it('Removes interactive classes', () => {
8
+ const cases: [string, string][] = [
9
+ ['text-red hover:text-green', 'text-red'],
10
+ ['text-red hover:text-green text-lg', 'text-red text-lg'],
11
+ [
12
+ `
13
+ text-red text-lg
14
+ focus:text-yellow
15
+ hover:focus:text-black
16
+ `,
17
+ 'text-red text-lg',
18
+ ],
19
+ ];
20
+
21
+ cases.forEach(([original, expected]) => {
22
+ expect(removeInteractiveClasses(original)).toEqual(expected);
23
+ });
24
+ });
25
+
26
+ });
@@ -0,0 +1,7 @@
1
+ export function removeInteractiveClasses(classes: string): string {
2
+ return classes
3
+ .split(/\s+/)
4
+ .filter((className) => !/^(hover|focus|focus-visible):/.test(className))
5
+ .join(' ')
6
+ .trim();
7
+ }
package/src/utils/vue.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { fail } from '@noeldemartin/utils';
2
- import { inject, reactive, ref } from 'vue';
2
+ import { computed, inject, reactive, ref, watch } from 'vue';
3
3
  import type { Directive, InjectionKey, PropType, Ref, UnwrapNestedRefs } from 'vue';
4
4
 
5
5
  type BaseProp<T> = {
@@ -30,6 +30,15 @@ export function componentRef<T>(): Ref<UnwrapNestedRefs<T> | undefined> {
30
30
  return ref<UnwrapNestedRefs<T>>();
31
31
  }
32
32
 
33
+ export function computedAsync<T>(getter: () => Promise<T>): Ref<T | undefined> {
34
+ const result = ref<T>();
35
+ const asyncValue = computed(getter);
36
+
37
+ watch(asyncValue, async () => (result.value = await asyncValue.value), { immediate: true });
38
+
39
+ return result;
40
+ }
41
+
33
42
  export function defineDirective(directive: Directive): Directive {
34
43
  return directive;
35
44
  }