├── src ├── contexts │ ├── index.ts │ ├── ToastContext.tsx │ └── ToastProvider.tsx ├── hooks │ ├── index.ts │ ├── useToastContext.ts │ ├── useToast.ts │ └── useInterval.ts ├── utils │ └── dimensions.ts ├── svgs │ ├── index.ts │ ├── CloseIcon.tsx │ ├── WarningIcon.tsx │ ├── SuccessIcon.tsx │ ├── InfoIcon.tsx │ └── ErrorIcon.tsx ├── components │ ├── index.ts │ ├── InfoToast.tsx │ ├── ErrorToast.tsx │ ├── SuccessToast.tsx │ ├── WarningToast.tsx │ ├── baseStyles.ts │ ├── BaseToast.tsx │ └── Toaster.tsx ├── index.ts └── types │ └── index.ts ├── commitlint.config.js ├── babel.config.js ├── example ├── app.json ├── README.md ├── babel.config.js ├── .expo │ ├── settings.json │ ├── packager-info.json │ └── README.md ├── tsconfig.json ├── package.json └── App.tsx ├── .prettierrc.json ├── .release-it.json ├── .eslintrc.js ├── tsconfig.json ├── LICENSE ├── .gitignore ├── README.md ├── package.json └── CONTRIBUTING.md /src/contexts/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./ToastProvider"; 2 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: ["@commitlint/config-conventional"], 3 | }; 4 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: ["module:metro-react-native-babel-preset"], 3 | }; 4 | -------------------------------------------------------------------------------- /example/app.json: -------------------------------------------------------------------------------- 1 | { 2 | "expo": { 3 | "experiments": { 4 | "turboModules": true 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/hooks/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./useInterval"; 2 | export * from "./useToast"; 3 | export * from "./useToastContext"; 4 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | ## Running the example 2 | 3 | ```sh 4 | yarn install 5 | ``` 6 | 7 | ```sh 8 | expo start 9 | ``` 10 | -------------------------------------------------------------------------------- /src/utils/dimensions.ts: -------------------------------------------------------------------------------- 1 | import { Dimensions } from "react-native"; 2 | 3 | export const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get( 4 | "window" 5 | ); 6 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 80, 3 | "arrowParens": "avoid", 4 | "singleQuote": false, 5 | "tabWidth": 2, 6 | "trailingComma": "es5", 7 | "useTabs": false 8 | } 9 | -------------------------------------------------------------------------------- /src/svgs/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./CloseIcon"; 2 | export * from "./ErrorIcon"; 3 | export * from "./InfoIcon"; 4 | export * from "./SuccessIcon"; 5 | export * from "./WarningIcon"; 6 | -------------------------------------------------------------------------------- /src/components/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./Toaster"; 2 | export * from "./ErrorToast"; 3 | export * from "./InfoToast"; 4 | export * from "./SuccessToast"; 5 | export * from "./WarningToast"; 6 | -------------------------------------------------------------------------------- /example/babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = function (api) { 2 | api.cache(true); 3 | return { 4 | presets: ["babel-preset-expo"], 5 | plugins: ["react-native-reanimated/plugin"], 6 | }; 7 | }; 8 | -------------------------------------------------------------------------------- /example/.expo/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "hostType": "lan", 3 | "lanType": "ip", 4 | "dev": true, 5 | "minify": false, 6 | "urlRandomness": null, 7 | "https": false, 8 | "scheme": null, 9 | "devClient": false 10 | } 11 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | // provider 2 | export { default as ToastProvider } from "./contexts/ToastProvider"; 3 | 4 | // hooks 5 | export { default as useToast } from "./hooks/useToast"; 6 | export { default as useToastContext } from "./hooks/useToastContext"; 7 | -------------------------------------------------------------------------------- /example/.expo/packager-info.json: -------------------------------------------------------------------------------- 1 | { 2 | "devToolsPort": 19002, 3 | "expoServerPort": null, 4 | "packagerPort": null, 5 | "packagerPid": null, 6 | "expoServerNgrokUrl": null, 7 | "packagerNgrokUrl": null, 8 | "ngrokPid": null 9 | } 10 | -------------------------------------------------------------------------------- /.release-it.json: -------------------------------------------------------------------------------- 1 | { 2 | "git": { 3 | "push": true, 4 | "tagName": "v${version}", 5 | "commitMessage": "chore: release v${version}" 6 | }, 7 | "github": { 8 | "release": true 9 | }, 10 | "npm": { 11 | "publish": false 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/contexts/ToastContext.tsx: -------------------------------------------------------------------------------- 1 | import { createContext } from "react"; 2 | import { IToastContext } from "../types"; 3 | 4 | const ToastContext = createContext(undefined!); 5 | 6 | ToastContext.displayName = "ToastContext"; 7 | 8 | export default ToastContext; 9 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": true, 4 | "jsx": "react-native", 5 | "lib": ["dom", "esnext"], 6 | "moduleResolution": "node", 7 | "noEmit": true, 8 | "skipLibCheck": true, 9 | "resolveJsonModule": true, 10 | "strict": true 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ["@react-native-community", "prettier"], 4 | ignorePatterns: ["node_modules/", "lib/"], 5 | rules: { 6 | "prettier/prettier": [ 7 | "error", 8 | { 9 | printWidth: 80, 10 | arrowParens: "avoid", 11 | singleQuote: false, 12 | tabWidth: 2, 13 | trailingComma: "es5", 14 | useTabs: false, 15 | }, 16 | ], 17 | }, 18 | }; 19 | -------------------------------------------------------------------------------- /src/components/InfoToast.tsx: -------------------------------------------------------------------------------- 1 | import React, { ComponentProps } from "react"; 2 | import { InfoIcon } from "../svgs"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import BaseToast from "./BaseToast"; 5 | 6 | export const InfoToast: React.FC> = props => { 7 | const { colors } = useToastContext(); 8 | return ( 9 | } /> 10 | ); 11 | }; 12 | -------------------------------------------------------------------------------- /src/components/ErrorToast.tsx: -------------------------------------------------------------------------------- 1 | import React, { ComponentProps } from "react"; 2 | import { ErrorIcon } from "../svgs"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import BaseToast from "./BaseToast"; 5 | 6 | export const ErrorToast: React.FC> = props => { 7 | const { colors } = useToastContext(); 8 | return ( 9 | } /> 10 | ); 11 | }; 12 | -------------------------------------------------------------------------------- /src/hooks/useToastContext.ts: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ToastContext from "../contexts/ToastContext"; 3 | import { IToastContext } from "../types"; 4 | 5 | const useToastContext = (): IToastContext => { 6 | const context = React.useContext(ToastContext); 7 | if (context === undefined) { 8 | throw new Error("useToastContext must be used within a ToastProvider"); 9 | } 10 | return context; 11 | }; 12 | 13 | export default useToastContext; 14 | -------------------------------------------------------------------------------- /src/components/SuccessToast.tsx: -------------------------------------------------------------------------------- 1 | import React, { ComponentProps } from "react"; 2 | import { SuccessIcon } from "../svgs"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import BaseToast from "./BaseToast"; 5 | 6 | export const SuccessToast: React.FC< 7 | ComponentProps 8 | > = props => { 9 | const { colors } = useToastContext(); 10 | return ( 11 | } 15 | /> 16 | ); 17 | }; 18 | -------------------------------------------------------------------------------- /src/components/WarningToast.tsx: -------------------------------------------------------------------------------- 1 | import React, { ComponentProps } from "react"; 2 | import { WarningIcon } from "../svgs"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import BaseToast from "./BaseToast"; 5 | 6 | export const WarningToast: React.FC< 7 | ComponentProps 8 | > = props => { 9 | const { colors } = useToastContext(); 10 | return ( 11 | } 15 | /> 16 | ); 17 | }; 18 | -------------------------------------------------------------------------------- /src/hooks/useToast.ts: -------------------------------------------------------------------------------- 1 | import { useMemo } from "react"; 2 | import { UseToastHook } from "../types"; 3 | import useToastContext from "./useToastContext"; 4 | 5 | const useToast = (): UseToastHook => { 6 | const context = useToastContext(); 7 | return useMemo( 8 | () => ({ 9 | showToast: context.showToast, 10 | queueToast: context.queueToast, 11 | hideToast: context.hideToast, 12 | clearToastQueue: context.clearToastQueue, 13 | }), 14 | [ 15 | context.clearToastQueue, 16 | context.hideToast, 17 | context.queueToast, 18 | context.showToast, 19 | ] 20 | ); 21 | }; 22 | 23 | export default useToast; 24 | -------------------------------------------------------------------------------- /src/hooks/useInterval.ts: -------------------------------------------------------------------------------- 1 | // h/t Dan Abramov - https://overreacted.io/making-setinterval-declarative-with-react-hooks/ 2 | import { useEffect, useRef } from "react"; 3 | 4 | export function useInterval(callback: () => void, delay?: number | null): void { 5 | const savedCallback = useRef<() => void>(); 6 | 7 | // Remember the latest callback. 8 | useEffect(() => { 9 | savedCallback.current = callback; 10 | }, [callback]); 11 | 12 | // Set up the interval. 13 | useEffect(() => { 14 | function tick() { 15 | savedCallback.current?.(); 16 | } 17 | if (delay) { 18 | const id = setInterval(tick, delay); 19 | return () => clearInterval(id); 20 | } 21 | return undefined; 22 | }, [delay]); 23 | } 24 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "paths": { 5 | "react-native-reanimated-toast": ["./src/index"] 6 | }, 7 | "allowUnreachableCode": false, 8 | "allowUnusedLabels": false, 9 | "esModuleInterop": true, 10 | "forceConsistentCasingInFileNames": true, 11 | "jsx": "react", 12 | "lib": ["esnext"], 13 | "module": "esnext", 14 | "moduleResolution": "node", 15 | "noFallthroughCasesInSwitch": true, 16 | "noImplicitReturns": true, 17 | "noImplicitUseStrict": false, 18 | "noStrictGenericChecks": false, 19 | "noUnusedLocals": true, 20 | "noUnusedParameters": true, 21 | "resolveJsonModule": true, 22 | "skipLibCheck": true, 23 | "strict": true, 24 | "target": "esnext" 25 | }, 26 | "exclude": ["example"], 27 | "include": ["src"] 28 | } 29 | -------------------------------------------------------------------------------- /example/.expo/README.md: -------------------------------------------------------------------------------- 1 | > Why do I have a folder named ".expo" in my project? 2 | 3 | The ".expo" folder is created when an Expo project is started using "expo start" command. 4 | 5 | > What does the "packager-info.json" file contain? 6 | 7 | The "packager-info.json" file contains port numbers and process PIDs that are used to serve the application to the mobile device/simulator. 8 | 9 | > What does the "settings.json" file contain? 10 | 11 | The "settings.json" file contains the server configuration that is used to serve the application manifest. 12 | 13 | > Should I commit the ".expo" folder? 14 | 15 | No, you should not share the ".expo" folder. It does not contain any information that is relevant for other developers working on the project, it is specific to your machine. 16 | 17 | Upon project creation, the ".expo" folder is already added to your ".gitignore" file. 18 | -------------------------------------------------------------------------------- /src/svgs/CloseIcon.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import Svg, { Path } from "react-native-svg"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import type { SvgProps } from "../types"; 5 | 6 | export function CloseIcon({ color, size = 12 }: SvgProps): React.ReactElement { 7 | const { colors } = useToastContext(); 8 | const fillColor = color ?? colors.closeIcon; 9 | 10 | return ( 11 | 12 | 16 | 20 | 21 | ); 22 | } 23 | -------------------------------------------------------------------------------- /src/svgs/WarningIcon.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import Svg, { Path } from "react-native-svg"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import type { SvgProps } from "../types"; 5 | 6 | export function WarningIcon({ 7 | color, 8 | size = 24, 9 | }: SvgProps): React.ReactElement { 10 | const { colors } = useToastContext(); 11 | const fillColor = color ?? colors.warning; 12 | 13 | return ( 14 | 15 | 19 | 23 | 24 | ); 25 | } 26 | -------------------------------------------------------------------------------- /src/svgs/SuccessIcon.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import Svg, { Path } from "react-native-svg"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import type { SvgProps } from "../types"; 5 | 6 | export function SuccessIcon({ 7 | color, 8 | size = 24, 9 | }: SvgProps): React.ReactElement { 10 | const { colors } = useToastContext(); 11 | const fillColor = color ?? colors.success; 12 | 13 | return ( 14 | 15 | 19 | 23 | 24 | ); 25 | } 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | Copyright (c) 2020 Ragnar Rebase 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-reanimated-toast-example", 3 | "description": "Example app for react-native-reanimated-toast", 4 | "version": "1.0.0", 5 | "private": true, 6 | "scripts": { 7 | "start": "expo start", 8 | "android": "expo start --android", 9 | "ios": "expo start --ios", 10 | "web": "expo web", 11 | "eject": "expo eject" 12 | }, 13 | "dependencies": { 14 | "expo": "~39.0.5", 15 | "expo-haptics": "~8.3.0", 16 | "expo-status-bar": "~1.0.2", 17 | "react": "~16.13.0", 18 | "react-dom": "~16.13.0", 19 | "react-native": "https://github.com/expo/react-native/archive/sdk-39.0.4.tar.gz", 20 | "react-native-gesture-handler": "~1.7.0", 21 | "react-native-reanimated": "2.0.0-alpha.7", 22 | "react-native-reanimated-toast": "^1.2.0", 23 | "react-native-svg": "^12.1.0", 24 | "react-native-web": "0.13.13" 25 | }, 26 | "devDependencies": { 27 | "@babel/core": "^7.8.6", 28 | "@types/react": "^17.0.0", 29 | "@types/react-dom": "^17.0.0", 30 | "@types/react-native": "^0.63.37", 31 | "babel-preset-expo": "~8.1.0", 32 | "typescript": "^4.1.2" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | project.xcworkspace 24 | 25 | # Android/IntelliJ 26 | # 27 | build/ 28 | .idea 29 | .gradle 30 | local.properties 31 | *.iml 32 | 33 | # node.js 34 | # 35 | node_modules/ 36 | example/node_modules/ 37 | npm-debug.log 38 | yarn-error.log 39 | 40 | # BUCK 41 | buck-out/ 42 | \.buckd/ 43 | *.keystore 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://docs.fastlane.tools/best-practices/source-control/ 51 | 52 | */fastlane/report.xml 53 | */fastlane/Preview.html 54 | */fastlane/screenshots 55 | 56 | # Bundle artifacts 57 | *.jsbundle 58 | 59 | # CocoaPods 60 | /ios/Pods/ 61 | 62 | # Expo 63 | .expo/* 64 | web-build/ 65 | 66 | # # generated by bob 67 | lib/ 68 | 69 | -------------------------------------------------------------------------------- /src/svgs/InfoIcon.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Svg, { Path } from "react-native-svg"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import type { SvgProps } from "../types"; 5 | 6 | export function InfoIcon({ color, size = 24 }: SvgProps): React.ReactElement { 7 | const { colors } = useToastContext(); 8 | const fillColor = color ?? colors.info; 9 | 10 | return ( 11 | 12 | 16 | 20 | 24 | 25 | ); 26 | } 27 | -------------------------------------------------------------------------------- /src/svgs/ErrorIcon.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import Svg, { Path } from "react-native-svg"; 3 | import useToastContext from "../hooks/useToastContext"; 4 | import type { SvgProps } from "../types"; 5 | 6 | export function ErrorIcon({ color, size = 24 }: SvgProps): React.ReactElement { 7 | const { colors } = useToastContext(); 8 | const fillColor = color ?? colors.error; 9 | 10 | return ( 11 | 12 | 16 | 20 | 24 | 25 | ); 26 | } 27 | -------------------------------------------------------------------------------- /src/components/baseStyles.ts: -------------------------------------------------------------------------------- 1 | import { useMemo } from "react"; 2 | import { StyleSheet, ViewStyle } from "react-native"; 3 | import type { Color } from "../types"; 4 | 5 | interface BaseStyles { 6 | base: ViewStyle; 7 | iconContainer: ViewStyle; 8 | icon: ViewStyle; 9 | contentContainer: ViewStyle; 10 | closeButtonContainer: ViewStyle; 11 | closeIcon: ViewStyle; 12 | title: ViewStyle; 13 | message: ViewStyle; 14 | } 15 | 16 | export const useBaseStyles = ( 17 | backgroundColor: Color, 18 | titleColor: Color, 19 | messageColor: Color 20 | ): StyleSheet.NamedStyles => { 21 | return useMemo( 22 | () => 23 | StyleSheet.create({ 24 | base: { 25 | flexDirection: "row", 26 | height: 80, 27 | width: "94%", 28 | borderRadius: 10, 29 | backgroundColor: backgroundColor, 30 | shadowOffset: { width: 0, height: 0 }, 31 | shadowOpacity: 0.1, 32 | shadowRadius: 6, 33 | elevation: 2, 34 | opacity: 0.95, 35 | }, 36 | iconContainer: { 37 | paddingHorizontal: 14, 38 | justifyContent: "center", 39 | alignItems: "center", 40 | }, 41 | icon: { 42 | width: 16, 43 | height: 16, 44 | }, 45 | contentContainer: { 46 | flex: 1, 47 | justifyContent: "center", 48 | alignItems: "flex-start", // in case of rtl the text will start from the right 49 | }, 50 | closeButtonContainer: { 51 | paddingHorizontal: 18, 52 | alignItems: "center", 53 | justifyContent: "center", 54 | color: "#ccc", 55 | }, 56 | closeIcon: { 57 | width: 9, 58 | height: 9, 59 | }, 60 | title: { 61 | fontSize: 14, 62 | fontWeight: "bold", 63 | marginBottom: 3, 64 | color: titleColor, 65 | }, 66 | message: { 67 | fontSize: 14, 68 | color: messageColor, 69 | }, 70 | }), 71 | [messageColor, titleColor, backgroundColor] 72 | ); 73 | }; 74 | -------------------------------------------------------------------------------- /src/components/BaseToast.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { View, TouchableOpacity, Text } from "react-native"; 3 | 4 | import { CloseIcon } from "../svgs"; 5 | import useToastContext from "../hooks/useToastContext"; 6 | import type { BaseToastProps } from "../types"; 7 | import { useBaseStyles } from "./baseStyles"; 8 | 9 | const BaseToast: React.FC = props => { 10 | const { 11 | iconElement, 12 | title, 13 | message, 14 | onClose, 15 | onPress, 16 | renderIcon, 17 | renderTitle, 18 | renderMessage, 19 | renderCloseButton, 20 | } = props; 21 | 22 | const { colors } = useToastContext(); 23 | const styles = useBaseStyles(colors.background, colors.title, colors.message); 24 | const baseStyle = [styles.base]; 25 | 26 | const inner = ( 27 | 28 | {renderIcon ? ( 29 | renderIcon(props) 30 | ) : ( 31 | 32 | {iconElement ? iconElement : } 33 | 34 | )} 35 | 36 | 37 | 38 | {renderTitle 39 | ? renderTitle(props) 40 | : title !== undefined && ( 41 | 42 | 43 | {title} 44 | 45 | 46 | )} 47 | {renderMessage 48 | ? renderMessage(props) 49 | : message !== undefined && ( 50 | 51 | 52 | {message} 53 | 54 | 55 | )} 56 | 57 | 58 | 59 | {renderCloseButton ? ( 60 | renderCloseButton(props) 61 | ) : ( 62 | 63 | 64 | 65 | )} 66 | 67 | ); 68 | 69 | if (onPress) { 70 | return {inner}; 71 | } 72 | 73 | return inner; 74 | }; 75 | 76 | export default BaseToast; 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

