├── .prettierrc ├── .editorconfig ├── CHANGELOG.md ├── .changeset ├── config.json └── README.md ├── jest.config.js ├── tsconfig.json ├── .github └── workflows │ ├── tests.yml │ └── release.yml ├── src ├── utils.ts └── index.tsx ├── LICENSE ├── rollup.config.js ├── .gitignore ├── .eslintrc.json ├── package.json ├── README.md └── test └── tunnelrat.test.tsx /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": false, 3 | "trailingComma": "es5", 4 | "singleQuote": true, 5 | "tabWidth": 2, 6 | "printWidth": 120 7 | } 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | tab_width = 2 8 | end_of_line = lf 9 | charset = utf-8 10 | trim_trailing_whitespace = false 11 | insert_final_newline = true 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # tunnel-rat 2 | 3 | ## 0.1.2 4 | 5 | ### Patch Changes 6 | 7 | - fix build 8 | 9 | ## 0.1.0 10 | 11 | ### Minor Changes 12 | 13 | - 46ebf4b: Upgrade Zustand to 4.0.0. 14 | - 348274e: Added support for multiple invocations of `In` to inject children into a single `Out`. 15 | -------------------------------------------------------------------------------- /.changeset/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/@changesets/config@2.1.1/schema.json", 3 | "changelog": "@changesets/cli/changelog", 4 | "commit": false, 5 | "fixed": [], 6 | "linked": [], 7 | "access": "public", 8 | "baseBranch": "main", 9 | "updateInternalDependencies": "patch", 10 | "ignore": [] 11 | } 12 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | verbose: true, 3 | preset: 'ts-jest', 4 | testMatch: ['**/?(*.)+(spec|test).+(ts|tsx)'], 5 | testPathIgnorePatterns: ['node_modules'], 6 | testEnvironment: 'jsdom', 7 | moduleFileExtensions: ['js', 'ts', 'tsx'], 8 | globals: { 9 | 'ts-jest': { 10 | isolatedModules: true, 11 | }, 12 | }, 13 | } 14 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "esnext", 4 | "target": "es2018", 5 | "allowSyntheticDefaultImports": true, 6 | "jsx": "react", 7 | "strict": true, 8 | "preserveSymlinks": true, 9 | "moduleResolution": "Node", 10 | "esModuleInterop": true, 11 | "declaration": true, 12 | "declarationDir": "dist", 13 | "skipLibCheck": true, 14 | "removeComments": false, 15 | "baseUrl": "." 16 | }, 17 | "include": ["src"] 18 | } 19 | -------------------------------------------------------------------------------- /.changeset/README.md: -------------------------------------------------------------------------------- 1 | # Changesets 2 | 3 | Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works 4 | with multi-package repos, or single-package repos to help you version and publish your code. You can 5 | find the full documentation for it [in our repository](https://github.com/changesets/changesets) 6 | 7 | We have a quick list of common questions to get you started engaging with this project in 8 | [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) 9 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: 4 | push: 5 | branches: 6 | - 'main' 7 | pull_request: {} 8 | 9 | jobs: 10 | build: 11 | name: Build and Run Tests 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Checkout repo 16 | uses: actions/checkout@v2 17 | 18 | - name: Set up Node 19 | uses: actions/setup-node@v1 20 | with: 21 | node-version: 16 22 | 23 | - name: Set up Yarn cache 24 | uses: c-hive/gha-yarn-cache@v2 25 | 26 | - name: Install dependencies 27 | run: yarn install 28 | 29 | - name: Build packages 30 | run: yarn build 31 | 32 | - name: Test packages 33 | run: yarn test 34 | -------------------------------------------------------------------------------- /src/utils.ts: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | /** 4 | * An SSR-friendly useLayoutEffect. 5 | * 6 | * React currently throws a warning when using useLayoutEffect on the server. 7 | * To get around it, we can conditionally useEffect on the server (no-op) and 8 | * useLayoutEffect elsewhere. 9 | * 10 | * @see https://github.com/facebook/react/issues/14927 11 | */ 12 | export const useIsomorphicLayoutEffect = 13 | typeof window !== 'undefined' && (window.document?.createElement || window.navigator?.product === 'ReactNative') 14 | ? React.useLayoutEffect 15 | : React.useEffect 16 | 17 | export function useMutableCallback(fn: T) { 18 | const ref = React.useRef(fn) 19 | useIsomorphicLayoutEffect(() => void (ref.current = fn), [fn]) 20 | return ref 21 | } 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Poimandres 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 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | concurrency: ${{ github.workflow }}-${{ github.ref }} 9 | 10 | jobs: 11 | release: 12 | name: Stable Release / Version PR 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: Checkout Repo 16 | uses: actions/checkout@v2 17 | with: 18 | # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits 19 | fetch-depth: 0 20 | 21 | - name: Setup Node.js 16.x 22 | uses: actions/setup-node@v2 23 | with: 24 | node-version: 16.x 25 | 26 | - name: Set up Yarn cache 27 | uses: c-hive/gha-yarn-cache@v2 28 | 29 | - name: Install Dependencies 30 | run: yarn 31 | 32 | - name: Create Release Pull Request or Publish to npm 33 | id: changesets 34 | uses: changesets/action@v1 35 | with: 36 | # This expects you to have a script called release which does a build for your packages and calls changeset publish 37 | publish: yarn release 38 | env: 39 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 40 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 41 | # - name: Send a Slack notification if a publish happens 42 | # if: steps.changesets.outputs.published == 'true' 43 | # # You can do something when a publish happens. 44 | # run: my-slack-bot send-notification --message "A new version of ${GITHUB_REPOSITORY} was published!" 45 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import path from 'path' 2 | import babel from '@rollup/plugin-babel' 3 | import resolve from '@rollup/plugin-node-resolve' 4 | 5 | const root = process.platform === 'win32' ? path.resolve('/') : '/' 6 | const external = (id) => !id.startsWith('.') && !id.startsWith(root) 7 | const extensions = ['.js', '.jsx', '.ts', '.tsx', '.json'] 8 | 9 | const getBabelOptions = ({ useESModules }) => ({ 10 | babelrc: false, 11 | extensions, 12 | exclude: '**/node_modules/**', 13 | babelHelpers: 'runtime', 14 | presets: [ 15 | [ 16 | '@babel/preset-env', 17 | { 18 | include: [ 19 | '@babel/plugin-proposal-optional-chaining', 20 | '@babel/plugin-proposal-nullish-coalescing-operator', 21 | '@babel/plugin-proposal-numeric-separator', 22 | '@babel/plugin-proposal-logical-assignment-operators', 23 | ], 24 | bugfixes: true, 25 | loose: true, 26 | modules: false, 27 | targets: '> 1%, not dead, not ie 11, not op_mini all', 28 | }, 29 | ], 30 | '@babel/preset-react', 31 | '@babel/preset-typescript', 32 | ], 33 | plugins: [['@babel/transform-runtime', { regenerator: false, useESModules }]], 34 | }) 35 | 36 | export default [ 37 | { 38 | input: `./src/index.tsx`, 39 | output: { file: `dist/index.js`, format: 'esm' }, 40 | external, 41 | plugins: [babel(getBabelOptions({ useESModules: true })), resolve({ extensions })], 42 | }, 43 | { 44 | input: `./src/index.tsx`, 45 | output: { file: `dist/index.cjs.js`, format: 'cjs' }, 46 | external, 47 | plugins: [babel(getBabelOptions({ useESModules: false })), resolve({ extensions })], 48 | }, 49 | ] 50 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { ReactNode } from 'react' 2 | import { create, StoreApi } from 'zustand' 3 | import { useIsomorphicLayoutEffect } from './utils' 4 | 5 | type Props = { children: React.ReactNode } 6 | 7 | type State = { 8 | current: Array 9 | version: number 10 | set: StoreApi['setState'] 11 | } 12 | 13 | export default function tunnel() { 14 | const useStore = create((set) => ({ 15 | current: new Array(), 16 | version: 0, 17 | set, 18 | })) 19 | 20 | return { 21 | In: ({ children }: Props) => { 22 | const set = useStore((state) => state.set) 23 | const version = useStore((state) => state.version) 24 | 25 | /* When this component mounts, we increase the store's version number. 26 | This will cause all existing rats to re-render (just like if the Out component 27 | were mapping items to a list.) The re-rendering will cause the final 28 | order of rendered components to match what the user is expecting. */ 29 | useIsomorphicLayoutEffect(() => { 30 | set((state) => ({ 31 | version: state.version + 1, 32 | })) 33 | }, []) 34 | 35 | /* Any time the children _or_ the store's version number change, insert 36 | the specified React children into the list of rats. */ 37 | useIsomorphicLayoutEffect(() => { 38 | set(({ current }) => ({ 39 | current: [...current, children], 40 | })) 41 | 42 | return () => 43 | set(({ current }) => ({ 44 | current: current.filter((c) => c !== children), 45 | })) 46 | }, [children, version]) 47 | 48 | return null 49 | }, 50 | 51 | Out: () => { 52 | const current = useStore((state) => state.current) 53 | return <>{current} 54 | }, 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es6": true, 5 | "node": true 6 | }, 7 | "extends": [ 8 | "prettier", 9 | "prettier/react", 10 | "prettier/@typescript-eslint", 11 | "plugin:prettier/recommended", 12 | "plugin:react-hooks/recommended", 13 | "plugin:import/errors", 14 | "plugin:import/warnings", 15 | "prettier", 16 | "prettier/react", 17 | "prettier/@typescript-eslint" 18 | ], 19 | "plugins": ["@typescript-eslint", "react", "react-hooks", "import", "jest", "prettier"], 20 | "parser": "@typescript-eslint/parser", 21 | "parserOptions": { 22 | "ecmaFeatures": { 23 | "jsx": true 24 | }, 25 | "ecmaVersion": 2018, 26 | "sourceType": "module", 27 | "rules": { 28 | "curly": ["warn", "multi-line", "consistent"], 29 | "no-console": "off", 30 | "no-empty-pattern": "warn", 31 | "no-duplicate-imports": "error", 32 | "import/no-unresolved": "off", 33 | "import/export": "error", 34 | // https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/FAQ.md#eslint-plugin-import 35 | // We recommend you do not use the following import/* rules, as TypeScript provides the same checks as part of standard type checking: 36 | "import/named": "off", 37 | "import/namespace": "off", 38 | "import/default": "off", 39 | "no-unused-vars": ["warn", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }], 40 | "@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }], 41 | "@typescript-eslint/no-use-before-define": "off", 42 | "@typescript-eslint/no-empty-function": "off", 43 | "@typescript-eslint/no-empty-interface": "off", 44 | "@typescript-eslint/no-explicit-any": "off", 45 | "jest/consistent-test-it": ["error", { "fn": "it", "withinDescribe": "it" }] 46 | } 47 | }, 48 | "settings": { 49 | "react": { 50 | "version": "detect" 51 | }, 52 | "import/extensions": [".js", ".jsx", ".ts", ".tsx"], 53 | "import/parsers": { 54 | "@typescript-eslint/parser": [".js", ".jsx", ".ts", ".tsx"] 55 | }, 56 | "import/resolver": { 57 | "node": { 58 | "extensions": [".js", ".jsx", ".ts", ".tsx", ".json"], 59 | "paths": ["src"] 60 | }, 61 | "alias": { 62 | "extensions": [".js", ".jsx", ".ts", ".tsx", ".json"], 63 | "map": [["react-three-fiber", "./src/targets/web.tsx"]] 64 | } 65 | } 66 | }, 67 | "overrides": [ 68 | { 69 | "files": ["src"], 70 | "parserOptions": { 71 | "project": "./tsconfig.json" 72 | } 73 | } 74 | ] 75 | } 76 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tunnel-rat", 3 | "version": "0.1.2", 4 | "description": "non gratum anus rodentum", 5 | "main": "dist/index.cjs", 6 | "module": "dist/index.js", 7 | "types": "dist/index.d.ts", 8 | "sideEffects": false, 9 | "scripts": { 10 | "build": "rollup -c", 11 | "postbuild": "tsc --emitDeclarationOnly", 12 | "prepublishOnly": "npm run build", 13 | "test": "jest", 14 | "ci": "yarn build && yarn test", 15 | "release": "yarn ci && yarn changeset publish" 16 | }, 17 | "husky": { 18 | "hooks": { 19 | "pre-commit": "lint-staged" 20 | } 21 | }, 22 | "lint-staged": { 23 | "*.{js,jsx,ts,tsx}": [ 24 | "eslint --fix" 25 | ] 26 | }, 27 | "repository": { 28 | "type": "git", 29 | "url": "git+https://github.com/pmndrs/tunnel-rat.git" 30 | }, 31 | "keywords": [ 32 | "react", 33 | "portal", 34 | "tunnel" 35 | ], 36 | "author": "Paul Henschel", 37 | "license": "MIT", 38 | "bugs": { 39 | "url": "https://github.com/pmndrs/tunnel-rat/issues" 40 | }, 41 | "devDependencies": { 42 | "@babel/core": "7.16.0", 43 | "@babel/plugin-proposal-class-properties": "^7.16.0", 44 | "@babel/plugin-transform-modules-commonjs": "7.16.0", 45 | "@babel/plugin-transform-parameters": "7.16.0", 46 | "@babel/plugin-transform-runtime": "7.16.0", 47 | "@babel/plugin-transform-template-literals": "7.16.0", 48 | "@babel/preset-env": "7.16.0", 49 | "@babel/preset-react": "7.16.0", 50 | "@babel/preset-typescript": "^7.16.0", 51 | "@changesets/cli": "^2.24.3", 52 | "@rollup/plugin-babel": "^5.3.0", 53 | "@rollup/plugin-node-resolve": "^13.0.6", 54 | "@testing-library/jest-dom": "^5.16.5", 55 | "@testing-library/react": "^13.3.0", 56 | "@types/jest": "^27.0.2", 57 | "@types/node": "^16.11.6", 58 | "@types/react": "^18.0.17", 59 | "@types/react-dom": "^18.0.6", 60 | "@types/react-test-renderer": "^18.0.0", 61 | "@typescript-eslint/eslint-plugin": "^5.3.0", 62 | "@typescript-eslint/parser": "^5.3.0", 63 | "eslint": "^8.1.0", 64 | "eslint-config-prettier": "^8.3.0", 65 | "eslint-import-resolver-alias": "^1.1.2", 66 | "eslint-plugin-import": "^2.25.2", 67 | "eslint-plugin-jest": "^25.2.2", 68 | "eslint-plugin-prettier": "^4.0.0", 69 | "eslint-plugin-react": "^7.26.1", 70 | "eslint-plugin-react-hooks": "^4.2.0", 71 | "husky": "^7.0.4", 72 | "jest": "^28.1.3", 73 | "jest-environment-jsdom": "^28.1.3", 74 | "lint-staged": "^11.2.6", 75 | "prettier": "^2.4.1", 76 | "react": "^18.2.0", 77 | "react-dom": "^18.2.0", 78 | "rollup": "^2.59.0", 79 | "rollup-plugin-size-snapshot": "^0.12.0", 80 | "rollup-plugin-terser": "^7.0.2", 81 | "ts-jest": "^28.0.8", 82 | "typescript": "^4.4.4" 83 | }, 84 | "homepage": "https://github.com/pmndrs/tunnel-rat#readme", 85 | "dependencies": { 86 | "zustand": "^4.3.2" 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Tunnel Rat 3 |

