Skip to content

Instantly share code, notes, and snippets.

@srmagura
Last active February 27, 2018 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srmagura/b57c1ec54f523804662d79e463d7b168 to your computer and use it in GitHub Desktop.
Save srmagura/b57c1ec54f523804662d79e463d7b168 to your computer and use it in GitHub Desktop.
const isBrowser: () => boolean = () => typeof window !== 'undefined'
function isInternetExplorer() {
if (!isBrowser())
return false
var ua = window.navigator.userAgent
var msie = ua.indexOf("MSIE ")
if (msie > 0 || navigator.userAgent.match(/Trident.*rv\:11\./)) {
return true
}
return false
}
class HintFallback extends React.Component<{}, {}> {
componentDidMount() {
window.setInterval(this.addTitleAttributes, 1000)
}
addTitleAttributes = () => {
const attrName = 'data-tooltip'
const array = $('[' + attrName + ']').toArray()
for (const domEl of array) {
const el = $(domEl)
const text = el.attr(attrName)
if (text && text !== el.attr('title'))
el.attr('title', text)
}
}
render() {
return null
}
}
interface ILayoutProps extends React.Props<any> {
}
interface ILayoutState {
mounted: boolean
}
export class Layout extends React.Component<ILayoutProps, ILayoutState> {
state: ILayoutState = {
mounted: false,
}
componentDidMount() {
this.setState({ mounted: true })
}
render() {
const { mounted } = this.state
// can't render this server-side because of the IE conditional
let reactHintEl: React.ReactNode = null
if (mounted) {
reactHintEl =
<ReactHint events={{ hover: true }} position="top"
attribute="data-tooltip" />
// react-hint does not support IE.
if (isInternetExplorer()) {
reactHintEl = <HintFallback />
}
}
return (
<div>
{reactHintEl}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment