├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── preview.png ├── public ├── favicon.ico ├── index.html └── manifest.json └── src ├── components ├── App.js ├── App.module.scss └── Calculator │ ├── Calculator.js │ ├── Calculator.module.scss │ ├── Controls │ ├── Button │ │ ├── Button.js │ │ └── Button.module.scss │ ├── Controls.js │ ├── Controls.module.scss │ └── buttons.json │ └── Display │ ├── Display.js │ └── Display.module.scss ├── index.css ├── index.js └── serviceWorker.js /.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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Calculator React App 2 | Functional calculator built in React, styled using Sass. 3 | ## What I Learned: 4 | - Using Sass variables, nesting, operators and mixins 5 | - Improved React syntax and efficiency 6 | - Mapping through JSON files 7 | ## GitHub Pages: 8 | ### Link: 9 | https://jarodburchill.github.io/calculator-react-app 10 | ### Preview: 11 | ![alt text](preview.png "Preview Image") 12 | ## Installation: 13 | ``` 14 | git clone https://github.com/jarodburchill/calculator-react-app 15 | cd calculator-react-app 16 | npm install 17 | npm start 18 | ``` 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "homepage": "https://jarodburchill.github.io/calculator-react-app", 3 | "name": "calculator-react-app", 4 | "version": "0.1.0", 5 | "private": true, 6 | "dependencies": { 7 | "gh-pages": "^2.0.1", 8 | "node-sass": "^4.12.0", 9 | "react": "^16.8.6", 10 | "react-dom": "^16.8.6", 11 | "react-scripts": "3.0.1" 12 | }, 13 | "scripts": { 14 | "start": "react-scripts start", 15 | "build": "react-scripts build", 16 | "predeploy": "npm run build", 17 | "deploy": "gh-pages -d build", 18 | "test": "react-scripts test", 19 | "eject": "react-scripts eject" 20 | }, 21 | "eslintConfig": { 22 | "extends": "react-app" 23 | }, 24 | "browserslist": { 25 | "production": [ 26 | ">0.2%", 27 | "not dead", 28 | "not op_mini all" 29 | ], 30 | "development": [ 31 | "last 1 chrome version", 32 | "last 1 firefox version", 33 | "last 1 safari version" 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarodburchill/calculator-react-app/9535f1a0559225984a1b024b63f1d07acf38af57/preview.png -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarodburchill/calculator-react-app/9535f1a0559225984a1b024b63f1d07acf38af57/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 13 | 14 | 23 | Calculator 24 | 25 | 26 | 27 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/components/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import Calculator from './Calculator/Calculator'; 3 | import styles from './App.module.scss'; 4 | 5 | const App = () => { 6 | return ( 7 |
8 | 9 |
10 | ); 11 | } 12 | 13 | export default App; 14 | -------------------------------------------------------------------------------- /src/components/App.module.scss: -------------------------------------------------------------------------------- 1 | .container { 2 | display: flex; 3 | align-items: center; 4 | justify-content: center; 5 | min-height: 100vh; 6 | background: peachpuff; 7 | } -------------------------------------------------------------------------------- /src/components/Calculator/Calculator.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import Controls from './Controls/Controls'; 3 | import Display from './Display/Display'; 4 | import styles from './Calculator.module.scss' 5 | 6 | const Calculator = () => { 7 | const [displayValue, setDisplayValue] = useState("0"); 8 | const [formula, setFormula] = useState(""); 9 | 10 | return ( 11 |
12 | 13 | 14 |
15 | ); 16 | } 17 | 18 | export default Calculator; -------------------------------------------------------------------------------- /src/components/Calculator/Calculator.module.scss: -------------------------------------------------------------------------------- 1 | .container { 2 | display: flex; 3 | align-items: center; 4 | justify-content: center; 5 | flex-direction: column; 6 | 7 | background: black; 8 | padding: 20px 15px; 9 | border: 0px; 10 | border-radius: 20px; 11 | } -------------------------------------------------------------------------------- /src/components/Calculator/Controls/Button/Button.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from 'react'; 2 | import styles from './Button.module.scss'; 3 | 4 | const Button = (props) => { 5 | const [calculate, setCalculate] = useState(false); 6 | 7 | const getButtonStyle = () => { 8 | switch (props.symbol) { 9 | case "C": 10 | return styles.clearButton; 11 | case "÷": 12 | case "×": 13 | case "-": 14 | case "+": 15 | case "=": 16 | return styles.operatorButton; 17 | case "0": 18 | return styles.zeroButton; 19 | default: 20 | return styles.defaultButton; 21 | } 22 | } 23 | 24 | useEffect(() => { 25 | if (calculate) { 26 | props.setDisplayValue(eval(props.formula)); 27 | props.setFormula(props.formula + props.symbol); 28 | setCalculate(false); 29 | } 30 | }, [props.formula]); 31 | 32 | const onButtonClick = e => { 33 | switch (props.symbol) { 34 | case "=": 35 | if (!props.formula.includes("=")) { 36 | setCalculate(true); 37 | props.setFormula(props.formula + props.displayValue); 38 | } 39 | break; 40 | case "C": 41 | props.setDisplayValue("0"); 42 | props.setFormula(""); 43 | break; 44 | case "÷": 45 | case "×": 46 | case "-": 47 | case "+": 48 | if (props.formula.includes("=")) { 49 | props.setFormula(props.displayValue + props.operator); 50 | props.setDisplayValue("0"); 51 | } 52 | else { 53 | props.setFormula(props.formula + props.displayValue + props.operator); 54 | props.setDisplayValue("0"); 55 | } 56 | break; 57 | case ".": 58 | if (props.formula.includes("=")) { 59 | props.setFormula(""); 60 | props.setDisplayValue("0" + props.symbol); 61 | } 62 | else if (!props.displayValue.includes(".")) { 63 | props.setDisplayValue(props.displayValue + props.symbol); 64 | } 65 | break; 66 | default: 67 | if (props.formula.includes("=")) { 68 | props.setFormula(""); 69 | props.setDisplayValue(props.symbol); 70 | } 71 | else if (props.displayValue === "0") { 72 | props.setDisplayValue(props.symbol); 73 | } 74 | else { 75 | props.setDisplayValue(props.displayValue + props.symbol); 76 | } 77 | break; 78 | } 79 | } 80 | 81 | return ( 82 | 85 | ); 86 | } 87 | 88 | export default Button; -------------------------------------------------------------------------------- /src/components/Calculator/Controls/Button/Button.module.scss: -------------------------------------------------------------------------------- 1 | @mixin button($widthMultiplier, $color, $backgroundColor) { 2 | //consts 3 | $width: 50px; 4 | $margin: 2.5px; 5 | 6 | //sizing 7 | height: 50px; 8 | outline: 0; 9 | border: 0px; 10 | border-radius: 50px; 11 | margin: $margin; 12 | @if $widthMultiplier == 1 { 13 | width: ($width); 14 | } 15 | @else { 16 | width: ($width * $widthMultiplier + $margin * (($widthMultiplier - 1) *2)); 17 | } 18 | 19 | //font 20 | color: $color; 21 | font-size: 200%; 22 | 23 | //background 24 | background: $backgroundColor; 25 | &:hover { 26 | background: lighten($backgroundColor, 25%); 27 | } 28 | } 29 | .defaultButton { 30 | @include button(1, white, rgb(100, 100, 100)); 31 | } 32 | .clearButton { 33 | @include button(3, black, rgb(200, 200, 200)); 34 | } 35 | .zeroButton { 36 | @include button(2, white, rgb(100, 100, 100)); 37 | } 38 | .operatorButton { 39 | @include button(1, white, rgb(255, 125, 0)); 40 | } 41 | -------------------------------------------------------------------------------- /src/components/Calculator/Controls/Controls.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import Button from './Button/Button'; 3 | import styles from './Controls.module.scss' 4 | 5 | const Controls = (props) => { 6 | const data = require('./buttons.json'); 7 | const buttons = data.buttons; 8 | 9 | const renderNewLine = (newLine) => { 10 | if (newLine) { 11 | return ( 12 |
13 | ); 14 | } 15 | } 16 | 17 | return ( 18 |
19 | {buttons.map((item, index) => { 20 | return ( 21 | 22 | {renderNewLine(item.newLine)} 23 |
35 | ); 36 | } 37 | 38 | export default Controls; -------------------------------------------------------------------------------- /src/components/Calculator/Controls/Controls.module.scss: -------------------------------------------------------------------------------- 1 | .container { 2 | border: 2px solid rgb(100, 100, 100); 3 | padding: 10px; 4 | border-radius: 30px; 5 | } -------------------------------------------------------------------------------- /src/components/Calculator/Controls/buttons.json: -------------------------------------------------------------------------------- 1 | { 2 | "buttons": [ 3 | { 4 | "symbol": "C" 5 | }, 6 | { 7 | "symbol": "÷", 8 | "operator": "/" 9 | }, 10 | { 11 | "symbol": "7", 12 | "newLine": true 13 | }, 14 | { 15 | "symbol": "8" 16 | }, 17 | { 18 | "symbol": "9" 19 | }, 20 | { 21 | "symbol": "×", 22 | "operator": "*" 23 | }, 24 | { 25 | "symbol": "4", 26 | "newLine": true 27 | }, 28 | { 29 | "symbol": "5" 30 | }, 31 | { 32 | "symbol": "6" 33 | }, 34 | { 35 | "symbol": "-", 36 | "operator": "-" 37 | }, 38 | { 39 | "symbol": "1", 40 | "newLine": true 41 | }, 42 | { 43 | "symbol": "2" 44 | }, 45 | { 46 | "symbol": "3" 47 | }, 48 | { 49 | "symbol": "+", 50 | "operator": "+" 51 | }, 52 | { 53 | "symbol": "0", 54 | "newLine": true 55 | }, 56 | { 57 | "symbol": "." 58 | }, 59 | { 60 | "symbol": "=" 61 | } 62 | ] 63 | } -------------------------------------------------------------------------------- /src/components/Calculator/Display/Display.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import styles from './Display.module.scss' 3 | 4 | const Display = (props) => { 5 | return ( 6 |
7 | 12 | 17 |
18 | ); 19 | } 20 | 21 | export default Display; -------------------------------------------------------------------------------- /src/components/Calculator/Display/Display.module.scss: -------------------------------------------------------------------------------- 1 | .container { 2 | border: 2px solid rgb(100, 100, 100); 3 | padding: 10px; 4 | border-radius: 30px; 5 | width: 220px; 6 | margin-bottom: 10px; 7 | } 8 | .formula { 9 | width: 200px; 10 | font-size: 100%; 11 | text-align: right; 12 | margin: 0 5px; 13 | padding: 0; 14 | border: 0; 15 | outline: 0; 16 | background: black; 17 | color: white; 18 | &::-webkit-inner-spin-button { 19 | -webkit-appearance: none; 20 | margin: 0; 21 | } 22 | } 23 | .display { 24 | width: 200px; 25 | font-size: 250%; 26 | text-align: right; 27 | margin: 5px; 28 | padding: 0; 29 | border: 0; 30 | outline: 0; 31 | background: black; 32 | color: white; 33 | &::-webkit-inner-spin-button { 34 | -webkit-appearance: none; 35 | margin: 0; 36 | } 37 | } -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './components/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 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 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 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | --------------------------------------------------------------------------------