# nygardk/react-share architecture

> Architecture diagram of the GitHub repository nygardk/react-share, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nygardk/react-share
- Repository: https://github.com/nygardk/react-share

## Overview

This repo is a **frontend React component library** published to **npm** as `react-share`. Its purpose is to provide:
- **Share Button** React components (open a share dialog/popup or invoke `mailto:`-style flows)
- **Share Count** React components (fetch counts from social network endpoints)
- A **demo app** under `./demo` to showcase usage
- CI/release automation (GitHub Actions + Changesets)

##### Core library surface area (what consumers import)
- Many “leaf” components:
`src/*ShareButton.ts` (e.g., `FacebookShareButton.ts`)
`src/*ShareCount.ts` (e.g., `RedditShareCount.ts`)

##### Internal reusable building blocks (the “real architecture”)
- `src/ShareButton.tsx` – common base behavior for buttons (click handling, window open, disabled, etc.)
- `src/hocs/`
- `createShareButton.tsx` – factory/HOC generating network-specific share button components
- `createShareCount.tsx` – factory/HOC generating network-specific share count components
- `src/hooks/useIsMounted.ts` – used to avoid state updates after unmount (relevant for async share count fetching)
- `src/utils/`
- `objectToGetParams.ts` – encodes query parameters for share URLs
- `assert.ts` – internal invariants / runtime checks

##### Demo application (consumer reference)
- `demo/` with `Demo.tsx`, `index.tsx`, `index.html`, `Demo.css` – a Vite-driven demo that imports from the library code.

##### Build / tooling / release
- `vite.config.js`, `vite.demo.config.js` – bundling for library and demo
- `tsconfig.json`, `tsconfig.demo.json` – TypeScript compilation
- `.github/workflows/ci.yml`, `.github/workflows/release.yml` – CI + publishing automation
- `.changeset/` – Changesets-based versioning + changelog workflow
- `package.json`, `package-lock.json` – dependencies, scripts, build entrypoints

At runtime, `react-share` runs **inside the consumer’s browser**. There is no server component in this repo.

There are two main runtime flows:

##### User clicks `<XShareButton url="…">…</XShareButton>`
2. Button logic (from `ShareButton.tsx` + `createShareButton.tsx`) builds a **network-specific share URL** (via `objectToGetParams`)
##### Optional callbacks
- `beforeOnClick` (possibly async)
- `onShareWindowClose`

Key point to show: **the share action is purely URL-based** (README emphasizes “no external script loading / no SDKs”).

##### `<RedditShareCount url="…"/>` mounts
2. `createShareCount.tsx` triggers an async request to some network endpoint (varies by platform and may be proxied/deprecated depending on network)
##### `useIsMounted` prevents setting state after unmount

**Left**: Consumer App (React) + End User
**Center**: `react-share` package boundary containing:
- `index.ts` (public API)
- Share Buttons (generated via `createShareButton` + `ShareButton.tsx`)
- Share Counts (generated via `createShareCount` + `useIsMounted`)
- Utils (`objectToGetParams`, `assert`)
**Right**: External Social Platforms (Share endpoints + Count endpoints)
**Bottom/Side**: CI/Release lane (GitHub Actions + Changesets → npm)

That single view will capture: distribution, integration, internal design, and third-party interactions.

## Diagram (Mermaid)

