react-kofi-overlay 0.1.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) 2023 Harrison Liddiard
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,91 @@
1
+ # Ko-fi overlay widget
2
+
3
+ [**Jump to usage**](#usage)
4
+
5
+ React component for accepting donations though Ko-fi without leaving your website:
6
+
7
+
8
+
9
+ ## Features
10
+
11
+ - Fully customize donate button style & content
12
+ - Accept donations in page context (no redirect)
13
+ - Customize donation panel size & placement
14
+ - Trigger a function when donation panel is open or closed
15
+
16
+ ## Motivation
17
+
18
+ To set up what Ko-fi calls their "[Floating Button Donation Widget](https://ko-fi.com/Manage/donation-widget-setup)," they let you do some basic customization and ask you to copy and paste two `<script>` tags onto your site:
19
+
20
+ ![Ko-fi donation widget setup](img/kofi_donation_widget_setup.png)
21
+
22
+ This has some limitations:
23
+
24
+ - It only allows you to choose from a handful of button calls-to-action, which may not meet your needs.
25
+ - It provides a limited selection of background color choices, which may not match your website theme.
26
+ - It requires you to load a `<script>` tag from their CDN, which creates a global variable, and you then need to call a method on that global to render the donate button. Few potential issues with that:
27
+ - Requires an extra network request on every page load.
28
+ - Doesn't work well with modern JS bundling + import/export systems like Webpack.
29
+ - Doesn't work well with a React component-based frontend.
30
+ - Doesn't play nice with web frameworks like Next.js. Next.js makes it difficult to inject an arbitrary script like `kofiWidgetOverlay.draw(...)` – and to only run that code after the external `overlay-widget.js` script is loaded.
31
+
32
+ This component is designed to address those limitations.
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ npm install react-kofi-overlay
38
+ ```
39
+
40
+ (or equivalent on your package manager)
41
+
42
+ ## Usage
43
+
44
+ ### Example
45
+
46
+ ```jsx
47
+ import { Donate } from 'react-kofi-overlay'
48
+
49
+ <Donate
50
+ username="liddiard"
51
+ classNames={{
52
+ donateBtn: 'myDonateButton',
53
+ profileLink: 'myProfileLink'
54
+ }}
55
+ styles={{
56
+ panel: { marginRight: '4em' }
57
+ }}
58
+ onToggle={(open) => {
59
+ console.log(`Donate panel ${open ? 'opened' : 'closed'}`)
60
+ }}
61
+ >
62
+ ❤️ Support Me
63
+ </Donate>
64
+ ```
65
+
66
+ ### Props
67
+
68
+ | Prop name | Required | Type | Description |
69
+ | ------------- | ------------- | ------------- | ------------- |
70
+ | `username` | required | string | Ko-fi profile to display in the donation panel. Corresponds to the Ko-fi page at ko-fi.com/<username> |
71
+ | (children) | required | ReactNode | Contents of the donation button. Can be plain text or any React nodes that are valid children of a `<button>` element. |
72
+ | `classNames` | optional | object | Class names to apply to different parts of the component for styling. See [Styling](#styling) below for more info. |
73
+ | `styles` | optional | object | Inline styles to apply to different parts of the component for styling. See [Styling](#styling) below for more info. |
74
+ | `onToggle` | optional | function | Function to call when the donate panel is opened or closed. It receives one argument: a boolean `open` value that indicates if the panel was opened or closed. |
75
+
76
+ ### Styling
77
+
78
+ The component accepts `classNames` or `styles` props to customize the appearance of the donate button and various parts of the donate panel. Both props should be an object with one or more of the keys below.
79
+
80
+ Note: While you could provide both `classNames` and `styles`, it isn't recommended because they will override one another. Pick one based on how you're writing CSS in your codebase.
81
+
82
+ | Name | Description |
83
+ | ------------- | ------------- |
84
+ | `donateBtn` | Donate button – a <button> element. |
85
+ | `panel` | Panel wrapper that popus up when donate button is clicked. By default, the panel is anchored to the bottom right of the viewport on desktop and full screen on mobile. |
86
+ | `closeBtn` | Button to close the donate panel. |
87
+ | `closeIcon` | (X) icon within the panel close button. |
88
+ | `profileLink` | Link to your Ko-fi profile. By default, it's anchored to the bottom of the donate panel. |
89
+
90
+
91
+ ## Development
@@ -0,0 +1,56 @@
1
+ import React, { CSSProperties } from 'react';
2
+ export interface StyleDefs<T> {
3
+ /**
4
+ * Popup wrapper. By default, the popup is anchored to the bottom right of
5
+ * the viewport on desktop and full screen on mobile.
6
+ */
7
+ popup?: T;
8
+ /**
9
+ * Donate button.
10
+ */
11
+ donateBtn?: T;
12
+ /**
13
+ * Button to close the donate popup.
14
+ */
15
+ closeBtn?: T;
16
+ /**
17
+ * (X) icon within the popup close button.
18
+ */
19
+ closeIcon?: T;
20
+ /**
21
+ * Link to your Ko-fi profile. By default, it's anchored to the bottom of the donate popup.
22
+ */
23
+ profileLink?: T;
24
+ }
25
+ export interface DonateProps {
26
+ /**
27
+ * Child nodes of the donate button. E.g., "Support me on Ko-fi"
28
+ */
29
+ children: React.ReactNode;
30
+ /**
31
+ * Your Ko-fi profile name. (ko-fi.com/[username])
32
+ */
33
+ username: string;
34
+ /**
35
+ * Optional custom class names to apply to the component.
36
+ */
37
+ classNames?: StyleDefs<string>;
38
+ /**
39
+ * Optional custom styles to apply to the component.
40
+ */
41
+ styles?: StyleDefs<CSSProperties>;
42
+ /**
43
+ * Optional function to be called when the donation form is opened or closed.
44
+ */
45
+ onToggle?: (open: boolean) => void;
46
+ }
47
+ /**
48
+ * Component to display a button that opens a Ko-fi donation popup when clicked.
49
+ *
50
+ * @component
51
+ * @example
52
+ * <Donate username="liddiard">
53
+ * Support My Work
54
+ * </Donate>
55
+ */
56
+ export default function Donate({ children, username, classNames, styles, onToggle }: DonateProps): React.JSX.Element;
package/dist/Donate.js ADDED
@@ -0,0 +1,70 @@
1
+ 'use client';
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __importDefault = (this && this.__importDefault) || function (mod) {
27
+ return (mod && mod.__esModule) ? mod : { "default": mod };
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ var react_1 = __importStar(require("react"));
31
+ var classnames_1 = __importDefault(require("classnames"));
32
+ var Donate_module_scss_1 = __importDefault(require("./Donate.module.scss"));
33
+ var react_icons_1 = require("react-icons");
34
+ var bs_1 = require("react-icons/bs");
35
+ /**
36
+ * Component to display a button that opens a Ko-fi donation popup when clicked.
37
+ *
38
+ * @component
39
+ * @example
40
+ * <Donate username="liddiard">
41
+ * Support My Work
42
+ * </Donate>
43
+ */
44
+ function Donate(_a) {
45
+ var children = _a.children, username = _a.username, _b = _a.classNames, classNames = _b === void 0 ? {} : _b, _c = _a.styles, styles = _c === void 0 ? {} : _c, _d = _a.onToggle, onToggle = _d === void 0 ? function () { } : _d;
46
+ var baseUrl = "https://ko-fi.com/".concat(username);
47
+ var _e = (0, react_1.useState)(false), open = _e[0], setOpen = _e[1];
48
+ var handleOpen = function (isOpen) {
49
+ setOpen(isOpen);
50
+ onToggle(isOpen);
51
+ };
52
+ return (react_1.default.createElement(react_1.default.Fragment, null,
53
+ react_1.default.createElement("button", { className: (0, classnames_1.default)(Donate_module_scss_1.default.donateBtn, classNames.donateBtn), style: styles.donateBtn, onClick: function () { return handleOpen(true); } }, children),
54
+ open ?
55
+ react_1.default.createElement("div", { className: (0, classnames_1.default)(Donate_module_scss_1.default.popup, classNames.popup), style: styles.popup },
56
+ react_1.default.createElement("button", { className: (0, classnames_1.default)(Donate_module_scss_1.default.closeBtn, classNames.closeBtn), style: styles.closeBtn, onClick: function () { return handleOpen(false); } },
57
+ react_1.default.createElement(react_icons_1.IconContext.Provider, { value: {
58
+ className: (0, classnames_1.default)(Donate_module_scss_1.default.closeIcon, classNames.closeIcon),
59
+ style: styles.closeIcon
60
+ } },
61
+ react_1.default.createElement(bs_1.BsX, { title: "Close" }))),
62
+ react_1.default.createElement("iframe", { src: "".concat(baseUrl, "?hidefeed=true&widget=true&embed=true") }),
63
+ react_1.default.createElement("div", { className: (0, classnames_1.default)(Donate_module_scss_1.default.profileLink, classNames.profileLink), style: styles.profileLink },
64
+ react_1.default.createElement("a", { href: baseUrl, target: "_blank", rel: "noreferrer" },
65
+ "ko-fi.com/",
66
+ username)))
67
+ : null));
68
+ }
69
+ exports.default = Donate;
70
+ //# sourceMappingURL=Donate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Donate.js","sourceRoot":"","sources":["../src/Donate.tsx"],"names":[],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