@ailuracode/alpine-share 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) ailuracode
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # @ailuracode/alpine-share
2
+
3
+ Web Share API magic for Alpine.js.
4
+
5
+ **[Full documentation →](../../docs/share.md)**
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @ailuracode/alpine-share alpinejs
11
+ ```
12
+
13
+ ## Quick example
14
+
15
+ ```js
16
+ import Alpine from "alpinejs";
17
+ import share from "@ailuracode/alpine-share";
18
+
19
+ Alpine.plugin(share);
20
+ Alpine.start();
21
+ ```
22
+
23
+ ```html
24
+ <button
25
+ x-show="$share.isSupported()"
26
+ @click="await $share({ title: 'Hello', url: window.location.href })"
27
+ >
28
+ Share page
29
+ </button>
30
+ ```
31
+
32
+ ## API summary
33
+
34
+ | | |
35
+ |-|-|
36
+ | **Magic** | `$share(data)` |
37
+ | **Helpers** | `$share.isSupported()`, `$share.canShare(data?)` |
38
+
39
+ Callable like `$clipboard`.
40
+
41
+ ## License
42
+
43
+ MIT
@@ -0,0 +1,11 @@
1
+ /// <reference types="@types/alpinejs" />
2
+
3
+ export type { ShareMagic } from "./index.js";
4
+
5
+ declare global {
6
+ namespace Alpine {
7
+ interface Magics<T> {
8
+ $share: import("./index.js").ShareMagic;
9
+ }
10
+ }
11
+ }
@@ -0,0 +1,25 @@
1
+ import AlpineType from 'alpinejs';
2
+
3
+ type ShareMagic = ((data: ShareData) => Promise<boolean>) & {
4
+ isSupported(): boolean;
5
+ canShare(data?: ShareData): boolean;
6
+ };
7
+ /** Returns whether the Web Share API is available in this environment. */
8
+ declare function isShareSupported(): boolean;
9
+ /** Returns whether the given payload can be shared, or whether sharing is available when omitted. */
10
+ declare function canShareData(data?: ShareData): boolean;
11
+ /** Invokes `navigator.share` and resolves to `true` on success. Never throws. */
12
+ declare function shareData(data: ShareData): Promise<boolean>;
13
+ /** Builds callable `$share` magic with `isSupported` and `canShare` helpers. */
14
+ declare function createShareMagic(): ShareMagic;
15
+ /** Alpine.js share plugin. Registers callable magic `$share`. */
16
+ declare function sharePlugin(Alpine: AlpineType.Alpine): void;
17
+ declare global {
18
+ namespace Alpine {
19
+ interface Magics<T> {
20
+ $share: ShareMagic;
21
+ }
22
+ }
23
+ }
24
+
25
+ export { type ShareMagic, canShareData, createShareMagic, sharePlugin as default, isShareSupported, shareData };
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ // src/index.ts
2
+ function isSecureContext() {
3
+ return typeof globalThis !== "undefined" && globalThis.isSecureContext === true;
4
+ }
5
+ function hasShareApi() {
6
+ return typeof navigator !== "undefined" && typeof navigator.share === "function";
7
+ }
8
+ function isShareSupported() {
9
+ return isSecureContext() && hasShareApi();
10
+ }
11
+ function canShareData(data) {
12
+ if (!isShareSupported()) {
13
+ return false;
14
+ }
15
+ if (data === void 0) {
16
+ return true;
17
+ }
18
+ if (typeof navigator.canShare === "function") {
19
+ try {
20
+ return navigator.canShare(data);
21
+ } catch {
22
+ return false;
23
+ }
24
+ }
25
+ if (data.files?.length) {
26
+ return false;
27
+ }
28
+ return Boolean(data.title || data.text || data.url);
29
+ }
30
+ async function shareData(data) {
31
+ if (!canShareData(data)) {
32
+ return false;
33
+ }
34
+ try {
35
+ await navigator.share(data);
36
+ return true;
37
+ } catch {
38
+ return false;
39
+ }
40
+ }
41
+ function createShareMagic() {
42
+ const share = (data) => shareData(data);
43
+ share.isSupported = isShareSupported;
44
+ share.canShare = canShareData;
45
+ return share;
46
+ }
47
+ function sharePlugin(Alpine) {
48
+ Alpine.magic("share", () => createShareMagic());
49
+ }
50
+ export {
51
+ canShareData,
52
+ createShareMagic,
53
+ sharePlugin as default,
54
+ isShareSupported,
55
+ shareData
56
+ };
57
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type AlpineType from \"alpinejs\";\n\nexport type ShareMagic = ((data: ShareData) => Promise<boolean>) & {\n isSupported(): boolean;\n canShare(data?: ShareData): boolean;\n};\n\nfunction isSecureContext(): boolean {\n return typeof globalThis !== \"undefined\" && globalThis.isSecureContext === true;\n}\n\nfunction hasShareApi(): boolean {\n return typeof navigator !== \"undefined\" && typeof navigator.share === \"function\";\n}\n\n/** Returns whether the Web Share API is available in this environment. */\nexport function isShareSupported(): boolean {\n return isSecureContext() && hasShareApi();\n}\n\n/** Returns whether the given payload can be shared, or whether sharing is available when omitted. */\nexport function canShareData(data?: ShareData): boolean {\n if (!isShareSupported()) {\n return false;\n }\n\n if (data === undefined) {\n return true;\n }\n\n if (typeof navigator.canShare === \"function\") {\n try {\n return navigator.canShare(data);\n } catch {\n return false;\n }\n }\n\n if (data.files?.length) {\n return false;\n }\n\n return Boolean(data.title || data.text || data.url);\n}\n\n/** Invokes `navigator.share` and resolves to `true` on success. Never throws. */\nexport async function shareData(data: ShareData): Promise<boolean> {\n if (!canShareData(data)) {\n return false;\n }\n\n try {\n await navigator.share(data);\n return true;\n } catch {\n return false;\n }\n}\n\n/** Builds callable `$share` magic with `isSupported` and `canShare` helpers. */\nexport function createShareMagic(): ShareMagic {\n const share = (data: ShareData) => shareData(data);\n share.isSupported = isShareSupported;\n share.canShare = canShareData;\n return share;\n}\n\n/** Alpine.js share plugin. Registers callable magic `$share`. */\nexport default function sharePlugin(Alpine: AlpineType.Alpine): void {\n Alpine.magic(\"share\", () => createShareMagic());\n}\n\ndeclare global {\n namespace Alpine {\n interface Magics<T> {\n $share: ShareMagic;\n }\n }\n}\n"],"mappings":";AAOA,SAAS,kBAA2B;AAClC,SAAO,OAAO,eAAe,eAAe,WAAW,oBAAoB;AAC7E;AAEA,SAAS,cAAuB;AAC9B,SAAO,OAAO,cAAc,eAAe,OAAO,UAAU,UAAU;AACxE;AAGO,SAAS,mBAA4B;AAC1C,SAAO,gBAAgB,KAAK,YAAY;AAC1C;AAGO,SAAS,aAAa,MAA2B;AACtD,MAAI,CAAC,iBAAiB,GAAG;AACvB,WAAO;AAAA,EACT;AAEA,MAAI,SAAS,QAAW;AACtB,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,UAAU,aAAa,YAAY;AAC5C,QAAI;AACF,aAAO,UAAU,SAAS,IAAI;AAAA,IAChC,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AAEA,MAAI,KAAK,OAAO,QAAQ;AACtB,WAAO;AAAA,EACT;AAEA,SAAO,QAAQ,KAAK,SAAS,KAAK,QAAQ,KAAK,GAAG;AACpD;AAGA,eAAsB,UAAU,MAAmC;AACjE,MAAI,CAAC,aAAa,IAAI,GAAG;AACvB,WAAO;AAAA,EACT;AAEA,MAAI;AACF,UAAM,UAAU,MAAM,IAAI;AAC1B,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAGO,SAAS,mBAA+B;AAC7C,QAAM,QAAQ,CAAC,SAAoB,UAAU,IAAI;AACjD,QAAM,cAAc;AACpB,QAAM,WAAW;AACjB,SAAO;AACT;AAGe,SAAR,YAA6B,QAAiC;AACnE,SAAO,MAAM,SAAS,MAAM,iBAAiB,CAAC;AAChD;","names":[]}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@ailuracode/alpine-share",
3
+ "version": "0.2.0",
4
+ "description": "Alpine.js Web Share API magic",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "ailuracode",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/ailuracode/alpine.git",
14
+ "directory": "packages/share"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/ailuracode/alpine/issues"
18
+ },
19
+ "homepage": "https://github.com/ailuracode/alpine/tree/master/packages/share#readme",
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./global": {
30
+ "types": "./dist/global.d.ts"
31
+ }
32
+ },
33
+ "types": "./dist/global.d.ts",
34
+ "peerDependencies": {
35
+ "alpinejs": "^3.0.0",
36
+ "@types/alpinejs": "^3.13.11"
37
+ },
38
+ "peerDependenciesMeta": {
39
+ "@types/alpinejs": {
40
+ "optional": true
41
+ }
42
+ },
43
+ "keywords": [
44
+ "alpinejs",
45
+ "alpine",
46
+ "plugin",
47
+ "share",
48
+ "web-share-api"
49
+ ],
50
+ "scripts": {
51
+ "build": "tsup src/index.ts --format esm --dts --clean --sourcemap --out-dir dist && cp src/global.d.ts dist/global.d.ts",
52
+ "test": "vitest run --config ../../vitest.config.js test"
53
+ }
54
+ }