@crediball/react 0.3.6 → 0.4.0
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/dist/CrediballProvider.d.ts +67 -8
- package/dist/CrediballProvider.js +99 -10
- package/dist/CreditIndicator.d.ts +9 -4
- package/dist/CreditIndicator.js +16 -10
- package/dist/CreditsBadge.d.ts +6 -3
- package/dist/CreditsBadge.js +17 -18
- package/dist/PaywallModal.d.ts +17 -3
- package/dist/PaywallModal.js +85 -89
- package/dist/creditEvent.d.ts +6 -19
- package/dist/creditEvent.js +6 -29
- package/dist/fetchWatcher.d.ts +15 -9
- package/dist/fetchWatcher.js +29 -37
- package/dist/format.d.ts +4 -0
- package/dist/format.js +13 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +7 -2
- package/dist/theme.d.ts +39 -0
- package/dist/theme.js +43 -0
- package/dist/variables/CrediballBalance.d.ts +26 -0
- package/dist/variables/CrediballBalance.js +19 -0
- package/dist/variables/CrediballLowCreditWarning.d.ts +25 -0
- package/dist/variables/CrediballLowCreditWarning.js +20 -0
- package/dist/variables/CrediballTopUpButton.d.ts +18 -0
- package/dist/variables/CrediballTopUpButton.js +27 -0
- package/dist/variables/CrediballUsage.d.ts +24 -0
- package/dist/variables/CrediballUsage.js +19 -0
- package/package.json +4 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
export interface CrediballBalanceRenderProps {
|
|
3
|
+
balance: number | null;
|
|
4
|
+
loading: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface CrediballBalanceProps {
|
|
7
|
+
className?: string;
|
|
8
|
+
style?: CSSProperties;
|
|
9
|
+
/** Format the raw number. Defaults to a locale-formatted whole number. */
|
|
10
|
+
format?: (balance: number) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Static content overrides the balance entirely. Pass a function to render
|
|
13
|
+
* fully custom markup from the live value (the escape hatch for anything
|
|
14
|
+
* this component's default <span> doesn't cover).
|
|
15
|
+
*/
|
|
16
|
+
children?: ReactNode | ((props: CrediballBalanceRenderProps) => ReactNode);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Tier 2 — a "variable" you drop anywhere inside <CrediballProvider>. Renders
|
|
20
|
+
* as an inline <span> that inherits the surrounding font, size, and color
|
|
21
|
+
* (`font: inherit; color: currentColor`) so it looks native wherever it's
|
|
22
|
+
* placed — a navbar, a sentence, a settings row.
|
|
23
|
+
*
|
|
24
|
+
* <p>You have <CrediballBalance /> credits left.</p>
|
|
25
|
+
*/
|
|
26
|
+
export declare function CrediballBalance({ className, style, format, children }: CrediballBalanceProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useCrediball } from "../CrediballProvider";
|
|
4
|
+
import { formatCredits } from "../format";
|
|
5
|
+
/**
|
|
6
|
+
* Tier 2 — a "variable" you drop anywhere inside <CrediballProvider>. Renders
|
|
7
|
+
* as an inline <span> that inherits the surrounding font, size, and color
|
|
8
|
+
* (`font: inherit; color: currentColor`) so it looks native wherever it's
|
|
9
|
+
* placed — a navbar, a sentence, a settings row.
|
|
10
|
+
*
|
|
11
|
+
* <p>You have <CrediballBalance /> credits left.</p>
|
|
12
|
+
*/
|
|
13
|
+
export function CrediballBalance({ className, style, format = formatCredits, children }) {
|
|
14
|
+
const { balance, loading } = useCrediball();
|
|
15
|
+
if (typeof children === "function") {
|
|
16
|
+
return _jsx(_Fragment, { children: children({ balance, loading }) });
|
|
17
|
+
}
|
|
18
|
+
return (_jsx("span", { className: className, style: { font: "inherit", color: "currentColor", ...style }, children: children ?? (balance == null ? "—" : format(balance)) }));
|
|
19
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
export interface CrediballLowCreditWarningRenderProps {
|
|
3
|
+
balance: number | null;
|
|
4
|
+
isLow: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface CrediballLowCreditWarningProps {
|
|
7
|
+
className?: string;
|
|
8
|
+
style?: CSSProperties;
|
|
9
|
+
/**
|
|
10
|
+
* Override the provider's default threshold (highest-cost active event) for
|
|
11
|
+
* just this instance — e.g. warn earlier next to an expensive action.
|
|
12
|
+
*/
|
|
13
|
+
threshold?: number;
|
|
14
|
+
/** Message shown when low. Defaults to a generic nudge. */
|
|
15
|
+
message?: string;
|
|
16
|
+
children?: ReactNode | ((props: CrediballLowCreditWarningRenderProps) => ReactNode);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Tier 2 — renders nothing until the user's balance is low, then shows a
|
|
20
|
+
* message inline. Place it next to a specific action ("this costs 20 credits")
|
|
21
|
+
* or globally near the top of the app.
|
|
22
|
+
*
|
|
23
|
+
* <CrediballLowCreditWarning threshold={20} message="Not enough for one more image." />
|
|
24
|
+
*/
|
|
25
|
+
export declare function CrediballLowCreditWarning({ className, style, threshold, message, children, }: CrediballLowCreditWarningProps): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useCrediball } from "../CrediballProvider";
|
|
4
|
+
/**
|
|
5
|
+
* Tier 2 — renders nothing until the user's balance is low, then shows a
|
|
6
|
+
* message inline. Place it next to a specific action ("this costs 20 credits")
|
|
7
|
+
* or globally near the top of the app.
|
|
8
|
+
*
|
|
9
|
+
* <CrediballLowCreditWarning threshold={20} message="Not enough for one more image." />
|
|
10
|
+
*/
|
|
11
|
+
export function CrediballLowCreditWarning({ className, style, threshold, message = "Low credit balance — top up to keep going.", children, }) {
|
|
12
|
+
const { balance, isLow: contextIsLow } = useCrediball();
|
|
13
|
+
const isLow = threshold != null && balance != null ? balance < threshold : contextIsLow;
|
|
14
|
+
if (!isLow)
|
|
15
|
+
return null;
|
|
16
|
+
if (typeof children === "function") {
|
|
17
|
+
return _jsx(_Fragment, { children: children({ balance, isLow }) });
|
|
18
|
+
}
|
|
19
|
+
return (_jsx("span", { className: className, style: { font: "inherit", color: "currentColor", ...style }, children: children ?? message }));
|
|
20
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
import { type CrediballThemeOverrides } from "../theme";
|
|
3
|
+
export interface CrediballTopUpButtonProps extends CrediballThemeOverrides {
|
|
4
|
+
className?: string;
|
|
5
|
+
style?: CSSProperties;
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
/** Defaults to opening the provider's paywall. */
|
|
8
|
+
onClick?: () => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Tier 2 — a themeable call-to-action that opens the paywall. Inherits the
|
|
12
|
+
* surrounding font; background/text color come from the same `--crediball-*`
|
|
13
|
+
* variables as every other component, so it matches PaywallModal and
|
|
14
|
+
* CreditsBadge without extra styling.
|
|
15
|
+
*
|
|
16
|
+
* <CrediballTopUpButton>Get more credits</CrediballTopUpButton>
|
|
17
|
+
*/
|
|
18
|
+
export declare function CrediballTopUpButton({ className, style, children, onClick, accentColor }: CrediballTopUpButtonProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useCrediball } from "../CrediballProvider";
|
|
4
|
+
import { theme, accentStyle } from "../theme";
|
|
5
|
+
/**
|
|
6
|
+
* Tier 2 — a themeable call-to-action that opens the paywall. Inherits the
|
|
7
|
+
* surrounding font; background/text color come from the same `--crediball-*`
|
|
8
|
+
* variables as every other component, so it matches PaywallModal and
|
|
9
|
+
* CreditsBadge without extra styling.
|
|
10
|
+
*
|
|
11
|
+
* <CrediballTopUpButton>Get more credits</CrediballTopUpButton>
|
|
12
|
+
*/
|
|
13
|
+
export function CrediballTopUpButton({ className, style, children, onClick, accentColor }) {
|
|
14
|
+
const { openPaywall } = useCrediball();
|
|
15
|
+
return (_jsx("button", { type: "button", className: className, onClick: onClick ?? openPaywall, style: {
|
|
16
|
+
font: "inherit",
|
|
17
|
+
appearance: "none",
|
|
18
|
+
border: "none",
|
|
19
|
+
cursor: "pointer",
|
|
20
|
+
color: theme.onAccent,
|
|
21
|
+
background: theme.accent,
|
|
22
|
+
borderRadius: theme.radiusPill,
|
|
23
|
+
padding: "0.5em 1.1em",
|
|
24
|
+
...accentStyle(accentColor),
|
|
25
|
+
...style,
|
|
26
|
+
}, children: children ?? "Add credits" }));
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
import type { UsagePeriod } from "@crediball/core";
|
|
3
|
+
export interface CrediballUsageRenderProps {
|
|
4
|
+
usage: UsagePeriod | null;
|
|
5
|
+
value: number | null;
|
|
6
|
+
loading: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface CrediballUsageProps {
|
|
9
|
+
className?: string;
|
|
10
|
+
style?: CSSProperties;
|
|
11
|
+
/** Show credits consumed this period, or credits remaining. Default "used". */
|
|
12
|
+
show?: "used" | "remaining";
|
|
13
|
+
/** Format the raw number. Defaults to a locale-formatted whole number. */
|
|
14
|
+
format?: (value: number) => string;
|
|
15
|
+
children?: ReactNode | ((props: CrediballUsageRenderProps) => ReactNode);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Tier 2 — credits consumed in the user's current period (their subscription's
|
|
19
|
+
* billing cycle, or the calendar month for non-subscribers). Inline, inherits
|
|
20
|
+
* host styling. Pass `show="remaining"` to render the balance instead.
|
|
21
|
+
*
|
|
22
|
+
* <p><CrediballUsage /> credits used this month.</p>
|
|
23
|
+
*/
|
|
24
|
+
export declare function CrediballUsage({ className, style, show, format, children, }: CrediballUsageProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useCrediball } from "../CrediballProvider";
|
|
4
|
+
import { formatCredits } from "../format";
|
|
5
|
+
/**
|
|
6
|
+
* Tier 2 — credits consumed in the user's current period (their subscription's
|
|
7
|
+
* billing cycle, or the calendar month for non-subscribers). Inline, inherits
|
|
8
|
+
* host styling. Pass `show="remaining"` to render the balance instead.
|
|
9
|
+
*
|
|
10
|
+
* <p><CrediballUsage /> credits used this month.</p>
|
|
11
|
+
*/
|
|
12
|
+
export function CrediballUsage({ className, style, show = "used", format = formatCredits, children, }) {
|
|
13
|
+
const { usage, remaining, loading } = useCrediball();
|
|
14
|
+
const value = show === "remaining" ? remaining : (usage?.usedCredits ?? null);
|
|
15
|
+
if (typeof children === "function") {
|
|
16
|
+
return _jsx(_Fragment, { children: children({ usage, value, loading }) });
|
|
17
|
+
}
|
|
18
|
+
return (_jsx("span", { className: className, style: { font: "inherit", color: "currentColor", ...style }, children: children ?? (value == null ? "—" : format(value)) }));
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crediball/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Drop-in React components for showing Crediball credits inside your AI app.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Filippo Rezzadore <filipporezzadore@gmail.com>",
|
|
@@ -50,6 +50,9 @@
|
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"react": ">=18"
|
|
52
52
|
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@crediball/core": "^0.1.0"
|
|
55
|
+
},
|
|
53
56
|
"devDependencies": {
|
|
54
57
|
"@types/react": "^18.3.11",
|
|
55
58
|
"typescript": "^5.6.3"
|