Skip to content

Commit

Permalink
[feat] add an optional defer property (#21)
Browse files Browse the repository at this point in the history
Thanks for your contribution!
  • Loading branch information
xtina-starr authored Mar 17, 2021
1 parent 03a0590 commit ce7ca01
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component } from "react";

export interface IZendeskProps {
defer?: boolean;
zendeskKey: string;
[objKey: string]: any;
}
Expand Down
15 changes: 10 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ export default class Zendesk extends Component {
}
}

insertScript (zendeskKey) {
insertScript (zendeskKey, defer) {
const script = document.createElement('script')
script.async = true
if (defer) {
script.defer = true
} else {
script.async = true
}
script.id = 'ze-snippet'
script.src = `https://static.zdassets.com/ekr/snippet.js?key=${zendeskKey}`
script.addEventListener('load', this.onScriptLoaded);
Expand All @@ -41,8 +45,8 @@ export default class Zendesk extends Component {

componentDidMount() {
if (canUseDOM && !window.zE) {
const {zendeskKey, ...other} = this.props
this.insertScript(zendeskKey)
const {defer, zendeskKey, ...other} = this.props
this.insertScript(zendeskKey, defer)
window.zESettings = other
}
}
Expand All @@ -61,5 +65,6 @@ export default class Zendesk extends Component {
}

Zendesk.propTypes = {
zendeskKey: PropTypes.string.isRequired
zendeskKey: PropTypes.string.isRequired,
defer: PropTypes.boolean
}

0 comments on commit ce7ca01

Please sign in to comment.