Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding aria-label to crisp link #3

Merged
merged 9 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
gatsby-ssr.js
gatsby-browser.js
!index.js

.DS_Store
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
websiteId: 'YOUR_CRISP_WEBSITE_ID',
enableDuringDevelop: false, // Optional. Disables Crisp Chat during gatsby develop. Defaults to true.
defer: true, // Optional. Sets the Crisp loading script to defer instead of async. Defaults to false.
accessible: false // Optional. Sets aria-label attribute on crisp plugin for screen readers. Defaults to false.
fraserisland marked this conversation as resolved.
Show resolved Hide resolved
},
}
'your',
Expand Down
32 changes: 32 additions & 0 deletions src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
exports.onInitialClientRender = (_, { accessible = true }) => {
if (accessible) {
const container = document.documentElement || document.body
const config = { attributes: true, childList: true, subtree: true }

let domObserver = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'childList') {
/* eslint-disable no-useless-computed-key */
const { [0]: addedNodes } = mutation.addedNodes

const crispContainerExists =
addedNodes &&
addedNodes.childNodes[0] &&
addedNodes.childNodes[0].className === 'crisp-kquevr'

if (crispContainerExists) {
const crispBoxElements = document.getElementsByClassName(
'crisp-kquevr',
)
let crispBox = crispBoxElements[0]
crispBox.setAttribute('aria-label', 'crisp chat box')

domObserver.disconnect()
}
}
})
})

domObserver.observe(container, config)
}
}