@canonical/react-components 2.6.1 → 2.7.1

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.
@@ -10,38 +10,6 @@ export interface EventQueue<T> {
10
10
  set: (operationId: string, onSuccess: (event: T) => void, onFailure: (msg: string) => void, onFinish?: () => void) => void;
11
11
  remove: (operationId: string) => void;
12
12
  }
13
- /**
14
- * This provides an event queue system for managing callbacks associated with
15
- * asynchronous operations (e.g., API calls) in an application.
16
- *
17
- * It allows components to register success, failure,
18
- * and optional finish handlers for a given operation ID, and later retrieve or remove them.
19
- *
20
- * This is useful for handling side effects when dealing
21
- * with multiple operations that need to be tracked and updated independently.
22
- *
23
- * The `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.
24
- * The returned `EventQueueProvider` and `useEventQueue` hook should be exported
25
- * from a shared module and reused across the app to ensure a single
26
- * context instance is used.
27
- *
28
- * Usage pattern:
29
- * // eventQueue.ts
30
- * export const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();
31
- *
32
- * // App.tsx
33
- * import { EventQueueProvider } from "./eventQueue";
34
- * ...
35
- * <EventQueueProvider>
36
- * <App />
37
- * </EventQueueProvider>
38
- *
39
- * // In any other component
40
- * import { useEventQueue } from "./eventQueue";
41
- * ...
42
- * const eventQueue = useEventQueue();
43
- * eventQueue.set(operationId, onSuccess, onFailure);
44
- */
45
13
  export declare function createEventQueue<T>(): {
46
14
  EventQueueProvider: FC<{
47
15
  children: ReactNode;
@@ -7,39 +7,6 @@ exports.createEventQueue = createEventQueue;
7
7
  var _react = _interopRequireWildcard(require("react"));
8
8
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
9
9
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
10
- /**
11
- * This provides an event queue system for managing callbacks associated with
12
- * asynchronous operations (e.g., API calls) in an application.
13
- *
14
- * It allows components to register success, failure,
15
- * and optional finish handlers for a given operation ID, and later retrieve or remove them.
16
- *
17
- * This is useful for handling side effects when dealing
18
- * with multiple operations that need to be tracked and updated independently.
19
- *
20
- * The `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.
21
- * The returned `EventQueueProvider` and `useEventQueue` hook should be exported
22
- * from a shared module and reused across the app to ensure a single
23
- * context instance is used.
24
- *
25
- * Usage pattern:
26
- * // eventQueue.ts
27
- * export const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();
28
- *
29
- * // App.tsx
30
- * import { EventQueueProvider } from "./eventQueue";
31
- * ...
32
- * <EventQueueProvider>
33
- * <App />
34
- * </EventQueueProvider>
35
- *
36
- * // In any other component
37
- * import { useEventQueue } from "./eventQueue";
38
- * ...
39
- * const eventQueue = useEventQueue();
40
- * eventQueue.set(operationId, onSuccess, onFailure);
41
- */
42
-
43
10
  function createEventQueue() {
44
11
  const EventQueueContext = /*#__PURE__*/(0, _react.createContext)(undefined);
45
12
  const eventQueue = new Map();
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+ import { Meta, StoryObj } from "@storybook/react";
3
+ declare const EventQueueProvider: React.FC<{
4
+ children: React.ReactNode;
5
+ }>;
6
+ declare const meta: Meta<typeof EventQueueProvider>;
7
+ export default meta;
8
+ type Story = StoryObj<typeof EventQueueProvider>;
9
+ export declare const Default: Story;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.Default = void 0;
7
+ var _react = _interopRequireWildcard(require("react"));
8
+ var _EventQueue = require("./EventQueue");
9
+ var _Button = _interopRequireDefault(require("../Button/Button"));
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
12
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
13
+ const {
14
+ EventQueueProvider,
15
+ useEventQueue
16
+ } = (0, _EventQueue.createEventQueue)();
17
+ const meta = {
18
+ title: "Components/EventQueue",
19
+ component: EventQueueProvider,
20
+ tags: ["autodocs"],
21
+ parameters: {
22
+ docs: {
23
+ description: {
24
+ component: "\nThis provides an event queue system for managing callbacks associated with\nasynchronous operations (e.g., API calls) in an application.\n\nIt allows components to register success, failure,\nand optional finish handlers for a given operation ID, and later retrieve or remove them.\n\nThis is useful for handling side effects when dealing\nwith multiple operations that need to be tracked and updated independently.\n\nThe `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.\nThe returned `EventQueueProvider` and `useEventQueue` hook should be exported\nfrom a shared module and reused across the app to ensure a single\ncontext instance is used.\n\n**Usage pattern:**\n\n```tsx\n// eventQueue.ts\nexport const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();\n\n// App.tsx\nimport { EventQueueProvider } from \"./eventQueue\";\n\n<EventQueueProvider>\n <App />\n</EventQueueProvider>\n\n// In any other component\nimport { useEventQueue } from \"./eventQueue\";\n\nconst eventQueue = useEventQueue();\neventQueue.set(operationId, onSuccess, onFailure);\n```"
25
+ }
26
+ }
27
+ },
28
+ decorators: [Story => /*#__PURE__*/_react.default.createElement(EventQueueProvider, null, /*#__PURE__*/_react.default.createElement(Story, null))]
29
+ };
30
+ var _default = exports.default = meta;
31
+ const Default = exports.Default = {
32
+ render: () => {
33
+ const EventQueueDemo = () => {
34
+ const queue = useEventQueue();
35
+ const operationId = "storybook-op-id";
36
+ const [logs, setLogs] = (0, _react.useState)([]);
37
+ const [isRegistered, setIsRegistered] = (0, _react.useState)(false);
38
+ const appendLog = msg => setLogs(prev => [...prev, msg]);
39
+
40
+ // This simulates the code that is executed when the asynchronous operation starts
41
+ const register = () => {
42
+ queue.set(operationId, event => appendLog("Success: ".concat(event.message)), msg => appendLog("Failure: ".concat(msg)), () => appendLog("Finished"));
43
+ appendLog("Registered callbacks");
44
+ setIsRegistered(true);
45
+ };
46
+
47
+ // This simulates the code that is executed when the asynchronous operation succeeds
48
+ const triggerSuccess = () => {
49
+ const event = queue.get(operationId);
50
+ if (event) {
51
+ var _event$onFinish;
52
+ event.onSuccess({
53
+ message: "This was successful!"
54
+ });
55
+ (_event$onFinish = event.onFinish) === null || _event$onFinish === void 0 || _event$onFinish.call(event);
56
+ queue.remove(operationId);
57
+ setIsRegistered(false);
58
+ }
59
+ };
60
+
61
+ // This simulates the code that is executed when the asynchronous operation fails
62
+ const triggerFailure = () => {
63
+ const event = queue.get(operationId);
64
+ if (event) {
65
+ var _event$onFinish2;
66
+ event.onFailure("Something failed!");
67
+ (_event$onFinish2 = event.onFinish) === null || _event$onFinish2 === void 0 || _event$onFinish2.call(event);
68
+ queue.remove(operationId);
69
+ setIsRegistered(false);
70
+ }
71
+ };
72
+ return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
73
+ style: {
74
+ marginBottom: "0.5rem",
75
+ display: "flex"
76
+ }
77
+ }, /*#__PURE__*/_react.default.createElement(_Button.default, {
78
+ onClick: register,
79
+ disabled: isRegistered
80
+ }, "Register Callbacks"), /*#__PURE__*/_react.default.createElement(_Button.default, {
81
+ onClick: triggerSuccess,
82
+ disabled: !isRegistered
83
+ }, "Trigger Success"), /*#__PURE__*/_react.default.createElement(_Button.default, {
84
+ onClick: triggerFailure,
85
+ disabled: !isRegistered
86
+ }, "Trigger Failure")), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("strong", null, "Logs:"), /*#__PURE__*/_react.default.createElement("ul", null, logs.map((log, index) => /*#__PURE__*/_react.default.createElement("li", {
87
+ key: index
88
+ }, log)))));
89
+ };
90
+ return /*#__PURE__*/_react.default.createElement(EventQueueDemo, null);
91
+ }
92
+ };
@@ -36,7 +36,7 @@
36
36
  .pagination-input {
37
37
  margin: 0 $spv--small 0 $spv--large;
38
38
  min-width: 0;
39
- width: 3rem;
39
+ width: 4rem;
40
40
  }