4 | 5 | [![Version](https://img.shields.io/npm/v/tunnel-rat?style=for-the-badge)](https://www.npmjs.com/package/tunnel-rat) 6 | [![Downloads](https://img.shields.io/npm/dt/tunnel-rat.svg?style=for-the-badge)](https://www.npmjs.com/package/tunnel-rat) 7 | [![Bundle Size](https://img.shields.io/bundlephobia/min/tunnel-rat?label=bundle%20size&style=for-the-badge)](https://bundlephobia.com/result?p=tunnel-rat) 8 | 9 | ## Tunnel Rat 10 | 11 | - Digs tunnels for React elements to **go in** and **appear somewhere else**! 12 | - Works across **separate renderers** – use it to easily **render HTML elements from within your @react-three/fiber application**! 13 | - Squeak! 🐀 14 | 15 | ## Examples & Sandboxes 16 | 17 | - https://codesandbox.io/s/basic-demo-forked-kxq8g 18 | - https://codesandbox.io/s/tunnel-rat-demo-ceupre 19 | 20 | ## Usage 21 | 22 | Create a tunnel: 23 | 24 | ```tsx 25 | import tunnel from 'tunnel-rat' 26 | const t = tunnel() 27 | ``` 28 | 29 | Use the tunnel's `In` component to send one or more elements into the tunnel: 30 | 31 | ```tsx 32 | 33 |

Very cool!

34 |

These will appear somewhere else!

35 |
36 | ``` 37 | 38 | Somewhere else, use the tunnel's `Out` component to render them: 39 | 40 | ```tsx 41 | 42 | ``` 43 | 44 | ## Examples 45 | 46 | This example describes a simple React app that has both a HTML UI as well as a @react-three/fiber 3D scene. Each of these is rendered using separate React renderers, which traditionally makes emitting HTML from within the Canvas a bit of a pain; but thanks to tunnel-rat, this is now super easy! 47 | 48 | ```jsx 49 | import { Canvas } from '@react-three/fiber' 50 | import tunnel from 'tunnel-rat' 51 | 52 | /* Create a tunnel. */ 53 | const ui = tunnel() 54 | 55 | const App = () => ( 56 |
57 |
58 | {/* Anything that goes into the tunnel, we want to render here. */} 59 | 60 |
61 | 62 | {/* Here we're entering the part of the app that is driven by 63 | @react-three/fiber, where all children of the component 64 | are rendered by an entirely separate React renderer, which would 65 | typically not allow the use of HTML tags. */} 66 | 67 | {/* Let's send something into the tunnel! */} 68 | 69 |

Hi, I'm a cube!

70 |
71 | 72 | 73 | 74 | 75 | 76 | 77 | {/* You can send multiple things through the tunnel, and 78 | they will all show up in the order that you've defined them in! */} 79 | 80 |

And I'm a sphere!

81 |
82 | 83 | 84 | 85 | 86 | 87 |
88 |
89 | ) 90 | ``` 91 | 92 | Of course, the whole thing also works the other way around: 93 | 94 | ```jsx 95 | import { Canvas } from '@react-three/fiber' 96 | import tunnel from 'tunnel-rat' 97 | 98 | /* Create a tunnel. */ 99 | const three = tunnel() 100 | 101 | const App = () => ( 102 |
103 |
104 | {/* Let's beam something into the R3F Canvas! */} 105 | 106 | 107 | 108 | 109 | 110 | 111 |
112 | 113 | 114 | {/* Render anything sent through the tunnel! */} 115 | 116 | 117 |
118 | ) 119 | ``` 120 | 121 | ## Gotchas 122 | 123 | If you have multiple in's React may loose track of object order and even mismatch objects, especially if the root elements are of the same type. Make sure you use keys, always treat multiple in's as a list! 124 | 125 | ```jsx 126 | 127 |

foo

128 |
129 | ... 130 | 131 | 132 |

bar

133 |
134 | ``` 135 | -------------------------------------------------------------------------------- /test/tunnelrat.test.tsx: -------------------------------------------------------------------------------- 1 | import '@testing-library/jest-dom' 2 | import { fireEvent, render, screen } from '@testing-library/react' 3 | import React from 'react' 4 | import tunnel from '../src' 5 | 6 | describe('tunnelrat', () => { 7 | it('transports the children of In into Out', () => { 8 | const t = tunnel() 9 | 10 | const Outlet = () => ( 11 |
    12 | 13 |
14 | ) 15 | 16 | const Inlets = () => ( 17 |
18 | 19 |
  • One
  • 20 |
    21 |
    22 | ) 23 | 24 | const { container } = render( 25 | <> 26 | 27 | 28 | 29 | ) 30 | 31 | expect(container).toMatchInlineSnapshot(` 32 |
    33 |
      34 |
    • 35 | One 36 |
    • 37 |
    38 |
    39 |
    40 | `) 41 | }) 42 | 43 | it('can handle multiple children', () => { 44 | const t = tunnel() 45 | 46 | const Outlet = () => ( 47 |
      48 | 49 |
    50 | ) 51 | 52 | const Inlets = () => ( 53 |
    54 | 55 |
  • One
  • 56 |
    57 | 58 |
  • Two
  • 59 |
    60 |
    61 | ) 62 | 63 | const { container } = render( 64 | <> 65 | 66 | 67 | 68 | ) 69 | 70 | expect(container).toMatchInlineSnapshot(` 71 |
    72 |
      73 |
    • 74 | One 75 |
    • 76 |
    • 77 | Two 78 |
    • 79 |
    80 |
    81 |
    82 | `) 83 | }) 84 | 85 | it('retains the expected order of multiple children after un- and remounts', () => { 86 | const t = tunnel() 87 | 88 | const Rat = ({ name }: { name: string }) => { 89 | const [visible, setVisible] = React.useState(true) 90 | 91 | return ( 92 |
    93 | 94 | {visible ? ( 95 | 96 |
  • {name}
  • 97 |
    98 | ) : null} 99 |
    100 | ) 101 | } 102 | 103 | const Outlet = () => ( 104 |
      105 | 106 |
    107 | ) 108 | 109 | const Inlets = () => ( 110 |
    111 | 112 | 113 | 114 |
    115 | ) 116 | 117 | const { container } = render( 118 | <> 119 | 120 | 121 | 122 | ) 123 | 124 | expect(container).toMatchInlineSnapshot(` 125 |
    126 |
      127 |
    • 128 | One 129 |
    • 130 |
    • 131 | Two 132 |
    • 133 |
    • 134 | Three 135 |
    • 136 |
    137 |
    138 |
    139 | 143 |
    144 |
    145 | 149 |
    150 |
    151 | 155 |
    156 |
    157 |
    158 | `) 159 | 160 | /* Remove the middle rat */ 161 | fireEvent.click(screen.getByText('Toggle Two')) 162 | 163 | expect(container).toMatchInlineSnapshot(` 164 |
    165 |
      166 |
    • 167 | One 168 |
    • 169 |
    • 170 | Three 171 |
    • 172 |
    173 |
    174 |
    175 | 179 |
    180 |
    181 | 185 |
    186 |
    187 | 191 |
    192 |
    193 |
    194 | `) 195 | 196 | /* Re-add it */ 197 | fireEvent.click(screen.getByText('Toggle Two')) 198 | 199 | /* The "Two" rat gets re-added, and at the top of the list. */ 200 | expect(container).toMatchInlineSnapshot(` 201 |
    202 |
      203 |
    • 204 | One 205 |
    • 206 |
    • 207 | Two 208 |
    • 209 |
    • 210 | Three 211 |
    • 212 |
    213 |
    214 |
    215 | 219 |
    220 |
    221 | 225 |
    226 |
    227 | 231 |
    232 |
    233 |
    234 | `) 235 | }) 236 | }) 237 | --------------------------------------------------------------------------------