@snipet/react-widget 0.0.0-stage → 0.2.1

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.
@@ -0,0 +1,23 @@
1
+ import type { Snipet, WidgetEvent, WidgetEvents, WidgetInit } from "@snipet/widget";
2
+ export type * from "@snipet/widget";
3
+ /** Event to the prop that handles it. */
4
+ declare const EVENT_PROPS: {
5
+ readonly open: "onOpen";
6
+ readonly close: "onClose";
7
+ readonly message: "onMessage";
8
+ readonly error: "onError";
9
+ };
10
+ type EventProps = {
11
+ [E in WidgetEvent as (typeof EVENT_PROPS)[E]]?: (payload: WidgetEvents[E]) => void;
12
+ };
13
+ export type SnipetWidgetProps = WidgetInit & EventProps & {
14
+ /** Where to load the widget script from; defaults to the CDN build of the installed version. */
15
+ src?: string;
16
+ };
17
+ /**
18
+ * Mounts the chat widget while rendered; prop changes reach it like configure().
19
+ * Render it once, anywhere: the widget draws itself in the page corner, so this renders nothing.
20
+ */
21
+ export declare function SnipetWidget({ src, onOpen, onClose, onMessage, onError, ...config }: SnipetWidgetProps): null;
22
+ /** The widget's API: open, close, setToken, on... Safe to call before the widget loads. */
23
+ export declare const useSnipet: () => Snipet;
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ "use client";
2
+ import { snipet } from "@snipet/widget";
3
+ import { useEffect, useRef } from "react";
4
+ /** Event to the prop that handles it. */
5
+ const EVENT_PROPS = { open: "onOpen", close: "onClose", message: "onMessage", error: "onError" };
6
+ /**
7
+ * Mounts the chat widget while rendered; prop changes reach it like configure().
8
+ * Render it once, anywhere: the widget draws itself in the page corner, so this renders nothing.
9
+ */
10
+ export function SnipetWidget({ src, onOpen, onClose, onMessage, onError, ...config }) {
11
+ const handlers = useRef({});
12
+ useEffect(() => {
13
+ handlers.current = { onOpen, onClose, onMessage, onError };
14
+ });
15
+ // The first run mounts; later ones (after any render) re-apply the config.
16
+ useEffect(() => snipet.init(config, src));
17
+ useEffect(() => {
18
+ const offs = Object.keys(EVENT_PROPS).map((event) => snipet.on(event, (payload) => handlers.current[EVENT_PROPS[event]]?.(payload)));
19
+ return () => {
20
+ offs.forEach((off) => off());
21
+ snipet.destroy();
22
+ };
23
+ }, []);
24
+ return null;
25
+ }
26
+ /** The widget's API: open, close, setToken, on... Safe to call before the widget loads. */
27
+ export const useSnipet = () => snipet;
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@snipet/react-widget",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.1",
4
+ "description": "React bindings for the Snipet chat widget.",
5
+ "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/usesnipet/snipet.git",
9
+ "directory": "packages/react-widget"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "sideEffects": false,
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "dependencies": {
25
+ "@snipet/widget": "0.2.1"
26
+ },
27
+ "peerDependencies": {
28
+ "react": ">=18"
29
+ },
30
+ "devDependencies": {
31
+ "@types/react": "^19.3.0",
32
+ "react": "^19.3.0",
33
+ "typescript": "~6.0.3"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc"
37
+ }
6
38
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.