├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── src ├── Cards.jsx ├── constants.js └── styles.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "react", 4 | "env", 5 | "stage-0" 6 | ], 7 | "plugins": [ 8 | "transform-object-rest-spread", 9 | "transform-react-jsx" 10 | ] 11 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/vim,node,linux,macos,windows,webstorm,intellij,visualstudio 3 | # Edit at https://www.gitignore.io/?templates=vim,node,linux,macos,windows,webstorm,intellij,visualstudio 4 | 5 | ### Intellij ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # Generated files 17 | .idea/**/contentModel.xml 18 | 19 | # Sensitive or high-churn files 20 | .idea/**/dataSources/ 21 | .idea/**/dataSources.ids 22 | .idea/**/dataSources.local.xml 23 | .idea/**/sqlDataSources.xml 24 | .idea/**/dynamic.xml 25 | .idea/**/uiDesigner.xml 26 | .idea/**/dbnavigator.xml 27 | 28 | # Gradle 29 | .idea/**/gradle.xml 30 | .idea/**/libraries 31 | 32 | # Gradle and Maven with auto-import 33 | # When using Gradle or Maven with auto-import, you should exclude module files, 34 | # since they will be recreated, and may cause churn. Uncomment if using 35 | # auto-import. 36 | # .idea/modules.xml 37 | # .idea/*.iml 38 | # .idea/modules 39 | 40 | # CMake 41 | cmake-build-*/ 42 | 43 | # Mongo Explorer plugin 44 | .idea/**/mongoSettings.xml 45 | 46 | # File-based project format 47 | *.iws 48 | 49 | # IntelliJ 50 | out/ 51 | 52 | # mpeltonen/sbt-idea plugin 53 | .idea_modules/ 54 | 55 | # JIRA plugin 56 | atlassian-ide-plugin.xml 57 | 58 | # Cursive Clojure plugin 59 | .idea/replstate.xml 60 | 61 | # Crashlytics plugin (for Android Studio and IntelliJ) 62 | com_crashlytics_export_strings.xml 63 | crashlytics.properties 64 | crashlytics-build.properties 65 | fabric.properties 66 | 67 | # Editor-based Rest Client 68 | .idea/httpRequests 69 | 70 | # Android studio 3.1+ serialized cache file 71 | .idea/caches/build_file_checksums.ser 72 | 73 | ### Intellij Patch ### 74 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 75 | 76 | # *.iml 77 | # modules.xml 78 | # .idea/misc.xml 79 | # *.ipr 80 | 81 | # Sonarlint plugin 82 | .idea/sonarlint 83 | 84 | ### Linux ### 85 | *~ 86 | 87 | # temporary files which can be created if a process still has a handle open of a deleted file 88 | .fuse_hidden* 89 | 90 | # KDE directory preferences 91 | .directory 92 | 93 | # Linux trash folder which might appear on any partition or disk 94 | .Trash-* 95 | 96 | # .nfs files are created when an open file is removed but is still being accessed 97 | .nfs* 98 | 99 | ### macOS ### 100 | # General 101 | .DS_Store 102 | .AppleDouble 103 | .LSOverride 104 | 105 | # Icon must end with two \r 106 | Icon 107 | 108 | # Thumbnails 109 | ._* 110 | 111 | # Files that might appear in the root of a volume 112 | .DocumentRevisions-V100 113 | .fseventsd 114 | .Spotlight-V100 115 | .TemporaryItems 116 | .Trashes 117 | .VolumeIcon.icns 118 | .com.apple.timemachine.donotpresent 119 | 120 | # Directories potentially created on remote AFP share 121 | .AppleDB 122 | .AppleDesktop 123 | Network Trash Folder 124 | Temporary Items 125 | .apdisk 126 | 127 | ### Node ### 128 | # Logs 129 | logs 130 | *.log 131 | npm-debug.log* 132 | yarn-debug.log* 133 | yarn-error.log* 134 | 135 | # Runtime data 136 | pids 137 | *.pid 138 | *.seed 139 | *.pid.lock 140 | 141 | # Directory for instrumented libs generated by jscoverage/JSCover 142 | lib-cov 143 | 144 | # Coverage directory used by tools like istanbul 145 | coverage 146 | 147 | # nyc test coverage 148 | .nyc_output 149 | 150 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 151 | .grunt 152 | 153 | # Bower dependency directory (https://bower.io/) 154 | bower_components 155 | 156 | # node-waf configuration 157 | .lock-wscript 158 | 159 | # Compiled binary addons (https://nodejs.org/api/addons.html) 160 | build/Release 161 | 162 | # Dependency directories 163 | node_modules/ 164 | jspm_packages/ 165 | 166 | # TypeScript v1 declaration files 167 | typings/ 168 | 169 | # Optional npm cache directory 170 | .npm 171 | 172 | # Optional eslint cache 173 | .eslintcache 174 | 175 | # Optional REPL history 176 | .node_repl_history 177 | 178 | # Output of 'npm pack' 179 | *.tgz 180 | 181 | # Yarn Integrity file 182 | .yarn-integrity 183 | 184 | # dotenv environment variables file 185 | .env 186 | .env.test 187 | 188 | # parcel-bundler cache (https://parceljs.org/) 189 | .cache 190 | 191 | # next.js build output 192 | .next 193 | 194 | # nuxt.js build output 195 | .nuxt 196 | 197 | # vuepress build output 198 | .vuepress/dist 199 | 200 | # Serverless directories 201 | .serverless/ 202 | 203 | # FuseBox cache 204 | .fusebox/ 205 | 206 | # DynamoDB Local files 207 | .dynamodb/ 208 | 209 | ### Vim ### 210 | # Swap 211 | [._]*.s[a-v][a-z] 212 | [._]*.sw[a-p] 213 | [._]s[a-rt-v][a-z] 214 | [._]ss[a-gi-z] 215 | [._]sw[a-p] 216 | 217 | # Session 218 | Session.vim 219 | 220 | # Temporary 221 | .netrwhist 222 | # Auto-generated tag files 223 | tags 224 | # Persistent undo 225 | [._]*.un~ 226 | 227 | ### WebStorm ### 228 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 229 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 230 | 231 | # User-specific stuff 232 | 233 | # Generated files 234 | 235 | # Sensitive or high-churn files 236 | 237 | # Gradle 238 | 239 | # Gradle and Maven with auto-import 240 | # When using Gradle or Maven with auto-import, you should exclude module files, 241 | # since they will be recreated, and may cause churn. Uncomment if using 242 | # auto-import. 243 | # .idea/modules.xml 244 | # .idea/*.iml 245 | # .idea/modules 246 | 247 | # CMake 248 | 249 | # Mongo Explorer plugin 250 | 251 | # File-based project format 252 | 253 | # IntelliJ 254 | 255 | # mpeltonen/sbt-idea plugin 256 | 257 | # JIRA plugin 258 | 259 | # Cursive Clojure plugin 260 | 261 | # Crashlytics plugin (for Android Studio and IntelliJ) 262 | 263 | # Editor-based Rest Client 264 | 265 | # Android studio 3.1+ serialized cache file 266 | 267 | ### WebStorm Patch ### 268 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 269 | 270 | # *.iml 271 | # modules.xml 272 | # .idea/misc.xml 273 | # *.ipr 274 | 275 | # Sonarlint plugin 276 | 277 | ### Windows ### 278 | # Windows thumbnail cache files 279 | Thumbs.db 280 | ehthumbs.db 281 | ehthumbs_vista.db 282 | 283 | # Dump file 284 | *.stackdump 285 | 286 | # Folder config file 287 | [Dd]esktop.ini 288 | 289 | # Recycle Bin used on file shares 290 | $RECYCLE.BIN/ 291 | 292 | # Windows Installer files 293 | *.cab 294 | *.msi 295 | *.msix 296 | *.msm 297 | *.msp 298 | 299 | # Windows shortcuts 300 | *.lnk 301 | 302 | ### VisualStudio ### 303 | ## Ignore Visual Studio temporary files, build results, and 304 | ## files generated by popular Visual Studio add-ons. 305 | ## 306 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 307 | 308 | # User-specific files 309 | *.rsuser 310 | *.suo 311 | *.user 312 | *.userosscache 313 | *.sln.docstates 314 | 315 | # User-specific files (MonoDevelop/Xamarin Studio) 316 | *.userprefs 317 | 318 | # Build results 319 | [Dd]ebug/ 320 | [Dd]ebugPublic/ 321 | [Rr]elease/ 322 | [Rr]eleases/ 323 | x64/ 324 | x86/ 325 | [Aa][Rr][Mm]/ 326 | [Aa][Rr][Mm]64/ 327 | bld/ 328 | [Bb]in/ 329 | [Oo]bj/ 330 | [Ll]og/ 331 | 332 | # Visual Studio 2015/2017 cache/options directory 333 | .vs/ 334 | # Uncomment if you have tasks that create the project's static files in wwwroot 335 | #wwwroot/ 336 | 337 | # Visual Studio 2017 auto generated files 338 | Generated\ Files/ 339 | 340 | # MSTest test Results 341 | [Tt]est[Rr]esult*/ 342 | [Bb]uild[Ll]og.* 343 | 344 | # NUNIT 345 | *.VisualState.xml 346 | TestResult.xml 347 | 348 | # Build Results of an ATL Project 349 | [Dd]ebugPS/ 350 | [Rr]eleasePS/ 351 | dlldata.c 352 | 353 | # Benchmark Results 354 | BenchmarkDotNet.Artifacts/ 355 | 356 | # .NET Core 357 | project.lock.json 358 | project.fragment.lock.json 359 | artifacts/ 360 | 361 | # StyleCop 362 | StyleCopReport.xml 363 | 364 | # Files built by Visual Studio 365 | *_i.c 366 | *_p.c 367 | *_h.h 368 | *.ilk 369 | *.meta 370 | *.obj 371 | *.iobj 372 | *.pch 373 | *.pdb 374 | *.ipdb 375 | *.pgc 376 | *.pgd 377 | *.rsp 378 | *.sbr 379 | *.tlb 380 | *.tli 381 | *.tlh 382 | *.tmp 383 | *.tmp_proj 384 | *_wpftmp.csproj 385 | *.vspscc 386 | *.vssscc 387 | .builds 388 | *.pidb 389 | *.svclog 390 | *.scc 391 | 392 | # Chutzpah Test files 393 | _Chutzpah* 394 | 395 | # Visual C++ cache files 396 | ipch/ 397 | *.aps 398 | *.ncb 399 | *.opendb 400 | *.opensdf 401 | *.sdf 402 | *.cachefile 403 | *.VC.db 404 | *.VC.VC.opendb 405 | 406 | # Visual Studio profiler 407 | *.psess 408 | *.vsp 409 | *.vspx 410 | *.sap 411 | 412 | # Visual Studio Trace Files 413 | *.e2e 414 | 415 | # TFS 2012 Local Workspace 416 | $tf/ 417 | 418 | # Guidance Automation Toolkit 419 | *.gpState 420 | 421 | # ReSharper is a .NET coding add-in 422 | _ReSharper*/ 423 | *.[Rr]e[Ss]harper 424 | *.DotSettings.user 425 | 426 | # JustCode is a .NET coding add-in 427 | .JustCode 428 | 429 | # TeamCity is a build add-in 430 | _TeamCity* 431 | 432 | # DotCover is a Code Coverage Tool 433 | *.dotCover 434 | 435 | # AxoCover is a Code Coverage Tool 436 | .axoCover/* 437 | !.axoCover/settings.json 438 | 439 | # Visual Studio code coverage results 440 | *.coverage 441 | *.coveragexml 442 | 443 | # NCrunch 444 | _NCrunch_* 445 | .*crunch*.local.xml 446 | nCrunchTemp_* 447 | 448 | # MightyMoose 449 | *.mm.* 450 | AutoTest.Net/ 451 | 452 | # Web workbench (sass) 453 | .sass-cache/ 454 | 455 | # Installshield output folder 456 | [Ee]xpress/ 457 | 458 | # DocProject is a documentation generator add-in 459 | DocProject/buildhelp/ 460 | DocProject/Help/*.HxT 461 | DocProject/Help/*.HxC 462 | DocProject/Help/*.hhc 463 | DocProject/Help/*.hhk 464 | DocProject/Help/*.hhp 465 | DocProject/Help/Html2 466 | DocProject/Help/html 467 | 468 | # Click-Once directory 469 | publish/ 470 | 471 | # Publish Web Output 472 | *.[Pp]ublish.xml 473 | *.azurePubxml 474 | # Note: Comment the next line if you want to checkin your web deploy settings, 475 | # but database connection strings (with potential passwords) will be unencrypted 476 | *.pubxml 477 | *.publishproj 478 | 479 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 480 | # checkin your Azure Web App publish settings, but sensitive information contained 481 | # in these scripts will be unencrypted 482 | PublishScripts/ 483 | 484 | # NuGet Packages 485 | *.nupkg 486 | # The packages folder can be ignored because of Package Restore 487 | **/[Pp]ackages/* 488 | # except build/, which is used as an MSBuild target. 489 | !**/[Pp]ackages/build/ 490 | # Uncomment if necessary however generally it will be regenerated when needed 491 | #!**/[Pp]ackages/repositories.config 492 | # NuGet v3's project.json files produces more ignorable files 493 | *.nuget.props 494 | *.nuget.targets 495 | 496 | # Microsoft Azure Build Output 497 | csx/ 498 | *.build.csdef 499 | 500 | # Microsoft Azure Emulator 501 | ecf/ 502 | rcf/ 503 | 504 | # Windows Store app package directories and files 505 | AppPackages/ 506 | BundleArtifacts/ 507 | Package.StoreAssociation.xml 508 | _pkginfo.txt 509 | *.appx 510 | 511 | # Visual Studio cache files 512 | # files ending in .cache can be ignored 513 | *.[Cc]ache 514 | # but keep track of directories ending in .cache 515 | !?*.[Cc]ache/ 516 | 517 | # Others 518 | ClientBin/ 519 | ~$* 520 | *.dbmdl 521 | *.dbproj.schemaview 522 | *.jfm 523 | *.pfx 524 | *.publishsettings 525 | orleans.codegen.cs 526 | 527 | # Including strong name files can present a security risk 528 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 529 | #*.snk 530 | 531 | # Since there are multiple workflows, uncomment next line to ignore bower_components 532 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 533 | #bower_components/ 534 | # ASP.NET Core default setup: bower directory is configured as wwwroot/lib/ and bower restore is true 535 | **/wwwroot/lib/ 536 | 537 | # RIA/Silverlight projects 538 | Generated_Code/ 539 | 540 | # Backup & report files from converting an old project file 541 | # to a newer Visual Studio version. Backup files are not needed, 542 | # because we have git ;-) 543 | _UpgradeReport_Files/ 544 | Backup*/ 545 | UpgradeLog*.XML 546 | UpgradeLog*.htm 547 | ServiceFabricBackup/ 548 | *.rptproj.bak 549 | 550 | # SQL Server files 551 | *.mdf 552 | *.ldf 553 | *.ndf 554 | 555 | # Business Intelligence projects 556 | *.rdl.data 557 | *.bim.layout 558 | *.bim_*.settings 559 | *.rptproj.rsuser 560 | 561 | # Microsoft Fakes 562 | FakesAssemblies/ 563 | 564 | # GhostDoc plugin setting file 565 | *.GhostDoc.xml 566 | 567 | # Node.js Tools for Visual Studio 568 | .ntvs_analysis.dat 569 | 570 | # Visual Studio 6 build log 571 | *.plg 572 | 573 | # Visual Studio 6 workspace options file 574 | *.opt 575 | 576 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 577 | *.vbw 578 | 579 | # Visual Studio LightSwitch build output 580 | **/*.HTMLClient/GeneratedArtifacts 581 | **/*.DesktopClient/GeneratedArtifacts 582 | **/*.DesktopClient/ModelManifest.xml 583 | **/*.Server/GeneratedArtifacts 584 | **/*.Server/ModelManifest.xml 585 | _Pvt_Extensions 586 | 587 | # Paket dependency manager 588 | .paket/paket.exe 589 | paket-files/ 590 | 591 | # FAKE - F# Make 592 | .fake/ 593 | 594 | # JetBrains Rider 595 | .idea/ 596 | *.sln.iml 597 | 598 | # CodeRush personal settings 599 | .cr/personal 600 | 601 | # Python Tools for Visual Studio (PTVS) 602 | __pycache__/ 603 | *.pyc 604 | 605 | # Cake - Uncomment if you are using it 606 | # tools/** 607 | # !tools/packages.config 608 | 609 | # Tabs Studio 610 | *.tss 611 | 612 | # Telerik's JustMock configuration file 613 | *.jmconfig 614 | 615 | # BizTalk build output 616 | *.btp.cs 617 | *.btm.cs 618 | *.odx.cs 619 | *.xsd.cs 620 | 621 | # OpenCover UI analysis results 622 | OpenCover/ 623 | 624 | # Azure Stream Analytics local run output 625 | ASALocalRun/ 626 | 627 | # MSBuild Binary and Structured Log 628 | *.binlog 629 | 630 | # NVidia Nsight GPU debugger configuration file 631 | *.nvuser 632 | 633 | # MFractors (Xamarin productivity tool) working folder 634 | .mfractor/ 635 | 636 | # Local History for Visual Studio 637 | .localhistory/ 638 | 639 | # BeatPulse healthcheck temp database 640 | healthchecksdb 641 | 642 | # Webpack 643 | build/ 644 | 645 | # Etc 646 | .eslintrc.json 647 | 648 | # End of https://www.gitignore.io/api/vim,node,linux,macos,windows,webstorm,intellij,visualstudio -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | demo 3 | .babelrc 4 | webpack.config.js 5 | .eslintrc.json 6 | .gitignore -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Tomorrow 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Card Carousel 2 | A simple React carousel. 3 | * GitHub 4 | * Demo 5 | 6 | ## Installation 7 | ``` 8 | npm install react-card-carousel --save 9 | ``` 10 | 11 | ## Peer Dependencies 12 | Be sure to have all peer dependencies installed as well. React Card Carousel requires the peer dependencies below: 13 | * `react` 14 | * `react-dom` 15 | * `prop-types` 16 | 17 | ## Current Version: 1.1.3 18 | * 1.1.3 fixed setState warning on componentWillUnmount 19 | * Removed styled-components as a peer dependency 20 | * Greatly reduced package size 21 | * Various bug fixes 22 | 23 | ## Usage 24 | Import ReactCardCarousel and use it as a wrapper around card elements. Example: 25 | 26 | ``` 27 | import React, { Component } from 'react'; 28 | import ReactCardCarousel from 'react-card-carousel'; 29 | 30 | class MyCarousel extends Component { 31 | 32 | static get CARD_STYLE() { 33 | return { 34 | height: '200px', 35 | width: '200px', 36 | paddingTop: '80px', 37 | textAlign: 'center', 38 | background: '#52C0F5', 39 | color: '#FFF', 40 | fontSize: '12px', 41 | textTransform: 'uppercase', 42 | borderRadius: '10px', 43 | }; 44 | } 45 | 46 | render() { 47 | return ( 48 | 49 |
50 | First Card 51 |
52 |
53 | Second Card 54 |
55 |
56 | Third Card 57 |
58 |
59 | Fourth Card 60 |
61 |
62 | Fifth Card 63 |
64 |
65 | ); 66 | } 67 | } 68 | 69 | export default MyCarousel; 70 | ``` 71 | 72 | ## Props 73 | * `alignment` (String, oneOf[`horizontal`, `vertical`], default: `horizontal`): Card alignment display. 74 | * `spread` (String, oneOf[`narrow`, `medium`, `wide`], default: `medium`): Spread between cards. 75 | * `initial_index` (Number, default: 0): Which card to display as center card initially. 76 | * `disable_keydown` (Boolean, default: false): Disables left and right arrow key scroll. 77 | * `disable_box_shadow` (Boolean, default: false): Disables box shadow around center card. 78 | * `disable_fade_in` (Boolean, default: false): Disables initial animation on component render. 79 | * `autoplay` (Boolean, default: false) 80 | * `autoplay_speed` (Number, default: 5000): Number in milliseconds. 81 | * `afterChange` (Function (cardIndex) => {}, default: null): After card change function. 82 | 83 | ## Methods 84 | To use any instance methods, you must first create a ref to the ReactCardCarousel instance. Learn more about React refs in the official documentation. 85 | 86 | ``` 87 | this.Carousel = Carousel }> 88 | ``` 89 | 90 | The methods can be accessed on the `this.Carousel` instance: 91 | * `this.Carousel.next()`: Sets next card as center card. 92 | * `this.Carousel.prev()`: Sets previous card as center card. 93 | * `this.Carousel.goTo(index)`: Sets the specified number index as center card. 94 | * `this.Carousel.getCurrentIndex()`: Gets current card index. 95 | 96 | *NOTE*: If you choose to create the ref using React.createRef() instead of using a callback ref, the methods can be accessed on the `this.Carousel.current` instance. 97 | 98 | ## Credits 99 | Created by @strawbee at Tomorrow Ideas. 100 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-card-carousel", 3 | "version": "1.1.3", 4 | "description": "Simple React carousel.", 5 | "license": "MIT", 6 | "author": { 7 | "name": "Joy Hou", 8 | "email": "yuejiao.hou@gmail.com", 9 | "url": "https://github.com/strawbee" 10 | }, 11 | "keywords": [ 12 | "React", 13 | "simple", 14 | "fast", 15 | "carousel", 16 | "cards" 17 | ], 18 | "main": "./build/index.js", 19 | "scripts": { 20 | "start": "webpack --watch", 21 | "watch": "webpack-dev-server --inline --hot --port=8005", 22 | "build": "webpack", 23 | "test": "echo \"Error: no test specified\" && exit 1" 24 | }, 25 | "peerDependencies": { 26 | "prop-types": "^15.6.0", 27 | "react": "^16.11.0", 28 | "react-dom": "^16.11.0" 29 | }, 30 | "devDependencies": { 31 | "babel-cli": "^7.0.0-beta.3", 32 | "babel-core": "^6.26.3", 33 | "babel-eslint": "^10.0.3", 34 | "babel-loader": "^7.1.5", 35 | "babel-plugin-transform-object-rest-spread": "^6.26.0", 36 | "babel-plugin-transform-react-jsx": "^6.24.1", 37 | "babel-preset-env": "^1.7.0", 38 | "babel-preset-react": "^6.16.0", 39 | "babel-preset-stage-0": "^6.24.1", 40 | "eslint": "^6.6.0", 41 | "path": "^0.12.7", 42 | "prop-types": "^15.6.0", 43 | "react": "^16.11.0", 44 | "react-dom": "^16.11.0", 45 | "webpack": "^4.41.2", 46 | "webpack-cli": "^3.3.9", 47 | "webpack-dev-server": "^3.9.0" 48 | }, 49 | "dependencies": {} 50 | } 51 | -------------------------------------------------------------------------------- /src/Cards.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { 4 | STYLES, 5 | getOpacity, 6 | getZIndex, 7 | getTransform, 8 | getBoxShadow, 9 | getCursor, 10 | } from './styles.js'; 11 | import { POSITION, ALIGNMENT, SPREAD } from './constants.js'; 12 | 13 | 14 | /** 15 | * React Card Carousel 16 | * @returns {React.Node} 17 | */ 18 | class Cards extends Component { 19 | 20 | constructor(props) { 21 | super(props); 22 | 23 | this.state = { 24 | current_index: props.disable_fade_in ? props.initial_index : null, 25 | interval: null, 26 | }; 27 | } 28 | 29 | static propTypes = { 30 | alignment: PropTypes.oneOf([ALIGNMENT.HORIZONTAL, ALIGNMENT.VERTICAL]), 31 | spread: PropTypes.oneOf([SPREAD.NARROW, SPREAD.MEDIUM, SPREAD.WIDE]), 32 | initial_index: PropTypes.number, 33 | disable_keydown: PropTypes.bool, 34 | disable_box_shadow: PropTypes.bool, 35 | disable_fade_in: PropTypes.bool, 36 | autoplay: PropTypes.bool, 37 | autoplay_speed: PropTypes.number, 38 | afterChange: PropTypes.func, 39 | } 40 | 41 | static defaultProps = { 42 | alignment: ALIGNMENT.HORIZONTAL, 43 | spread: SPREAD.MEDIUM, 44 | initial_index: 0, 45 | disable_keydown: false, 46 | disable_box_shadow: false, 47 | disable_fade_in: false, 48 | autoplay: false, 49 | autoplay_speed: 5000, 50 | afterChange: () => {}, 51 | } 52 | 53 | /** 54 | * @public 55 | * Sets current index state 56 | */ 57 | goTo = (idx) => { 58 | this.setState({ current_index: Number(idx) }, this.props.afterChange); 59 | } 60 | 61 | /** 62 | * @public 63 | * Goes to next card 64 | */ 65 | next = () => { 66 | if (this._is_mounted) { 67 | this._cardOnClick(POSITION.NEXT); 68 | } 69 | } 70 | 71 | /** 72 | * @public 73 | * Goes to previous card 74 | */ 75 | prev = () => this._cardOnClick(POSITION.PREV); 76 | 77 | /** 78 | * @public 79 | * Gets current card index 80 | */ 81 | getCurrentIndex = () => this.state.current_index; 82 | 83 | componentDidMount() { 84 | const { 85 | initial_index, 86 | disable_keydown, 87 | disable_fade_in, 88 | autoplay, 89 | } = this.props; 90 | 91 | this._is_mounted = true; 92 | 93 | // Triggers initial animation 94 | if (!disable_fade_in) setTimeout(() => { 95 | this.setState({ current_index: initial_index }); 96 | }, 0.25); 97 | 98 | // Sets right and left key event listener 99 | if (!disable_keydown) { 100 | document.onkeydown = this._keydownEventListener; 101 | } 102 | 103 | // Sets autoplay interval 104 | if (autoplay) this._autoplay(); 105 | } 106 | 107 | componentWillUnmount() { 108 | this._is_mounted = false; 109 | if (!this.props.disable_keydown) document.onkeydown = null; 110 | } 111 | 112 | /** 113 | * Event listener for left/right arrow keys 114 | */ 115 | _keydownEventListener = (e) => { 116 | if (e.which === 39) { 117 | return this.next(); 118 | } 119 | if (e.which === 37) { 120 | return this.prev(); 121 | } 122 | } 123 | 124 | /** 125 | * Sets interval for advancing cards 126 | */ 127 | _autoplay = () => { 128 | if (this._is_mounted) { 129 | const { autoplay_speed } = this.props; 130 | const interval = setInterval(this.next, autoplay_speed); 131 | this.setState({ interval }); 132 | } 133 | } 134 | 135 | /** 136 | * Resets autoplay interval 137 | */ 138 | _resetInterval = () => { 139 | clearInterval(this.state.interval); 140 | this._autoplay(); 141 | } 142 | 143 | /** 144 | * Gets card class for a specific card index 145 | * @param {Number} index 146 | * @returns {String} 147 | */ 148 | _getCardClass = (index) => { 149 | const { children } = this.props; 150 | const { current_index } = this.state; 151 | 152 | if (current_index === null) return POSITION.HIDDEN; 153 | 154 | if (index === current_index) return POSITION.CURRENT; 155 | 156 | if (index === current_index + 1 157 | || (index === 0 && current_index === React.Children.count(children) - 1)) { 158 | return POSITION.NEXT; 159 | } 160 | 161 | if (index === current_index - 1 162 | || (index === React.Children.count(children) - 1 && current_index === 0)) { 163 | return POSITION.PREV; 164 | } 165 | 166 | return POSITION.HIDDEN; 167 | } 168 | 169 | /** 170 | * Changes current_index state 171 | * @param {String} position 172 | */ 173 | _cardOnClick = (position) => { 174 | const { children, autoplay } = this.props; 175 | const { current_index } = this.state; 176 | 177 | if (autoplay) this._resetInterval(); 178 | 179 | if (position === POSITION.NEXT) { 180 | if (current_index === React.Children.count(children) - 1) { 181 | this.setState({ current_index: 0 }, this.props.afterChange); 182 | } 183 | else this.setState({ current_index: current_index + 1 }, this.props.afterChange); 184 | } 185 | 186 | else if (position === POSITION.PREV) { 187 | if (current_index === 0) { 188 | this.setState({ current_index: React.Children.count(children) - 1 }, this.props.afterChange); 189 | } 190 | else this.setState({ current_index: current_index - 1 }, this.props.afterChange); 191 | } 192 | } 193 | 194 | /** 195 | * @returns {React.Node} 196 | */ 197 | ChildComponents = () => { 198 | const { alignment, spread, disable_box_shadow } = this.props; 199 | 200 | return React.Children.map( 201 | this.props.children, (child, index) => { 202 | 203 | const position = this._getCardClass(index); 204 | 205 | return ( 206 |
this._cardOnClick(position) } 209 | style={{ 210 | ...STYLES.CARD, 211 | opacity: getOpacity(position), 212 | zIndex: getZIndex(position), 213 | transform: getTransform(position, alignment, spread), 214 | boxShadow: getBoxShadow(position, alignment, disable_box_shadow), 215 | cursor: getCursor(position, alignment), 216 | }} 217 | > 218 | { child } 219 |
220 | ); 221 | }); 222 | } 223 | 224 | render() { 225 | return ( 226 |
227 | 228 |
229 | ); 230 | } 231 | } 232 | 233 | export default Cards; -------------------------------------------------------------------------------- /src/constants.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Card position 3 | * @returns {Object} 4 | */ 5 | export const POSITION = { 6 | 'PREV': 'prev', 7 | 'NEXT': 'next', 8 | 'CURRENT': 'current', 9 | 'HIDDEN': 'hidden', 10 | }; 11 | 12 | /** 13 | * Cards alignment 14 | * @returns {Object} 15 | */ 16 | export const ALIGNMENT ={ 17 | 'HORIZONTAL': 'horizontal', 18 | 'VERTICAL': 'vertical', 19 | }; 20 | 21 | /** 22 | * Cards alignment 23 | * @returns {Object} 24 | */ 25 | export const SPREAD ={ 26 | 'NARROW': 'narrow', 27 | 'MEDIUM': 'medium', 28 | 'WIDE': 'wide' 29 | }; -------------------------------------------------------------------------------- /src/styles.js: -------------------------------------------------------------------------------- 1 | import { POSITION, ALIGNMENT, SPREAD } from './constants'; 2 | 3 | 4 | export const STYLES = { 5 | CONTAINER: { 6 | position: 'relative', 7 | width: '100%', 8 | height: '100%', 9 | margin: 0, 10 | padding: 0, 11 | }, 12 | CARD: { 13 | position: 'absolute', 14 | left: '50%', 15 | top: '50%', 16 | transition: 'all 0.6s', 17 | } 18 | }; 19 | 20 | /** 21 | * @param {String} position 22 | * @returns {Number} 23 | */ 24 | export function getOpacity(position) { 25 | if (position === POSITION.HIDDEN) return 0; 26 | return 1; 27 | } 28 | 29 | /** 30 | * @param {String} position 31 | * @returns {Number} 32 | */ 33 | export function getZIndex(position) { 34 | if (position === POSITION.HIDDEN) return 0; 35 | if (position === POSITION.CURRENT) return 2; 36 | return 1; 37 | } 38 | 39 | /** 40 | * @param {String} position 41 | * @returns {String} 42 | */ 43 | export function getTransform(position, alignment, spread) { 44 | const { prev, next } = _getTranslationDistances(spread); 45 | 46 | if (alignment === ALIGNMENT.HORIZONTAL) { 47 | if (position === POSITION.PREV) return `translate(${ prev }, -50%) scale(0.82)`; 48 | if (position === POSITION.NEXT) return `translate(${ next }, -50%) scale(0.82)`; 49 | } 50 | if (alignment === ALIGNMENT.VERTICAL) { 51 | if (position === POSITION.PREV) return `translate(-50%, ${ prev }) scale(0.82)`; 52 | if (position === POSITION.NEXT) return `translate(-50%, ${ next }) scale(0.82)`; 53 | } 54 | if (position === POSITION.HIDDEN) return `translate(-50%, -50%) scale(0.5)`; 55 | 56 | return 'translate(-50%, -50%)'; 57 | } 58 | 59 | /** 60 | * @param {String} position 61 | * @returns {String} 62 | */ 63 | export function getBoxShadow(position, alignment, disable_box_shadow) { 64 | if (!disable_box_shadow && position === POSITION.CURRENT) { 65 | if (alignment === ALIGNMENT.HORIZONTAL) { 66 | return '30px 0px 20px -20px rgba(0, 0, 0, .4), -30px 0px 20px -20px rgba(0, 0, 0, .4)'; 67 | } 68 | if (alignment === ALIGNMENT.VERTICAL) { 69 | return '0px 30px 20px -20px rgba(0, 0, 0, .4), 0px -30px 20px -20px rgba(0, 0, 0, .4)'; 70 | 71 | } 72 | } 73 | return 'unset'; 74 | } 75 | 76 | /** 77 | * @param {String} position 78 | * @returns {String} 79 | */ 80 | export function getCursor(position, alignment) { 81 | if (position === POSITION.NEXT) { 82 | if (alignment === ALIGNMENT.HORIZONTAL) return 'e-resize'; 83 | if (alignment === ALIGNMENT.VERTICAL) return 's-resize'; 84 | } 85 | if (position === POSITION.PREV) { 86 | if (alignment === ALIGNMENT.HORIZONTAL) return 'w-resize'; 87 | if (alignment === ALIGNMENT.VERTICAL) return 'n-resize'; 88 | } 89 | return 'unset'; 90 | } 91 | 92 | /** 93 | * @param {String} spread 94 | * @returns {Object} 95 | */ 96 | function _getTranslationDistances(spread) { 97 | let prev, next; 98 | if (spread === SPREAD.MEDIUM) { 99 | prev = '-85%'; 100 | next = '-15%'; 101 | } 102 | else if (spread === SPREAD.NARROW) { 103 | prev = '-75%'; 104 | next = '-25%'; 105 | } 106 | else if (spread === SPREAD.WIDE) { 107 | prev = '-95%'; 108 | next = '-5%'; 109 | } 110 | 111 | return { prev, next }; 112 | } -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | 3 | module.exports = { 4 | mode: 'production', 5 | entry: `${ __dirname }/src/Cards.jsx`, 6 | output: { 7 | path: path.resolve(__dirname, 'build'), 8 | filename: 'index.js', 9 | libraryTarget: 'commonjs2' 10 | }, 11 | module: { 12 | rules: [ 13 | { 14 | test: /\.jsx?$/, 15 | include: path.resolve(__dirname, 'src'), 16 | exclude: /(node_modules|build)/, 17 | use: 'babel-loader' 18 | } 19 | ] 20 | }, 21 | externals: { 22 | 'react': 'commonjs react' 23 | } 24 | }; --------------------------------------------------------------------------------