@blocklet/payment-react 1.20.5 → 1.20.6
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/.aigne/doc-smith/config.yaml +2 -2
- package/.aigne/doc-smith/upload-cache.yaml +381 -0
- package/docs/components-business-auto-topup.md +128 -179
- package/docs/components-business-overdue-invoice-payment.md +108 -143
- package/docs/components-business-resume-subscription.md +117 -104
- package/docs/components-business.md +12 -36
- package/docs/components-checkout-checkout-donate.md +209 -149
- package/docs/components-checkout-checkout-form.md +115 -136
- package/docs/components-checkout-checkout-table.md +92 -172
- package/docs/components-checkout.md +43 -109
- package/docs/components-history-credit-grants-list.md +45 -70
- package/docs/components-history-credit-transactions-list.md +57 -67
- package/docs/components-history-invoice-list.md +58 -52
- package/docs/components-history-payment-list.md +19 -40
- package/docs/components-history.md +42 -67
- package/docs/components-ui-form-elements-address-form.md +37 -65
- package/docs/components-ui-form-elements-country-select.md +80 -59
- package/docs/components-ui-form-elements-currency-selector.md +57 -73
- package/docs/components-ui-form-elements-phone-input.md +90 -112
- package/docs/components-ui-form-elements.md +46 -80
- package/docs/components-ui-payment-summary.md +71 -119
- package/docs/components-ui-pricing-table.md +117 -204
- package/docs/components-ui.md +59 -32
- package/docs/components.md +89 -62
- package/docs/getting-started.md +36 -63
- package/docs/guides-theming.md +122 -84
- package/docs/guides-utilities.md +107 -145
- package/docs/guides.md +7 -84
- package/docs/hooks-use-mobile.md +50 -36
- package/docs/hooks-use-subscription.md +72 -89
- package/docs/hooks.md +12 -82
- package/docs/overview.md +45 -52
- package/docs/providers-donate-provider.md +73 -95
- package/docs/providers-payment-provider.md +115 -169
- package/docs/providers.md +27 -86
- package/es/locales/en.js +7 -0
- package/es/locales/zh.js +8 -1
- package/es/payment/index.js +3 -0
- package/es/payment/progress-item.d.ts +12 -0
- package/es/payment/progress-item.js +78 -0
- package/es/payment/success.d.ts +4 -1
- package/es/payment/success.js +55 -3
- package/lib/locales/en.js +7 -0
- package/lib/locales/zh.js +8 -1
- package/lib/payment/index.js +3 -0
- package/lib/payment/progress-item.d.ts +12 -0
- package/lib/payment/progress-item.js +107 -0
- package/lib/payment/success.d.ts +4 -1
- package/lib/payment/success.js +59 -3
- package/package.json +3 -3
- package/src/locales/en.tsx +7 -0
- package/src/locales/zh.tsx +8 -1
- package/src/payment/index.tsx +6 -0
- package/src/payment/progress-item.tsx +107 -0
- package/src/payment/success.tsx +88 -3
package/docs/guides-utilities.md
CHANGED
|
@@ -1,235 +1,197 @@
|
|
|
1
1
|
# Utilities
|
|
2
2
|
|
|
3
|
-
The `@blocklet/payment-react` library exports
|
|
3
|
+
The `@blocklet/payment-react` library exports a suite of utility functions and classes designed to simplify common tasks such as making API requests, managing cache, handling dates, formatting prices, and setting up internationalization. These tools are built to work seamlessly with the library's components and providers.
|
|
4
4
|
|
|
5
5
|
## API Client
|
|
6
6
|
|
|
7
|
-
The library
|
|
7
|
+
The library provides a pre-configured Axios instance for making API requests to your payment backend. It automatically handles base URLs and other necessary configurations.
|
|
8
8
|
|
|
9
|
-
```tsx
|
|
9
|
+
```tsx API Client Usage icon=logos:javascript
|
|
10
10
|
import { api } from '@blocklet/payment-react';
|
|
11
11
|
|
|
12
12
|
// Basic GET request
|
|
13
|
-
const
|
|
13
|
+
const getPayments = async () => {
|
|
14
|
+
const response = await api.get('/api/payments');
|
|
15
|
+
return response.data;
|
|
16
|
+
};
|
|
14
17
|
|
|
15
|
-
// POST request with a
|
|
16
|
-
const
|
|
18
|
+
// POST request with a payload
|
|
19
|
+
const createCheckout = async (amount) => {
|
|
20
|
+
const data = await api.post('/api/checkout', { amount });
|
|
21
|
+
return data;
|
|
22
|
+
};
|
|
17
23
|
|
|
18
|
-
//
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
}
|
|
24
|
+
// GET request with query parameters
|
|
25
|
+
const getPaidInvoices = async () => {
|
|
26
|
+
const results = await api.get('/api/invoices', {
|
|
27
|
+
params: { status: 'paid' }
|
|
28
|
+
});
|
|
29
|
+
return results.data;
|
|
30
|
+
};
|
|
22
31
|
|
|
23
|
-
//
|
|
24
|
-
const
|
|
25
|
-
|
|
32
|
+
// PUT request with additional configuration
|
|
33
|
+
const updateSubscription = async (data) => {
|
|
34
|
+
const config = {
|
|
35
|
+
headers: { 'Custom-Header': 'value' }
|
|
36
|
+
};
|
|
37
|
+
const response = await api.put('/api/subscription', data, config);
|
|
38
|
+
return response.data;
|
|
26
39
|
};
|
|
27
|
-
const response = await api.put('/api/subscription', data, config);
|
|
28
40
|
```
|
|
29
41
|
|
|
30
42
|
## CachedRequest
|
|
31
43
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
```d2
|
|
35
|
-
direction: down
|
|
36
|
-
|
|
37
|
-
fetch: "Call priceRequest.fetch()"
|
|
38
|
-
cache_check: "Is data in cache and not expired?"
|
|
39
|
-
|
|
40
|
-
fetch -> cache_check
|
|
44
|
+
To optimize performance and reduce redundant network requests, you can use the `CachedRequest` class. It provides a simple way to cache data with configurable strategies and time-to-live (TTL).
|
|
41
45
|
|
|
42
|
-
|
|
43
|
-
direction: down
|
|
44
|
-
style.stroke: "#4CAF50"
|
|
45
|
-
return_cached: "Return cached data"
|
|
46
|
-
}
|
|
46
|
+
### Configuration Options
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
direction: down
|
|
50
|
-
style.stroke: "#F44336"
|
|
51
|
-
api_call: "Execute api.get('/api/prices')"
|
|
52
|
-
store_cache: "Store response in cache with timestamp"
|
|
53
|
-
return_fresh: "Return fresh data"
|
|
54
|
-
api_call -> store_cache -> return_fresh
|
|
55
|
-
}
|
|
48
|
+
When creating a `CachedRequest` instance, you can provide the following options:
|
|
56
49
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
50
|
+
| Option | Type | Description | Default |
|
|
51
|
+
| :--- | :--- | :--- | :--- |
|
|
52
|
+
| `strategy` | `'session' \| 'local' \| 'memory'` | The storage mechanism for the cache. | `'session'` |
|
|
53
|
+
| `ttl` | `number` | The cache's time-to-live in milliseconds. A value of `0` means no expiration. | `0` |
|
|
60
54
|
|
|
61
|
-
### Usage
|
|
55
|
+
### Usage Example
|
|
62
56
|
|
|
63
|
-
|
|
57
|
+
Here’s how to create and use a cached request to fetch product prices.
|
|
64
58
|
|
|
65
|
-
```tsx
|
|
59
|
+
```tsx CachedRequest Example icon=logos:javascript
|
|
66
60
|
import { CachedRequest, api } from '@blocklet/payment-react';
|
|
67
61
|
|
|
68
|
-
// Create a cached request instance
|
|
62
|
+
// 1. Create a cached request instance
|
|
69
63
|
const priceRequest = new CachedRequest(
|
|
70
|
-
'product-prices',
|
|
64
|
+
'product-prices',
|
|
71
65
|
() => api.get('/api/prices'),
|
|
72
66
|
{
|
|
73
|
-
strategy: 'session', //
|
|
67
|
+
strategy: 'session', // Cache data in sessionStorage
|
|
74
68
|
ttl: 5 * 60 * 1000 // Cache for 5 minutes
|
|
75
69
|
}
|
|
76
70
|
);
|
|
77
71
|
|
|
78
|
-
// Use the cached request
|
|
72
|
+
// 2. Use the cached request in your component
|
|
79
73
|
async function fetchPrices() {
|
|
80
|
-
// This will use the cache if available and not expired
|
|
74
|
+
// This will use the cache if it's available and not expired
|
|
81
75
|
const prices = await priceRequest.fetch();
|
|
82
|
-
|
|
83
|
-
// To bypass the cache and force a
|
|
76
|
+
|
|
77
|
+
// To bypass the cache and force a fresh data fetch
|
|
84
78
|
const freshPrices = await priceRequest.fetch(true);
|
|
85
|
-
|
|
79
|
+
|
|
86
80
|
return prices;
|
|
87
81
|
}
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### Configuration Options
|
|
91
82
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
83
|
+
// 3. Clear the cache when data changes
|
|
84
|
+
async function updatePrices() {
|
|
85
|
+
await api.post('/api/prices', newPriceData);
|
|
86
|
+
priceRequest.clearCache(); // Or await priceRequest.fetch(true);
|
|
87
|
+
}
|
|
88
|
+
```
|
|
96
89
|
|
|
97
|
-
## Date Handling
|
|
90
|
+
## Date Handling
|
|
98
91
|
|
|
99
|
-
The library exports a pre-configured `dayjs` instance with useful plugins
|
|
92
|
+
The library re-exports a pre-configured `dayjs` instance with useful plugins like `relativeTime`, `utc`, and `timezone` already included. You can use this for any date and time manipulation needs.
|
|
100
93
|
|
|
101
|
-
```tsx
|
|
94
|
+
```tsx Date Handling with dayjs icon=logos:javascript
|
|
102
95
|
import { dayjs } from '@blocklet/payment-react';
|
|
103
96
|
|
|
104
97
|
// Format the current date
|
|
105
|
-
const formattedDate = dayjs().format('YYYY-MM-DD');
|
|
98
|
+
const formattedDate = dayjs().format('YYYY-MM-DD HH:mm');
|
|
106
99
|
|
|
107
|
-
// Parse a timestamp
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
const unixValue = dateObject.unix();
|
|
100
|
+
// Parse a timestamp
|
|
101
|
+
const dateFromTimestamp = dayjs(1672531200000);
|
|
102
|
+
const unixTimestamp = dateFromTimestamp.unix();
|
|
111
103
|
|
|
112
|
-
//
|
|
104
|
+
// Calculate relative time
|
|
113
105
|
const fiveMinutesAgo = dayjs().subtract(5, 'minute');
|
|
114
|
-
const
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
## Formatting Utilities
|
|
118
|
-
|
|
119
|
-
A collection of functions to format data for display, from prices and statuses to plain text.
|
|
120
|
-
|
|
121
|
-
### Price and Amount Formatting
|
|
122
|
-
|
|
123
|
-
These functions help display monetary values and quantities correctly according to currency decimals and locale conventions.
|
|
124
|
-
|
|
125
|
-
- `formatPrice(price, currency, unit_label, quantity)`: Formats a full price object into a human-readable string, including recurring intervals (e.g., "$10.00 / month").
|
|
126
|
-
- `formatAmount(amount, decimals)`: Formats a raw amount string based on the currency's decimal places.
|
|
127
|
-
- `formatBNStr(str, decimals, precision)`: Formats a BigNumber string into a readable number, handling large values and token conversions.
|
|
128
|
-
|
|
129
|
-
```tsx
|
|
130
|
-
import { formatPrice, formatAmount } from '@blocklet/payment-react';
|
|
131
|
-
|
|
132
|
-
// Example price and currency objects (simplified)
|
|
133
|
-
const price = { type: 'recurring', recurring: { interval: 'month', interval_count: 1 }, unit_amount: '1000' };
|
|
134
|
-
const currency = { symbol: '$', decimal: 2 };
|
|
135
|
-
|
|
136
|
-
const displayPrice = formatPrice(price, currency);
|
|
137
|
-
// Result: "10.00 $ per month"
|
|
138
|
-
|
|
139
|
-
const displayAmount = formatAmount('12345', 2);
|
|
140
|
-
// Result: "123.45"
|
|
106
|
+
const relativeTime = dayjs().from(fiveMinutesAgo); // "5 minutes ago"
|
|
141
107
|
```
|
|
142
108
|
|
|
143
|
-
### Status Colors
|
|
144
|
-
|
|
145
|
-
Several utility functions map resource statuses to semantic colors (`success`, `warning`, `error`, `primary`, `default`) for consistent UI indicators in components like `<Status />`.
|
|
146
|
-
|
|
147
|
-
| Function | Status Examples | Color |
|
|
148
|
-
|---|---|---|
|
|
149
|
-
| `getSubscriptionStatusColor` | `active`, `trialing` | `success`, `primary` |
|
|
150
|
-
| `getInvoiceStatusColor` | `paid`, `open`, `uncollectible` | `success`, `secondary`, `warning` |
|
|
151
|
-
| `getPaymentIntentStatusColor` | `succeeded`, `requires_action` | `success`, `warning` |
|
|
152
|
-
| `getRefundStatusColor` | `succeeded`, `pending` | `success`, `default` |
|
|
153
|
-
|
|
154
|
-
### Text Utilities
|
|
155
|
-
|
|
156
|
-
- `truncateText(text, maxLength, useWidth)`: Truncates a string to a specified length, adding an ellipsis. The `useWidth` option calculates length based on character width for better CJK character handling.
|
|
157
|
-
|
|
158
109
|
## Internationalization (i18n)
|
|
159
110
|
|
|
160
|
-
|
|
111
|
+
`@blocklet/payment-react` includes built-in support for i18n. You can create your own translator or merge your translations with the library's defaults.
|
|
161
112
|
|
|
162
|
-
###
|
|
113
|
+
### Creating a Translator
|
|
163
114
|
|
|
164
|
-
|
|
115
|
+
Use the `createTranslator` function to set up your own translation instance.
|
|
165
116
|
|
|
166
|
-
```tsx
|
|
117
|
+
```tsx Custom Translator Setup icon=logos:javascript
|
|
167
118
|
import { createTranslator } from '@blocklet/payment-react';
|
|
168
119
|
|
|
169
120
|
const myTranslations = {
|
|
170
|
-
en: {
|
|
121
|
+
en: {
|
|
171
122
|
checkout: { title: 'Complete Payment' }
|
|
172
123
|
},
|
|
173
|
-
zh: {
|
|
124
|
+
zh: {
|
|
174
125
|
checkout: { title: '完成支付' }
|
|
175
126
|
}
|
|
176
127
|
};
|
|
177
128
|
|
|
178
129
|
const translator = createTranslator({ fallbackLocale: 'en' }, myTranslations);
|
|
179
130
|
|
|
180
|
-
|
|
131
|
+
translator('checkout.title', 'zh'); // '完成支付'
|
|
181
132
|
```
|
|
182
133
|
|
|
183
134
|
### Merging with Library Translations
|
|
184
135
|
|
|
185
|
-
To leverage the built-in translations for
|
|
136
|
+
To leverage the library's built-in translations for components while adding your own, you can merge them together.
|
|
186
137
|
|
|
187
|
-
```tsx
|
|
138
|
+
```tsx Merging Translations icon=logos:javascript
|
|
188
139
|
import { translations as paymentTranslations } from '@blocklet/payment-react';
|
|
189
140
|
import merge from 'lodash/merge';
|
|
190
141
|
|
|
191
|
-
|
|
192
|
-
import
|
|
142
|
+
// Your application's translations
|
|
143
|
+
import en from './locales/en';
|
|
144
|
+
import zh from './locales/zh';
|
|
193
145
|
|
|
194
146
|
export const translations = merge(
|
|
195
147
|
{
|
|
196
|
-
zh,
|
|
197
148
|
en,
|
|
149
|
+
zh,
|
|
198
150
|
},
|
|
199
151
|
paymentTranslations
|
|
200
152
|
);
|
|
201
153
|
```
|
|
202
154
|
|
|
203
|
-
##
|
|
204
|
-
|
|
205
|
-
To optimize your application's bundle size, you can use the `createLazyComponent` utility to dynamically load components only when they are needed.
|
|
155
|
+
## Formatting Utilities
|
|
206
156
|
|
|
207
|
-
|
|
208
|
-
import { createLazyComponent } from '@blocklet/payment-react';
|
|
157
|
+
Several helper functions are available to format data for display.
|
|
209
158
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
159
|
+
| Function | Description |
|
|
160
|
+
| :--- | :--- |
|
|
161
|
+
| `formatPrice` | Formats a price object into a human-readable string, considering recurring intervals. |
|
|
162
|
+
| `formatRecurring` | Formats a recurring object into strings like "monthly" or "every 3 months". |
|
|
163
|
+
| `formatBNStr` | Formats a BigNumber string from a base unit into a token value with specified precision. |
|
|
164
|
+
| `formatNumber` | Formats a number or string with thousand separators and precision. |
|
|
165
|
+
| `formatError` | Parses various error formats (GraphQL, Joi, Axios) into a readable string. |
|
|
216
166
|
|
|
217
|
-
|
|
218
|
-
|
|
167
|
+
```tsx Formatting Example icon=logos:javascript
|
|
168
|
+
import { formatNumber, formatRecurring } from '@blocklet/payment-react';
|
|
219
169
|
|
|
220
|
-
|
|
221
|
-
|
|
170
|
+
// Format a number
|
|
171
|
+
const formatted = formatNumber('1234567.89123', 2); // "1,234,567.89"
|
|
222
172
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
{/* This component will be loaded on demand */}
|
|
227
|
-
<LazyLoadedComponent />
|
|
228
|
-
</div>
|
|
229
|
-
);
|
|
230
|
-
}
|
|
173
|
+
// Format a recurring interval
|
|
174
|
+
const monthly = formatRecurring({ interval: 'month', interval_count: 1 }); // "per month"
|
|
175
|
+
const quarterly = formatRecurring({ interval: 'month', interval_count: 3 }); // "Quarterly"
|
|
231
176
|
```
|
|
232
177
|
|
|
233
|
-
|
|
178
|
+
## Validation Utilities
|
|
179
|
+
|
|
180
|
+
The library also includes helpers for form validation.
|
|
181
|
+
|
|
182
|
+
### `validatePostalCode`
|
|
183
|
+
|
|
184
|
+
Checks if a given postal code is valid for a specific country.
|
|
185
|
+
|
|
186
|
+
```tsx Postal Code Validation icon=logos:javascript
|
|
187
|
+
import { validatePostalCode } from '@blocklet/payment-react';
|
|
188
|
+
|
|
189
|
+
// Returns true
|
|
190
|
+
const isValidUS = validatePostalCode('90210', 'US');
|
|
191
|
+
|
|
192
|
+
// Returns false
|
|
193
|
+
const isInvalidUS = validatePostalCode('ABC-123', 'US');
|
|
234
194
|
|
|
235
|
-
|
|
195
|
+
// Returns true for a UK postal code
|
|
196
|
+
const isValidGB = validatePostalCode('SW1A 0AA', 'GB');
|
|
197
|
+
```
|
package/docs/guides.md
CHANGED
|
@@ -1,95 +1,18 @@
|
|
|
1
1
|
# Guides
|
|
2
2
|
|
|
3
|
-
Welcome to the guides section.
|
|
3
|
+
Welcome to the guides section. Here, you'll find in-depth articles on advanced topics that go beyond individual component usage. These guides are designed to help you customize the library's appearance and leverage its powerful utility functions to build more sophisticated payment solutions.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Explore the guides below to master theming and utilities.
|
|
6
6
|
|
|
7
|
-
<x-cards>
|
|
7
|
+
<x-cards data-columns="2">
|
|
8
8
|
<x-card data-title="Theming" data-icon="lucide:palette" data-href="/guides/theming">
|
|
9
|
-
Learn how to customize the look and feel of payment components to match your application's
|
|
9
|
+
Learn how to customize the look and feel of payment components to match your application's design system. This guide covers using the built-in theme provider, overriding styles with Material-UI theme options, and applying custom CSS.
|
|
10
10
|
</x-card>
|
|
11
11
|
<x-card data-title="Utilities" data-icon="lucide:wrench" data-href="/guides/utilities">
|
|
12
|
-
|
|
12
|
+
Discover a collection of powerful utility functions for handling API requests, caching data, formatting dates and amounts, and managing internationalization. This guide will help you write cleaner and more efficient code.
|
|
13
13
|
</x-card>
|
|
14
14
|
</x-cards>
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
After exploring these guides, you might be interested in learning about the custom React hooks provided by the library.
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
The library is built on Material-UI and exposes a flexible theming system. You can provide a custom theme configuration to adjust everything from colors and typography to the shape of buttons and inputs. For a comprehensive overview, see the full [Theming Guide](./guides-theming.md).
|
|
21
|
-
|
|
22
|
-
For example, you can change the primary button color by passing a `theme` object to a component. Note that components requiring session context must be wrapped in `PaymentProvider`.
|
|
23
|
-
|
|
24
|
-
```tsx
|
|
25
|
-
import { PaymentProvider, CheckoutForm } from '@blocklet/payment-react';
|
|
26
|
-
// useSessionContext is a hook from your app's session management setup.
|
|
27
|
-
import { useSessionContext } from '../hooks/session';
|
|
28
|
-
|
|
29
|
-
function MyCheckoutPage() {
|
|
30
|
-
const { session, connectApi } = useSessionContext();
|
|
31
|
-
|
|
32
|
-
// For a complete guide on setting up PaymentProvider and session context,
|
|
33
|
-
// please refer to the PaymentProvider documentation.
|
|
34
|
-
if (!session) {
|
|
35
|
-
return <div>Loading session...</div>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return (
|
|
39
|
-
<PaymentProvider session={session} connect={connectApi}>
|
|
40
|
-
<CheckoutForm
|
|
41
|
-
id="plink_xxx"
|
|
42
|
-
theme={{
|
|
43
|
-
components: {
|
|
44
|
-
MuiButton: {
|
|
45
|
-
styleOverrides: {
|
|
46
|
-
containedPrimary: {
|
|
47
|
-
backgroundColor: '#1DC1C7',
|
|
48
|
-
color: '#fff',
|
|
49
|
-
'&:hover': {
|
|
50
|
-
backgroundColor: 'rgb(20, 135, 139)',
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
}}
|
|
57
|
-
/>
|
|
58
|
-
</PaymentProvider>
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Simplifying Logic with Utilities
|
|
64
|
-
|
|
65
|
-
Beyond components, `@blocklet/payment-react` exports a suite of utility functions and classes to handle common tasks. The `CachedRequest` class, for instance, provides an easy way to cache API responses, reducing network load and improving performance. Discover more tools in our [Utilities Guide](./guides-utilities.md).
|
|
66
|
-
|
|
67
|
-
```tsx
|
|
68
|
-
import { CachedRequest, api } from '@blocklet/payment-react';
|
|
69
|
-
|
|
70
|
-
// Create a cached request that fetches prices and caches them for 5 minutes
|
|
71
|
-
const priceRequest = new CachedRequest(
|
|
72
|
-
'product-prices',
|
|
73
|
-
() => api.get('/api/prices'),
|
|
74
|
-
{
|
|
75
|
-
strategy: 'session', // 'session' | 'local' | 'memory'
|
|
76
|
-
ttl: 5 * 60 * 1000 // 5 minutes
|
|
77
|
-
}
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
async function fetchPrices() {
|
|
81
|
-
// Will use cache if available and not expired
|
|
82
|
-
const prices = await priceRequest.fetch();
|
|
83
|
-
|
|
84
|
-
// Force a refresh from the network
|
|
85
|
-
const freshPrices = await priceRequest.fetch(true);
|
|
86
|
-
|
|
87
|
-
return prices;
|
|
88
|
-
}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
---
|
|
92
|
-
|
|
93
|
-
## Next Steps
|
|
94
|
-
|
|
95
|
-
After mastering these advanced topics, proceed to the [Hooks](./hooks.md) documentation to learn about handling real-time events and responsive design.
|
|
18
|
+
➡️ **Next: [Hooks](./hooks.md)**
|
package/docs/hooks-use-mobile.md
CHANGED
|
@@ -1,70 +1,84 @@
|
|
|
1
1
|
# useMobile
|
|
2
2
|
|
|
3
|
-
The `useMobile` hook is a convenient utility for
|
|
3
|
+
The `useMobile` hook is a convenient utility for detecting if the application is being viewed on a mobile-sized device. It helps in creating responsive layouts and components directly within your React logic by leveraging Material-UI's breakpoint system.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## How It Works
|
|
6
|
+
|
|
7
|
+
Internally, `useMobile` uses Material-UI's `useTheme` and `useMediaQuery` hooks. It checks the current screen width against the theme's defined breakpoints to determine if the viewport qualifies as "mobile".
|
|
6
8
|
|
|
7
9
|
## Basic Usage
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
Here's a simple example of how to use the `useMobile` hook to conditionally render content.
|
|
10
12
|
|
|
11
|
-
```tsx
|
|
13
|
+
```tsx ResponsiveComponent.tsx icon=logos:react
|
|
12
14
|
import { useMobile } from '@blocklet/payment-react';
|
|
13
|
-
import Typography from '@mui/material
|
|
15
|
+
import { Typography, Box } from '@mui/material';
|
|
14
16
|
|
|
15
17
|
function ResponsiveComponent() {
|
|
16
|
-
const { isMobile } = useMobile();
|
|
18
|
+
const { isMobile, mobileSize } = useMobile();
|
|
17
19
|
|
|
18
20
|
return (
|
|
19
|
-
<
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
<Box p={2}>
|
|
22
|
+
<Typography variant="h5">
|
|
23
|
+
Responsive Content
|
|
24
|
+
</Typography>
|
|
25
|
+
{
|
|
26
|
+
isMobile ? (
|
|
27
|
+
<Typography>
|
|
28
|
+
This is the mobile view. The screen is {mobileSize} or smaller.
|
|
29
|
+
</Typography>
|
|
30
|
+
) : (
|
|
31
|
+
<Typography>
|
|
32
|
+
This is the desktop view. The screen is wider than {mobileSize}.
|
|
33
|
+
</Typography>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
</Box>
|
|
22
37
|
);
|
|
23
38
|
}
|
|
24
|
-
```
|
|
25
39
|
|
|
26
|
-
|
|
40
|
+
export default ResponsiveComponent;
|
|
41
|
+
```
|
|
27
42
|
|
|
28
43
|
## Parameters
|
|
29
44
|
|
|
30
|
-
The `useMobile` hook accepts an optional parameter to customize the breakpoint
|
|
31
|
-
|
|
32
|
-
| Name | Type | Default | Description |
|
|
33
|
-
|---------------|--------------------------------------------|---------|---------------------------------------------------------------------------------------------------------------|
|
|
34
|
-
| `mobilePoint` | `'xs'` \| `'sm'` \| `'md'` \| `'lg'` \| `'xl'` | `'md'` | The Material-UI breakpoint to use as the threshold. The hook will return `true` for screen sizes at or below this point. |
|
|
35
|
-
|
|
36
|
-
## Return Value
|
|
45
|
+
The `useMobile` hook accepts an optional parameter to customize the breakpoint.
|
|
37
46
|
|
|
38
|
-
|
|
47
|
+
| Name | Type | Description |
|
|
48
|
+
| :--- | :--- | :--- |
|
|
49
|
+
| `mobilePoint` | `'md' \| 'sm' \| 'lg' \| 'xl' \| 'xs'` | The breakpoint to consider as the mobile threshold. The `isMobile` flag will be true if the screen width is at or below this point. Defaults to `'md'`. |
|
|
39
50
|
|
|
40
|
-
|
|
41
|
-
|--------------|-----------|---------------------------------------------------------------------------------------------------------|
|
|
42
|
-
| `isMobile` | `boolean` | `true` if the screen width is at or below the specified `mobilePoint`, otherwise `false`. |
|
|
43
|
-
| `mobileSize` | `string` | A string representing the pixel width of the `mobilePoint` breakpoint (e.g., `'900px'` for the `'md'` breakpoint). |
|
|
51
|
+
### Customizing the Breakpoint
|
|
44
52
|
|
|
45
|
-
|
|
53
|
+
You can easily change the breakpoint by passing a different value.
|
|
46
54
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
```tsx
|
|
55
|
+
```tsx CustomBreakpoint.tsx icon=logos:react
|
|
50
56
|
import { useMobile } from '@blocklet/payment-react';
|
|
51
|
-
import
|
|
57
|
+
import { Typography } from '@mui/material';
|
|
52
58
|
|
|
53
59
|
function CustomBreakpointComponent() {
|
|
54
|
-
// Consider 'sm' and below as mobile
|
|
55
|
-
const { isMobile
|
|
60
|
+
// Consider 'sm' (small) screens and below as mobile
|
|
61
|
+
const { isMobile } = useMobile('sm');
|
|
56
62
|
|
|
57
63
|
return (
|
|
58
|
-
<
|
|
59
|
-
<p>The mobile breakpoint is set to {mobileSize}.</p>
|
|
64
|
+
<div>
|
|
60
65
|
{isMobile ? (
|
|
61
|
-
<
|
|
66
|
+
<Typography>Display for small screens</Typography>
|
|
62
67
|
) : (
|
|
63
|
-
<
|
|
68
|
+
<Typography>Display for medium and larger screens</Typography>
|
|
64
69
|
)}
|
|
65
|
-
</
|
|
70
|
+
</div>
|
|
66
71
|
);
|
|
67
72
|
}
|
|
73
|
+
|
|
74
|
+
export default CustomBreakpointComponent;
|
|
68
75
|
```
|
|
69
76
|
|
|
70
|
-
|
|
77
|
+
## Return Value
|
|
78
|
+
|
|
79
|
+
The hook returns an object with the following properties:
|
|
80
|
+
|
|
81
|
+
| Key | Type | Description |
|
|
82
|
+
| :--- | :--- | :--- |
|
|
83
|
+
| `isMobile` | `boolean` | `true` if the current viewport width is less than or equal to the specified `mobilePoint` breakpoint, otherwise `false`. |
|
|
84
|
+
| `mobileSize` | `string` | A string representing the pixel width of the `mobilePoint` breakpoint (e.g., `"900px"`). |
|