refactor: refactor parent focus styles (#1036)

This commit is contained in:
Nolan Lawson 2019-02-23 12:50:56 -08:00 committed by GitHub
parent 56162c7a69
commit 8c37a7cc02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 20 deletions

View File

@ -9,9 +9,7 @@
aria-posinset={index}
aria-setsize={length}
aria-label={ariaLabel}
on:focus="onFocus()"
on:blur="onBlur()"
ref:article
on:applyFocusStylesToParent="noop()"
>
<StatusHeader {notification} {notificationId} {status} {statusId} {timelineType}
{account} {accountId} {uuid} isStatusInNotification="true" />
@ -48,6 +46,8 @@
import { goto } from '../../../../__sapper__/client'
import { composeNewStatusMentioning } from '../../_actions/mention'
import { classname } from '../../_utils/classname'
import { applyFocusStylesToParent } from '../../_utils/events'
import noop from 'lodash-es/noop'
export default {
components: {
@ -80,6 +80,7 @@
))
},
methods: {
noop,
openAuthorProfile () {
let { accountId } = this.get()
goto(`/accounts/${accountId}`)
@ -87,14 +88,10 @@
async mentionAuthor () {
let { account } = this.get()
await composeNewStatusMentioning(account)
},
// apply focus styles to parent rather than article because it shows up better
onFocus () {
this.refs.article.parentElement.classList.toggle('focus', true)
},
onBlur () {
this.refs.article.parentElement.classList.toggle('focus', false)
}
},
events: {
applyFocusStylesToParent
}
}
</script>

View File

@ -5,9 +5,7 @@
aria-setsize={length}
aria-label={ariaLabel}
on:recalculateHeight
on:focus="onFocus()"
on:blur="onBlur()"
ref:article
on:applyFocusStylesToParent="noop()"
>
{#if showHeader}
<StatusHeader {...params} />
@ -136,6 +134,8 @@
import { LONG_POST_LENGTH, LONG_POST_TEXT } from '../../_static/statuses'
import { absoluteDateFormatter } from '../../_utils/formatters'
import { composeNewStatusMentioning } from '../../_actions/mention'
import { applyFocusStylesToParent } from '../../_utils/events'
import noop from 'lodash-es/noop'
const INPUT_TAGS = new Set(['a', 'button', 'input', 'textarea'])
const isUserInputElement = node => INPUT_TAGS.has(node.localName)
@ -181,6 +181,7 @@
}),
store: () => store,
methods: {
noop,
onClickOrKeydown (e) {
let { type, keyCode, target } = e
@ -214,13 +215,6 @@
async mentionAuthor () {
let { accountForShortcut } = this.get()
await composeNewStatusMentioning(accountForShortcut)
},
// apply focus styles to parent rather than article because it shows up better
onFocus () {
this.refs.article.parentElement.classList.toggle('focus', true)
},
onBlur () {
this.refs.article.parentElement.classList.toggle('focus', false)
}
},
computed: {
@ -324,6 +318,9 @@
absoluteFormattedDate,
shortcutScope
})
},
events: {
applyFocusStylesToParent
}
}
</script>

View File

@ -49,3 +49,25 @@ export function selectionChange (node, callback) {
}
}
}
// in some cases we apply focus styles to parent rather
// than the node itself because it shows up better
export function applyFocusStylesToParent (node) {
function onFocus () {
node.parentElement.classList.toggle('focus', true)
}
function onBlur () {
node.parentElement.classList.toggle('focus', false)
}
node.addEventListener('focus', onFocus)
node.addEventListener('blur', onBlur)
return {
destroy () {
node.removeEventListener('focus', onFocus)
node.removeEventListener('blur', onBlur)
}
}
}