├── public ├── robots.txt ├── favicon.ico ├── logo192.png ├── logo512.png ├── manifest.json └── index.html ├── src ├── setupTests.js ├── App.test.js ├── App.js ├── index.css ├── index.js ├── App.css ├── logo.svg ├── ReactHorizontalDatePicker.css ├── ReactHorizontalDatePicker.js └── serviceWorker.js ├── .gitignore ├── .npmignore ├── package.json ├── package (copy).json └── README.md /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shavron/react-horizontal-strip-datepicker/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shavron/react-horizontal-strip-datepicker/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shavron/react-horizontal-strip-datepicker/HEAD/public/logo512.png -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './App.css' 3 | import ReactHorizontalDatePicker from './ReactHorizontalDatePicker'; 4 | 5 | function App() { 6 | 7 | const onSelectedDay = (d) =>{ 8 | console.log(d) 9 | }; 10 | 11 | return ( 12 | 13 | ); 14 | } 15 | 16 | export default App; 17 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /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 "./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.register(); 13 | -------------------------------------------------------------------------------- /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 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-horizontal-strip-datepicker", 3 | "version": "0.1.12", 4 | "description": "react horizontal strip datepicker", 5 | "license": "MIT", 6 | "author": "Abhilash Shukla", 7 | "private": false, 8 | "engines": { 9 | "node": " 13.2.0", 10 | "npm": "6.13.4" 11 | }, 12 | "keywords": [ 13 | "react", 14 | "react date-picker", 15 | "horizontal-datepicker", 16 | "scrollable datepicker", 17 | "horizontal-calender", 18 | "web", 19 | "whatsapp" 20 | ], 21 | "dependencies": { 22 | "date-fns": "^2.8.1", 23 | "react": "^16.12.0", 24 | "react-dom": "^16.12.0", 25 | "react-scripts": "3.4.0", 26 | "react-waypoint": "^9.0.2" 27 | }, 28 | "scripts": { 29 | "start": "react-scripts start", 30 | "build": "react-scripts build", 31 | "test": "react-scripts test", 32 | "eject": "react-scripts eject" 33 | }, 34 | "eslintConfig": { 35 | "extends": "react-app" 36 | }, 37 | "browserslist": { 38 | "production": [ 39 | ">0.2%", 40 | "not dead", 41 | "not op_mini all" 42 | ], 43 | "development": [ 44 | "last 1 chrome version", 45 | "last 1 firefox version", 46 | "last 1 safari version" 47 | ] 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /package (copy).json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-horizontal-strip-datepicker", 3 | "version": "0.1.12", 4 | "description": "react horizontal strip datepicker", 5 | "engines": { 6 | "node": " 13.2.0", 7 | "npm": "6.13.4" 8 | }, 9 | "keywords": [ 10 | "react", 11 | "react date-picker", 12 | "horizontal-datepicker", 13 | "scrollable datepicker", 14 | "horizontal-calender", 15 | "web" 16 | ], 17 | "main": "./dist/ReactHorizontalDatePicker.js", 18 | "license": "MIT", 19 | "author": "Abhilash Shukla", 20 | "private": false, 21 | "babel": { 22 | "presets": [ 23 | "@babel/preset-react" 24 | ] 25 | }, 26 | "dependencies": { 27 | "date-fns": "^2.8.1", 28 | "react": "^16.12.0", 29 | "react-dom": "^16.12.0", 30 | "react-scripts": "3.3.0", 31 | "react-waypoint": "^9.0.2" 32 | }, 33 | "devDependencies": { 34 | "@babel/cli": "^7.7.7", 35 | "@babel/preset-react": "^7.7.4", 36 | "react": "^16.12.0", 37 | "react-dom": "^16.12.0", 38 | "react-scripts": "3.3.0" 39 | }, 40 | "scripts": { 41 | "publish": "rm -rf dist && mkdir dist && babel src -d dist --copy-files", 42 | "start": "react-scripts start", 43 | "build": "react-scripts build", 44 | "test": "react-scripts test", 45 | "eject": "react-scripts eject" 46 | }, 47 | "eslintConfig": { 48 | "extends": "react-app" 49 | }, 50 | "browserslist": { 51 | "production": [ 52 | ">0.2%", 53 | "not dead", 54 | "not op_mini all" 55 | ], 56 | "development": [ 57 | "last 1 chrome version", 58 | "last 1 firefox version", 59 | "last 1 safari version" 60 | ] 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 13 | 14 | 18 | 19 | 28 | React App 29 | 30 | 31 | 32 |
33 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /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 | ## react-horizontal-strip-datepicker 8 | 9 | A stable horizontal date picker with the option to scroll for web 10 | ![Example](https://i.imgur.com/BaNEgIS.png?1) 11 | 12 | ### Installation 13 | 14 | Run `yarn add react-horizontal-strip-datepicker` 15 | 16 | ### Usage 17 | 18 | Import: 19 | 20 | `import ReactHorizontalDatePicker from "react-horizontal-strip-datepicker";` 21 | 22 | and use as: 23 | 24 | ```javascript 25 | 31 | ``` 32 | 33 | Available Props are 34 | 35 | | Prop | Type | Default | Description | 36 | | ------------------ | -------- | ------- | ------------------------------------------ | 37 | | enableScroll | Boolean | false | Set List to be scrollable | 38 | | selectedDay | Function | | Function to get the selected Day | 39 | | enableDays | Number | 90 | Number of days to render from current date | 40 | | enableDaysBefore | Number | 0 | Number of days to render before current date | 41 | 42 | enableDays has no effect if enableScroll is true. 43 | 44 | Example: 45 | 46 | ```javascript 47 | import React from 'react' 48 | 49 | import ReactHorizontalDatePicker from 'react-horizontal-strip-datepicker' 50 | import 'react-horizontal-strip-datepicker/dist/ReactHorizontalDatePicker.css' 51 | 52 | const App = () => { 53 | const onSelectedDay = (d) => { 54 | console.log(d) 55 | } 56 | 57 | return ( 58 | 64 | ) 65 | } 66 | 67 | export default App 68 | ``` 69 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/ReactHorizontalDatePicker.css: -------------------------------------------------------------------------------- 1 | .datepicker-dateList { 2 | display: flex; 3 | margin: 2px 0 2px 0; 4 | } 5 | 6 | .datepicker-date-day-Item{ 7 | font-weight: bold; 8 | display: flex; 9 | flex-direction: column; 10 | align-items: center; 11 | cursor: pointer; 12 | /* margin: 0 12px 0 12px; */ 13 | width: 45px; 14 | height: 45px; 15 | flex-shrink: 0; 16 | border-radius: 50%; 17 | border: 2px solid #fdfbfb; /* color: #e6545b; */ 18 | } 19 | 20 | .datepicker-button-previous-wrapper { 21 | font-weight: bold; 22 | display: flex; 23 | flex-direction: column; 24 | align-items: center; 25 | cursor: pointer; 26 | margin: 0 0 0 12px; 27 | width: 45px; 28 | height: 45px; 29 | flex-shrink: 0; 30 | border-radius: 50%; 31 | /* color: #e6545b; */ 32 | } 33 | 34 | 35 | .datepicker-strip { 36 | display: flex; 37 | /*align-items: center;*/ 38 | flex-direction: column; 39 | margin-top: 10px; 40 | width: max-content; 41 | max-width: 100%; 42 | overflow: hidden; 43 | line-height: 1.5; 44 | font-family: sans-serif; 45 | } 46 | 47 | .date-day-Item-selected { 48 | width: 45px; 49 | height: 45px; 50 | border-radius: 50%; 51 | border: 2px solid #e6545b; 52 | color: #e6545b; 53 | 54 | } 55 | 56 | .datepicker-date-day-Item:hover{ 57 | background-color: #e6545b; 58 | border: 2px solid transparent; 59 | color: #fff; 60 | width: 45px; 61 | height: 45px; 62 | border-radius: 50%; 63 | 64 | } 65 | 66 | .date-day-item-disabled { 67 | color: rgb(176, 176, 176); 68 | pointer-events: none; 69 | } 70 | 71 | .datepicker-datelist-scrollable { 72 | display: flex; 73 | overflow-x: scroll; 74 | scrollbar-width: none; 75 | /* margin: 2px 0 2px 0; */ 76 | 77 | } 78 | 79 | .datepicker-datelist-scrollable::-webkit-scrollbar { 80 | -webkit-appearance: none; 81 | display: none; 82 | } 83 | 84 | .datepicker{ 85 | display: flex; 86 | margin: 12px; 87 | align-items: center; 88 | position: relative; 89 | } 90 | 91 | .datepicker-day-label{ 92 | font-size: 12px; 93 | margin: 4px 0 0 0; 94 | text-align: center; 95 | } 96 | 97 | .scroll-head{ 98 | border: 1px solid #e9e9e9; 99 | border-radius: 2px; 100 | margin: 8px 0px 8px 0px; 101 | text-align: center; 102 | height: 18px; 103 | font-size: 12px; 104 | background: #e9e9e9; 105 | } 106 | .blank-space-div{ 107 | border: 1px solid transparent; 108 | border-radius: 2px; 109 | margin: 8px 0px 8px 0px; 110 | text-align: center; 111 | height: 18px; 112 | font-size: 12px; 113 | 114 | } 115 | 116 | 117 | 118 | .datepicker-button-previous, .datepicker-button-next{ 119 | border: none; 120 | text-decoration: none; 121 | background: #e6545b; 122 | cursor: pointer; 123 | border-radius: 50%; 124 | width: 45px; 125 | height: 45px; 126 | color: white; 127 | font-size: 20px; 128 | font-weight: bold; 129 | flex-shrink: 0; 130 | } 131 | 132 | 133 | .button-previous{ 134 | font-weight: bold; 135 | display: flex; 136 | flex-direction: column; 137 | align-items: center; 138 | cursor: pointer; 139 | width: 45px; 140 | height: 45px; 141 | flex-shrink: 0; 142 | border-radius: 50%; 143 | border: 2px solid transparent; 144 | /* color: #e6545b; */ 145 | } 146 | 147 | .datepicker-button-previous{ 148 | transform: rotate(180deg); 149 | 150 | } 151 | 152 | 153 | .datepicker-month-label { 154 | margin-left: 12px; 155 | font-size: 15px; 156 | color: #e6545b; 157 | font-weight: bold; 158 | } 159 | 160 | .datepicker-date-label { 161 | /* font-size: 12px; */ 162 | margin-top: -3px; 163 | } 164 | .wrapper{ 165 | position: relative; 166 | /* border: 1px solid #000; */ 167 | } 168 | @-webkit-keyframes ripple { 169 | 0% { 170 | width: 0; 171 | height: 0; 172 | opacity: .9; 173 | } 174 | 100% { 175 | width: 100%; 176 | height: 100%; 177 | opacity: 0; 178 | } 179 | } 180 | 181 | @keyframes ripple { 182 | 0% { 183 | width: 0; 184 | height: 0; 185 | opacity: .9; 186 | } 187 | 100% { 188 | width: 100%; 189 | height: 100%; 190 | opacity: 0; 191 | } 192 | } 193 | .ripple:before { 194 | content: ''; 195 | position: absolute; 196 | top: 50%; 197 | left: 50%; 198 | width: 0; 199 | height: 0; 200 | -webkit-transform: translate(-50%, -50%); 201 | transform: translate(-50%, -50%); 202 | border-radius: 50%; 203 | background-color: currentColor; 204 | visibility: hidden; 205 | z-index: 2; 206 | } 207 | .ripple:not(:active):before { 208 | -webkit-animation: ripple 0.4s cubic-bezier(0, 0, 0.2, 1); 209 | animation: ripple 0.4s cubic-bezier(0, 0, 0.2, 1); 210 | transition: visibility .9s step-end; 211 | } 212 | .ripple:active:before { 213 | visibility: visible; 214 | } -------------------------------------------------------------------------------- /src/ReactHorizontalDatePicker.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import { Waypoint } from "react-waypoint"; 3 | import "./ReactHorizontalDatePicker.css"; 4 | import { 5 | format, 6 | addWeeks, 7 | subWeeks, 8 | addDays, 9 | subDays, 10 | isSameDay, 11 | isBefore, 12 | getDate 13 | } from "date-fns"; 14 | 15 | export default React.memo(function ReactHorizontalDatePicker({ 16 | enableDays, 17 | enableScroll, 18 | selectedDay 19 | }) { 20 | const [selectedDate, setSelectedDate] = useState(new Date()); 21 | const [headingDate, setHeadingDate] = useState(new Date()); 22 | const [currentWeek, setCurrentWeek] = useState(new Date()); 23 | const [currentDate] = useState(new Date()); 24 | const scrollWidth = 250; 25 | enableScroll = enableScroll || false; 26 | enableDays = enableScroll === true ? enableDays || 90 : 7; 27 | const heading_dateFormat = "MMM yyyy"; 28 | 29 | useEffect(() => { 30 | console.log(headingDate); 31 | }, [headingDate]); 32 | 33 | const applyStyles = day => { 34 | const classes = []; 35 | if (isSameDay(day, selectedDate)) { 36 | classes.push(" date-day-Item-selected"); 37 | } 38 | 39 | if (isBefore(day, currentDate)) { 40 | classes.push(" date-day-item-disabled"); 41 | } 42 | return classes.join(" "); 43 | }; 44 | 45 | const _handlePosition = (pos, date) => { 46 | let currentPosition = pos.currentPosition; 47 | let previousPosition = pos.previousPosition; 48 | 49 | if (previousPosition == "inside" && currentPosition == "above") { 50 | setHeadingDate(date); 51 | } 52 | if (previousPosition == "above" && currentPosition == "inside") { 53 | setHeadingDate(addDays(date, -1)); 54 | } 55 | }; 56 | 57 | const _verticalList = () => { 58 | const _dayFormat = "E"; 59 | const _dateFormat = "dd"; 60 | const _monthFormat = "MMM"; 61 | const _verticalListItems = []; 62 | const _startDay = subDays(currentWeek, 1); 63 | 64 | for (let i = 0; i < enableDays; i++) { 65 | let _day = format(addDays(_startDay, i), _dayFormat); 66 | let _date = format(addDays(_startDay, i), _dateFormat); 67 | let _month = format(addDays(_startDay, i), _monthFormat); 68 | 69 | _verticalListItems.push( 70 | 74 | _date == 1 ? _handlePosition(pos, addDays(_startDay, i)) : "" 75 | } 76 | > 77 |
78 | {format(addDays(_startDay, i), _dateFormat) == 1 ? ( 79 |
80 | {format(addDays(_startDay, i), "MMM")} 81 |
82 | ) : ( 83 |
84 | )} 85 |
onDateClick(addDays(_startDay, i))} 90 | > 91 |
{_day}
92 |
{_date}
93 |
94 |
95 |
96 | ); 97 | } 98 | 99 | return ( 100 |
108 | {_verticalListItems} 109 |
110 | ); 111 | }; 112 | 113 | const onDateClick = day => { 114 | setSelectedDate(day); 115 | selectedDay(selectedDate); 116 | }; 117 | 118 | const nextScroll = () => { 119 | enableScroll 120 | ? (document.getElementById("container").scrollLeft += scrollWidth) 121 | : setCurrentWeek(addWeeks(currentWeek, 1)); 122 | }; 123 | 124 | const prevScroll = () => { 125 | enableScroll 126 | ? (document.getElementById("container").scrollLeft -= scrollWidth) 127 | : setCurrentWeek(subWeeks(currentWeek, 1)); 128 | }; 129 | 130 | return ( 131 |
132 | 133 | {format(selectedDate, "dd MMM yyy")} 134 | 135 |
136 |
137 |
{format(headingDate, "MMM")}
138 |
139 | {" "} 140 | 143 |
144 |
145 | {_verticalList()} 146 |
147 |
148 | 149 |
150 | {" "} 151 | 154 |
155 |
156 |
157 |
158 | ); 159 | }); 160 | -------------------------------------------------------------------------------- /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.0/8 are 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 | headers: { 'Service-Worker': 'script' } 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready.then(registration => { 134 | registration.unregister(); 135 | }); 136 | } 137 | } 138 | --------------------------------------------------------------------------------