@bgord/ui 0.8.2 → 0.8.4

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.
@@ -16,5 +16,5 @@ export function Dialog(props) {
16
16
  useShortcuts({ Escape: locked ? noop : dialog.disable });
17
17
  useScrollLock(props.on);
18
18
  useClickOutside(ref, locked ? noop : dialog.disable);
19
- return (_jsx("dialog", { ref: ref, tabIndex: 0, "aria-modal": "true", "data-disp": props.on ? "flex" : "none", "data-dir": "column", "data-mx": "auto", "data-p": "5", "data-position": "fixed", "data-z": "2", "data-bg": "neutral-900", "data-br": "xs", "data-backdrop": "stronger", "data-animation": "grow-fade-in", ...rest }));
19
+ return (_jsx("dialog", { ref: ref, tabIndex: 0, "aria-modal": "true", "data-disp": props.on ? "flex" : "none", "data-dir": "column", "data-mx": "auto", "data-p": "5", "data-position": "fixed", "data-z": "2", "data-bg": "neutral-900", "data-br": "xs", "data-backdrop": "stronger", "data-animation": "grow-fade-in", ...dialog.props.target, ...rest }));
20
20
  }
@@ -7,6 +7,7 @@ export * from "./use-hover";
7
7
  export * from "./use-meta-enter-submit";
8
8
  export * from "./use-mutation";
9
9
  export * from "./use-number-field";
10
+ export * from "./use-online-status";
10
11
  export * from "./use-scroll-lock";
11
12
  export * from "./use-shortcuts";
12
13
  export * from "./use-text-field";
@@ -7,6 +7,7 @@ export * from "./use-hover";
7
7
  export * from "./use-meta-enter-submit";
8
8
  export * from "./use-mutation";
9
9
  export * from "./use-number-field";
10
+ export * from "./use-online-status";
10
11
  export * from "./use-scroll-lock";
11
12
  export * from "./use-shortcuts";
12
13
  export * from "./use-text-field";
@@ -5,10 +5,13 @@ export declare enum MutationState {
5
5
  done = "done"
6
6
  }
7
7
  type MutationErrorType = unknown;
8
+ type MutationContextType = {
9
+ form?: HTMLFormElement;
10
+ };
8
11
  type UseMutationOptions = {
9
12
  perform: () => Promise<Response>;
10
- onSuccess?: (response: Response) => void | Promise<void>;
11
- onError?: (error: MutationErrorType) => void | Promise<void>;
13
+ onSuccess?: (response: Response, context: MutationContextType) => void | Promise<void>;
14
+ onError?: (error: MutationErrorType, context: MutationContextType) => void | Promise<void>;
12
15
  autoResetDelayMs?: number;
13
16
  };
