# @microbitsclub/paywall-react

> React wrapper for Microbits paywalls

Latest version **0.3.0** (published 2022-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @microbitsclub/paywall-react
pnpm add @microbitsclub/paywall-react
yarn add @microbitsclub/paywall-react
bun add @microbitsclub/paywall-react
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2022-09-28 |
| First published | 2022-08-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 47.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Microbits |
| Maintainers | nateful |
| Keywords | React, Microbits, Paywall |

## Links

- npm: https://www.npmjs.com/package/@microbitsclub/paywall-react
- Repository: https://github.com/microbitsclub/paywall-react
- Issues: https://github.com/microbitsclub/paywall-react/issues
- npm.io page: https://npm.io/package/@microbitsclub/paywall-react

## Dependencies (4)

- [js-cookie](https://npm.io/package/js-cookie.md) ^3.0.1
- [cross-fetch](https://npm.io/package/cross-fetch.md) ^3.1.5
- [react-solid-state](https://npm.io/package/react-solid-state.md) ^0.27.3
- [@microbitsclub/microbits-client](https://npm.io/package/@microbitsclub/microbits-client.md) ^0.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2022-09-28
- 0.2.0 — 2022-08-23
- 0.0.1 — 2022-08-23

## README

# Microbits Paywall for React

Renders a popup paywall for paywalled microbits content.

## Installation

```
npm install @microbitsclub/paywall-react
```

## Configuration

[Get your merchant ID](https://microbits.club/a/signin?redirect=https%3A%2F%2Fmicrobits.club%2Fa%2Fdevelopers).

## Usage

Create a `PaywallPopoverConfig` instance. 

```typescript
import { stringifyUrl } from '@microbitsclub/paywall-react/dist/src/url';

const paywallPopoverConfig: PaywallPopoverConfig = {
    merchantId: '<your_merchant_id>',

    // Compute the final URL for paywalled content.
    getContentUrl(path, query) {
        return stringifyUrl({
            proto: 'http',
            hostname: 'localhost',
            port: 3000,
            ...path,
            query,
        });
    },
    
    // Compute the final URL for requests to your server. These requests are
    // handled by @microbitsclub/microbits-client request handlers.
    getServerUrl: (path, query) => {
        return stringifyUrl({
            proto: 'http',
            hostname: 'localhost',
            port: 7000,
            ...path,
            query,
        });
    },
};
```

Wrap your React components with `PaywallPopoverProvider` wherever a popover paywall should be displayed.

```typescript
const App: FC<AppProps> = props => {
    return (
        <PaywallPopoverProvider config={paywallPopoverConfig}>
            <StoryList>
                {props.stories.map(({ id, title }) => (
                    <StoryCard key={id} id={id} title={title} />
                ))}
            </StoryList>
        </PaywallPopoverProvider>
    );
}
```

Intercept link clicks and use the `attempNavigateToContent` hook API.

```typescript
const StoryCard: FC<StoryCardProps> = props => {
    const contentUrl = `/story/${props.id}`;
    const popoverPaywall = usePaywallPopover();

    const handleClick = useCallback<MouseEventHandler>(
        e => {
            // Prevent the default action
            e.preventDefault();

            // Handle the attempted navigation
            popoverPaywall.attempNavigateToContent(contentUrl);
        },
        [contentUrl, popoverPaywall]
    );

    return (
        <StoryLinkBox>
            <a href={contentUrl} onClick={handleClick}>{props.title}</a>
        </StoryLinkBox>
    );
}
```

---
_Source: https://npm.io/package/@microbitsclub/paywall-react · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
