@bgord/ui 0.8.24 → 0.8.25

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.
@@ -0,0 +1,23 @@
1
+ import { type PropsWithChildren } from "react";
2
+ export type NotificationVariantType = "positive" | "negative" | "warning" | "neutral";
3
+ export type NotificationType = {
4
+ id: string;
5
+ message: string;
6
+ variant: NotificationVariantType;
7
+ };
8
+ export type NotificationContextValueType = {
9
+ notifications: Array<NotificationType>;
10
+ notify: (config: Omit<NotificationType, "id">) => void;
11
+ dismiss: (id: NotificationType["id"]) => void;
12
+ dismissAll: () => void;
13
+ };
14
+ export declare const NotificationContext: import("react").Context<NotificationContextValueType | undefined>;
15
+ type NotificationProviderPropsType = PropsWithChildren<{
16
+ duration: number;
17
+ }>;
18
+ export declare function NotificationProvider(props: NotificationProviderPropsType): import("react/jsx-runtime").JSX.Element;
19
+ export declare function useNotifications(): NotificationContextValueType["notifications"];
20
+ export declare function useNotify(): NotificationContextValueType["notify"];
21
+ export declare function useDismiss(): NotificationContextValueType["dismiss"];
22
+ export declare function useDismissAll(): NotificationContextValueType["dismissAll"];
23
+ export {};
@@ -0,0 +1,70 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, use, useCallback, useEffect, useReducer, useRef, } from "react";
3
+ function notificationReducer(state, action) {
4
+ switch (action.type) {
5
+ case "ADD":
6
+ return { notifications: [action.notification, ...state.notifications] };
7
+ case "DISMISS":
8
+ return { notifications: state.notifications.filter((notification) => notification.id !== action.id) };
9
+ case "DISMISS_ALL":
10
+ return { notifications: [] };
11
+ }
12
+ }
13
+ export const NotificationContext = createContext(undefined);
14
+ export function NotificationProvider(props) {
15
+ const [state, dispatch] = useReducer(notificationReducer, { notifications: [] });
16
+ const timers = useRef(new Map());
17
+ const notify = useCallback((config) => {
18
+ const notification = { id: crypto.randomUUID(), ...config };
19
+ dispatch({ type: "ADD", notification });
20
+ const timer = setTimeout(() => {
21
+ dispatch({ type: "DISMISS", id: notification.id });
22
+ timers.current.delete(notification.id);
23
+ }, props.duration);
24
+ timers.current.set(notification.id, timer);
25
+ }, [props.duration]);
26
+ const dismiss = useCallback((id) => {
27
+ const timer = timers.current.get(id);
28
+ if (timer !== undefined) {
29
+ clearTimeout(timer);
30
+ timers.current.delete(id);
31
+ }
32
+ dispatch({ type: "DISMISS", id });
33
+ }, []);
34
+ const dismissAll = useCallback(() => {
35
+ timers.current.forEach((timer) => clearTimeout(timer));
36
+ timers.current.clear();
37
+ dispatch({ type: "DISMISS_ALL" });
38
+ }, []);
39
+ useEffect(() => {
40
+ return () => {
41
+ timers.current.forEach((timer) => clearTimeout(timer));
42
+ timers.current.clear();
43
+ };
44
+ }, []);
45
+ return (_jsx(NotificationContext.Provider, { value: { notifications: state.notifications, notify, dismiss, dismissAll }, children: props.children }));
46
+ }
47
+ export function useNotifications() {
48
+ const value = use(NotificationContext);
49
+ if (value === undefined)
50
+ throw new Error("useNotifications must be used within the NotificationContext");
51
+ return value.notifications;
52
+ }
53
+ export function useNotify() {
54
+ const value = use(NotificationContext);
55
+ if (value === undefined)
56
+ throw new Error("useNotify must be used within the NotificationContext");
57
+ return value.notify;
58
+ }
59
+ export function useDismiss() {
60
+ const value = use(NotificationContext);
61
+ if (value === undefined)
62
+ throw new Error("useDismiss must be used within the NotificationContext");
63
+ return value.dismiss;
64
+ }
65
+ export function useDismissAll() {
66
+ const value = use(NotificationContext);
67
+ if (value === undefined)
68
+ throw new Error("useDismissAll must be used within the NotificationContext");
69
+ return value.dismissAll;
70
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bgord/ui",
3
- "version": "0.8.24",
3
+ "version": "0.8.25",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -19,22 +19,22 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "@biomejs/biome": "2.4.4",
23
- "@commitlint/cli": "20.4.2",
24
- "@commitlint/config-conventional": "20.4.2",
25
- "@happy-dom/global-registrator": "20.7.0",
22
+ "@biomejs/biome": "2.4.6",
23
+ "@commitlint/cli": "20.4.4",
24
+ "@commitlint/config-conventional": "20.4.4",
25
+ "@happy-dom/global-registrator": "20.8.4",
26
26
  "@stryker-mutator/core": "9.6.0",
27
27
  "@testing-library/dom": "10.4.1",
28
28
  "@testing-library/jest-dom": "6.9.1",
29
29
  "@testing-library/react": "16.3.2",
30
30
  "@testing-library/user-event": "14.6.1",
31
- "@types/bun": "1.3.9",
31
+ "@types/bun": "1.3.10",
32
32
  "@types/js-cookie": "3.0.6",
33
33
  "@types/react": "19.2.14",
34
34
  "@types/react-dom": "19.2.3",
35
35
  "cspell": "9.7.0",
36
- "knip": "5.85.0",
37
- "lefthook": "2.1.1",
36
+ "knip": "5.86.0",
37
+ "lefthook": "2.1.4",
38
38
  "lockfile-lint": "5.0.0",
39
39
  "only-allow": "1.2.2",
40
40
  "shellcheck": "4.1.0",
package/readme.md CHANGED
@@ -55,6 +55,7 @@ src/
55
55
  ├── get-safe-window.ts
56
56
  ├── head.ts
57
57
  ├── noop.ts
58
+ ├── notifications.tsx
58
59
  ├── number-field.ts
59
60
  ├── pluralize.ts
60
61
  ├── rhythm.ts