14
17
  type UseMutationReturnType = {
@@ -18,7 +21,7 @@ type UseMutationReturnType = {
18
21
  isLoading: boolean;
19
22
  isError: boolean;
20
23
  isDone: boolean;
21
- mutate: () => Promise<Response | undefined>;
24
+ mutate: (formElement?: HTMLFormElement) => Promise<Response | undefined>;
22
25
  handleSubmit: React.FormEventHandler<HTMLFormElement>;
23
26
  reset: () => void;
24
27
  };
@@ -1,4 +1,4 @@
1
- import { useCallback, useState } from "react";
1
+ import { useReducer } from "react";
2
2
  export var MutationState;
3
3
  (function (MutationState) {
4
4
  MutationState["idle"] = "idle";
@@ -6,52 +6,55 @@ export var MutationState;
6
6
  MutationState["error"] = "error";
7
7
  MutationState["done"] = "done";
8
8
  })(MutationState || (MutationState = {}));
9
+ function mutationReducer(_state, action) {
10
+ if (action.type === "START")
11
+ return { state: MutationState.loading, error: null };
12
+ if (action.type === "ERROR")
13
+ return { state: MutationState.error, error: action.error };
14
+ if (action.type === "DONE")
15
+ return { state: MutationState.done, error: null };
16
+ if (action.type === "RESET")
17
+ return { state: MutationState.idle, error: null };
18
+ throw new Error("Unknown useMutation action type");
19
+ }
9
20
  export function useMutation(options) {
10
- const [state, setState] = useState(MutationState.idle);
11
- const [error, setError] = useState(null);
12
- const reset = useCallback(() => {
13
- setError(null);
14
- setState(MutationState.idle);
15
- }, []);
16
- const mutate = useCallback(async () => {
17
- if (state === MutationState.loading)
21
+ const [mutation, dispatch] = useReducer(mutationReducer, { state: MutationState.idle, error: null });
22
+ const mutate = async (form) => {
23
+ if (mutation.state === MutationState.loading)
18
24
  return;
19
- setError(null);
20
- setState(MutationState.loading);
25
+ dispatch({ type: "START" });
21
26
  try {
22
27
  const response = await options.perform();
23
28
  if (!response.ok) {
24
- setState(MutationState.error);
25
- setError(null);
26
- await options.onError?.(null);
29
+ dispatch({ type: "ERROR", error: null });
30
+ await options.onError?.(null, { form: form });
27
31
  return;
28
32
  }
29
- setState(MutationState.done);
30
- await options.onSuccess?.(response);
33
+ dispatch({ type: "DONE" });
34
+ await options.onSuccess?.(response, { form: form });
31
35
  if (options.autoResetDelayMs) {
32
- setTimeout(() => setState(MutationState.idle), options.autoResetDelayMs);
36
+ setTimeout(() => dispatch({ type: "RESET" }), options.autoResetDelayMs);
33
37
  }
34
38
  return response;
35
39
  }
36
40
  catch (error) {
37
- setState(MutationState.error);
38
- setError(error);
39
- await options.onError?.(error);
41
+ dispatch({ type: "ERROR", error });
42
+ await options.onError?.(error, { form: form });
40
43
  }
41
- }, [state, options]);
42
- const handleSubmit = useCallback(async (event) => {
44
+ };
45
+ const handleSubmit = async (event) => {
43
46
  event.preventDefault();
44
- await mutate();
45
- }, [mutate]);
47
+ await mutate(event.currentTarget);
48
+ };
46
49
  return {
47
- state,
48
- error,
49
- isIdle: state === MutationState.idle,
50
- isLoading: state === MutationState.loading,
51
- isError: state === MutationState.error,
52
- isDone: state === MutationState.done,
50
+ state: mutation.state,
51
+ error: mutation.error,
52
+ isIdle: mutation.state === MutationState.idle,
53
+ isLoading: mutation.state === MutationState.loading,
54
+ isError: mutation.state === MutationState.error,
55
+ isDone: mutation.state === MutationState.done,
53
56
  mutate,
54
57
  handleSubmit,
55
- reset,
58
+ reset: () => dispatch({ type: "RESET" }),
56
59
  };
57
60
  }
@@ -0,0 +1,5 @@
1
+ export declare enum OnlineStatus {
2
+ online = "online",
3
+ offline = "offline"
4
+ }
5
+ export declare function useOnlineStatus(): OnlineStatus;
@@ -0,0 +1,28 @@
1
+ import { useSyncExternalStore } from "react";
2
+ export var OnlineStatus;
3
+ (function (OnlineStatus) {
4
+ OnlineStatus["online"] = "online";
5
+ OnlineStatus["offline"] = "offline";
6
+ })(OnlineStatus || (OnlineStatus = {}));
7
+ const OnlineStatusStore = {
8
+ subscribe: (callback) => {
9
+ if (typeof window === "undefined")
10
+ return () => { };
11
+ window.addEventListener("online", callback);
12
+ window.addEventListener("offline", callback);
13
+ return () => {
14
+ window.removeEventListener("online", callback);
15
+ window.removeEventListener("offline", callback);
16
+ };
17
+ },
18
+ getSnapshot: () => {
19
+ if (typeof navigator === "undefined")
20
+ return OnlineStatus.online;
21
+ return navigator.onLine ? OnlineStatus.online : OnlineStatus.offline;
22
+ },
23
+ getServerSnapshot: () => OnlineStatus.online,
24
+ };
25
+ export function useOnlineStatus() {
26
+ const status = useSyncExternalStore(OnlineStatusStore.subscribe, OnlineStatusStore.getSnapshot, OnlineStatusStore.getServerSnapshot);
27
+ return status;
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bgord/ui",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -23,7 +23,7 @@
23
23
  "access": "public"
24
24
  },
25
25
  "devDependencies": {
26
- "@biomejs/biome": "2.3.2",
26
+ "@biomejs/biome": "2.3.4",
27
27
  "@commitlint/cli": "20.1.0",
28
28
  "@commitlint/config-conventional": "20.0.0",
29
29
  "@happy-dom/global-registrator": "20.0.10",
@@ -35,8 +35,8 @@
35
35
  "@types/js-cookie": "3.0.6",
36
36
  "@types/react": "19.2.2",
37
37
  "@types/react-dom": "19.2.2",
38
- "cspell": "9.2.2",
39
- "knip": "5.66.4",
38
+ "cspell": "9.3.0",
39
+ "knip": "5.68.0",
40
40
  "lefthook": "2.0.2",
41
41
  "only-allow": "1.2.1",
42
42
  "shellcheck": "4.1.0"
package/readme.md CHANGED
@@ -36,6 +36,7 @@ src/
36
36
  │   ├── use-meta-enter-submit.ts
37
37
  │   ├── use-mutation.ts
38
38
  │   ├── use-number-field.ts
39
+ │   ├── use-online-status.ts
39
40
  │   ├── use-scroll-lock.ts
40
41
  │   ├── use-shortcuts.ts
41
42
  │   ├── use-text-field.ts