41
41
 
42
42
  .pagination-select {
@@ -10,38 +10,6 @@ export interface EventQueue<T> {
10
10
  set: (operationId: string, onSuccess: (event: T) => void, onFailure: (msg: string) => void, onFinish?: () => void) => void;
11
11
  remove: (operationId: string) => void;
12
12
  }
13
- /**
14
- * This provides an event queue system for managing callbacks associated with
15
- * asynchronous operations (e.g., API calls) in an application.
16
- *
17
- * It allows components to register success, failure,
18
- * and optional finish handlers for a given operation ID, and later retrieve or remove them.
19
- *
20
- * This is useful for handling side effects when dealing
21
- * with multiple operations that need to be tracked and updated independently.
22
- *
23
- * The `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.
24
- * The returned `EventQueueProvider` and `useEventQueue` hook should be exported
25
- * from a shared module and reused across the app to ensure a single
26
- * context instance is used.
27
- *
28
- * Usage pattern:
29
- * // eventQueue.ts
30
- * export const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();
31
- *
32
- * // App.tsx
33
- * import { EventQueueProvider } from "./eventQueue";
34
- * ...
35
- * <EventQueueProvider>
36
- * <App />
37
- * </EventQueueProvider>
38
- *
39
- * // In any other component
40
- * import { useEventQueue } from "./eventQueue";
41
- * ...
42
- * const eventQueue = useEventQueue();
43
- * eventQueue.set(operationId, onSuccess, onFailure);
44
- */
45
13
  export declare function createEventQueue<T>(): {
46
14
  EventQueueProvider: FC<{
47
15
  children: ReactNode;
@@ -1,38 +1,5 @@
1
1
  import { createContext, useContext } from "react";
2
2
  import React from "react";
3
- /**
4
- * This provides an event queue system for managing callbacks associated with
5
- * asynchronous operations (e.g., API calls) in an application.
6
- *
7
- * It allows components to register success, failure,
8
- * and optional finish handlers for a given operation ID, and later retrieve or remove them.
9
- *
10
- * This is useful for handling side effects when dealing
11
- * with multiple operations that need to be tracked and updated independently.
12
- *
13
- * The `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.
14
- * The returned `EventQueueProvider` and `useEventQueue` hook should be exported
15
- * from a shared module and reused across the app to ensure a single
16
- * context instance is used.
17
- *
18
- * Usage pattern:
19
- * // eventQueue.ts
20
- * export const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();
21
- *
22
- * // App.tsx
23
- * import { EventQueueProvider } from "./eventQueue";
24
- * ...
25
- * <EventQueueProvider>
26
- * <App />
27
- * </EventQueueProvider>
28
- *
29
- * // In any other component
30
- * import { useEventQueue } from "./eventQueue";
31
- * ...
32
- * const eventQueue = useEventQueue();
33
- * eventQueue.set(operationId, onSuccess, onFailure);
34
- */
35
-
36
3
  export function createEventQueue() {
37
4
  var EventQueueContext = /*#__PURE__*/createContext(undefined);
38
5
  var eventQueue = new Map();
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+ import { Meta, StoryObj } from "@storybook/react";
3
+ declare const EventQueueProvider: React.FC<{
4
+ children: React.ReactNode;
5
+ }>;
6
+ declare const meta: Meta<typeof EventQueueProvider>;
7
+ export default meta;
8
+ type Story = StoryObj<typeof EventQueueProvider>;
9
+ export declare const Default: Story;
@@ -0,0 +1,83 @@
1
+ import React, { useState } from "react";
2
+ import { createEventQueue } from "./EventQueue";
3
+ import Button from "../Button/Button";
4
+ var {
5
+ EventQueueProvider,
6
+ useEventQueue
7
+ } = createEventQueue();
8
+ var meta = {
9
+ title: "Components/EventQueue",
10
+ component: EventQueueProvider,
11
+ tags: ["autodocs"],
12
+ parameters: {
13
+ docs: {
14
+ description: {
15
+ component: "\nThis provides an event queue system for managing callbacks associated with\nasynchronous operations (e.g., API calls) in an application.\n\nIt allows components to register success, failure,\nand optional finish handlers for a given operation ID, and later retrieve or remove them.\n\nThis is useful for handling side effects when dealing\nwith multiple operations that need to be tracked and updated independently.\n\nThe `createEventQueue` function should be used to create a single provider and context that are shared throughout your application.\nThe returned `EventQueueProvider` and `useEventQueue` hook should be exported\nfrom a shared module and reused across the app to ensure a single\ncontext instance is used.\n\n**Usage pattern:**\n\n```tsx\n// eventQueue.ts\nexport const { EventQueueProvider, useEventQueue } = createEventQueue<EventType>();\n\n// App.tsx\nimport { EventQueueProvider } from \"./eventQueue\";\n\n<EventQueueProvider>\n <App />\n</EventQueueProvider>\n\n// In any other component\nimport { useEventQueue } from \"./eventQueue\";\n\nconst eventQueue = useEventQueue();\neventQueue.set(operationId, onSuccess, onFailure);\n```"
16
+ }
17
+ }
18
+ },
19
+ decorators: [Story => /*#__PURE__*/React.createElement(EventQueueProvider, null, /*#__PURE__*/React.createElement(Story, null))]
20
+ };
21
+ export default meta;
22
+ export var Default = {
23
+ render: () => {
24
+ var EventQueueDemo = () => {
25
+ var queue = useEventQueue();
26
+ var operationId = "storybook-op-id";
27
+ var [logs, setLogs] = useState([]);
28
+ var [isRegistered, setIsRegistered] = useState(false);
29
+ var appendLog = msg => setLogs(prev => [...prev, msg]);
30
+
31
+ // This simulates the code that is executed when the asynchronous operation starts
32
+ var register = () => {
33
+ queue.set(operationId, event => appendLog("Success: ".concat(event.message)), msg => appendLog("Failure: ".concat(msg)), () => appendLog("Finished"));
34
+ appendLog("Registered callbacks");
35
+ setIsRegistered(true);
36
+ };
37
+
38
+ // This simulates the code that is executed when the asynchronous operation succeeds
39
+ var triggerSuccess = () => {
40
+ var event = queue.get(operationId);
41
+ if (event) {
42
+ var _event$onFinish;
43
+ event.onSuccess({
44
+ message: "This was successful!"
45
+ });
46
+ (_event$onFinish = event.onFinish) === null || _event$onFinish === void 0 || _event$onFinish.call(event);
47
+ queue.remove(operationId);
48
+ setIsRegistered(false);
49
+ }
50
+ };
51
+
52
+ // This simulates the code that is executed when the asynchronous operation fails
53
+ var triggerFailure = () => {
54
+ var event = queue.get(operationId);
55
+ if (event) {
56
+ var _event$onFinish2;
57
+ event.onFailure("Something failed!");
58
+ (_event$onFinish2 = event.onFinish) === null || _event$onFinish2 === void 0 || _event$onFinish2.call(event);
59
+ queue.remove(operationId);
60
+ setIsRegistered(false);
61
+ }
62
+ };
63
+ return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
64
+ style: {
65
+ marginBottom: "0.5rem",
66
+ display: "flex"
67
+ }
68
+ }, /*#__PURE__*/React.createElement(Button, {
69
+ onClick: register,
70
+ disabled: isRegistered
71
+ }, "Register Callbacks"), /*#__PURE__*/React.createElement(Button, {
72
+ onClick: triggerSuccess,
73
+ disabled: !isRegistered
74
+ }, "Trigger Success"), /*#__PURE__*/React.createElement(Button, {
75
+ onClick: triggerFailure,
76
+ disabled: !isRegistered
77
+ }, "Trigger Failure")), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("strong", null, "Logs:"), /*#__PURE__*/React.createElement("ul", null, logs.map((log, index) => /*#__PURE__*/React.createElement("li", {
78
+ key: index
79
+ }, log)))));
80
+ };
81
+ return /*#__PURE__*/React.createElement(EventQueueDemo, null);
82
+ }
83
+ };
@@ -36,7 +36,7 @@
36
36
  .pagination-input {
37
37
  margin: 0 $spv--small 0 $spv--large;
38
38
  min-width: 0;
39
- width: 3rem;
39
+ width: 4rem;
40
40
  }
41
41
 
42
42
  .pagination-select {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonical/react-components",
3
- "version": "2.6.1",
3
+ "version": "2.7.1",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "author": {
@@ -93,7 +93,7 @@
93
93
  "tsc-alias": "1.8.10",
94
94
  "typescript": "5.7.3",
95
95
  "typescript-eslint": "8.24.1",
96
- "vanilla-framework": "4.23.0",
96
+ "vanilla-framework": "4.24.1",
97
97
  "wait-on": "8.0.2",
98
98
  "webpack": "5.98.0"
99
99
  },