axokit-react 0.0.2 → 0.0.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.
- package/README.md +2 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +27 -1
- package/dist/index.mjs +28 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -34,6 +34,7 @@ function SaveButton() {
|
|
|
34
34
|
loadingText="Saving..."
|
|
35
35
|
successText="Saved ✓"
|
|
36
36
|
errorText="Try again"
|
|
37
|
+
showSpinner // built‑in loader
|
|
37
38
|
disableOnSuccess={false} // optional
|
|
38
39
|
className="px-4 py-2 rounded bg-blue-600 text-white"
|
|
39
40
|
>
|
|
@@ -74,6 +75,7 @@ function HookExample() {
|
|
|
74
75
|
- `successDuration?: number` — default: `1200` ms.
|
|
75
76
|
- `errorDuration?: number` — default: `3000` ms.
|
|
76
77
|
- `disableOnSuccess?: boolean` — default: `true`.
|
|
78
|
+
- `showSpinner?: boolean` — default: `false`. When `true`, a tiny built‑in SVG spinner appears to the left of the label during loading.
|
|
77
79
|
- `onSuccess?: () => void`.
|
|
78
80
|
- `onActionError?: (error: Error) => void`.
|
|
79
81
|
- All other native `button` props are forwarded.
|
package/dist/index.d.ts
CHANGED
|
@@ -25,8 +25,10 @@ interface AsyncButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement>
|
|
|
25
25
|
onSuccess?: () => void;
|
|
26
26
|
onActionError?: (error: Error) => void;
|
|
27
27
|
disableOnSuccess?: boolean;
|
|
28
|
+
/** automatically render a small spinner next to the label when loading */
|
|
29
|
+
showSpinner?: boolean;
|
|
28
30
|
children: React.ReactNode;
|
|
29
31
|
}
|
|
30
|
-
declare function AsyncButton({ onAction, loadingText, successText, errorText, successDuration, errorDuration, onSuccess, onActionError, disableOnSuccess, children, disabled, className, ...props }: AsyncButtonProps): react_jsx_runtime.JSX.Element;
|
|
32
|
+
declare function AsyncButton({ onAction, loadingText, successText, errorText, successDuration, errorDuration, onSuccess, onActionError, disableOnSuccess, showSpinner, children, disabled, className, ...props }: AsyncButtonProps): react_jsx_runtime.JSX.Element;
|
|
31
33
|
|
|
32
34
|
export { AsyncButton, useAsyncAction };
|
package/dist/index.js
CHANGED
|
@@ -109,6 +109,7 @@ function AsyncButton({
|
|
|
109
109
|
onSuccess,
|
|
110
110
|
onActionError,
|
|
111
111
|
disableOnSuccess = true,
|
|
112
|
+
showSpinner = false,
|
|
112
113
|
children,
|
|
113
114
|
disabled,
|
|
114
115
|
className = "",
|
|
@@ -132,6 +133,31 @@ function AsyncButton({
|
|
|
132
133
|
return children;
|
|
133
134
|
}
|
|
134
135
|
};
|
|
136
|
+
const renderContent = () => {
|
|
137
|
+
const label = getLabel();
|
|
138
|
+
if (state === "loading" && showSpinner) {
|
|
139
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { display: "flex", gap: "0.5em", alignItems: "center" }, children: [
|
|
140
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
141
|
+
"svg",
|
|
142
|
+
{
|
|
143
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
144
|
+
width: "24",
|
|
145
|
+
height: "24",
|
|
146
|
+
viewBox: "0 0 24 24",
|
|
147
|
+
fill: "none",
|
|
148
|
+
stroke: "currentColor",
|
|
149
|
+
"stroke-width": "2",
|
|
150
|
+
"stroke-linecap": "round",
|
|
151
|
+
"stroke-linejoin": "round",
|
|
152
|
+
className: "lucide lucide-loader-circle-icon lucide-loader-circle",
|
|
153
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "M21 12a9 9 0 1 1-6.219-8.56" })
|
|
154
|
+
}
|
|
155
|
+
),
|
|
156
|
+
label
|
|
157
|
+
] }) });
|
|
158
|
+
}
|
|
159
|
+
return label;
|
|
160
|
+
};
|
|
135
161
|
const isDisabled = disabled || state === "loading" || state === "success" && disableOnSuccess;
|
|
136
162
|
const { onClick: _nativeOnClick, type: buttonType = "button", ...rest } = props;
|
|
137
163
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
@@ -144,7 +170,7 @@ function AsyncButton({
|
|
|
144
170
|
disabled: isDisabled,
|
|
145
171
|
"aria-busy": state === "loading",
|
|
146
172
|
"data-state": state,
|
|
147
|
-
children:
|
|
173
|
+
children: renderContent()
|
|
148
174
|
}
|
|
149
175
|
);
|
|
150
176
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/AsyncButton.tsx
|
|
2
2
|
import * as React from "react";
|
|
3
|
-
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
4
|
function useAsyncAction(action, options = {}) {
|
|
5
5
|
const { successDuration = 1200, errorDuration = 3e3, onSuccess, onActionError } = options;
|
|
6
6
|
const [state, setState] = React.useState("idle");
|
|
@@ -72,6 +72,7 @@ function AsyncButton({
|
|
|
72
72
|
onSuccess,
|
|
73
73
|
onActionError,
|
|
74
74
|
disableOnSuccess = true,
|
|
75
|
+
showSpinner = false,
|
|
75
76
|
children,
|
|
76
77
|
disabled,
|
|
77
78
|
className = "",
|
|
@@ -95,6 +96,31 @@ function AsyncButton({
|
|
|
95
96
|
return children;
|
|
96
97
|
}
|
|
97
98
|
};
|
|
99
|
+
const renderContent = () => {
|
|
100
|
+
const label = getLabel();
|
|
101
|
+
if (state === "loading" && showSpinner) {
|
|
102
|
+
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: "0.5em", alignItems: "center" }, children: [
|
|
103
|
+
/* @__PURE__ */ jsx(
|
|
104
|
+
"svg",
|
|
105
|
+
{
|
|
106
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
107
|
+
width: "24",
|
|
108
|
+
height: "24",
|
|
109
|
+
viewBox: "0 0 24 24",
|
|
110
|
+
fill: "none",
|
|
111
|
+
stroke: "currentColor",
|
|
112
|
+
"stroke-width": "2",
|
|
113
|
+
"stroke-linecap": "round",
|
|
114
|
+
"stroke-linejoin": "round",
|
|
115
|
+
className: "lucide lucide-loader-circle-icon lucide-loader-circle",
|
|
116
|
+
children: /* @__PURE__ */ jsx("path", { d: "M21 12a9 9 0 1 1-6.219-8.56" })
|
|
117
|
+
}
|
|
118
|
+
),
|
|
119
|
+
label
|
|
120
|
+
] }) });
|
|
121
|
+
}
|
|
122
|
+
return label;
|
|
123
|
+
};
|
|
98
124
|
const isDisabled = disabled || state === "loading" || state === "success" && disableOnSuccess;
|
|
99
125
|
const { onClick: _nativeOnClick, type: buttonType = "button", ...rest } = props;
|
|
100
126
|
return /* @__PURE__ */ jsx(
|
|
@@ -107,7 +133,7 @@ function AsyncButton({
|
|
|
107
133
|
disabled: isDisabled,
|
|
108
134
|
"aria-busy": state === "loading",
|
|
109
135
|
"data-state": state,
|
|
110
|
-
children:
|
|
136
|
+
children: renderContent()
|
|
111
137
|
}
|
|
112
138
|
);
|
|
113
139
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "axokit-react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Headless async button and hook for React",
|
|
5
|
-
"main": "dist/index.
|
|
6
|
-
"module": "dist/index.
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|