Skip to content

Commit

Permalink
refactor(ui-tooltip): fix tooltips not read in NVDA
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrTopi authored and matyasf committed Aug 13, 2024
1 parent 6b78081 commit a25bcb2
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions packages/ui-tooltip/src/Tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
*/

/** @jsx jsx */
import { Component, ReactNode } from 'react'

import { onlyText } from 'react-children-utilities'
import { Children, Component, ReactNode, isValidElement } from 'react'
import type { ReactElement } from 'react'

import {
getElementType,
Expand Down Expand Up @@ -73,6 +72,55 @@ class Tooltip extends Component<TooltipProps, TooltipState> {

ref: Element | null = null

// TEMP - START

hasChildren = (
element: ReactNode
): element is ReactElement<{ children: ReactNode | ReactNode[] }> =>
isValidElement<{ children?: ReactNode[] }>(element) &&
Boolean(element.props.children)

childToString = (child?: ReactNode): string => {
if (
typeof child === 'undefined' ||
child === null ||
typeof child === 'boolean'
) {
return ''
}

if (JSON.stringify(child) === '{}') {
return ''
}

return (child as number | string).toString()
}

onlyText = (children: ReactNode | ReactNode[]): string => {
if (!(children instanceof Array) && !isValidElement(children)) {
return this.childToString(children)
}

return Children.toArray(children).reduce(
(text: string, child: ReactNode): string => {
let newText = ''

if (this.hasChildren(child)) {
newText = this.onlyText(child.props.children)
} else if (isValidElement(child)) {
newText = ''
} else {
newText = this.childToString(child)
}

return text.concat(newText)
},
''
)
}

// TEMP - END

handleRef = (el: Element | null) => {
this.ref = el
if (typeof this.props.elementRef === 'function') {
Expand Down Expand Up @@ -107,7 +155,7 @@ class Tooltip extends Component<TooltipProps, TooltipState> {
renderTrigger() {
const { children, as } = this.props as TooltipProps
const { hasFocus } = this.state
const toolTipRawText = onlyText(this.props.renderTip as any)
const toolTipRawText = this.onlyText(this.props.renderTip as any)
const triggerProps = {
'aria-describedby': this._id,
'aria-label': toolTipRawText
Expand Down

0 comments on commit a25bcb2

Please sign in to comment.