```mermaid
flowchart TD

%% =========================
%% Context / Actors (Top)
%% =========================
EU["End User"]:::actor
DEV["Developer"]:::actor

subgraph "Consumer Environment (Browser)"
direction TB
APP["Consumer React App"]:::frontend
BROWSER["Browser Runtime"]:::browser
POPUP["Popup/Share Window"]:::browser
end

EU -->|"uses"| APP
APP -->|"runs_in"| BROWSER

%% =========================
%% npm Distribution (Build/Install)
%% =========================
NPM[("npm Registry")]:::external

DEV -.->|"npm_install react-share"| NPM
NPM -.->|"package_dependency"| APP

%% =========================
%% Repo: react-share (Library + Demo)
%% =========================
subgraph "react-share Repository"
direction TB

  %% --------
  %% Library
  %% --------
  subgraph "Library Package (src/)"
  direction TB

    INDEX["Public API (index.ts)"]:::code

    subgraph "Factories / HOCs (src/hocs/)"
    direction TB
      CSB["createShareButton (HOC)"]:::code
      CSC["createShareCount (HOC)"]:::code
      CIC["createIcon (HOC)"]:::code
    end

    BASEBTN["ShareButton Base (ShareButton.tsx)"]:::code

    subgraph "Leaf Components (network-specific)"
    direction TB
      LEAFBTN["*ShareButton.ts (e.g., X/Facebook/Email)"]:::code
      LEAFCOUNT["*ShareCount.ts (e.g., Reddit/VK)"]:::code
      LEAFICON["*Icon.ts (e.g., X/Bluesky)"]:::code
    end

    subgraph "Hooks (src/hooks/)"
    direction TB
      MOUNT["useIsMounted"]:::code
    end

    subgraph "Utilities (src/utils/)"
    direction TB
      GETPARAMS["objectToGetParams"]:::code
      ASSERT["assert"]:::code
    end
  end

  %% --------
  %% Demo
  %% --------
  subgraph "Demo App (demo/)"
  direction TB
    DEMOHTML["index.html"]:::demo
    DEMOENTRY["index.tsx"]:::demo
    DEMOCOMP["Demo.tsx"]:::demo
    DEMOCSS["Demo.css"]:::demo
    DEMODECL["declarations.d.ts"]:::demo
  end

  %% --------
  %% Tooling
  %% --------
  subgraph "Build Tooling"
  direction TB
    VITELIB["Vite (library config)"]:::tooling
    VITEDEMO["Vite (demo config)"]:::tooling
    TSLIB["TypeScript (library)"]:::tooling
    TSDEMO["TypeScript (demo)"]:::tooling
    PKG["package.json"]:::tooling
    LOCK["package-lock.json"]:::tooling
  end

  %% --------
  %% CI/Release
  %% --------
  subgraph "CI / Release Automation"
  direction TB
    CI["GitHub Actions CI (ci.yml)"]:::pipeline
    REL["GitHub Actions Release (release.yml)"]:::pipeline
    NPMINSTALL["Reusable Action: npm-install"]:::pipeline
    CHANGESETS["Changesets (.changeset + config)"]:::pipeline
  end
end

%% =========================
%% External Social Networks
%% =========================
subgraph "External Social Platforms (Third-Party)"
direction TB
  SHAREEP[("Share Endpoints (N networks)\nURL-based share dialogs")]:::external
  COUNTEP[("Share Count APIs (subset)\nHTTP/JSONP-like endpoints")]:::external
end

%% =========================
%% Internal Library Relationships
%% =========================
INDEX -->|"exports"| LEAFBTN
INDEX -->|"exports"| LEAFCOUNT
INDEX -->|"exports"| LEAFICON

LEAFBTN -->|"created_by"| CSB
CSB -->|"wraps"| BASEBTN
CSB -->|"builds_shareUrl"| GETPARAMS
CSB -->|"runtime_checks"| ASSERT
BASEBTN -->|"runtime_checks"| ASSERT
GETPARAMS -->|"runtime_checks"| ASSERT

LEAFCOUNT -->|"created_by"| CSC
CSC -->|"uses"| MOUNT
CSC -->|"runtime_checks"| ASSERT

LEAFICON -->|"created_by"| CIC
CIC -->|"runtime_checks"| ASSERT

%% =========================
%% Demo uses Library
%% =========================
DEMOHTML -->|"loads"| DEMOENTRY
DEMOENTRY -->|"renders"| DEMOCOMP
DEMOCOMP -->|"imports_components"| INDEX
DEMOCOMP -->|"styles"| DEMOCSS

%% =========================
%% Runtime Flows (Solid)
%% =========================
APP -->|"imports"| INDEX

BROWSER -->|"renders_UI"| LEAFBTN
BROWSER -->|"renders_UI"| LEAFCOUNT
BROWSER -->|"renders_UI"| LEAFICON

LEAFBTN -->|"onClick"| BASEBTN
BASEBTN -->|"window.open(shareUrl)"| POPUP
POPUP -->|"navigate_to shareUrl"| SHAREEP

LEAFCOUNT -->|"fetch_count()"| COUNTEP
LEAFCOUNT -->|"avoid_setState_after_unmount"| MOUNT

%% =========================
%% Build/Publish Flows (Dashed)
%% =========================
PKG -.->|"drives_scripts"| VITELIB
PKG -.->|"drives_scripts"| TSLIB
PKG -.->|"drives_scripts"| VITEDEMO
PKG -.->|"drives_scripts"| TSDEMO

TSLIB -.->|"typecheck_emit"| VITELIB
VITELIB -.->|"build_artifacts (ESM/CJS + types)"| NPM

TSDEMO -.->|"typecheck"| VITEDEMO
VITEDEMO -.->|"build_demo"| DEMOHTML

CI -.->|"uses"| NPMINSTALL
REL -.->|"uses"| NPMINSTALL
REL -.->|"versioning_changelog"| CHANGESETS
REL -.->|"publish"| NPM

%% =========================
%% Notes
%% =========================
NOTE1["Note: No SDK/script injection;\nShare is URL-based (tree-shake friendly)"]:::note
NOTE2["Note: Share Count endpoints are third-party\nand may change/deprecate"]:::note

INDEX --- NOTE1
CSC --- NOTE2

%% =========================
%% Click Events (from mapping)
%% =========================
click INDEX "https://github.com/nygardk/react-share/blob/master/src/index.ts"
click BASEBTN "https://github.com/nygardk/react-share/blob/master/src/ShareButton.tsx"
click CSB "https://github.com/nygardk/react-share/blob/master/src/hocs/createShareButton.tsx"
click CSC "https://github.com/nygardk/react-share/blob/master/src/hocs/createShareCount.tsx"
click CIC "https://github.com/nygardk/react-share/blob/master/src/hocs/createIcon.tsx"
click MOUNT "https://github.com/nygardk/react-share/blob/master/src/hooks/useIsMounted.ts"
click GETPARAMS "https://github.com/nygardk/react-share/blob/master/src/utils/objectToGetParams.ts"
click ASSERT "https://github.com/nygardk/react-share/blob/master/src/utils/assert.ts"
click LEAFBTN "https://github.com/nygardk/react-share/blob/master/src/*ShareButton.ts"
click LEAFCOUNT "https://github.com/nygardk/react-share/blob/master/src/*ShareCount.ts"
click LEAFICON "https://github.com/nygardk/react-share/blob/master/src/*Icon.ts"
click DEMOENTRY "https://github.com/nygardk/react-share/blob/master/demo/index.tsx"
click DEMOCOMP "https://github.com/nygardk/react-share/blob/master/demo/Demo.tsx"
click DEMOHTML "https://github.com/nygardk/react-share/blob/master/demo/index.html"
click DEMOCSS "https://github.com/nygardk/react-share/blob/master/demo/Demo.css"
click DEMODECL "https://github.com/nygardk/react-share/blob/master/demo/declarations.d.ts"
click VITELIB "https://github.com/nygardk/react-share/blob/master/vite.config.js"
click VITEDEMO "https://github.com/nygardk/react-share/blob/master/vite.demo.config.js"
click TSLIB "https://github.com/nygardk/react-share/blob/master/tsconfig.json"
click TSDEMO "https://github.com/nygardk/react-share/blob/master/tsconfig.demo.json"
click CI "https://github.com/nygardk/react-share/blob/master/.github/workflows/ci.yml"
click REL "https://github.com/nygardk/react-share/blob/master/.github/workflows/release.yml"
click CHANGESETS "https://github.com/nygardk/react-share/tree/master/.changeset/"
click NPMINSTALL "https://github.com/nygardk/react-share/blob/master/.github/actions/npm-install/action.yml"
click PKG "https://github.com/nygardk/react-share/blob/master/package.json"
click LOCK "https://github.com/nygardk/react-share/blob/master/package-lock.json"
click DEMOENTRY "https://github.com/nygardk/react-share/tree/master/demo/"
click DEMOCOMP "https://github.com/nygardk/react-share/tree/master/demo/"

%% =========================
%% Styles
%% =========================
classDef actor fill:#111827,stroke:#111827,color:#ffffff,stroke-width:1px
classDef frontend fill:#1d4ed8,stroke:#0b2f80,color:#ffffff,stroke-width:1px
classDef code fill:#2563eb,stroke:#1e40af,color:#ffffff,stroke-width:1px
classDef demo fill:#0ea5e9,stroke:#075985,color:#ffffff,stroke-width:1px
classDef browser fill:#6b7280,stroke:#374151,color:#ffffff,stroke-width:1px
classDef external fill:#16a34a,stroke:#14532d,color:#ffffff,stroke-width:1px
classDef tooling fill:#7c3aed,stroke:#4c1d95,color:#ffffff,stroke-width:1px
classDef pipeline fill:#a855f7,stroke:#581c87,color:#ffffff,stroke-width:1px
classDef note fill:#f59e0b,stroke:#92400e,color:#111827,stroke-width:1px
```