Reanimated Toast

3 |
4 | 5 | [![runs with expo](https://img.shields.io/badge/Runs%20with%20Expo-000.svg?style=flat-square&logo=EXPO&labelColor=f3f3f3&logoColor=000)](https://expo.io/) 6 | [![typescript](https://camo.githubusercontent.com/0f9fcc0ac1b8617ad4989364f60f78b2d6b32985ad6a508f215f14d8f897b8d3/68747470733a2f2f62616467656e2e6e65742f62616467652f547970655363726970742f7374726963742532302546302539462539322541412f626c7565)](https://www.typescriptlang.org/) 7 | [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) 8 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) 9 | 10 | ## 🌟 Features 11 | 12 | - High performance with Reanimated v2 13 | - Autocompletion thanks to TypeScript 14 | - Easily customizable 15 | - Runs with Expo 16 | 17 | ## Installation 18 | 19 | > ⚠️ You need to install [react-native-reanimated v2](https://docs.swmansion.com/react-native-reanimated/docs/next/installation) & [react-native-gesture-handler](https://github.com/software-mansion/react-native-gesture-handler) and follow their installation instructions. 20 | 21 | `yarn add react-native-reanimated-toast` 22 | 23 | Wrap the whole app in ToastProvider. 24 | Usually you'd do this in your entry file, such as `index.js` or `App.js`: 25 | 26 | ```js 27 | import * as React from 'react'; 28 | import { ToastProvider } from "react-native-reanimated-toast"; 29 | 30 | export default function App() { 31 | return ( 32 | 33 | {/* Rest of your app code */} 34 | 35 | ); 36 | ``` 37 | 38 | ## Example Usage 39 | 40 | Use the `useToast` hook in any of your function components'. 41 | 42 | ```js 43 | const ToastExample = () => { 44 | const { showToast } = useToast(); 45 | 46 | return ( 47 | 57 | ); 58 | }; 59 | ``` 60 | 61 | ## License 62 | 63 | Licensed under the MIT license. 64 | 65 | --- 66 | 67 | 👉 [View Example App](https://github.com/rrebase/react-native-reanimated-toast/tree/master/example) 68 | -------------------------------------------------------------------------------- /example/App.tsx: -------------------------------------------------------------------------------- 1 | import * as Haptics from "expo-haptics"; 2 | import React from "react"; 3 | import { 4 | Dimensions, 5 | Image, 6 | Platform, 7 | StyleSheet, 8 | Text, 9 | View, 10 | } from "react-native"; 11 | import { TouchableOpacity } from "react-native-gesture-handler"; 12 | import { ToastProvider, useToast } from "react-native-reanimated-toast"; 13 | 14 | const unsplashImgSrc = 15 | "https://images.unsplash.com/photo-1511406361295-0a1ff814c0ce?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=90"; 16 | 17 | const { width, height } = Dimensions.get("window"); 18 | const monospaceFontFamily = Platform.OS === "ios" ? "Courier" : "monospace"; 19 | 20 | const ToastExample = () => { 21 | const { showToast } = useToast(); 22 | 23 | return ( 24 | 25 | 33 | 34 | react-native-reanimated-toast 35 | { 38 | Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light); 39 | showToast({ 40 | title: "Announcement", 41 | message: "It's now possible to see stats 🎉!", 42 | autoHideDuration: 3000, 43 | }); 44 | }} 45 | > 46 | Show Toast 47 | 48 | 49 | 50 | ); 51 | }; 52 | 53 | const App = () => { 54 | // Try adding StrictMode back when Animated is emoved as it seems to come from there 55 | return ( 56 | 57 | 58 | 59 | ); 60 | }; 61 | 62 | export default App; 63 | 64 | const styles = StyleSheet.create({ 65 | white: { color: "#fff" }, 66 | image: { position: "absolute" }, 67 | showToast: { 68 | margin: 16, 69 | padding: 12, 70 | borderRadius: 6, 71 | borderWidth: 1, 72 | borderColor: "#337492", 73 | alignItems: "center", 74 | }, 75 | container: { 76 | flex: 1, 77 | alignItems: "center", 78 | justifyContent: "center", 79 | flexDirection: "column", 80 | backgroundColor: "#212329", 81 | }, 82 | text: { 83 | color: "#fff", 84 | marginTop: 16, 85 | fontFamily: monospaceFontFamily, 86 | }, 87 | }); 88 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-reanimated-toast", 3 | "version": "1.2.0", 4 | "description": "A performant toast with fully configurable options 🚀", 5 | "main": "lib/commonjs/index", 6 | "module": "lib/module/index", 7 | "types": "lib/typescript/index.d.ts", 8 | "react-native": "src/index.ts", 9 | "files": [ 10 | "src", 11 | "lib" 12 | ], 13 | "keywords": [ 14 | "react-native", 15 | "ios", 16 | "android", 17 | "reanimated", 18 | "toast", 19 | "snackbar", 20 | "alert", 21 | "message", 22 | "banner", 23 | "dialog" 24 | ], 25 | "repository": "https://github.com/rrebase/react-native-reanimated-toast", 26 | "author": "Ragnar Rebase", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/rrebase/react-native-reanimated-toast/issues" 30 | }, 31 | "homepage": "https://github.com/rrebase/react-native-reanimated-toast#readme", 32 | "scripts": { 33 | "typescript": "tsc --noEmit", 34 | "lint": "eslint \"**/*.{js,ts,tsx}\"", 35 | "build": "bob build && yarn copy-dts", 36 | "copy-dts": "copyfiles -u 1 \"src/**/*.d.ts\" lib/typescript", 37 | "example": "yarn --cwd example", 38 | "setup": "yarn install && yarn example", 39 | "release": "release-it" 40 | }, 41 | "dependencies": { 42 | "react-native-svg": "^12.1.0" 43 | }, 44 | "devDependencies": { 45 | "@commitlint/cli": "^11.0.0", 46 | "@commitlint/config-conventional": "^11.0.0", 47 | "@react-native-community/bob": "^0.16.2", 48 | "@react-native-community/eslint-config": "^2.0.0", 49 | "@types/react": "^17.0.0", 50 | "@types/react-native": "0.63.37", 51 | "auto-changelog": "^2.2.1", 52 | "copyfiles": "^2.4.1", 53 | "eslint": "^7.15.0", 54 | "eslint-config-prettier": "^6.15.0", 55 | "eslint-plugin-prettier": "^3.2.0", 56 | "husky": "^4.3.5", 57 | "prettier": "^2.2.1", 58 | "react": "~17.0.1", 59 | "react-native": "^0.63.4", 60 | "react-native-gesture-handler": "~1.7.0", 61 | "react-native-reanimated": "2.0.0-alpha.7", 62 | "release-it": "^14.2.2", 63 | "typescript": "^4.1.2" 64 | }, 65 | "peerDependencies": { 66 | "react": "*", 67 | "react-native": "*", 68 | "react-native-gesture-handler": ">=1.7.0", 69 | "react-native-reanimated": ">=2.0.0-alpha.7" 70 | }, 71 | "husky": { 72 | "hooks": { 73 | "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", 74 | "pre-commit": "yarn lint && yarn typescript" 75 | } 76 | }, 77 | "@react-native-community/bob": { 78 | "source": "src", 79 | "output": "lib", 80 | "targets": [ 81 | "commonjs", 82 | "module", 83 | "typescript" 84 | ] 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/contexts/ToastProvider.tsx: -------------------------------------------------------------------------------- 1 | import React, { useCallback, useMemo, useRef, useState } from "react"; 2 | import { Toaster } from "../components/Toaster"; 3 | import type { IToastContext, ToastProps, ToastProviderProps } from "../types"; 4 | import ToastContext from "./ToastContext"; 5 | 6 | const defaultContext: Partial = { 7 | defaults: { 8 | position: "top", 9 | type: "info", 10 | autoHideDuration: 5000, 11 | transitionDuration: { enter: 250, exit: 100 }, 12 | height: 60, 13 | topOffset: 40, 14 | bottomOffset: 40, 15 | }, 16 | colors: { 17 | background: "#fff", 18 | title: "#221D23", 19 | message: "#221D23", 20 | closeIcon: "#221D23", 21 | info: "#00c9ff", 22 | error: "#fe3618", 23 | warning: "#fcc603", 24 | success: "#00ac31", 25 | }, 26 | }; 27 | 28 | const ToastProvider: React.FC = ({ 29 | defaults, 30 | colors = defaultContext.colors, 31 | customToasts, 32 | renderToaster = true, 33 | children, 34 | ...rest 35 | }) => { 36 | const toasts = useRef([]); 37 | const [activeToast, setActiveToast] = useState(null); 38 | 39 | const showToast = useCallback(toast => { 40 | toasts.current.shift(); 41 | toasts.current.unshift(toast); 42 | setActiveToast(toast); 43 | }, []); 44 | 45 | const queueToast = useCallback(toast => { 46 | toasts.current.push(toast); 47 | if (toasts.current.length === 1) { 48 | setActiveToast(toast); 49 | } 50 | }, []); 51 | 52 | const hideToast = useCallback(() => { 53 | toasts.current.shift(); 54 | setActiveToast(toasts.current?.[0] ?? null); 55 | }, []); 56 | 57 | const clearToastQueue = useCallback(() => { 58 | toasts.current.length = 0; // isn't javascript wonderful? /s 59 | setActiveToast(null); 60 | }, []); 61 | 62 | const value = useMemo( 63 | () => ({ 64 | ...defaultContext, 65 | toasts: toasts.current, 66 | activeToast, 67 | showToast, 68 | queueToast, 69 | hideToast, 70 | clearToastQueue, 71 | defaults: { 72 | ...defaultContext.defaults, 73 | ...defaults, 74 | } as IToastContext["defaults"], 75 | colors: { 76 | ...defaultContext.colors, 77 | ...colors, 78 | } as IToastContext["colors"], 79 | customToasts, 80 | }), 81 | [ 82 | activeToast, 83 | clearToastQueue, 84 | colors, 85 | customToasts, 86 | defaults, 87 | hideToast, 88 | queueToast, 89 | showToast, 90 | ] 91 | ); 92 | 93 | return ( 94 | 95 | {children} 96 | {renderToaster && } 97 | 98 | ); 99 | }; 100 | 101 | export default ToastProvider; 102 | -------------------------------------------------------------------------------- /src/types/index.ts: -------------------------------------------------------------------------------- 1 | import { ColorValue } from "react-native"; 2 | import { Color as SvgColor } from "react-native-svg"; 3 | 4 | export interface ToastProps { 5 | /** 6 | * Use predefined types for standard colors/icons, or configure your own types on the provider and use them here. Defaults to 'info' 7 | */ 8 | type?: "info" | "success" | "error" | "warning" | string; // TODO: type-safe the custom config values with generics. (this is hard) 9 | 10 | /** 11 | * Where to show the toasts. Defaults to 'top' 12 | */ 13 | position?: "top" | "bottom"; 14 | 15 | /** 16 | * Bolded top line of text 17 | */ 18 | title?: string; 19 | 20 | /** 21 | * Bottom line of text 22 | */ 23 | message?: string; 24 | 25 | /** 26 | * Number of milliseconds to wait before automatically calling `onHide`. 27 | * Set autoHideDuration to `null` to prevent the toast from automatically hiding. 28 | * 29 | * Default: `5000` 30 | */ 31 | autoHideDuration?: number; 32 | 33 | /** 34 | * Duration of transition in milliseconds. Enter and exit durations can be specified. 35 | * 36 | * Default: `{ enter: 250, exit: 100 }` 37 | */ 38 | transitionDuration?: { enter: number; exit: number }; 39 | 40 | /** 41 | * Height of the toast component. Defaults to 60 42 | */ 43 | height?: number; 44 | 45 | /** 46 | * Points from the top of the screen (when position='top'). Defaults to 40 47 | */ 48 | topOffset?: number; 49 | 50 | /** 51 | * Points from the bottom of the screen (when position='bottom'). Defaults to 40 52 | */ 53 | bottomOffset?: number; 54 | 55 | /** 56 | * Callback when a toast is shown 57 | */ 58 | onShow?: (toast: ToastProps) => void; 59 | 60 | /** 61 | * Callback when a toast is queued 62 | */ 63 | onQueue?: (toast: ToastProps) => void; 64 | 65 | /** 66 | * Callback when a toast is hidden (automatically or by the user) 67 | */ 68 | onHide?: (toast: ToastProps) => void; 69 | 70 | /** 71 | * Callback when a toast is pressed (prevents onHide from firing) 72 | */ 73 | onPress?: (toast: ToastProps) => void; 74 | } 75 | 76 | export interface ToastProviderProps { 77 | /** 78 | * Optionally provide defaults for all toasts (settings here can be overridden when showing or queueing a toast) 79 | */ 80 | defaults?: ToastProps; 81 | /** 82 | * Optionally override default colors 83 | */ 84 | colors?: ToastColors; 85 | /** 86 | * Configure your own toast components to render. Object keys maps to the `ToastProps` `type` prop while the values are render functions to render the toast. 87 | */ 88 | customToasts?: ToastComponentsConfig; 89 | /** 90 | * The Toaster will be rendered for you by default unless you set this to false. 91 | * If you need to render it in a place other than where the provider is placed the useToast hook will give you the Toaster you need to render. 92 | * react-navigation is known to interfere with the rendering of the toasts. Place the Toaster just before your to fix it. 93 | */ 94 | renderToaster?: boolean; 95 | } 96 | 97 | export interface UseToastHook { 98 | /** 99 | * Pops up a toast (jumps to the front of the queue and hides the existing toast if there is one) 100 | */ 101 | showToast: (props: ToastProps) => void; 102 | /** 103 | * Queues up a toast (or shows it instantly if there are no toasts showing and in the queue) 104 | */ 105 | queueToast: (props: ToastProps) => void; 106 | /** 107 | * Hides the current toast, which will show the next toast in the queue if there is one 108 | */ 109 | hideToast: () => void; 110 | /** 111 | * Purges the toast queue (but does not hide the one being shown) 112 | */ 113 | clearToastQueue: () => void; 114 | } 115 | 116 | export interface IToastContext extends ToastContextSettings, UseToastHook { 117 | toasts: Readonly; 118 | activeToast?: ToastProps | null; 119 | } 120 | 121 | type OptionalSettings = Pick< 122 | ToastProps, 123 | "title" | "message" | "onShow" | "onQueue" | "onHide" | "onPress" 124 | >; 125 | 126 | interface ToastContextSettings { 127 | colors: Required; 128 | customToasts?: ToastComponentsConfig; 129 | defaults: Required> & 130 | OptionalSettings; 131 | } 132 | 133 | export type Color = ColorValue & SvgColor; 134 | export interface ToastColors { 135 | /** 136 | * Defaults to #fff 137 | */ 138 | background?: Color; 139 | /** 140 | * Defaults to #221D23 141 | */ 142 | title?: Color; 143 | /** 144 | * Defaults to #221D23 145 | */ 146 | message?: Color; 147 | /** 148 | * Defaults to #221D23 149 | */ 150 | closeIcon?: Color; 151 | /** 152 | * Accent color for the info toast type. Defaults to #00c9ff 153 | */ 154 | info?: Color; 155 | /** 156 | * Accent color for the error toast type. Defaults to #fe3618 157 | */ 158 | error?: Color; 159 | /** 160 | * Accent color for the warning toast type. Defaults to #fcc603 161 | */ 162 | warning?: Color; 163 | /** 164 | * Accent color for the success toast type. Defaults to #00ac31 165 | */ 166 | success?: Color; 167 | } 168 | 169 | export interface ToastComponentsConfig { 170 | [x: string]: (toast: BaseToastProps) => React.ReactElement; 171 | } 172 | 173 | export interface SvgProps { 174 | color?: Color; 175 | size?: number; 176 | } 177 | 178 | export interface BaseToastProps extends ToastProps { 179 | /** 180 | * Color of the left accent and icon 181 | */ 182 | color?: Color; 183 | /** 184 | * Icon to show for the toast. This library does not include any of the icon libraries but has SVG defaults for info, success, warning, error, and close. 185 | * You can use an SVG component like this library does or provide an icon from any library you wish (make sure to size and color it properly). 186 | */ 187 | iconElement?: React.ReactNode; 188 | /** 189 | * The close button should fire this to hide the toast. 190 | */ 191 | onClose: () => void; 192 | /** 193 | * Optional render function to render the icon (and its wrapper). 194 | */ 195 | renderIcon?: (toast: BaseToastProps) => React.ReactNode; 196 | /** 197 | * Optional render function to render the title. 198 | */ 199 | renderTitle?: (toast: BaseToastProps) => React.ReactNode; 200 | /** 201 | * Optional render function to render the message. 202 | */ 203 | renderMessage?: (toast: BaseToastProps) => React.ReactNode; 204 | /** 205 | * Optional render function to render the close button. 206 | */ 207 | renderCloseButton?: (toast: BaseToastProps) => React.ReactNode; 208 | } 209 | 210 | export type Toaster = React.FC; 211 | -------------------------------------------------------------------------------- /src/components/Toaster.tsx: -------------------------------------------------------------------------------- 1 | import React, { 2 | useCallback, 3 | useLayoutEffect, 4 | useMemo, 5 | useRef, 6 | useState, 7 | } from "react"; 8 | import { LayoutChangeEvent } from "react-native"; 9 | import { PanGestureHandler } from "react-native-gesture-handler"; 10 | 11 | import { 12 | useSharedValue, 13 | withSpring, 14 | useAnimatedStyle, 15 | useAnimatedGestureHandler, 16 | interpolate, 17 | default as Reanimated, 18 | withTiming, 19 | } from "react-native-reanimated"; 20 | 21 | import { useInterval } from "../hooks"; 22 | import type { 23 | BaseToastProps, 24 | ToastProps, 25 | ToastComponentsConfig, 26 | } from "../types"; 27 | 28 | import { SuccessToast } from "./SuccessToast"; 29 | import { WarningToast } from "./WarningToast"; 30 | import { ErrorToast } from "./ErrorToast"; 31 | import { InfoToast } from "./InfoToast"; 32 | import useToastContext from "../hooks/useToastContext"; 33 | 34 | const defaultComponentsConfig: ToastComponentsConfig = { 35 | success: (props: BaseToastProps) => , 36 | warning: (props: BaseToastProps) => , 37 | error: (props: BaseToastProps) => , 38 | info: (props: BaseToastProps) => , 39 | }; 40 | 41 | interface GestureContext { 42 | startY?: number; 43 | } 44 | 45 | const ToasterInternal: React.FC = () => { 46 | const { activeToast, defaults, customToasts, hideToast } = useToastContext(); 47 | const translationY = useSharedValue(0); 48 | const [holding, setHolding] = useState(false); 49 | 50 | const toastTypes: ToastComponentsConfig = { 51 | ...defaultComponentsConfig, 52 | ...customToasts, 53 | }; 54 | 55 | const [inProgress, setInProgress] = useState(false); 56 | const [isVisible, setIsVisible] = useState(false); 57 | 58 | const onHide = useMemo(() => activeToast?.onHide ?? defaults.onHide, [ 59 | activeToast?.onHide, 60 | defaults.onHide, 61 | ]); 62 | 63 | const onShow = useMemo(() => activeToast?.onShow ?? defaults.onShow, [ 64 | activeToast?.onShow, 65 | defaults.onShow, 66 | ]); 67 | 68 | const prevHeightRef = useRef(); 69 | const heightRef = useRef( 70 | activeToast?.height ?? defaults.height 71 | ); 72 | const onLayout = useCallback((e: LayoutChangeEvent): void => { 73 | prevHeightRef.current = heightRef.current; 74 | heightRef.current = e.nativeEvent.layout.height; 75 | }, []); 76 | 77 | const topOffset = activeToast?.topOffset ?? defaults.topOffset; 78 | const height = heightRef.current ?? defaults.height; 79 | 80 | const hiddenY = -(height + 5); 81 | const openY = topOffset; 82 | 83 | const gestureHandler = useAnimatedGestureHandler({ 84 | onStart: (_, ctx: GestureContext) => { 85 | ctx.startY = translationY.value; 86 | setHolding(true); 87 | }, 88 | onActive: (event, ctx: GestureContext) => { 89 | translationY.value = Math.min(ctx.startY! + event.translationY, openY); 90 | }, 91 | onEnd: (event, ctx: GestureContext) => { 92 | const newY = ctx.startY! + event.translationY; 93 | if (newY > openY - 12) { 94 | translationY.value = withSpring(openY); 95 | } else { 96 | translationY.value = withTiming(hiddenY, { 97 | // hide the toast faster with higher velocity 98 | duration: interpolate(-event.velocityY, [0, 2000], [500, 0]), 99 | }); 100 | } 101 | setHolding(false); 102 | }, 103 | }); 104 | 105 | const reanimatedStyle = useAnimatedStyle(() => { 106 | return { 107 | position: "absolute", 108 | alignItems: "center", 109 | justifyContent: "center", 110 | left: 0, 111 | right: 0, 112 | top: 0, 113 | transform: [ 114 | { 115 | translateY: translationY.value, 116 | }, 117 | ], 118 | }; 119 | }); 120 | 121 | const prevToastRef = useRef(); 122 | 123 | const transitionDuration = 124 | activeToast?.transitionDuration ?? defaults.transitionDuration; 125 | 126 | useLayoutEffect(() => { 127 | // no toasts or correct one is already shown 128 | if ( 129 | activeToast === prevToastRef.current || 130 | (!activeToast && !prevToastRef.current) 131 | ) { 132 | return; 133 | } 134 | 135 | const hide = async (): Promise => { 136 | setInProgress(true); 137 | 138 | // translationY.value = 0; 139 | translationY.value = withTiming(hiddenY, { 140 | duration: transitionDuration.exit, 141 | }); 142 | setIsVisible(false); 143 | setInProgress(false); 144 | onHide?.(prevToastRef.current as ToastProps); 145 | prevToastRef.current = null; 146 | }; 147 | 148 | const show = async (): Promise => { 149 | if (inProgress || isVisible) { 150 | await hide(); 151 | } 152 | setInProgress(true); 153 | 154 | // translationY.value = 1; 155 | translationY.value = withTiming(openY, { 156 | duration: transitionDuration.enter, 157 | }); 158 | 159 | prevToastRef.current = activeToast; 160 | setIsVisible(true); 161 | setInProgress(false); 162 | onShow?.(activeToast as ToastProps); 163 | }; 164 | 165 | // no toasts left but one is visible and not yet animating 166 | if (!activeToast && isVisible && !inProgress) { 167 | hide(); 168 | } 169 | 170 | // we have a toast that isn't visible and not yet animating 171 | if (activeToast && !isVisible && !inProgress) { 172 | show(); 173 | } 174 | 175 | // activeToast was replaced and the wrong one is showing 176 | if (activeToast && !inProgress && activeToast !== prevToastRef.current) { 177 | show(); 178 | } 179 | // eslint-disable-next-line react-hooks/exhaustive-deps 180 | }, [activeToast, hideToast, inProgress, isVisible, onHide, onShow]); 181 | 182 | // TODO: Can the default be specified in a different manner to avoid this ?? operator 183 | const autoHideDuration = 184 | activeToast?.autoHideDuration ?? defaults.autoHideDuration; 185 | 186 | // this will auto-cancel if inProgress flips to true or a toast is not visible 187 | useInterval( 188 | hideToast, 189 | isVisible && !inProgress && !holding ? autoHideDuration : null 190 | ); 191 | 192 | const toastType = activeToast?.type ?? defaults.type; 193 | const onPress = activeToast?.onPress ?? defaults.onPress; 194 | const onPressCallback = useMemo(() => { 195 | if (!onPress) { 196 | return undefined; 197 | } 198 | 199 | return (toast: ToastProps) => { 200 | hideToast(); 201 | onPress?.(toast); 202 | }; 203 | }, [hideToast, onPress]); 204 | const renderContent = (): React.ReactElement | null => { 205 | const toastComponent = toastTypes[toastType]; 206 | 207 | if (!toastComponent) { 208 | console.error(`Toast of type '${toastType}' does not exist.`); 209 | return null; 210 | } 211 | 212 | return toastComponent({ 213 | ...defaults, 214 | ...activeToast, 215 | onClose: hideToast, 216 | onPress: onPressCallback, 217 | }); 218 | }; 219 | 220 | return ( 221 | 222 | 223 | {renderContent()} 224 | 225 | 226 | ); 227 | }; 228 | 229 | export const Toaster = React.memo(ToasterInternal); 230 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We want this community to be friendly and respectful to each other. Please follow it in all your interactions with the project. 4 | 5 | ## Development workflow 6 | 7 | To get started with the project, run `yarn setup` in the root directory to install the required dependencies for each package: 8 | 9 | ```sh 10 | yarn setup 11 | ``` 12 | 13 | While developing, you can run the [example](https://github.com/rrebase/react-native-reanimated-toast/tree/master/example) to test your changes. 14 | 15 | To start the packager: 16 | 17 | ```sh 18 | yarn example start 19 | ``` 20 | 21 | To run the example app on Android: 22 | 23 | ```sh 24 | yarn example android 25 | ``` 26 | 27 | To run the example app on iOS: 28 | 29 | ```sh 30 | yarn example android 31 | ``` 32 | 33 | Make sure your code passes TypeScript and ESLint. Run the following to verify: 34 | 35 | ```sh 36 | yarn typescript 37 | yarn lint 38 | ``` 39 | 40 | To fix formatting errors, run the following: 41 | 42 | ```sh 43 | yarn lint --fix 44 | ``` 45 | 46 | Remember to add tests for your change if possible. Run the unit tests by: 47 | 48 | ```sh 49 | yarn test 50 | ``` 51 | 52 | ### Commit message convention 53 | 54 | We follow the [conventional commits specification](https://www.conventionalcommits.org/en) for our commit messages: 55 | 56 | - `fix`: bug fixes, e.g. fix crash due to deprecated method. 57 | - `feat`: new features, e.g. add new method to the module. 58 | - `refactor`: code refactor, e.g. migrate from class components to hooks. 59 | - `docs`: changes into documentation, e.g. add usage example for the module.. 60 | - `test`: adding or updating tests, eg add integration tests using detox. 61 | - `chore`: tooling changes, e.g. change CI config. 62 | 63 | Our pre-commit hooks verify that your commit message matches this format when committing. 64 | 65 | ### Linting and tests 66 | 67 | [ESLint](https://eslint.org/), [Prettier](https://prettier.io/), [TypeScript](https://www.typescriptlang.org/) 68 | 69 | We use [TypeScript](https://www.typescriptlang.org/) for type checking, [ESLint](https://eslint.org/) with [Prettier](https://prettier.io/) for linting and formatting the code, and [Jest](https://jestjs.io/) for testing. 70 | 71 | Our pre-commit hooks verify that the linter and tests pass when committing. 72 | 73 | ### Scripts 74 | 75 | The `package.json` file contains various scripts for common tasks: 76 | 77 | - `yarn setup`: setup project by installing all dependencies and pods. 78 | - `yarn typescript`: type-check files with TypeScript. 79 | - `yarn lint`: lint files with ESLint. 80 | - `yarn test`: run unit tests with Jest. 81 | 82 | ### Sending a pull request 83 | 84 | > **Working on your first pull request?** You can learn how from this _free_ series: [How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github). 85 | 86 | When you're sending a pull request: 87 | 88 | - Prefer small pull requests focused on one change. 89 | - Verify that linters and tests are passing. 90 | - Review the documentation to make sure it looks good. 91 | - Follow the pull request template when opening a pull request. 92 | - For pull requests that change the API or implementation, discuss with maintainers first by opening an issue. 93 | 94 | ## Code of Conduct 95 | 96 | ### Our Pledge 97 | 98 | We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 99 | 100 | We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. 101 | 102 | ### Our Standards 103 | 104 | Examples of behavior that contributes to a positive environment for our community include: 105 | 106 | - Demonstrating empathy and kindness toward other people 107 | - Being respectful of differing opinions, viewpoints, and experiences 108 | - Giving and gracefully accepting constructive feedback 109 | - Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience 110 | - Focusing on what is best not just for us as individuals, but for the overall community 111 | 112 | Examples of unacceptable behavior include: 113 | 114 | - The use of sexualized language or imagery, and sexual attention or 115 | advances of any kind 116 | - Trolling, insulting or derogatory comments, and personal or political attacks 117 | - Public or private harassment 118 | - Publishing others' private information, such as a physical or email 119 | address, without their explicit permission 120 | - Other conduct which could reasonably be considered inappropriate in a 121 | professional setting 122 | 123 | ### Enforcement Responsibilities 124 | 125 | Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. 126 | 127 | Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. 128 | 129 | ### Scope 130 | 131 | This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. 132 | 133 | ### Enforcement 134 | 135 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [INSERT CONTACT METHOD]. All complaints will be reviewed and investigated promptly and fairly. 136 | 137 | All community leaders are obligated to respect the privacy and security of the reporter of any incident. 138 | 139 | ### Enforcement Guidelines 140 | 141 | Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: 142 | 143 | #### 1. Correction 144 | 145 | **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. 146 | 147 | **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. 148 | 149 | #### 2. Warning 150 | 151 | **Community Impact**: A violation through a single incident or series of actions. 152 | 153 | **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. 154 | 155 | #### 3. Temporary Ban 156 | 157 | **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. 158 | 159 | **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. 160 | 161 | #### 4. Permanent Ban 162 | 163 | **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. 164 | 165 | **Consequence**: A permanent ban from any sort of public interaction within the community. 166 | 167 | ### Attribution 168 | 169 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, 170 | available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 171 | 172 | Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). 173 | 174 | [homepage]: https://www.contributor-covenant.org 175 | 176 | For answers to common questions about this code of conduct, see the FAQ at 177 | https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. 178 | --------------------------------------------------------------------------------