├── .editorconfig ├── .gitignore ├── .travis.yml ├── README.md ├── example ├── .gitignore ├── README.md ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json ├── src │ ├── App.css │ ├── App.test.tsx │ ├── App.tsx │ ├── index.css │ ├── index.tsx │ ├── logo.svg │ ├── react-app-env.d.ts │ └── serviceWorker.ts ├── tsconfig.json └── yarn.lock ├── package.json ├── rollup.config.js ├── src ├── index.tsx ├── react-app-env.d.ts └── test.ts ├── tsconfig.json ├── tsconfig.test.json └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # See https://help.github.com/ignore-files/ for more about ignoring files. 3 | 4 | # dependencies 5 | node_modules 6 | 7 | # builds 8 | build 9 | dist 10 | .rpt2_cache 11 | 12 | # misc 13 | .DS_Store 14 | .env 15 | .env.local 16 | .env.development.local 17 | .env.test.local 18 | .env.production.local 19 | 20 | npm-debug.log* 21 | yarn-debug.log* 22 | yarn-error.log* 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 9 4 | - 8 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # use-3d-effect 2 | 3 | > 4 | 5 | [![NPM](https://img.shields.io/npm/v/use-3d-effect.svg)](https://www.npmjs.com/package/use-3d-effect) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) 6 | 7 | ![screencast](https://media.giphy.com/media/fLp5ARL1xMwa7XTYQ4/giphy.gif) 8 | 9 | [Demo](https://hermanya.github.io/use-3d-effect) 10 | 11 | ## Install 12 | 13 | ```bash 14 | npm install --save use-3d-effect 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```tsx 20 | import * as React from 'react' 21 | import { animated } from 'react-spring'; 22 | import { use3dEffect } from 'use-3d-effect'; 23 | 24 | const Example = () => { 25 | const ref = React.useRef(null); 26 | const {style, ...mouseHandlers} = use3dEffect(ref); 27 | 28 | return ( 29 | 37 | Hover over me! 38 | 39 | ); 40 | }; 41 | ``` 42 | 43 | ## License 44 | 45 | MIT © [Hermanya](https://github.com/Hermanya) 46 | 47 | --- 48 | 49 | This hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook). 50 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "homepage": "https://Hermanya.github.io/use-3d-effect", 4 | "version": "0.1.0", 5 | "private": true, 6 | "dependencies": { 7 | "@types/jest": "24.0.11", 8 | "@types/node": "11.11.0", 9 | "@types/react": "16.8.7", 10 | "@types/react-dom": "16.8.2", 11 | "@types/react-syntax-highlighter": "^10.1.0", 12 | "react": "link:../node_modules/react", 13 | "react-dom": "^16.8.4", 14 | "react-scripts": "2.1.8", 15 | "react-syntax-highlighter": "^10.2.0", 16 | "typescript": "3.3.3333", 17 | "use-3d-effect": "link:.." 18 | }, 19 | "scripts": { 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject", 24 | "predeploy": "yarn build", 25 | "deploy": "touch build/.nojekyll && gh-pages -d build" 26 | }, 27 | "devDependencies": { 28 | "@babel/core": "^7.2.2", 29 | "@babel/runtime": "^7.3.1", 30 | "@typescript-eslint/eslint-plugin": "^1.4.2", 31 | "cross-env": "^5.2.0", 32 | "eslint-config-prettier": "^4.1.0", 33 | "eslint-plugin-prettier": "^3.0.1", 34 | "gh-pages": "^2.0.1", 35 | "prettier": "^1.16.4" 36 | }, 37 | "eslintConfig": { 38 | "parser": "@typescript-eslint/parser", 39 | "extends": [ 40 | "plugin:@typescript-eslint/recommended", 41 | "react-app", 42 | "prettier", 43 | "prettier/@typescript-eslint" 44 | ], 45 | "plugins": [ 46 | "@typescript-eslint", 47 | "react", 48 | "prettier" 49 | ], 50 | "rules": { 51 | "prettier/prettier": "error" 52 | } 53 | }, 54 | "browserslist": [ 55 | ">0.2%", 56 | "not dead", 57 | "not ie <= 11", 58 | "not op_mini all" 59 | ] 60 | } 61 | -------------------------------------------------------------------------------- /example/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hermanya/use-3d-effect/962bd9a62a07e8a6a4eb05b0d0f25949797ad3c4/example/public/favicon.ico -------------------------------------------------------------------------------- /example/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | 11 | 15 | 16 | 25 | React App 26 | 27 | 28 | 29 |
30 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /example/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /example/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | font-size: 1rem; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 0.5em; 8 | pointer-events: none; 9 | } 10 | 11 | .App-header { 12 | background-color: #282c34; 13 | min-height: 50vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | font-size: calc(10px + 2vmin); 19 | color: white; 20 | } 21 | 22 | .App-body { 23 | max-width: 80em; 24 | margin: auto; 25 | display: flex; 26 | flex-direction: column; 27 | align-items: center; 28 | justify-content: center; 29 | /* font-size: calc(10px + 2vmin); */ 30 | color: black; 31 | } 32 | 33 | .App-footer { 34 | display: flex; 35 | justify-content: center; 36 | margin: 1em 0; 37 | font-size: 1rem; 38 | } 39 | 40 | .App-footer .App-link:not(:last-child) { 41 | margin-right: 1em; 42 | } 43 | .App-link { 44 | color: #61dafb; 45 | } 46 | 47 | .App-code { 48 | display: block; 49 | margin-top: 1em; 50 | font-size: 1rem; 51 | } 52 | 53 | @keyframes App-logo-spin { 54 | from { 55 | transform: rotate(0deg); 56 | } 57 | to { 58 | transform: rotate(360deg); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /example/src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import App from "./App"; 4 | 5 | it("renders without crashing", () => { 6 | const div = document.createElement("div"); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /example/src/App.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import logo from "./logo.svg"; 3 | import "./App.css"; 4 | import { animated } from "react-spring"; 5 | import { use3dEffect } from "use-3d-effect"; 6 | import SyntaxHighlighter from "react-syntax-highlighter"; 7 | 8 | const example = ` 9 | import { animated } from 'react-spring'; 10 | import { use3dEffect } from 'use-3d-effect'; 11 | 12 | const Example = () => { 13 | const ref = React.useRef(null); 14 | const {style, ...mouseHandlers} = use3dEffect(ref); 15 | 16 | return ( 17 | 25 | Hover over me! 26 | 27 | ); 28 | }; 29 | `; 30 | const App: React.FunctionComponent<{}> = (): JSX.Element => { 31 | const ref = React.useRef(null); 32 | const { style, ...mouseHandlers } = use3dEffect(ref); 33 | 34 | return ( 35 |
36 |
37 |

38 | logo use-3d-effect 39 |

40 |

React hook for the 3D tilt card effect.

41 | 51 | Hover over me! 52 | 53 | 70 |
71 |
72 | 73 | yarn add use-3d-effect{" "} 74 | react-spring 75 | 76 | 84 | {example} 85 | 86 |
87 |
88 | ); 89 | }; 90 | 91 | export default App; 92 | -------------------------------------------------------------------------------- /example/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 5 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 6 | sans-serif; 7 | -webkit-font-smoothing: antialiased; 8 | -moz-osx-font-smoothing: grayscale; 9 | } 10 | 11 | code { 12 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 13 | monospace; 14 | } 15 | -------------------------------------------------------------------------------- /example/src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /example/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /example/src/serviceWorker.ts: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | type Config = { 24 | onSuccess?: (registration: ServiceWorkerRegistration) => void; 25 | onUpdate?: (registration: ServiceWorkerRegistration) => void; 26 | }; 27 | 28 | export function register(config?: Config) { 29 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 30 | // The URL constructor is available in all browsers that support SW. 31 | const publicUrl = new URL( 32 | (process as { env: { [key: string]: string } }).env.PUBLIC_URL, 33 | window.location.href 34 | ); 35 | if (publicUrl.origin !== window.location.origin) { 36 | // Our service worker won't work if PUBLIC_URL is on a different origin 37 | // from what our page is served on. This might happen if a CDN is used to 38 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 39 | return; 40 | } 41 | 42 | window.addEventListener('load', () => { 43 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 44 | 45 | if (isLocalhost) { 46 | // This is running on localhost. Let's check if a service worker still exists or not. 47 | checkValidServiceWorker(swUrl, config); 48 | 49 | // Add some additional logging to localhost, pointing developers to the 50 | // service worker/PWA documentation. 51 | navigator.serviceWorker.ready.then(() => { 52 | console.log( 53 | 'This web app is being served cache-first by a service ' + 54 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 55 | ); 56 | }); 57 | } else { 58 | // Is not localhost. Just register service worker 59 | registerValidSW(swUrl, config); 60 | } 61 | }); 62 | } 63 | } 64 | 65 | function registerValidSW(swUrl: string, config?: Config) { 66 | navigator.serviceWorker 67 | .register(swUrl) 68 | .then(registration => { 69 | registration.onupdatefound = () => { 70 | const installingWorker = registration.installing; 71 | if (installingWorker == null) { 72 | return; 73 | } 74 | installingWorker.onstatechange = () => { 75 | if (installingWorker.state === 'installed') { 76 | if (navigator.serviceWorker.controller) { 77 | // At this point, the updated precached content has been fetched, 78 | // but the previous service worker will still serve the older 79 | // content until all client tabs are closed. 80 | console.log( 81 | 'New content is available and will be used when all ' + 82 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 83 | ); 84 | 85 | // Execute callback 86 | if (config && config.onUpdate) { 87 | config.onUpdate(registration); 88 | } 89 | } else { 90 | // At this point, everything has been precached. 91 | // It's the perfect time to display a 92 | // "Content is cached for offline use." message. 93 | console.log('Content is cached for offline use.'); 94 | 95 | // Execute callback 96 | if (config && config.onSuccess) { 97 | config.onSuccess(registration); 98 | } 99 | } 100 | } 101 | }; 102 | }; 103 | }) 104 | .catch(error => { 105 | console.error('Error during service worker registration:', error); 106 | }); 107 | } 108 | 109 | function checkValidServiceWorker(swUrl: string, config?: Config) { 110 | // Check if the service worker can be found. If it can't reload the page. 111 | fetch(swUrl) 112 | .then(response => { 113 | // Ensure service worker exists, and that we really are getting a JS file. 114 | const contentType = response.headers.get('content-type'); 115 | if ( 116 | response.status === 404 || 117 | (contentType != null && contentType.indexOf('javascript') === -1) 118 | ) { 119 | // No service worker found. Probably a different app. Reload the page. 120 | navigator.serviceWorker.ready.then(registration => { 121 | registration.unregister().then(() => { 122 | window.location.reload(); 123 | }); 124 | }); 125 | } else { 126 | // Service worker found. Proceed as normal. 127 | registerValidSW(swUrl, config); 128 | } 129 | }) 130 | .catch(() => { 131 | console.log( 132 | 'No internet connection found. App is running in offline mode.' 133 | ); 134 | }); 135 | } 136 | 137 | export function unregister() { 138 | if ('serviceWorker' in navigator) { 139 | navigator.serviceWorker.ready.then(registration => { 140 | registration.unregister(); 141 | }); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "module": "esnext", 16 | "moduleResolution": "node", 17 | "resolveJsonModule": true, 18 | "isolatedModules": true, 19 | "noEmit": true, 20 | "jsx": "preserve" 21 | }, 22 | "include": [ 23 | "src" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "use-3d-effect", 3 | "version": "1.1.0", 4 | "description": "React hook for 3D tilt card effect", 5 | "author": "Hermanya", 6 | "license": "MIT", 7 | "repository": "Hermanya/use-3d-effect", 8 | "main": "dist/index.js", 9 | "module": "dist/index.es.js", 10 | "jsnext:main": "dist/index.es.js", 11 | "engines": { 12 | "node": ">=8", 13 | "npm": ">=5" 14 | }, 15 | "scripts": { 16 | "test": "cross-env CI=1 react-scripts test --env=jsdom", 17 | "test:watch": "react-scripts test --env=jsdom", 18 | "build": "rollup -c", 19 | "start": "rollup -c -w", 20 | "prepare": "yarn run build", 21 | "predeploy": "cd example && yarn install && yarn run build", 22 | "deploy": "gh-pages -d example/build" 23 | }, 24 | "peerDependencies": { 25 | "react": "^16.8.0", 26 | "react-spring": "^8.0.14" 27 | }, 28 | "dependencies": { 29 | "resize-observer-polyfill": "^1.5.1" 30 | }, 31 | "devDependencies": { 32 | "@babel/core": "^7.2.2", 33 | "@babel/runtime": "^7.3.1", 34 | "@types/jest": "^23.3.13", 35 | "@types/react": "^16.8.7", 36 | "@typescript-eslint/eslint-plugin": "^1.4.2", 37 | "cross-env": "^5.2.0", 38 | "eslint-config-prettier": "^4.1.0", 39 | "eslint-plugin-prettier": "^3.0.1", 40 | "gh-pages": "^2.0.1", 41 | "prettier": "^1.16.4", 42 | "react": "^16.8.0", 43 | "react-scripts": "^2.1.3", 44 | "react-spring": "^8.0.14", 45 | "rollup": "^1.1.2", 46 | "rollup-plugin-babel": "^4.3.2", 47 | "rollup-plugin-commonjs": "^9.2.0", 48 | "rollup-plugin-node-resolve": "^4.0.0", 49 | "rollup-plugin-peer-deps-external": "^2.2.0", 50 | "rollup-plugin-typescript2": "^0.19.2", 51 | "rollup-plugin-url": "^2.1.0", 52 | "typescript": "^3.2.4" 53 | }, 54 | "eslintConfig": { 55 | "parser": "@typescript-eslint/parser", 56 | "extends": [ 57 | "plugin:@typescript-eslint/recommended", 58 | "react-app", 59 | "prettier", 60 | "prettier/@typescript-eslint" 61 | ], 62 | "plugins": [ 63 | "@typescript-eslint", 64 | "react", 65 | "prettier" 66 | ], 67 | "rules": { 68 | "prettier/prettier": "error" 69 | } 70 | }, 71 | "files": [ 72 | "dist" 73 | ] 74 | } 75 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import typescript from 'rollup-plugin-typescript2' 2 | import commonjs from 'rollup-plugin-commonjs' 3 | import external from 'rollup-plugin-peer-deps-external' 4 | import resolve from 'rollup-plugin-node-resolve' 5 | import url from 'rollup-plugin-url' 6 | 7 | import pkg from './package.json' 8 | 9 | export default { 10 | input: 'src/index.tsx', 11 | output: [ 12 | { 13 | file: pkg.main, 14 | format: 'cjs', 15 | exports: 'named', 16 | sourcemap: true 17 | }, 18 | { 19 | file: pkg.module, 20 | format: 'es', 21 | exports: 'named', 22 | sourcemap: true 23 | } 24 | ], 25 | plugins: [ 26 | external(), 27 | url({ exclude: ['**/*.svg'] }), 28 | resolve(), 29 | typescript({ 30 | rollupCommonJSResolveHack: true, 31 | clean: true 32 | }), 33 | commonjs() 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import { useSpring } from "react-spring"; 2 | import { useState, useEffect } from "react"; 3 | import ResizeObserver from "resize-observer-polyfill"; 4 | 5 | interface Bounds { 6 | left: number; 7 | top: number; 8 | width: number; 9 | height: number; 10 | } 11 | const initialBounds: Bounds = { left: 0, top: 0, width: 0, height: 0 }; 12 | function useBoundingClientRect(ref: React.RefObject): Bounds { 13 | const [bounds, set] = useState(initialBounds); 14 | const [ro] = useState( 15 | () => 16 | new ResizeObserver(([entry]: ResizeObserverEntry[]) => 17 | set(entry.target.getBoundingClientRect()) 18 | ) 19 | ); 20 | useEffect(() => { 21 | if (ref.current) ro.observe(ref.current); 22 | return () => ro.disconnect(); 23 | }, []); 24 | return bounds; 25 | } 26 | 27 | export const use3dEffect = ( 28 | ref: React.RefObject 29 | ): { 30 | style: React.CSSProperties; 31 | onMouseLeave: () => void; 32 | onMouseEnter: (event: React.MouseEvent) => void; 33 | } => { 34 | const [props, set] = useSpring(() => ({ 35 | xys: [0, 0, 1], 36 | config: { mass: 5, tension: 350, friction: 40 } 37 | })); 38 | const { top, left, width, height } = useBoundingClientRect(ref); 39 | 40 | const calc = (x: number, y: number): number[] => [ 41 | -((top + height / 2 - y) / (height / 2)) * 10, 42 | -((left + width / 2 - x) / (width / 2)) * 10, 43 | 1.1 44 | ]; 45 | const trans = (x: number, y: number, s: number): string => 46 | `perspective(1000px) rotateX(${x}deg) rotateY(${-y}deg) scale(${s})`; 47 | 48 | return { 49 | style: { 50 | // @ts-ignore 51 | transform: props.xys.interpolate(trans) 52 | }, 53 | onMouseLeave: () => set({ xys: [0, 0, 1] }), 54 | // @ts-ignore 55 | onMouseMove: ({ pageX: x, pageY: y }: React.MouseEvent) => 56 | set({ xys: calc(x, y) }) 57 | }; 58 | }; 59 | -------------------------------------------------------------------------------- /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | import { use3dEffect } from "./"; 2 | 3 | describe("use3dEffect", () => { 4 | it("is truthy", () => { 5 | expect(use3dEffect).toBeTruthy(); 6 | }); 7 | }); 8 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "build", 4 | "module": "esnext", 5 | "target": "es5", 6 | "lib": [ 7 | "es6", 8 | "dom", 9 | "es2016", 10 | "es2017" 11 | ], 12 | "sourceMap": true, 13 | "allowJs": false, 14 | "jsx": "preserve", 15 | "declaration": true, 16 | "moduleResolution": "node", 17 | "forceConsistentCasingInFileNames": true, 18 | "noImplicitReturns": true, 19 | "noImplicitThis": true, 20 | "noImplicitAny": true, 21 | "strictNullChecks": true, 22 | "suppressImplicitAnyIndexErrors": true, 23 | "noUnusedLocals": true, 24 | "noUnusedParameters": true, 25 | "skipLibCheck": true, 26 | "esModuleInterop": true, 27 | "allowSyntheticDefaultImports": true, 28 | "strict": true, 29 | "resolveJsonModule": true, 30 | "isolatedModules": false, 31 | "noEmit": true 32 | }, 33 | "include": [ 34 | "src" 35 | ], 36 | "exclude": [ 37 | "node_modules", 38 | "build", 39 | "dist", 40 | "example", 41 | "rollup.config.js" 42 | ] 43 | } 44 | -------------------------------------------------------------------------------- /tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "module": "commonjs" 5 | } 6 | } --------------------------------------------------------------------------------