From 42e466f3c277429f199f60c864b3fde8effa5180 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Thu, 21 Feb 2019 23:50:27 -0800 Subject: [PATCH] fix: j/k shortcuts correctly set active element (#1028) fixes #1018 --- src/routes/_components/list/ListItem.html | 8 --- src/routes/_components/list/listStore.js | 1 - .../shortcut/ScrollListShortcuts.html | 21 ++++++-- .../_components/status/Notification.html | 15 +++--- src/routes/_components/status/Status.html | 8 +-- .../timeline/NotificationVirtualListItem.html | 1 - .../timeline/StatusVirtualListItem.html | 1 - .../_components/virtualList/VirtualList.html | 6 ++- .../virtualList/VirtualListItem.html | 5 +- .../virtualList/virtualListStore.js | 1 - tests/spec/024-shortcuts-navigation.js | 4 +- tests/spec/025-shortcuts-status.js | 49 +++++++++---------- tests/spec/026-shortcuts-notification.js | 16 +++--- tests/utils.js | 11 +++++ 14 files changed, 79 insertions(+), 68 deletions(-) diff --git a/src/routes/_components/list/ListItem.html b/src/routes/_components/list/ListItem.html index bea70643..62c3c77c 100644 --- a/src/routes/_components/list/ListItem.html +++ b/src/routes/_components/list/ListItem.html @@ -5,13 +5,5 @@ virtualIndex={index} virtualLength={length} virtualKey={key} - active={active} /> - diff --git a/src/routes/_components/list/listStore.js b/src/routes/_components/list/listStore.js index 2ee464a6..0668c8de 100644 --- a/src/routes/_components/list/listStore.js +++ b/src/routes/_components/list/listStore.js @@ -8,7 +8,6 @@ class ListStore extends RealmStore { const listStore = new ListStore() -listStore.computeForRealm('activeItem', null) listStore.computeForRealm('intersectionStates', {}) if (process.browser && process.env.NODE_ENV !== 'production') { diff --git a/src/routes/_components/shortcut/ScrollListShortcuts.html b/src/routes/_components/shortcut/ScrollListShortcuts.html index 2ded3188..f4b23463 100644 --- a/src/routes/_components/shortcut/ScrollListShortcuts.html +++ b/src/routes/_components/shortcut/ScrollListShortcuts.html @@ -15,8 +15,10 @@ export default { data: () => ({ scope: 'global', - itemToKey: (item) => item, - keyToElement: (key) => document.getElementById('list-item-' + key), + itemToKey: (item) => item.key, + keyToElement: (key) => { + return document.querySelector(`[shortcut-key=${JSON.stringify(key)}]`) + }, activeItemChangeTime: 0 }), computed: { @@ -94,7 +96,11 @@ } }, checkActiveItem (timeStamp) { - let { activeItem } = this.store.get() + let activeElement = document.activeElement + if (!activeElement) { + return null + } + let activeItem = activeElement.getAttribute('shortcut-key') if (!activeItem) { return null } @@ -108,7 +114,14 @@ }, setActiveItem (key, timeStamp) { this.set({ activeItemChangeTime: timeStamp }) - this.store.setForRealm({ activeItem: key }) + let { keyToElement } = this.get() + try { + keyToElement(key).focus({ + preventScroll: true + }) + } catch (err) { + console.error(err) + } } } } diff --git a/src/routes/_components/status/Notification.html b/src/routes/_components/status/Notification.html index d2e938b0..b9f8be91 100644 --- a/src/routes/_components/status/Notification.html +++ b/src/routes/_components/status/Notification.html @@ -1,6 +1,6 @@ {#if status} {:else}
@@ -25,8 +26,9 @@ padding: 10px 20px; border-bottom: 1px solid var(--main-border); } - .notification-article.status-active { + .notification-article:focus { background-color: var(--status-active-background); + outline: none; } @media (max-width: 767px) { .notification-article { @@ -53,7 +55,6 @@ Shortcut }, data: () => ({ - active: false, shortcutScope: null }), store: () => store, @@ -69,11 +70,11 @@ ariaLabel: ({ status, account, $omitEmojiInDisplayNames }) => ( !status && `${getAccountAccessibleName(account, $omitEmojiInDisplayNames)} followed you, @${account.acct}` ), - className: ({ active, $underlineLinks }) => (classname( + className: ({ $underlineLinks }) => (classname( 'notification-article', - active && 'status-active', $underlineLinks && 'underline-links' - )) + )), + focusKey: ({ uuid }) => `notification-follower-${uuid}` }, methods: { openAuthorProfile () { diff --git a/src/routes/_components/status/Status.html b/src/routes/_components/status/Status.html index 57376c2f..dbf90575 100644 --- a/src/routes/_components/status/Status.html +++ b/src/routes/_components/status/Status.html @@ -2,6 +2,7 @@ tabindex="0" delegate-key={delegateKey} focus-key={delegateKey} + shortcut-key={shortcutScope} aria-posinset={index} aria-setsize={length} aria-label={ariaLabel} @@ -74,8 +75,9 @@ background-color: var(--status-direct-background); } - .status-article.status-active { + .status-article:focus { background-color: var(--status-active-background); + outline: none; } .status-article.status-in-own-thread { @@ -171,7 +173,6 @@ Shortcut }, data: () => ({ - active: false, notification: void 0, replyVisibility: void 0, contentPreloaded: false, @@ -272,12 +273,11 @@ status.reblog || timelineType === 'pinned' ), - className: ({ visibility, timelineType, isStatusInOwnThread, active, $underlineLinks, $disableTapOnStatus }) => (classname( + className: ({ visibility, timelineType, isStatusInOwnThread, $underlineLinks, $disableTapOnStatus }) => (classname( 'status-article', visibility === 'direct' && 'status-direct', timelineType !== 'search' && 'status-in-timeline', isStatusInOwnThread && 'status-in-own-thread', - active && 'status-active', $underlineLinks && 'underline-links', !$disableTapOnStatus && 'tap-on-status' )), diff --git a/src/routes/_components/timeline/NotificationVirtualListItem.html b/src/routes/_components/timeline/NotificationVirtualListItem.html index e42a00c5..d7ab0d34 100644 --- a/src/routes/_components/timeline/NotificationVirtualListItem.html +++ b/src/routes/_components/timeline/NotificationVirtualListItem.html @@ -6,7 +6,6 @@ shortcutScope={virtualKey} index={virtualIndex} length={virtualLength} - {active} on:recalculateHeight />