@dismissible/react-client 0.1.0 → 0.3.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/README.md +161 -1
- package/dist/components/Dismissible.d.ts +3 -0
- package/dist/contexts/DismissibleContext.d.ts +10 -0
- package/dist/contexts/DismissibleProvider.d.ts +30 -0
- package/dist/dismissible-client.es.js +483 -370
- package/dist/dismissible-client.umd.js +1 -1
- package/dist/root.d.ts +5 -1
- package/dist/types/dismissible.types.d.ts +36 -0
- package/dist/utils/auth.utils.d.ts +19 -0
- package/package.json +11 -7
- package/dist/utils/msw-toggle.d.ts +0 -13
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ A React component library for creating dismissible UI elements with persistent s
|
|
|
9
9
|
|
|
10
10
|
- 🎯 **Easy to use** - Simple component API for dismissible content
|
|
11
11
|
- 💾 **Persistent state** - Dismissal state is saved and restored across sessions
|
|
12
|
+
- 🔐 **JWT Authentication** - Built-in support for JWT-based user authentication
|
|
12
13
|
- 🎨 **Customizable** - Custom loading, error, and dismiss button components
|
|
13
14
|
- ♿ **Accessible** - Built with accessibility best practices
|
|
14
15
|
- 🪝 **Hook-based** - Includes `useDismissibleItem` hook for custom implementations
|
|
@@ -49,6 +50,51 @@ function App() {
|
|
|
49
50
|
|
|
50
51
|
## API Reference
|
|
51
52
|
|
|
53
|
+
### `<DismissibleProvider>` Component
|
|
54
|
+
|
|
55
|
+
Context provider for JWT authentication and configuration. Wrap your app or components that need JWT authentication.
|
|
56
|
+
|
|
57
|
+
#### Props
|
|
58
|
+
|
|
59
|
+
| Prop | Type | Required | Description |
|
|
60
|
+
|------|------|----------|-------------|
|
|
61
|
+
| `jwt` | `string \| (() => string)` | ❌ | JWT token (static string or function) |
|
|
62
|
+
| `baseUrl` | `string` | ❌ | Custom API base URL override |
|
|
63
|
+
| `children` | `ReactNode` | ✅ | Components that will use the dismissible functionality |
|
|
64
|
+
|
|
65
|
+
#### Example
|
|
66
|
+
|
|
67
|
+
```tsx
|
|
68
|
+
import { DismissibleProvider } from '@dismissible/react-client';
|
|
69
|
+
|
|
70
|
+
// With static JWT
|
|
71
|
+
function App() {
|
|
72
|
+
return (
|
|
73
|
+
<DismissibleProvider jwt="eyJhbGciOiJIUzI1NiIs...">
|
|
74
|
+
<YourApp />
|
|
75
|
+
</DismissibleProvider>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// With dynamic JWT function
|
|
80
|
+
function AppWithDynamicAuth() {
|
|
81
|
+
return (
|
|
82
|
+
<DismissibleProvider jwt={() => getAccessToken()}>
|
|
83
|
+
<YourApp />
|
|
84
|
+
</DismissibleProvider>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Without JWT (anonymous/backwards compatible)
|
|
89
|
+
function AppWithoutAuth() {
|
|
90
|
+
return (
|
|
91
|
+
<DismissibleProvider>
|
|
92
|
+
<YourApp />
|
|
93
|
+
</DismissibleProvider>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
52
98
|
### `<Dismissible>` Component
|
|
53
99
|
|
|
54
100
|
The main component for creating dismissible content.
|
|
@@ -131,6 +177,56 @@ function CustomDismissible({ id, children }) {
|
|
|
131
177
|
|
|
132
178
|
## Usage Examples
|
|
133
179
|
|
|
180
|
+
### JWT Authentication Setup
|
|
181
|
+
|
|
182
|
+
For enterprise accounts that require user-specific dismissible state, wrap your app with the `DismissibleProvider`:
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
import { DismissibleProvider, Dismissible } from '@dismissible/react-client';
|
|
186
|
+
|
|
187
|
+
function App() {
|
|
188
|
+
// Example with static JWT token
|
|
189
|
+
const jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...";
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<DismissibleProvider jwt={jwt}>
|
|
193
|
+
<Dashboard />
|
|
194
|
+
</DismissibleProvider>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function Dashboard() {
|
|
199
|
+
return (
|
|
200
|
+
<div>
|
|
201
|
+
{/* These dismissible items will be user-specific */}
|
|
202
|
+
<Dismissible id="user-welcome-banner">
|
|
203
|
+
<div className="alert alert-info">
|
|
204
|
+
<h4>Welcome back!</h4>
|
|
205
|
+
<p>You have 3 new notifications.</p>
|
|
206
|
+
</div>
|
|
207
|
+
</Dismissible>
|
|
208
|
+
</div>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Dynamic JWT with Authentication Provider
|
|
214
|
+
|
|
215
|
+
```tsx
|
|
216
|
+
import { DismissibleProvider } from '@dismissible/react-client';
|
|
217
|
+
import { useAuth } from './auth'; // Your auth context
|
|
218
|
+
|
|
219
|
+
function App() {
|
|
220
|
+
const { getAccessToken } = useAuth();
|
|
221
|
+
|
|
222
|
+
return (
|
|
223
|
+
<DismissibleProvider jwt={() => getAccessToken()}>
|
|
224
|
+
<YourApp />
|
|
225
|
+
</DismissibleProvider>
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
134
230
|
### Basic Dismissible Banner
|
|
135
231
|
|
|
136
232
|
```tsx
|
|
@@ -271,10 +367,49 @@ function ConditionalBanner({ user }) {
|
|
|
271
367
|
}
|
|
272
368
|
```
|
|
273
369
|
|
|
370
|
+
### User-Specific vs Anonymous Dismissible Items
|
|
371
|
+
|
|
372
|
+
The behavior changes based on whether JWT authentication is configured:
|
|
373
|
+
|
|
374
|
+
```tsx
|
|
375
|
+
import { DismissibleProvider, Dismissible } from '@dismissible/react-client';
|
|
376
|
+
|
|
377
|
+
// With JWT - dismissible state is user-specific
|
|
378
|
+
function AuthenticatedApp() {
|
|
379
|
+
return (
|
|
380
|
+
<DismissibleProvider jwt={() => getAccessToken()}>
|
|
381
|
+
<div>
|
|
382
|
+
{/* Each user will see this banner independently */}
|
|
383
|
+
<Dismissible id="feature-announcement">
|
|
384
|
+
<div>New feature available!</div>
|
|
385
|
+
</Dismissible>
|
|
386
|
+
|
|
387
|
+
{/* User A dismissing this won't affect User B */}
|
|
388
|
+
<Dismissible id="survey-request">
|
|
389
|
+
<div>Please take our survey!</div>
|
|
390
|
+
</Dismissible>
|
|
391
|
+
</div>
|
|
392
|
+
</DismissibleProvider>
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Without JWT - dismissible state is account-level (anonymous)
|
|
397
|
+
function AnonymousApp() {
|
|
398
|
+
return (
|
|
399
|
+
<div>
|
|
400
|
+
{/* These will be dismissed for all users of this account */}
|
|
401
|
+
<Dismissible id="general-announcement">
|
|
402
|
+
<div>Site maintenance scheduled</div>
|
|
403
|
+
</Dismissible>
|
|
404
|
+
</div>
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
```
|
|
408
|
+
|
|
274
409
|
### Using the Hook for Complex Logic
|
|
275
410
|
|
|
276
411
|
```tsx
|
|
277
|
-
import { useDismissibleItem } from '@dismissible/react-client';
|
|
412
|
+
import { useDismissibleItem, DismissibleProvider } from '@dismissible/react-client';
|
|
278
413
|
import { useState, useEffect } from 'react';
|
|
279
414
|
|
|
280
415
|
function SmartNotification({ id, message, type = 'info' }) {
|
|
@@ -310,6 +445,19 @@ function SmartNotification({ id, message, type = 'info' }) {
|
|
|
310
445
|
</div>
|
|
311
446
|
);
|
|
312
447
|
}
|
|
448
|
+
|
|
449
|
+
// Usage with authentication
|
|
450
|
+
function App() {
|
|
451
|
+
return (
|
|
452
|
+
<DismissibleProvider jwt={() => getUserToken()}>
|
|
453
|
+
<SmartNotification
|
|
454
|
+
id="user-specific-notification"
|
|
455
|
+
message="Welcome back!"
|
|
456
|
+
type="info"
|
|
457
|
+
/>
|
|
458
|
+
</DismissibleProvider>
|
|
459
|
+
);
|
|
460
|
+
}
|
|
313
461
|
```
|
|
314
462
|
|
|
315
463
|
## Styling
|
|
@@ -342,12 +490,24 @@ The library is written in TypeScript and exports all type definitions:
|
|
|
342
490
|
```tsx
|
|
343
491
|
import type {
|
|
344
492
|
DismissibleProps,
|
|
493
|
+
DismissibleProviderProps,
|
|
494
|
+
JwtToken,
|
|
345
495
|
IDismissibleItem
|
|
346
496
|
} from '@dismissible/react-client';
|
|
347
497
|
|
|
498
|
+
// Dismissible component with custom props
|
|
348
499
|
const MyComponent: React.FC<DismissibleProps> = (props) => {
|
|
349
500
|
// Your component implementation
|
|
350
501
|
};
|
|
502
|
+
|
|
503
|
+
// Provider with JWT authentication
|
|
504
|
+
const AuthProvider: React.FC<DismissibleProviderProps> = ({ children, jwt }) => {
|
|
505
|
+
return (
|
|
506
|
+
<DismissibleProvider jwt={jwt}>
|
|
507
|
+
{children}
|
|
508
|
+
</DismissibleProvider>
|
|
509
|
+
);
|
|
510
|
+
};
|
|
351
511
|
```
|
|
352
512
|
|
|
353
513
|
## Development
|
|
@@ -30,6 +30,8 @@ export interface DismissibleProps {
|
|
|
30
30
|
cachePrefix?: string;
|
|
31
31
|
/** Cache expiration time in milliseconds (default: never expires) */
|
|
32
32
|
cacheExpiration?: number;
|
|
33
|
+
/** Ignore errors and display the component anyway (default: false) */
|
|
34
|
+
ignoreErrors?: boolean;
|
|
33
35
|
}
|
|
34
36
|
/**
|
|
35
37
|
* A wrapper component that can be dismissed and hidden by users
|
|
@@ -44,6 +46,7 @@ export interface DismissibleProps {
|
|
|
44
46
|
* @param enableCache - Enable localStorage caching
|
|
45
47
|
* @param cachePrefix - Cache key prefix
|
|
46
48
|
* @param cacheExpiration - Cache expiration time in milliseconds
|
|
49
|
+
* @param ignoreErrors - Ignore errors and display the component anyway
|
|
47
50
|
* @returns JSX element or null if dismissed
|
|
48
51
|
*/
|
|
49
52
|
export declare const Dismissible: React.FC<DismissibleProps>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DismissibleContextValue } from '../types/dismissible.types';
|
|
2
|
+
/**
|
|
3
|
+
* React context for sharing dismissible authentication state
|
|
4
|
+
*/
|
|
5
|
+
export declare const DismissibleContext: import('react').Context<DismissibleContextValue | null>;
|
|
6
|
+
/**
|
|
7
|
+
* Hook to consume the DismissibleContext
|
|
8
|
+
* @returns The context value or null if not within a provider
|
|
9
|
+
*/
|
|
10
|
+
export declare const useDismissibleContext: () => DismissibleContextValue | null;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { DismissibleProviderProps } from '../types/dismissible.types';
|
|
3
|
+
/**
|
|
4
|
+
* Provider component for managing dismissible authentication state
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* // With static JWT
|
|
9
|
+
* <DismissibleProvider jwt="eyJhbGciOiJ..." baseUrl="https://api.dismissible.io">
|
|
10
|
+
* <App />
|
|
11
|
+
* </DismissibleProvider>
|
|
12
|
+
*
|
|
13
|
+
* // With synchronous JWT function
|
|
14
|
+
* <DismissibleProvider
|
|
15
|
+
* jwt={() => getAccessToken()}
|
|
16
|
+
* baseUrl="https://api.dismissible.io"
|
|
17
|
+
* >
|
|
18
|
+
* <App />
|
|
19
|
+
* </DismissibleProvider>
|
|
20
|
+
*
|
|
21
|
+
* // With asynchronous JWT function
|
|
22
|
+
* <DismissibleProvider
|
|
23
|
+
* jwt={async () => await fetchAccessToken()}
|
|
24
|
+
* baseUrl="https://api.dismissible.io"
|
|
25
|
+
* >
|
|
26
|
+
* <App />
|
|
27
|
+
* </DismissibleProvider>
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare const DismissibleProvider: React.FC<DismissibleProviderProps>;
|