From 6fbb49d74e7d77148d57d5c8c9619ccced42c625 Mon Sep 17 00:00:00 2001 From: leigh-mil Date: Wed, 23 Jan 2019 10:58:13 -0500 Subject: [PATCH] Add event listener for focus so elements hidden behind the footer will scroll to the center of the screen --- js/index.js | 11 +++++++++++ js/lib/viewport.js | 15 +++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 js/lib/viewport.js diff --git a/js/index.js b/js/index.js index 455c89d7..68b6a6e4 100644 --- a/js/index.js +++ b/js/index.js @@ -1,5 +1,7 @@ import 'svg-innerhtml' import 'babel-polyfill' +import ally from 'ally.js' + import classes from '../styles/atat.scss' import Vue from 'vue/dist/vue' @@ -28,6 +30,7 @@ import MembersList from './components/members_list' import LocalDatetime from './components/local_datetime' import RequestsList from './components/requests_list' import ConfirmationPopover from './components/confirmation_popover' +import {isNotInVerticalViewport} from './lib/viewport' Vue.config.productionTip = false @@ -77,6 +80,14 @@ const app = new Vue({ const modal = modalOpen.getAttribute("data-modal") this.openModal(modal) } + + ally.query.focusable().forEach( function(el) { + el.addEventListener('focus', function(){ + if (isNotInVerticalViewport(el)) { + el.scrollIntoView({block: 'center'}) + } + }) + }) }, delimiters: ['!{', '}'] }) diff --git a/js/lib/viewport.js b/js/lib/viewport.js new file mode 100644 index 00000000..afe5aa8d --- /dev/null +++ b/js/lib/viewport.js @@ -0,0 +1,15 @@ +export const isNotInVerticalViewport = (el) => { + const bounds = el.getBoundingClientRect() + + if (bounds.top < 0) { + return true + } + + if (bounds.bottom > ((window.innerHeight - 50))) { + // 50 is a magic number to offset for the sticky footer + // see variables.scss for $footer-height + return true + } + + return false +}