Components / Share

@peerkit-dev/share


Peer-to-peer file sharing with drag-and-drop and transfer controls.

  • JavaScript
  • Web Components
  • React

Use ShareController, the <peerkit-share> element, or the React component. Files travel on a real PeerKit data channel for the active session only — nothing is stored after it ends.

JavaScript and Web Components

npm install @peerkit-dev/share-core @peerkit-dev/sdk

React

npm install @peerkit-dev/share @peerkit-dev/sdk react react-dom

Try Live DemoDocumentation

Live demo

PeerKitShare

Starting a real data session…

Share files

Provide a session or invitation

Waiting for the other participant

Installation

Install @peerkit-dev/share-core for vanilla JavaScript or the <peerkit-share> element. Install @peerkit-dev/share when the page is React. The React package depends on the core package and re-exports ShareController.

JavaScript and Web Components

npm install @peerkit-dev/share-core @peerkit-dev/sdk

React

npm install @peerkit-dev/share @peerkit-dev/sdk react react-dom

Vanilla JavaScript

ShareController holds the view state. subscribe and getSnapshot are how a page reads it. Call sync() after construction, and dispose() when the view goes away.

import { ShareController } from "@peerkit-dev/share-core";

const share = new ShareController(() => ({
  invitationUrl: "https://peerkit.dev/s#inv_…",
}));
share.subscribe(() => {
  const snapshot = share.getSnapshot();
  console.log(snapshot.statusLabel, snapshot.canSend);
});
share.sync();

Web Component

Importing @peerkit-dev/share-core does not register the element. Import @peerkit-dev/share-core/element, or load dist/element.global.js from a script tag. The element renders in light DOM.

<script type="module">
  import "@peerkit-dev/share-core/element";
</script>
<peerkit-share></peerkit-share>
<script type="module">
  const share = document.querySelector("peerkit-share");
  share.invitationUrl = "https://peerkit.dev/s#inv_…";
</script>

React

Prefer an existing PeerKitSession when pairing components so they share one connection. Messaging and file transfers are live, session-based operations — PeerKit does not store conversations or files.

import { PeerKitShare } from "@peerkit-dev/share";
import type { PeerKitSession } from "@peerkit-dev/sdk";

export function Files({ session }: { session: PeerKitSession }) {
  return (
    <PeerKitShare
      session={session}
      onTransfer={(transfer) => console.log(transfer.status, transfer.name)}
      onError={(error) => console.error(error.message)}
    />
  );
}

React props

The custom element uses the same names as properties. className and style are React. Theme either surface with the CSS variables below.

PropPurpose
sessionAdvanced. Existing SDK session. Recommended for shared use with the other component.
invitationUrl / signalingUrl + tokenSimple join when the component should create and connect its own session.
labelsOverride UI copy for titles, buttons, and empty states.
className / styleRoot styling. Theme with --peerkit-share-* variables on .pk-share or a wrapper.
onTransfer, onError, onConnectionChange, onReadyCallbacks for session activity and failures.

Theming

PeerKitShare ships neutral defaults. Override --peerkit-share-* variables in your CSS. Fonts inherit from the host app unless you set --peerkit-share-font.

.my-share {
  --peerkit-share-accent: #0d9488;
  --peerkit-share-button-bg: #0d9488;
  --peerkit-share-button-ink: #ffffff;
  --peerkit-share-bg: #f8fafc;
  --peerkit-share-progress: #0d9488;
}

Useful tokens include accent, button background/ink/hover, background, surface, ink, muted, line, radius, progress, status-ready, danger, and font.