( props: DOMProps & AriaLabelingProps, defaultLabel?: string )
| 20 | * @param defaultLabel - Default value for aria-label when not present. |
| 21 | */ |
| 22 | export function useLabels( |
| 23 | props: DOMProps & AriaLabelingProps, |
| 24 | defaultLabel?: string |
| 25 | ): DOMProps & AriaLabelingProps { |
| 26 | let {id, 'aria-label': label, 'aria-labelledby': labelledBy} = props; |
| 27 | |
| 28 | // If there is both an aria-label and aria-labelledby, |
| 29 | // combine them by pointing to the element itself. |
| 30 | id = useId(id); |
| 31 | if (labelledBy && label) { |
| 32 | let ids = new Set([id, ...labelledBy.trim().split(/\s+/)]); |
| 33 | labelledBy = [...ids].join(' '); |
| 34 | } else if (labelledBy) { |
| 35 | labelledBy = labelledBy.trim().split(/\s+/).join(' '); |
| 36 | } |
| 37 | |
| 38 | // If no labels are provided, use the default |
| 39 | if (!label && !labelledBy && defaultLabel) { |
| 40 | label = defaultLabel; |
| 41 | } |
| 42 | |
| 43 | return { |
| 44 | id, |
| 45 | 'aria-label': label, |
| 46 | 'aria-labelledby': labelledBy |
| 47 | }; |
| 48 | } |
no test coverage detected