[
  {
    "path": ".eslintrc",
    "content": "{\n  \"extends\": [\"airbnb-base\", \"prettier\"],\n  \"plugins\": [\"prettier\"],\n  \"rules\": {\n    \"quotes\": [1, \"single\"],\n    \"no-tabs\": 0,\n    \"indent\": [\"error\", \"tab\"]\n  }\n}\n"
  },
  {
    "path": ".gitignore",
    "content": ".DS_Store\nnode_modules\npackage-lock.json"
  },
  {
    "path": ".nojekyll",
    "content": ""
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2022 Russell Samora\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE."
  },
  {
    "path": "README.md",
    "content": "# enter-view\n\nDependency-free JavaScript library to detect when element enters into view. [See demo](https://russellsamora.github.io/enter-view/). It uses requestAnimationFrame in favor of scroll events for less jank. Less than 1kb minified + gzipped.\n\n## Installation\nOld school (exposes the `enterView` global):\n\n```html\n<script src=\"https://unpkg.com/enter-view\"></script>\n```\n\nNew school:\n\n```sh\nnpm install enter-view --save\n```\n\nAnd then import/require it:\n\n```js\nimport enterView from 'enter-view'; // or...\nconst enterView = require('enterView');\n```\n\n## Usage\n\n```\nenterView({\n\tselector: '.class-name',\n\tenter: function(el) {\n\t\tel.classList.add('entered');\n\t}\n});\n```\n\n```\nenterView({\n\tselector: '.class-name',\n\tenter: function(el) {\n\t\tel.classList.add('entered');\n\t},\n\texit: function(el) {\n\t\tel.classList.remove('entered');\n\t},\n\tprogress: function(el, progress) {\n\t\tel.style.opacity = progress;\n\t},\n\toffset: 0.5, // enter at middle of viewport\n\tonce: true, // trigger just once\n});\n```\n\n## Options\n\n#### selector: [string or array of elements] _required_\n\nTakes a class, id, or array of dom elements.\n\n#### enter: [function] _optional_\n\nCallback function that returns the element that was entered.\n\n#### exit: [function] _optional_\n\nCallback function that returns the element that was exited.\n\n#### progress: [function] _optional_\n\nCallback function that returns the element that was progressed through, and a value between 0 and 1 of how far through the element progress has been made.\n\n#### offset: [number] _optional_ (defaults to 0)\n\nA value from 0 to 1 of how far from the bottom of the viewport to offset the trigger by. 0 = top of element crosses bottom of viewport (enters screen from bottom), 1 = top of element crosses top of viewport (exits screen top).\n\n#### once: [boolean] _optional_ (defaults to false)\n\nWhether or not to trigger the callback just once.\n\n## Contributors\n\n- [Russell Samora](https://github.com/russellsamora)\n- [Jonathan Soma](https://github.com/jsoma)\n"
  },
  {
    "path": "enter-view.js",
    "content": "/*\n * enter-view.js is library\n */\n\n(function(factory) {\n  if (typeof define === 'function' && define.amd) {\n    define(factory);\n  } else if (typeof module !== 'undefined' && module.exports) {\n    module.exports = factory();\n  } else {\n    window.enterView = factory.call(this);\n  }\n})(() => {\n  const lib = ({\n    selector,\n    enter = () => {},\n    exit = () => {},\n    progress = () => {},\n    offset = 0,\n    once = false\n  }) => {\n    let raf = null;\n    let ticking = false;\n    let elements = [];\n    let height = 0;\n\n    function setupRaf() {\n      raf =\n        window.requestAnimationFrame ||\n        window.webkitRequestAnimationFrame ||\n        window.mozRequestAnimationFrame ||\n        window.msRequestAnimationFrame ||\n        function(callback) {\n          return setTimeout(callback, 1000 / 60);\n        };\n    }\n\n    function getOffsetHeight() {\n      if (offset && typeof offset === 'number') {\n        const fraction = Math.min(Math.max(0, offset), 1);\n        return height - fraction * height;\n      }\n      return height;\n    }\n\n    function updateHeight() {\n      const cH = document.documentElement.clientHeight;\n      const wH = window.innerHeight || 0;\n      height = Math.max(cH, wH);\n    }\n\n    function updateScroll() {\n      ticking = false;\n      const targetFromTop = getOffsetHeight();\n\n      elements = elements.filter(el => {\n        const { top, bottom, height } = el.getBoundingClientRect();\n        const entered = top < targetFromTop;\n        const exited = bottom < targetFromTop;\n\n        // enter + exit\n        if (entered && !el.__ev_entered) {\n          enter(el);\n          el.__ev_progress = 0;\n          progress(el, el.__ev_progress);\n          if (once) return false;\n        } else if (!entered && el.__ev_entered) {\n          el.__ev_progress = 0;\n          progress(el, el.__ev_progress);\n          exit(el);\n        }\n\n        // progress\n        if (entered && !exited) {\n          const delta = (targetFromTop - top) / height;\n          el.__ev_progress = Math.min(1, Math.max(0, delta));\n          progress(el, el.__ev_progress);\n        }\n\n        if (entered && exited && el.__ev_progress !== 1) {\n          el.__ev_progress = 1;\n          progress(el, el.__ev_progress);\n        }\n\n        el.__ev_entered = entered;\n        return true;\n      });\n\n      if (!elements.length) {\n\t\t\t\twindow.removeEventListener('scroll', onScroll, true);\n\t\t\t\twindow.removeEventListener('resize', onResize, true);\n\t\t\t\twindow.removeEventListener('load', onLoad, true);\n      }\n    }\n\n    function onScroll() {\n      if (!ticking) {\n        ticking = true;\n        raf(updateScroll);\n      }\n    }\n\n    function onResize() {\n      updateHeight();\n      updateScroll();\n    }\n\n    function onLoad() {\n      updateHeight();\n      updateScroll();\n    }\n\n    function selectionToArray(selection) {\n      const len = selection.length;\n      const result = [];\n      for (let i = 0; i < len; i += 1) {\n        result.push(selection[i]);\n      }\n      return result;\n    }\n\n    function selectAll(selector, parent = document) {\n      if (typeof selector === 'string') {\n        return selectionToArray(parent.querySelectorAll(selector));\n      } else if (selector instanceof NodeList) {\n        return selectionToArray(selector);\n      } else if (selector instanceof Array) {\n        return selector;\n      }\n    }\n\n    function setupElements() {\n      elements = selectAll(selector);\n    }\n\n    function setupEvents() {\n      window.addEventListener('resize', onResize, true);\n      window.addEventListener('scroll', onScroll, true);\n      window.addEventListener('load', onLoad, true);\n      onResize();\n    }\n\n    function init() {\n      if (!selector) {\n        console.error('must pass a selector');\n        return false;\n      }\n\n      setupElements();\n\n      if (!elements || !elements.length) {\n        console.error('no selector elements found');\n        return false;\n      }\n\n      setupRaf();\n      setupEvents();\n      updateScroll();\n    }\n\n    init();\n  };\n\n  return lib;\n});\n"
  },
  {
    "path": "index.html",
    "content": "<!doctype html>\n<html lang='en'>\n\t<head>\n\t    <title>enter-view.js</title>\n        <meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no'>\n        <meta charset='utf-8'>\n        <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>\n        <style>\n        \t* {\n        \t\tbox-sizing: border-box;\n        \t}\n        \thtml {\n        \t\tmargin: 0;\n        \t\tpadding: 0;\n        \t}\n        \tbody {\n        \t\tmargin: 0;\n        \t\tpadding: 0;\n        \t\twidth: 100%;\n        \t\tfont-size: 17px;\n        \t\tfont-family: Helvetica, sans-serif;\n        \t\tfont-weight: 300;\n        \t}\n        \tp {\n        \t\tcolor: #333;\n        \t\tfont-size: 1.2em;\n        \t\tline-height: 1.6;\n        \t\tmax-width: 40em;\n        \t\tpadding: 1em;\n        \t\tmargin: 0 auto;\n        \t}\n        \th1 {\n        \t\tfont-weight: 300;\n        \t\tmargin: 1em;\n        \t\ttext-align: center;\n        \t}\n        \tp.description {\n        \t\tfont-style: italic;\n        \t\tfont-size: 1em;\n        \t\ttext-align: center;\n        \t\tmax-width: 100%;\n        \t}\n        \tarticle {\n        \t\tpadding: 1em;\n        \t}\n        \t.prompt {\n        \t\ttext-align: center;\n        \t\theight: 90vh;\n        \t}\n        \t.explain {\n        \t\ttext-align: center;\n        \t\tmargin-top: 50vh;\n        \t}\n\n        \t.blocks {\n        \t\tmargin-bottom: 75vh;\n        \t}\n\n        \t.block {\n        \t\twidth: 100%;\n        \t\tmargin-top: 1em;\n        \t\tmargin-bottom: 1em;\n        \t\theight: 25vh;\n        \t\tbackground: #ccc;\n        \t\ttext-align: center;\n\t\t\t\t\t\ttransform-style: preserve-3d;\n  \t\t\t\ttransition: 1.5s background;\n        \t}\n\n        \t.block p {\n        \t\tposition: relative;\n  \t\t\t\ttop: 50%;\n  \t\t\t\ttransform: translateY(-50%);\n  \t\t\t\tfont-size: 2em;\n        \t}\n\n        \t.block.entered {\n        \t\tbackground: #fff888;\n        \t}\n\n        \t.progress {\n        \t\tfont-size: 0.75em;\n        \t}\n\n\t\t\t\t\tpre {\n\t\t\t\t\t\tmax-width: 35rem;\n\t\t\t\t\t\tmargin: 0 auto;\n\t\t\t\t\t\tbackground-color: #eee;\n\t\t\t\t\t\tpadding: 1rem;\n\t\t\t\t\t}\n\n\n        </style>\n\t</head>\n\t<body>\n\t\t<article>\n\t\t\t<h1>Enter-view.js Demo</h1>\n\t\t\t<p class='description'>Dependency-free JavaScript <a href='https://github.com/russellsamora/enter-view'>library</a> to detect when element enters into view</p>\n\t\t\t<p class='prompt'>Start scrolling to see examples.</p>\n\t\t\t<div class='blocks'>\n\t\t\t\t<p class='explain'>Default (no offset)</p>\n\t\t\t\t<code>\n\t\t\t\t\t<pre>\nenterView({\n selector: '.block-a',\n enter: function(el) {\n  el.classList.add('entered');\n }\n});\n\t\t\t\t\t</pre>\n\t\t\t\t</code>\n\t\t\t\t<div class='block block-a'><p>.block-a</p></div>\n\n\t\t\t\t<p class='explain'>50% offset</p>\n\t\t\t\t<code>\n\t\t\t\t\t<pre>\nenterView({\n selector: '.block-b',\n offset: 0.5,\n enter: function(el) {\n  el.classList.add('entered');\n }\n});\n\t\t\t\t\t</pre>\n\t\t\t\t</code>\n\t\t\t\t<div class='block block-b'><p>.block-b</p></div>\n\n\t\t\t\t<p class='explain'>75% offset, trigger every time</p>\n\t\t\t\t<code>\n\t\t\t\t\t<pre>\nvar count = 0;\nenterView({\n selector: '.block-c',\n offset: '0.75,\n enter: function(el) {\n  el.classList.add('entered');\n  count += 1;\n  el.querySelector('span').innerText = count;\n }\n});\n\t\t\t\t\t</pre>\n\t\t\t\t</code>\n\t\t\t\t<div class='block block-c'><p>.block-c (<span>0</span>)</p></div>\n\n\t\t\t\t<p class='explain'>Multiple elements, 50% offset</p>\n\t\t\t\t<code>\n\t\t\t\t\t<pre>\nenterView({\n selector: '.block-d',\n offset: 0.5,\n enter: function(el) {\n  el.classList.add('entered');\n }\n});\n\t\t\t\t\t</pre>\n\t\t\t\t</code>\n\t\t\t\t<div class='block block-d'><p>.block-d</p></div>\n\t\t\t\t<div class='block block-d'><p>.block-d</p></div>\n\t\t\t\t<div class='block block-d'><p>.block-d</p></div>\n\n\t\t\t<p class='explain'>Exit element, 50% offset</p>\n\t\t\t<code>\n\t\t\t\t<pre>\nenterView({\n selector: '.block-e',\n offset: 0.5,\n enter: function(el) {\n  el.classList.add('entered');\n },\n exit: function(el) {\n  el.classList.remove('entered');\n },\n});\n\t\t\t\t</pre>\n\t\t\t</code>\n\t\t\t<div class='block block-e'><p>.block-e</p></div>\n\n\t\t\t<p class='explain'>Progress increments, 50% offset</p>\n\t\t\t<code>\n\t\t\t\t<pre>\nenterView({\n selector: '.block-f',\n offset: 0.5,\n enter: function(el) {\n  el.classList.add('entered');\n },\n progress: function(el, progress) {\n  var p = el.querySelector('.progress')\n  p.innerText = progress.toFixed(2);\n }\n});\n\t\t\t\t</pre>\n\t\t\t</code>\n\t\t\t<div class='block block-f'><p>.block-f <span class='progress'></span></p></div>\n\t\t</div>\n\t\t</article>\n\t\t<script src='enter-view.min.js'></script>\n\t\t<script>\n\t\t\tenterView({\n\t\t\t\tselector: '.block-a',\n\t\t\t\tenter: function(el) {\n\t\t\t\t\tconsole.log(\"entered\", el);\n\t\t\t\t\tel.classList.add('entered');\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tenterView({\n\t\t\t\tselector: '.block-b',\n\t\t\t\toffset: 0.5,\n\t\t\t\tenter: function(el) {\n\t\t\t\t\tel.classList.add('entered');\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tvar count = 0;\n\t\t\tenterView({\n\t\t\t\tselector: '.block-c',\n\t\t\t\toffset: 0.75,\n\t\t\t\tenter: function(el) {\n\t\t\t\t\tel.classList.add('entered');\n\t\t\t\t\tcount += 1;\n\t\t\t\t\tel.querySelector('span').innerText = count;\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tenterView({\n\t\t\t\tselector: '.block-d',\n\t\t\t\toffset: 0.5,\n\t\t\t\tenter: function(el) {\n\t\t\t\t\tel.classList.add('entered');\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tenterView({\n\t\t\t\tselector: '.block-e',\n\t\t\t\toffset: 0.5,\n\t\t\t\tenter: function (el) {\n\t\t\t\t\tel.classList.add('entered');\n\t\t\t\t},\n\t\t\t\texit: function (el) {\n\t\t\t\t\tel.classList.remove('entered');\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tenterView({\n\t\t\t\tselector: '.block-f',\n\t\t\t\toffset: 0.5,\n\t\t\t\tenter: function(el) {\n\t\t\t\t\tel.classList.add('entered');\n\t\t\t\t},\n\t\t\t\tprogress: function(el, progress) {\n\t\t\t\t\tvar p = el.querySelector('.progress')\n\t\t\t\t\tp.innerText = progress.toFixed(2);\n\t\t\t\t}\n\t\t\t});\n\t\t</script>\n\t</body>\n</html>"
  },
  {
    "path": "package.json",
    "content": "{\n\t\"name\": \"enter-view\",\n\t\"version\": \"2.0.1\",\n\t\"description\": \"Dependency-free JavaScript library to detect when element enters into view\",\n\t\"main\": \"enter-view.min.js\",\n\t\"scripts\": {\n\t\t\"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\n\t},\n\t\"keywords\": [\n\t\t\"enter\",\n\t\t\"view\",\n\t\t\"viewport\",\n\t\t\"scroll\",\n\t\t\"into\",\n\t\t\"element\"\n\t],\n\t\"author\": \"Russell Samora\",\n\t\"license\": \"MIT\",\n\t\"devDependencies\": {\n\t\t\"eslint\": \"^2.5.3\",\n\t\t\"eslint-config-airbnb\": \"^6.2.0\",\n\t\t\"eslint-plugin-react\": \"^4.2.3\",\n\t\t\"terser\": \"^5.5.1\"\n\t},\n\t\"dependencies\": {}\n}"
  }
]