@flung.dev/connect 0.1.1 → 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 +222 -12
- package/dist/core/client.d.ts +9 -2
- package/dist/core/client.js +29 -10
- package/dist/core/controller.d.ts +214 -0
- package/dist/core/controller.js +407 -0
- package/dist/core/events.d.ts +25 -0
- package/dist/core/events.js +1 -0
- package/dist/core/index.d.ts +10 -4
- package/dist/core/index.js +6 -3
- package/dist/core/labels.d.ts +78 -0
- package/dist/core/labels.js +41 -0
- package/dist/core/platforms.d.ts +7 -1
- package/dist/core/platforms.js +11 -1
- package/dist/core/types.d.ts +20 -0
- package/dist/core/types.js +1 -0
- package/dist/element/copy.d.ts +3 -33
- package/dist/element/copy.js +2 -29
- package/dist/element/dom.d.ts +25 -5
- package/dist/element/dom.js +49 -5
- package/dist/element/events.d.ts +14 -18
- package/dist/element/flung-connect.d.ts +21 -1
- package/dist/element/flung-connect.js +221 -190
- package/dist/element/marks.d.ts +17 -0
- package/dist/element/marks.js +35 -0
- package/dist/element/styles.d.ts +6 -4
- package/dist/element/styles.js +195 -52
- package/dist/element/tooltip.d.ts +26 -0
- package/dist/element/tooltip.js +135 -0
- package/dist/index.d.ts +2 -4
- package/dist/index.js +0 -1
- package/dist/react/index.d.ts +53 -3
- package/dist/react/index.js +40 -2
- package/dist/react/use-flung-connect.d.ts +13 -0
- package/dist/react/use-flung-connect.js +65 -0
- package/package.json +1 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
|
|
2
|
+
import { createConnectController, initialConnectState } from '../core/controller.js';
|
|
3
|
+
const noSubscribe = () => () => undefined;
|
|
4
|
+
/**
|
|
5
|
+
* The connect panel with none of Flung's markup, for drawing every piece with your own components and CSS. It runs a
|
|
6
|
+
* `createConnectController` for as long as the component is mounted, and a new one when the token string, `baseUrl`
|
|
7
|
+
* or whether there is a `fetch` changes. A token function, `fetch` and the callbacks are read fresh on every call, so
|
|
8
|
+
* they need no `useCallback`. Call `connect`, `submitHandle` and `reconnect` straight from a click or submit handler: a
|
|
9
|
+
* browser opens a window only during one.
|
|
10
|
+
*/
|
|
11
|
+
export function useFlungConnect(options) {
|
|
12
|
+
const latest = useRef(options);
|
|
13
|
+
latest.current = options;
|
|
14
|
+
const current = useRef(null);
|
|
15
|
+
const [controller, setController] = useState(null);
|
|
16
|
+
const { token, baseUrl } = options;
|
|
17
|
+
const tokenKey = token === null ? null : typeof token === 'string' ? `token:${token}` : 'function';
|
|
18
|
+
const hasFetch = options.fetch !== undefined;
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
const source = latest.current.token;
|
|
21
|
+
const made = createConnectController({
|
|
22
|
+
token: typeof source === 'function'
|
|
23
|
+
? () => {
|
|
24
|
+
const now = latest.current.token;
|
|
25
|
+
return typeof now === 'function' ? now() : Promise.resolve(now ?? '');
|
|
26
|
+
}
|
|
27
|
+
: source,
|
|
28
|
+
baseUrl,
|
|
29
|
+
platforms: latest.current.platforms,
|
|
30
|
+
mode: latest.current.mode,
|
|
31
|
+
labels: latest.current.labels,
|
|
32
|
+
readRedirect: latest.current.readRedirect,
|
|
33
|
+
fetch: hasFetch ? (input, init) => (latest.current.fetch ?? fetch)(input, init) : undefined,
|
|
34
|
+
onLoad: (detail) => latest.current.onLoad?.(detail),
|
|
35
|
+
onConnect: (detail) => latest.current.onConnect?.(detail),
|
|
36
|
+
onDisconnect: (detail) => latest.current.onDisconnect?.(detail),
|
|
37
|
+
onError: (detail) => latest.current.onError?.(detail),
|
|
38
|
+
});
|
|
39
|
+
current.current = made;
|
|
40
|
+
setController(made);
|
|
41
|
+
return () => {
|
|
42
|
+
made.destroy();
|
|
43
|
+
if (current.current === made)
|
|
44
|
+
current.current = null;
|
|
45
|
+
};
|
|
46
|
+
}, [tokenKey, baseUrl, hasFetch]);
|
|
47
|
+
const platformsKey = options.platforms?.join(',') ?? '';
|
|
48
|
+
const labelsKey = JSON.stringify(options.labels ?? {});
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
controller?.configure({ platforms: latest.current.platforms ?? null, mode: latest.current.mode ?? 'popup', labels: latest.current.labels });
|
|
51
|
+
}, [controller, platformsKey, options.mode, labelsKey]);
|
|
52
|
+
const initial = initialConnectState(token !== null);
|
|
53
|
+
const state = useSyncExternalStore(controller?.subscribe ?? noSubscribe, controller?.getState ?? (() => initial), () => initial);
|
|
54
|
+
const actions = useMemo(() => ({
|
|
55
|
+
connect: (platform, options) => current.current?.connect(platform, options),
|
|
56
|
+
submitHandle: (platform, handle) => current.current?.submitHandle(platform, handle),
|
|
57
|
+
reconnect: (accountId) => current.current?.reconnect(accountId),
|
|
58
|
+
requestDisconnect: (accountId) => current.current?.requestDisconnect(accountId),
|
|
59
|
+
confirmDisconnect: (platform) => current.current?.confirmDisconnect(platform) ?? Promise.resolve(),
|
|
60
|
+
cancel: (platform) => current.current?.cancel(platform),
|
|
61
|
+
dismissNotice: () => current.current?.dismissNotice(),
|
|
62
|
+
refresh: () => current.current?.refresh() ?? Promise.resolve(),
|
|
63
|
+
}), []);
|
|
64
|
+
return { ...state, ...actions };
|
|
65
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flung.dev/connect",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Let people connect and disconnect their social accounts from your page: a <flung-connect> web component and a React component for Flung, with no runtime dependencies.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|