diff --git a/.changeset/clean-singers-switch.md b/.changeset/clean-singers-switch.md new file mode 100644 index 000000000..deaa0307b --- /dev/null +++ b/.changeset/clean-singers-switch.md @@ -0,0 +1,7 @@ +--- +"@knocklabs/react-native-example": minor +"@knocklabs/react-native": minor +"@knocklabs/expo": minor +--- + +Move KnockExpoPushNotificationProvider to @knocklabs/expo diff --git a/examples/react-native-example/package.json b/examples/react-native-example/package.json index bef2eb90b..4abc71e74 100644 --- a/examples/react-native-example/package.json +++ b/examples/react-native-example/package.json @@ -11,7 +11,7 @@ "format:check": "prettier \"src/**/*.{js,ts,tsx}\" --check" }, "dependencies": { - "@knocklabs/react-native": "workspace:^", + "@knocklabs/expo": "workspace:^", "expo": ">=51.0.24", "expo-constants": ">=16.0.2", "expo-device": ">=6.0.2", diff --git a/examples/react-native-example/src/App.tsx b/examples/react-native-example/src/App.tsx index 92e4a858b..8b1c6e2d0 100644 --- a/examples/react-native-example/src/App.tsx +++ b/examples/react-native-example/src/App.tsx @@ -3,7 +3,7 @@ import { KnockFeedProvider, KnockProvider, NotificationIconButton, -} from "@knocklabs/react-native"; +} from "@knocklabs/expo"; import { StatusBar } from "expo-status-bar"; import React, { useCallback, useState } from "react"; import { StyleSheet, View } from "react-native"; diff --git a/examples/react-native-example/src/NotificationFeedContainer.tsx b/examples/react-native-example/src/NotificationFeedContainer.tsx index 5ff5b25c5..3d876bc39 100644 --- a/examples/react-native-example/src/NotificationFeedContainer.tsx +++ b/examples/react-native-example/src/NotificationFeedContainer.tsx @@ -1,5 +1,5 @@ -import { ActionButton, FeedItem } from "@knocklabs/client"; -import { NotificationFeed } from "@knocklabs/react-native"; +import type { ActionButton, FeedItem } from "@knocklabs/client"; +import { NotificationFeed } from "@knocklabs/expo"; import React, { useCallback } from "react"; import { StyleSheet, Text, TouchableOpacity, View } from "react-native"; diff --git a/packages/expo/.eslintrc.js b/packages/expo/.eslintrc.js new file mode 100644 index 000000000..d4f519594 --- /dev/null +++ b/packages/expo/.eslintrc.js @@ -0,0 +1,11 @@ +/** @type {import("eslint").Linter.Config} */ +module.exports = { + root: true, + extends: [ + "@knocklabs/eslint-config/library.js", + "plugin:react-hooks/recommended", + ], + parserOptions: { + projects: ["tsconfig.json", "tsconfig.node.json"], + }, +}; diff --git a/packages/expo/Babel.config.json b/packages/expo/Babel.config.json new file mode 100644 index 000000000..edbfbb26f --- /dev/null +++ b/packages/expo/Babel.config.json @@ -0,0 +1,17 @@ +{ + "presets": [ + [ + "@nx/react/babel", + { + "runtime": "automatic", + "useBuiltIns": "usage" + } + ] + ], + "plugins": ["module:react-native-dotenv"], + "env": { + "test": { + "presets": ["module:metro-react-native-babel-preset"] + } + } +} diff --git a/packages/expo/README.md b/packages/expo/README.md new file mode 100644 index 000000000..ba0fb07aa --- /dev/null +++ b/packages/expo/README.md @@ -0,0 +1,146 @@ +# Knock Expo SDK + +A set of components for integrating [Knock](https://knock.app) in-app notifications into an Expo + React Native application. + +> Not using Expo? See our vanilla [React Native SDK](../react-native/README.md). + +## Installation + +Via NPM: + +``` +npm install @knocklabs/expo +``` + +Via Yarn: + +``` +yarn add @knocklabs/expo +``` + +## Migrating from `@knocklabs/react-native` + +As of `@knocklabs/react-native` v0.4.0, `KnockExpoPushNotificationProvider` has moved to our Expo SDK. To migrate: + +1. Remove `@knocklabs/react-native` from your project + + NPM: + + ```bash + npm uninstall @knocklabs/react-native + ``` + + Yarn: + + ```bash + yarn remove @knocklabs/react-native + ``` + +1. Install `@knocklabs/expo` + + NPM: + + ```bash + npm install @knocklabs/expo + ``` + + Yarn: + + ```bash + yarn add @knocklabs/expo + ``` + +1. Update any import statements from `@knocklabs/react-native` to `@knocklabs/expo` + + From: + + ```js + import { + KnockExpoPushNotificationProvider, + KnockFeedProvider, + KnockProvider, + NotificationIconButton, + } from "@knocklabs/react-native"; + ``` + + To: + + ```js + import { + KnockExpoPushNotificationProvider, + KnockFeedProvider, + KnockProvider, + NotificationIconButton, + } from "@knocklabs/expo"; + ``` + +## Configuration + +To configure the feed you will need: + +1. A public API key (found in the Knock dashboard) +1. A user ID, and optionally an auth token for production environments +1. If integrating an in-app feed, a feed channel ID (found in the Knock dashboard) + +## Usage + +You can integrate the feed into your app as follows: + +```jsx +import { + KnockFeedProvider, + KnockProvider, + NotificationFeedContainer, +} from "@knocklabs/expo"; + +const YourAppLayout = () => { + const [isVisible, setIsVisible] = useState(false); + const notifButtonRef = useRef(null); + + return ( + + + + Notifications go in here! + + + + ); +}; +``` + +## Headless usage + +Alternatively, if you don't want to use our components you can render the feed in a headless mode using our hooks: + +```jsx +import { useAuthenticatedKnockClient, useNotifications } from "@knocklabs/expo"; +import create from "zustand"; + +const YourAppLayout = () => { + const knockClient = useAuthenticatedKnockClient( + process.env.KNOCK_PUBLIC_API_KEY, + currentUser.id, + ); + + const notificationFeed = useNotifications( + knockClient, + process.env.KNOCK_FEED_ID, + ); + + const useNotificationStore = create(notificationFeed.store); + const { metadata } = useNotificationStore(); + + useEffect(() => { + notificationFeed.fetch(); + }, [notificationFeed]); + + return Total unread: {metadata.unread_count}; +}; +``` + +## Related links + +- [Signup for Knock](https://knock.app) +- [Knock documentation](https://docs.knock.app) +- [Knock dashboard](https://dashboard.knock.app) diff --git a/packages/expo/package.json b/packages/expo/package.json new file mode 100644 index 000000000..f22eddfd6 --- /dev/null +++ b/packages/expo/package.json @@ -0,0 +1,80 @@ +{ + "name": "@knocklabs/expo", + "version": "0.0.0", + "author": "@knocklabs", + "license": "MIT", + "main": "dist/cjs/index.js", + "module": "dist/esm/index.mjs", + "types": "dist/types/index.d.ts", + "typings": "dist/types/index.d.ts", + "react-native": "./src/index.ts", + "exports": { + ".": { + "require": "./dist/cjs/index.js", + "types": "./dist/types/index.d.ts", + "react-native": "./src/index.ts", + "default": "./dist/esm/index.mjs" + } + }, + "files": [ + "dist", + "src", + "README.md" + ], + "scripts": { + "clean": "rimraf dist", + "dev": "tsc && vite build --watch", + "build": "yarn clean && yarn build:esm && yarn build:cjs", + "build:esm": "BUILD_TARGET=esm; tsc && vite build", + "build:cjs": "BUILD_TARGET=cjs; tsc && vite build", + "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", + "format": "prettier \"src/**/*.{js,ts,tsx}\" --write", + "format:check": "prettier \"src/**/*.{js,ts,tsx}\" --check", + "type:check": "tsc --noEmit", + "preview": "vite preview" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/knocklabs/javascript.git" + }, + "bugs": { + "url": "https://github.com/knocklabs/javascript/issues" + }, + "peerDependencies": { + "expo": "*", + "expo-constants": "*", + "expo-device": "*", + "expo-notifications": "*", + "react": "*", + "react-native": "*" + }, + "dependencies": { + "@knocklabs/client": "workspace:^", + "@knocklabs/react-core": "workspace:^", + "@knocklabs/react-native": "workspace:^", + "react-native-gesture-handler": "^2.19.0", + "react-native-render-html": "^6.3.4", + "react-native-svg": "^15.6.0" + }, + "devDependencies": { + "@types/react": "^18.3.6", + "@types/react-native-htmlview": "^0.16.5", + "@typescript-eslint/eslint-plugin": "^6.20.0", + "@typescript-eslint/parser": "^8.8.1", + "@vitejs/plugin-react": "^4.3.2", + "eslint": "^8.56.0", + "eslint-plugin-react-hooks": "^4.6.0", + "eslint-plugin-react-refresh": "^0.4.4", + "expo": ">=51.0.24", + "expo-constants": ">=16.0.2", + "expo-device": ">=6.0.2", + "expo-notifications": ">=0.28.16", + "react": "^18.2.0", + "react-native": "^0.73.4", + "rimraf": "^6.0.1", + "typescript": "^5.6.2", + "vite": "^5.0.0", + "vite-plugin-dts": "^3.6.3", + "vite-plugin-no-bundle": "^4.0.0" + } +} diff --git a/packages/expo/src/index.ts b/packages/expo/src/index.ts new file mode 100644 index 000000000..153b3d687 --- /dev/null +++ b/packages/expo/src/index.ts @@ -0,0 +1,2 @@ +export * from "./modules/push"; +export * from "@knocklabs/react-native"; diff --git a/packages/react-native/src/modules/push/KnockExpoPushNotificationProvider.tsx b/packages/expo/src/modules/push/KnockExpoPushNotificationProvider.tsx similarity index 100% rename from packages/react-native/src/modules/push/KnockExpoPushNotificationProvider.tsx rename to packages/expo/src/modules/push/KnockExpoPushNotificationProvider.tsx diff --git a/packages/react-native/src/modules/push/index.ts b/packages/expo/src/modules/push/index.ts similarity index 100% rename from packages/react-native/src/modules/push/index.ts rename to packages/expo/src/modules/push/index.ts diff --git a/packages/expo/tsconfig.json b/packages/expo/tsconfig.json new file mode 100644 index 000000000..cd7cd18db --- /dev/null +++ b/packages/expo/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "@knocklabs/typescript-config/react-library.json", + "include": ["src"], + "exclude": ["node_modules", "dist"] +} diff --git a/packages/expo/tsconfig.node.json b/packages/expo/tsconfig.node.json new file mode 100644 index 000000000..c8d60c17b --- /dev/null +++ b/packages/expo/tsconfig.node.json @@ -0,0 +1,4 @@ +{ + "extends": "@knocklabs/typescript-config/node.json", + "include": ["vite.config.ts"] +} diff --git a/packages/expo/vite.config.mts b/packages/expo/vite.config.mts new file mode 100644 index 000000000..63233a3f7 --- /dev/null +++ b/packages/expo/vite.config.mts @@ -0,0 +1,48 @@ +import react from "@vitejs/plugin-react"; +import { resolve } from "path"; +import { LibraryFormats, defineConfig, loadEnv } from "vite"; +import dts from "vite-plugin-dts"; +import noBundlePlugin from "vite-plugin-no-bundle"; + +export default defineConfig(({ mode }) => { + const env = loadEnv(mode, process.cwd(), ""); + const CJS = env.BUILD_TARGET?.toLocaleLowerCase()?.match("cjs"); + const formats: LibraryFormats[] = CJS ? ["cjs"] : ["es"]; + + return { + plugins: [ + dts({ + outDir: "dist/types", + }), + react(), + noBundlePlugin({ copy: "**/*.css", root: "./src" }), + ], + build: { + outDir: CJS ? "dist/cjs" : "dist/esm", + sourcemap: true, + lib: { + entry: resolve(__dirname, "src/index.ts"), + name: "expo", + formats, + }, + rollupOptions: { + // External packages that should not be bundled into your library. + external: [ + "react", + "react-native", + "expo", + "expo-constants", + "expo-device", + "expo-notifications", + ], + output: { + interop: "compat", + format: formats[0], + globals: { + react: "React", + }, + }, + }, + }, + }; +}); diff --git a/packages/react-native/README.md b/packages/react-native/README.md index a040710fd..b4c0a7041 100644 --- a/packages/react-native/README.md +++ b/packages/react-native/README.md @@ -4,6 +4,8 @@ A set of components for integrating [Knock](https://knock.app) in-app notificati [Full documentation](https://docs.knock.app/in-app-ui/react-native/overview) +> Using Expo? See our [Expo SDK](../expo/README.md) and our [migration guide](../expo/README.md#migrating-from-knocklabsreact-native). + ## Installation Via NPM: diff --git a/packages/react-native/package.json b/packages/react-native/package.json index c6e559c41..64e1e658b 100644 --- a/packages/react-native/package.json +++ b/packages/react-native/package.json @@ -41,27 +41,9 @@ "url": "https://github.com/knocklabs/javascript/issues" }, "peerDependencies": { - "expo": "*", - "expo-constants": "*", - "expo-device": "*", - "expo-notifications": "*", "react": "*", "react-native": "*" }, - "peerDependenciesMeta": { - "expo": { - "optional": true - }, - "expo-constants": { - "optional": true - }, - "expo-device": { - "optional": true - }, - "expo-notifications": { - "optional": true - } - }, "dependencies": { "@knocklabs/client": "workspace:^", "@knocklabs/react-core": "workspace:^", @@ -78,10 +60,6 @@ "eslint": "^8.56.0", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.4", - "expo": ">=51.0.24", - "expo-constants": ">=16.0.2", - "expo-device": ">=6.0.2", - "expo-notifications": ">=0.28.16", "react": "^18.2.0", "react-native": "^0.73.4", "rimraf": "^6.0.1", diff --git a/packages/react-native/src/index.ts b/packages/react-native/src/index.ts index bbce3d312..5ea4e960f 100644 --- a/packages/react-native/src/index.ts +++ b/packages/react-native/src/index.ts @@ -1,4 +1,3 @@ export * from "./modules/feed"; -export * from "./modules/push"; export * from "@knocklabs/react-core"; export * from "./assets"; diff --git a/packages/react-native/vite.config.mts b/packages/react-native/vite.config.mts index 1827fbaa3..6b34c263f 100644 --- a/packages/react-native/vite.config.mts +++ b/packages/react-native/vite.config.mts @@ -27,14 +27,7 @@ export default defineConfig(({ mode }) => { }, rollupOptions: { // External packages that should not be bundled into your library. - external: [ - "react", - "react-native", - "expo", - "expo-constants", - "expo-device", - "expo-notifications", - ], + external: ["react", "react-native"], output: { interop: "compat", format: formats[0], diff --git a/yarn.lock b/yarn.lock index efd887744..47d1a5530 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4988,6 +4988,45 @@ __metadata: languageName: unknown linkType: soft +"@knocklabs/expo@workspace:^, @knocklabs/expo@workspace:packages/expo": + version: 0.0.0-use.local + resolution: "@knocklabs/expo@workspace:packages/expo" + dependencies: + "@knocklabs/client": "workspace:^" + "@knocklabs/react-core": "workspace:^" + "@knocklabs/react-native": "workspace:^" + "@types/react": "npm:^18.3.6" + "@types/react-native-htmlview": "npm:^0.16.5" + "@typescript-eslint/eslint-plugin": "npm:^6.20.0" + "@typescript-eslint/parser": "npm:^8.8.1" + "@vitejs/plugin-react": "npm:^4.3.2" + eslint: "npm:^8.56.0" + eslint-plugin-react-hooks: "npm:^4.6.0" + eslint-plugin-react-refresh: "npm:^0.4.4" + expo: "npm:>=51.0.24" + expo-constants: "npm:>=16.0.2" + expo-device: "npm:>=6.0.2" + expo-notifications: "npm:>=0.28.16" + react: "npm:^18.2.0" + react-native: "npm:^0.73.4" + react-native-gesture-handler: "npm:^2.19.0" + react-native-render-html: "npm:^6.3.4" + react-native-svg: "npm:^15.6.0" + rimraf: "npm:^6.0.1" + typescript: "npm:^5.6.2" + vite: "npm:^5.0.0" + vite-plugin-dts: "npm:^3.6.3" + vite-plugin-no-bundle: "npm:^4.0.0" + peerDependencies: + expo: "*" + expo-constants: "*" + expo-device: "*" + expo-notifications: "*" + react: "*" + react-native: "*" + languageName: unknown + linkType: soft + "@knocklabs/javascript@workspace:.": version: 0.0.0-use.local resolution: "@knocklabs/javascript@workspace:." @@ -5056,7 +5095,7 @@ __metadata: resolution: "@knocklabs/react-native-example@workspace:examples/react-native-example" dependencies: "@babel/core": "npm:^7.25.7" - "@knocklabs/react-native": "workspace:^" + "@knocklabs/expo": "workspace:^" expo: "npm:>=51.0.24" expo-constants: "npm:>=16.0.2" expo-device: "npm:>=6.0.2" @@ -5082,10 +5121,6 @@ __metadata: eslint: "npm:^8.56.0" eslint-plugin-react-hooks: "npm:^4.6.0" eslint-plugin-react-refresh: "npm:^0.4.4" - expo: "npm:>=51.0.24" - expo-constants: "npm:>=16.0.2" - expo-device: "npm:>=6.0.2" - expo-notifications: "npm:>=0.28.16" react: "npm:^18.2.0" react-native: "npm:^0.73.4" react-native-gesture-handler: "npm:^2.19.0" @@ -5097,21 +5132,8 @@ __metadata: vite-plugin-dts: "npm:^3.6.3" vite-plugin-no-bundle: "npm:^4.0.0" peerDependencies: - expo: "*" - expo-constants: "*" - expo-device: "*" - expo-notifications: "*" react: "*" react-native: "*" - peerDependenciesMeta: - expo: - optional: true - expo-constants: - optional: true - expo-device: - optional: true - expo-notifications: - optional: true languageName: unknown linkType: soft